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 2>actual &&
125 grep "already used by worktree at" actual
129 test_expect_success SYMLINKS
'die the same branch is already checked out (symlink)' '
130 head=$(git -C there rev-parse --git-path HEAD) &&
131 ref=$(git -C there symbolic-ref HEAD) &&
133 ln -s "$ref" "$head" &&
134 test_must_fail git -C here checkout newmain
137 test_expect_success
'not die the same branch is already checked out' '
140 git worktree add --force anothernewmain newmain
144 test_expect_success
'not die on re-checking out current branch' '
151 test_expect_success
'"add" from a bare repo' '
153 git clone --bare . bare &&
155 git worktree add -b bare-main ../there2 main
159 test_expect_success
'checkout from a bare repo without "add"' '
162 test_must_fail git checkout main
166 test_expect_success
'"add" default branch of a bare repo' '
168 git clone --bare . bare2 &&
170 git worktree add ../there3 main &&
172 # Simple check that a Git command does not
173 # immediately fail with the current setup
176 cat >expect <<-EOF &&
180 test_cmp expect actual
183 test_expect_success
'"add" to bare repo with worktree config' '
185 git clone --bare . bare3 &&
187 git config extensions.worktreeconfig true &&
189 # Add config values that are erroneous to have in
190 # a config.worktree file outside of the main
191 # working tree, to check that Git filters them out
192 # when copying config during "git worktree add".
193 git config --worktree core.bare true &&
194 git config --worktree core.worktree "$(pwd)" &&
196 # We want to check that bogus.key is copied
197 git config --worktree bogus.key value &&
198 git config --unset core.bare &&
199 git worktree add ../there4 main &&
202 # Simple check that a Git command does not
203 # immediately fail with the current setup
205 git worktree add --detach ../there5 &&
210 # the worktree has the arbitrary value copied.
211 test_cmp_config -C there4 value bogus.key &&
212 test_cmp_config -C there5 value bogus.key &&
214 # however, core.bare and core.worktree were removed.
215 test_must_fail git -C there4 config core.bare &&
216 test_must_fail git -C there4 config core.worktree &&
218 cat >expect <<-EOF &&
223 test_cmp expect actual &&
225 test_cmp expect actual
228 test_expect_success
'checkout with grafts' '
229 test_when_finished rm .git/info/grafts &&
231 SHA1=$(git rev-parse HEAD) &&
235 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
236 cat >expected <<-\EOF &&
240 git log --format=%s -2 >actual &&
241 test_cmp expected actual &&
242 git worktree add --detach grafted main &&
243 git --git-dir=grafted/.git log --format=%s -2 >actual &&
244 test_cmp expected actual
247 test_expect_success
'"add" from relative HEAD' '
251 git rev-parse HEAD~1 >expected &&
252 git worktree add relhead HEAD~1 &&
253 git -C relhead rev-parse HEAD >actual &&
254 test_cmp expected actual
257 test_expect_success
'"add -b" with <branch> omitted' '
258 git worktree add -b burble flornk &&
259 test_cmp_rev HEAD burble
262 test_expect_success
'"add --detach" with <branch> omitted' '
263 git worktree add --detach fishhook &&
264 git rev-parse HEAD >expected &&
265 git -C fishhook rev-parse HEAD >actual &&
266 test_cmp expected actual &&
267 test_must_fail git -C fishhook symbolic-ref HEAD
270 test_expect_success
'"add" with <branch> omitted' '
271 git worktree add wiffle/bat &&
272 test_cmp_rev HEAD bat
275 test_expect_success
'"add" checks out existing branch of dwimd name' '
276 git branch dwim HEAD~1 &&
277 git worktree add dwim &&
278 test_cmp_rev HEAD~1 dwim &&
281 test_cmp_rev HEAD dwim
285 test_expect_success
'"add <path>" dwim fails with checked out branch' '
286 git checkout -b test-branch &&
287 test_must_fail git worktree add test-branch &&
288 test_path_is_missing test-branch
291 test_expect_success
'"add --force" with existing dwimd name doesnt die' '
292 git checkout test-branch &&
293 git worktree add --force test-branch
296 test_expect_success
'"add" no auto-vivify with --detach and <branch> omitted' '
297 git worktree add --detach mish/mash &&
298 test_must_fail git rev-parse mash -- &&
299 test_must_fail git -C mish/mash symbolic-ref HEAD
302 # Helper function to test mutually exclusive options.
304 # Note: Quoted arguments containing spaces are not supported.
305 test_wt_add_excl
() {
307 test_expect_success
"'worktree add' with '$opts' has mutually exclusive options" '
308 test_must_fail git worktree add $opts 2>actual &&
309 grep -E "fatal:( options)? .* cannot be used together" actual
313 test_wt_add_excl
-b poodle
-B poodle bamboo main
314 test_wt_add_excl
-b poodle
--detach bamboo main
315 test_wt_add_excl
-B poodle
--detach bamboo main
316 test_wt_add_excl
--orphan --detach bamboo
317 test_wt_add_excl
--orphan --no-checkout bamboo
318 test_wt_add_excl
--orphan bamboo main
319 test_wt_add_excl
--orphan -b bamboo wtdir
/ main
321 test_expect_success
'"add -B" fails if the branch is checked out' '
322 git rev-parse newmain >before &&
323 test_must_fail git worktree add -B newmain bamboo main &&
324 git rev-parse newmain >after &&
325 test_cmp before after
328 test_expect_success
'add -B' '
329 git worktree add -B poodle bamboo2 main^ &&
330 git -C bamboo2 symbolic-ref HEAD >actual &&
331 echo refs/heads/poodle >expected &&
332 test_cmp expected actual &&
333 test_cmp_rev main^ poodle
336 test_expect_success
'add --quiet' '
337 test_when_finished "git worktree remove -f -f another-worktree" &&
338 git worktree add --quiet another-worktree main 2>actual &&
339 test_must_be_empty actual
342 test_expect_success
'add --quiet -b' '
343 test_when_finished "git branch -D quietnewbranch" &&
344 test_when_finished "git worktree remove -f -f another-worktree" &&
345 git worktree add --quiet -b quietnewbranch another-worktree 2>actual &&
346 test_must_be_empty actual
349 test_expect_success
'"add --orphan"' '
350 test_when_finished "git worktree remove -f -f orphandir" &&
351 git worktree add --orphan -b neworphan orphandir &&
352 echo refs/heads/neworphan >expected &&
353 git -C orphandir symbolic-ref HEAD >actual &&
354 test_cmp expected actual
357 test_expect_success
'"add --orphan (no -b)"' '
358 test_when_finished "git worktree remove -f -f neworphan" &&
359 git worktree add --orphan neworphan &&
360 echo refs/heads/neworphan >expected &&
361 git -C neworphan symbolic-ref HEAD >actual &&
362 test_cmp expected actual
365 test_expect_success
'"add --orphan --quiet"' '
366 test_when_finished "git worktree remove -f -f orphandir" &&
367 git worktree add --quiet --orphan -b neworphan orphandir 2>log.actual &&
368 test_must_be_empty log.actual &&
369 echo refs/heads/neworphan >expected &&
370 git -C orphandir symbolic-ref HEAD >actual &&
371 test_cmp expected actual
374 test_expect_success
'"add --orphan" fails if the branch already exists' '
375 test_when_finished "git branch -D existingbranch" &&
376 git worktree add -b existingbranch orphandir main &&
377 git worktree remove orphandir &&
378 test_must_fail git worktree add --orphan -b existingbranch orphandir
381 test_expect_success
'"add --orphan" with empty repository' '
382 test_when_finished "rm -rf empty_repo" &&
383 echo refs/heads/newbranch >expected &&
384 GIT_DIR="empty_repo" git init --bare &&
385 git -C empty_repo worktree add --orphan -b newbranch worktreedir &&
386 git -C empty_repo/worktreedir symbolic-ref HEAD >actual &&
387 test_cmp expected actual
390 test_expect_success
'"add" worktree with orphan branch and lock' '
391 git worktree add --lock --orphan -b orphanbr orphan-with-lock &&
392 test_when_finished "git worktree unlock orphan-with-lock || :" &&
393 test -f .git/worktrees/orphan-with-lock/locked
396 test_expect_success
'"add" worktree with orphan branch, lock, and reason' '
397 lock_reason="why not" &&
398 git worktree add --detach --lock --reason "$lock_reason" orphan-with-lock-reason main &&
399 test_when_finished "git worktree unlock orphan-with-lock-reason || :" &&
400 test -f .git/worktrees/orphan-with-lock-reason/locked &&
401 echo "$lock_reason" >expect &&
402 test_cmp expect .git/worktrees/orphan-with-lock-reason/locked
405 # Note: Quoted arguments containing spaces are not supported.
406 test_wt_add_orphan_hint
() {
407 local context
="$1" &&
408 local use_branch
=$2 &&
411 test_expect_success
"'worktree add' show orphan hint in bad/orphan HEAD w/ $context" '
412 test_when_finished "rm -rf repo" &&
414 (cd repo && test_commit commit) &&
415 git -C repo switch --orphan noref &&
416 test_must_fail git -C repo worktree add $opts foobar/ 2>actual &&
417 ! grep "error: unknown switch" actual &&
418 grep "hint: If you meant to create a worktree containing a new unborn branch" actual &&
419 if [ $use_branch -eq 1 ]
421 grep -E "^hint: +git worktree add --orphan -b [^ ]+ [^ ]+$" actual
423 grep -E "^hint: +git worktree add --orphan [^ ]+$" actual
429 test_wt_add_orphan_hint
'no opts' 0
430 test_wt_add_orphan_hint
'-b' 1 -b foobar_branch
431 test_wt_add_orphan_hint
'-B' 1 -B foobar_branch
433 test_expect_success
"'worktree add' doesn't show orphan hint in bad/orphan HEAD w/ --quiet" '
434 test_when_finished "rm -rf repo" &&
436 (cd repo && test_commit commit) &&
437 test_must_fail git -C repo worktree add --quiet foobar_branch foobar/ 2>actual &&
438 ! grep "error: unknown switch" actual &&
439 ! grep "hint: If you meant to create a worktree containing a new unborn branch" actual
442 test_expect_success
'local clone from linked checkout' '
443 git clone --local here here-clone &&
444 ( cd here-clone && git fsck )
447 test_expect_success
'local clone --shared from linked checkout' '
448 git -C bare worktree add --detach ../baretree &&
449 git clone --local --shared baretree bare-clone &&
450 grep /bare/ bare-clone/.git/objects/info/alternates
453 test_expect_success
'"add" worktree with --no-checkout' '
454 git worktree add --no-checkout -b swamp swamp &&
455 ! test -e swamp/init.t &&
456 git -C swamp reset --hard &&
457 test_cmp init.t swamp/init.t
460 test_expect_success
'"add" worktree with --checkout' '
461 git worktree add --checkout -b swmap2 swamp2 &&
462 test_cmp init.t swamp2/init.t
465 test_expect_success
'put a worktree under rebase' '
466 git worktree add under-rebase &&
470 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
471 git worktree list | grep "under-rebase.*detached HEAD"
475 test_expect_success
'add a worktree, checking out a rebased branch' '
476 test_must_fail git worktree add new-rebase under-rebase &&
480 test_expect_success
'checking out a rebased branch from another worktree' '
481 git worktree add new-place &&
482 test_must_fail git -C new-place checkout under-rebase
485 test_expect_success
'not allow to delete a branch under rebase' '
488 test_must_fail git branch -D under-rebase
492 test_expect_success
'rename a branch under rebase not allowed' '
493 test_must_fail git branch -M under-rebase rebase-with-new-name
496 test_expect_success
'check out from current worktree branch ok' '
499 git checkout under-rebase &&
505 test_expect_success
'checkout a branch under bisect' '
506 git worktree add under-bisect &&
511 git bisect good HEAD~2 &&
512 git worktree list | grep "under-bisect.*detached HEAD" &&
513 test_must_fail git worktree add new-bisect under-bisect &&
518 test_expect_success
'rename a branch under bisect not allowed' '
519 test_must_fail git branch -M under-bisect bisect-with-new-name
521 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
522 test_branch_upstream
() {
523 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream
&&
525 git config
"branch.$1.remote" &&
526 git config
"branch.$1.merge"
527 } >actual.upstream
&&
528 test_cmp expect.upstream actual.upstream
531 test_expect_success
'--track sets up tracking' '
532 test_when_finished rm -rf track &&
533 git worktree add --track -b track track main &&
534 test_branch_upstream track . main
537 # setup remote repository $1 and repository $2 with $1 set up as
538 # remote. The remote has two branches, main and foo.
539 setup_remote_repo
() {
543 test_commit
$1_main &&
544 git checkout
-b foo
&&
545 test_commit upstream_foo
550 test_commit
$2_main &&
551 git remote add
$1 ..
/$1 &&
552 git config remote.
$1.fetch \
553 "refs/heads/*:refs/remotes/$1/*" &&
558 test_expect_success
'"add" <path> <remote/branch> w/ no HEAD' '
559 test_when_finished rm -rf repo_upstream repo_local foo &&
560 setup_remote_repo repo_upstream repo_local &&
561 git -C repo_local config --bool core.bare true &&
562 git -C repo_local branch -D main &&
563 git -C repo_local worktree add ./foo repo_upstream/foo
566 test_expect_success
'--no-track avoids setting up tracking' '
567 test_when_finished rm -rf repo_upstream repo_local foo &&
568 setup_remote_repo repo_upstream repo_local &&
571 git worktree add --no-track -b foo ../foo repo_upstream/foo
575 test_must_fail git config "branch.foo.remote" &&
576 test_must_fail git config "branch.foo.merge" &&
577 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
581 test_expect_success
'"add" <path> <non-existent-branch> fails' '
582 test_must_fail git worktree add foo non-existent
585 test_expect_success
'"add" <path> <branch> dwims' '
586 test_when_finished rm -rf repo_upstream repo_dwim foo &&
587 setup_remote_repo repo_upstream repo_dwim &&
588 git init repo_dwim &&
591 git worktree add ../foo foo
595 test_branch_upstream foo repo_upstream foo &&
596 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
600 test_expect_success
'"add" <path> <branch> dwims with checkout.defaultRemote' '
601 test_when_finished rm -rf repo_upstream repo_dwim foo &&
602 setup_remote_repo repo_upstream repo_dwim &&
603 git init repo_dwim &&
606 git remote add repo_upstream2 ../repo_upstream &&
607 git fetch repo_upstream2 &&
608 test_must_fail git worktree add ../foo foo &&
609 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
610 git status -uno --porcelain >status.actual &&
611 test_must_be_empty status.actual
615 test_branch_upstream foo repo_upstream foo &&
616 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
620 test_expect_success
'git worktree add does not match remote' '
621 test_when_finished rm -rf repo_a repo_b foo &&
622 setup_remote_repo repo_a repo_b &&
625 git worktree add ../foo
629 test_must_fail git config "branch.foo.remote" &&
630 test_must_fail git config "branch.foo.merge" &&
631 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
635 test_expect_success
'git worktree add --guess-remote sets up tracking' '
636 test_when_finished rm -rf repo_a repo_b foo &&
637 setup_remote_repo repo_a repo_b &&
640 git worktree add --guess-remote ../foo
644 test_branch_upstream foo repo_a foo &&
645 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
648 test_expect_success
'git worktree add --guess-remote sets up tracking (quiet)' '
649 test_when_finished rm -rf repo_a repo_b foo &&
650 setup_remote_repo repo_a repo_b &&
653 git worktree add --quiet --guess-remote ../foo 2>actual &&
654 test_must_be_empty actual
658 test_branch_upstream foo repo_a foo &&
659 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
663 test_expect_success
'git worktree --no-guess-remote (quiet)' '
664 test_when_finished rm -rf repo_a repo_b foo &&
665 setup_remote_repo repo_a repo_b &&
668 git worktree add --quiet --no-guess-remote ../foo
672 test_must_fail git config "branch.foo.remote" &&
673 test_must_fail git config "branch.foo.merge" &&
674 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
678 test_expect_success
'git worktree add with worktree.guessRemote sets up tracking' '
679 test_when_finished rm -rf repo_a repo_b foo &&
680 setup_remote_repo repo_a repo_b &&
683 git config worktree.guessRemote true &&
684 git worktree add ../foo
688 test_branch_upstream foo repo_a foo &&
689 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
693 test_expect_success
'git worktree --no-guess-remote option overrides config' '
694 test_when_finished rm -rf repo_a repo_b foo &&
695 setup_remote_repo repo_a repo_b &&
698 git config worktree.guessRemote true &&
699 git worktree add --no-guess-remote ../foo
703 test_must_fail git config "branch.foo.remote" &&
704 test_must_fail git config "branch.foo.merge" &&
705 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
709 test_dwim_orphan
() {
710 local info_text
="No possible source branch, inferring '--orphan'" &&
711 local fetch_error_text
="fatal: No local or remote refs exist despite at least one remote" &&
712 local orphan_hint
="hint: If you meant to create a worktree containing a new unborn branch" &&
713 local invalid_ref_regex
="^fatal: invalid reference: " &&
714 local bad_combo_regex
="^fatal: options '[-a-z]*' and '[-a-z]*' cannot be used together" &&
716 local git_ns
="repo" &&
717 local dashc_args
="-C $git_ns" &&
721 local empty_repo
=1 &&
725 local remote_ref
=0 &&
726 local use_detach
=0 &&
727 local use_new_branch
=0 &&
729 local outcome
="$1" &&
730 local outcome_text
&&
738 outcome_text
='"add" DWIM infer --orphan'
742 outcome_text
='"add" DWIM doesnt infer --orphan'
746 outcome_text
='"add" error need fetch'
748 "fatal_orphan_bad_combo")
750 outcome_text
='"add" error inferred "--orphan" gives illegal opts combo'
754 outcome_text
='"add" error, warn on bad HEAD, hint use orphan'
757 echo "test_dwim_orphan(): invalid outcome: '$outcome'" >&2 &&
764 # How and from where to create the worktree
768 dashc_args
="-C $git_ns" &&
769 context
="$context, 'git -C repo'"
774 dashc_args
="-C $git_ns" &&
775 context
="$context, 'git -C wt'"
781 context
="$context, 'cd repo && git'"
787 context
="$context, 'cd wt && git'"
790 # Bypass the "pull first" warning
792 args
="$args --force" &&
793 context
="$context, --force"
796 # Try to use remote refs when DWIM
798 args
="$args --guess-remote" &&
799 context
="$context, --guess-remote"
802 args
="$args --no-guess-remote" &&
803 context
="$context, --no-guess-remote"
806 # Whether there is at least one local branch present
810 context
="$context, >=1 local branches"
814 context
="$context, 0 local branches"
817 # Whether the HEAD points at a valid ref (skip this opt when no refs)
819 # requires: local_ref
820 context
="$context, valid HEAD"
824 context
="$context, invalid (or orphan) HEAD"
827 # Whether the code path is tested with the base add command, -b, or --detach
830 context
="$context, no --branch"
834 context
="$context, --branch"
838 context
="$context, --detach"
841 # Whether to check that all output is suppressed (except errors)
842 # or that the output is as expected
845 args
="$args --quiet" &&
846 context
="$context, --quiet"
850 context
="$context, no --quiet (expect output)"
853 # Whether there is at least one remote attached to the repo
857 context
="$context, >=1 remotes"
862 context
="$context, 0 remotes"
865 # Whether there is at least one valid remote ref
870 context
="$context, >=1 fetched remote branches"
875 context
="$context, 0 fetched remote branches"
878 # Options or flags that become illegal when --orphan is inferred
880 args
="$args --no-checkout" &&
881 context
="$context, --no-checkout"
884 args
="$args --track" &&
885 context
="$context, --track"
888 # All other options are illegal
890 echo "test_dwim_orphan(): invalid arg: '$1'" >&2 &&
896 context
="${context#', '}" &&
897 if [ $use_new_branch -eq 1 ]
900 elif [ $use_detach -eq 1 ]
902 args
="$args --detach"
904 context
="DWIM (no --branch), $context"
906 if [ $empty_repo -eq 1 ]
908 context
="empty repo, $context"
910 args
="$args ../foo" &&
911 context
="${context%', '}" &&
912 test_expect_success
"$outcome_text w/ $context" '
913 test_when_finished "rm -rf repo" &&
915 if [ $local_ref -eq 1 ] && [ "$git_ns" = "repo" ]
917 (cd repo && test_commit commit) &&
918 if [ $bad_head -eq 1 ]
920 git -C repo symbolic-ref HEAD refs/heads/badbranch
922 elif [ $local_ref -eq 1 ] && [ "$git_ns" = "wt" ]
924 test_when_finished "git -C repo worktree remove -f ../wt" &&
925 git -C repo worktree add --orphan -b main ../wt &&
926 (cd wt && test_commit commit) &&
927 if [ $bad_head -eq 1 ]
929 git -C wt symbolic-ref HEAD refs/heads/badbranch
931 elif [ $local_ref -eq 0 ] && [ "$git_ns" = "wt" ]
933 test_when_finished "git -C repo worktree remove -f ../wt" &&
934 git -C repo worktree add --orphan -b orphanbranch ../wt
939 test_when_finished "rm -rf upstream" &&
941 (cd upstream && test_commit commit) &&
942 git -C upstream switch -c foo &&
943 git -C repo remote add upstream ../upstream
946 if [ $remote_ref -eq 1 ]
950 if [ $success -eq 1 ]
952 test_when_finished git -C repo worktree remove ../foo
959 if [ "$outcome" = "infer" ]
961 git $dashc_args worktree add $args 2>actual &&
962 if [ $use_quiet -eq 1 ]
964 test_must_be_empty actual
966 grep "$info_text" actual
968 elif [ "$outcome" = "no_infer" ]
970 git $dashc_args worktree add $args 2>actual &&
971 if [ $use_quiet -eq 1 ]
973 test_must_be_empty actual
975 ! grep "$info_text" actual
977 elif [ "$outcome" = "fetch_error" ]
979 test_must_fail git $dashc_args worktree add $args 2>actual &&
980 grep "$fetch_error_text" actual
981 elif [ "$outcome" = "fatal_orphan_bad_combo" ]
983 test_must_fail git $dashc_args worktree add $args 2>actual &&
984 if [ $use_quiet -eq 1 ]
986 ! grep "$info_text" actual
988 grep "$info_text" actual
990 grep "$bad_combo_regex" actual
991 elif [ "$outcome" = "warn_bad_head" ]
993 test_must_fail git $dashc_args worktree add $args 2>actual &&
994 if [ $use_quiet -eq 1 ]
996 grep "$invalid_ref_regex" actual &&
997 ! grep "$orphan_hint" actual
999 headpath=$(git $dashc_args rev-parse --path-format=absolute --git-path HEAD) &&
1000 headcontents=$(cat "$headpath") &&
1001 grep "HEAD points to an invalid (or orphaned) reference" actual &&
1002 grep "HEAD path: .$headpath." actual &&
1003 grep "HEAD contents: .$headcontents." actual &&
1004 grep "$orphan_hint" actual &&
1005 ! grep "$info_text" actual
1007 grep "$invalid_ref_regex" actual
1013 if [ $success -ne 1 ]
1015 test_path_is_missing foo
1020 for quiet_mode
in "no_quiet" "quiet"
1022 for changedir_type
in "cd_repo" "cd_wt" "-C_repo" "-C_wt"
1024 dwim_test_args
="$quiet_mode $changedir_type"
1025 test_dwim_orphan
'infer' $dwim_test_args no_-b
1026 test_dwim_orphan
'no_infer' $dwim_test_args no_-b local_ref good_head
1027 test_dwim_orphan
'infer' $dwim_test_args no_-b no_local_ref no_remote no_remote_ref no_guess_remote
1028 test_dwim_orphan
'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref no_guess_remote
1029 test_dwim_orphan
'fetch_error' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote
1030 test_dwim_orphan
'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote force
1031 test_dwim_orphan
'no_infer' $dwim_test_args no_-b no_local_ref remote remote_ref guess_remote
1033 test_dwim_orphan
'infer' $dwim_test_args -b
1034 test_dwim_orphan
'no_infer' $dwim_test_args -b local_ref good_head
1035 test_dwim_orphan
'infer' $dwim_test_args -b no_local_ref no_remote no_remote_ref no_guess_remote
1036 test_dwim_orphan
'infer' $dwim_test_args -b no_local_ref remote no_remote_ref no_guess_remote
1037 test_dwim_orphan
'infer' $dwim_test_args -b no_local_ref remote no_remote_ref guess_remote
1038 test_dwim_orphan
'infer' $dwim_test_args -b no_local_ref remote remote_ref guess_remote
1040 test_dwim_orphan
'warn_bad_head' $dwim_test_args no_-b local_ref bad_head
1041 test_dwim_orphan
'warn_bad_head' $dwim_test_args -b local_ref bad_head
1042 test_dwim_orphan
'warn_bad_head' $dwim_test_args detach local_ref bad_head
1045 test_dwim_orphan
'fatal_orphan_bad_combo' $quiet_mode no_-b no_checkout
1046 test_dwim_orphan
'fatal_orphan_bad_combo' $quiet_mode no_-b track
1047 test_dwim_orphan
'fatal_orphan_bad_combo' $quiet_mode -b no_checkout
1048 test_dwim_orphan
'fatal_orphan_bad_combo' $quiet_mode -b track
1051 post_checkout_hook
() {
1052 test_when_finished
"rm -rf .git/hooks" &&
1054 test_hook
-C "$1" post-checkout
<<-\EOF
1057 git rev-parse --git-dir --show-toplevel
1062 test_expect_success '"add" invokes post-checkout hook (branch)' '
1063 post_checkout_hook &&
1065 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1066 echo $(pwd)/.git/worktrees/gumby &&
1069 git worktree add gumby &&
1070 test_cmp hook.expect gumby/hook.actual
1073 test_expect_success '"add" invokes post-checkout hook (detached)' '
1074 post_checkout_hook &&
1076 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1077 echo $(pwd)/.git/worktrees/grumpy &&
1080 git worktree add --detach grumpy &&
1081 test_cmp hook.expect grumpy/hook.actual
1084 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
1085 post_checkout_hook &&
1086 rm -f hook.actual &&
1087 git worktree add --no-checkout gloopy &&
1088 test_path_is_missing gloopy/hook.actual
1091 test_expect_success '"add" in other worktree invokes post-checkout hook' '
1092 post_checkout_hook &&
1094 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1095 echo $(pwd)/.git/worktrees/guppy &&
1098 git -C gloopy worktree add --detach ../guppy &&
1099 test_cmp hook.expect guppy/hook.actual
1102 test_expect_success '"add" in bare repo invokes post-checkout hook' '
1104 git clone --bare . bare &&
1106 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
1107 echo $(pwd)/bare/worktrees/goozy &&
1110 post_checkout_hook bare &&
1111 git -C bare worktree add --detach ../goozy &&
1112 test_cmp hook.expect goozy/hook.actual
1115 test_expect_success '"add" an existing but missing worktree' '
1116 git worktree add --detach pneu &&
1117 test_must_fail git worktree add --detach pneu &&
1119 test_must_fail git worktree add --detach pneu &&
1120 git worktree add --force --detach pneu
1123 test_expect_success '"add" an existing locked but missing worktree' '
1124 git worktree add --detach gnoo &&
1125 git worktree lock gnoo &&
1126 test_when_finished "git worktree unlock gnoo || :" &&
1128 test_must_fail git worktree add --detach gnoo &&
1129 test_must_fail git worktree add --force --detach gnoo &&
1130 git worktree add --force --force --detach gnoo
1133 test_expect_success '"add" not tripped up by magic worktree matching"' '
1134 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
1135 # directory `sub2` should not mistakenly complain that `bar` is an
1136 # already-registered worktree
1138 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
1139 git -C sub2 --git-dir=../.git worktree add --detach bozo
1142 test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
1143 git worktree add --detach ". weird*..?.lock.lock" &&
1144 test -d .git/worktrees/---weird-.-
1147 test_expect_success '"add" should not fail because of another bad worktree
' '
1148 git init add-fail
&&
1151 test_commit first
&&
1153 git worktree add sub
/to-be-deleted
&&
1155 git worktree add second
1159 test_expect_success '"add" with uninitialized submodule
, with submodule.recurse
unset' '
1160 test_config_global protocol.
file.allow always
&&
1161 test_create_repo submodule
&&
1162 test_commit
-C submodule first
&&
1163 test_create_repo project
&&
1164 git
-C project submodule add ..
/submodule
&&
1165 git
-C project add submodule
&&
1167 git
-C project commit
-m add_sub
&&
1168 git clone project project-clone
&&
1169 git
-C project-clone worktree add ..
/project-2
1171 test_expect_success '"add" with uninitialized submodule
, with submodule.recurse
set' '
1172 git
-C project-clone
-c submodule.recurse worktree add ..
/project-3
1175 test_expect_success '"add" with initialized submodule
, with submodule.recurse
unset' '
1176 test_config_global protocol.
file.allow always
&&
1177 git
-C project-clone submodule update
--init &&
1178 git
-C project-clone worktree add ..
/project-4
1181 test_expect_success '"add" with initialized submodule
, with submodule.recurse
set' '
1182 git
-C project-clone
-c submodule.recurse worktree add ..
/project-5