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
shejialuo <shejialuo@gmail.com>
Date
Dec 18, 2024, 13:31 UTC
Message-ID
<Z2LOpOxu0oAY0DW3@ArchLinux>
In-Reply-To
<pull.1848.git.git.1734488445457.gitgitgadget@gmail.com>
On Wed, Dec 18, 2024 at 02:20:45AM +0000, AreaZR via GitGitGadget wrote:
Show 6 quoted lines
> From: Seija Kijin <doremylover123@gmail.com>
> 
> The is_main_worktree function just checks for !wt->id,
> but the compiler doesn't know this as it is in a different
> file, so just exit out early.
> 

I think maybe we should exit out the loop early. However, the above statement is confusing. As you have said, `is_main_worktree` checks whether the `wt->id` is NULL. Why compiler doesn't know this? And why we need to exit out the loop due to above reason?

Show 23 quoted lines
> Signed-off-by: Seija Kijin <doremylover123@gmail.com>
> ---
>     refs: exit early from the loop if it is not a main worktree
>     
>     The is_main_worktree function just checks for !wt->id, but the compiler
>     doesn't know this as it is in a different file, so just exit out early.
> 
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1848%2FAreaZR%2Fexit-early-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1848/AreaZR/exit-early-v1
> Pull-Request: https://github.com/git/git/pull/1848
> 
>  refs.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/refs.c b/refs.c
> index 8b713692359..cce63a618d7 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -2791,6 +2791,7 @@ static int has_worktrees(void)
>  		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.

However, I don't know whether it's a good idea to exit the loop early in the first place. CC Patrick to help.

Thanks, Jialuo

Previous: AreaZR via GitGitGadgetNext: Eric Sunshine
Message 2 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.