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

Re: [PATCH v2 1/2] wt-status: avoid passing NULL worktree

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 19, 2026, 19:30 UTC
Message-ID
<xmqqv7fs4jlp.fsf@gitster.g>
In-Reply-To
<902295b87146e5cb5358cebab51f8d66701290a8.1771511192.git.phillip.wood@dunelm.org.uk>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 6 quoted lines
> In general the "struct worktree" returned may not correspond to
> the "current" worktree defined by is_current_worktree() as that
> function uses "the_repository" rather than "wt->repo" when
> deciding which worktree is "current".  In practice the "struct
> repository" we pass corresponds to "the_repository" as we only
> ever operate on a single repository at the moment.

This may technically be a correct description, but feels very unsatisfactory, as it fails to answer this very simple question:

    what does it mean when is_current_worktree() says "no" to the
    worktree instance returned by this function?  In other words,
    what are the sample sequences that can lead to such a worktree?

We start a Git process in a directory which is part of a set of worktrees governed by a single repository. That repository becomes the_repository and the worktree instance that represents our directory would satisfy is_current_worktree(). Then we visit another directory that is one of a set of worktrees goverend by a separate and different repository. We now have a repository instance that is different from our the_repository. Perhaps our in-core submodule code may do that, and that different repository is the submodule in question. Running this function will yield the worktree instance, whose path is a subdirectory of our current directory where the submodule is checked out? It may be the current worktree if we asked is_current_worktree() about that worktree in the context of the submodule, but it is not in the context of our superproject repository. In fact, none of the worktrees governed by the submodule repository can be "current", as they are not our checkout, from the viewpoint of our superproject repository.

Is that what is going on here?  
A related question that is much more relevant is this:
    What is the significance of the worktree, relative to our
    process, returned by this function for a given repo?  What is so
    special about this worktree, among others that are also linked
    to the same repository?
    What does it mean for a worktree to "corresponds to"
    repo->{gitdir,worktree}?  Why does the currently running Git
    process want to grab such a worktree?

If the answer were "it is the current worktree", then we would have a nice and very understandable name "get-current-worktree-for-repo" for the function, but because I do not think of a good answer to the question (and you already explained why it is not the "current" worktree), I cannot improve on "get _A_ worktree from repository", the name given by the patch, which leaves the "which one of the worktrees are we talking about? Why did we pick that particular one instead of other ones" unanswered.

Or perhaps "the current worktree" is not a per-Git-process concept, but is a per-process-per-repo concept?

In other words, the function is_current_worktree(wt) may not take a repository and always compute things relative to the_repository, but once we wean ourselves off of the_repository, we would/should have repo_is_current_worktree(repo, wt), making is_current_worktree(wt) a thin wrapper for repo_is_current_worktree(the_repository, wt)?

Show 14 quoted lines
> diff --git a/worktree.h b/worktree.h
> index e4bcccdc0ae..06efe26b835 100644
> --- a/worktree.h
> +++ b/worktree.h
> @@ -38,6 +38,12 @@ struct worktree **get_worktrees(void);
>   */
>  struct worktree **get_worktrees_without_reading_head(void);
>  
> +/*
> + * Construct a struct worktree corresponding to repo->gitdir and
> + * repo->worktree.
> + */
> +struct worktree *get_worktree_from_repository(struct repository *repo);
> +
Previous: Phillip WoodNext: Junio C Hamano
Message 32 of 39 in “worktree: change representation and usage of primary worktree”
  1. Shreyansh PaliwalFeb 13, 2026
  2. [RFC][PATCH 1/2] worktree: represent the primary worktree with '/' instead of NULLShreyansh Paliwal, Feb 13, 2026
  3. Junio C HamanoFeb 13, 2026
  4. Shreyansh PaliwalFeb 14, 2026
  5. [RFC][PATCH 2/2] worktree: stop passing NULL as primary worktreeShreyansh Paliwal, Feb 13, 2026
  6. Junio C HamanoFeb 13, 2026
  7. Shreyansh PaliwalFeb 14, 2026
  8. Phillip WoodFeb 14, 2026
  9. Junio C HamanoFeb 14, 2026
  10. Shreyansh PaliwalFeb 15, 2026
  11. Phillip WoodFeb 16, 2026
  12. Junio C HamanoFeb 17, 2026
  13. Shreyansh PaliwalFeb 17, 2026
  14. 0/2 worktree_git_path(): remove repository argumentPhillip Wood, Feb 16, 2026
  15. 1/2 wt-status: avoid passing NULL worktreePhillip Wood, Feb 16, 2026
  16. Phillip WoodFeb 17, 2026
  17. Shreyansh PaliwalFeb 17, 2026
  18. Phillip WoodFeb 17, 2026
  19. Shreyansh PaliwalFeb 17, 2026
  20. Junio C HamanoFeb 17, 2026
  21. Karthik NayakFeb 17, 2026
  22. Phillip WoodFeb 18, 2026
  23. Junio C HamanoFeb 17, 2026
  24. Phillip WoodFeb 18, 2026
  25. 2/2 path: remove repository argument from worktree_git_path()Phillip Wood, Feb 16, 2026
  26. Karthik NayakFeb 17, 2026
  27. Shreyansh PaliwalFeb 17, 2026
  28. Phillip WoodFeb 17, 2026
  29. Shreyansh PaliwalFeb 17, 2026
  30. 0/2 worktree_git_path(): remove repository argumentPhillip Wood, Feb 19, 2026
  31. 1/2 wt-status: avoid passing NULL worktreePhillip Wood, Feb 19, 2026
  32. Junio C HamanoFeb 19, 2026
  33. Junio C HamanoFeb 19, 2026
  34. Phillip WoodFeb 25, 2026
  35. Junio C HamanoFeb 25, 2026
  36. Phillip WoodFeb 26, 2026
  37. Junio C HamanoFeb 26, 2026
  38. 2/2 path: remove repository argument from worktree_git_path()Phillip Wood, Feb 19, 2026
  39. Junio C HamanoFeb 19, 2026

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.