git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: Eric Sunshine <sunshine@sunshineco.com>
Cc: Junio C Hamano <gitster@pobox.com>,
	shejialuo <shejialuo@gmail.com>,
	AreaZR via GitGitGadget <gitgitgadget@gmail.com>,
	git@vger.kernel.org, AreaZR <gfunni234@gmail.com>,
	Seija Kijin <doremylover123@gmail.com>
Subject: Re: [PATCH] refs: exit early from the loop if it is not a main worktree
Date: Fri, 27 Dec 2024 15:34:13 +0100	[thread overview]
Message-ID: <Z2665YeBYXUYB12D@pks.im> (raw)
In-Reply-To: <CAPig+cTf=Lm_yNWQPq2SKfuROhSMMwNVXNmXhags43SRKonwxA@mail.gmail.com>

On Thu, Dec 19, 2024 at 12:54:55AM -0500, Eric Sunshine wrote:
> On Wed, Dec 18, 2024 at 8:10 PM Junio C Hamano <gitster@pobox.com> wrote:
> > Eric Sunshine <sunshine@sunshineco.com> writes:
> > > On Wed, Dec 18, 2024 at 8:30 AM shejialuo <shejialuo@gmail.com> 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.)

Overall I have to say that I found the worktree APIs to be quite
lacking. It's surprising that you don't even have a way to load a single
worktree, for example.

In any case, for this specific case I'm not aware of any other users of
the functionality, so there is no immediate need to lift it up into the
worktree subsystem.

Patrick

  parent reply	other threads:[~2024-12-27 14:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-18  2:20 [PATCH] refs: exit early from the loop if it is not a main worktree AreaZR via GitGitGadget
2024-12-18 13:31 ` shejialuo
2024-12-18 23:52   ` Eric Sunshine
2024-12-19  1:10     ` Junio C Hamano
2024-12-19  5:54       ` Eric Sunshine
2024-12-19  6:12         ` Junio C Hamano
2024-12-27 14:34         ` Patrick Steinhardt [this message]
2024-12-27 14:34     ` Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z2665YeBYXUYB12D@pks.im \
    --to=ps@pks.im \
    --cc=doremylover123@gmail.com \
    --cc=gfunni234@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=gitster@pobox.com \
    --cc=shejialuo@gmail.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).