]> git.ipfire.org Git - thirdparty/git.git/blob - t/t2400-worktree-add.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / t2400-worktree-add.sh
1 #!/bin/sh
2
3 test_description='test git worktree add'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 TEST_CREATE_REPO_NO_TEMPLATE=1
9 . ./test-lib.sh
10
11 . "$TEST_DIRECTORY"/lib-rebase.sh
12
13 test_expect_success 'setup' '
14 test_commit init
15 '
16
17 test_expect_success '"add" an existing worktree' '
18 mkdir -p existing/subtree &&
19 test_must_fail git worktree add --detach existing main
20 '
21
22 test_expect_success '"add" an existing empty worktree' '
23 mkdir existing_empty &&
24 git worktree add --detach existing_empty main
25 '
26
27 test_expect_success '"add" using shorthand - fails when no previous branch' '
28 test_must_fail git worktree add existing_short -
29 '
30
31 test_expect_success '"add" using - shorthand' '
32 git checkout -b newbranch &&
33 echo hello >myworld &&
34 git add myworld &&
35 git commit -m myworld &&
36 git checkout main &&
37 git worktree add short-hand - &&
38 echo refs/heads/newbranch >expect &&
39 git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
40 test_cmp expect actual
41 '
42
43 test_expect_success '"add" refuses to checkout locked branch' '
44 test_must_fail git worktree add zere main &&
45 ! test -d zere &&
46 ! test -d .git/worktrees/zere
47 '
48
49 test_expect_success 'checking out paths not complaining about linked checkouts' '
50 (
51 cd existing_empty &&
52 echo dirty >>init.t &&
53 git checkout main -- init.t
54 )
55 '
56
57 test_expect_success '"add" worktree' '
58 git rev-parse HEAD >expect &&
59 git worktree add --detach here main &&
60 (
61 cd here &&
62 test_cmp ../init.t init.t &&
63 test_must_fail git symbolic-ref HEAD &&
64 git rev-parse HEAD >actual &&
65 test_cmp ../expect actual &&
66 git fsck
67 )
68 '
69
70 test_expect_success '"add" worktree with lock' '
71 git worktree add --detach --lock here-with-lock main &&
72 test_when_finished "git worktree unlock here-with-lock || :" &&
73 test -f .git/worktrees/here-with-lock/locked
74 '
75
76 test_expect_success '"add" worktree with lock and reason' '
77 lock_reason="why not" &&
78 git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main &&
79 test_when_finished "git worktree unlock here-with-lock-reason || :" &&
80 test -f .git/worktrees/here-with-lock-reason/locked &&
81 echo "$lock_reason" >expect &&
82 test_cmp expect .git/worktrees/here-with-lock-reason/locked
83 '
84
85 test_expect_success '"add" worktree with reason but no lock' '
86 test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main &&
87 test_path_is_missing .git/worktrees/here-with-reason-only/locked
88 '
89
90 test_expect_success '"add" worktree from a subdir' '
91 (
92 mkdir sub &&
93 cd sub &&
94 git worktree add --detach here main &&
95 cd here &&
96 test_cmp ../../init.t init.t
97 )
98 '
99
100 test_expect_success '"add" from a linked checkout' '
101 (
102 cd here &&
103 git worktree add --detach nested-here main &&
104 cd nested-here &&
105 git fsck
106 )
107 '
108
109 test_expect_success '"add" worktree creating new branch' '
110 git worktree add -b newmain there main &&
111 (
112 cd there &&
113 test_cmp ../init.t init.t &&
114 git symbolic-ref HEAD >actual &&
115 echo refs/heads/newmain >expect &&
116 test_cmp expect actual &&
117 git fsck
118 )
119 '
120
121 test_expect_success 'die the same branch is already checked out' '
122 (
123 cd here &&
124 test_must_fail git checkout newmain
125 )
126 '
127
128 test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
129 head=$(git -C there rev-parse --git-path HEAD) &&
130 ref=$(git -C there symbolic-ref HEAD) &&
131 rm "$head" &&
132 ln -s "$ref" "$head" &&
133 test_must_fail git -C here checkout newmain
134 '
135
136 test_expect_success 'not die the same branch is already checked out' '
137 (
138 cd here &&
139 git worktree add --force anothernewmain newmain
140 )
141 '
142
143 test_expect_success 'not die on re-checking out current branch' '
144 (
145 cd there &&
146 git checkout newmain
147 )
148 '
149
150 test_expect_success '"add" from a bare repo' '
151 (
152 git clone --bare . bare &&
153 cd bare &&
154 git worktree add -b bare-main ../there2 main
155 )
156 '
157
158 test_expect_success 'checkout from a bare repo without "add"' '
159 (
160 cd bare &&
161 test_must_fail git checkout main
162 )
163 '
164
165 test_expect_success '"add" default branch of a bare repo' '
166 (
167 git clone --bare . bare2 &&
168 cd bare2 &&
169 git worktree add ../there3 main &&
170 cd ../there3 &&
171 # Simple check that a Git command does not
172 # immediately fail with the current setup
173 git status
174 ) &&
175 cat >expect <<-EOF &&
176 init.t
177 EOF
178 ls there3 >actual &&
179 test_cmp expect actual
180 '
181
182 test_expect_success '"add" to bare repo with worktree config' '
183 (
184 git clone --bare . bare3 &&
185 cd bare3 &&
186 git config extensions.worktreeconfig true &&
187
188 # Add config values that are erroneous to have in
189 # a config.worktree file outside of the main
190 # working tree, to check that Git filters them out
191 # when copying config during "git worktree add".
192 git config --worktree core.bare true &&
193 git config --worktree core.worktree "$(pwd)" &&
194
195 # We want to check that bogus.key is copied
196 git config --worktree bogus.key value &&
197 git config --unset core.bare &&
198 git worktree add ../there4 main &&
199 cd ../there4 &&
200
201 # Simple check that a Git command does not
202 # immediately fail with the current setup
203 git status &&
204 git worktree add --detach ../there5 &&
205 cd ../there5 &&
206 git status
207 ) &&
208
209 # the worktree has the arbitrary value copied.
210 test_cmp_config -C there4 value bogus.key &&
211 test_cmp_config -C there5 value bogus.key &&
212
213 # however, core.bare and core.worktree were removed.
214 test_must_fail git -C there4 config core.bare &&
215 test_must_fail git -C there4 config core.worktree &&
216
217 cat >expect <<-EOF &&
218 init.t
219 EOF
220
221 ls there4 >actual &&
222 test_cmp expect actual &&
223 ls there5 >actual &&
224 test_cmp expect actual
225 '
226
227 test_expect_success 'checkout with grafts' '
228 test_when_finished rm .git/info/grafts &&
229 test_commit abc &&
230 SHA1=$(git rev-parse HEAD) &&
231 test_commit def &&
232 test_commit xyz &&
233 mkdir .git/info &&
234 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
235 cat >expected <<-\EOF &&
236 xyz
237 abc
238 EOF
239 git log --format=%s -2 >actual &&
240 test_cmp expected actual &&
241 git worktree add --detach grafted main &&
242 git --git-dir=grafted/.git log --format=%s -2 >actual &&
243 test_cmp expected actual
244 '
245
246 test_expect_success '"add" from relative HEAD' '
247 test_commit a &&
248 test_commit b &&
249 test_commit c &&
250 git rev-parse HEAD~1 >expected &&
251 git worktree add relhead HEAD~1 &&
252 git -C relhead rev-parse HEAD >actual &&
253 test_cmp expected actual
254 '
255
256 test_expect_success '"add -b" with <branch> omitted' '
257 git worktree add -b burble flornk &&
258 test_cmp_rev HEAD burble
259 '
260
261 test_expect_success '"add --detach" with <branch> omitted' '
262 git worktree add --detach fishhook &&
263 git rev-parse HEAD >expected &&
264 git -C fishhook rev-parse HEAD >actual &&
265 test_cmp expected actual &&
266 test_must_fail git -C fishhook symbolic-ref HEAD
267 '
268
269 test_expect_success '"add" with <branch> omitted' '
270 git worktree add wiffle/bat &&
271 test_cmp_rev HEAD bat
272 '
273
274 test_expect_success '"add" checks out existing branch of dwimd name' '
275 git branch dwim HEAD~1 &&
276 git worktree add dwim &&
277 test_cmp_rev HEAD~1 dwim &&
278 (
279 cd dwim &&
280 test_cmp_rev HEAD dwim
281 )
282 '
283
284 test_expect_success '"add <path>" dwim fails with checked out branch' '
285 git checkout -b test-branch &&
286 test_must_fail git worktree add test-branch &&
287 test_path_is_missing test-branch
288 '
289
290 test_expect_success '"add --force" with existing dwimd name doesnt die' '
291 git checkout test-branch &&
292 git worktree add --force test-branch
293 '
294
295 test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
296 git worktree add --detach mish/mash &&
297 test_must_fail git rev-parse mash -- &&
298 test_must_fail git -C mish/mash symbolic-ref HEAD
299 '
300
301 test_expect_success '"add" -b/-B mutually exclusive' '
302 test_must_fail git worktree add -b poodle -B poodle bamboo main
303 '
304
305 test_expect_success '"add" -b/--detach mutually exclusive' '
306 test_must_fail git worktree add -b poodle --detach bamboo main
307 '
308
309 test_expect_success '"add" -B/--detach mutually exclusive' '
310 test_must_fail git worktree add -B poodle --detach bamboo main
311 '
312
313 test_expect_success '"add -B" fails if the branch is checked out' '
314 git rev-parse newmain >before &&
315 test_must_fail git worktree add -B newmain bamboo main &&
316 git rev-parse newmain >after &&
317 test_cmp before after
318 '
319
320 test_expect_success 'add -B' '
321 git worktree add -B poodle bamboo2 main^ &&
322 git -C bamboo2 symbolic-ref HEAD >actual &&
323 echo refs/heads/poodle >expected &&
324 test_cmp expected actual &&
325 test_cmp_rev main^ poodle
326 '
327
328 test_expect_success 'add --quiet' '
329 git worktree add --quiet another-worktree main 2>actual &&
330 test_must_be_empty actual
331 '
332
333 test_expect_success 'local clone from linked checkout' '
334 git clone --local here here-clone &&
335 ( cd here-clone && git fsck )
336 '
337
338 test_expect_success 'local clone --shared from linked checkout' '
339 git -C bare worktree add --detach ../baretree &&
340 git clone --local --shared baretree bare-clone &&
341 grep /bare/ bare-clone/.git/objects/info/alternates
342 '
343
344 test_expect_success '"add" worktree with --no-checkout' '
345 git worktree add --no-checkout -b swamp swamp &&
346 ! test -e swamp/init.t &&
347 git -C swamp reset --hard &&
348 test_cmp init.t swamp/init.t
349 '
350
351 test_expect_success '"add" worktree with --checkout' '
352 git worktree add --checkout -b swmap2 swamp2 &&
353 test_cmp init.t swamp2/init.t
354 '
355
356 test_expect_success 'put a worktree under rebase' '
357 git worktree add under-rebase &&
358 (
359 cd under-rebase &&
360 set_fake_editor &&
361 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
362 git worktree list | grep "under-rebase.*detached HEAD"
363 )
364 '
365
366 test_expect_success 'add a worktree, checking out a rebased branch' '
367 test_must_fail git worktree add new-rebase under-rebase &&
368 ! test -d new-rebase
369 '
370
371 test_expect_success 'checking out a rebased branch from another worktree' '
372 git worktree add new-place &&
373 test_must_fail git -C new-place checkout under-rebase
374 '
375
376 test_expect_success 'not allow to delete a branch under rebase' '
377 (
378 cd under-rebase &&
379 test_must_fail git branch -D under-rebase
380 )
381 '
382
383 test_expect_success 'rename a branch under rebase not allowed' '
384 test_must_fail git branch -M under-rebase rebase-with-new-name
385 '
386
387 test_expect_success 'check out from current worktree branch ok' '
388 (
389 cd under-rebase &&
390 git checkout under-rebase &&
391 git checkout - &&
392 git rebase --abort
393 )
394 '
395
396 test_expect_success 'checkout a branch under bisect' '
397 git worktree add under-bisect &&
398 (
399 cd under-bisect &&
400 git bisect start &&
401 git bisect bad &&
402 git bisect good HEAD~2 &&
403 git worktree list | grep "under-bisect.*detached HEAD" &&
404 test_must_fail git worktree add new-bisect under-bisect &&
405 ! test -d new-bisect
406 )
407 '
408
409 test_expect_success 'rename a branch under bisect not allowed' '
410 test_must_fail git branch -M under-bisect bisect-with-new-name
411 '
412 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
413 test_branch_upstream () {
414 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
415 {
416 git config "branch.$1.remote" &&
417 git config "branch.$1.merge"
418 } >actual.upstream &&
419 test_cmp expect.upstream actual.upstream
420 }
421
422 test_expect_success '--track sets up tracking' '
423 test_when_finished rm -rf track &&
424 git worktree add --track -b track track main &&
425 test_branch_upstream track . main
426 '
427
428 # setup remote repository $1 and repository $2 with $1 set up as
429 # remote. The remote has two branches, main and foo.
430 setup_remote_repo () {
431 git init $1 &&
432 (
433 cd $1 &&
434 test_commit $1_main &&
435 git checkout -b foo &&
436 test_commit upstream_foo
437 ) &&
438 git init $2 &&
439 (
440 cd $2 &&
441 test_commit $2_main &&
442 git remote add $1 ../$1 &&
443 git config remote.$1.fetch \
444 "refs/heads/*:refs/remotes/$1/*" &&
445 git fetch --all
446 )
447 }
448
449 test_expect_success '--no-track avoids setting up tracking' '
450 test_when_finished rm -rf repo_upstream repo_local foo &&
451 setup_remote_repo repo_upstream repo_local &&
452 (
453 cd repo_local &&
454 git worktree add --no-track -b foo ../foo repo_upstream/foo
455 ) &&
456 (
457 cd foo &&
458 test_must_fail git config "branch.foo.remote" &&
459 test_must_fail git config "branch.foo.merge" &&
460 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
461 )
462 '
463
464 test_expect_success '"add" <path> <non-existent-branch> fails' '
465 test_must_fail git worktree add foo non-existent
466 '
467
468 test_expect_success '"add" <path> <branch> dwims' '
469 test_when_finished rm -rf repo_upstream repo_dwim foo &&
470 setup_remote_repo repo_upstream repo_dwim &&
471 git init repo_dwim &&
472 (
473 cd repo_dwim &&
474 git worktree add ../foo foo
475 ) &&
476 (
477 cd foo &&
478 test_branch_upstream foo repo_upstream foo &&
479 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
480 )
481 '
482
483 test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
484 test_when_finished rm -rf repo_upstream repo_dwim foo &&
485 setup_remote_repo repo_upstream repo_dwim &&
486 git init repo_dwim &&
487 (
488 cd repo_dwim &&
489 git remote add repo_upstream2 ../repo_upstream &&
490 git fetch repo_upstream2 &&
491 test_must_fail git worktree add ../foo foo &&
492 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
493 git status -uno --porcelain >status.actual &&
494 test_must_be_empty status.actual
495 ) &&
496 (
497 cd foo &&
498 test_branch_upstream foo repo_upstream foo &&
499 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
500 )
501 '
502
503 test_expect_success 'git worktree add does not match remote' '
504 test_when_finished rm -rf repo_a repo_b foo &&
505 setup_remote_repo repo_a repo_b &&
506 (
507 cd repo_b &&
508 git worktree add ../foo
509 ) &&
510 (
511 cd foo &&
512 test_must_fail git config "branch.foo.remote" &&
513 test_must_fail git config "branch.foo.merge" &&
514 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
515 )
516 '
517
518 test_expect_success 'git worktree add --guess-remote sets up tracking' '
519 test_when_finished rm -rf repo_a repo_b foo &&
520 setup_remote_repo repo_a repo_b &&
521 (
522 cd repo_b &&
523 git worktree add --guess-remote ../foo
524 ) &&
525 (
526 cd foo &&
527 test_branch_upstream foo repo_a foo &&
528 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
529 )
530 '
531
532 test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
533 test_when_finished rm -rf repo_a repo_b foo &&
534 setup_remote_repo repo_a repo_b &&
535 (
536 cd repo_b &&
537 git config worktree.guessRemote true &&
538 git worktree add ../foo
539 ) &&
540 (
541 cd foo &&
542 test_branch_upstream foo repo_a foo &&
543 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
544 )
545 '
546
547 test_expect_success 'git worktree --no-guess-remote option overrides config' '
548 test_when_finished rm -rf repo_a repo_b foo &&
549 setup_remote_repo repo_a repo_b &&
550 (
551 cd repo_b &&
552 git config worktree.guessRemote true &&
553 git worktree add --no-guess-remote ../foo
554 ) &&
555 (
556 cd foo &&
557 test_must_fail git config "branch.foo.remote" &&
558 test_must_fail git config "branch.foo.merge" &&
559 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
560 )
561 '
562
563 post_checkout_hook () {
564 test_when_finished "rm -rf .git/hooks" &&
565 mkdir .git/hooks &&
566 test_hook -C "$1" post-checkout <<-\EOF
567 {
568 echo $*
569 git rev-parse --git-dir --show-toplevel
570 } >hook.actual
571 EOF
572 }
573
574 test_expect_success '"add" invokes post-checkout hook (branch)' '
575 post_checkout_hook &&
576 {
577 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
578 echo $(pwd)/.git/worktrees/gumby &&
579 echo $(pwd)/gumby
580 } >hook.expect &&
581 git worktree add gumby &&
582 test_cmp hook.expect gumby/hook.actual
583 '
584
585 test_expect_success '"add" invokes post-checkout hook (detached)' '
586 post_checkout_hook &&
587 {
588 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
589 echo $(pwd)/.git/worktrees/grumpy &&
590 echo $(pwd)/grumpy
591 } >hook.expect &&
592 git worktree add --detach grumpy &&
593 test_cmp hook.expect grumpy/hook.actual
594 '
595
596 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
597 post_checkout_hook &&
598 rm -f hook.actual &&
599 git worktree add --no-checkout gloopy &&
600 test_path_is_missing gloopy/hook.actual
601 '
602
603 test_expect_success '"add" in other worktree invokes post-checkout hook' '
604 post_checkout_hook &&
605 {
606 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
607 echo $(pwd)/.git/worktrees/guppy &&
608 echo $(pwd)/guppy
609 } >hook.expect &&
610 git -C gloopy worktree add --detach ../guppy &&
611 test_cmp hook.expect guppy/hook.actual
612 '
613
614 test_expect_success '"add" in bare repo invokes post-checkout hook' '
615 rm -rf bare &&
616 git clone --bare . bare &&
617 {
618 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
619 echo $(pwd)/bare/worktrees/goozy &&
620 echo $(pwd)/goozy
621 } >hook.expect &&
622 post_checkout_hook bare &&
623 git -C bare worktree add --detach ../goozy &&
624 test_cmp hook.expect goozy/hook.actual
625 '
626
627 test_expect_success '"add" an existing but missing worktree' '
628 git worktree add --detach pneu &&
629 test_must_fail git worktree add --detach pneu &&
630 rm -fr pneu &&
631 test_must_fail git worktree add --detach pneu &&
632 git worktree add --force --detach pneu
633 '
634
635 test_expect_success '"add" an existing locked but missing worktree' '
636 git worktree add --detach gnoo &&
637 git worktree lock gnoo &&
638 test_when_finished "git worktree unlock gnoo || :" &&
639 rm -fr gnoo &&
640 test_must_fail git worktree add --detach gnoo &&
641 test_must_fail git worktree add --force --detach gnoo &&
642 git worktree add --force --force --detach gnoo
643 '
644
645 test_expect_success '"add" not tripped up by magic worktree matching"' '
646 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
647 # directory `sub2` should not mistakenly complain that `bar` is an
648 # already-registered worktree
649 mkdir sub1 sub2 &&
650 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
651 git -C sub2 --git-dir=../.git worktree add --detach bozo
652 '
653
654 test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
655 git worktree add --detach ". weird*..?.lock.lock" &&
656 test -d .git/worktrees/---weird-.-
657 '
658
659 test_expect_success '"add" should not fail because of another bad worktree' '
660 git init add-fail &&
661 (
662 cd add-fail &&
663 test_commit first &&
664 mkdir sub &&
665 git worktree add sub/to-be-deleted &&
666 rm -rf sub &&
667 git worktree add second
668 )
669 '
670
671 test_expect_success '"add" with uninitialized submodule, with submodule.recurse unset' '
672 test_create_repo submodule &&
673 test_commit -C submodule first &&
674 test_create_repo project &&
675 git -C project submodule add ../submodule &&
676 git -C project add submodule &&
677 test_tick &&
678 git -C project commit -m add_sub &&
679 git clone project project-clone &&
680 git -C project-clone worktree add ../project-2
681 '
682 test_expect_success '"add" with uninitialized submodule, with submodule.recurse set' '
683 git -C project-clone -c submodule.recurse worktree add ../project-3
684 '
685
686 test_expect_success '"add" with initialized submodule, with submodule.recurse unset' '
687 git -C project-clone submodule update --init &&
688 git -C project-clone worktree add ../project-4
689 '
690
691 test_expect_success '"add" with initialized submodule, with submodule.recurse set' '
692 git -C project-clone -c submodule.recurse worktree add ../project-5
693 '
694
695 test_done