Skip to content

Commit a49cb0f

Browse files
phillipwoodgitster
authored andcommitted
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 a973f60 (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>
1 parent cb18484 commit a49cb0f

File tree

7 files changed

+19
-22
lines changed

7 files changed

+19
-22
lines changed

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ int cmd_fsck(int argc,
11371137
* and may get overwritten by other calls
11381138
* while we're examining the index.
11391139
*/
1140-
path = xstrdup(worktree_git_path(the_repository, wt, "index"));
1140+
path = xstrdup(worktree_git_path(wt, "index"));
11411141
wt_gitdir = get_worktree_git_dir(wt);
11421142

11431143
read_index_from(&istate, path, wt_gitdir);

builtin/worktree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,14 +1191,14 @@ static void validate_no_submodules(const struct worktree *wt)
11911191

11921192
wt_gitdir = get_worktree_git_dir(wt);
11931193

1194-
if (is_directory(worktree_git_path(the_repository, wt, "modules"))) {
1194+
if (is_directory(worktree_git_path(wt, "modules"))) {
11951195
/*
11961196
* There could be false positives, e.g. the "modules"
11971197
* directory exists but is empty. But it's a rare case and
11981198
* this simpler check is probably good enough for now.
11991199
*/
12001200
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"),
12021202
wt_gitdir) > 0) {
12031203
for (i = 0; i < istate.cache_nr; i++) {
12041204
struct cache_entry *ce = istate.cache[i];

path.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,17 +486,16 @@ const char *mkpath(const char *fmt, ...)
486486
return cleanup_path(pathname->buf);
487487
}
488488

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, ...)
491490
{
492491
struct strbuf *pathname = get_pathname();
493492
va_list args;
494493

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__);
497496

498497
va_start(args, fmt);
499-
repo_git_pathv(r, wt, pathname, fmt, args);
498+
repo_git_pathv(wt->repo, wt, pathname, fmt, args);
500499
va_end(args);
501500
return pathname->buf;
502501
}

path.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ const char *repo_git_path_replace(struct repository *repo,
6666

6767
/*
6868
* 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.
7170
*/
72-
const char *worktree_git_path(struct repository *r,
73-
const struct worktree *wt,
71+
const char *worktree_git_path(const struct worktree *wt,
7472
const char *fmt, ...)
75-
__attribute__((format (printf, 3, 4)));
73+
__attribute__((format (printf, 2, 3)));
7674

7775
/*
7876
* The `repo_worktree_path` family of functions will construct a path into a

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
18471847
wt_gitdir = get_worktree_git_dir(wt);
18481848

18491849
if (read_index_from(&istate,
1850-
worktree_git_path(the_repository, wt, "index"),
1850+
worktree_git_path(wt, "index"),
18511851
wt_gitdir) > 0)
18521852
do_add_index_objects_to_pending(revs, &istate, flags);
18531853

worktree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ const char *worktree_lock_reason(struct worktree *wt)
308308
if (!wt->lock_reason_valid) {
309309
struct strbuf path = STRBUF_INIT;
310310

311-
strbuf_addstr(&path, worktree_git_path(the_repository, wt, "locked"));
311+
strbuf_addstr(&path, worktree_git_path(wt, "locked"));
312312
if (file_exists(path.buf)) {
313313
struct strbuf lock_reason = STRBUF_INIT;
314314
if (strbuf_read_file(&lock_reason, path.buf, 0) < 0)

wt-status.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ static char *get_branch(const struct worktree *wt, const char *path)
16241624
struct object_id oid;
16251625
const char *branch_name;
16261626

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)
16281628
goto got_nothing;
16291629

16301630
while (sb.len && sb.buf[sb.len - 1] == '\n')
@@ -1726,18 +1726,18 @@ int wt_status_check_rebase(const struct worktree *wt,
17261726
if (!wt)
17271727
BUG("wt_status_check_rebase() called with NULL worktree");
17281728

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)) {
17311731
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)
17331733
state->am_empty_patch = 1;
17341734
} else {
17351735
state->rebase_in_progress = 1;
17361736
state->branch = get_branch(wt, "rebase-apply/head-name");
17371737
state->onto = get_branch(wt, "rebase-apply/onto");
17381738
}
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))
17411741
state->rebase_interactive_in_progress = 1;
17421742
else
17431743
state->rebase_in_progress = 1;
@@ -1756,7 +1756,7 @@ int wt_status_check_bisect(const struct worktree *wt,
17561756
if (!wt)
17571757
BUG("wt_status_check_bisect() called with NULL worktree");
17581758

1759-
if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
1759+
if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
17601760
state->bisect_in_progress = 1;
17611761
state->bisecting_from = get_branch(wt, "BISECT_START");
17621762
return 1;

0 commit comments

Comments
 (0)