Skip to content

Commit a973f60

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 78f2210 commit a973f60

File tree

7 files changed

+24
-17
lines changed

7 files changed

+24
-17
lines changed

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
10501050
* and may get overwritten by other calls
10511051
* while we're examining the index.
10521052
*/
1053-
path = xstrdup(worktree_git_path(wt, "index"));
1053+
path = xstrdup(worktree_git_path(the_repository, wt, "index"));
10541054
read_index_from(&istate, path, get_worktree_git_dir(wt));
10551055
fsck_index(&istate, path, wt->is_current);
10561056
discard_index(&istate);

builtin/worktree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,14 +1146,14 @@ static void validate_no_submodules(const struct worktree *wt)
11461146
struct strbuf path = STRBUF_INIT;
11471147
int i, found_submodules = 0;
11481148

1149-
if (is_directory(worktree_git_path(wt, "modules"))) {
1149+
if (is_directory(worktree_git_path(the_repository, wt, "modules"))) {
11501150
/*
11511151
* There could be false positives, e.g. the "modules"
11521152
* directory exists but is empty. But it's a rare case and
11531153
* this simpler check is probably good enough for now.
11541154
*/
11551155
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"),
11571157
get_worktree_git_dir(wt)) > 0) {
11581158
for (i = 0; i < istate.cache_nr; i++) {
11591159
struct cache_entry *ce = istate.cache[i];

path.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,17 @@ const char *mkpath(const char *fmt, ...)
512512
return cleanup_path(pathname->buf);
513513
}
514514

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, ...)
516517
{
517518
struct strbuf *pathname = get_pathname();
518519
va_list args;
520+
521+
if (wt && wt->repo != r)
522+
BUG("worktree not connected to expected repository");
523+
519524
va_start(args, fmt);
520-
repo_git_pathv(the_repository, wt, pathname, fmt, args);
525+
repo_git_pathv(r, wt, pathname, fmt, args);
521526
va_end(args);
522527
return pathname->buf;
523528
}

path.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ const char *git_path(const char *fmt, ...)
9595

9696
/*
9797
* 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.
99100
*/
100-
const char *worktree_git_path(const struct worktree *wt,
101+
const char *worktree_git_path(struct repository *r,
102+
const struct worktree *wt,
101103
const char *fmt, ...)
102-
__attribute__((format (printf, 2, 3)));
104+
__attribute__((format (printf, 3, 4)));
103105

104106
/*
105107
* Return a path into the main repository's (the_repository) git directory.

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
18721872
continue; /* current index already taken care of */
18731873

18741874
if (read_index_from(&istate,
1875-
worktree_git_path(wt, "index"),
1875+
worktree_git_path(the_repository, wt, "index"),
18761876
get_worktree_git_dir(wt)) > 0)
18771877
do_add_index_objects_to_pending(revs, &istate, flags);
18781878
discard_index(&istate);

worktree.c

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

255-
strbuf_addstr(&path, worktree_git_path(wt, "locked"));
255+
strbuf_addstr(&path, worktree_git_path(the_repository, wt, "locked"));
256256
if (file_exists(path.buf)) {
257257
struct strbuf lock_reason = STRBUF_INIT;
258258
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
@@ -1618,7 +1618,7 @@ static char *get_branch(const struct worktree *wt, const char *path)
16181618
struct object_id oid;
16191619
const char *branch_name;
16201620

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)
16221622
goto got_nothing;
16231623

16241624
while (sb.len && sb.buf[sb.len - 1] == '\n')
@@ -1716,18 +1716,18 @@ int wt_status_check_rebase(const struct worktree *wt,
17161716
{
17171717
struct stat st;
17181718

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)) {
17211721
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)
17231723
state->am_empty_patch = 1;
17241724
} else {
17251725
state->rebase_in_progress = 1;
17261726
state->branch = get_branch(wt, "rebase-apply/head-name");
17271727
state->onto = get_branch(wt, "rebase-apply/onto");
17281728
}
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))
17311731
state->rebase_interactive_in_progress = 1;
17321732
else
17331733
state->rebase_in_progress = 1;
@@ -1743,7 +1743,7 @@ int wt_status_check_bisect(const struct worktree *wt,
17431743
{
17441744
struct stat st;
17451745

1746-
if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
1746+
if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
17471747
state->bisect_in_progress = 1;
17481748
state->bisecting_from = get_branch(wt, "BISECT_START");
17491749
return 1;

0 commit comments

Comments
 (0)