3 test_description
='test git worktree add'
7 .
"$TEST_DIRECTORY"/lib-rebase.sh
9 test_expect_success
'setup' '
13 test_expect_success
'"add" an existing worktree' '
14 mkdir -p existing/subtree &&
15 test_must_fail git worktree add --detach existing master
18 test_expect_success
'"add" an existing empty worktree' '
19 mkdir existing_empty &&
20 git worktree add --detach existing_empty master
23 test_expect_success
'"add" using shorthand - fails when no previous branch' '
24 test_must_fail git worktree add existing_short -
27 test_expect_success
'"add" using - shorthand' '
28 git checkout -b newbranch &&
29 echo hello >myworld &&
31 git commit -m myworld &&
32 git checkout master &&
33 git worktree add short-hand - &&
34 echo refs/heads/newbranch >expect &&
35 git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
36 test_cmp expect actual
39 test_expect_success
'"add" refuses to checkout locked branch' '
40 test_must_fail git worktree add zere master &&
42 ! test -d .git/worktrees/zere
45 test_expect_success
'checking out paths not complaining about linked checkouts' '
48 echo dirty >>init.t &&
49 git checkout master -- init.t
53 test_expect_success
'"add" worktree' '
54 git rev-parse HEAD >expect &&
55 git worktree add --detach here master &&
58 test_cmp ../init.t init.t &&
59 test_must_fail git symbolic-ref HEAD &&
60 git rev-parse HEAD >actual &&
61 test_cmp ../expect actual &&
66 test_expect_success
'"add" worktree with lock' '
67 git rev-parse HEAD >expect &&
68 git worktree add --detach --lock here-with-lock master &&
69 test -f .git/worktrees/here-with-lock/locked
72 test_expect_success
'"add" worktree from a subdir' '
76 git worktree add --detach here master &&
78 test_cmp ../../init.t init.t
82 test_expect_success
'"add" from a linked checkout' '
85 git worktree add --detach nested-here master &&
91 test_expect_success
'"add" worktree creating new branch' '
92 git worktree add -b newmaster there master &&
95 test_cmp ../init.t init.t &&
96 git symbolic-ref HEAD >actual &&
97 echo refs/heads/newmaster >expect &&
98 test_cmp expect actual &&
103 test_expect_success
'die the same branch is already checked out' '
106 test_must_fail git checkout newmaster
110 test_expect_success SYMLINKS
'die the same branch is already checked out (symlink)' '
111 head=$(git -C there rev-parse --git-path HEAD) &&
112 ref=$(git -C there symbolic-ref HEAD) &&
114 ln -s "$ref" "$head" &&
115 test_must_fail git -C here checkout newmaster
118 test_expect_success
'not die the same branch is already checked out' '
121 git worktree add --force anothernewmaster newmaster
125 test_expect_success
'not die on re-checking out current branch' '
128 git checkout newmaster
132 test_expect_success
'"add" from a bare repo' '
134 git clone --bare . bare &&
136 git worktree add -b bare-master ../there2 master
140 test_expect_success
'checkout from a bare repo without "add"' '
143 test_must_fail git checkout master
147 test_expect_success
'"add" default branch of a bare repo' '
149 git clone --bare . bare2 &&
151 git worktree add ../there3 master
155 test_expect_success
'checkout with grafts' '
156 test_when_finished rm .git/info/grafts &&
158 SHA1=$(git rev-parse HEAD) &&
161 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
162 cat >expected <<-\EOF &&
166 git log --format=%s -2 >actual &&
167 test_cmp expected actual &&
168 git worktree add --detach grafted master &&
169 git --git-dir=grafted/.git log --format=%s -2 >actual &&
170 test_cmp expected actual
173 test_expect_success
'"add" from relative HEAD' '
177 git rev-parse HEAD~1 >expected &&
178 git worktree add relhead HEAD~1 &&
179 git -C relhead rev-parse HEAD >actual &&
180 test_cmp expected actual
183 test_expect_success
'"add -b" with <branch> omitted' '
184 git worktree add -b burble flornk &&
185 test_cmp_rev HEAD burble
188 test_expect_success
'"add --detach" with <branch> omitted' '
189 git worktree add --detach fishhook &&
190 git rev-parse HEAD >expected &&
191 git -C fishhook rev-parse HEAD >actual &&
192 test_cmp expected actual &&
193 test_must_fail git -C fishhook symbolic-ref HEAD
196 test_expect_success
'"add" with <branch> omitted' '
197 git worktree add wiffle/bat &&
198 test_cmp_rev HEAD bat
201 test_expect_success
'"add" checks out existing branch of dwimd name' '
202 git branch dwim HEAD~1 &&
203 git worktree add dwim &&
204 test_cmp_rev HEAD~1 dwim &&
207 test_cmp_rev HEAD dwim
211 test_expect_success
'"add <path>" dwim fails with checked out branch' '
212 git checkout -b test-branch &&
213 test_must_fail git worktree add test-branch &&
214 test_path_is_missing test-branch
217 test_expect_success
'"add --force" with existing dwimd name doesnt die' '
218 git checkout test-branch &&
219 git worktree add --force test-branch
222 test_expect_success
'"add" no auto-vivify with --detach and <branch> omitted' '
223 git worktree add --detach mish/mash &&
224 test_must_fail git rev-parse mash -- &&
225 test_must_fail git -C mish/mash symbolic-ref HEAD
228 test_expect_success
'"add" -b/-B mutually exclusive' '
229 test_must_fail git worktree add -b poodle -B poodle bamboo master
232 test_expect_success
'"add" -b/--detach mutually exclusive' '
233 test_must_fail git worktree add -b poodle --detach bamboo master
236 test_expect_success
'"add" -B/--detach mutually exclusive' '
237 test_must_fail git worktree add -B poodle --detach bamboo master
240 test_expect_success
'"add -B" fails if the branch is checked out' '
241 git rev-parse newmaster >before &&
242 test_must_fail git worktree add -B newmaster bamboo master &&
243 git rev-parse newmaster >after &&
244 test_cmp before after
247 test_expect_success
'add -B' '
248 git worktree add -B poodle bamboo2 master^ &&
249 git -C bamboo2 symbolic-ref HEAD >actual &&
250 echo refs/heads/poodle >expected &&
251 test_cmp expected actual &&
252 test_cmp_rev master^ poodle
255 test_expect_success
'local clone from linked checkout' '
256 git clone --local here here-clone &&
257 ( cd here-clone && git fsck )
260 test_expect_success
'local clone --shared from linked checkout' '
261 git -C bare worktree add --detach ../baretree &&
262 git clone --local --shared baretree bare-clone &&
263 grep /bare/ bare-clone/.git/objects/info/alternates
266 test_expect_success
'"add" worktree with --no-checkout' '
267 git worktree add --no-checkout -b swamp swamp &&
268 ! test -e swamp/init.t &&
269 git -C swamp reset --hard &&
270 test_cmp init.t swamp/init.t
273 test_expect_success
'"add" worktree with --checkout' '
274 git worktree add --checkout -b swmap2 swamp2 &&
275 test_cmp init.t swamp2/init.t
278 test_expect_success
'put a worktree under rebase' '
279 git worktree add under-rebase &&
283 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
284 git worktree list | grep "under-rebase.*detached HEAD"
288 test_expect_success
'add a worktree, checking out a rebased branch' '
289 test_must_fail git worktree add new-rebase under-rebase &&
293 test_expect_success
'checking out a rebased branch from another worktree' '
294 git worktree add new-place &&
295 test_must_fail git -C new-place checkout under-rebase
298 test_expect_success
'not allow to delete a branch under rebase' '
301 test_must_fail git branch -D under-rebase
305 test_expect_success
'rename a branch under rebase not allowed' '
306 test_must_fail git branch -M under-rebase rebase-with-new-name
309 test_expect_success
'check out from current worktree branch ok' '
312 git checkout under-rebase &&
318 test_expect_success
'checkout a branch under bisect' '
319 git worktree add under-bisect &&
324 git bisect good HEAD~2 &&
325 git worktree list | grep "under-bisect.*detached HEAD" &&
326 test_must_fail git worktree add new-bisect under-bisect &&
331 test_expect_success
'rename a branch under bisect not allowed' '
332 test_must_fail git branch -M under-bisect bisect-with-new-name
334 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
335 test_branch_upstream
() {
336 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream
&&
338 git config
"branch.$1.remote" &&
339 git config
"branch.$1.merge"
340 } >actual.upstream
&&
341 test_cmp expect.upstream actual.upstream
344 test_expect_success
'--track sets up tracking' '
345 test_when_finished rm -rf track &&
346 git worktree add --track -b track track master &&
347 test_branch_upstream track . master
350 # setup remote repository $1 and repository $2 with $1 set up as
351 # remote. The remote has two branches, master and foo.
352 setup_remote_repo
() {
356 test_commit
$1_master &&
357 git checkout
-b foo
&&
358 test_commit upstream_foo
363 test_commit
$2_master &&
364 git remote add
$1 ..
/$1 &&
365 git config remote.
$1.fetch \
366 "refs/heads/*:refs/remotes/$1/*" &&
371 test_expect_success
'--no-track avoids setting up tracking' '
372 test_when_finished rm -rf repo_upstream repo_local foo &&
373 setup_remote_repo repo_upstream repo_local &&
376 git worktree add --no-track -b foo ../foo repo_upstream/foo
380 test_must_fail git config "branch.foo.remote" &&
381 test_must_fail git config "branch.foo.merge" &&
382 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
386 test_expect_success
'"add" <path> <non-existent-branch> fails' '
387 test_must_fail git worktree add foo non-existent
390 test_expect_success
'"add" <path> <branch> dwims' '
391 test_when_finished rm -rf repo_upstream repo_dwim foo &&
392 setup_remote_repo repo_upstream repo_dwim &&
393 git init repo_dwim &&
396 git worktree add ../foo foo
400 test_branch_upstream foo repo_upstream foo &&
401 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
405 test_expect_success
'git worktree add does not match remote' '
406 test_when_finished rm -rf repo_a repo_b foo &&
407 setup_remote_repo repo_a repo_b &&
410 git worktree add ../foo
414 test_must_fail git config "branch.foo.remote" &&
415 test_must_fail git config "branch.foo.merge" &&
416 ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
420 test_expect_success
'git worktree add --guess-remote sets up tracking' '
421 test_when_finished rm -rf repo_a repo_b foo &&
422 setup_remote_repo repo_a repo_b &&
425 git worktree add --guess-remote ../foo
429 test_branch_upstream foo repo_a foo &&
430 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
434 test_expect_success
'git worktree add with worktree.guessRemote sets up tracking' '
435 test_when_finished rm -rf repo_a repo_b foo &&
436 setup_remote_repo repo_a repo_b &&
439 git config worktree.guessRemote true &&
440 git worktree add ../foo
444 test_branch_upstream foo repo_a foo &&
445 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
449 test_expect_success
'git worktree --no-guess-remote option overrides config' '
450 test_when_finished rm -rf repo_a repo_b foo &&
451 setup_remote_repo repo_a repo_b &&
454 git config worktree.guessRemote true &&
455 git worktree add --no-guess-remote ../foo
459 test_must_fail git config "branch.foo.remote" &&
460 test_must_fail git config "branch.foo.merge" &&
461 ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
465 post_checkout_hook
() {
467 test_when_finished
"rm -f $gitdir/hooks/post-checkout" &&
468 mkdir
-p $gitdir/hooks
&&
469 write_script
$gitdir/hooks
/post-checkout
<<-\EOF
472 git rev-parse --git-dir --show-toplevel
477 test_expect_success '"add" invokes post-checkout hook (branch)' '
478 post_checkout_hook &&
480 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
481 echo $(pwd)/.git/worktrees/gumby &&
484 git worktree add gumby &&
485 test_cmp hook.expect gumby/hook.actual
488 test_expect_success '"add" invokes post-checkout hook (detached)' '
489 post_checkout_hook &&
491 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
492 echo $(pwd)/.git/worktrees/grumpy &&
495 git worktree add --detach grumpy &&
496 test_cmp hook.expect grumpy/hook.actual
499 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
500 post_checkout_hook &&
502 git worktree add --no-checkout gloopy &&
503 test_path_is_missing gloopy/hook.actual
506 test_expect_success '"add" in other worktree invokes post-checkout hook' '
507 post_checkout_hook &&
509 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
510 echo $(pwd)/.git/worktrees/guppy &&
513 git -C gloopy worktree add --detach ../guppy &&
514 test_cmp hook.expect guppy/hook.actual
517 test_expect_success '"add" in bare repo invokes post-checkout hook' '
519 git clone --bare . bare &&
521 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
522 echo $(pwd)/bare/worktrees/goozy &&
525 post_checkout_hook bare &&
526 git -C bare worktree add --detach ../goozy &&
527 test_cmp hook.expect goozy/hook.actual