Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t2400-worktree-add.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t2400-worktree-add.sh` 1291 lines ยท 36732 bytes Raw Blame History 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 test_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 test_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 omitted' ' 284 git worktree add -b burble flornk && 285 test_cmp_rev HEAD burble 286 ' 287 288 test_expect_success '"add --detach" with 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 omitted' ' 297 git worktree add wiffle/bat && 298 test_cmp_rev HEAD bat 299 ' 300 301 test_expect_success '"add" with trailing slash and 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 " 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 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 test_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 test_grep ! "error: unknown switch" actual && 457 test_grep "hint: If you meant to create a worktree containing a new unborn branch" actual && 458 if [ $use_branch -eq 1 ] 459 then 460 test_grep -E "^hint: +git worktree add --orphan -b [^ ]+ [^ ]+$" actual 461 else 462 test_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 test_grep ! "error: unknown switch" actual && 478 test_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 test_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 test_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 test_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" 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" fails' ' 623 test_must_fail git worktree add foo non-existent 624 ' 625 626 test_expect_success '"add" 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" does not dwim with -b' ' 642 test_when_finished rm -rf repo_upstream repo_dwim wt && 643 setup_remote_repo repo_upstream repo_dwim && 644 ( 645 cd repo_dwim && 646 test_must_fail git worktree add -b branch ../wt foo 2>actual && 647 test_grep "^fatal: invalid reference: foo" actual 648 ) 649 ' 650 651 test_expect_success '"add" dwims with checkout.defaultRemote' ' 652 test_when_finished rm -rf repo_upstream repo_dwim foo && 653 setup_remote_repo repo_upstream repo_dwim && 654 ( 655 cd repo_dwim && 656 git remote add repo_upstream2 ../repo_upstream && 657 git fetch repo_upstream2 && 658 test_must_fail git worktree add ../foo foo 2>error.actual && 659 test_grep "matched multiple (2) remote tracking branches" error.actual && 660 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo && 661 git status -uno --porcelain >status.actual && 662 test_must_be_empty status.actual 663 ) && 664 ( 665 cd foo && 666 test_branch_upstream foo repo_upstream foo && 667 test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo 668 ) 669 ' 670 671 test_expect_success 'git worktree add does not match remote' ' 672 test_when_finished rm -rf repo_a repo_b foo && 673 setup_remote_repo repo_a repo_b && 674 ( 675 cd repo_b && 676 git worktree add ../foo 677 ) && 678 ( 679 cd foo && 680 test_must_fail git config "branch.foo.remote" && 681 test_must_fail git config "branch.foo.merge" && 682 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo 683 ) 684 ' 685 686 test_expect_success 'git worktree add --guess-remote sets up tracking' ' 687 test_when_finished rm -rf repo_a repo_b foo && 688 setup_remote_repo repo_a repo_b && 689 ( 690 cd repo_b && 691 git worktree add --guess-remote ../foo 692 ) && 693 ( 694 cd foo && 695 test_branch_upstream foo repo_a foo && 696 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo 697 ) 698 ' 699 700 test_expect_success 'git worktree add --guess-remote fails if there are multiple matches' ' 701 test_when_finished rm -rf repo_a repo_b foo && 702 setup_remote_repo repo_a repo_b && 703 ( 704 cd repo_b && 705 git remote add repo_a2 ../repo_a && 706 git fetch repo_a2 && 707 test_must_fail git worktree add --guess-remote ../foo 2>actual && 708 test_grep "matched multiple (2) remote tracking branches" actual 709 ) 710 ' 711 712 test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' ' 713 test_when_finished rm -rf repo_a repo_b foo && 714 setup_remote_repo repo_a repo_b && 715 ( 716 cd repo_b && 717 git worktree add --quiet --guess-remote ../foo 2>actual && 718 test_must_be_empty actual 719 ) && 720 ( 721 cd foo && 722 test_branch_upstream foo repo_a foo && 723 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo 724 ) 725 ' 726 727 test_expect_success 'git worktree --no-guess-remote (quiet)' ' 728 test_when_finished rm -rf repo_a repo_b foo && 729 setup_remote_repo repo_a repo_b && 730 ( 731 cd repo_b && 732 git worktree add --quiet --no-guess-remote ../foo 733 ) && 734 ( 735 cd foo && 736 test_must_fail git config "branch.foo.remote" && 737 test_must_fail git config "branch.foo.merge" && 738 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo 739 ) 740 ' 741 742 test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' ' 743 test_when_finished rm -rf repo_a repo_b foo && 744 setup_remote_repo repo_a repo_b && 745 ( 746 cd repo_b && 747 git config worktree.guessRemote true && 748 git worktree add ../foo 749 ) && 750 ( 751 cd foo && 752 test_branch_upstream foo repo_a foo && 753 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo 754 ) 755 ' 756 757 test_expect_success 'git worktree --no-guess-remote option overrides config' ' 758 test_when_finished rm -rf repo_a repo_b foo && 759 setup_remote_repo repo_a repo_b && 760 ( 761 cd repo_b && 762 git config worktree.guessRemote true && 763 git worktree add --no-guess-remote ../foo 764 ) && 765 ( 766 cd foo && 767 test_must_fail git config "branch.foo.remote" && 768 test_must_fail git config "branch.foo.merge" && 769 test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo 770 ) 771 ' 772 773 test_dwim_orphan () { 774 local info_text="No possible source branch, inferring '--orphan'" && 775 local fetch_error_text="fatal: No local or remote refs exist despite at least one remote" && 776 local orphan_hint="hint: If you meant to create a worktree containing a new unborn branch" && 777 local invalid_ref_regex="^fatal: invalid reference: " && 778 local bad_combo_regex="^fatal: options '[-a-z]*' and '[-a-z]*' cannot be used together" && 779 780 local git_ns="repo" && 781 local dashc_args="-C $git_ns" && 782 local use_cd=0 && 783 784 local bad_head=0 && 785 local empty_repo=1 && 786 local local_ref=0 && 787 local use_quiet=0 && 788 local remote=0 && 789 local remote_ref=0 && 790 local use_detach=0 && 791 local use_new_branch=0 && 792 793 local outcome="$1" && 794 local outcome_text && 795 local success && 796 shift && 797 local args="" && 798 local context="" && 799 case "$outcome" in 800 "infer") 801 success=1 && 802 outcome_text='"add" DWIM infer --orphan' 803 ;; 804 "no_infer") 805 success=1 && 806 outcome_text='"add" DWIM doesnt infer --orphan' 807 ;; 808 "fetch_error") 809 success=0 && 810 outcome_text='"add" error need fetch' 811 ;; 812 "fatal_orphan_bad_combo") 813 success=0 && 814 outcome_text='"add" error inferred "--orphan" gives illegal opts combo' 815 ;; 816 "warn_bad_head") 817 success=0 && 818 outcome_text='"add" error, warn on bad HEAD, hint use orphan' 819 ;; 820 *) 821 echo "test_dwim_orphan(): invalid outcome: '$outcome'" >&2 && 822 return 1 823 ;; 824 esac && 825 while [ $# -gt 0 ] 826 do 827 case "$1" in 828 # How and from where to create the worktree 829 "-C_repo") 830 use_cd=0 && 831 git_ns="repo" && 832 dashc_args="-C $git_ns" && 833 context="$context, 'git -C repo'" 834 ;; 835 "-C_wt") 836 use_cd=0 && 837 git_ns="wt" && 838 dashc_args="-C $git_ns" && 839 context="$context, 'git -C wt'" 840 ;; 841 "cd_repo") 842 use_cd=1 && 843 git_ns="repo" && 844 dashc_args="" && 845 context="$context, 'cd repo && git'" 846 ;; 847 "cd_wt") 848 use_cd=1 && 849 git_ns="wt" && 850 dashc_args="" && 851 context="$context, 'cd wt && git'" 852 ;; 853 854 # Bypass the "pull first" warning 855 "force") 856 args="$args --force" && 857 context="$context, --force" 858 ;; 859 860 # Try to use remote refs when DWIM 861 "guess_remote") 862 args="$args --guess-remote" && 863 context="$context, --guess-remote" 864 ;; 865 "no_guess_remote") 866 args="$args --no-guess-remote" && 867 context="$context, --no-guess-remote" 868 ;; 869 870 # Whether there is at least one local branch present 871 "local_ref") 872 empty_repo=0 && 873 local_ref=1 && 874 context="$context, >=1 local branches" 875 ;; 876 "no_local_ref") 877 empty_repo=0 && 878 context="$context, 0 local branches" 879 ;; 880 881 # Whether the HEAD points at a valid ref (skip this opt when no refs) 882 "good_head") 883 # requires: local_ref 884 context="$context, valid HEAD" 885 ;; 886 "bad_head") 887 bad_head=1 && 888 context="$context, invalid (or orphan) HEAD" 889 ;; 890 891 # Whether the code path is tested with the base add command, -b, or --detach 892 "no_-b") 893 use_new_branch=0 && 894 context="$context, no --branch" 895 ;; 896 "-b") 897 use_new_branch=1 && 898 context="$context, --branch" 899 ;; 900 "detach") 901 use_detach=1 && 902 context="$context, --detach" 903 ;; 904 905 # Whether to check that all output is suppressed (except errors) 906 # or that the output is as expected 907 "quiet") 908 use_quiet=1 && 909 args="$args --quiet" && 910 context="$context, --quiet" 911 ;; 912 "no_quiet") 913 use_quiet=0 && 914 context="$context, no --quiet (expect output)" 915 ;; 916 917 # Whether there is at least one remote attached to the repo 918 "remote") 919 empty_repo=0 && 920 remote=1 && 921 context="$context, >=1 remotes" 922 ;; 923 "no_remote") 924 empty_repo=0 && 925 remote=0 && 926 context="$context, 0 remotes" 927 ;; 928 929 # Whether there is at least one valid remote ref 930 "remote_ref") 931 # requires: remote 932 empty_repo=0 && 933 remote_ref=1 && 934 context="$context, >=1 fetched remote branches" 935 ;; 936 "no_remote_ref") 937 empty_repo=0 && 938 remote_ref=0 && 939 context="$context, 0 fetched remote branches" 940 ;; 941 942 # Options or flags that become illegal when --orphan is inferred 943 "no_checkout") 944 args="$args --no-checkout" && 945 context="$context, --no-checkout" 946 ;; 947 "track") 948 args="$args --track" && 949 context="$context, --track" 950 ;; 951 952 # All other options are illegal 953 *) 954 echo "test_dwim_orphan(): invalid arg: '$1'" >&2 && 955 return 1 956 ;; 957 esac && 958 shift 959 done && 960 context="${context#', '}" && 961 if [ $use_new_branch -eq 1 ] 962 then 963 args="$args -b foo" 964 elif [ $use_detach -eq 1 ] 965 then 966 args="$args --detach" 967 else 968 context="DWIM (no --branch), $context" 969 fi && 970 if [ $empty_repo -eq 1 ] 971 then 972 context="empty repo, $context" 973 fi && 974 args="$args ../foo" && 975 context="${context%', '}" && 976 test_expect_success "$outcome_text w/ $context" ' 977 test_when_finished "rm -rf repo" && 978 git init repo && 979 if [ $local_ref -eq 1 ] && [ "$git_ns" = "repo" ] 980 then 981 (cd repo && test_commit commit) && 982 if [ $bad_head -eq 1 ] 983 then 984 git -C repo symbolic-ref HEAD refs/heads/badbranch 985 fi 986 elif [ $local_ref -eq 1 ] && [ "$git_ns" = "wt" ] 987 then 988 test_when_finished "git -C repo worktree remove -f ../wt" && 989 git -C repo worktree add --orphan -b main ../wt && 990 (cd wt && test_commit commit) && 991 if [ $bad_head -eq 1 ] 992 then 993 git -C wt symbolic-ref HEAD refs/heads/badbranch 994 fi 995 elif [ $local_ref -eq 0 ] && [ "$git_ns" = "wt" ] 996 then 997 test_when_finished "git -C repo worktree remove -f ../wt" && 998 git -C repo worktree add --orphan -b orphanbranch ../wt 999 fi && 1000 1001 if [ $remote -eq 1 ] 1002 then 1003 test_when_finished "rm -rf upstream" && 1004 git init upstream && 1005 (cd upstream && test_commit commit) && 1006 git -C upstream switch -c foo && 1007 git -C repo remote add upstream ../upstream 1008 fi && 1009 1010 if [ $remote_ref -eq 1 ] 1011 then 1012 git -C repo fetch 1013 fi && 1014 if [ $success -eq 1 ] 1015 then 1016 test_when_finished git -C repo worktree remove ../foo 1017 fi && 1018 ( 1019 if [ $use_cd -eq 1 ] 1020 then 1021 cd $git_ns 1022 fi && 1023 if [ "$outcome" = "infer" ] 1024 then 1025 git $dashc_args worktree add $args 2>actual && 1026 if [ $use_quiet -eq 1 ] 1027 then 1028 test_must_be_empty actual 1029 else 1030 test_grep "$info_text" actual 1031 fi 1032 elif [ "$outcome" = "no_infer" ] 1033 then 1034 git $dashc_args worktree add $args 2>actual && 1035 if [ $use_quiet -eq 1 ] 1036 then 1037 test_must_be_empty actual 1038 else 1039 test_grep ! "$info_text" actual 1040 fi 1041 elif [ "$outcome" = "fetch_error" ] 1042 then 1043 test_must_fail git $dashc_args worktree add $args 2>actual && 1044 test_grep "$fetch_error_text" actual 1045 elif [ "$outcome" = "fatal_orphan_bad_combo" ] 1046 then 1047 test_must_fail git $dashc_args worktree add $args 2>actual && 1048 if [ $use_quiet -eq 1 ] 1049 then 1050 test_grep ! "$info_text" actual 1051 else 1052 test_grep "$info_text" actual 1053 fi && 1054 test_grep "$bad_combo_regex" actual 1055 elif [ "$outcome" = "warn_bad_head" ] 1056 then 1057 test_must_fail git $dashc_args worktree add $args 2>actual && 1058 if [ $use_quiet -eq 1 ] 1059 then 1060 test_grep "$invalid_ref_regex" actual && 1061 test_grep ! "$orphan_hint" actual 1062 else 1063 test_grep "HEAD points to an invalid (or orphaned) reference" actual && 1064 test_grep "$orphan_hint" actual && 1065 test_grep ! "$info_text" actual 1066 fi && 1067 test_grep "$invalid_ref_regex" actual 1068 else 1069 # Unreachable 1070 false 1071 fi 1072 ) && 1073 if [ $success -ne 1 ] 1074 then 1075 test_path_is_missing foo 1076 fi 1077 ' 1078 } 1079 1080 for quiet_mode in "no_quiet" "quiet" 1081 do 1082 for changedir_type in "cd_repo" "cd_wt" "-C_repo" "-C_wt" 1083 do 1084 dwim_test_args="$quiet_mode $changedir_type" 1085 test_dwim_orphan 'infer' $dwim_test_args no_-b 1086 test_dwim_orphan 'no_infer' $dwim_test_args no_-b local_ref good_head 1087 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref no_remote no_remote_ref no_guess_remote 1088 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref no_guess_remote 1089 test_dwim_orphan 'fetch_error' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote 1090 test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote force 1091 test_dwim_orphan 'no_infer' $dwim_test_args no_-b no_local_ref remote remote_ref guess_remote 1092 1093 test_dwim_orphan 'infer' $dwim_test_args -b 1094 test_dwim_orphan 'no_infer' $dwim_test_args -b local_ref good_head 1095 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref no_remote no_remote_ref no_guess_remote 1096 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref no_guess_remote 1097 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref guess_remote 1098 test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote remote_ref guess_remote 1099 1100 test_dwim_orphan 'warn_bad_head' $dwim_test_args no_-b local_ref bad_head 1101 test_dwim_orphan 'warn_bad_head' $dwim_test_args -b local_ref bad_head 1102 test_dwim_orphan 'warn_bad_head' $dwim_test_args detach local_ref bad_head 1103 done 1104 1105 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b no_checkout 1106 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b track 1107 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b no_checkout 1108 test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b track 1109 done 1110 1111 post_checkout_hook () { 1112 test_when_finished "rm -rf .git/hooks" && 1113 mkdir .git/hooks && 1114 test_hook -C "$1" post-checkout <<-\EOF 1115 { 1116 echo $* 1117 git rev-parse --git-dir --show-toplevel 1118 } >hook.actual 1119 EOF 1120 } 1121 1122 test_expect_success '"add" invokes post-checkout hook (branch)' ' 1123 post_checkout_hook && 1124 { 1125 echo $ZERO_OID $(git rev-parse HEAD) 1 && 1126 echo $(pwd)/.git/worktrees/gumby && 1127 echo $(pwd)/gumby 1128 } >hook.expect && 1129 git worktree add gumby && 1130 test_cmp hook.expect gumby/hook.actual 1131 ' 1132 1133 test_expect_success '"add" invokes post-checkout hook (detached)' ' 1134 post_checkout_hook && 1135 { 1136 echo $ZERO_OID $(git rev-parse HEAD) 1 && 1137 echo $(pwd)/.git/worktrees/grumpy && 1138 echo $(pwd)/grumpy 1139 } >hook.expect && 1140 git worktree add --detach grumpy && 1141 test_cmp hook.expect grumpy/hook.actual 1142 ' 1143 1144 test_expect_success '"add --no-checkout" suppresses post-checkout hook' ' 1145 post_checkout_hook && 1146 rm -f hook.actual && 1147 git worktree add --no-checkout gloopy && 1148 test_path_is_missing gloopy/hook.actual 1149 ' 1150 1151 test_expect_success '"add" in other worktree invokes post-checkout hook' ' 1152 post_checkout_hook && 1153 { 1154 echo $ZERO_OID $(git rev-parse HEAD) 1 && 1155 echo $(pwd)/.git/worktrees/guppy && 1156 echo $(pwd)/guppy 1157 } >hook.expect && 1158 git -C gloopy worktree add --detach ../guppy && 1159 test_cmp hook.expect guppy/hook.actual 1160 ' 1161 1162 test_expect_success '"add" in bare repo invokes post-checkout hook' ' 1163 rm -rf bare && 1164 git clone --bare . bare && 1165 { 1166 echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 && 1167 echo $(pwd)/bare/worktrees/goozy && 1168 echo $(pwd)/goozy 1169 } >hook.expect && 1170 post_checkout_hook bare && 1171 git -C bare worktree add --detach ../goozy && 1172 test_cmp hook.expect goozy/hook.actual 1173 ' 1174 1175 test_expect_success '"add" an existing but missing worktree' ' 1176 git worktree add --detach pneu && 1177 test_must_fail git worktree add --detach pneu && 1178 rm -fr pneu && 1179 test_must_fail git worktree add --detach pneu && 1180 git worktree add --force --detach pneu 1181 ' 1182 1183 test_expect_success '"add" an existing locked but missing worktree' ' 1184 git worktree add --detach gnoo && 1185 git worktree lock gnoo && 1186 test_when_finished "git worktree unlock gnoo || :" && 1187 rm -fr gnoo && 1188 test_must_fail git worktree add --detach gnoo && 1189 test_must_fail git worktree add --force --detach gnoo && 1190 git worktree add --force --force --detach gnoo 1191 ' 1192 1193 test_expect_success '"add" not tripped up by magic worktree matching"' ' 1194 # if worktree "sub1/bar" exists, "git worktree add bar" in distinct 1195 # directory `sub2` should not mistakenly complain that `bar` is an 1196 # already-registered worktree 1197 mkdir sub1 sub2 && 1198 git -C sub1 --git-dir=../.git worktree add --detach bozo && 1199 git -C sub2 --git-dir=../.git worktree add --detach bozo 1200 ' 1201 1202 test_expect_success FUNNYNAMES 'sanitize generated worktree name' ' 1203 git worktree add --detach ". weird*..?.lock.lock" && 1204 test_path_is_dir .git/worktrees/---weird-.- 1205 ' 1206 1207 test_expect_success '"add" should not fail because of another bad worktree' ' 1208 git init add-fail && 1209 ( 1210 cd add-fail && 1211 test_commit first && 1212 mkdir sub && 1213 git worktree add sub/to-be-deleted && 1214 rm -rf sub && 1215 git worktree add second 1216 ) 1217 ' 1218 1219 test_expect_success '"add" with uninitialized submodule, with submodule.recurse unset' ' 1220 test_config_global protocol.file.allow always && 1221 test_create_repo submodule && 1222 test_commit -C submodule first && 1223 test_create_repo project && 1224 git -C project submodule add ../submodule && 1225 git -C project add submodule && 1226 test_tick && 1227 git -C project commit -m add_sub && 1228 git clone project project-clone && 1229 git -C project-clone worktree add ../project-2 1230 ' 1231 test_expect_success '"add" with uninitialized submodule, with submodule.recurse set' ' 1232 git -C project-clone -c submodule.recurse worktree add ../project-3 1233 ' 1234 1235 test_expect_success '"add" with initialized submodule, with submodule.recurse unset' ' 1236 test_config_global protocol.file.allow always && 1237 git -C project-clone submodule update --init && 1238 git -C project-clone worktree add ../project-4 1239 ' 1240 1241 test_expect_success '"add" with initialized submodule, with submodule.recurse set' ' 1242 git -C project-clone -c submodule.recurse worktree add ../project-5 1243 ' 1244 1245 test_expect_success 'can create worktrees with relative paths' ' 1246 test_when_finished "git worktree remove relative" && 1247 test_config worktree.useRelativePaths false && 1248 git worktree add --relative-paths ./relative && 1249 echo "gitdir: ../.git/worktrees/relative" >expect && 1250 test_cmp expect relative/.git && 1251 echo "../../../relative/.git" >expect && 1252 test_cmp expect .git/worktrees/relative/gitdir 1253 ' 1254 1255 test_expect_success 'can create worktrees with absolute paths' ' 1256 test_config worktree.useRelativePaths true && 1257 git worktree add ./relative && 1258 echo "gitdir: ../.git/worktrees/relative" >expect && 1259 test_cmp expect relative/.git && 1260 git worktree add --no-relative-paths ./absolute && 1261 echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect && 1262 test_cmp expect absolute/.git && 1263 echo "$(pwd)/absolute/.git" >expect && 1264 test_cmp expect .git/worktrees/absolute/gitdir 1265 ' 1266 1267 test_expect_success 'move repo without breaking relative internal links' ' 1268 test_when_finished rm -rf repo moved && 1269 git init repo && 1270 ( 1271 cd repo && 1272 test_commit initial && 1273 git worktree add --relative-paths wt1 && 1274 cd .. && 1275 mv repo moved && 1276 cd moved/wt1 && 1277 git worktree list >out 2>err && 1278 test_must_be_empty err 1279 ) 1280 ' 1281 1282 test_expect_success 'relative worktree sets extension config' ' 1283 test_when_finished "rm -rf repo" && 1284 git init repo && 1285 git -C repo commit --allow-empty -m base && 1286 git -C repo worktree add --relative-paths ./foo && 1287 test_cmp_config -C repo 1 core.repositoryformatversion && 1288 test_cmp_config -C repo true extensions.relativeworktrees 1289 ' 1290 1291 test_done