Skip to content

Commit 78bb251

Browse files
carenasgitster
authored andcommitted
checkout/switch: disallow checking out same branch in multiple worktrees
Commands `git switch -C` and `git checkout -B` neglect to check whether the provided branch is already checked out in some other worktree, as shown by the following: $ git worktree list .../foo beefb00f [main] $ git worktree add ../other Preparing worktree (new branch 'other') HEAD is now at beefb00f first $ cd ../other $ git switch -C main Switched to and reset branch 'main' $ git worktree list .../foo beefb00f [main] .../other beefb00f [main] Fix this problem by teaching `git switch -C` and `git checkout -B` to check whether the branch in question is already checked out elsewhere. Unlike what it is done for `git switch` and `git checkout`, that have an historical exception to ignore other worktrees if the branch to check is the current one (as required when called as part of other tools), the logic implemented is more strict and will require the user to invoke the command with `--ignore-other-worktrees` to explicitly indicate they want the risky behaviour. This matches the current behaviour of `git branch -f` and is safer; for more details see the tests in t2400. Reported-by: Jinwook Jeong <[email protected]> Helped-by: Eric Sunshine <[email protected]> Helped-by: Rubén Justo <[email protected]> Helped-by: Phillip Wood <[email protected]> Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 37537d6 commit 78bb251

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

builtin/checkout.c

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,8 @@ static void die_if_some_operation_in_progress(void)
14741474
}
14751475

14761476
static int checkout_branch(struct checkout_opts *opts,
1477-
struct branch_info *new_branch_info)
1477+
struct branch_info *new_branch_info,
1478+
char *check_branch_path)
14781479
{
14791480
if (opts->pathspec.nr)
14801481
die(_("paths cannot be used with switching branches"));
@@ -1533,13 +1534,13 @@ static int checkout_branch(struct checkout_opts *opts,
15331534
if (!opts->can_switch_when_in_progress)
15341535
die_if_some_operation_in_progress();
15351536

1536-
if (new_branch_info->path && !opts->force_detach && !opts->new_branch &&
1537-
!opts->ignore_other_worktrees) {
1537+
if (!opts->ignore_other_worktrees && !opts->force_detach &&
1538+
check_branch_path && ref_exists(check_branch_path)) {
15381539
int flag;
15391540
char *head_ref = resolve_refdup("HEAD", 0, NULL, &flag);
1540-
if (head_ref &&
1541-
(!(flag & REF_ISSYMREF) || strcmp(head_ref, new_branch_info->path)))
1542-
die_if_checked_out(new_branch_info->path, 1);
1541+
if (opts->new_branch_force || (head_ref &&
1542+
(!(flag & REF_ISSYMREF) || strcmp(head_ref, check_branch_path))))
1543+
die_if_checked_out(check_branch_path, 1);
15431544
free(head_ref);
15441545
}
15451546

@@ -1627,7 +1628,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
16271628
const char * const usagestr[],
16281629
struct branch_info *new_branch_info)
16291630
{
1631+
int ret;
16301632
int parseopt_flags = 0;
1633+
char *check_branch_path = NULL;
16311634

16321635
opts->overwrite_ignore = 1;
16331636
opts->prefix = prefix;
@@ -1717,6 +1720,13 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
17171720
opts->new_branch = argv0 + 1;
17181721
}
17191722

1723+
if (opts->new_branch && !opts->ignore_other_worktrees) {
1724+
struct strbuf buf = STRBUF_INIT;
1725+
1726+
strbuf_branchname(&buf, opts->new_branch, INTERPRET_BRANCH_LOCAL);
1727+
strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
1728+
check_branch_path = strbuf_detach(&buf, NULL);
1729+
}
17201730
/*
17211731
* Extract branch name from command line arguments, so
17221732
* all that is left is pathspecs.
@@ -1741,6 +1751,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
17411751
new_branch_info, opts, &rev);
17421752
argv += n;
17431753
argc -= n;
1754+
1755+
if (!opts->ignore_other_worktrees && !check_branch_path && new_branch_info->path)
1756+
check_branch_path = xstrdup(new_branch_info->path);
17441757
} else if (!opts->accept_ref && opts->from_treeish) {
17451758
struct object_id rev;
17461759

@@ -1817,9 +1830,12 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
18171830
}
18181831

18191832
if (opts->patch_mode || opts->pathspec.nr)
1820-
return checkout_paths(opts, new_branch_info);
1833+
ret = checkout_paths(opts, new_branch_info);
18211834
else
1822-
return checkout_branch(opts, new_branch_info);
1835+
ret = checkout_branch(opts, new_branch_info, check_branch_path);
1836+
1837+
free(check_branch_path);
1838+
return ret;
18231839
}
18241840

18251841
int cmd_checkout(int argc, const char **argv, const char *prefix)

t/t2400-worktree-add.sh

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,52 @@ test_expect_success '"add" worktree creating new branch' '
118118
)
119119
'
120120

121-
test_expect_success 'die the same branch is already checked out' '
121+
test_expect_success 'die if the same branch is already checked out' '
122122
(
123123
cd here &&
124-
test_must_fail git checkout newmain
124+
test_must_fail git checkout newmain &&
125+
test_must_fail git checkout -B newmain &&
126+
test_must_fail git switch newmain &&
127+
test_must_fail git switch -C newmain
125128
)
126129
'
127130

128-
test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
131+
test_expect_success SYMLINKS 'die if the same branch is already checked out (symlink)' '
129132
head=$(git -C there rev-parse --git-path HEAD) &&
130133
ref=$(git -C there symbolic-ref HEAD) &&
131134
rm "$head" &&
132135
ln -s "$ref" "$head" &&
133136
test_must_fail git -C here checkout newmain
134137
'
135138

136-
test_expect_success 'not die the same branch is already checked out' '
139+
test_expect_success 'allow creating multiple worktrees for same branch with force' '
140+
git worktree add --force anothernewmain newmain
141+
'
142+
143+
test_expect_success 'allow checkout/reset from the conflicted branch' '
137144
(
138145
cd here &&
139-
git worktree add --force anothernewmain newmain
146+
git checkout -b conflictedmain newmain &&
147+
git checkout -B conflictedmain newmain &&
148+
git switch -C conflictedmain newmain
149+
)
150+
'
151+
152+
test_expect_success 'and not die on re-checking out current branch even if conflicted' '
153+
(
154+
cd there &&
155+
git checkout newmain &&
156+
git switch newmain
140157
)
141158
'
142159

143-
test_expect_success 'not die on re-checking out current branch' '
160+
test_expect_failure 'unless using force without --ignore-other-worktrees' '
144161
(
145162
cd there &&
146-
git checkout newmain
163+
test_must_fail git checkout -B newmain &&
164+
test_must_fail git switch -C newmain &&
165+
git checkout --ignore-other-worktrees -B newmain &&
166+
git switch --ignore-other-worktrees -C newmain
147167
)
148168
'
149169

0 commit comments

Comments
 (0)