The eleventh batch
[git/gitster.git] / t / t2400-worktree-add.sh
blobcfc4aeb1798c6d023909cec771e5b74e983af5ea
1 #!/bin/sh
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
9 TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
12 . "$TEST_DIRECTORY"/lib-rebase.sh
14 test_expect_success 'setup' '
15 test_commit init
18 test_expect_success '"add" an existing worktree' '
19 mkdir -p existing/subtree &&
20 test_must_fail git worktree add --detach existing main
23 test_expect_success '"add" an existing empty worktree' '
24 mkdir existing_empty &&
25 git worktree add --detach existing_empty main
28 test_expect_success '"add" using shorthand - fails when no previous branch' '
29 test_must_fail git worktree add existing_short -
32 test_expect_success '"add" using - shorthand' '
33 git checkout -b newbranch &&
34 echo hello >myworld &&
35 git add myworld &&
36 git commit -m myworld &&
37 git checkout main &&
38 git worktree add short-hand - &&
39 echo refs/heads/newbranch >expect &&
40 git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
41 test_cmp expect actual
44 test_expect_success '"add" refuses to checkout locked branch' '
45 test_must_fail git worktree add zere main &&
46 ! test -d zere &&
47 ! test -d .git/worktrees/zere
50 test_expect_success 'checking out paths not complaining about linked checkouts' '
52 cd existing_empty &&
53 echo dirty >>init.t &&
54 git checkout main -- init.t
58 test_expect_success '"add" worktree' '
59 git rev-parse HEAD >expect &&
60 git worktree add --detach here main &&
62 cd here &&
63 test_cmp ../init.t init.t &&
64 test_must_fail git symbolic-ref HEAD &&
65 git rev-parse HEAD >actual &&
66 test_cmp ../expect actual &&
67 git fsck
71 test_expect_success '"add" worktree with lock' '
72 git worktree add --detach --lock here-with-lock main &&
73 test_when_finished "git worktree unlock here-with-lock || :" &&
74 test -f .git/worktrees/here-with-lock/locked
77 test_expect_success '"add" worktree with lock and reason' '
78 lock_reason="why not" &&
79 git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main &&
80 test_when_finished "git worktree unlock here-with-lock-reason || :" &&
81 test -f .git/worktrees/here-with-lock-reason/locked &&
82 echo "$lock_reason" >expect &&
83 test_cmp expect .git/worktrees/here-with-lock-reason/locked
86 test_expect_success '"add" worktree with reason but no lock' '
87 test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main &&
88 test_path_is_missing .git/worktrees/here-with-reason-only/locked
91 test_expect_success '"add" worktree from a subdir' '
93 mkdir sub &&
94 cd sub &&
95 git worktree add --detach here main &&
96 cd here &&
97 test_cmp ../../init.t init.t
101 test_expect_success '"add" from a linked checkout' '
103 cd here &&
104 git worktree add --detach nested-here main &&
105 cd nested-here &&
106 git fsck
110 test_expect_success '"add" worktree creating new branch' '
111 git worktree add -b newmain there main &&
113 cd there &&
114 test_cmp ../init.t init.t &&
115 git symbolic-ref HEAD >actual &&
116 echo refs/heads/newmain >expect &&
117 test_cmp expect actual &&
118 git fsck
122 test_expect_success 'die the same branch is already checked out' '
124 cd here &&
125 test_must_fail git checkout newmain 2>actual &&
126 grep "already used by worktree at" actual
130 test_expect_success 'refuse to reset a branch in use elsewhere' '
132 cd here &&
134 # we know we are on detached HEAD but just in case ...
135 git checkout --detach HEAD &&
136 git rev-parse --verify HEAD >old.head &&
138 git rev-parse --verify refs/heads/newmain >old.branch &&
139 test_must_fail git checkout -B newmain 2>error &&
140 git rev-parse --verify refs/heads/newmain >new.branch &&
141 git rev-parse --verify HEAD >new.head &&
143 grep "already used by worktree at" error &&
144 test_cmp old.branch new.branch &&
145 test_cmp old.head new.head &&
147 # and we must be still on the same detached HEAD state
148 test_must_fail git symbolic-ref HEAD
152 test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
153 head=$(git -C there rev-parse --git-path HEAD) &&
154 ref=$(git -C there symbolic-ref HEAD) &&
155 rm "$head" &&
156 ln -s "$ref" "$head" &&
157 test_must_fail git -C here checkout newmain
160 test_expect_success 'not die the same branch is already checked out' '
162 cd here &&
163 git worktree add --force anothernewmain newmain
167 test_expect_success 'not die on re-checking out current branch' '
169 cd there &&
170 git checkout newmain
174 test_expect_success '"add" from a bare repo' '
176 git clone --bare . bare &&
177 cd bare &&
178 git worktree add -b bare-main ../there2 main
182 test_expect_success 'checkout from a bare repo without "add"' '
184 cd bare &&
185 test_must_fail git checkout main
189 test_expect_success '"add" default branch of a bare repo' '
191 git clone --bare . bare2 &&
192 cd bare2 &&
193 git worktree add ../there3 main &&
194 cd ../there3 &&
195 # Simple check that a Git command does not
196 # immediately fail with the current setup
197 git status
198 ) &&
199 cat >expect <<-EOF &&
200 init.t
202 ls there3 >actual &&
203 test_cmp expect actual
206 test_expect_success '"add" to bare repo with worktree config' '
208 git clone --bare . bare3 &&
209 cd bare3 &&
210 git config extensions.worktreeconfig true &&
212 # Add config values that are erroneous to have in
213 # a config.worktree file outside of the main
214 # working tree, to check that Git filters them out
215 # when copying config during "git worktree add".
216 git config --worktree core.bare true &&
217 git config --worktree core.worktree "$(pwd)" &&
219 # We want to check that bogus.key is copied
220 git config --worktree bogus.key value &&
221 git config --unset core.bare &&
222 git worktree add ../there4 main &&
223 cd ../there4 &&
225 # Simple check that a Git command does not
226 # immediately fail with the current setup
227 git status &&
228 git worktree add --detach ../there5 &&
229 cd ../there5 &&
230 git status
231 ) &&
233 # the worktree has the arbitrary value copied.
234 test_cmp_config -C there4 value bogus.key &&
235 test_cmp_config -C there5 value bogus.key &&
237 # however, core.bare and core.worktree were removed.
238 test_must_fail git -C there4 config core.bare &&
239 test_must_fail git -C there4 config core.worktree &&
241 cat >expect <<-EOF &&
242 init.t
245 ls there4 >actual &&
246 test_cmp expect actual &&
247 ls there5 >actual &&
248 test_cmp expect actual
251 test_expect_success 'checkout with grafts' '
252 test_when_finished rm .git/info/grafts &&
253 test_commit abc &&
254 SHA1=$(git rev-parse HEAD) &&
255 test_commit def &&
256 test_commit xyz &&
257 mkdir .git/info &&
258 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
259 cat >expected <<-\EOF &&
263 git log --format=%s -2 >actual &&
264 test_cmp expected actual &&
265 git worktree add --detach grafted main &&
266 git --git-dir=grafted/.git log --format=%s -2 >actual &&
267 test_cmp expected actual
270 test_expect_success '"add" from relative HEAD' '
271 test_commit a &&
272 test_commit b &&
273 test_commit c &&
274 git rev-parse HEAD~1 >expected &&
275 git worktree add relhead HEAD~1 &&
276 git -C relhead rev-parse HEAD >actual &&
277 test_cmp expected actual
280 test_expect_success '"add -b" with <branch> omitted' '
281 git worktree add -b burble flornk &&
282 test_cmp_rev HEAD burble
285 test_expect_success '"add --detach" with <branch> omitted' '
286 git worktree add --detach fishhook &&
287 git rev-parse HEAD >expected &&
288 git -C fishhook rev-parse HEAD >actual &&
289 test_cmp expected actual &&
290 test_must_fail git -C fishhook symbolic-ref HEAD
293 test_expect_success '"add" with <branch> omitted' '
294 git worktree add wiffle/bat &&
295 test_cmp_rev HEAD bat
298 test_expect_success '"add" checks out existing branch of dwimd name' '
299 git branch dwim HEAD~1 &&
300 git worktree add dwim &&
301 test_cmp_rev HEAD~1 dwim &&
303 cd dwim &&
304 test_cmp_rev HEAD dwim
308 test_expect_success '"add <path>" dwim fails with checked out branch' '
309 git checkout -b test-branch &&
310 test_must_fail git worktree add test-branch &&
311 test_path_is_missing test-branch
314 test_expect_success '"add --force" with existing dwimd name doesnt die' '
315 git checkout test-branch &&
316 git worktree add --force test-branch
319 test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
320 git worktree add --detach mish/mash &&
321 test_must_fail git rev-parse mash -- &&
322 test_must_fail git -C mish/mash symbolic-ref HEAD
325 # Helper function to test mutually exclusive options.
327 # Note: Quoted arguments containing spaces are not supported.
328 test_wt_add_excl () {
329 local opts="$*" &&
330 test_expect_success "'worktree add' with '$opts' has mutually exclusive options" '
331 test_must_fail git worktree add $opts 2>actual &&
332 grep -E "fatal:( options)? .* cannot be used together" actual
336 test_wt_add_excl -b poodle -B poodle bamboo main
337 test_wt_add_excl -b poodle --detach bamboo main
338 test_wt_add_excl -B poodle --detach bamboo main
339 test_wt_add_excl --orphan --detach bamboo
340 test_wt_add_excl --orphan --no-checkout bamboo
341 test_wt_add_excl --orphan bamboo main
342 test_wt_add_excl --orphan -b bamboo wtdir/ main
344 test_expect_success '"add -B" fails if the branch is checked out' '
345 git rev-parse newmain >before &&
346 test_must_fail git worktree add -B newmain bamboo main &&
347 git rev-parse newmain >after &&
348 test_cmp before after
351 test_expect_success 'add -B' '
352 git worktree add -B poodle bamboo2 main^ &&
353 git -C bamboo2 symbolic-ref HEAD >actual &&
354 echo refs/heads/poodle >expected &&
355 test_cmp expected actual &&
356 test_cmp_rev main^ poodle
359 test_expect_success 'add --quiet' '
360 test_when_finished "git worktree remove -f -f another-worktree" &&
361 git worktree add --quiet another-worktree main 2>actual &&
362 test_must_be_empty actual
365 test_expect_success 'add --quiet -b' '
366 test_when_finished "git branch -D quietnewbranch" &&
367 test_when_finished "git worktree remove -f -f another-worktree" &&
368 git worktree add --quiet -b quietnewbranch another-worktree 2>actual &&
369 test_must_be_empty actual
372 test_expect_success '"add --orphan"' '
373 test_when_finished "git worktree remove -f -f orphandir" &&
374 git worktree add --orphan -b neworphan orphandir &&
375 echo refs/heads/neworphan >expected &&
376 git -C orphandir symbolic-ref HEAD >actual &&
377 test_cmp expected actual
380 test_expect_success '"add --orphan (no -b)"' '
381 test_when_finished "git worktree remove -f -f neworphan" &&
382 git worktree add --orphan neworphan &&
383 echo refs/heads/neworphan >expected &&
384 git -C neworphan symbolic-ref HEAD >actual &&
385 test_cmp expected actual
388 test_expect_success '"add --orphan --quiet"' '
389 test_when_finished "git worktree remove -f -f orphandir" &&
390 git worktree add --quiet --orphan -b neworphan orphandir 2>log.actual &&
391 test_must_be_empty log.actual &&
392 echo refs/heads/neworphan >expected &&
393 git -C orphandir symbolic-ref HEAD >actual &&
394 test_cmp expected actual
397 test_expect_success '"add --orphan" fails if the branch already exists' '
398 test_when_finished "git branch -D existingbranch" &&
399 git worktree add -b existingbranch orphandir main &&
400 git worktree remove orphandir &&
401 test_must_fail git worktree add --orphan -b existingbranch orphandir
404 test_expect_success '"add --orphan" with empty repository' '
405 test_when_finished "rm -rf empty_repo" &&
406 echo refs/heads/newbranch >expected &&
407 GIT_DIR="empty_repo" git init --bare &&
408 git -C empty_repo worktree add --orphan -b newbranch worktreedir &&
409 git -C empty_repo/worktreedir symbolic-ref HEAD >actual &&
410 test_cmp expected actual
413 test_expect_success '"add" worktree with orphan branch and lock' '
414 git worktree add --lock --orphan -b orphanbr orphan-with-lock &&
415 test_when_finished "git worktree unlock orphan-with-lock || :" &&
416 test -f .git/worktrees/orphan-with-lock/locked
419 test_expect_success '"add" worktree with orphan branch, lock, and reason' '
420 lock_reason="why not" &&
421 git worktree add --detach --lock --reason "$lock_reason" orphan-with-lock-reason main &&
422 test_when_finished "git worktree unlock orphan-with-lock-reason || :" &&
423 test -f .git/worktrees/orphan-with-lock-reason/locked &&
424 echo "$lock_reason" >expect &&
425 test_cmp expect .git/worktrees/orphan-with-lock-reason/locked
428 # Note: Quoted arguments containing spaces are not supported.
429 test_wt_add_orphan_hint () {
430 local context="$1" &&
431 local use_branch="$2" &&
432 shift 2 &&
433 local opts="$*" &&
434 test_expect_success "'worktree add' show orphan hint in bad/orphan HEAD w/ $context" '
435 test_when_finished "rm -rf repo" &&
436 git init repo &&
437 (cd repo && test_commit commit) &&
438 git -C repo switch --orphan noref &&
439 test_must_fail git -C repo worktree add $opts foobar/ 2>actual &&
440 ! grep "error: unknown switch" actual &&
441 grep "hint: If you meant to create a worktree containing a new unborn branch" actual &&
442 if [ $use_branch -eq 1 ]
443 then
444 grep -E "^hint: +git worktree add --orphan -b [^ ]+ [^ ]+$" actual
445 else
446 grep -E "^hint: +git worktree add --orphan [^ ]+$" actual
452 test_wt_add_orphan_hint 'no opts' 0
453 test_wt_add_orphan_hint '-b' 1 -b foobar_branch
454 test_wt_add_orphan_hint '-B' 1 -B foobar_branch
456 test_expect_success "'worktree add' doesn't show orphan hint in bad/orphan HEAD w/ --quiet" '
457 test_when_finished "rm -rf repo" &&
458 git init repo &&
459 (cd repo && test_commit commit) &&
460 test_must_fail git -C repo worktree add --quiet foobar_branch foobar/ 2>actual &&
461 ! grep "error: unknown switch" actual &&
462 ! grep "hint: If you meant to create a worktree containing a new unborn branch" actual
465 test_expect_success 'local clone from linked checkout' '
466 git clone --local here here-clone &&
467 ( cd here-clone && git fsck )
470 test_expect_success 'local clone --shared from linked checkout' '
471 git -C bare worktree add --detach ../baretree &&
472 git clone --local --shared baretree bare-clone &&
473 grep /bare/ bare-clone/.git/objects/info/alternates
476 test_expect_success '"add" worktree with --no-checkout' '
477 git worktree add --no-checkout -b swamp swamp &&
478 ! test -e swamp/init.t &&
479 git -C swamp reset --hard &&
480 test_cmp init.t swamp/init.t
483 test_expect_success '"add" worktree with --checkout' '
484 git worktree add --checkout -b swmap2 swamp2 &&
485 test_cmp init.t swamp2/init.t
488 test_expect_success 'put a worktree under rebase' '
489 git worktree add under-rebase &&
491 cd under-rebase &&
492 set_fake_editor &&
493 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
494 git worktree list >actual &&
495 grep "under-rebase.*detached HEAD" actual
499 test_expect_success 'add a worktree, checking out a rebased branch' '
500 test_must_fail git worktree add new-rebase under-rebase &&
501 ! test -d new-rebase
504 test_expect_success 'checking out a rebased branch from another worktree' '
505 git worktree add new-place &&
506 test_must_fail git -C new-place checkout under-rebase
509 test_expect_success 'not allow to delete a branch under rebase' '
511 cd under-rebase &&
512 test_must_fail git branch -D under-rebase
516 test_expect_success 'rename a branch under rebase not allowed' '
517 test_must_fail git branch -M under-rebase rebase-with-new-name
520 test_expect_success 'check out from current worktree branch ok' '
522 cd under-rebase &&
523 git checkout under-rebase &&
524 git checkout - &&
525 git rebase --abort
529 test_expect_success 'checkout a branch under bisect' '
530 git worktree add under-bisect &&
532 cd under-bisect &&
533 git bisect start &&
534 git bisect bad &&
535 git bisect good HEAD~2 &&
536 git worktree list >actual &&
537 grep "under-bisect.*detached HEAD" actual &&
538 test_must_fail git worktree add new-bisect under-bisect &&
539 ! test -d new-bisect
543 test_expect_success 'rename a branch under bisect not allowed' '
544 test_must_fail git branch -M under-bisect bisect-with-new-name
546 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
547 test_branch_upstream () {
548 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
550 git config "branch.$1.remote" &&
551 git config "branch.$1.merge"
552 } >actual.upstream &&
553 test_cmp expect.upstream actual.upstream
556 test_expect_success '--track sets up tracking' '
557 test_when_finished rm -rf track &&
558 git worktree add --track -b track track main &&
559 test_branch_upstream track . main
562 # setup remote repository $1 and repository $2 with $1 set up as
563 # remote. The remote has two branches, main and foo.
564 setup_remote_repo () {
565 git init $1 &&
567 cd $1 &&
568 test_commit $1_main &&
569 git checkout -b foo &&
570 test_commit upstream_foo
571 ) &&
572 git init $2 &&
574 cd $2 &&
575 test_commit $2_main &&
576 git remote add $1 ../$1 &&
577 git config remote.$1.fetch \
578 "refs/heads/*:refs/remotes/$1/*" &&
579 git fetch --all
583 test_expect_success '"add" <path> <remote/branch> w/ no HEAD' '
584 test_when_finished rm -rf repo_upstream repo_local foo &&
585 setup_remote_repo repo_upstream repo_local &&
586 git -C repo_local config --bool core.bare true &&
587 git -C repo_local branch -D main &&
588 git -C repo_local worktree add ./foo repo_upstream/foo
591 test_expect_success '--no-track avoids setting up tracking' '
592 test_when_finished rm -rf repo_upstream repo_local foo &&
593 setup_remote_repo repo_upstream repo_local &&
595 cd repo_local &&
596 git worktree add --no-track -b foo ../foo repo_upstream/foo
597 ) &&
599 cd foo &&
600 test_must_fail git config "branch.foo.remote" &&
601 test_must_fail git config "branch.foo.merge" &&
602 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
606 test_expect_success '"add" <path> <non-existent-branch> fails' '
607 test_must_fail git worktree add foo non-existent
610 test_expect_success '"add" <path> <branch> dwims' '
611 test_when_finished rm -rf repo_upstream repo_dwim foo &&
612 setup_remote_repo repo_upstream repo_dwim &&
613 git init repo_dwim &&
615 cd repo_dwim &&
616 git worktree add ../foo foo
617 ) &&
619 cd foo &&
620 test_branch_upstream foo repo_upstream foo &&
621 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
625 test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
626 test_when_finished rm -rf repo_upstream repo_dwim foo &&
627 setup_remote_repo repo_upstream repo_dwim &&
628 git init repo_dwim &&
630 cd repo_dwim &&
631 git remote add repo_upstream2 ../repo_upstream &&
632 git fetch repo_upstream2 &&
633 test_must_fail git worktree add ../foo foo &&
634 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
635 git status -uno --porcelain >status.actual &&
636 test_must_be_empty status.actual
637 ) &&
639 cd foo &&
640 test_branch_upstream foo repo_upstream foo &&
641 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
645 test_expect_success 'git worktree add does not match remote' '
646 test_when_finished rm -rf repo_a repo_b foo &&
647 setup_remote_repo repo_a repo_b &&
649 cd repo_b &&
650 git worktree add ../foo
651 ) &&
653 cd foo &&
654 test_must_fail git config "branch.foo.remote" &&
655 test_must_fail git config "branch.foo.merge" &&
656 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
660 test_expect_success 'git worktree add --guess-remote sets up tracking' '
661 test_when_finished rm -rf repo_a repo_b foo &&
662 setup_remote_repo repo_a repo_b &&
664 cd repo_b &&
665 git worktree add --guess-remote ../foo
666 ) &&
668 cd foo &&
669 test_branch_upstream foo repo_a foo &&
670 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
673 test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' '
674 test_when_finished rm -rf repo_a repo_b foo &&
675 setup_remote_repo repo_a repo_b &&
677 cd repo_b &&
678 git worktree add --quiet --guess-remote ../foo 2>actual &&
679 test_must_be_empty actual
680 ) &&
682 cd foo &&
683 test_branch_upstream foo repo_a foo &&
684 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
688 test_expect_success 'git worktree --no-guess-remote (quiet)' '
689 test_when_finished rm -rf repo_a repo_b foo &&
690 setup_remote_repo repo_a repo_b &&
692 cd repo_b &&
693 git worktree add --quiet --no-guess-remote ../foo
694 ) &&
696 cd foo &&
697 test_must_fail git config "branch.foo.remote" &&
698 test_must_fail git config "branch.foo.merge" &&
699 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
703 test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
704 test_when_finished rm -rf repo_a repo_b foo &&
705 setup_remote_repo repo_a repo_b &&
707 cd repo_b &&
708 git config worktree.guessRemote true &&
709 git worktree add ../foo
710 ) &&
712 cd foo &&
713 test_branch_upstream foo repo_a foo &&
714 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
718 test_expect_success 'git worktree --no-guess-remote option overrides config' '
719 test_when_finished rm -rf repo_a repo_b foo &&
720 setup_remote_repo repo_a repo_b &&
722 cd repo_b &&
723 git config worktree.guessRemote true &&
724 git worktree add --no-guess-remote ../foo
725 ) &&
727 cd foo &&
728 test_must_fail git config "branch.foo.remote" &&
729 test_must_fail git config "branch.foo.merge" &&
730 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
734 test_dwim_orphan () {
735 local info_text="No possible source branch, inferring '--orphan'" &&
736 local fetch_error_text="fatal: No local or remote refs exist despite at least one remote" &&
737 local orphan_hint="hint: If you meant to create a worktree containing a new unborn branch" &&
738 local invalid_ref_regex="^fatal: invalid reference: " &&
739 local bad_combo_regex="^fatal: options '[-a-z]*' and '[-a-z]*' cannot be used together" &&
741 local git_ns="repo" &&
742 local dashc_args="-C $git_ns" &&
743 local use_cd=0 &&
745 local bad_head=0 &&
746 local empty_repo=1 &&
747 local local_ref=0 &&
748 local use_quiet=0 &&
749 local remote=0 &&
750 local remote_ref=0 &&
751 local use_detach=0 &&
752 local use_new_branch=0 &&
754 local outcome="$1" &&
755 local outcome_text &&
756 local success &&
757 shift &&
758 local args="" &&
759 local context="" &&
760 case "$outcome" in
761 "infer")
762 success=1 &&
763 outcome_text='"add" DWIM infer --orphan'
765 "no_infer")
766 success=1 &&
767 outcome_text='"add" DWIM doesnt infer --orphan'
769 "fetch_error")
770 success=0 &&
771 outcome_text='"add" error need fetch'
773 "fatal_orphan_bad_combo")
774 success=0 &&
775 outcome_text='"add" error inferred "--orphan" gives illegal opts combo'
777 "warn_bad_head")
778 success=0 &&
779 outcome_text='"add" error, warn on bad HEAD, hint use orphan'
782 echo "test_dwim_orphan(): invalid outcome: '$outcome'" >&2 &&
783 return 1
785 esac &&
786 while [ $# -gt 0 ]
788 case "$1" in
789 # How and from where to create the worktree
790 "-C_repo")
791 use_cd=0 &&
792 git_ns="repo" &&
793 dashc_args="-C $git_ns" &&
794 context="$context, 'git -C repo'"
796 "-C_wt")
797 use_cd=0 &&
798 git_ns="wt" &&
799 dashc_args="-C $git_ns" &&
800 context="$context, 'git -C wt'"
802 "cd_repo")
803 use_cd=1 &&
804 git_ns="repo" &&
805 dashc_args="" &&
806 context="$context, 'cd repo && git'"
808 "cd_wt")
809 use_cd=1 &&
810 git_ns="wt" &&
811 dashc_args="" &&
812 context="$context, 'cd wt && git'"
815 # Bypass the "pull first" warning
816 "force")
817 args="$args --force" &&
818 context="$context, --force"
821 # Try to use remote refs when DWIM
822 "guess_remote")
823 args="$args --guess-remote" &&
824 context="$context, --guess-remote"
826 "no_guess_remote")
827 args="$args --no-guess-remote" &&
828 context="$context, --no-guess-remote"
831 # Whether there is at least one local branch present
832 "local_ref")
833 empty_repo=0 &&
834 local_ref=1 &&
835 context="$context, >=1 local branches"
837 "no_local_ref")
838 empty_repo=0 &&
839 context="$context, 0 local branches"
842 # Whether the HEAD points at a valid ref (skip this opt when no refs)
843 "good_head")
844 # requires: local_ref
845 context="$context, valid HEAD"
847 "bad_head")
848 bad_head=1 &&
849 context="$context, invalid (or orphan) HEAD"
852 # Whether the code path is tested with the base add command, -b, or --detach
853 "no_-b")
854 use_new_branch=0 &&
855 context="$context, no --branch"
857 "-b")
858 use_new_branch=1 &&
859 context="$context, --branch"
861 "detach")
862 use_detach=1 &&
863 context="$context, --detach"
866 # Whether to check that all output is suppressed (except errors)
867 # or that the output is as expected
868 "quiet")
869 use_quiet=1 &&
870 args="$args --quiet" &&
871 context="$context, --quiet"
873 "no_quiet")
874 use_quiet=0 &&
875 context="$context, no --quiet (expect output)"
878 # Whether there is at least one remote attached to the repo
879 "remote")
880 empty_repo=0 &&
881 remote=1 &&
882 context="$context, >=1 remotes"
884 "no_remote")
885 empty_repo=0 &&
886 remote=0 &&
887 context="$context, 0 remotes"
890 # Whether there is at least one valid remote ref
891 "remote_ref")
892 # requires: remote
893 empty_repo=0 &&
894 remote_ref=1 &&
895 context="$context, >=1 fetched remote branches"
897 "no_remote_ref")
898 empty_repo=0 &&
899 remote_ref=0 &&
900 context="$context, 0 fetched remote branches"
903 # Options or flags that become illegal when --orphan is inferred
904 "no_checkout")
905 args="$args --no-checkout" &&
906 context="$context, --no-checkout"
908 "track")
909 args="$args --track" &&
910 context="$context, --track"
913 # All other options are illegal
915 echo "test_dwim_orphan(): invalid arg: '$1'" >&2 &&
916 return 1
918 esac &&
919 shift
920 done &&
921 context="${context#', '}" &&
922 if [ $use_new_branch -eq 1 ]
923 then
924 args="$args -b foo"
925 elif [ $use_detach -eq 1 ]
926 then
927 args="$args --detach"
928 else
929 context="DWIM (no --branch), $context"
930 fi &&
931 if [ $empty_repo -eq 1 ]
932 then
933 context="empty repo, $context"
934 fi &&
935 args="$args ../foo" &&
936 context="${context%', '}" &&
937 test_expect_success "$outcome_text w/ $context" '
938 test_when_finished "rm -rf repo" &&
939 git init repo &&
940 if [ $local_ref -eq 1 ] && [ "$git_ns" = "repo" ]
941 then
942 (cd repo && test_commit commit) &&
943 if [ $bad_head -eq 1 ]
944 then
945 git -C repo symbolic-ref HEAD refs/heads/badbranch
947 elif [ $local_ref -eq 1 ] && [ "$git_ns" = "wt" ]
948 then
949 test_when_finished "git -C repo worktree remove -f ../wt" &&
950 git -C repo worktree add --orphan -b main ../wt &&
951 (cd wt && test_commit commit) &&
952 if [ $bad_head -eq 1 ]
953 then
954 git -C wt symbolic-ref HEAD refs/heads/badbranch
956 elif [ $local_ref -eq 0 ] && [ "$git_ns" = "wt" ]
957 then
958 test_when_finished "git -C repo worktree remove -f ../wt" &&
959 git -C repo worktree add --orphan -b orphanbranch ../wt
960 fi &&
962 if [ $remote -eq 1 ]
963 then
964 test_when_finished "rm -rf upstream" &&
965 git init upstream &&
966 (cd upstream && test_commit commit) &&
967 git -C upstream switch -c foo &&
968 git -C repo remote add upstream ../upstream
969 fi &&
971 if [ $remote_ref -eq 1 ]
972 then
973 git -C repo fetch
974 fi &&
975 if [ $success -eq 1 ]
976 then
977 test_when_finished git -C repo worktree remove ../foo
978 fi &&
980 if [ $use_cd -eq 1 ]
981 then
982 cd $git_ns
983 fi &&
984 if [ "$outcome" = "infer" ]
985 then
986 git $dashc_args worktree add $args 2>actual &&
987 if [ $use_quiet -eq 1 ]
988 then
989 test_must_be_empty actual
990 else
991 grep "$info_text" actual
993 elif [ "$outcome" = "no_infer" ]
994 then
995 git $dashc_args worktree add $args 2>actual &&
996 if [ $use_quiet -eq 1 ]
997 then
998 test_must_be_empty actual
999 else
1000 ! grep "$info_text" actual
1002 elif [ "$outcome" = "fetch_error" ]
1003 then
1004 test_must_fail git $dashc_args worktree add $args 2>actual &&
1005 grep "$fetch_error_text" actual
1006 elif [ "$outcome" = "fatal_orphan_bad_combo" ]
1007 then
1008 test_must_fail git $dashc_args worktree add $args 2>actual &&
1009 if [ $use_quiet -eq 1 ]
1010 then
1011 ! grep "$info_text" actual
1012 else
1013 grep "$info_text" actual
1014 fi &&
1015 grep "$bad_combo_regex" actual
1016 elif [ "$outcome" = "warn_bad_head" ]
1017 then
1018 test_must_fail git $dashc_args worktree add $args 2>actual &&
1019 if [ $use_quiet -eq 1 ]
1020 then
1021 grep "$invalid_ref_regex" actual &&
1022 ! grep "$orphan_hint" actual
1023 else
1024 headpath=$(git $dashc_args rev-parse --path-format=absolute --git-path HEAD) &&
1025 headcontents=$(cat "$headpath") &&
1026 grep "HEAD points to an invalid (or orphaned) reference" actual &&
1027 grep "HEAD path: .$headpath." actual &&
1028 grep "HEAD contents: .$headcontents." actual &&
1029 grep "$orphan_hint" actual &&
1030 ! grep "$info_text" actual
1031 fi &&
1032 grep "$invalid_ref_regex" actual
1033 else
1034 # Unreachable
1035 false
1037 ) &&
1038 if [ $success -ne 1 ]
1039 then
1040 test_path_is_missing foo
1045 for quiet_mode in "no_quiet" "quiet"
1047 for changedir_type in "cd_repo" "cd_wt" "-C_repo" "-C_wt"
1049 dwim_test_args="$quiet_mode $changedir_type"
1050 test_dwim_orphan 'infer' $dwim_test_args no_-b
1051 test_dwim_orphan 'no_infer' $dwim_test_args no_-b local_ref good_head
1052 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref no_remote no_remote_ref no_guess_remote
1053 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref no_guess_remote
1054 test_dwim_orphan 'fetch_error' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote
1055 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote force
1056 test_dwim_orphan 'no_infer' $dwim_test_args no_-b no_local_ref remote remote_ref guess_remote
1058 test_dwim_orphan 'infer' $dwim_test_args -b
1059 test_dwim_orphan 'no_infer' $dwim_test_args -b local_ref good_head
1060 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref no_remote no_remote_ref no_guess_remote
1061 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref no_guess_remote
1062 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref guess_remote
1063 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote remote_ref guess_remote
1065 test_dwim_orphan 'warn_bad_head' $dwim_test_args no_-b local_ref bad_head
1066 test_dwim_orphan 'warn_bad_head' $dwim_test_args -b local_ref bad_head
1067 test_dwim_orphan 'warn_bad_head' $dwim_test_args detach local_ref bad_head
1068 done
1070 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b no_checkout
1071 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b track
1072 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b no_checkout
1073 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b track
1074 done
1076 post_checkout_hook () {
1077 test_when_finished "rm -rf .git/hooks" &&
1078 mkdir .git/hooks &&
1079 test_hook -C "$1" post-checkout <<-\EOF
1081 echo $*
1082 git rev-parse --git-dir --show-toplevel
1083 } >hook.actual
1087 test_expect_success '"add" invokes post-checkout hook (branch)' '
1088 post_checkout_hook &&
1090 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1091 echo $(pwd)/.git/worktrees/gumby &&
1092 echo $(pwd)/gumby
1093 } >hook.expect &&
1094 git worktree add gumby &&
1095 test_cmp hook.expect gumby/hook.actual
1098 test_expect_success '"add" invokes post-checkout hook (detached)' '
1099 post_checkout_hook &&
1101 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1102 echo $(pwd)/.git/worktrees/grumpy &&
1103 echo $(pwd)/grumpy
1104 } >hook.expect &&
1105 git worktree add --detach grumpy &&
1106 test_cmp hook.expect grumpy/hook.actual
1109 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
1110 post_checkout_hook &&
1111 rm -f hook.actual &&
1112 git worktree add --no-checkout gloopy &&
1113 test_path_is_missing gloopy/hook.actual
1116 test_expect_success '"add" in other worktree invokes post-checkout hook' '
1117 post_checkout_hook &&
1119 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1120 echo $(pwd)/.git/worktrees/guppy &&
1121 echo $(pwd)/guppy
1122 } >hook.expect &&
1123 git -C gloopy worktree add --detach ../guppy &&
1124 test_cmp hook.expect guppy/hook.actual
1127 test_expect_success '"add" in bare repo invokes post-checkout hook' '
1128 rm -rf bare &&
1129 git clone --bare . bare &&
1131 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
1132 echo $(pwd)/bare/worktrees/goozy &&
1133 echo $(pwd)/goozy
1134 } >hook.expect &&
1135 post_checkout_hook bare &&
1136 git -C bare worktree add --detach ../goozy &&
1137 test_cmp hook.expect goozy/hook.actual
1140 test_expect_success '"add" an existing but missing worktree' '
1141 git worktree add --detach pneu &&
1142 test_must_fail git worktree add --detach pneu &&
1143 rm -fr pneu &&
1144 test_must_fail git worktree add --detach pneu &&
1145 git worktree add --force --detach pneu
1148 test_expect_success '"add" an existing locked but missing worktree' '
1149 git worktree add --detach gnoo &&
1150 git worktree lock gnoo &&
1151 test_when_finished "git worktree unlock gnoo || :" &&
1152 rm -fr gnoo &&
1153 test_must_fail git worktree add --detach gnoo &&
1154 test_must_fail git worktree add --force --detach gnoo &&
1155 git worktree add --force --force --detach gnoo
1158 test_expect_success '"add" not tripped up by magic worktree matching"' '
1159 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
1160 # directory `sub2` should not mistakenly complain that `bar` is an
1161 # already-registered worktree
1162 mkdir sub1 sub2 &&
1163 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
1164 git -C sub2 --git-dir=../.git worktree add --detach bozo
1167 test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
1168 git worktree add --detach ". weird*..?.lock.lock" &&
1169 test -d .git/worktrees/---weird-.-
1172 test_expect_success '"add" should not fail because of another bad worktree' '
1173 git init add-fail &&
1175 cd add-fail &&
1176 test_commit first &&
1177 mkdir sub &&
1178 git worktree add sub/to-be-deleted &&
1179 rm -rf sub &&
1180 git worktree add second
1184 test_expect_success '"add" with uninitialized submodule, with submodule.recurse unset' '
1185 test_config_global protocol.file.allow always &&
1186 test_create_repo submodule &&
1187 test_commit -C submodule first &&
1188 test_create_repo project &&
1189 git -C project submodule add ../submodule &&
1190 git -C project add submodule &&
1191 test_tick &&
1192 git -C project commit -m add_sub &&
1193 git clone project project-clone &&
1194 git -C project-clone worktree add ../project-2
1196 test_expect_success '"add" with uninitialized submodule, with submodule.recurse set' '
1197 git -C project-clone -c submodule.recurse worktree add ../project-3
1200 test_expect_success '"add" with initialized submodule, with submodule.recurse unset' '
1201 test_config_global protocol.file.allow always &&
1202 git -C project-clone submodule update --init &&
1203 git -C project-clone worktree add ../project-4
1206 test_expect_success '"add" with initialized submodule, with submodule.recurse set' '
1207 git -C project-clone -c submodule.recurse worktree add ../project-5
1210 test_done