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
'refuse to reset a branch in use elsewhere' '
133 # we know we are on detached HEAD but just in case ...
134 git checkout --detach HEAD &&
135 git rev-parse --verify HEAD >old.head &&
137 git rev-parse --verify refs/heads/newmain >old.branch &&
138 test_must_fail git checkout -B newmain 2>error &&
139 git rev-parse --verify refs/heads/newmain >new.branch &&
140 git rev-parse --verify HEAD >new.head &&
142 grep "already used by worktree at" error &&
143 test_cmp old.branch new.branch &&
144 test_cmp old.head new.head &&
146 # and we must be still on the same detached HEAD state
147 test_must_fail git symbolic-ref HEAD
151 test_expect_success SYMLINKS
'die the same branch is already checked out (symlink)' '
152 head=$(git -C there rev-parse --git-path HEAD) &&
153 ref=$(git -C there symbolic-ref HEAD) &&
155 ln -s "$ref" "$head" &&
156 test_must_fail git -C here checkout newmain
159 test_expect_success
'not die the same branch is already checked out' '
162 git worktree add --force anothernewmain newmain
166 test_expect_success
'not die on re-checking out current branch' '
173 test_expect_success
'"add" from a bare repo' '
175 git clone --bare . bare &&
177 git worktree add -b bare-main ../there2 main
181 test_expect_success
'checkout from a bare repo without "add"' '
184 test_must_fail git checkout main
188 test_expect_success
'"add" default branch of a bare repo' '
190 git clone --bare . bare2 &&
192 git worktree add ../there3 main &&
194 # Simple check that a Git command does not
195 # immediately fail with the current setup
198 cat >expect <<-EOF &&
202 test_cmp expect actual
205 test_expect_success
'"add" to bare repo with worktree config' '
207 git clone --bare . bare3 &&
209 git config extensions.worktreeconfig true &&
211 # Add config values that are erroneous to have in
212 # a config.worktree file outside of the main
213 # working tree, to check that Git filters them out
214 # when copying config during "git worktree add".
215 git config --worktree core.bare true &&
216 git config --worktree core.worktree "$(pwd)" &&
218 # We want to check that bogus.key is copied
219 git config --worktree bogus.key value &&
220 git config --unset core.bare &&
221 git worktree add ../there4 main &&
224 # Simple check that a Git command does not
225 # immediately fail with the current setup
227 git worktree add --detach ../there5 &&
232 # the worktree has the arbitrary value copied.
233 test_cmp_config -C there4 value bogus.key &&
234 test_cmp_config -C there5 value bogus.key &&
236 # however, core.bare and core.worktree were removed.
237 test_must_fail git -C there4 config core.bare &&
238 test_must_fail git -C there4 config core.worktree &&
240 cat >expect <<-EOF &&
245 test_cmp expect actual &&
247 test_cmp expect actual
250 test_expect_success
'checkout with grafts' '
251 test_when_finished rm .git/info/grafts &&
253 SHA1=$(git rev-parse HEAD) &&
257 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
258 cat >expected <<-\EOF &&
262 git log --format=%s -2 >actual &&
263 test_cmp expected actual &&
264 git worktree add --detach grafted main &&
265 git --git-dir=grafted/.git log --format=%s -2 >actual &&
266 test_cmp expected actual
269 test_expect_success
'"add" from relative HEAD' '
273 git rev-parse HEAD~1 >expected &&
274 git worktree add relhead HEAD~1 &&
275 git -C relhead rev-parse HEAD >actual &&
276 test_cmp expected actual
279 test_expect_success
'"add -b" with <branch> omitted' '
280 git worktree add -b burble flornk &&
281 test_cmp_rev HEAD burble
284 test_expect_success
'"add --detach" with <branch> omitted' '
285 git worktree add --detach fishhook &&
286 git rev-parse HEAD >expected &&
287 git -C fishhook rev-parse HEAD >actual &&
288 test_cmp expected actual &&
289 test_must_fail git -C fishhook symbolic-ref HEAD
292 test_expect_success
'"add" with <branch> omitted' '
293 git worktree add wiffle/bat &&
294 test_cmp_rev HEAD bat
297 test_expect_success
'"add" checks out existing branch of dwimd name' '
298 git branch dwim HEAD~1 &&
299 git worktree add dwim &&
300 test_cmp_rev HEAD~1 dwim &&
303 test_cmp_rev HEAD dwim
307 test_expect_success
'"add <path>" dwim fails with checked out branch' '
308 git checkout -b test-branch &&
309 test_must_fail git worktree add test-branch &&
310 test_path_is_missing test-branch
313 test_expect_success
'"add --force" with existing dwimd name doesnt die' '
314 git checkout test-branch &&
315 git worktree add --force test-branch
318 test_expect_success
'"add" no auto-vivify with --detach and <branch> omitted' '
319 git worktree add --detach mish/mash &&
320 test_must_fail git rev-parse mash -- &&
321 test_must_fail git -C mish/mash symbolic-ref HEAD
324 # Helper function to test mutually exclusive options.
326 # Note: Quoted arguments containing spaces are not supported.
327 test_wt_add_excl
() {
329 test_expect_success
"'worktree add' with '$opts' has mutually exclusive options" '
330 test_must_fail git worktree add $opts 2>actual &&
331 grep -E "fatal:( options)? .* cannot be used together" actual
335 test_wt_add_excl
-b poodle
-B poodle bamboo main
336 test_wt_add_excl
-b poodle
--detach bamboo main
337 test_wt_add_excl
-B poodle
--detach bamboo main
338 test_wt_add_excl
--orphan --detach bamboo
339 test_wt_add_excl
--orphan --no-checkout bamboo
340 test_wt_add_excl
--orphan bamboo main
341 test_wt_add_excl
--orphan -b bamboo wtdir
/ main
343 test_expect_success
'"add -B" fails if the branch is checked out' '
344 git rev-parse newmain >before &&
345 test_must_fail git worktree add -B newmain bamboo main &&
346 git rev-parse newmain >after &&
347 test_cmp before after
350 test_expect_success
'add -B' '
351 git worktree add -B poodle bamboo2 main^ &&
352 git -C bamboo2 symbolic-ref HEAD >actual &&
353 echo refs/heads/poodle >expected &&
354 test_cmp expected actual &&
355 test_cmp_rev main^ poodle
358 test_expect_success
'add --quiet' '
359 test_when_finished "git worktree remove -f -f another-worktree" &&
360 git worktree add --quiet another-worktree main 2>actual &&
361 test_must_be_empty actual
364 test_expect_success
'add --quiet -b' '
365 test_when_finished "git branch -D quietnewbranch" &&
366 test_when_finished "git worktree remove -f -f another-worktree" &&
367 git worktree add --quiet -b quietnewbranch another-worktree 2>actual &&
368 test_must_be_empty actual
371 test_expect_success
'"add --orphan"' '
372 test_when_finished "git worktree remove -f -f orphandir" &&
373 git worktree add --orphan -b neworphan orphandir &&
374 echo refs/heads/neworphan >expected &&
375 git -C orphandir symbolic-ref HEAD >actual &&
376 test_cmp expected actual
379 test_expect_success
'"add --orphan (no -b)"' '
380 test_when_finished "git worktree remove -f -f neworphan" &&
381 git worktree add --orphan neworphan &&
382 echo refs/heads/neworphan >expected &&
383 git -C neworphan symbolic-ref HEAD >actual &&
384 test_cmp expected actual
387 test_expect_success
'"add --orphan --quiet"' '
388 test_when_finished "git worktree remove -f -f orphandir" &&
389 git worktree add --quiet --orphan -b neworphan orphandir 2>log.actual &&
390 test_must_be_empty log.actual &&
391 echo refs/heads/neworphan >expected &&
392 git -C orphandir symbolic-ref HEAD >actual &&
393 test_cmp expected actual
396 test_expect_success
'"add --orphan" fails if the branch already exists' '
397 test_when_finished "git branch -D existingbranch" &&
398 git worktree add -b existingbranch orphandir main &&
399 git worktree remove orphandir &&
400 test_must_fail git worktree add --orphan -b existingbranch orphandir
403 test_expect_success
'"add --orphan" with empty repository' '
404 test_when_finished "rm -rf empty_repo" &&
405 echo refs/heads/newbranch >expected &&
406 GIT_DIR="empty_repo" git init --bare &&
407 git -C empty_repo worktree add --orphan -b newbranch worktreedir &&
408 git -C empty_repo/worktreedir symbolic-ref HEAD >actual &&
409 test_cmp expected actual
412 test_expect_success
'"add" worktree with orphan branch and lock' '
413 git worktree add --lock --orphan -b orphanbr orphan-with-lock &&
414 test_when_finished "git worktree unlock orphan-with-lock || :" &&
415 test -f .git/worktrees/orphan-with-lock/locked
418 test_expect_success
'"add" worktree with orphan branch, lock, and reason' '
419 lock_reason="why not" &&
420 git worktree add --detach --lock --reason "$lock_reason" orphan-with-lock-reason main &&
421 test_when_finished "git worktree unlock orphan-with-lock-reason || :" &&
422 test -f .git/worktrees/orphan-with-lock-reason/locked &&
423 echo "$lock_reason" >expect &&
424 test_cmp expect .git/worktrees/orphan-with-lock-reason/locked
427 # Note: Quoted arguments containing spaces are not supported.
428 test_wt_add_orphan_hint
() {
429 local context
="$1" &&
430 local use_branch
=$2 &&
433 test_expect_success
"'worktree add' show orphan hint in bad/orphan HEAD w/ $context" '
434 test_when_finished "rm -rf repo" &&
436 (cd repo && test_commit commit) &&
437 git -C repo switch --orphan noref &&
438 test_must_fail git -C repo worktree add $opts foobar/ 2>actual &&
439 ! grep "error: unknown switch" actual &&
440 grep "hint: If you meant to create a worktree containing a new unborn branch" actual &&
441 if [ $use_branch -eq 1 ]
443 grep -E "^hint: +git worktree add --orphan -b [^ ]+ [^ ]+$" actual
445 grep -E "^hint: +git worktree add --orphan [^ ]+$" actual
451 test_wt_add_orphan_hint
'no opts' 0
452 test_wt_add_orphan_hint
'-b' 1 -b foobar_branch
453 test_wt_add_orphan_hint
'-B' 1 -B foobar_branch
455 test_expect_success
"'worktree add' doesn't show orphan hint in bad/orphan HEAD w/ --quiet" '
456 test_when_finished "rm -rf repo" &&
458 (cd repo && test_commit commit) &&
459 test_must_fail git -C repo worktree add --quiet foobar_branch foobar/ 2>actual &&
460 ! grep "error: unknown switch" actual &&
461 ! grep "hint: If you meant to create a worktree containing a new unborn branch" actual
464 test_expect_success
'local clone from linked checkout' '
465 git clone --local here here-clone &&
466 ( cd here-clone && git fsck )
469 test_expect_success
'local clone --shared from linked checkout' '
470 git -C bare worktree add --detach ../baretree &&
471 git clone --local --shared baretree bare-clone &&
472 grep /bare/ bare-clone/.git/objects/info/alternates
475 test_expect_success
'"add" worktree with --no-checkout' '
476 git worktree add --no-checkout -b swamp swamp &&
477 ! test -e swamp/init.t &&
478 git -C swamp reset --hard &&
479 test_cmp init.t swamp/init.t
482 test_expect_success
'"add" worktree with --checkout' '
483 git worktree add --checkout -b swmap2 swamp2 &&
484 test_cmp init.t swamp2/init.t
487 test_expect_success
'put a worktree under rebase' '
488 git worktree add under-rebase &&
492 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
493 git worktree list | grep "under-rebase.*detached HEAD"
497 test_expect_success
'add a worktree, checking out a rebased branch' '
498 test_must_fail git worktree add new-rebase under-rebase &&
502 test_expect_success
'checking out a rebased branch from another worktree' '
503 git worktree add new-place &&
504 test_must_fail git -C new-place checkout under-rebase
507 test_expect_success
'not allow to delete a branch under rebase' '
510 test_must_fail git branch -D under-rebase
514 test_expect_success
'rename a branch under rebase not allowed' '
515 test_must_fail git branch -M under-rebase rebase-with-new-name
518 test_expect_success
'check out from current worktree branch ok' '
521 git checkout under-rebase &&
527 test_expect_success
'checkout a branch under bisect' '
528 git worktree add under-bisect &&
533 git bisect good HEAD~2 &&
534 git worktree list | grep "under-bisect.*detached HEAD" &&
535 test_must_fail git worktree add new-bisect under-bisect &&
540 test_expect_success
'rename a branch under bisect not allowed' '
541 test_must_fail git branch -M under-bisect bisect-with-new-name
543 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
544 test_branch_upstream
() {
545 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream
&&
547 git config
"branch.$1.remote" &&
548 git config
"branch.$1.merge"
549 } >actual.upstream
&&
550 test_cmp expect.upstream actual.upstream
553 test_expect_success
'--track sets up tracking' '
554 test_when_finished rm -rf track &&
555 git worktree add --track -b track track main &&
556 test_branch_upstream track . main
559 # setup remote repository $1 and repository $2 with $1 set up as
560 # remote. The remote has two branches, main and foo.
561 setup_remote_repo
() {
565 test_commit
$1_main &&
566 git checkout
-b foo
&&
567 test_commit upstream_foo
572 test_commit
$2_main &&
573 git remote add
$1 ..
/$1 &&
574 git config remote.
$1.fetch \
575 "refs/heads/*:refs/remotes/$1/*" &&
580 test_expect_success
'"add" <path> <remote/branch> w/ no HEAD' '
581 test_when_finished rm -rf repo_upstream repo_local foo &&
582 setup_remote_repo repo_upstream repo_local &&
583 git -C repo_local config --bool core.bare true &&
584 git -C repo_local branch -D main &&
585 git -C repo_local worktree add ./foo repo_upstream/foo
588 test_expect_success
'--no-track avoids setting up tracking' '
589 test_when_finished rm -rf repo_upstream repo_local foo &&
590 setup_remote_repo repo_upstream repo_local &&
593 git worktree add --no-track -b foo ../foo repo_upstream/foo
597 test_must_fail git config "branch.foo.remote" &&
598 test_must_fail git config "branch.foo.merge" &&
599 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
603 test_expect_success
'"add" <path> <non-existent-branch> fails' '
604 test_must_fail git worktree add foo non-existent
607 test_expect_success
'"add" <path> <branch> dwims' '
608 test_when_finished rm -rf repo_upstream repo_dwim foo &&
609 setup_remote_repo repo_upstream repo_dwim &&
610 git init repo_dwim &&
613 git worktree add ../foo foo
617 test_branch_upstream foo repo_upstream foo &&
618 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
622 test_expect_success
'"add" <path> <branch> dwims with checkout.defaultRemote' '
623 test_when_finished rm -rf repo_upstream repo_dwim foo &&
624 setup_remote_repo repo_upstream repo_dwim &&
625 git init repo_dwim &&
628 git remote add repo_upstream2 ../repo_upstream &&
629 git fetch repo_upstream2 &&
630 test_must_fail git worktree add ../foo foo &&
631 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
632 git status -uno --porcelain >status.actual &&
633 test_must_be_empty status.actual
637 test_branch_upstream foo repo_upstream foo &&
638 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
642 test_expect_success
'git worktree add does not match remote' '
643 test_when_finished rm -rf repo_a repo_b foo &&
644 setup_remote_repo repo_a repo_b &&
647 git worktree add ../foo
651 test_must_fail git config "branch.foo.remote" &&
652 test_must_fail git config "branch.foo.merge" &&
653 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
657 test_expect_success
'git worktree add --guess-remote sets up tracking' '
658 test_when_finished rm -rf repo_a repo_b foo &&
659 setup_remote_repo repo_a repo_b &&
662 git worktree add --guess-remote ../foo
666 test_branch_upstream foo repo_a foo &&
667 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
670 test_expect_success
'git worktree add --guess-remote sets up tracking (quiet)' '
671 test_when_finished rm -rf repo_a repo_b foo &&
672 setup_remote_repo repo_a repo_b &&
675 git worktree add --quiet --guess-remote ../foo 2>actual &&
676 test_must_be_empty actual
680 test_branch_upstream foo repo_a foo &&
681 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
685 test_expect_success
'git worktree --no-guess-remote (quiet)' '
686 test_when_finished rm -rf repo_a repo_b foo &&
687 setup_remote_repo repo_a repo_b &&
690 git worktree add --quiet --no-guess-remote ../foo
694 test_must_fail git config "branch.foo.remote" &&
695 test_must_fail git config "branch.foo.merge" &&
696 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
700 test_expect_success
'git worktree add with worktree.guessRemote sets up tracking' '
701 test_when_finished rm -rf repo_a repo_b foo &&
702 setup_remote_repo repo_a repo_b &&
705 git config worktree.guessRemote true &&
706 git worktree add ../foo
710 test_branch_upstream foo repo_a foo &&
711 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
715 test_expect_success
'git worktree --no-guess-remote option overrides config' '
716 test_when_finished rm -rf repo_a repo_b foo &&
717 setup_remote_repo repo_a repo_b &&
720 git config worktree.guessRemote true &&
721 git worktree add --no-guess-remote ../foo
725 test_must_fail git config "branch.foo.remote" &&
726 test_must_fail git config "branch.foo.merge" &&
727 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
731 test_dwim_orphan
() {
732 local info_text
="No possible source branch, inferring '--orphan'" &&
733 local fetch_error_text
="fatal: No local or remote refs exist despite at least one remote" &&
734 local orphan_hint
="hint: If you meant to create a worktree containing a new unborn branch" &&
735 local invalid_ref_regex
="^fatal: invalid reference: " &&
736 local bad_combo_regex
="^fatal: options '[-a-z]*' and '[-a-z]*' cannot be used together" &&
738 local git_ns
="repo" &&
739 local dashc_args
="-C $git_ns" &&
743 local empty_repo
=1 &&
747 local remote_ref
=0 &&
748 local use_detach
=0 &&
749 local use_new_branch
=0 &&
751 local outcome
="$1" &&
752 local outcome_text
&&
760 outcome_text
='"add" DWIM infer --orphan'
764 outcome_text
='"add" DWIM doesnt infer --orphan'
768 outcome_text
='"add" error need fetch'
770 "fatal_orphan_bad_combo")
772 outcome_text
='"add" error inferred "--orphan" gives illegal opts combo'
776 outcome_text
='"add" error, warn on bad HEAD, hint use orphan'
779 echo "test_dwim_orphan(): invalid outcome: '$outcome'" >&2 &&
786 # How and from where to create the worktree
790 dashc_args
="-C $git_ns" &&
791 context
="$context, 'git -C repo'"
796 dashc_args
="-C $git_ns" &&
797 context
="$context, 'git -C wt'"
803 context
="$context, 'cd repo && git'"
809 context
="$context, 'cd wt && git'"
812 # Bypass the "pull first" warning
814 args
="$args --force" &&
815 context
="$context, --force"
818 # Try to use remote refs when DWIM
820 args
="$args --guess-remote" &&
821 context
="$context, --guess-remote"
824 args
="$args --no-guess-remote" &&
825 context
="$context, --no-guess-remote"
828 # Whether there is at least one local branch present
832 context
="$context, >=1 local branches"
836 context
="$context, 0 local branches"
839 # Whether the HEAD points at a valid ref (skip this opt when no refs)
841 # requires: local_ref
842 context
="$context, valid HEAD"
846 context
="$context, invalid (or orphan) HEAD"
849 # Whether the code path is tested with the base add command, -b, or --detach
852 context
="$context, no --branch"
856 context
="$context, --branch"
860 context
="$context, --detach"
863 # Whether to check that all output is suppressed (except errors)
864 # or that the output is as expected
867 args
="$args --quiet" &&
868 context
="$context, --quiet"
872 context
="$context, no --quiet (expect output)"
875 # Whether there is at least one remote attached to the repo
879 context
="$context, >=1 remotes"
884 context
="$context, 0 remotes"
887 # Whether there is at least one valid remote ref
892 context
="$context, >=1 fetched remote branches"
897 context
="$context, 0 fetched remote branches"
900 # Options or flags that become illegal when --orphan is inferred
902 args
="$args --no-checkout" &&
903 context
="$context, --no-checkout"
906 args
="$args --track" &&
907 context
="$context, --track"
910 # All other options are illegal
912 echo "test_dwim_orphan(): invalid arg: '$1'" >&2 &&
918 context
="${context#', '}" &&
919 if [ $use_new_branch -eq 1 ]
922 elif [ $use_detach -eq 1 ]
924 args
="$args --detach"
926 context
="DWIM (no --branch), $context"
928 if [ $empty_repo -eq 1 ]
930 context
="empty repo, $context"
932 args
="$args ../foo" &&
933 context
="${context%', '}" &&
934 test_expect_success
"$outcome_text w/ $context" '
935 test_when_finished "rm -rf repo" &&
937 if [ $local_ref -eq 1 ] && [ "$git_ns" = "repo" ]
939 (cd repo && test_commit commit) &&
940 if [ $bad_head -eq 1 ]
942 git -C repo symbolic-ref HEAD refs/heads/badbranch
944 elif [ $local_ref -eq 1 ] && [ "$git_ns" = "wt" ]
946 test_when_finished "git -C repo worktree remove -f ../wt" &&
947 git -C repo worktree add --orphan -b main ../wt &&
948 (cd wt && test_commit commit) &&
949 if [ $bad_head -eq 1 ]
951 git -C wt symbolic-ref HEAD refs/heads/badbranch
953 elif [ $local_ref -eq 0 ] && [ "$git_ns" = "wt" ]
955 test_when_finished "git -C repo worktree remove -f ../wt" &&
956 git -C repo worktree add --orphan -b orphanbranch ../wt
961 test_when_finished "rm -rf upstream" &&
963 (cd upstream && test_commit commit) &&
964 git -C upstream switch -c foo &&
965 git -C repo remote add upstream ../upstream
968 if [ $remote_ref -eq 1 ]
972 if [ $success -eq 1 ]
974 test_when_finished git -C repo worktree remove ../foo
981 if [ "$outcome" = "infer" ]
983 git $dashc_args worktree add $args 2>actual &&
984 if [ $use_quiet -eq 1 ]
986 test_must_be_empty actual
988 grep "$info_text" actual
990 elif [ "$outcome" = "no_infer" ]
992 git $dashc_args worktree add $args 2>actual &&
993 if [ $use_quiet -eq 1 ]
995 test_must_be_empty actual
997 ! grep "$info_text" actual
999 elif [ "$outcome" = "fetch_error" ]
1001 test_must_fail git $dashc_args worktree add $args 2>actual &&
1002 grep "$fetch_error_text" actual
1003 elif [ "$outcome" = "fatal_orphan_bad_combo" ]
1005 test_must_fail git $dashc_args worktree add $args 2>actual &&
1006 if [ $use_quiet -eq 1 ]
1008 ! grep "$info_text" actual
1010 grep "$info_text" actual
1012 grep "$bad_combo_regex" actual
1013 elif [ "$outcome" = "warn_bad_head" ]
1015 test_must_fail git $dashc_args worktree add $args 2>actual &&
1016 if [ $use_quiet -eq 1 ]
1018 grep "$invalid_ref_regex" actual &&
1019 ! grep "$orphan_hint" actual
1021 headpath=$(git $dashc_args rev-parse --path-format=absolute --git-path HEAD) &&
1022 headcontents=$(cat "$headpath") &&
1023 grep "HEAD points to an invalid (or orphaned) reference" actual &&
1024 grep "HEAD path: .$headpath." actual &&
1025 grep "HEAD contents: .$headcontents." actual &&
1026 grep "$orphan_hint" actual &&
1027 ! grep "$info_text" actual
1029 grep "$invalid_ref_regex" actual
1035 if [ $success -ne 1 ]
1037 test_path_is_missing foo
1042 for quiet_mode
in "no_quiet" "quiet"
1044 for changedir_type
in "cd_repo" "cd_wt" "-C_repo" "-C_wt"
1046 dwim_test_args
="$quiet_mode $changedir_type"
1047 test_dwim_orphan
'infer' $dwim_test_args no_-b
1048 test_dwim_orphan
'no_infer' $dwim_test_args no_-b local_ref good_head
1049 test_dwim_orphan
'infer' $dwim_test_args no_-b no_local_ref no_remote no_remote_ref no_guess_remote
1050 test_dwim_orphan
'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref no_guess_remote
1051 test_dwim_orphan
'fetch_error' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote
1052 test_dwim_orphan
'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote force
1053 test_dwim_orphan
'no_infer' $dwim_test_args no_-b no_local_ref remote remote_ref guess_remote
1055 test_dwim_orphan
'infer' $dwim_test_args -b
1056 test_dwim_orphan
'no_infer' $dwim_test_args -b local_ref good_head
1057 test_dwim_orphan
'infer' $dwim_test_args -b no_local_ref no_remote no_remote_ref no_guess_remote
1058 test_dwim_orphan
'infer' $dwim_test_args -b no_local_ref remote no_remote_ref no_guess_remote
1059 test_dwim_orphan
'infer' $dwim_test_args -b no_local_ref remote no_remote_ref guess_remote
1060 test_dwim_orphan
'infer' $dwim_test_args -b no_local_ref remote remote_ref guess_remote
1062 test_dwim_orphan
'warn_bad_head' $dwim_test_args no_-b local_ref bad_head
1063 test_dwim_orphan
'warn_bad_head' $dwim_test_args -b local_ref bad_head
1064 test_dwim_orphan
'warn_bad_head' $dwim_test_args detach local_ref bad_head
1067 test_dwim_orphan
'fatal_orphan_bad_combo' $quiet_mode no_-b no_checkout
1068 test_dwim_orphan
'fatal_orphan_bad_combo' $quiet_mode no_-b track
1069 test_dwim_orphan
'fatal_orphan_bad_combo' $quiet_mode -b no_checkout
1070 test_dwim_orphan
'fatal_orphan_bad_combo' $quiet_mode -b track
1073 post_checkout_hook
() {
1074 test_when_finished
"rm -rf .git/hooks" &&
1076 test_hook
-C "$1" post-checkout
<<-\EOF
1079 git rev-parse --git-dir --show-toplevel
1084 test_expect_success '"add" invokes post-checkout hook (branch)' '
1085 post_checkout_hook &&
1087 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1088 echo $(pwd)/.git/worktrees/gumby &&
1091 git worktree add gumby &&
1092 test_cmp hook.expect gumby/hook.actual
1095 test_expect_success '"add" invokes post-checkout hook (detached)' '
1096 post_checkout_hook &&
1098 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1099 echo $(pwd)/.git/worktrees/grumpy &&
1102 git worktree add --detach grumpy &&
1103 test_cmp hook.expect grumpy/hook.actual
1106 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
1107 post_checkout_hook &&
1108 rm -f hook.actual &&
1109 git worktree add --no-checkout gloopy &&
1110 test_path_is_missing gloopy/hook.actual
1113 test_expect_success '"add" in other worktree invokes post-checkout hook' '
1114 post_checkout_hook &&
1116 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1117 echo $(pwd)/.git/worktrees/guppy &&
1120 git -C gloopy worktree add --detach ../guppy &&
1121 test_cmp hook.expect guppy/hook.actual
1124 test_expect_success '"add" in bare repo invokes post-checkout hook' '
1126 git clone --bare . bare &&
1128 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
1129 echo $(pwd)/bare/worktrees/goozy &&
1132 post_checkout_hook bare &&
1133 git -C bare worktree add --detach ../goozy &&
1134 test_cmp hook.expect goozy/hook.actual
1137 test_expect_success '"add" an existing but missing worktree' '
1138 git worktree add --detach pneu &&
1139 test_must_fail git worktree add --detach pneu &&
1141 test_must_fail git worktree add --detach pneu &&
1142 git worktree add --force --detach pneu
1145 test_expect_success '"add" an existing locked but missing worktree' '
1146 git worktree add --detach gnoo &&
1147 git worktree lock gnoo &&
1148 test_when_finished "git worktree unlock gnoo || :" &&
1150 test_must_fail git worktree add --detach gnoo &&
1151 test_must_fail git worktree add --force --detach gnoo &&
1152 git worktree add --force --force --detach gnoo
1155 test_expect_success '"add" not tripped up by magic worktree matching"' '
1156 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
1157 # directory `sub2` should not mistakenly complain that `bar` is an
1158 # already-registered worktree
1160 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
1161 git -C sub2 --git-dir=../.git worktree add --detach bozo
1164 test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
1165 git worktree add --detach ". weird*..?.lock.lock" &&
1166 test -d .git/worktrees/---weird-.-
1169 test_expect_success '"add" should not fail because of another bad worktree
' '
1170 git init add-fail
&&
1173 test_commit first
&&
1175 git worktree add sub
/to-be-deleted
&&
1177 git worktree add second
1181 test_expect_success '"add" with uninitialized submodule
, with submodule.recurse
unset' '
1182 test_config_global protocol.
file.allow always
&&
1183 test_create_repo submodule
&&
1184 test_commit
-C submodule first
&&
1185 test_create_repo project
&&
1186 git
-C project submodule add ..
/submodule
&&
1187 git
-C project add submodule
&&
1189 git
-C project commit
-m add_sub
&&
1190 git clone project project-clone
&&
1191 git
-C project-clone worktree add ..
/project-2
1193 test_expect_success '"add" with uninitialized submodule
, with submodule.recurse
set' '
1194 git
-C project-clone
-c submodule.recurse worktree add ..
/project-3
1197 test_expect_success '"add" with initialized submodule
, with submodule.recurse
unset' '
1198 test_config_global protocol.
file.allow always
&&
1199 git
-C project-clone submodule update
--init &&
1200 git
-C project-clone worktree add ..
/project-4
1203 test_expect_success '"add" with initialized submodule
, with submodule.recurse
set' '
1204 git
-C project-clone
-c submodule.recurse worktree add ..
/project-5