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