aboutsummaryrefslogtreecommitdiff
path: root/worktree.c
diff options
context:
space:
mode:
authorPhillip Wood <phillip.wood@dunelm.org.uk>2026-02-19 14:26:32 +0000
committerJunio C Hamano <gitster@pobox.com>2026-02-19 11:03:23 -0800
commitcb18484385eb66f6220d2418d62ad790358899d1 (patch)
treec393a837c65240e6aa36feb8445e58d6e93626e1 /worktree.c
parent67ad42147a7acc2af6074753ebd03d904476118f (diff)
downloadgit-cb18484385eb66f6220d2418d62ad790358899d1.tar.xz
wt-status: avoid passing NULL worktree
In preparation for removing the repository argument from worktree_git_path() add a function to construct a "struct worktree" 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: - branch.c:prepare_checked_out_branches() which loops over all worktrees. - worktree.c:is_worktree_being_rebased() which is called from builtin/branch.c:reject_rebase_or_bisect_branch() that loops over all worktrees and worktree.c:is_shared_symref() which dereferences wt earlier in the function. - wt-status:wt_status_get_state() which is updated to avoid passing a NULL worktree by this patch. This updates the only callers that pass a NULL worktree to 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 <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'worktree.c')
-rw-r--r--worktree.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/worktree.c b/worktree.c
index 9308389cb6..218c332a66 100644
--- a/worktree.c
+++ b/worktree.c
@@ -66,6 +66,26 @@ static int is_current_worktree(struct worktree *wt)
return is_current;
}
+struct worktree *get_worktree_from_repository(struct repository *repo)
+{
+ struct worktree *wt = xcalloc(1, sizeof(*wt));
+ char *gitdir = absolute_pathdup(repo->gitdir);
+ char *commondir = absolute_pathdup(repo->commondir);
+
+ wt->repo = repo;
+ 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(gitdir) + 1);
+ wt->is_current = is_current_worktree(wt);
+ add_head_info(wt);
+
+ free(gitdir);
+ free(commondir);
+ return wt;
+}
+
/*
* When in a secondary worktree, and when extensions.worktreeConfig
* is true, only $commondir/config and $commondir/worktrees/<id>/