path: remove repository argument from worktree_git_path()

worktree_git_path() takes a struct repository and a struct worktree which also contains a struct repository. The repository argument was added by a973f60dc7c (path: stop relying on `the_repository` in `worktree_git_path()`, 2024-08-13) and exists because the worktree argument is optional. Having two ways of passing a repository is a potential foot-gun as if the the worktree argument is present the repository argument must match the worktree's repository member. Since the last commit there are no callers that pass a NULL worktree so lets remove the repository argument. This removes the potential confusion and lets us delete a number of uses of "the_repository". worktree_git_path() has the following callers: - builtin/worktree.c:validate_no_submodules() which is called from check_clean_worktree() and move_worktree(), both of which supply a non-NULL worktree. - builtin/fsck.c:cmd_fsck() which loops over all worktrees. - revision.c:add_index_objects_to_pending() which loops over all worktrees. - worktree.c:worktree_lock_reason() which dereferences wt before calling worktree_git_path(). - wt-status.c:wt_status_check_bisect() and wt_status_check_rebase() which are always called with a non-NULL worktree after the last commit. - wt-status.c:git_branch() which is only called by wt_status_check_bisect() and wt_status_check_rebase(). Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Feb 19, 2026 at 14:26 UTC a49cb0f093809e3e66566f161aa930f37346775d
7 files changed +19 -22
builtin/fsck.c
+1 -1
@@ -1137,7 +1137,7 @@ int cmd_fsck(int argc,
1137 * and may get overwritten by other calls
1138 * while we're examining the index.
1139 */
1140 - path = xstrdup(worktree_git_path(the_repository, wt, "index"));
1140 + path = xstrdup(worktree_git_path(wt, "index"));
1141 wt_gitdir = get_worktree_git_dir(wt);
1142
1143 read_index_from(&istate, path, wt_gitdir);
builtin/worktree.c
+2 -2
@@ -1191,14 +1191,14 @@ static void validate_no_submodules(const struct worktree *wt)
1191
1192 wt_gitdir = get_worktree_git_dir(wt);
1193
1194 - if (is_directory(worktree_git_path(the_repository, wt, "modules"))) {
1194 + if (is_directory(worktree_git_path(wt, "modules"))) {
1195 /*
1196 * There could be false positives, e.g. the "modules"
1197 * directory exists but is empty. But it's a rare case and
1198 * this simpler check is probably good enough for now.
1199 */
1200 found_submodules = 1;
1201 - } else if (read_index_from(&istate, worktree_git_path(the_repository, wt, "index"),
1201 + } else if (read_index_from(&istate, worktree_git_path(wt, "index"),
1202 wt_gitdir) > 0) {
1203 for (i = 0; i < istate.cache_nr; i++) {
1204 struct cache_entry *ce = istate.cache[i];
path.c
+4 -5
@@ -486,17 +486,16 @@ const char *mkpath(const char *fmt, ...)
486 return cleanup_path(pathname->buf);
487 }
488
489 -const char *worktree_git_path(struct repository *r,
490 - const struct worktree *wt, const char *fmt, ...)
489 +const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
490 {
491 struct strbuf *pathname = get_pathname();
492 va_list args;
493
495 - if (wt && wt->repo != r)
496 - BUG("worktree not connected to expected repository");
494 + if (!wt)
495 + BUG("%s() called with NULL worktree", __func__);
496
497 va_start(args, fmt);
499 - repo_git_pathv(r, wt, pathname, fmt, args);
498 + repo_git_pathv(wt->repo, wt, pathname, fmt, args);
499 va_end(args);
500 return pathname->buf;
501 }
path.h
+3 -5
@@ -66,13 +66,11 @@ const char *repo_git_path_replace(struct repository *repo,
66
67 /*
68 * Similar to repo_git_path() but can produce paths for a specified
69 - * worktree instead of current one. When no worktree is given, then the path is
70 - * computed relative to main worktree of the given repository.
69 + * worktree instead of current one.
70 */
72 -const char *worktree_git_path(struct repository *r,
73 - const struct worktree *wt,
71 +const char *worktree_git_path(const struct worktree *wt,
72 const char *fmt, ...)
75 - __attribute__((format (printf, 3, 4)));
73 + __attribute__((format (printf, 2, 3)));
74
75 /*
76 * The `repo_worktree_path` family of functions will construct a path into a
revision.c
+1 -1
@@ -1847,7 +1847,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1847 wt_gitdir = get_worktree_git_dir(wt);
1848
1849 if (read_index_from(&istate,
1850 - worktree_git_path(the_repository, wt, "index"),
1850 + worktree_git_path(wt, "index"),
1851 wt_gitdir) > 0)
1852 do_add_index_objects_to_pending(revs, &istate, flags);
1853
worktree.c
+1 -1
@@ -308,7 +308,7 @@ const char *worktree_lock_reason(struct worktree *wt)
308 if (!wt->lock_reason_valid) {
309 struct strbuf path = STRBUF_INIT;
310
311 - strbuf_addstr(&path, worktree_git_path(the_repository, wt, "locked"));
311 + strbuf_addstr(&path, worktree_git_path(wt, "locked"));
312 if (file_exists(path.buf)) {
313 struct strbuf lock_reason = STRBUF_INIT;
314 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
wt-status.c
+7 -7
@@ -1624,7 +1624,7 @@ static char *get_branch(const struct worktree *wt, const char *path)
1624 struct object_id oid;
1625 const char *branch_name;
1626
1627 - if (strbuf_read_file(&sb, worktree_git_path(the_repository, wt, "%s", path), 0) <= 0)
1627 + if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1628 goto got_nothing;
1629
1630 while (sb.len && sb.buf[sb.len - 1] == '\n')
@@ -1726,18 +1726,18 @@ int wt_status_check_rebase(const struct worktree *wt,
1726 if (!wt)
1727 BUG("wt_status_check_rebase() called with NULL worktree");
1728
1729 - if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) {
1730 - if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) {
1729 + if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1730 + if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1731 state->am_in_progress = 1;
1732 - if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size)
1732 + if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1733 state->am_empty_patch = 1;
1734 } else {
1735 state->rebase_in_progress = 1;
1736 state->branch = get_branch(wt, "rebase-apply/head-name");
1737 state->onto = get_branch(wt, "rebase-apply/onto");
1738 }
1739 - } else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) {
1740 - if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st))
1739 + } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1740 + if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1741 state->rebase_interactive_in_progress = 1;
1742 else
1743 state->rebase_in_progress = 1;
@@ -1756,7 +1756,7 @@ int wt_status_check_bisect(const struct worktree *wt,
1756 if (!wt)
1757 BUG("wt_status_check_bisect() called with NULL worktree");
1758
1759 - if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
1759 + if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1760 state->bisect_in_progress = 1;
1761 state->bisecting_from = get_branch(wt, "BISECT_START");
1762 return 1;