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