Git fork
at reftables-rust 804 lines 20 kB view raw
1#!/bin/sh 2# 3# Copyright (c) 2006 Junio C Hamano 4# 5 6test_description='git checkout tests. 7 8Creates main, forks renamer and side branches from it. 9Test switching across them. 10 11 ! [main] Initial A one, A two 12 * [renamer] Renamer R one->uno, M two 13 ! [side] Side M one, D two, A three 14 ! [simple] Simple D one, M two 15 ---- 16 + [simple] Simple D one, M two 17 + [side] Side M one, D two, A three 18 * [renamer] Renamer R one->uno, M two 19 +*++ [main] Initial A one, A two 20 21' 22 23GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main 24export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 25 26. ./test-lib.sh 27 28test_tick 29 30fill () { 31 for i 32 do 33 echo "$i" 34 done 35} 36 37 38test_expect_success setup ' 39 fill x y z >same && 40 fill 1 2 3 4 5 6 7 8 >one && 41 fill a b c d e >two && 42 git add same one two && 43 git commit -m "Initial A one, A two" && 44 45 git checkout -b renamer && 46 rm -f one && 47 fill 1 3 4 5 6 7 8 >uno && 48 git add uno && 49 fill a b c d e f >two && 50 git commit -a -m "Renamer R one->uno, M two" && 51 52 git checkout -b side main && 53 fill 1 2 3 4 5 6 7 >one && 54 fill A B C D E >three && 55 rm -f two && 56 git update-index --add --remove one two three && 57 git commit -m "Side M one, D two, A three" && 58 59 git checkout -b simple main && 60 rm -f one && 61 fill a c e >two && 62 git commit -a -m "Simple D one, M two" && 63 64 git checkout main 65' 66 67test_expect_success 'checkout from non-existing branch' ' 68 git checkout -b delete-me main && 69 git update-ref -d --no-deref refs/heads/delete-me && 70 test refs/heads/delete-me = "$(git symbolic-ref HEAD)" && 71 git checkout main && 72 test refs/heads/main = "$(git symbolic-ref HEAD)" 73' 74 75test_expect_success 'checkout with dirty tree without -m' ' 76 fill 0 1 2 3 4 5 6 7 8 >one && 77 if git checkout side 78 then 79 echo Not happy 80 false 81 else 82 echo "happy - failed correctly" 83 fi 84' 85 86test_expect_success 'checkout with unrelated dirty tree without -m' ' 87 git checkout -f main && 88 fill 0 1 2 3 4 5 6 7 8 >same && 89 cp same kept && 90 git checkout side >messages && 91 test_cmp same kept && 92 printf "M\t%s\n" same >messages.expect && 93 test_cmp messages.expect messages 94' 95 96test_expect_success 'checkout -m with dirty tree' ' 97 git checkout -f main && 98 git clean -f && 99 100 fill 0 1 2 3 4 5 6 7 8 >one && 101 git checkout -m side >messages && 102 103 test "$(git symbolic-ref HEAD)" = "refs/heads/side" && 104 105 printf "M\t%s\n" one >expect.messages && 106 test_cmp expect.messages messages && 107 108 fill "M one" "A three" "D two" >expect.main && 109 git diff --name-status main >current.main && 110 test_cmp expect.main current.main && 111 112 fill "M one" >expect.side && 113 git diff --name-status side >current.side && 114 test_cmp expect.side current.side && 115 116 git diff --cached >current.index && 117 test_must_be_empty current.index 118' 119 120test_expect_success 'checkout -m with dirty tree, renamed' ' 121 git checkout -f main && git clean -f && 122 123 fill 1 2 3 4 5 7 8 >one && 124 if git checkout renamer 125 then 126 echo Not happy 127 false 128 else 129 echo "happy - failed correctly" 130 fi && 131 132 git checkout -m renamer && 133 fill 1 3 4 5 7 8 >expect && 134 test_cmp expect uno && 135 ! test -f one && 136 git diff --cached >current && 137 test_must_be_empty current 138' 139 140test_expect_success 'checkout -m with merge conflict' ' 141 git checkout -f main && git clean -f && 142 143 fill 1 T 3 4 5 6 S 8 >one && 144 if git checkout renamer 145 then 146 echo Not happy 147 false 148 else 149 echo "happy - failed correctly" 150 fi && 151 152 git checkout -m renamer && 153 154 git diff main:one :3:uno | 155 sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current && 156 fill d2 aT d7 aS >expect && 157 test_cmp expect current && 158 git diff --cached two >current && 159 test_must_be_empty current 160' 161 162test_expect_success 'format of merge conflict from checkout -m' ' 163 git checkout -f main && 164 git clean -f && 165 166 fill b d >two && 167 git checkout -m simple && 168 169 git ls-files >current && 170 fill same two two two >expect && 171 test_cmp expect current && 172 173 cat <<-EOF >expect && 174 <<<<<<< simple 175 a 176 c 177 e 178 ======= 179 b 180 d 181 >>>>>>> local 182 EOF 183 test_cmp expect two 184' 185 186test_expect_success 'checkout --merge --conflict=diff3 <branch>' ' 187 git checkout -f main && 188 git reset --hard && 189 git clean -f && 190 191 fill b d >two && 192 git checkout --merge --conflict=diff3 simple && 193 194 cat <<-EOF >expect && 195 <<<<<<< simple 196 a 197 c 198 e 199 ||||||| main 200 a 201 b 202 c 203 d 204 e 205 ======= 206 b 207 d 208 >>>>>>> local 209 EOF 210 test_cmp expect two 211' 212 213test_expect_success 'switch to another branch while carrying a deletion' ' 214 git checkout -f main && 215 git reset --hard && 216 git clean -f && 217 git rm two && 218 219 test_must_fail git checkout simple 2>errs && 220 test_grep overwritten errs && 221 222 test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs && 223 test_must_be_empty errs 224' 225 226test_expect_success 'checkout to detach HEAD (with advice declined)' ' 227 git config set advice.detachedHead false && 228 rev=$(git rev-parse --short renamer^) && 229 git checkout -f renamer && 230 git clean -f && 231 git checkout renamer^ 2>messages && 232 test_grep "HEAD is now at $rev" messages && 233 test_line_count = 1 messages && 234 H=$(git rev-parse --verify HEAD) && 235 M=$(git show-ref -s --verify refs/heads/main) && 236 test "z$H" = "z$M" && 237 if git symbolic-ref HEAD >/dev/null 2>&1 238 then 239 echo "OOPS, HEAD is still symbolic???" 240 false 241 else 242 : happy 243 fi 244' 245 246test_expect_success 'checkout to detach HEAD' ' 247 git config set advice.detachedHead true && 248 rev=$(git rev-parse --short renamer^) && 249 git checkout -f renamer && 250 git clean -f && 251 git checkout renamer^ 2>messages && 252 grep "HEAD is now at $rev" messages && 253 test_line_count -gt 1 messages && 254 H=$(git rev-parse --verify HEAD) && 255 M=$(git show-ref -s --verify refs/heads/main) && 256 test "z$H" = "z$M" && 257 if git symbolic-ref HEAD >/dev/null 2>&1 258 then 259 echo "OOPS, HEAD is still symbolic???" 260 false 261 else 262 : happy 263 fi 264' 265 266test_expect_success 'checkout to detach HEAD with branchname^' ' 267 git checkout -f main && 268 git clean -f && 269 git checkout renamer^ && 270 H=$(git rev-parse --verify HEAD) && 271 M=$(git show-ref -s --verify refs/heads/main) && 272 test "z$H" = "z$M" && 273 if git symbolic-ref HEAD >/dev/null 2>&1 274 then 275 echo "OOPS, HEAD is still symbolic???" 276 false 277 else 278 : happy 279 fi 280' 281 282test_expect_success 'checkout to detach HEAD with :/message' ' 283 git checkout -f main && 284 git clean -f && 285 git checkout ":/Initial" && 286 H=$(git rev-parse --verify HEAD) && 287 M=$(git show-ref -s --verify refs/heads/main) && 288 test "z$H" = "z$M" && 289 if git symbolic-ref HEAD >/dev/null 2>&1 290 then 291 echo "OOPS, HEAD is still symbolic???" 292 false 293 else 294 : happy 295 fi 296' 297 298test_expect_success 'checkout to detach HEAD with HEAD^0' ' 299 git checkout -f main && 300 git clean -f && 301 git checkout HEAD^0 && 302 H=$(git rev-parse --verify HEAD) && 303 M=$(git show-ref -s --verify refs/heads/main) && 304 test "z$H" = "z$M" && 305 if git symbolic-ref HEAD >/dev/null 2>&1 306 then 307 echo "OOPS, HEAD is still symbolic???" 308 false 309 else 310 : happy 311 fi 312' 313 314test_expect_success 'checkout with ambiguous tag/branch names' ' 315 git tag both side && 316 git branch both main && 317 git reset --hard && 318 git checkout main && 319 320 git checkout both && 321 H=$(git rev-parse --verify HEAD) && 322 M=$(git show-ref -s --verify refs/heads/main) && 323 test "z$H" = "z$M" && 324 name=$(git symbolic-ref HEAD 2>/dev/null) && 325 test "z$name" = zrefs/heads/both 326' 327 328test_expect_success 'checkout with ambiguous tag/branch names' ' 329 git reset --hard && 330 git checkout main && 331 332 git tag frotz side && 333 git branch frotz main && 334 git reset --hard && 335 git checkout main && 336 337 git checkout tags/frotz && 338 H=$(git rev-parse --verify HEAD) && 339 S=$(git show-ref -s --verify refs/heads/side) && 340 test "z$H" = "z$S" && 341 if name=$(git symbolic-ref HEAD 2>/dev/null) 342 then 343 echo "Bad -- should have detached" 344 false 345 else 346 : happy 347 fi 348' 349 350test_expect_success 'switch branches while in subdirectory' ' 351 git reset --hard && 352 git checkout main && 353 354 mkdir subs && 355 git -C subs checkout side && 356 ! test -f subs/one && 357 rm -fr subs 358' 359 360test_expect_success 'checkout specific path while in subdirectory' ' 361 git reset --hard && 362 git checkout side && 363 mkdir subs && 364 >subs/bero && 365 git add subs/bero && 366 git commit -m "add subs/bero" && 367 368 git checkout main && 369 mkdir -p subs && 370 git -C subs checkout side -- bero && 371 test -f subs/bero 372' 373 374test_expect_success 'checkout w/--track sets up tracking' ' 375 git config branch.autosetupmerge false && 376 git checkout main && 377 git checkout --track -b track1 && 378 test "$(git config branch.track1.remote)" && 379 test "$(git config branch.track1.merge)" 380' 381 382test_expect_success 'checkout w/autosetupmerge=always sets up tracking' ' 383 test_when_finished git config branch.autosetupmerge false && 384 git config branch.autosetupmerge always && 385 git checkout main && 386 git checkout -b track2 && 387 test "$(git config branch.track2.remote)" && 388 test "$(git config branch.track2.merge)" 389' 390 391test_expect_success 'checkout w/--track from non-branch HEAD fails' ' 392 git checkout main^0 && 393 test_must_fail git symbolic-ref HEAD && 394 test_must_fail git checkout --track -b track && 395 test_must_fail git rev-parse --verify track && 396 test_must_fail git symbolic-ref HEAD && 397 test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)" 398' 399 400test_expect_success 'checkout w/--track from tag fails' ' 401 git checkout main^0 && 402 test_must_fail git symbolic-ref HEAD && 403 test_must_fail git checkout --track -b track frotz && 404 test_must_fail git rev-parse --verify track && 405 test_must_fail git symbolic-ref HEAD && 406 test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)" 407' 408 409test_expect_success 'detach a symbolic link HEAD' ' 410 git checkout main && 411 git config --bool core.prefersymlinkrefs yes && 412 git checkout side && 413 git checkout main && 414 it=$(git symbolic-ref HEAD) && 415 test "z$it" = zrefs/heads/main && 416 here=$(git rev-parse --verify refs/heads/main) && 417 git checkout side^ && 418 test "z$(git rev-parse --verify refs/heads/main)" = "z$here" 419' 420 421test_expect_success 'checkout with --track fakes a sensible -b <name>' ' 422 git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" && 423 git update-ref refs/remotes/origin/koala/bear renamer && 424 425 git checkout --track origin/koala/bear && 426 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" && 427 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" && 428 429 git checkout main && git branch -D koala/bear && 430 431 git checkout --track refs/remotes/origin/koala/bear && 432 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" && 433 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" && 434 435 git checkout main && git branch -D koala/bear && 436 437 git checkout --track remotes/origin/koala/bear && 438 test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" && 439 test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" 440' 441 442test_expect_success 'checkout with --track, but without -b, fails with too short tracked name' ' 443 test_must_fail git checkout --track renamer 444' 445 446setup_conflicting_index () { 447 rm -f .git/index && 448 O=$(echo original | git hash-object -w --stdin) && 449 A=$(echo ourside | git hash-object -w --stdin) && 450 B=$(echo theirside | git hash-object -w --stdin) && 451 ( 452 echo "100644 $A 0 fild" && 453 echo "100644 $O 1 file" && 454 echo "100644 $A 2 file" && 455 echo "100644 $B 3 file" && 456 echo "100644 $A 0 filf" 457 ) | git update-index --index-info 458} 459 460test_expect_success 'checkout an unmerged path should fail' ' 461 setup_conflicting_index && 462 echo "none of the above" >sample && 463 cat sample >fild && 464 cat sample >file && 465 cat sample >filf && 466 test_must_fail git checkout fild file filf && 467 test_cmp sample fild && 468 test_cmp sample filf && 469 test_cmp sample file 470' 471 472test_expect_success 'checkout with an unmerged path can be ignored' ' 473 setup_conflicting_index && 474 echo "none of the above" >sample && 475 echo ourside >expect && 476 cat sample >fild && 477 cat sample >file && 478 cat sample >filf && 479 git checkout -f fild file filf && 480 test_cmp expect fild && 481 test_cmp expect filf && 482 test_cmp sample file 483' 484 485test_expect_success 'checkout unmerged stage' ' 486 setup_conflicting_index && 487 echo "none of the above" >sample && 488 echo ourside >expect && 489 cat sample >fild && 490 cat sample >file && 491 cat sample >filf && 492 git checkout --ours . && 493 test_cmp expect fild && 494 test_cmp expect filf && 495 test_cmp expect file && 496 git checkout --theirs file && 497 test ztheirside = "z$(cat file)" 498' 499 500test_expect_success 'checkout --ours is incompatible with switching' ' 501 test_must_fail git checkout --ours 2>error && 502 test_grep "needs the paths to check out" error && 503 504 test_must_fail git checkout --ours HEAD 2>error && 505 test_grep "cannot be used with switching" error && 506 507 test_must_fail git checkout --ours main 2>error && 508 test_grep "cannot be used with switching" error && 509 510 git checkout --ours file 511' 512 513test_expect_success 'checkout path with --merge from tree-ish is a no-no' ' 514 setup_conflicting_index && 515 test_must_fail git checkout -m HEAD -- file 516' 517 518test_expect_success 'checkout with --merge' ' 519 setup_conflicting_index && 520 echo "none of the above" >sample && 521 echo ourside >expect && 522 cat sample >fild && 523 cat sample >file && 524 cat sample >filf && 525 git checkout -m -- fild file filf && 526 ( 527 echo "<<<<<<< ours" && 528 echo ourside && 529 echo "=======" && 530 echo theirside && 531 echo ">>>>>>> theirs" 532 ) >merged && 533 test_cmp expect fild && 534 test_cmp expect filf && 535 test_cmp merged file 536' 537 538test_expect_success 'checkout -m works after (mistaken) resolution' ' 539 setup_conflicting_index && 540 echo "none of the above" >sample && 541 cat sample >fild && 542 cat sample >file && 543 cat sample >filf && 544 # resolve to something 545 git add file && 546 git checkout --merge -- fild file filf && 547 { 548 echo "<<<<<<< ours" && 549 echo ourside && 550 echo "=======" && 551 echo theirside && 552 echo ">>>>>>> theirs" 553 } >merged && 554 test_cmp expect fild && 555 test_cmp expect filf && 556 test_cmp merged file 557' 558 559test_expect_success 'checkout -m works after (mistaken) resolution to remove' ' 560 setup_conflicting_index && 561 echo "none of the above" >sample && 562 cat sample >fild && 563 cat sample >file && 564 cat sample >filf && 565 # resolve to remove 566 git rm file && 567 git checkout --merge -- fild file filf && 568 { 569 echo "<<<<<<< ours" && 570 echo ourside && 571 echo "=======" && 572 echo theirside && 573 echo ">>>>>>> theirs" 574 } >merged && 575 test_cmp expect fild && 576 test_cmp expect filf && 577 test_cmp merged file 578' 579 580test_expect_success 'checkout with --merge, in diff3 -m style' ' 581 git config merge.conflictstyle diff3 && 582 setup_conflicting_index && 583 echo "none of the above" >sample && 584 echo ourside >expect && 585 cat sample >fild && 586 cat sample >file && 587 cat sample >filf && 588 git checkout -m -- fild file filf && 589 ( 590 echo "<<<<<<< ours" && 591 echo ourside && 592 echo "||||||| base" && 593 echo original && 594 echo "=======" && 595 echo theirside && 596 echo ">>>>>>> theirs" 597 ) >merged && 598 test_cmp expect fild && 599 test_cmp expect filf && 600 test_cmp merged file 601' 602 603test_expect_success 'checkout --conflict=merge, overriding config' ' 604 git config merge.conflictstyle diff3 && 605 setup_conflicting_index && 606 echo "none of the above" >sample && 607 echo ourside >expect && 608 cat sample >fild && 609 cat sample >file && 610 cat sample >filf && 611 git checkout --conflict=merge -- fild file filf && 612 ( 613 echo "<<<<<<< ours" && 614 echo ourside && 615 echo "=======" && 616 echo theirside && 617 echo ">>>>>>> theirs" 618 ) >merged && 619 test_cmp expect fild && 620 test_cmp expect filf && 621 test_cmp merged file 622' 623 624test_expect_success 'checkout --conflict=diff3' ' 625 test_unconfig merge.conflictstyle && 626 setup_conflicting_index && 627 echo "none of the above" >sample && 628 echo ourside >expect && 629 cat sample >fild && 630 cat sample >file && 631 cat sample >filf && 632 git checkout --conflict=diff3 -- fild file filf && 633 ( 634 echo "<<<<<<< ours" && 635 echo ourside && 636 echo "||||||| base" && 637 echo original && 638 echo "=======" && 639 echo theirside && 640 echo ">>>>>>> theirs" 641 ) >merged && 642 test_cmp expect fild && 643 test_cmp expect filf && 644 test_cmp merged file 645' 646 647test_expect_success 'checkout --conflict=diff3 --no-conflict does not merge' ' 648 setup_conflicting_index && 649 echo "none of the above" >expect && 650 cat expect >fild && 651 cat expect >file && 652 test_must_fail git checkout --conflict=diff3 --no-conflict -- fild file 2>err && 653 test_cmp expect file && 654 test_cmp expect fild && 655 echo "error: path ${SQ}file${SQ} is unmerged" >expect && 656 test_cmp expect err 657' 658 659test_expect_success 'checkout --conflict=diff3 --no-merge does not merge' ' 660 setup_conflicting_index && 661 echo "none of the above" >expect && 662 cat expect >fild && 663 cat expect >file && 664 test_must_fail git checkout --conflict=diff3 --no-merge -- fild file 2>err && 665 test_cmp expect file && 666 test_cmp expect fild && 667 echo "error: path ${SQ}file${SQ} is unmerged" >expect && 668 test_cmp expect err 669' 670 671test_expect_success 'checkout --no-merge --conflict=diff3 does merge' ' 672 setup_conflicting_index && 673 echo "none of the above" >fild && 674 echo "none of the above" >file && 675 git checkout --no-merge --conflict=diff3 -- fild file && 676 echo "ourside" >expect && 677 test_cmp expect fild && 678 cat >expect <<-\EOF && 679 <<<<<<< ours 680 ourside 681 ||||||| base 682 original 683 ======= 684 theirside 685 >>>>>>> theirs 686 EOF 687 test_cmp expect file 688' 689 690test_expect_success 'checkout --merge --conflict=diff3 --no-conflict does merge' ' 691 setup_conflicting_index && 692 echo "none of the above" >fild && 693 echo "none of the above" >file && 694 git checkout --merge --conflict=diff3 --no-conflict -- fild file && 695 echo "ourside" >expect && 696 test_cmp expect fild && 697 cat >expect <<-\EOF && 698 <<<<<<< ours 699 ourside 700 ======= 701 theirside 702 >>>>>>> theirs 703 EOF 704 test_cmp expect file 705' 706 707test_expect_success 'checkout with invalid conflict style' ' 708 test_must_fail git checkout --conflict=bad 2>actual -- file && 709 echo "error: unknown conflict style ${SQ}bad${SQ}" >expect && 710 test_cmp expect actual 711' 712 713test_expect_success 'failing checkout -b should not break working tree' ' 714 git clean -fd && # Remove untracked files in the way 715 git reset --hard main && 716 git symbolic-ref HEAD refs/heads/main && 717 test_must_fail git checkout -b renamer side^ && 718 test $(git symbolic-ref HEAD) = refs/heads/main && 719 git diff --exit-code && 720 git diff --cached --exit-code 721' 722 723test_expect_success 'switch out of non-branch' ' 724 git reset --hard main && 725 git checkout main^0 && 726 echo modified >one && 727 test_must_fail git checkout renamer 2>error.log && 728 ! grep "^Previous HEAD" error.log 729' 730 731( 732 echo "#!$SHELL_PATH" 733 cat <<\EOF 734O=$1 A=$2 B=$3 735cat "$A" >.tmp 736exec >"$A" 737echo '<<<<<<< filfre-theirs' 738cat "$B" 739echo '||||||| filfre-common' 740cat "$O" 741echo '=======' 742cat ".tmp" 743echo '>>>>>>> filfre-ours' 744rm -f .tmp 745exit 1 746EOF 747) >filfre.sh 748chmod +x filfre.sh 749 750test_expect_success 'custom merge driver with checkout -m' ' 751 git reset --hard && 752 753 git config merge.filfre.driver "./filfre.sh %O %A %B" && 754 git config merge.filfre.name "Feel-free merge driver" && 755 git config merge.filfre.recursive binary && 756 echo "arm merge=filfre" >.gitattributes && 757 758 git checkout -b left && 759 echo neutral >arm && 760 git add arm .gitattributes && 761 test_tick && 762 git commit -m neutral && 763 git branch right && 764 765 echo left >arm && 766 test_tick && 767 git commit -a -m left && 768 git checkout right && 769 770 echo right >arm && 771 test_tick && 772 git commit -a -m right && 773 774 test_must_fail git merge left && 775 ( 776 for t in filfre-common left right 777 do 778 grep $t arm || exit 1 779 done 780 ) && 781 782 mv arm expect && 783 git checkout -m arm && 784 test_cmp expect arm 785' 786 787test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' 788 git reset --hard main && 789 # default config does not copy tracking info 790 git checkout -b foo-no-inherit koala/bear && 791 test_cmp_config "" --default "" branch.foo-no-inherit.remote && 792 test_cmp_config "" --default "" branch.foo-no-inherit.merge && 793 # with autoSetupMerge=inherit, we copy tracking info from koala/bear 794 test_config branch.autoSetupMerge inherit && 795 git checkout -b foo koala/bear && 796 test_cmp_config origin branch.foo.remote && 797 test_cmp_config refs/heads/koala/bear branch.foo.merge && 798 # no tracking info to inherit from main 799 git checkout -b main2 main && 800 test_cmp_config "" --default "" branch.main2.remote && 801 test_cmp_config "" --default "" branch.main2.merge 802' 803 804test_done