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