]> git.ipfire.org Git - thirdparty/git.git/blob - t/t2400-worktree-add.sh
glossary: add definitions for dereference & peel
[thirdparty/git.git] / t / t2400-worktree-add.sh
1 #!/bin/sh
2
3 test_description='test git worktree add'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 TEST_CREATE_REPO_NO_TEMPLATE=1
9 . ./test-lib.sh
10
11 . "$TEST_DIRECTORY"/lib-rebase.sh
12
13 test_expect_success 'setup' '
14 test_commit init
15 '
16
17 test_expect_success '"add" an existing worktree' '
18 mkdir -p existing/subtree &&
19 test_must_fail git worktree add --detach existing main
20 '
21
22 test_expect_success '"add" an existing empty worktree' '
23 mkdir existing_empty &&
24 git worktree add --detach existing_empty main
25 '
26
27 test_expect_success '"add" using shorthand - fails when no previous branch' '
28 test_must_fail git worktree add existing_short -
29 '
30
31 test_expect_success '"add" using - shorthand' '
32 git checkout -b newbranch &&
33 echo hello >myworld &&
34 git add myworld &&
35 git commit -m myworld &&
36 git checkout main &&
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
41 '
42
43 test_expect_success '"add" refuses to checkout locked branch' '
44 test_must_fail git worktree add zere main &&
45 ! test -d zere &&
46 ! test -d .git/worktrees/zere
47 '
48
49 test_expect_success 'checking out paths not complaining about linked checkouts' '
50 (
51 cd existing_empty &&
52 echo dirty >>init.t &&
53 git checkout main -- init.t
54 )
55 '
56
57 test_expect_success '"add" worktree' '
58 git rev-parse HEAD >expect &&
59 git worktree add --detach here main &&
60 (
61 cd here &&
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 &&
66 git fsck
67 )
68 '
69
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
74 '
75
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
83 '
84
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
88 '
89
90 test_expect_success '"add" worktree from a subdir' '
91 (
92 mkdir sub &&
93 cd sub &&
94 git worktree add --detach here main &&
95 cd here &&
96 test_cmp ../../init.t init.t
97 )
98 '
99
100 test_expect_success '"add" from a linked checkout' '
101 (
102 cd here &&
103 git worktree add --detach nested-here main &&
104 cd nested-here &&
105 git fsck
106 )
107 '
108
109 test_expect_success '"add" worktree creating new branch' '
110 git worktree add -b newmain there main &&
111 (
112 cd there &&
113 test_cmp ../init.t init.t &&
114 git symbolic-ref HEAD >actual &&
115 echo refs/heads/newmain >expect &&
116 test_cmp expect actual &&
117 git fsck
118 )
119 '
120
121 test_expect_success 'die the same branch is already checked out' '
122 (
123 cd here &&
124 test_must_fail git checkout newmain
125 )
126 '
127
128 test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
129 head=$(git -C there rev-parse --git-path HEAD) &&
130 ref=$(git -C there symbolic-ref HEAD) &&
131 rm "$head" &&
132 ln -s "$ref" "$head" &&
133 test_must_fail git -C here checkout newmain
134 '
135
136 test_expect_success 'not die the same branch is already checked out' '
137 (
138 cd here &&
139 git worktree add --force anothernewmain newmain
140 )
141 '
142
143 test_expect_success 'not die on re-checking out current branch' '
144 (
145 cd there &&
146 git checkout newmain
147 )
148 '
149
150 test_expect_success '"add" from a bare repo' '
151 (
152 git clone --bare . bare &&
153 cd bare &&
154 git worktree add -b bare-main ../there2 main
155 )
156 '
157
158 test_expect_success 'checkout from a bare repo without "add"' '
159 (
160 cd bare &&
161 test_must_fail git checkout main
162 )
163 '
164
165 test_expect_success '"add" default branch of a bare repo' '
166 (
167 git clone --bare . bare2 &&
168 cd bare2 &&
169 git worktree add ../there3 main &&
170 cd ../there3 &&
171 # Simple check that a Git command does not
172 # immediately fail with the current setup
173 git status
174 ) &&
175 cat >expect <<-EOF &&
176 init.t
177 EOF
178 ls there3 >actual &&
179 test_cmp expect actual
180 '
181
182 test_expect_success '"add" to bare repo with worktree config' '
183 (
184 git clone --bare . bare3 &&
185 cd bare3 &&
186 git config extensions.worktreeconfig true &&
187
188 # Add config values that are erroneous to have in
189 # a config.worktree file outside of the main
190 # working tree, to check that Git filters them out
191 # when copying config during "git worktree add".
192 git config --worktree core.bare true &&
193 git config --worktree core.worktree "$(pwd)" &&
194
195 # We want to check that bogus.key is copied
196 git config --worktree bogus.key value &&
197 git config --unset core.bare &&
198 git worktree add ../there4 main &&
199 cd ../there4 &&
200
201 # Simple check that a Git command does not
202 # immediately fail with the current setup
203 git status &&
204 git worktree add --detach ../there5 &&
205 cd ../there5 &&
206 git status
207 ) &&
208
209 # the worktree has the arbitrary value copied.
210 test_cmp_config -C there4 value bogus.key &&
211 test_cmp_config -C there5 value bogus.key &&
212
213 # however, core.bare and core.worktree were removed.
214 test_must_fail git -C there4 config core.bare &&
215 test_must_fail git -C there4 config core.worktree &&
216
217 cat >expect <<-EOF &&
218 init.t
219 EOF
220
221 ls there4 >actual &&
222 test_cmp expect actual &&
223 ls there5 >actual &&
224 test_cmp expect actual
225 '
226
227 test_expect_success 'checkout with grafts' '
228 test_when_finished rm .git/info/grafts &&
229 test_commit abc &&
230 SHA1=$(git rev-parse HEAD) &&
231 test_commit def &&
232 test_commit xyz &&
233 mkdir .git/info &&
234 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
235 cat >expected <<-\EOF &&
236 xyz
237 abc
238 EOF
239 git log --format=%s -2 >actual &&
240 test_cmp expected actual &&
241 git worktree add --detach grafted main &&
242 git --git-dir=grafted/.git log --format=%s -2 >actual &&
243 test_cmp expected actual
244 '
245
246 test_expect_success '"add" from relative HEAD' '
247 test_commit a &&
248 test_commit b &&
249 test_commit c &&
250 git rev-parse HEAD~1 >expected &&
251 git worktree add relhead HEAD~1 &&
252 git -C relhead rev-parse HEAD >actual &&
253 test_cmp expected actual
254 '
255
256 test_expect_success '"add -b" with <branch> omitted' '
257 git worktree add -b burble flornk &&
258 test_cmp_rev HEAD burble
259 '
260
261 test_expect_success '"add --detach" with <branch> omitted' '
262 git worktree add --detach fishhook &&
263 git rev-parse HEAD >expected &&
264 git -C fishhook rev-parse HEAD >actual &&
265 test_cmp expected actual &&
266 test_must_fail git -C fishhook symbolic-ref HEAD
267 '
268
269 test_expect_success '"add" with <branch> omitted' '
270 git worktree add wiffle/bat &&
271 test_cmp_rev HEAD bat
272 '
273
274 test_expect_success '"add" checks out existing branch of dwimd name' '
275 git branch dwim HEAD~1 &&
276 git worktree add dwim &&
277 test_cmp_rev HEAD~1 dwim &&
278 (
279 cd dwim &&
280 test_cmp_rev HEAD dwim
281 )
282 '
283
284 test_expect_success '"add <path>" dwim fails with checked out branch' '
285 git checkout -b test-branch &&
286 test_must_fail git worktree add test-branch &&
287 test_path_is_missing test-branch
288 '
289
290 test_expect_success '"add --force" with existing dwimd name doesnt die' '
291 git checkout test-branch &&
292 git worktree add --force test-branch
293 '
294
295 test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
296 git worktree add --detach mish/mash &&
297 test_must_fail git rev-parse mash -- &&
298 test_must_fail git -C mish/mash symbolic-ref HEAD
299 '
300
301 # Helper function to test mutually exclusive options.
302 #
303 # Note: Quoted arguments containing spaces are not supported.
304 test_wt_add_excl () {
305 local opts="$*" &&
306 test_expect_success "'worktree add' with '$opts' has mutually exclusive options" '
307 test_must_fail git worktree add $opts 2>actual &&
308 grep -E "fatal:( options)? .* cannot be used together" actual
309 '
310 }
311
312 test_wt_add_excl -b poodle -B poodle bamboo main
313 test_wt_add_excl -b poodle --detach bamboo main
314 test_wt_add_excl -B poodle --detach bamboo main
315 test_wt_add_excl --orphan --detach bamboo
316 test_wt_add_excl --orphan --no-checkout bamboo
317 test_wt_add_excl --orphan bamboo main
318 test_wt_add_excl --orphan -b bamboo wtdir/ main
319
320 test_expect_success '"add -B" fails if the branch is checked out' '
321 git rev-parse newmain >before &&
322 test_must_fail git worktree add -B newmain bamboo main &&
323 git rev-parse newmain >after &&
324 test_cmp before after
325 '
326
327 test_expect_success 'add -B' '
328 git worktree add -B poodle bamboo2 main^ &&
329 git -C bamboo2 symbolic-ref HEAD >actual &&
330 echo refs/heads/poodle >expected &&
331 test_cmp expected actual &&
332 test_cmp_rev main^ poodle
333 '
334
335 test_expect_success 'add --quiet' '
336 test_when_finished "git worktree remove -f -f another-worktree" &&
337 git worktree add --quiet another-worktree main 2>actual &&
338 test_must_be_empty actual
339 '
340
341 test_expect_success 'add --quiet -b' '
342 test_when_finished "git branch -D quietnewbranch" &&
343 test_when_finished "git worktree remove -f -f another-worktree" &&
344 git worktree add --quiet -b quietnewbranch another-worktree 2>actual &&
345 test_must_be_empty actual
346 '
347
348 test_expect_success '"add --orphan"' '
349 test_when_finished "git worktree remove -f -f orphandir" &&
350 git worktree add --orphan -b neworphan orphandir &&
351 echo refs/heads/neworphan >expected &&
352 git -C orphandir symbolic-ref HEAD >actual &&
353 test_cmp expected actual
354 '
355
356 test_expect_success '"add --orphan (no -b)"' '
357 test_when_finished "git worktree remove -f -f neworphan" &&
358 git worktree add --orphan neworphan &&
359 echo refs/heads/neworphan >expected &&
360 git -C neworphan symbolic-ref HEAD >actual &&
361 test_cmp expected actual
362 '
363
364 test_expect_success '"add --orphan --quiet"' '
365 test_when_finished "git worktree remove -f -f orphandir" &&
366 git worktree add --quiet --orphan -b neworphan orphandir 2>log.actual &&
367 test_must_be_empty log.actual &&
368 echo refs/heads/neworphan >expected &&
369 git -C orphandir symbolic-ref HEAD >actual &&
370 test_cmp expected actual
371 '
372
373 test_expect_success '"add --orphan" fails if the branch already exists' '
374 test_when_finished "git branch -D existingbranch" &&
375 git worktree add -b existingbranch orphandir main &&
376 git worktree remove orphandir &&
377 test_must_fail git worktree add --orphan -b existingbranch orphandir
378 '
379
380 test_expect_success '"add --orphan" with empty repository' '
381 test_when_finished "rm -rf empty_repo" &&
382 echo refs/heads/newbranch >expected &&
383 GIT_DIR="empty_repo" git init --bare &&
384 git -C empty_repo worktree add --orphan -b newbranch worktreedir &&
385 git -C empty_repo/worktreedir symbolic-ref HEAD >actual &&
386 test_cmp expected actual
387 '
388
389 test_expect_success '"add" worktree with orphan branch and lock' '
390 git worktree add --lock --orphan -b orphanbr orphan-with-lock &&
391 test_when_finished "git worktree unlock orphan-with-lock || :" &&
392 test -f .git/worktrees/orphan-with-lock/locked
393 '
394
395 test_expect_success '"add" worktree with orphan branch, lock, and reason' '
396 lock_reason="why not" &&
397 git worktree add --detach --lock --reason "$lock_reason" orphan-with-lock-reason main &&
398 test_when_finished "git worktree unlock orphan-with-lock-reason || :" &&
399 test -f .git/worktrees/orphan-with-lock-reason/locked &&
400 echo "$lock_reason" >expect &&
401 test_cmp expect .git/worktrees/orphan-with-lock-reason/locked
402 '
403
404 # Note: Quoted arguments containing spaces are not supported.
405 test_wt_add_orphan_hint () {
406 local context="$1" &&
407 local use_branch=$2 &&
408 shift 2 &&
409 local opts="$*" &&
410 test_expect_success "'worktree add' show orphan hint in bad/orphan HEAD w/ $context" '
411 test_when_finished "rm -rf repo" &&
412 git init repo &&
413 (cd repo && test_commit commit) &&
414 git -C repo switch --orphan noref &&
415 test_must_fail git -C repo worktree add $opts foobar/ 2>actual &&
416 ! grep "error: unknown switch" actual &&
417 grep "hint: If you meant to create a worktree containing a new orphan branch" actual &&
418 if [ $use_branch -eq 1 ]
419 then
420 grep -E "^hint: +git worktree add --orphan -b [^ ]+ [^ ]+$" actual
421 else
422 grep -E "^hint: +git worktree add --orphan [^ ]+$" actual
423 fi
424
425 '
426 }
427
428 test_wt_add_orphan_hint 'no opts' 0
429 test_wt_add_orphan_hint '-b' 1 -b foobar_branch
430 test_wt_add_orphan_hint '-B' 1 -B foobar_branch
431
432 test_expect_success "'worktree add' doesn't show orphan hint in bad/orphan HEAD w/ --quiet" '
433 test_when_finished "rm -rf repo" &&
434 git init repo &&
435 (cd repo && test_commit commit) &&
436 test_must_fail git -C repo worktree add --quiet foobar_branch foobar/ 2>actual &&
437 ! grep "error: unknown switch" actual &&
438 ! grep "hint: If you meant to create a worktree containing a new orphan branch" actual
439 '
440
441 test_expect_success 'local clone from linked checkout' '
442 git clone --local here here-clone &&
443 ( cd here-clone && git fsck )
444 '
445
446 test_expect_success 'local clone --shared from linked checkout' '
447 git -C bare worktree add --detach ../baretree &&
448 git clone --local --shared baretree bare-clone &&
449 grep /bare/ bare-clone/.git/objects/info/alternates
450 '
451
452 test_expect_success '"add" worktree with --no-checkout' '
453 git worktree add --no-checkout -b swamp swamp &&
454 ! test -e swamp/init.t &&
455 git -C swamp reset --hard &&
456 test_cmp init.t swamp/init.t
457 '
458
459 test_expect_success '"add" worktree with --checkout' '
460 git worktree add --checkout -b swmap2 swamp2 &&
461 test_cmp init.t swamp2/init.t
462 '
463
464 test_expect_success 'put a worktree under rebase' '
465 git worktree add under-rebase &&
466 (
467 cd under-rebase &&
468 set_fake_editor &&
469 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
470 git worktree list | grep "under-rebase.*detached HEAD"
471 )
472 '
473
474 test_expect_success 'add a worktree, checking out a rebased branch' '
475 test_must_fail git worktree add new-rebase under-rebase &&
476 ! test -d new-rebase
477 '
478
479 test_expect_success 'checking out a rebased branch from another worktree' '
480 git worktree add new-place &&
481 test_must_fail git -C new-place checkout under-rebase
482 '
483
484 test_expect_success 'not allow to delete a branch under rebase' '
485 (
486 cd under-rebase &&
487 test_must_fail git branch -D under-rebase
488 )
489 '
490
491 test_expect_success 'rename a branch under rebase not allowed' '
492 test_must_fail git branch -M under-rebase rebase-with-new-name
493 '
494
495 test_expect_success 'check out from current worktree branch ok' '
496 (
497 cd under-rebase &&
498 git checkout under-rebase &&
499 git checkout - &&
500 git rebase --abort
501 )
502 '
503
504 test_expect_success 'checkout a branch under bisect' '
505 git worktree add under-bisect &&
506 (
507 cd under-bisect &&
508 git bisect start &&
509 git bisect bad &&
510 git bisect good HEAD~2 &&
511 git worktree list | grep "under-bisect.*detached HEAD" &&
512 test_must_fail git worktree add new-bisect under-bisect &&
513 ! test -d new-bisect
514 )
515 '
516
517 test_expect_success 'rename a branch under bisect not allowed' '
518 test_must_fail git branch -M under-bisect bisect-with-new-name
519 '
520 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
521 test_branch_upstream () {
522 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
523 {
524 git config "branch.$1.remote" &&
525 git config "branch.$1.merge"
526 } >actual.upstream &&
527 test_cmp expect.upstream actual.upstream
528 }
529
530 test_expect_success '--track sets up tracking' '
531 test_when_finished rm -rf track &&
532 git worktree add --track -b track track main &&
533 test_branch_upstream track . main
534 '
535
536 # setup remote repository $1 and repository $2 with $1 set up as
537 # remote. The remote has two branches, main and foo.
538 setup_remote_repo () {
539 git init $1 &&
540 (
541 cd $1 &&
542 test_commit $1_main &&
543 git checkout -b foo &&
544 test_commit upstream_foo
545 ) &&
546 git init $2 &&
547 (
548 cd $2 &&
549 test_commit $2_main &&
550 git remote add $1 ../$1 &&
551 git config remote.$1.fetch \
552 "refs/heads/*:refs/remotes/$1/*" &&
553 git fetch --all
554 )
555 }
556
557 test_expect_success '"add" <path> <remote/branch> w/ no HEAD' '
558 test_when_finished rm -rf repo_upstream repo_local foo &&
559 setup_remote_repo repo_upstream repo_local &&
560 git -C repo_local config --bool core.bare true &&
561 git -C repo_local branch -D main &&
562 git -C repo_local worktree add ./foo repo_upstream/foo
563 '
564
565 test_expect_success '--no-track avoids setting up tracking' '
566 test_when_finished rm -rf repo_upstream repo_local foo &&
567 setup_remote_repo repo_upstream repo_local &&
568 (
569 cd repo_local &&
570 git worktree add --no-track -b foo ../foo repo_upstream/foo
571 ) &&
572 (
573 cd foo &&
574 test_must_fail git config "branch.foo.remote" &&
575 test_must_fail git config "branch.foo.merge" &&
576 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
577 )
578 '
579
580 test_expect_success '"add" <path> <non-existent-branch> fails' '
581 test_must_fail git worktree add foo non-existent
582 '
583
584 test_expect_success '"add" <path> <branch> dwims' '
585 test_when_finished rm -rf repo_upstream repo_dwim foo &&
586 setup_remote_repo repo_upstream repo_dwim &&
587 git init repo_dwim &&
588 (
589 cd repo_dwim &&
590 git worktree add ../foo foo
591 ) &&
592 (
593 cd foo &&
594 test_branch_upstream foo repo_upstream foo &&
595 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
596 )
597 '
598
599 test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
600 test_when_finished rm -rf repo_upstream repo_dwim foo &&
601 setup_remote_repo repo_upstream repo_dwim &&
602 git init repo_dwim &&
603 (
604 cd repo_dwim &&
605 git remote add repo_upstream2 ../repo_upstream &&
606 git fetch repo_upstream2 &&
607 test_must_fail git worktree add ../foo foo &&
608 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
609 git status -uno --porcelain >status.actual &&
610 test_must_be_empty status.actual
611 ) &&
612 (
613 cd foo &&
614 test_branch_upstream foo repo_upstream foo &&
615 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
616 )
617 '
618
619 test_expect_success 'git worktree add does not match remote' '
620 test_when_finished rm -rf repo_a repo_b foo &&
621 setup_remote_repo repo_a repo_b &&
622 (
623 cd repo_b &&
624 git worktree add ../foo
625 ) &&
626 (
627 cd foo &&
628 test_must_fail git config "branch.foo.remote" &&
629 test_must_fail git config "branch.foo.merge" &&
630 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
631 )
632 '
633
634 test_expect_success 'git worktree add --guess-remote sets up tracking' '
635 test_when_finished rm -rf repo_a repo_b foo &&
636 setup_remote_repo repo_a repo_b &&
637 (
638 cd repo_b &&
639 git worktree add --guess-remote ../foo
640 ) &&
641 (
642 cd foo &&
643 test_branch_upstream foo repo_a foo &&
644 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
645 )
646 '
647 test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' '
648 test_when_finished rm -rf repo_a repo_b foo &&
649 setup_remote_repo repo_a repo_b &&
650 (
651 cd repo_b &&
652 git worktree add --quiet --guess-remote ../foo 2>actual &&
653 test_must_be_empty actual
654 ) &&
655 (
656 cd foo &&
657 test_branch_upstream foo repo_a foo &&
658 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
659 )
660 '
661
662 test_expect_success 'git worktree --no-guess-remote (quiet)' '
663 test_when_finished rm -rf repo_a repo_b foo &&
664 setup_remote_repo repo_a repo_b &&
665 (
666 cd repo_b &&
667 git worktree add --quiet --no-guess-remote ../foo
668 ) &&
669 (
670 cd foo &&
671 test_must_fail git config "branch.foo.remote" &&
672 test_must_fail git config "branch.foo.merge" &&
673 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
674 )
675 '
676
677 test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
678 test_when_finished rm -rf repo_a repo_b foo &&
679 setup_remote_repo repo_a repo_b &&
680 (
681 cd repo_b &&
682 git config worktree.guessRemote true &&
683 git worktree add ../foo
684 ) &&
685 (
686 cd foo &&
687 test_branch_upstream foo repo_a foo &&
688 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
689 )
690 '
691
692 test_expect_success 'git worktree --no-guess-remote option overrides config' '
693 test_when_finished rm -rf repo_a repo_b foo &&
694 setup_remote_repo repo_a repo_b &&
695 (
696 cd repo_b &&
697 git config worktree.guessRemote true &&
698 git worktree add --no-guess-remote ../foo
699 ) &&
700 (
701 cd foo &&
702 test_must_fail git config "branch.foo.remote" &&
703 test_must_fail git config "branch.foo.merge" &&
704 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
705 )
706 '
707
708 test_dwim_orphan () {
709 local info_text="No possible source branch, inferring '--orphan'" &&
710 local fetch_error_text="fatal: No local or remote refs exist despite at least one remote" &&
711 local orphan_hint="hint: If you meant to create a worktree containing a new orphan branch" &&
712 local invalid_ref_regex="^fatal: invalid reference: " &&
713 local bad_combo_regex="^fatal: '[-a-z]*' and '[-a-z]*' cannot be used together" &&
714
715 local git_ns="repo" &&
716 local dashc_args="-C $git_ns" &&
717 local use_cd=0 &&
718
719 local bad_head=0 &&
720 local empty_repo=1 &&
721 local local_ref=0 &&
722 local use_quiet=0 &&
723 local remote=0 &&
724 local remote_ref=0 &&
725 local use_detach=0 &&
726 local use_new_branch=0 &&
727
728 local outcome="$1" &&
729 local outcome_text &&
730 local success &&
731 shift &&
732 local args="" &&
733 local context="" &&
734 case "$outcome" in
735 "infer")
736 success=1 &&
737 outcome_text='"add" DWIM infer --orphan'
738 ;;
739 "no_infer")
740 success=1 &&
741 outcome_text='"add" DWIM doesnt infer --orphan'
742 ;;
743 "fetch_error")
744 success=0 &&
745 outcome_text='"add" error need fetch'
746 ;;
747 "fatal_orphan_bad_combo")
748 success=0 &&
749 outcome_text='"add" error inferred "--orphan" gives illegal opts combo'
750 ;;
751 "warn_bad_head")
752 success=0 &&
753 outcome_text='"add" error, warn on bad HEAD, hint use orphan'
754 ;;
755 *)
756 echo "test_dwim_orphan(): invalid outcome: '$outcome'" >&2 &&
757 return 1
758 ;;
759 esac &&
760 while [ $# -gt 0 ]
761 do
762 case "$1" in
763 # How and from where to create the worktree
764 "-C_repo")
765 use_cd=0 &&
766 git_ns="repo" &&
767 dashc_args="-C $git_ns" &&
768 context="$context, 'git -C repo'"
769 ;;
770 "-C_wt")
771 use_cd=0 &&
772 git_ns="wt" &&
773 dashc_args="-C $git_ns" &&
774 context="$context, 'git -C wt'"
775 ;;
776 "cd_repo")
777 use_cd=1 &&
778 git_ns="repo" &&
779 dashc_args="" &&
780 context="$context, 'cd repo && git'"
781 ;;
782 "cd_wt")
783 use_cd=1 &&
784 git_ns="wt" &&
785 dashc_args="" &&
786 context="$context, 'cd wt && git'"
787 ;;
788
789 # Bypass the "pull first" warning
790 "force")
791 args="$args --force" &&
792 context="$context, --force"
793 ;;
794
795 # Try to use remote refs when DWIM
796 "guess_remote")
797 args="$args --guess-remote" &&
798 context="$context, --guess-remote"
799 ;;
800 "no_guess_remote")
801 args="$args --no-guess-remote" &&
802 context="$context, --no-guess-remote"
803 ;;
804
805 # Whether there is at least one local branch present
806 "local_ref")
807 empty_repo=0 &&
808 local_ref=1 &&
809 context="$context, >=1 local branches"
810 ;;
811 "no_local_ref")
812 empty_repo=0 &&
813 context="$context, 0 local branches"
814 ;;
815
816 # Whether the HEAD points at a valid ref (skip this opt when no refs)
817 "good_head")
818 # requires: local_ref
819 context="$context, valid HEAD"
820 ;;
821 "bad_head")
822 bad_head=1 &&
823 context="$context, invalid (or orphan) HEAD"
824 ;;
825
826 # Whether the code path is tested with the base add command, -b, or --detach
827 "no_-b")
828 use_new_branch=0 &&
829 context="$context, no --branch"
830 ;;
831 "-b")
832 use_new_branch=1 &&
833 context="$context, --branch"
834 ;;
835 "detach")
836 use_detach=1 &&
837 context="$context, --detach"
838 ;;
839
840 # Whether to check that all output is suppressed (except errors)
841 # or that the output is as expected
842 "quiet")
843 use_quiet=1 &&
844 args="$args --quiet" &&
845 context="$context, --quiet"
846 ;;
847 "no_quiet")
848 use_quiet=0 &&
849 context="$context, no --quiet (expect output)"
850 ;;
851
852 # Whether there is at least one remote attached to the repo
853 "remote")
854 empty_repo=0 &&
855 remote=1 &&
856 context="$context, >=1 remotes"
857 ;;
858 "no_remote")
859 empty_repo=0 &&
860 remote=0 &&
861 context="$context, 0 remotes"
862 ;;
863
864 # Whether there is at least one valid remote ref
865 "remote_ref")
866 # requires: remote
867 empty_repo=0 &&
868 remote_ref=1 &&
869 context="$context, >=1 fetched remote branches"
870 ;;
871 "no_remote_ref")
872 empty_repo=0 &&
873 remote_ref=0 &&
874 context="$context, 0 fetched remote branches"
875 ;;
876
877 # Options or flags that become illegal when --orphan is inferred
878 "no_checkout")
879 args="$args --no-checkout" &&
880 context="$context, --no-checkout"
881 ;;
882 "track")
883 args="$args --track" &&
884 context="$context, --track"
885 ;;
886
887 # All other options are illegal
888 *)
889 echo "test_dwim_orphan(): invalid arg: '$1'" >&2 &&
890 return 1
891 ;;
892 esac &&
893 shift
894 done &&
895 context="${context#', '}" &&
896 if [ $use_new_branch -eq 1 ]
897 then
898 args="$args -b foo"
899 elif [ $use_detach -eq 1 ]
900 then
901 args="$args --detach"
902 else
903 context="DWIM (no --branch), $context"
904 fi &&
905 if [ $empty_repo -eq 1 ]
906 then
907 context="empty repo, $context"
908 fi &&
909 args="$args ../foo" &&
910 context="${context%', '}" &&
911 test_expect_success "$outcome_text w/ $context" '
912 test_when_finished "rm -rf repo" &&
913 git init repo &&
914 if [ $local_ref -eq 1 ] && [ "$git_ns" = "repo" ]
915 then
916 (cd repo && test_commit commit) &&
917 if [ $bad_head -eq 1 ]
918 then
919 git -C repo symbolic-ref HEAD refs/heads/badbranch
920 fi
921 elif [ $local_ref -eq 1 ] && [ "$git_ns" = "wt" ]
922 then
923 test_when_finished "git -C repo worktree remove -f ../wt" &&
924 git -C repo worktree add --orphan -b main ../wt &&
925 (cd wt && test_commit commit) &&
926 if [ $bad_head -eq 1 ]
927 then
928 git -C wt symbolic-ref HEAD refs/heads/badbranch
929 fi
930 elif [ $local_ref -eq 0 ] && [ "$git_ns" = "wt" ]
931 then
932 test_when_finished "git -C repo worktree remove -f ../wt" &&
933 git -C repo worktree add --orphan -b orphanbranch ../wt
934 fi &&
935
936 if [ $remote -eq 1 ]
937 then
938 test_when_finished "rm -rf upstream" &&
939 git init upstream &&
940 (cd upstream && test_commit commit) &&
941 git -C upstream switch -c foo &&
942 git -C repo remote add upstream ../upstream
943 fi &&
944
945 if [ $remote_ref -eq 1 ]
946 then
947 git -C repo fetch
948 fi &&
949 if [ $success -eq 1 ]
950 then
951 test_when_finished git -C repo worktree remove ../foo
952 fi &&
953 (
954 if [ $use_cd -eq 1 ]
955 then
956 cd $git_ns
957 fi &&
958 if [ "$outcome" = "infer" ]
959 then
960 git $dashc_args worktree add $args 2>actual &&
961 if [ $use_quiet -eq 1 ]
962 then
963 test_must_be_empty actual
964 else
965 grep "$info_text" actual
966 fi
967 elif [ "$outcome" = "no_infer" ]
968 then
969 git $dashc_args worktree add $args 2>actual &&
970 if [ $use_quiet -eq 1 ]
971 then
972 test_must_be_empty actual
973 else
974 ! grep "$info_text" actual
975 fi
976 elif [ "$outcome" = "fetch_error" ]
977 then
978 test_must_fail git $dashc_args worktree add $args 2>actual &&
979 grep "$fetch_error_text" actual
980 elif [ "$outcome" = "fatal_orphan_bad_combo" ]
981 then
982 test_must_fail git $dashc_args worktree add $args 2>actual &&
983 if [ $use_quiet -eq 1 ]
984 then
985 ! grep "$info_text" actual
986 else
987 grep "$info_text" actual
988 fi &&
989 grep "$bad_combo_regex" actual
990 elif [ "$outcome" = "warn_bad_head" ]
991 then
992 test_must_fail git $dashc_args worktree add $args 2>actual &&
993 if [ $use_quiet -eq 1 ]
994 then
995 grep "$invalid_ref_regex" actual &&
996 ! grep "$orphan_hint" actual
997 else
998 headpath=$(git $dashc_args rev-parse --path-format=absolute --git-path HEAD) &&
999 headcontents=$(cat "$headpath") &&
1000 grep "HEAD points to an invalid (or orphaned) reference" actual &&
1001 grep "HEAD path: .$headpath." actual &&
1002 grep "HEAD contents: .$headcontents." actual &&
1003 grep "$orphan_hint" actual &&
1004 ! grep "$info_text" actual
1005 fi &&
1006 grep "$invalid_ref_regex" actual
1007 else
1008 # Unreachable
1009 false
1010 fi
1011 ) &&
1012 if [ $success -ne 1 ]
1013 then
1014 test_path_is_missing foo
1015 fi
1016 '
1017 }
1018
1019 for quiet_mode in "no_quiet" "quiet"
1020 do
1021 for changedir_type in "cd_repo" "cd_wt" "-C_repo" "-C_wt"
1022 do
1023 dwim_test_args="$quiet_mode $changedir_type"
1024 test_dwim_orphan 'infer' $dwim_test_args no_-b
1025 test_dwim_orphan 'no_infer' $dwim_test_args no_-b local_ref good_head
1026 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref no_remote no_remote_ref no_guess_remote
1027 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref no_guess_remote
1028 test_dwim_orphan 'fetch_error' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote
1029 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote force
1030 test_dwim_orphan 'no_infer' $dwim_test_args no_-b no_local_ref remote remote_ref guess_remote
1031
1032 test_dwim_orphan 'infer' $dwim_test_args -b
1033 test_dwim_orphan 'no_infer' $dwim_test_args -b local_ref good_head
1034 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref no_remote no_remote_ref no_guess_remote
1035 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref no_guess_remote
1036 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref guess_remote
1037 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote remote_ref guess_remote
1038
1039 test_dwim_orphan 'warn_bad_head' $dwim_test_args no_-b local_ref bad_head
1040 test_dwim_orphan 'warn_bad_head' $dwim_test_args -b local_ref bad_head
1041 test_dwim_orphan 'warn_bad_head' $dwim_test_args detach local_ref bad_head
1042 done
1043
1044 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b no_checkout
1045 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b track
1046 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b no_checkout
1047 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b track
1048 done
1049
1050 post_checkout_hook () {
1051 test_when_finished "rm -rf .git/hooks" &&
1052 mkdir .git/hooks &&
1053 test_hook -C "$1" post-checkout <<-\EOF
1054 {
1055 echo $*
1056 git rev-parse --git-dir --show-toplevel
1057 } >hook.actual
1058 EOF
1059 }
1060
1061 test_expect_success '"add" invokes post-checkout hook (branch)' '
1062 post_checkout_hook &&
1063 {
1064 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1065 echo $(pwd)/.git/worktrees/gumby &&
1066 echo $(pwd)/gumby
1067 } >hook.expect &&
1068 git worktree add gumby &&
1069 test_cmp hook.expect gumby/hook.actual
1070 '
1071
1072 test_expect_success '"add" invokes post-checkout hook (detached)' '
1073 post_checkout_hook &&
1074 {
1075 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1076 echo $(pwd)/.git/worktrees/grumpy &&
1077 echo $(pwd)/grumpy
1078 } >hook.expect &&
1079 git worktree add --detach grumpy &&
1080 test_cmp hook.expect grumpy/hook.actual
1081 '
1082
1083 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
1084 post_checkout_hook &&
1085 rm -f hook.actual &&
1086 git worktree add --no-checkout gloopy &&
1087 test_path_is_missing gloopy/hook.actual
1088 '
1089
1090 test_expect_success '"add" in other worktree invokes post-checkout hook' '
1091 post_checkout_hook &&
1092 {
1093 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
1094 echo $(pwd)/.git/worktrees/guppy &&
1095 echo $(pwd)/guppy
1096 } >hook.expect &&
1097 git -C gloopy worktree add --detach ../guppy &&
1098 test_cmp hook.expect guppy/hook.actual
1099 '
1100
1101 test_expect_success '"add" in bare repo invokes post-checkout hook' '
1102 rm -rf bare &&
1103 git clone --bare . bare &&
1104 {
1105 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
1106 echo $(pwd)/bare/worktrees/goozy &&
1107 echo $(pwd)/goozy
1108 } >hook.expect &&
1109 post_checkout_hook bare &&
1110 git -C bare worktree add --detach ../goozy &&
1111 test_cmp hook.expect goozy/hook.actual
1112 '
1113
1114 test_expect_success '"add" an existing but missing worktree' '
1115 git worktree add --detach pneu &&
1116 test_must_fail git worktree add --detach pneu &&
1117 rm -fr pneu &&
1118 test_must_fail git worktree add --detach pneu &&
1119 git worktree add --force --detach pneu
1120 '
1121
1122 test_expect_success '"add" an existing locked but missing worktree' '
1123 git worktree add --detach gnoo &&
1124 git worktree lock gnoo &&
1125 test_when_finished "git worktree unlock gnoo || :" &&
1126 rm -fr gnoo &&
1127 test_must_fail git worktree add --detach gnoo &&
1128 test_must_fail git worktree add --force --detach gnoo &&
1129 git worktree add --force --force --detach gnoo
1130 '
1131
1132 test_expect_success '"add" not tripped up by magic worktree matching"' '
1133 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
1134 # directory `sub2` should not mistakenly complain that `bar` is an
1135 # already-registered worktree
1136 mkdir sub1 sub2 &&
1137 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
1138 git -C sub2 --git-dir=../.git worktree add --detach bozo
1139 '
1140
1141 test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
1142 git worktree add --detach ". weird*..?.lock.lock" &&
1143 test -d .git/worktrees/---weird-.-
1144 '
1145
1146 test_expect_success '"add" should not fail because of another bad worktree' '
1147 git init add-fail &&
1148 (
1149 cd add-fail &&
1150 test_commit first &&
1151 mkdir sub &&
1152 git worktree add sub/to-be-deleted &&
1153 rm -rf sub &&
1154 git worktree add second
1155 )
1156 '
1157
1158 test_expect_success '"add" with uninitialized submodule, with submodule.recurse unset' '
1159 test_config_global protocol.file.allow always &&
1160 test_create_repo submodule &&
1161 test_commit -C submodule first &&
1162 test_create_repo project &&
1163 git -C project submodule add ../submodule &&
1164 git -C project add submodule &&
1165 test_tick &&
1166 git -C project commit -m add_sub &&
1167 git clone project project-clone &&
1168 git -C project-clone worktree add ../project-2
1169 '
1170 test_expect_success '"add" with uninitialized submodule, with submodule.recurse set' '
1171 git -C project-clone -c submodule.recurse worktree add ../project-3
1172 '
1173
1174 test_expect_success '"add" with initialized submodule, with submodule.recurse unset' '
1175 test_config_global protocol.file.allow always &&
1176 git -C project-clone submodule update --init &&
1177 git -C project-clone worktree add ../project-4
1178 '
1179
1180 test_expect_success '"add" with initialized submodule, with submodule.recurse set' '
1181 git -C project-clone -c submodule.recurse worktree add ../project-5
1182 '
1183
1184 test_done