at commit b70101c2

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