switch: reject if the branch is already checked out elsewhere (test)

Since 5883034 (checkout: reject if the branch is already checked out elsewhere) in normal use, we do not allow multiple worktrees having the same checked out branch. A bug has recently been fixed that caused this to not work as expected. Let's add a test to notice if this changes in the future. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Feb 25, 2023 at 15:22 UTC 894ea945095b74542c6c4f4aefbe20f5b68be437
1 file changed +29
t/t2060-switch.sh
+29
@@ -146,4 +146,33 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
146 test_cmp_config "" --default "" branch.main2.merge
147 '
148
149 +test_expect_success 'switch back when temporarily detached and checked out elsewhere ' '
150 + test_when_finished "
151 + git worktree remove wt1 ||:
152 + git worktree remove wt2 ||:
153 + git checkout - ||:
154 + git branch -D shared ||:
155 + " &&
156 + git checkout -b shared &&
157 + test_commit shared-first &&
158 + HASH1=$(git rev-parse --verify HEAD) &&
159 + test_commit shared-second &&
160 + test_commit shared-third &&
161 + HASH2=$(git rev-parse --verify HEAD) &&
162 + git worktree add wt1 -f shared &&
163 + git -C wt1 bisect start &&
164 + git -C wt1 bisect good $HASH1 &&
165 + git -C wt1 bisect bad $HASH2 &&
166 + git worktree add wt2 -f shared &&
167 + git -C wt2 bisect start &&
168 + git -C wt2 bisect good $HASH1 &&
169 + git -C wt2 bisect bad $HASH2 &&
170 + # we test in both worktrees to ensure that works
171 + # as expected with "first" and "next" worktrees
172 + test_must_fail git -C wt1 switch shared &&
173 + git -C wt1 switch --ignore-other-worktrees shared &&
174 + test_must_fail git -C wt2 switch shared &&
175 + git -C wt2 switch --ignore-other-worktrees shared
176 +'
177 +
178 test_done