at commit 382f8815

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