Skip to content

Commit 11153ec

Browse files
committed
Merge branch 'pw/no-more-NULL-means-current-worktree' into jch
* pw/no-more-NULL-means-current-worktree: path: remove repository argument from worktree_git_path() wt-status: avoid passing NULL worktree
2 parents 1e75fbf + a49cb0f commit 11153ec

File tree

9 files changed

+66
-25
lines changed

9 files changed

+66
-25
lines changed

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ int cmd_fsck(int argc,
11051105
* and may get overwritten by other calls
11061106
* while we're examining the index.
11071107
*/
1108-
path = xstrdup(worktree_git_path(the_repository, wt, "index"));
1108+
path = xstrdup(worktree_git_path(wt, "index"));
11091109
wt_gitdir = get_worktree_git_dir(wt);
11101110

11111111
read_index_from(&istate, path, wt_gitdir);

builtin/worktree.c

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

12271227
wt_gitdir = get_worktree_git_dir(wt);
12281228

1229-
if (is_directory(worktree_git_path(the_repository, wt, "modules"))) {
1229+
if (is_directory(worktree_git_path(wt, "modules"))) {
12301230
/*
12311231
* There could be false positives, e.g. the "modules"
12321232
* directory exists but is empty. But it's a rare case and
12331233
* this simpler check is probably good enough for now.
12341234
*/
12351235
found_submodules = 1;
1236-
} else if (read_index_from(&istate, worktree_git_path(the_repository, wt, "index"),
1236+
} else if (read_index_from(&istate, worktree_git_path(wt, "index"),
12371237
wt_gitdir) > 0) {
12381238
for (i = 0; i < istate.cache_nr; i++) {
12391239
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
@@ -1848,7 +1848,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
18481848
wt_gitdir = get_worktree_git_dir(wt);
18491849

18501850
if (read_index_from(&istate,
1851-
worktree_git_path(the_repository, wt, "index"),
1851+
worktree_git_path(wt, "index"),
18521852
wt_gitdir) > 0)
18531853
do_add_index_objects_to_pending(revs, &istate, flags);
18541854

t/t7512-status-help.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,15 @@ EOF
594594
test_cmp expected actual
595595
'
596596

597+
test_expect_success 'rebase in a linked worktree' '
598+
test_might_fail git rebase --abort &&
599+
git worktree add wt &&
600+
test_when_finished "test_might_fail git -C wt rebase --abort;
601+
git worktree remove wt" &&
602+
GIT_SEQUENCE_EDITOR="echo break >" git -C wt rebase -i HEAD &&
603+
git -C wt status >actual &&
604+
test_grep "interactive rebase in progress" actual
605+
'
597606

598607
test_expect_success 'prepare am_session' '
599608
git reset --hard main &&

worktree.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ static int is_current_worktree(struct worktree *wt)
6666
return is_current;
6767
}
6868

69+
struct worktree *get_worktree_from_repository(struct repository *repo)
70+
{
71+
struct worktree *wt = xcalloc(1, sizeof(*wt));
72+
char *gitdir = absolute_pathdup(repo->gitdir);
73+
char *commondir = absolute_pathdup(repo->commondir);
74+
75+
wt->repo = repo;
76+
wt->path = absolute_pathdup(repo->worktree ? repo->worktree
77+
: repo->gitdir);
78+
wt->is_bare = !repo->worktree;
79+
if (fspathcmp(gitdir, commondir))
80+
wt->id = xstrdup(find_last_dir_sep(gitdir) + 1);
81+
wt->is_current = is_current_worktree(wt);
82+
add_head_info(wt);
83+
84+
free(gitdir);
85+
free(commondir);
86+
return wt;
87+
}
88+
6989
/*
7090
* When in a secondary worktree, and when extensions.worktreeConfig
7191
* is true, only $commondir/config and $commondir/worktrees/<id>/
@@ -288,7 +308,7 @@ const char *worktree_lock_reason(struct worktree *wt)
288308
if (!wt->lock_reason_valid) {
289309
struct strbuf path = STRBUF_INIT;
290310

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

worktree.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ struct worktree **get_worktrees(void);
3838
*/
3939
struct worktree **get_worktrees_without_reading_head(void);
4040

41+
/*
42+
* Construct a struct worktree corresponding to repo->gitdir and
43+
* repo->worktree.
44+
*/
45+
struct worktree *get_worktree_from_repository(struct repository *repo);
46+
4147
/*
4248
* Returns 1 if linked worktrees exist, 0 otherwise.
4349
*/

wt-status.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ static char *get_branch(const struct worktree *wt, const char *path)
16481648
struct object_id oid;
16491649
const char *branch_name;
16501650

1651-
if (strbuf_read_file(&sb, worktree_git_path(the_repository, wt, "%s", path), 0) <= 0)
1651+
if (strbuf_read_file(&sb, worktree_git_path(wt, "%s", path), 0) <= 0)
16521652
goto got_nothing;
16531653

16541654
while (sb.len && sb.buf[sb.len - 1] == '\n')
@@ -1747,18 +1747,21 @@ int wt_status_check_rebase(const struct worktree *wt,
17471747
{
17481748
struct stat st;
17491749

1750-
if (!stat(worktree_git_path(the_repository, wt, "rebase-apply"), &st)) {
1751-
if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/applying"), &st)) {
1750+
if (!wt)
1751+
BUG("wt_status_check_rebase() called with NULL worktree");
1752+
1753+
if (!stat(worktree_git_path(wt, "rebase-apply"), &st)) {
1754+
if (!stat(worktree_git_path(wt, "rebase-apply/applying"), &st)) {
17521755
state->am_in_progress = 1;
1753-
if (!stat(worktree_git_path(the_repository, wt, "rebase-apply/patch"), &st) && !st.st_size)
1756+
if (!stat(worktree_git_path(wt, "rebase-apply/patch"), &st) && !st.st_size)
17541757
state->am_empty_patch = 1;
17551758
} else {
17561759
state->rebase_in_progress = 1;
17571760
state->branch = get_branch(wt, "rebase-apply/head-name");
17581761
state->onto = get_branch(wt, "rebase-apply/onto");
17591762
}
1760-
} else if (!stat(worktree_git_path(the_repository, wt, "rebase-merge"), &st)) {
1761-
if (!stat(worktree_git_path(the_repository, wt, "rebase-merge/interactive"), &st))
1763+
} else if (!stat(worktree_git_path(wt, "rebase-merge"), &st)) {
1764+
if (!stat(worktree_git_path(wt, "rebase-merge/interactive"), &st))
17621765
state->rebase_interactive_in_progress = 1;
17631766
else
17641767
state->rebase_in_progress = 1;
@@ -1774,7 +1777,10 @@ int wt_status_check_bisect(const struct worktree *wt,
17741777
{
17751778
struct stat st;
17761779

1777-
if (!stat(worktree_git_path(the_repository, wt, "BISECT_LOG"), &st)) {
1780+
if (!wt)
1781+
BUG("wt_status_check_bisect() called with NULL worktree");
1782+
1783+
if (!stat(worktree_git_path(wt, "BISECT_LOG"), &st)) {
17781784
state->bisect_in_progress = 1;
17791785
state->bisecting_from = get_branch(wt, "BISECT_START");
17801786
return 1;
@@ -1821,18 +1827,19 @@ void wt_status_get_state(struct repository *r,
18211827
struct stat st;
18221828
struct object_id oid;
18231829
enum replay_action action;
1830+
struct worktree *wt = get_worktree_from_repository(r);
18241831

18251832
if (!stat(git_path_merge_head(r), &st)) {
1826-
wt_status_check_rebase(NULL, state);
1833+
wt_status_check_rebase(wt, state);
18271834
state->merge_in_progress = 1;
1828-
} else if (wt_status_check_rebase(NULL, state)) {
1835+
} else if (wt_status_check_rebase(wt, state)) {
18291836
; /* all set */
18301837
} else if (refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
18311838
!repo_get_oid(r, "CHERRY_PICK_HEAD", &oid)) {
18321839
state->cherry_pick_in_progress = 1;
18331840
oidcpy(&state->cherry_pick_head_oid, &oid);
18341841
}
1835-
wt_status_check_bisect(NULL, state);
1842+
wt_status_check_bisect(wt, state);
18361843
if (refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD") &&
18371844
!repo_get_oid(r, "REVERT_HEAD", &oid)) {
18381845
state->revert_in_progress = 1;
@@ -1850,6 +1857,8 @@ void wt_status_get_state(struct repository *r,
18501857
if (get_detached_from)
18511858
wt_status_get_detached_from(r, state);
18521859
wt_status_check_sparse_checkout(r, state);
1860+
1861+
free_worktree(wt);
18531862
}
18541863

18551864
static void wt_longstatus_print_state(struct wt_status *s)

0 commit comments

Comments
 (0)