git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 19, 2024, 01:10 UTC
Message-ID
<xmqqv7vgo4u7.fsf@gitster.g>
In-Reply-To
<CAPig+cQd=vc5rte47biFbR+w_DV2OhdCRpC2WH_dKsSi4wvZ2A@mail.gmail.com>
Eric Sunshine <sunshine@sunshineco.com> writes:
Show 17 quoted lines
> 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.
Thanks for a well-reasoned write-up.

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.

Previous: Eric SunshineNext: Eric Sunshine
Message 4 of 8 in “refs: exit early from the loop if it is not a main worktree”
  1. refs: exit early from the loop if it is not a main worktreeAreaZR via GitGitGadget, Dec 18, 2024
  2. shejialuoDec 18, 2024
  3. Eric SunshineDec 18, 2024
  4. Junio C HamanoDec 19, 2024
  5. Eric SunshineDec 19, 2024
  6. Junio C HamanoDec 19, 2024
  7. Patrick SteinhardtDec 27, 2024
  8. Patrick SteinhardtDec 27, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.