at commit a6de316e

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