3 test_description
='test git worktree add'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_CREATE_REPO_NO_TEMPLATE
=1
11 .
"$TEST_DIRECTORY"/lib-rebase.sh
13 test_expect_success
'setup' '
17 test_expect_success
'"add" an existing worktree' '
18 mkdir -p existing/subtree &&
19 test_must_fail git worktree add --detach existing main
22 test_expect_success
'"add" an existing empty worktree' '
23 mkdir existing_empty &&
24 git worktree add --detach existing_empty main
27 test_expect_success
'"add" using shorthand - fails when no previous branch' '
28 test_must_fail git worktree add existing_short -
31 test_expect_success
'"add" using - shorthand' '
32 git checkout -b newbranch &&
33 echo hello >myworld &&
35 git commit -m myworld &&
37 git worktree add short-hand - &&
38 echo refs/heads/newbranch >expect &&
39 git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
40 test_cmp expect actual
43 test_expect_success
'"add" refuses to checkout locked branch' '
44 test_must_fail git worktree add zere main &&
46 ! test -d .git/worktrees/zere
49 test_expect_success
'checking out paths not complaining about linked checkouts' '
52 echo dirty >>init.t &&
53 git checkout main -- init.t
57 test_expect_success
'"add" worktree' '
58 git rev-parse HEAD >expect &&
59 git worktree add --detach here main &&
62 test_cmp ../init.t init.t &&
63 test_must_fail git symbolic-ref HEAD &&
64 git rev-parse HEAD >actual &&
65 test_cmp ../expect actual &&
70 test_expect_success
'"add" worktree with lock' '
71 git worktree add --detach --lock here-with-lock main &&
72 test_when_finished "git worktree unlock here-with-lock || :" &&
73 test -f .git/worktrees/here-with-lock/locked
76 test_expect_success
'"add" worktree with lock and reason' '
77 lock_reason="why not" &&
78 git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main &&
79 test_when_finished "git worktree unlock here-with-lock-reason || :" &&
80 test -f .git/worktrees/here-with-lock-reason/locked &&
81 echo "$lock_reason" >expect &&
82 test_cmp expect .git/worktrees/here-with-lock-reason/locked
85 test_expect_success
'"add" worktree with reason but no lock' '
86 test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main &&
87 test_path_is_missing .git/worktrees/here-with-reason-only/locked
90 test_expect_success
'"add" worktree from a subdir' '
94 git worktree add --detach here main &&
96 test_cmp ../../init.t init.t
100 test_expect_success
'"add" from a linked checkout' '
103 git worktree add --detach nested-here main &&
109 test_expect_success
'"add" worktree creating new branch' '
110 git worktree add -b newmain there main &&
113 test_cmp ../init.t init.t &&
114 git symbolic-ref HEAD >actual &&
115 echo refs/heads/newmain >expect &&
116 test_cmp expect actual &&
121 test_expect_success
'die the same branch is already checked out' '
124 test_must_fail git checkout newmain
128 test_expect_success SYMLINKS
'die the same branch is already checked out (symlink)' '
129 head=$(git -C there rev-parse --git-path HEAD) &&
130 ref=$(git -C there symbolic-ref HEAD) &&
132 ln -s "$ref" "$head" &&
133 test_must_fail git -C here checkout newmain
136 test_expect_success
'not die the same branch is already checked out' '
139 git worktree add --force anothernewmain newmain
143 test_expect_success
'not die on re-checking out current branch' '
150 test_expect_success
'"add" from a bare repo' '
152 git clone --bare . bare &&
154 git worktree add -b bare-main ../there2 main
158 test_expect_success
'checkout from a bare repo without "add"' '
161 test_must_fail git checkout main
165 test_expect_success
'"add" default branch of a bare repo' '
167 git clone --bare . bare2 &&
169 git worktree add ../there3 main &&
171 # Simple check that a Git command does not
172 # immediately fail with the current setup
175 cat >expect <<-EOF &&
179 test_cmp expect actual
182 test_expect_success
'"add" to bare repo with worktree config' '
184 git clone --bare . bare3 &&
186 git config extensions.worktreeconfig true &&
188 # Add config values that are erroneous to have in
189 # a config.worktree file outside of the main
190 # working tree, to check that Git filters them out
191 # when copying config during "git worktree add".
192 git config --worktree core.bare true &&
193 git config --worktree core.worktree "$(pwd)" &&
195 # We want to check that bogus.key is copied
196 git config --worktree bogus.key value &&
197 git config --unset core.bare &&
198 git worktree add ../there4 main &&
201 # Simple check that a Git command does not
202 # immediately fail with the current setup
204 git worktree add --detach ../there5 &&
209 # the worktree has the arbitrary value copied.
210 test_cmp_config -C there4 value bogus.key &&
211 test_cmp_config -C there5 value bogus.key &&
213 # however, core.bare and core.worktree were removed.
214 test_must_fail git -C there4 config core.bare &&
215 test_must_fail git -C there4 config core.worktree &&
217 cat >expect <<-EOF &&
222 test_cmp expect actual &&
224 test_cmp expect actual
227 test_expect_success
'checkout with grafts' '
228 test_when_finished rm .git/info/grafts &&
230 SHA1=$(git rev-parse HEAD) &&
234 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
235 cat >expected <<-\EOF &&
239 git log --format=%s -2 >actual &&
240 test_cmp expected actual &&
241 git worktree add --detach grafted main &&
242 git --git-dir=grafted/.git log --format=%s -2 >actual &&
243 test_cmp expected actual
246 test_expect_success
'"add" from relative HEAD' '
250 git rev-parse HEAD~1 >expected &&
251 git worktree add relhead HEAD~1 &&
252 git -C relhead rev-parse HEAD >actual &&
253 test_cmp expected actual
256 test_expect_success
'"add -b" with <branch> omitted' '
257 git worktree add -b burble flornk &&
258 test_cmp_rev HEAD burble
261 test_expect_success
'"add --detach" with <branch> omitted' '
262 git worktree add --detach fishhook &&
263 git rev-parse HEAD >expected &&
264 git -C fishhook rev-parse HEAD >actual &&
265 test_cmp expected actual &&
266 test_must_fail git -C fishhook symbolic-ref HEAD
269 test_expect_success
'"add" with <branch> omitted' '
270 git worktree add wiffle/bat &&
271 test_cmp_rev HEAD bat
274 test_expect_success
'"add" checks out existing branch of dwimd name' '
275 git branch dwim HEAD~1 &&
276 git worktree add dwim &&
277 test_cmp_rev HEAD~1 dwim &&
280 test_cmp_rev HEAD dwim
284 test_expect_success
'"add <path>" dwim fails with checked out branch' '
285 git checkout -b test-branch &&
286 test_must_fail git worktree add test-branch &&
287 test_path_is_missing test-branch
290 test_expect_success
'"add --force" with existing dwimd name doesnt die' '
291 git checkout test-branch &&
292 git worktree add --force test-branch
295 test_expect_success
'"add" no auto-vivify with --detach and <branch> omitted' '
296 git worktree add --detach mish/mash &&
297 test_must_fail git rev-parse mash -- &&
298 test_must_fail git -C mish/mash symbolic-ref HEAD
301 test_expect_success
'"add" -b/-B mutually exclusive' '
302 test_must_fail git worktree add -b poodle -B poodle bamboo main
305 test_expect_success
'"add" -b/--detach mutually exclusive' '
306 test_must_fail git worktree add -b poodle --detach bamboo main
309 test_expect_success
'"add" -B/--detach mutually exclusive' '
310 test_must_fail git worktree add -B poodle --detach bamboo main
313 test_expect_success
'"add -B" fails if the branch is checked out' '
314 git rev-parse newmain >before &&
315 test_must_fail git worktree add -B newmain bamboo main &&
316 git rev-parse newmain >after &&
317 test_cmp before after
320 test_expect_success
'add -B' '
321 git worktree add -B poodle bamboo2 main^ &&
322 git -C bamboo2 symbolic-ref HEAD >actual &&
323 echo refs/heads/poodle >expected &&
324 test_cmp expected actual &&
325 test_cmp_rev main^ poodle
328 test_expect_success
'add --quiet' '
329 git worktree add --quiet another-worktree main 2>actual &&
330 test_must_be_empty actual
333 test_expect_success
'local clone from linked checkout' '
334 git clone --local here here-clone &&
335 ( cd here-clone && git fsck )
338 test_expect_success
'local clone --shared from linked checkout' '
339 git -C bare worktree add --detach ../baretree &&
340 git clone --local --shared baretree bare-clone &&
341 grep /bare/ bare-clone/.git/objects/info/alternates
344 test_expect_success
'"add" worktree with --no-checkout' '
345 git worktree add --no-checkout -b swamp swamp &&
346 ! test -e swamp/init.t &&
347 git -C swamp reset --hard &&
348 test_cmp init.t swamp/init.t
351 test_expect_success
'"add" worktree with --checkout' '
352 git worktree add --checkout -b swmap2 swamp2 &&
353 test_cmp init.t swamp2/init.t
356 test_expect_success
'put a worktree under rebase' '
357 git worktree add under-rebase &&
361 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
362 git worktree list | grep "under-rebase.*detached HEAD"
366 test_expect_success
'add a worktree, checking out a rebased branch' '
367 test_must_fail git worktree add new-rebase under-rebase &&
371 test_expect_success
'checking out a rebased branch from another worktree' '
372 git worktree add new-place &&
373 test_must_fail git -C new-place checkout under-rebase
376 test_expect_success
'not allow to delete a branch under rebase' '
379 test_must_fail git branch -D under-rebase
383 test_expect_success
'rename a branch under rebase not allowed' '
384 test_must_fail git branch -M under-rebase rebase-with-new-name
387 test_expect_success
'check out from current worktree branch ok' '
390 git checkout under-rebase &&
396 test_expect_success
'checkout a branch under bisect' '
397 git worktree add under-bisect &&
402 git bisect good HEAD~2 &&
403 git worktree list | grep "under-bisect.*detached HEAD" &&
404 test_must_fail git worktree add new-bisect under-bisect &&
409 test_expect_success
'rename a branch under bisect not allowed' '
410 test_must_fail git branch -M under-bisect bisect-with-new-name
412 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
413 test_branch_upstream
() {
414 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream
&&
416 git config
"branch.$1.remote" &&
417 git config
"branch.$1.merge"
418 } >actual.upstream
&&
419 test_cmp expect.upstream actual.upstream
422 test_expect_success
'--track sets up tracking' '
423 test_when_finished rm -rf track &&
424 git worktree add --track -b track track main &&
425 test_branch_upstream track . main
428 # setup remote repository $1 and repository $2 with $1 set up as
429 # remote. The remote has two branches, main and foo.
430 setup_remote_repo
() {
434 test_commit
$1_main &&
435 git checkout
-b foo
&&
436 test_commit upstream_foo
441 test_commit
$2_main &&
442 git remote add
$1 ..
/$1 &&
443 git config remote.
$1.fetch \
444 "refs/heads/*:refs/remotes/$1/*" &&
449 test_expect_success
'--no-track avoids setting up tracking' '
450 test_when_finished rm -rf repo_upstream repo_local foo &&
451 setup_remote_repo repo_upstream repo_local &&
454 git worktree add --no-track -b foo ../foo repo_upstream/foo
458 test_must_fail git config "branch.foo.remote" &&
459 test_must_fail git config "branch.foo.merge" &&
460 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
464 test_expect_success
'"add" <path> <non-existent-branch> fails' '
465 test_must_fail git worktree add foo non-existent
468 test_expect_success
'"add" <path> <branch> dwims' '
469 test_when_finished rm -rf repo_upstream repo_dwim foo &&
470 setup_remote_repo repo_upstream repo_dwim &&
471 git init repo_dwim &&
474 git worktree add ../foo foo
478 test_branch_upstream foo repo_upstream foo &&
479 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
483 test_expect_success
'"add" <path> <branch> dwims with checkout.defaultRemote' '
484 test_when_finished rm -rf repo_upstream repo_dwim foo &&
485 setup_remote_repo repo_upstream repo_dwim &&
486 git init repo_dwim &&
489 git remote add repo_upstream2 ../repo_upstream &&
490 git fetch repo_upstream2 &&
491 test_must_fail git worktree add ../foo foo &&
492 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
493 git status -uno --porcelain >status.actual &&
494 test_must_be_empty status.actual
498 test_branch_upstream foo repo_upstream foo &&
499 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
503 test_expect_success
'git worktree add does not match remote' '
504 test_when_finished rm -rf repo_a repo_b foo &&
505 setup_remote_repo repo_a repo_b &&
508 git worktree add ../foo
512 test_must_fail git config "branch.foo.remote" &&
513 test_must_fail git config "branch.foo.merge" &&
514 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
518 test_expect_success
'git worktree add --guess-remote sets up tracking' '
519 test_when_finished rm -rf repo_a repo_b foo &&
520 setup_remote_repo repo_a repo_b &&
523 git worktree add --guess-remote ../foo
527 test_branch_upstream foo repo_a foo &&
528 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
532 test_expect_success
'git worktree add with worktree.guessRemote sets up tracking' '
533 test_when_finished rm -rf repo_a repo_b foo &&
534 setup_remote_repo repo_a repo_b &&
537 git config worktree.guessRemote true &&
538 git worktree add ../foo
542 test_branch_upstream foo repo_a foo &&
543 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
547 test_expect_success
'git worktree --no-guess-remote option overrides config' '
548 test_when_finished rm -rf repo_a repo_b foo &&
549 setup_remote_repo repo_a repo_b &&
552 git config worktree.guessRemote true &&
553 git worktree add --no-guess-remote ../foo
557 test_must_fail git config "branch.foo.remote" &&
558 test_must_fail git config "branch.foo.merge" &&
559 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
563 post_checkout_hook
() {
564 test_when_finished
"rm -rf .git/hooks" &&
566 test_hook
-C "$1" post-checkout
<<-\EOF
569 git rev-parse --git-dir --show-toplevel
574 test_expect_success '"add" invokes post-checkout hook (branch)' '
575 post_checkout_hook &&
577 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
578 echo $(pwd)/.git/worktrees/gumby &&
581 git worktree add gumby &&
582 test_cmp hook.expect gumby/hook.actual
585 test_expect_success '"add" invokes post-checkout hook (detached)' '
586 post_checkout_hook &&
588 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
589 echo $(pwd)/.git/worktrees/grumpy &&
592 git worktree add --detach grumpy &&
593 test_cmp hook.expect grumpy/hook.actual
596 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
597 post_checkout_hook &&
599 git worktree add --no-checkout gloopy &&
600 test_path_is_missing gloopy/hook.actual
603 test_expect_success '"add" in other worktree invokes post-checkout hook' '
604 post_checkout_hook &&
606 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
607 echo $(pwd)/.git/worktrees/guppy &&
610 git -C gloopy worktree add --detach ../guppy &&
611 test_cmp hook.expect guppy/hook.actual
614 test_expect_success '"add" in bare repo invokes post-checkout hook' '
616 git clone --bare . bare &&
618 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
619 echo $(pwd)/bare/worktrees/goozy &&
622 post_checkout_hook bare &&
623 git -C bare worktree add --detach ../goozy &&
624 test_cmp hook.expect goozy/hook.actual
627 test_expect_success '"add" an existing but missing worktree' '
628 git worktree add --detach pneu &&
629 test_must_fail git worktree add --detach pneu &&
631 test_must_fail git worktree add --detach pneu &&
632 git worktree add --force --detach pneu
635 test_expect_success '"add" an existing locked but missing worktree' '
636 git worktree add --detach gnoo &&
637 git worktree lock gnoo &&
638 test_when_finished "git worktree unlock gnoo || :" &&
640 test_must_fail git worktree add --detach gnoo &&
641 test_must_fail git worktree add --force --detach gnoo &&
642 git worktree add --force --force --detach gnoo
645 test_expect_success '"add" not tripped up by magic worktree matching"' '
646 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
647 # directory `sub2` should not mistakenly complain that `bar` is an
648 # already-registered worktree
650 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
651 git -C sub2 --git-dir=../.git worktree add --detach bozo
654 test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
655 git worktree add --detach ". weird*..?.lock.lock" &&
656 test -d .git/worktrees/---weird-.-
659 test_expect_success '"add" should not fail because of another bad worktree
' '
665 git worktree add sub
/to-be-deleted
&&
667 git worktree add second
671 test_expect_success '"add" with uninitialized submodule
, with submodule.recurse
unset' '
672 test_config_global protocol.
file.allow always
&&
673 test_create_repo submodule
&&
674 test_commit
-C submodule first
&&
675 test_create_repo project
&&
676 git
-C project submodule add ..
/submodule
&&
677 git
-C project add submodule
&&
679 git
-C project commit
-m add_sub
&&
680 git clone project project-clone
&&
681 git
-C project-clone worktree add ..
/project-2
683 test_expect_success '"add" with uninitialized submodule
, with submodule.recurse
set' '
684 git
-C project-clone
-c submodule.recurse worktree add ..
/project-3
687 test_expect_success '"add" with initialized submodule
, with submodule.recurse
unset' '
688 test_config_global protocol.
file.allow always
&&
689 git
-C project-clone submodule update
--init &&
690 git
-C project-clone worktree add ..
/project-4
693 test_expect_success '"add" with initialized submodule
, with submodule.recurse
set' '
694 git
-C project-clone
-c submodule.recurse worktree add ..
/project-5