path: stop relying on `the_repository` in `worktree_git_path()`

When not provided a worktree, then `worktree_git_path()` will fall back to returning a path relative to the main repository. In this case, we implicitly rely on `the_repository` to derive the path. Remove this dependency by passing a `struct repository` as parameter. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 13, 2024 at 11:13 UTC a973f60dc7c178828e351ec4e68886ffecfbcadc
7 files changed +24 -17
builtin/fsck.c
+1 -1
@@ -1050,7 +1050,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
1050 * and may get overwritten by other calls
1051 * while we're examining the index.
1052 */
1053 - path = xstrdup(worktree_git_path(wt, "index"));
1053 + path = xstrdup(worktree_git_path(the_repository, wt, "index"));
1054 read_index_from(&istate, path, get_worktree_git_dir(wt));
1055 fsck_index(&istate, path, wt->is_current);
1056 discard_index(&istate);
builtin/worktree.c
+2 -2
@@ -1146,14 +1146,14 @@ static void validate_no_submodules(const struct worktree *wt)
1146 struct strbuf path = STRBUF_INIT;
1147 int i, found_submodules = 0;
1148
1149 - if (is_directory(worktree_git_path(wt, "modules"))) {
1149 + if (is_directory(worktree_git_path(the_repository, wt, "modules"))) {
1150 /*
1151 * There could be false positives, e.g. the "modules"
1152 * directory exists but is empty. But it's a rare case and
1153 * this simpler check is probably good enough for now.
1154 */
1155 found_submodules = 1;
1156 - } else if (read_index_from(&istate, worktree_git_path(wt, "index"),
1156 + } else if (read_index_from(&istate, worktree_git_path(the_repository, wt, "index"),
1157 get_worktree_git_dir(wt)) > 0) {
1158 for (i = 0; i < istate.cache_nr; i++) {
1159 struct cache_entry *ce = istate.cache[i];
path.c
+7 -2
@@ -512,12 +512,17 @@ const char *mkpath(const char *fmt, ...)
512 return cleanup_path(pathname->buf);
513 }
514
515 -const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...)
515 +const char *worktree_git_path(struct repository *r,
516 + const struct worktree *wt, const char *fmt, ...)
517 {
518 struct strbuf *pathname = get_pathname();
519 va_list args;
520 +
521 + if (wt && wt->repo != r)
522 + BUG("worktree not connected to expected repository");
523 +
524 va_start(args, fmt);
520 - repo_git_pathv(the_repository, wt, pathname, fmt, args);
525 + repo_git_pathv(r, wt, pathname, fmt, args);
526 va_end(args);
527 return pathname->buf;
528 }
path.h
+5 -3
@@ -95,11 +95,13 @@ const char *git_path(const char *fmt, ...)
95
96 /*
97 * Similar to git_path() but can produce paths for a specified
98 - * worktree instead of current one
98 + * worktree instead of current one. When no worktree is given, then the path is
99 + * computed relative to main worktree of the given repository.
100 */
100 -const char *worktree_git_path(const struct worktree *wt,
101 +const char *worktree_git_path(struct repository *r,
102 + const struct worktree *wt,
103 const char *fmt, ...)
102 - __attribute__((format (printf, 2, 3)));
104 + __attribute__((format (printf, 3, 4)));
105
106 /*
107 * Return a path into the main repository's (the_repository) git directory.
revision.c
+1 -1
@@ -1872,7 +1872,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1872 continue; /* current index already taken care of */
1873
1874 if (read_index_from(&istate,
1875 - worktree_git_path(wt, "index"),
1875 + worktree_git_path(the_repository, wt, "index"),
1876 get_worktree_git_dir(wt)) > 0)
1877 do_add_index_objects_to_pending(revs, &istate, flags);
1878 discard_index(&istate);
worktree.c
+1 -1
@@ -252,7 +252,7 @@ const char *worktree_lock_reason(struct worktree *wt)
252 if (!wt->lock_reason_valid) {
253 struct strbuf path = STRBUF_INIT;
254
255 - strbuf_addstr(&path, worktree_git_path(wt, "locked"));
255 + strbuf_addstr(&path, worktree_git_path(the_repository, wt, "locked"));
256 if (file_exists(path.buf)) {
257 struct strbuf lock_reason = STRBUF_INIT;
258 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)
wt-status.c
+7 -7
@@ -1618,7 +1618,7 @@ static char *get_branch(const struct worktree *wt, const char *path)
1618 struct object_id oid;
1619 const char *branch_name;
1620
1621 - if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
1621 + if (strbuf_read_file(&sb, worktree_git_path(the_repository, wt, "%s", path), 0) <= 0)
1622 goto got_nothing;
1623
1624 while (sb.len && sb.buf[sb.len - 1] == '\n')
@@ -1716,18 +1716,18 @@ int wt_status_check_rebase(const struct worktree *wt,
1716 {
1717 struct stat st;
1718
1719 - if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1720 - if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
1719 + if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) {
1720 + if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) {
1721 state->am_in_progress = 1;
1722 - if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
1722 + if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size)
1723 state->am_empty_patch = 1;
1724 } else {
1725 state->rebase_in_progress = 1;
1726 state->branch = get_branch(wt, "rebase-apply/head-name");
1727 state->onto = get_branch(wt, "rebase-apply/onto");
1728 }
1729 - } else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1730 - if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
1729 + } else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) {
1730 + if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st))
1731 state->rebase_interactive_in_progress = 1;
1732 else
1733 state->rebase_in_progress = 1;
@@ -1743,7 +1743,7 @@ int wt_status_check_bisect(const struct worktree *wt,
1743 {
1744 struct stat st;
1745
1746 - if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1746 + if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
1747 state->bisect_in_progress = 1;
1748 state->bisecting_from = get_branch(wt, "BISECT_START");
1749 return 1;