3 test_description
='test git worktree add'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 .
"$TEST_DIRECTORY"/lib-rebase.sh
12 test_expect_success
'setup' '
16 test_expect_success
'"add" an existing worktree' '
17 mkdir -p existing/subtree &&
18 test_must_fail git worktree add --detach existing main
21 test_expect_success
'"add" an existing empty worktree' '
22 mkdir existing_empty &&
23 git worktree add --detach existing_empty main
26 test_expect_success
'"add" using shorthand - fails when no previous branch' '
27 test_must_fail git worktree add existing_short -
30 test_expect_success
'"add" using - shorthand' '
31 git checkout -b newbranch &&
32 echo hello >myworld &&
34 git commit -m myworld &&
36 git worktree add short-hand - &&
37 echo refs/heads/newbranch >expect &&
38 git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
39 test_cmp expect actual
42 test_expect_success
'"add" refuses to checkout locked branch' '
43 test_must_fail git worktree add zere main &&
45 ! test -d .git/worktrees/zere
48 test_expect_success
'checking out paths not complaining about linked checkouts' '
51 echo dirty >>init.t &&
52 git checkout main -- init.t
56 test_expect_success
'"add" worktree' '
57 git rev-parse HEAD >expect &&
58 git worktree add --detach here main &&
61 test_cmp ../init.t init.t &&
62 test_must_fail git symbolic-ref HEAD &&
63 git rev-parse HEAD >actual &&
64 test_cmp ../expect actual &&
69 test_expect_success
'"add" worktree with lock' '
70 git worktree add --detach --lock here-with-lock main &&
71 test_when_finished "git worktree unlock here-with-lock || :" &&
72 test -f .git/worktrees/here-with-lock/locked
75 test_expect_success
'"add" worktree with lock and reason' '
76 lock_reason="why not" &&
77 git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main &&
78 test_when_finished "git worktree unlock here-with-lock-reason || :" &&
79 test -f .git/worktrees/here-with-lock-reason/locked &&
80 echo "$lock_reason" >expect &&
81 test_cmp expect .git/worktrees/here-with-lock-reason/locked
84 test_expect_success
'"add" worktree with reason but no lock' '
85 test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main &&
86 test_path_is_missing .git/worktrees/here-with-reason-only/locked
89 test_expect_success
'"add" worktree from a subdir' '
93 git worktree add --detach here main &&
95 test_cmp ../../init.t init.t
99 test_expect_success
'"add" from a linked checkout' '
102 git worktree add --detach nested-here main &&
108 test_expect_success
'"add" worktree creating new branch' '
109 git worktree add -b newmain there main &&
112 test_cmp ../init.t init.t &&
113 git symbolic-ref HEAD >actual &&
114 echo refs/heads/newmain >expect &&
115 test_cmp expect actual &&
120 test_expect_success
'die the same branch is already checked out' '
123 test_must_fail git checkout newmain
127 test_expect_success SYMLINKS
'die the same branch is already checked out (symlink)' '
128 head=$(git -C there rev-parse --git-path HEAD) &&
129 ref=$(git -C there symbolic-ref HEAD) &&
131 ln -s "$ref" "$head" &&
132 test_must_fail git -C here checkout newmain
135 test_expect_success
'not die the same branch is already checked out' '
138 git worktree add --force anothernewmain newmain
142 test_expect_success
'not die on re-checking out current branch' '
149 test_expect_success
'"add" from a bare repo' '
151 git clone --bare . bare &&
153 git worktree add -b bare-main ../there2 main
157 test_expect_success
'checkout from a bare repo without "add"' '
160 test_must_fail git checkout main
164 test_expect_success
'"add" default branch of a bare repo' '
166 git clone --bare . bare2 &&
168 git worktree add ../there3 main &&
170 # Simple check that a Git command does not
171 # immediately fail with the current setup
174 cat >expect <<-EOF &&
178 test_cmp expect actual
181 test_expect_success
'"add" to bare repo with worktree config' '
183 git clone --bare . bare3 &&
185 git config extensions.worktreeconfig true &&
187 # Add config values that are erroneous to have in
188 # a config.worktree file outside of the main
189 # working tree, to check that Git filters them out
190 # when copying config during "git worktree add".
191 git config --worktree core.bare true &&
192 git config --worktree core.worktree "$(pwd)" &&
194 # We want to check that bogus.key is copied
195 git config --worktree bogus.key value &&
196 git config --unset core.bare &&
197 git worktree add ../there4 main &&
200 # Simple check that a Git command does not
201 # immediately fail with the current setup
203 git worktree add --detach ../there5 &&
208 # the worktree has the arbitrary value copied.
209 test_cmp_config -C there4 value bogus.key &&
210 test_cmp_config -C there5 value bogus.key &&
212 # however, core.bare and core.worktree were removed.
213 test_must_fail git -C there4 config core.bare &&
214 test_must_fail git -C there4 config core.worktree &&
216 cat >expect <<-EOF &&
221 test_cmp expect actual &&
223 test_cmp expect actual
226 test_expect_success
'checkout with grafts' '
227 test_when_finished rm .git/info/grafts &&
229 SHA1=$(git rev-parse HEAD) &&
232 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
233 cat >expected <<-\EOF &&
237 git log --format=%s -2 >actual &&
238 test_cmp expected actual &&
239 git worktree add --detach grafted main &&
240 git --git-dir=grafted/.git log --format=%s -2 >actual &&
241 test_cmp expected actual
244 test_expect_success
'"add" from relative HEAD' '
248 git rev-parse HEAD~1 >expected &&
249 git worktree add relhead HEAD~1 &&
250 git -C relhead rev-parse HEAD >actual &&
251 test_cmp expected actual
254 test_expect_success
'"add -b" with <branch> omitted' '
255 git worktree add -b burble flornk &&
256 test_cmp_rev HEAD burble
259 test_expect_success
'"add --detach" with <branch> omitted' '
260 git worktree add --detach fishhook &&
261 git rev-parse HEAD >expected &&
262 git -C fishhook rev-parse HEAD >actual &&
263 test_cmp expected actual &&
264 test_must_fail git -C fishhook symbolic-ref HEAD
267 test_expect_success
'"add" with <branch> omitted' '
268 git worktree add wiffle/bat &&
269 test_cmp_rev HEAD bat
272 test_expect_success
'"add" checks out existing branch of dwimd name' '
273 git branch dwim HEAD~1 &&
274 git worktree add dwim &&
275 test_cmp_rev HEAD~1 dwim &&
278 test_cmp_rev HEAD dwim
282 test_expect_success
'"add <path>" dwim fails with checked out branch' '
283 git checkout -b test-branch &&
284 test_must_fail git worktree add test-branch &&
285 test_path_is_missing test-branch
288 test_expect_success
'"add --force" with existing dwimd name doesnt die' '
289 git checkout test-branch &&
290 git worktree add --force test-branch
293 test_expect_success
'"add" no auto-vivify with --detach and <branch> omitted' '
294 git worktree add --detach mish/mash &&
295 test_must_fail git rev-parse mash -- &&
296 test_must_fail git -C mish/mash symbolic-ref HEAD
299 test_expect_success
'"add" -b/-B mutually exclusive' '
300 test_must_fail git worktree add -b poodle -B poodle bamboo main
303 test_expect_success
'"add" -b/--detach mutually exclusive' '
304 test_must_fail git worktree add -b poodle --detach bamboo main
307 test_expect_success
'"add" -B/--detach mutually exclusive' '
308 test_must_fail git worktree add -B poodle --detach bamboo main
311 test_expect_success
'"add -B" fails if the branch is checked out' '
312 git rev-parse newmain >before &&
313 test_must_fail git worktree add -B newmain bamboo main &&
314 git rev-parse newmain >after &&
315 test_cmp before after
318 test_expect_success
'add -B' '
319 git worktree add -B poodle bamboo2 main^ &&
320 git -C bamboo2 symbolic-ref HEAD >actual &&
321 echo refs/heads/poodle >expected &&
322 test_cmp expected actual &&
323 test_cmp_rev main^ poodle
326 test_expect_success
'add --quiet' '
327 git worktree add --quiet another-worktree main 2>actual &&
328 test_must_be_empty actual
331 test_expect_success
'local clone from linked checkout' '
332 git clone --local here here-clone &&
333 ( cd here-clone && git fsck )
336 test_expect_success
'local clone --shared from linked checkout' '
337 git -C bare worktree add --detach ../baretree &&
338 git clone --local --shared baretree bare-clone &&
339 grep /bare/ bare-clone/.git/objects/info/alternates
342 test_expect_success
'"add" worktree with --no-checkout' '
343 git worktree add --no-checkout -b swamp swamp &&
344 ! test -e swamp/init.t &&
345 git -C swamp reset --hard &&
346 test_cmp init.t swamp/init.t
349 test_expect_success
'"add" worktree with --checkout' '
350 git worktree add --checkout -b swmap2 swamp2 &&
351 test_cmp init.t swamp2/init.t
354 test_expect_success
'put a worktree under rebase' '
355 git worktree add under-rebase &&
359 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
360 git worktree list | grep "under-rebase.*detached HEAD"
364 test_expect_success
'add a worktree, checking out a rebased branch' '
365 test_must_fail git worktree add new-rebase under-rebase &&
369 test_expect_success
'checking out a rebased branch from another worktree' '
370 git worktree add new-place &&
371 test_must_fail git -C new-place checkout under-rebase
374 test_expect_success
'not allow to delete a branch under rebase' '
377 test_must_fail git branch -D under-rebase
381 test_expect_success
'rename a branch under rebase not allowed' '
382 test_must_fail git branch -M under-rebase rebase-with-new-name
385 test_expect_success
'check out from current worktree branch ok' '
388 git checkout under-rebase &&
394 test_expect_success
'checkout a branch under bisect' '
395 git worktree add under-bisect &&
400 git bisect good HEAD~2 &&
401 git worktree list | grep "under-bisect.*detached HEAD" &&
402 test_must_fail git worktree add new-bisect under-bisect &&
407 test_expect_success
'rename a branch under bisect not allowed' '
408 test_must_fail git branch -M under-bisect bisect-with-new-name
410 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
411 test_branch_upstream
() {
412 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream
&&
414 git config
"branch.$1.remote" &&
415 git config
"branch.$1.merge"
416 } >actual.upstream
&&
417 test_cmp expect.upstream actual.upstream
420 test_expect_success
'--track sets up tracking' '
421 test_when_finished rm -rf track &&
422 git worktree add --track -b track track main &&
423 test_branch_upstream track . main
426 # setup remote repository $1 and repository $2 with $1 set up as
427 # remote. The remote has two branches, main and foo.
428 setup_remote_repo
() {
432 test_commit
$1_main &&
433 git checkout
-b foo
&&
434 test_commit upstream_foo
439 test_commit
$2_main &&
440 git remote add
$1 ..
/$1 &&
441 git config remote.
$1.fetch \
442 "refs/heads/*:refs/remotes/$1/*" &&
447 test_expect_success
'--no-track avoids setting up tracking' '
448 test_when_finished rm -rf repo_upstream repo_local foo &&
449 setup_remote_repo repo_upstream repo_local &&
452 git worktree add --no-track -b foo ../foo repo_upstream/foo
456 test_must_fail git config "branch.foo.remote" &&
457 test_must_fail git config "branch.foo.merge" &&
458 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
462 test_expect_success
'"add" <path> <non-existent-branch> fails' '
463 test_must_fail git worktree add foo non-existent
466 test_expect_success
'"add" <path> <branch> dwims' '
467 test_when_finished rm -rf repo_upstream repo_dwim foo &&
468 setup_remote_repo repo_upstream repo_dwim &&
469 git init repo_dwim &&
472 git worktree add ../foo foo
476 test_branch_upstream foo repo_upstream foo &&
477 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
481 test_expect_success
'"add" <path> <branch> dwims with checkout.defaultRemote' '
482 test_when_finished rm -rf repo_upstream repo_dwim foo &&
483 setup_remote_repo repo_upstream repo_dwim &&
484 git init repo_dwim &&
487 git remote add repo_upstream2 ../repo_upstream &&
488 git fetch repo_upstream2 &&
489 test_must_fail git worktree add ../foo foo &&
490 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
491 git status -uno --porcelain >status.actual &&
492 test_must_be_empty status.actual
496 test_branch_upstream foo repo_upstream foo &&
497 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
501 test_expect_success
'git worktree add does not match remote' '
502 test_when_finished rm -rf repo_a repo_b foo &&
503 setup_remote_repo repo_a repo_b &&
506 git worktree add ../foo
510 test_must_fail git config "branch.foo.remote" &&
511 test_must_fail git config "branch.foo.merge" &&
512 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
516 test_expect_success
'git worktree add --guess-remote sets up tracking' '
517 test_when_finished rm -rf repo_a repo_b foo &&
518 setup_remote_repo repo_a repo_b &&
521 git worktree add --guess-remote ../foo
525 test_branch_upstream foo repo_a foo &&
526 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
530 test_expect_success
'git worktree add with worktree.guessRemote sets up tracking' '
531 test_when_finished rm -rf repo_a repo_b foo &&
532 setup_remote_repo repo_a repo_b &&
535 git config worktree.guessRemote true &&
536 git worktree add ../foo
540 test_branch_upstream foo repo_a foo &&
541 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
545 test_expect_success
'git worktree --no-guess-remote option overrides config' '
546 test_when_finished rm -rf repo_a repo_b foo &&
547 setup_remote_repo repo_a repo_b &&
550 git config worktree.guessRemote true &&
551 git worktree add --no-guess-remote ../foo
555 test_must_fail git config "branch.foo.remote" &&
556 test_must_fail git config "branch.foo.merge" &&
557 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
561 post_checkout_hook
() {
562 test_hook
-C "$1" post-checkout
<<-\EOF
565 git rev-parse --git-dir --show-toplevel
570 test_expect_success '"add" invokes post-checkout hook (branch)' '
571 post_checkout_hook &&
573 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
574 echo $(pwd)/.git/worktrees/gumby &&
577 git worktree add gumby &&
578 test_cmp hook.expect gumby/hook.actual
581 test_expect_success '"add" invokes post-checkout hook (detached)' '
582 post_checkout_hook &&
584 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
585 echo $(pwd)/.git/worktrees/grumpy &&
588 git worktree add --detach grumpy &&
589 test_cmp hook.expect grumpy/hook.actual
592 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
593 post_checkout_hook &&
595 git worktree add --no-checkout gloopy &&
596 test_path_is_missing gloopy/hook.actual
599 test_expect_success '"add" in other worktree invokes post-checkout hook' '
600 post_checkout_hook &&
602 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
603 echo $(pwd)/.git/worktrees/guppy &&
606 git -C gloopy worktree add --detach ../guppy &&
607 test_cmp hook.expect guppy/hook.actual
610 test_expect_success '"add" in bare repo invokes post-checkout hook' '
612 git clone --bare . bare &&
614 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
615 echo $(pwd)/bare/worktrees/goozy &&
618 post_checkout_hook bare &&
619 git -C bare worktree add --detach ../goozy &&
620 test_cmp hook.expect goozy/hook.actual
623 test_expect_success '"add" an existing but missing worktree' '
624 git worktree add --detach pneu &&
625 test_must_fail git worktree add --detach pneu &&
627 test_must_fail git worktree add --detach pneu &&
628 git worktree add --force --detach pneu
631 test_expect_success '"add" an existing locked but missing worktree' '
632 git worktree add --detach gnoo &&
633 git worktree lock gnoo &&
634 test_when_finished "git worktree unlock gnoo || :" &&
636 test_must_fail git worktree add --detach gnoo &&
637 test_must_fail git worktree add --force --detach gnoo &&
638 git worktree add --force --force --detach gnoo
641 test_expect_success '"add" not tripped up by magic worktree matching"' '
642 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
643 # directory `sub2` should not mistakenly complain that `bar` is an
644 # already-registered worktree
646 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
647 git -C sub2 --git-dir=../.git worktree add --detach bozo
650 test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
651 git worktree add --detach ". weird*..?.lock.lock" &&
652 test -d .git/worktrees/---weird-.-
655 test_expect_success '"add" should not fail because of another bad worktree
' '
661 git worktree add sub
/to-be-deleted
&&
663 git worktree add second
667 test_expect_success '"add" with uninitialized submodule
, with submodule.recurse
unset' '
668 test_create_repo submodule
&&
669 test_commit
-C submodule first
&&
670 test_create_repo project
&&
671 git
-C project submodule add ..
/submodule
&&
672 git
-C project add submodule
&&
674 git
-C project commit
-m add_sub
&&
675 git clone project project-clone
&&
676 git
-C project-clone worktree add ..
/project-2
678 test_expect_success '"add" with uninitialized submodule
, with submodule.recurse
set' '
679 git
-C project-clone
-c submodule.recurse worktree add ..
/project-3
682 test_expect_success '"add" with initialized submodule
, with submodule.recurse
unset' '
683 git
-C project-clone submodule update
--init &&
684 git
-C project-clone worktree add ..
/project-4
687 test_expect_success '"add" with initialized submodule
, with submodule.recurse
set' '
688 git
-C project-clone
-c submodule.recurse worktree add ..
/project-5