Subject: [PATCH v2 0/2] worktree_git_path(): remove repository argument

From: Phillip Wood <[email-masked]>
Date: Thu, 19 Feb 2026 14:26:31 +0000

From: Phillip Wood <[email-masked]>

These patches remove the repository argument from worktree_git_path()
in favor of using the repository in the "sturct worktree" argument.
This enables us to remove some uses of "the_repository". The first
patch adds a new function git_worktree_from_repository() to construct
a "struct worktree" based on the repository's worktree and uses it
to avoid passing a NULL worktree to worktree_git_path(). The second
patch then removes the repository argument from that function and
always uses the repository in the worktree argument instead.

Thanks to Karthik and Junio for their comments, here are the changes
since V1:
 - always set worktree path - for bare repositories the worktree path
   is repo->gitdir
 - fix the worktree bareness (there were too many negations)
 - fix the wortkree id (it comes from repo->gitdir not repo->commondir)
 - add a test for "git status" on a rebase in a linked worktree.
 - expand the commit message to explain
   (a) that we use the "gitdir" and "worktree" members of "struct
       repository" to construct the "struct worktree"
   (b) how the "current" worktree is determined

Base-Commit: 852829b3dd2fe4e7c7fc4d8badde644cf1b66c74
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fget-current-worktree%2Fv2
View-Changes-At: https://github.com/phillipwood/git/compare/852829b3d...db9d519cb
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/get-current-worktree/v2

Phillip Wood (2):
  wt-status: avoid passing NULL worktree
  path: remove repository argument from worktree_git_path()

 builtin/fsck.c         |  2 +-
 builtin/worktree.c     |  4 ++--
 path.c                 |  9 ++++-----
 path.h                 |  8 +++-----
 revision.c             |  2 +-
 t/t7512-status-help.sh |  9 +++++++++
 worktree.c             | 22 +++++++++++++++++++++-
 worktree.h             |  6 ++++++
 wt-status.c            | 29 +++++++++++++++++++----------
 9 files changed, 66 insertions(+), 25 deletions(-)

Range-diff against v1:
1:  409871a7d52 ! 1:  902295b8714 wt-status: avoid passing NULL worktree
    @@ Commit message
     
         In preparation for removing the repository argument from
         worktree_git_path() add a function to construct a "struct worktree"
    -    from a "struct repository" and use that to avoid passing a NULL
    -    worktree to wt_status_check_bisect() and wt_status_check_rebase().
    +    from a "struct repository" using its "gitdir" and "worktree"
    +    members. This function is then used to avoid passing a NULL worktree to
    +    wt_status_check_bisect() and wt_status_check_rebase(). 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.
     
         wt_status_check_bisect() and wt_status_check_rebase() have the following
         callers:
    @@ Commit message
            NULL worktree by this patch.
     
         This updates the only callers that pass a NULL worktree to
    -    worktree_git_path().
    +    worktree_git_path(). A new test is added to check that "git status"
    +    detects a rebase in a linked worktree.
     
         Signed-off-by: Phillip Wood <[email-masked]>
     
    + ## t/t7512-status-help.sh ##
    +@@ t/t7512-status-help.sh: EOF
    + 	test_cmp expected actual
    + '
    + 
    ++test_expect_success 'rebase in a linked worktree' '
    ++	test_might_fail git rebase --abort &&
    ++	git worktree add wt &&
    ++	test_when_finished "test_might_fail git -C wt rebase --abort;
    ++				git worktree remove wt" &&
    ++	GIT_SEQUENCE_EDITOR="echo break >" git -C wt rebase -i HEAD &&
    ++	git -C wt status >actual &&
    ++	test_grep "interactive rebase in progress" actual
    ++'
    + 
    + test_expect_success 'prepare am_session' '
    + 	git reset --hard main &&
    +
      ## worktree.c ##
     @@ worktree.c: static int is_current_worktree(struct worktree *wt)
      	return is_current;
    @@ worktree.c: static int is_current_worktree(struct worktree *wt)
     +	char *commondir = absolute_pathdup(repo->commondir);
     +
     +	wt->repo = repo;
    -+	if (repo->worktree)
    -+		wt->path = absolute_pathdup(repo->worktree);
    -+	wt->is_bare = !!repo->worktree;
    ++	wt->path = absolute_pathdup(repo->worktree ? repo->worktree
    ++						   : repo->gitdir);
    ++	wt->is_bare = !repo->worktree;
     +	if (fspathcmp(gitdir, commondir))
    -+		wt->id = xstrdup(find_last_dir_sep(commondir) + 1);
    ++		wt->id = xstrdup(find_last_dir_sep(gitdir) + 1);
     +	wt->is_current = is_current_worktree(wt);
     +	add_head_info(wt);
     +
    @@ worktree.h: struct worktree **get_worktrees(void);
       */
      struct worktree **get_worktrees_without_reading_head(void);
      
    --/*
    -+/* Construct a struct worktree from a struct repository */
    ++/*
    ++ * Construct a struct worktree corresponding to repo->gitdir and
    ++ * repo->worktree.
    ++ */
     +struct worktree *get_worktree_from_repository(struct repository *repo);
     +
    -+ /*
    + /*
       * Returns 1 if linked worktrees exist, 0 otherwise.
       */
    - int submodule_uses_worktrees(const char *path);
     
      ## wt-status.c ##
     @@ wt-status.c: int wt_status_check_rebase(const struct worktree *wt,
2:  23b8a355b41 = 2:  db9d519cbda path: remove repository argument from worktree_git_path()
-- 
2.52.0.362.g884e03848a9