Re: [PATCH] refs: exit early from the loop if it is not a main worktree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Dec 18, 2024 at 8:10 PM Junio C Hamano <gitster@xxxxxxxxx> wrote:
> Eric Sunshine <sunshine@xxxxxxxxxxxxxx> writes:
> > On Wed, Dec 18, 2024 at 8:30 AM shejialuo <shejialuo@xxxxxxxxx> wrote:
> >> On Wed, Dec 18, 2024 at 02:20:45AM +0000, AreaZR via GitGitGadget wrote:
> >> >               if (is_main_worktree(worktrees[i]))
> >> >                       continue;
> >> >               ret = 1;
> >> > +             break;
> >>
> >> So, when we find a linked worktree, we just return the value. From my
> >> perspective, if we decide to optimize like this way, we could drop the
> >> loop because the first element of the result of `get_worktrees` is the
> >> main worktree. And we could just check whether the "worktrees[1]" is
> >> NULL to do above.
> >
> > You're correct. get_worktrees() guarantees that the main worktree (or
> > bare repository) is the first item in the list, so merely checking
> > whether `worktrees[1]` is non-NULL would be sufficient to answer
> > whether linked worktrees are present; no looping is required.
>
> Would many other callers potentially want to know if the repository
> has more than one worktree?  It looks to me that the has_worktrees()
> helper function in refs.c is a sign that the worktree API is missing
> a function.  Calling get_worktrees() to prepare a list of worktrees
> and then counting the result, only to see if there are more than
> one, sounds a bit wasteful if we need to do so too often.

If the need to answer this question does become common, then I can
imagine a function being added to the worktree API which tries to be
smart about it by only calling readdir() -- and validating a
.git/worktrees/<id>/ metainformation -- enough times to be able to
answer the question.

However, although I haven't audited the code, I suspect the question
"are there any linked worktrees" is rare, possibly only asked by
`refs.c`. And in that case, it is asked only at the start of a
refs-migration operation. Moreover, it appears that even that case of
asking the question is probably temporary, existing only until someone
extends the migration logic to work correctly in the presence of
worktrees. (I'm sure Patrick can shed more light on this, though.)





[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux