tree-wide: apply equals-null.cocci
[alt-git.git] / t / t2400-worktree-add.sh
blob37ad79470fb9e009e99447b04cc05673314e49e3
1 #!/bin/sh
3 test_description='test git worktree add'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 . "$TEST_DIRECTORY"/lib-rebase.sh
12 test_expect_success 'setup' '
13 test_commit init
16 test_expect_success '"add" an existing worktree' '
17 mkdir -p existing/subtree &&
18 test_must_fail git worktree add --detach existing main
21 test_expect_success '"add" an existing empty worktree' '
22 mkdir existing_empty &&
23 git worktree add --detach existing_empty main
26 test_expect_success '"add" using shorthand - fails when no previous branch' '
27 test_must_fail git worktree add existing_short -
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
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
48 test_expect_success 'checking out paths not complaining about linked checkouts' '
50 cd existing_empty &&
51 echo dirty >>init.t &&
52 git checkout main -- init.t
56 test_expect_success '"add" worktree' '
57 git rev-parse HEAD >expect &&
58 git worktree add --detach here main &&
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
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
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
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
89 test_expect_success '"add" worktree from a subdir' '
91 mkdir sub &&
92 cd sub &&
93 git worktree add --detach here main &&
94 cd here &&
95 test_cmp ../../init.t init.t
99 test_expect_success '"add" from a linked checkout' '
101 cd here &&
102 git worktree add --detach nested-here main &&
103 cd nested-here &&
104 git fsck
108 test_expect_success '"add" worktree creating new branch' '
109 git worktree add -b newmain there main &&
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
120 test_expect_success 'die the same branch is already checked out' '
122 cd here &&
123 test_must_fail git checkout newmain
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
135 test_expect_success 'not die the same branch is already checked out' '
137 cd here &&
138 git worktree add --force anothernewmain newmain
142 test_expect_success 'not die on re-checking out current branch' '
144 cd there &&
145 git checkout newmain
149 test_expect_success '"add" from a bare repo' '
151 git clone --bare . bare &&
152 cd bare &&
153 git worktree add -b bare-main ../there2 main
157 test_expect_success 'checkout from a bare repo without "add"' '
159 cd bare &&
160 test_must_fail git checkout main
164 test_expect_success '"add" default branch of a bare repo' '
166 git clone --bare . bare2 &&
167 cd bare2 &&
168 git worktree add ../there3 main
172 test_expect_success 'checkout with grafts' '
173 test_when_finished rm .git/info/grafts &&
174 test_commit abc &&
175 SHA1=$(git rev-parse HEAD) &&
176 test_commit def &&
177 test_commit xyz &&
178 echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
179 cat >expected <<-\EOF &&
183 git log --format=%s -2 >actual &&
184 test_cmp expected actual &&
185 git worktree add --detach grafted main &&
186 git --git-dir=grafted/.git log --format=%s -2 >actual &&
187 test_cmp expected actual
190 test_expect_success '"add" from relative HEAD' '
191 test_commit a &&
192 test_commit b &&
193 test_commit c &&
194 git rev-parse HEAD~1 >expected &&
195 git worktree add relhead HEAD~1 &&
196 git -C relhead rev-parse HEAD >actual &&
197 test_cmp expected actual
200 test_expect_success '"add -b" with <branch> omitted' '
201 git worktree add -b burble flornk &&
202 test_cmp_rev HEAD burble
205 test_expect_success '"add --detach" with <branch> omitted' '
206 git worktree add --detach fishhook &&
207 git rev-parse HEAD >expected &&
208 git -C fishhook rev-parse HEAD >actual &&
209 test_cmp expected actual &&
210 test_must_fail git -C fishhook symbolic-ref HEAD
213 test_expect_success '"add" with <branch> omitted' '
214 git worktree add wiffle/bat &&
215 test_cmp_rev HEAD bat
218 test_expect_success '"add" checks out existing branch of dwimd name' '
219 git branch dwim HEAD~1 &&
220 git worktree add dwim &&
221 test_cmp_rev HEAD~1 dwim &&
223 cd dwim &&
224 test_cmp_rev HEAD dwim
228 test_expect_success '"add <path>" dwim fails with checked out branch' '
229 git checkout -b test-branch &&
230 test_must_fail git worktree add test-branch &&
231 test_path_is_missing test-branch
234 test_expect_success '"add --force" with existing dwimd name doesnt die' '
235 git checkout test-branch &&
236 git worktree add --force test-branch
239 test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
240 git worktree add --detach mish/mash &&
241 test_must_fail git rev-parse mash -- &&
242 test_must_fail git -C mish/mash symbolic-ref HEAD
245 test_expect_success '"add" -b/-B mutually exclusive' '
246 test_must_fail git worktree add -b poodle -B poodle bamboo main
249 test_expect_success '"add" -b/--detach mutually exclusive' '
250 test_must_fail git worktree add -b poodle --detach bamboo main
253 test_expect_success '"add" -B/--detach mutually exclusive' '
254 test_must_fail git worktree add -B poodle --detach bamboo main
257 test_expect_success '"add -B" fails if the branch is checked out' '
258 git rev-parse newmain >before &&
259 test_must_fail git worktree add -B newmain bamboo main &&
260 git rev-parse newmain >after &&
261 test_cmp before after
264 test_expect_success 'add -B' '
265 git worktree add -B poodle bamboo2 main^ &&
266 git -C bamboo2 symbolic-ref HEAD >actual &&
267 echo refs/heads/poodle >expected &&
268 test_cmp expected actual &&
269 test_cmp_rev main^ poodle
272 test_expect_success 'add --quiet' '
273 git worktree add --quiet another-worktree main 2>actual &&
274 test_must_be_empty actual
277 test_expect_success 'local clone from linked checkout' '
278 git clone --local here here-clone &&
279 ( cd here-clone && git fsck )
282 test_expect_success 'local clone --shared from linked checkout' '
283 git -C bare worktree add --detach ../baretree &&
284 git clone --local --shared baretree bare-clone &&
285 grep /bare/ bare-clone/.git/objects/info/alternates
288 test_expect_success '"add" worktree with --no-checkout' '
289 git worktree add --no-checkout -b swamp swamp &&
290 ! test -e swamp/init.t &&
291 git -C swamp reset --hard &&
292 test_cmp init.t swamp/init.t
295 test_expect_success '"add" worktree with --checkout' '
296 git worktree add --checkout -b swmap2 swamp2 &&
297 test_cmp init.t swamp2/init.t
300 test_expect_success 'put a worktree under rebase' '
301 git worktree add under-rebase &&
303 cd under-rebase &&
304 set_fake_editor &&
305 FAKE_LINES="edit 1" git rebase -i HEAD^ &&
306 git worktree list | grep "under-rebase.*detached HEAD"
310 test_expect_success 'add a worktree, checking out a rebased branch' '
311 test_must_fail git worktree add new-rebase under-rebase &&
312 ! test -d new-rebase
315 test_expect_success 'checking out a rebased branch from another worktree' '
316 git worktree add new-place &&
317 test_must_fail git -C new-place checkout under-rebase
320 test_expect_success 'not allow to delete a branch under rebase' '
322 cd under-rebase &&
323 test_must_fail git branch -D under-rebase
327 test_expect_success 'rename a branch under rebase not allowed' '
328 test_must_fail git branch -M under-rebase rebase-with-new-name
331 test_expect_success 'check out from current worktree branch ok' '
333 cd under-rebase &&
334 git checkout under-rebase &&
335 git checkout - &&
336 git rebase --abort
340 test_expect_success 'checkout a branch under bisect' '
341 git worktree add under-bisect &&
343 cd under-bisect &&
344 git bisect start &&
345 git bisect bad &&
346 git bisect good HEAD~2 &&
347 git worktree list | grep "under-bisect.*detached HEAD" &&
348 test_must_fail git worktree add new-bisect under-bisect &&
349 ! test -d new-bisect
353 test_expect_success 'rename a branch under bisect not allowed' '
354 test_must_fail git branch -M under-bisect bisect-with-new-name
356 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
357 test_branch_upstream () {
358 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
360 git config "branch.$1.remote" &&
361 git config "branch.$1.merge"
362 } >actual.upstream &&
363 test_cmp expect.upstream actual.upstream
366 test_expect_success '--track sets up tracking' '
367 test_when_finished rm -rf track &&
368 git worktree add --track -b track track main &&
369 test_branch_upstream track . main
372 # setup remote repository $1 and repository $2 with $1 set up as
373 # remote. The remote has two branches, main and foo.
374 setup_remote_repo () {
375 git init $1 &&
377 cd $1 &&
378 test_commit $1_main &&
379 git checkout -b foo &&
380 test_commit upstream_foo
381 ) &&
382 git init $2 &&
384 cd $2 &&
385 test_commit $2_main &&
386 git remote add $1 ../$1 &&
387 git config remote.$1.fetch \
388 "refs/heads/*:refs/remotes/$1/*" &&
389 git fetch --all
393 test_expect_success '--no-track avoids setting up tracking' '
394 test_when_finished rm -rf repo_upstream repo_local foo &&
395 setup_remote_repo repo_upstream repo_local &&
397 cd repo_local &&
398 git worktree add --no-track -b foo ../foo repo_upstream/foo
399 ) &&
401 cd foo &&
402 test_must_fail git config "branch.foo.remote" &&
403 test_must_fail git config "branch.foo.merge" &&
404 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
408 test_expect_success '"add" <path> <non-existent-branch> fails' '
409 test_must_fail git worktree add foo non-existent
412 test_expect_success '"add" <path> <branch> dwims' '
413 test_when_finished rm -rf repo_upstream repo_dwim foo &&
414 setup_remote_repo repo_upstream repo_dwim &&
415 git init repo_dwim &&
417 cd repo_dwim &&
418 git worktree add ../foo foo
419 ) &&
421 cd foo &&
422 test_branch_upstream foo repo_upstream foo &&
423 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
427 test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
428 test_when_finished rm -rf repo_upstream repo_dwim foo &&
429 setup_remote_repo repo_upstream repo_dwim &&
430 git init repo_dwim &&
432 cd repo_dwim &&
433 git remote add repo_upstream2 ../repo_upstream &&
434 git fetch repo_upstream2 &&
435 test_must_fail git worktree add ../foo foo &&
436 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
437 git status -uno --porcelain >status.actual &&
438 test_must_be_empty status.actual
439 ) &&
441 cd foo &&
442 test_branch_upstream foo repo_upstream foo &&
443 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
447 test_expect_success 'git worktree add does not match remote' '
448 test_when_finished rm -rf repo_a repo_b foo &&
449 setup_remote_repo repo_a repo_b &&
451 cd repo_b &&
452 git worktree add ../foo
453 ) &&
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_a/foo refs/heads/foo
462 test_expect_success 'git worktree add --guess-remote sets up tracking' '
463 test_when_finished rm -rf repo_a repo_b foo &&
464 setup_remote_repo repo_a repo_b &&
466 cd repo_b &&
467 git worktree add --guess-remote ../foo
468 ) &&
470 cd foo &&
471 test_branch_upstream foo repo_a foo &&
472 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
476 test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
477 test_when_finished rm -rf repo_a repo_b foo &&
478 setup_remote_repo repo_a repo_b &&
480 cd repo_b &&
481 git config worktree.guessRemote true &&
482 git worktree add ../foo
483 ) &&
485 cd foo &&
486 test_branch_upstream foo repo_a foo &&
487 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
491 test_expect_success 'git worktree --no-guess-remote option overrides config' '
492 test_when_finished rm -rf repo_a repo_b foo &&
493 setup_remote_repo repo_a repo_b &&
495 cd repo_b &&
496 git config worktree.guessRemote true &&
497 git worktree add --no-guess-remote ../foo
498 ) &&
500 cd foo &&
501 test_must_fail git config "branch.foo.remote" &&
502 test_must_fail git config "branch.foo.merge" &&
503 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
507 post_checkout_hook () {
508 gitdir=${1:-.git}
509 test_when_finished "rm -f $gitdir/hooks/post-checkout" &&
510 mkdir -p $gitdir/hooks &&
511 write_script $gitdir/hooks/post-checkout <<-\EOF
513 echo $*
514 git rev-parse --git-dir --show-toplevel
515 } >hook.actual
519 test_expect_success '"add" invokes post-checkout hook (branch)' '
520 post_checkout_hook &&
522 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
523 echo $(pwd)/.git/worktrees/gumby &&
524 echo $(pwd)/gumby
525 } >hook.expect &&
526 git worktree add gumby &&
527 test_cmp hook.expect gumby/hook.actual
530 test_expect_success '"add" invokes post-checkout hook (detached)' '
531 post_checkout_hook &&
533 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
534 echo $(pwd)/.git/worktrees/grumpy &&
535 echo $(pwd)/grumpy
536 } >hook.expect &&
537 git worktree add --detach grumpy &&
538 test_cmp hook.expect grumpy/hook.actual
541 test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
542 post_checkout_hook &&
543 rm -f hook.actual &&
544 git worktree add --no-checkout gloopy &&
545 test_path_is_missing gloopy/hook.actual
548 test_expect_success '"add" in other worktree invokes post-checkout hook' '
549 post_checkout_hook &&
551 echo $ZERO_OID $(git rev-parse HEAD) 1 &&
552 echo $(pwd)/.git/worktrees/guppy &&
553 echo $(pwd)/guppy
554 } >hook.expect &&
555 git -C gloopy worktree add --detach ../guppy &&
556 test_cmp hook.expect guppy/hook.actual
559 test_expect_success '"add" in bare repo invokes post-checkout hook' '
560 rm -rf bare &&
561 git clone --bare . bare &&
563 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
564 echo $(pwd)/bare/worktrees/goozy &&
565 echo $(pwd)/goozy
566 } >hook.expect &&
567 post_checkout_hook bare &&
568 git -C bare worktree add --detach ../goozy &&
569 test_cmp hook.expect goozy/hook.actual
572 test_expect_success '"add" an existing but missing worktree' '
573 git worktree add --detach pneu &&
574 test_must_fail git worktree add --detach pneu &&
575 rm -fr pneu &&
576 test_must_fail git worktree add --detach pneu &&
577 git worktree add --force --detach pneu
580 test_expect_success '"add" an existing locked but missing worktree' '
581 git worktree add --detach gnoo &&
582 git worktree lock gnoo &&
583 test_when_finished "git worktree unlock gnoo || :" &&
584 rm -fr gnoo &&
585 test_must_fail git worktree add --detach gnoo &&
586 test_must_fail git worktree add --force --detach gnoo &&
587 git worktree add --force --force --detach gnoo
590 test_expect_success '"add" not tripped up by magic worktree matching"' '
591 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
592 # directory `sub2` should not mistakenly complain that `bar` is an
593 # already-registered worktree
594 mkdir sub1 sub2 &&
595 git -C sub1 --git-dir=../.git worktree add --detach bozo &&
596 git -C sub2 --git-dir=../.git worktree add --detach bozo
599 test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
600 git worktree add --detach ". weird*..?.lock.lock" &&
601 test -d .git/worktrees/---weird-.-
604 test_expect_success '"add" should not fail because of another bad worktree' '
605 git init add-fail &&
607 cd add-fail &&
608 test_commit first &&
609 mkdir sub &&
610 git worktree add sub/to-be-deleted &&
611 rm -rf sub &&
612 git worktree add second
616 test_expect_success '"add" with uninitialized submodule, with submodule.recurse unset' '
617 test_create_repo submodule &&
618 test_commit -C submodule first &&
619 test_create_repo project &&
620 git -C project submodule add ../submodule &&
621 git -C project add submodule &&
622 test_tick &&
623 git -C project commit -m add_sub &&
624 git clone project project-clone &&
625 git -C project-clone worktree add ../project-2
627 test_expect_success '"add" with uninitialized submodule, with submodule.recurse set' '
628 git -C project-clone -c submodule.recurse worktree add ../project-3
631 test_expect_success '"add" with initialized submodule, with submodule.recurse unset' '
632 git -C project-clone submodule update --init &&
633 git -C project-clone worktree add ../project-4
636 test_expect_success '"add" with initialized submodule, with submodule.recurse set' '
637 git -C project-clone -c submodule.recurse worktree add ../project-5
640 test_done