t / t3404-rebase-interactive.shon commit fetch: allow explicit --refmap to override configuration (c5558f8)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Johannes E. Schindelin
   4#
   5
   6test_description='git rebase interactive
   7
   8This test runs git rebase "interactively", by faking an edit, and verifies
   9that the result still makes sense.
  10
  11Initial setup:
  12
  13     one - two - three - four (conflict-branch)
  14   /
  15 A - B - C - D - E            (master)
  16 | \
  17 |   F - G - H                (branch1)
  18 |     \
  19 |\      I                    (branch2)
  20 | \
  21 |   J - K - L - M            (no-conflict-branch)
  22  \
  23    N - O - P                 (no-ff-branch)
  24
  25 where A, B, D and G all touch file1, and one, two, three, four all
  26 touch file "conflict".
  27'
  28. ./test-lib.sh
  29
  30. "$TEST_DIRECTORY"/lib-rebase.sh
  31
  32# WARNING: Modifications to the initial repository can change the SHA ID used
  33# in the expect2 file for the 'stop on conflicting pick' test.
  34
  35test_expect_success 'setup' '
  36        test_commit A file1 &&
  37        test_commit B file1 &&
  38        test_commit C file2 &&
  39        test_commit D file1 &&
  40        test_commit E file3 &&
  41        git checkout -b branch1 A &&
  42        test_commit F file4 &&
  43        test_commit G file1 &&
  44        test_commit H file5 &&
  45        git checkout -b branch2 F &&
  46        test_commit I file6 &&
  47        git checkout -b conflict-branch A &&
  48        test_commit one conflict &&
  49        test_commit two conflict &&
  50        test_commit three conflict &&
  51        test_commit four conflict &&
  52        git checkout -b no-conflict-branch A &&
  53        test_commit J fileJ &&
  54        test_commit K fileK &&
  55        test_commit L fileL &&
  56        test_commit M fileM &&
  57        git checkout -b no-ff-branch A &&
  58        test_commit N fileN &&
  59        test_commit O fileO &&
  60        test_commit P fileP
  61'
  62
  63# "exec" commands are ran with the user shell by default, but this may
  64# be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
  65# to create a file. Unseting SHELL avoids such non-portable behavior
  66# in tests. It must be exported for it to take effect where needed.
  67SHELL=
  68export SHELL
  69
  70test_expect_success 'rebase -i with the exec command' '
  71        git checkout master &&
  72        (
  73        set_fake_editor &&
  74        FAKE_LINES="1 exec_>touch-one
  75                2 exec_>touch-two exec_false exec_>touch-three
  76                3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
  77        export FAKE_LINES &&
  78        test_must_fail git rebase -i A
  79        ) &&
  80        test_path_is_file touch-one &&
  81        test_path_is_file touch-two &&
  82        test_path_is_missing touch-three " (should have stopped before)" &&
  83        test_cmp_rev C HEAD &&
  84        git rebase --continue &&
  85        test_path_is_file touch-three &&
  86        test_path_is_file "touch-file  name with spaces" &&
  87        test_path_is_file touch-after-semicolon &&
  88        test_cmp_rev master HEAD &&
  89        rm -f touch-*
  90'
  91
  92test_expect_success 'rebase -i with the exec command runs from tree root' '
  93        git checkout master &&
  94        mkdir subdir && (cd subdir &&
  95        set_fake_editor &&
  96        FAKE_LINES="1 exec_>touch-subdir" \
  97                git rebase -i HEAD^
  98        ) &&
  99        test_path_is_file touch-subdir &&
 100        rm -fr subdir
 101'
 102
 103test_expect_success 'rebase -i with the exec command checks tree cleanness' '
 104        git checkout master &&
 105        set_fake_editor &&
 106        test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
 107        test_cmp_rev master^ HEAD &&
 108        git reset --hard &&
 109        git rebase --continue
 110'
 111
 112test_expect_success 'rebase -i with exec of inexistent command' '
 113        git checkout master &&
 114        test_when_finished "git rebase --abort" &&
 115        set_fake_editor &&
 116        test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
 117        git rebase -i HEAD^ >actual 2>&1 &&
 118        ! grep "Maybe git-rebase is broken" actual
 119'
 120
 121test_expect_success 'no changes are a nop' '
 122        git checkout branch2 &&
 123        set_fake_editor &&
 124        git rebase -i F &&
 125        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
 126        test $(git rev-parse I) = $(git rev-parse HEAD)
 127'
 128
 129test_expect_success 'test the [branch] option' '
 130        git checkout -b dead-end &&
 131        git rm file6 &&
 132        git commit -m "stop here" &&
 133        set_fake_editor &&
 134        git rebase -i F branch2 &&
 135        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
 136        test $(git rev-parse I) = $(git rev-parse branch2) &&
 137        test $(git rev-parse I) = $(git rev-parse HEAD)
 138'
 139
 140test_expect_success 'test --onto <branch>' '
 141        git checkout -b test-onto branch2 &&
 142        set_fake_editor &&
 143        git rebase -i --onto branch1 F &&
 144        test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
 145        test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
 146        test $(git rev-parse I) = $(git rev-parse branch2)
 147'
 148
 149test_expect_success 'rebase on top of a non-conflicting commit' '
 150        git checkout branch1 &&
 151        git tag original-branch1 &&
 152        set_fake_editor &&
 153        git rebase -i branch2 &&
 154        test file6 = $(git diff --name-only original-branch1) &&
 155        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
 156        test $(git rev-parse I) = $(git rev-parse branch2) &&
 157        test $(git rev-parse I) = $(git rev-parse HEAD~2)
 158'
 159
 160test_expect_success 'reflog for the branch shows state before rebase' '
 161        test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
 162'
 163
 164test_expect_success 'exchange two commits' '
 165        set_fake_editor &&
 166        FAKE_LINES="2 1" git rebase -i HEAD~2 &&
 167        test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
 168        test G = $(git cat-file commit HEAD | sed -ne \$p)
 169'
 170
 171cat > expect << EOF
 172diff --git a/file1 b/file1
 173index f70f10e..fd79235 100644
 174--- a/file1
 175+++ b/file1
 176@@ -1 +1 @@
 177-A
 178+G
 179EOF
 180
 181cat > expect2 << EOF
 182<<<<<<< HEAD
 183D
 184=======
 185G
 186>>>>>>> 5d18e54... G
 187EOF
 188
 189test_expect_success 'stop on conflicting pick' '
 190        git tag new-branch1 &&
 191        set_fake_editor &&
 192        test_must_fail git rebase -i master &&
 193        test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
 194        test_cmp expect .git/rebase-merge/patch &&
 195        test_cmp expect2 file1 &&
 196        test "$(git diff --name-status |
 197                sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
 198        test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
 199        test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
 200'
 201
 202test_expect_success 'abort' '
 203        git rebase --abort &&
 204        test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
 205        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
 206        test_path_is_missing .git/rebase-merge
 207'
 208
 209test_expect_success 'abort with error when new base cannot be checked out' '
 210        git rm --cached file1 &&
 211        git commit -m "remove file in base" &&
 212        set_fake_editor &&
 213        test_must_fail git rebase -i master > output 2>&1 &&
 214        grep "The following untracked working tree files would be overwritten by checkout:" \
 215                output &&
 216        grep "file1" output &&
 217        test_path_is_missing .git/rebase-merge &&
 218        git reset --hard HEAD^
 219'
 220
 221test_expect_success 'retain authorship' '
 222        echo A > file7 &&
 223        git add file7 &&
 224        test_tick &&
 225        GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
 226        git tag twerp &&
 227        set_fake_editor &&
 228        git rebase -i --onto master HEAD^ &&
 229        git show HEAD | grep "^Author: Twerp Snog"
 230'
 231
 232test_expect_success 'squash' '
 233        git reset --hard twerp &&
 234        echo B > file7 &&
 235        test_tick &&
 236        GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
 237        echo "******************************" &&
 238        set_fake_editor &&
 239        FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
 240                git rebase -i --onto master HEAD~2 &&
 241        test B = $(cat file7) &&
 242        test $(git rev-parse HEAD^) = $(git rev-parse master)
 243'
 244
 245test_expect_success 'retain authorship when squashing' '
 246        git show HEAD | grep "^Author: Twerp Snog"
 247'
 248
 249test_expect_success '-p handles "no changes" gracefully' '
 250        HEAD=$(git rev-parse HEAD) &&
 251        set_fake_editor &&
 252        git rebase -i -p HEAD^ &&
 253        git update-index --refresh &&
 254        git diff-files --quiet &&
 255        git diff-index --quiet --cached HEAD -- &&
 256        test $HEAD = $(git rev-parse HEAD)
 257'
 258
 259test_expect_failure 'exchange two commits with -p' '
 260        git checkout H &&
 261        set_fake_editor &&
 262        FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
 263        test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
 264        test G = $(git cat-file commit HEAD | sed -ne \$p)
 265'
 266
 267test_expect_success 'preserve merges with -p' '
 268        git checkout -b to-be-preserved master^ &&
 269        : > unrelated-file &&
 270        git add unrelated-file &&
 271        test_tick &&
 272        git commit -m "unrelated" &&
 273        git checkout -b another-branch master &&
 274        echo B > file1 &&
 275        test_tick &&
 276        git commit -m J file1 &&
 277        test_tick &&
 278        git merge to-be-preserved &&
 279        echo C > file1 &&
 280        test_tick &&
 281        git commit -m K file1 &&
 282        echo D > file1 &&
 283        test_tick &&
 284        git commit -m L1 file1 &&
 285        git checkout HEAD^ &&
 286        echo 1 > unrelated-file &&
 287        test_tick &&
 288        git commit -m L2 unrelated-file &&
 289        test_tick &&
 290        git merge another-branch &&
 291        echo E > file1 &&
 292        test_tick &&
 293        git commit -m M file1 &&
 294        git checkout -b to-be-rebased &&
 295        test_tick &&
 296        set_fake_editor &&
 297        git rebase -i -p --onto branch1 master &&
 298        git update-index --refresh &&
 299        git diff-files --quiet &&
 300        git diff-index --quiet --cached HEAD -- &&
 301        test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
 302        test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
 303        test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
 304        test $(git show HEAD~5:file1) = B &&
 305        test $(git show HEAD~3:file1) = C &&
 306        test $(git show HEAD:file1) = E &&
 307        test $(git show HEAD:unrelated-file) = 1
 308'
 309
 310test_expect_success 'edit ancestor with -p' '
 311        set_fake_editor &&
 312        FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
 313        echo 2 > unrelated-file &&
 314        test_tick &&
 315        git commit -m L2-modified --amend unrelated-file &&
 316        git rebase --continue &&
 317        git update-index --refresh &&
 318        git diff-files --quiet &&
 319        git diff-index --quiet --cached HEAD -- &&
 320        test $(git show HEAD:unrelated-file) = 2
 321'
 322
 323test_expect_success '--continue tries to commit' '
 324        test_tick &&
 325        set_fake_editor &&
 326        test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
 327        echo resolved > file1 &&
 328        git add file1 &&
 329        FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
 330        test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
 331        git show HEAD | grep chouette
 332'
 333
 334test_expect_success 'verbose flag is heeded, even after --continue' '
 335        git reset --hard master@{1} &&
 336        test_tick &&
 337        set_fake_editor &&
 338        test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
 339        echo resolved > file1 &&
 340        git add file1 &&
 341        git rebase --continue > output &&
 342        grep "^ file1 | 2 +-$" output
 343'
 344
 345test_expect_success 'multi-squash only fires up editor once' '
 346        base=$(git rev-parse HEAD~4) &&
 347        set_fake_editor &&
 348        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
 349                EXPECT_HEADER_COUNT=4 \
 350                git rebase -i $base &&
 351        test $base = $(git rev-parse HEAD^) &&
 352        test 1 = $(git show | grep ONCE | wc -l)
 353'
 354
 355test_expect_success 'multi-fixup does not fire up editor' '
 356        git checkout -b multi-fixup E &&
 357        base=$(git rev-parse HEAD~4) &&
 358        set_fake_editor &&
 359        FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
 360                git rebase -i $base &&
 361        test $base = $(git rev-parse HEAD^) &&
 362        test 0 = $(git show | grep NEVER | wc -l) &&
 363        git checkout to-be-rebased &&
 364        git branch -D multi-fixup
 365'
 366
 367test_expect_success 'commit message used after conflict' '
 368        git checkout -b conflict-fixup conflict-branch &&
 369        base=$(git rev-parse HEAD~4) &&
 370        set_fake_editor &&
 371        test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
 372        echo three > conflict &&
 373        git add conflict &&
 374        FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
 375                git rebase --continue &&
 376        test $base = $(git rev-parse HEAD^) &&
 377        test 1 = $(git show | grep ONCE | wc -l) &&
 378        git checkout to-be-rebased &&
 379        git branch -D conflict-fixup
 380'
 381
 382test_expect_success 'commit message retained after conflict' '
 383        git checkout -b conflict-squash conflict-branch &&
 384        base=$(git rev-parse HEAD~4) &&
 385        set_fake_editor &&
 386        test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
 387        echo three > conflict &&
 388        git add conflict &&
 389        FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
 390                git rebase --continue &&
 391        test $base = $(git rev-parse HEAD^) &&
 392        test 2 = $(git show | grep TWICE | wc -l) &&
 393        git checkout to-be-rebased &&
 394        git branch -D conflict-squash
 395'
 396
 397cat > expect-squash-fixup << EOF
 398B
 399
 400D
 401
 402ONCE
 403EOF
 404
 405test_expect_success 'squash and fixup generate correct log messages' '
 406        git checkout -b squash-fixup E &&
 407        base=$(git rev-parse HEAD~4) &&
 408        set_fake_editor &&
 409        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
 410                EXPECT_HEADER_COUNT=4 \
 411                git rebase -i $base &&
 412        git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
 413        test_cmp expect-squash-fixup actual-squash-fixup &&
 414        git checkout to-be-rebased &&
 415        git branch -D squash-fixup
 416'
 417
 418test_expect_success 'squash ignores comments' '
 419        git checkout -b skip-comments E &&
 420        base=$(git rev-parse HEAD~4) &&
 421        set_fake_editor &&
 422        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
 423                EXPECT_HEADER_COUNT=4 \
 424                git rebase -i $base &&
 425        test $base = $(git rev-parse HEAD^) &&
 426        test 1 = $(git show | grep ONCE | wc -l) &&
 427        git checkout to-be-rebased &&
 428        git branch -D skip-comments
 429'
 430
 431test_expect_success 'squash ignores blank lines' '
 432        git checkout -b skip-blank-lines E &&
 433        base=$(git rev-parse HEAD~4) &&
 434        set_fake_editor &&
 435        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
 436                EXPECT_HEADER_COUNT=4 \
 437                git rebase -i $base &&
 438        test $base = $(git rev-parse HEAD^) &&
 439        test 1 = $(git show | grep ONCE | wc -l) &&
 440        git checkout to-be-rebased &&
 441        git branch -D skip-blank-lines
 442'
 443
 444test_expect_success 'squash works as expected' '
 445        git checkout -b squash-works no-conflict-branch &&
 446        one=$(git rev-parse HEAD~3) &&
 447        set_fake_editor &&
 448        FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
 449                git rebase -i HEAD~3 &&
 450        test $one = $(git rev-parse HEAD~2)
 451'
 452
 453test_expect_success 'interrupted squash works as expected' '
 454        git checkout -b interrupted-squash conflict-branch &&
 455        one=$(git rev-parse HEAD~3) &&
 456        set_fake_editor &&
 457        test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
 458        (echo one; echo two; echo four) > conflict &&
 459        git add conflict &&
 460        test_must_fail git rebase --continue &&
 461        echo resolved > conflict &&
 462        git add conflict &&
 463        git rebase --continue &&
 464        test $one = $(git rev-parse HEAD~2)
 465'
 466
 467test_expect_success 'interrupted squash works as expected (case 2)' '
 468        git checkout -b interrupted-squash2 conflict-branch &&
 469        one=$(git rev-parse HEAD~3) &&
 470        set_fake_editor &&
 471        test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
 472        (echo one; echo four) > conflict &&
 473        git add conflict &&
 474        test_must_fail git rebase --continue &&
 475        (echo one; echo two; echo four) > conflict &&
 476        git add conflict &&
 477        test_must_fail git rebase --continue &&
 478        echo resolved > conflict &&
 479        git add conflict &&
 480        git rebase --continue &&
 481        test $one = $(git rev-parse HEAD~2)
 482'
 483
 484test_expect_success '--continue tries to commit, even for "edit"' '
 485        echo unrelated > file7 &&
 486        git add file7 &&
 487        test_tick &&
 488        git commit -m "unrelated change" &&
 489        parent=$(git rev-parse HEAD^) &&
 490        test_tick &&
 491        set_fake_editor &&
 492        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 493        echo edited > file7 &&
 494        git add file7 &&
 495        FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
 496        test edited = $(git show HEAD:file7) &&
 497        git show HEAD | grep chouette &&
 498        test $parent = $(git rev-parse HEAD^)
 499'
 500
 501test_expect_success 'aborted --continue does not squash commits after "edit"' '
 502        old=$(git rev-parse HEAD) &&
 503        test_tick &&
 504        set_fake_editor &&
 505        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 506        echo "edited again" > file7 &&
 507        git add file7 &&
 508        test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue &&
 509        test $old = $(git rev-parse HEAD) &&
 510        git rebase --abort
 511'
 512
 513test_expect_success 'auto-amend only edited commits after "edit"' '
 514        test_tick &&
 515        set_fake_editor &&
 516        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 517        echo "edited again" > file7 &&
 518        git add file7 &&
 519        FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
 520        echo "and again" > file7 &&
 521        git add file7 &&
 522        test_tick &&
 523        test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
 524        git rebase --abort
 525'
 526
 527test_expect_success 'clean error after failed "exec"' '
 528        test_tick &&
 529        test_when_finished "git rebase --abort || :" &&
 530        set_fake_editor &&
 531        test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
 532        echo "edited again" > file7 &&
 533        git add file7 &&
 534        test_must_fail git rebase --continue 2>error &&
 535        grep "You have staged changes in your working tree." error
 536'
 537
 538test_expect_success 'rebase a detached HEAD' '
 539        grandparent=$(git rev-parse HEAD~2) &&
 540        git checkout $(git rev-parse HEAD) &&
 541        test_tick &&
 542        set_fake_editor &&
 543        FAKE_LINES="2 1" git rebase -i HEAD~2 &&
 544        test $grandparent = $(git rev-parse HEAD~2)
 545'
 546
 547test_expect_success 'rebase a commit violating pre-commit' '
 548
 549        mkdir -p .git/hooks &&
 550        PRE_COMMIT=.git/hooks/pre-commit &&
 551        echo "#!/bin/sh" > $PRE_COMMIT &&
 552        echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
 553        chmod a+x $PRE_COMMIT &&
 554        echo "monde! " >> file1 &&
 555        test_tick &&
 556        test_must_fail git commit -m doesnt-verify file1 &&
 557        git commit -m doesnt-verify --no-verify file1 &&
 558        test_tick &&
 559        set_fake_editor &&
 560        FAKE_LINES=2 git rebase -i HEAD~2
 561
 562'
 563
 564test_expect_success 'rebase with a file named HEAD in worktree' '
 565
 566        rm -fr .git/hooks &&
 567        git reset --hard &&
 568        git checkout -b branch3 A &&
 569
 570        (
 571                GIT_AUTHOR_NAME="Squashed Away" &&
 572                export GIT_AUTHOR_NAME &&
 573                >HEAD &&
 574                git add HEAD &&
 575                git commit -m "Add head" &&
 576                >BODY &&
 577                git add BODY &&
 578                git commit -m "Add body"
 579        ) &&
 580
 581        set_fake_editor &&
 582        FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
 583        test "$(git show -s --pretty=format:%an)" = "Squashed Away"
 584
 585'
 586
 587test_expect_success 'do "noop" when there is nothing to cherry-pick' '
 588
 589        git checkout -b branch4 HEAD &&
 590        GIT_EDITOR=: git commit --amend \
 591                --author="Somebody else <somebody@else.com>" &&
 592        test $(git rev-parse branch3) != $(git rev-parse branch4) &&
 593        set_fake_editor &&
 594        git rebase -i branch3 &&
 595        test $(git rev-parse branch3) = $(git rev-parse branch4)
 596
 597'
 598
 599test_expect_success 'submodule rebase setup' '
 600        git checkout A &&
 601        mkdir sub &&
 602        (
 603                cd sub && git init && >elif &&
 604                git add elif && git commit -m "submodule initial"
 605        ) &&
 606        echo 1 >file1 &&
 607        git add file1 sub &&
 608        test_tick &&
 609        git commit -m "One" &&
 610        echo 2 >file1 &&
 611        test_tick &&
 612        git commit -a -m "Two" &&
 613        (
 614                cd sub && echo 3 >elif &&
 615                git commit -a -m "submodule second"
 616        ) &&
 617        test_tick &&
 618        set_fake_editor &&
 619        git commit -a -m "Three changes submodule"
 620'
 621
 622test_expect_success 'submodule rebase -i' '
 623        set_fake_editor &&
 624        FAKE_LINES="1 squash 2 3" git rebase -i A
 625'
 626
 627test_expect_success 'submodule conflict setup' '
 628        git tag submodule-base &&
 629        git checkout HEAD^ &&
 630        (
 631                cd sub && git checkout HEAD^ && echo 4 >elif &&
 632                git add elif && git commit -m "submodule conflict"
 633        ) &&
 634        git add sub &&
 635        test_tick &&
 636        git commit -m "Conflict in submodule" &&
 637        git tag submodule-topic
 638'
 639
 640test_expect_success 'rebase -i continue with only submodule staged' '
 641        set_fake_editor &&
 642        test_must_fail git rebase -i submodule-base &&
 643        git add sub &&
 644        git rebase --continue &&
 645        test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
 646'
 647
 648test_expect_success 'rebase -i continue with unstaged submodule' '
 649        git checkout submodule-topic &&
 650        git reset --hard &&
 651        set_fake_editor &&
 652        test_must_fail git rebase -i submodule-base &&
 653        git reset &&
 654        git rebase --continue &&
 655        test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
 656'
 657
 658test_expect_success 'avoid unnecessary reset' '
 659        git checkout master &&
 660        git reset --hard &&
 661        test-chmtime =123456789 file3 &&
 662        git update-index --refresh &&
 663        HEAD=$(git rev-parse HEAD) &&
 664        set_fake_editor &&
 665        git rebase -i HEAD~4 &&
 666        test $HEAD = $(git rev-parse HEAD) &&
 667        MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
 668        test 123456789 = $MTIME
 669'
 670
 671test_expect_success 'reword' '
 672        git checkout -b reword-branch master &&
 673        set_fake_editor &&
 674        FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
 675        git show HEAD | grep "E changed" &&
 676        test $(git rev-parse master) != $(git rev-parse HEAD) &&
 677        test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
 678        FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
 679        git show HEAD^ | grep "D changed" &&
 680        FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
 681        git show HEAD~3 | grep "B changed" &&
 682        FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
 683        git show HEAD~2 | grep "C changed"
 684'
 685
 686test_expect_success 'rebase -i can copy notes' '
 687        git config notes.rewrite.rebase true &&
 688        git config notes.rewriteRef "refs/notes/*" &&
 689        test_commit n1 &&
 690        test_commit n2 &&
 691        test_commit n3 &&
 692        git notes add -m"a note" n3 &&
 693        set_fake_editor &&
 694        git rebase -i --onto n1 n2 &&
 695        test "a note" = "$(git notes show HEAD)"
 696'
 697
 698cat >expect <<EOF
 699an earlier note
 700
 701a note
 702EOF
 703
 704test_expect_success 'rebase -i can copy notes over a fixup' '
 705        git reset --hard n3 &&
 706        git notes add -m"an earlier note" n2 &&
 707        set_fake_editor &&
 708        GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
 709        git notes show > output &&
 710        test_cmp expect output
 711'
 712
 713test_expect_success 'rebase while detaching HEAD' '
 714        git symbolic-ref HEAD &&
 715        grandparent=$(git rev-parse HEAD~2) &&
 716        test_tick &&
 717        set_fake_editor &&
 718        FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
 719        test $grandparent = $(git rev-parse HEAD~2) &&
 720        test_must_fail git symbolic-ref HEAD
 721'
 722
 723test_tick # Ensure that the rebased commits get a different timestamp.
 724test_expect_success 'always cherry-pick with --no-ff' '
 725        git checkout no-ff-branch &&
 726        git tag original-no-ff-branch &&
 727        set_fake_editor &&
 728        git rebase -i --no-ff A &&
 729        touch empty &&
 730        for p in 0 1 2
 731        do
 732                test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
 733                git diff HEAD~$p original-no-ff-branch~$p > out &&
 734                test_cmp empty out
 735        done &&
 736        test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
 737        git diff HEAD~3 original-no-ff-branch~3 > out &&
 738        test_cmp empty out
 739'
 740
 741test_expect_success 'set up commits with funny messages' '
 742        git checkout -b funny A &&
 743        echo >>file1 &&
 744        test_tick &&
 745        git commit -a -m "end with slash\\" &&
 746        echo >>file1 &&
 747        test_tick &&
 748        git commit -a -m "something (\000) that looks like octal" &&
 749        echo >>file1 &&
 750        test_tick &&
 751        git commit -a -m "something (\n) that looks like a newline" &&
 752        echo >>file1 &&
 753        test_tick &&
 754        git commit -a -m "another commit"
 755'
 756
 757test_expect_success 'rebase-i history with funny messages' '
 758        git rev-list A..funny >expect &&
 759        test_tick &&
 760        set_fake_editor &&
 761        FAKE_LINES="1 2 3 4" git rebase -i A &&
 762        git rev-list A.. >actual &&
 763        test_cmp expect actual
 764'
 765
 766
 767test_expect_success 'prepare for rebase -i --exec' '
 768        git checkout master &&
 769        git checkout -b execute &&
 770        test_commit one_exec main.txt one_exec &&
 771        test_commit two_exec main.txt two_exec &&
 772        test_commit three_exec main.txt three_exec
 773'
 774
 775
 776test_expect_success 'running "git rebase -i --exec git show HEAD"' '
 777        set_fake_editor &&
 778        git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
 779        (
 780                FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
 781                export FAKE_LINES &&
 782                git rebase -i HEAD~2 >expect
 783        ) &&
 784        sed -e "1,9d" expect >expected &&
 785        test_cmp expected actual
 786'
 787
 788
 789test_expect_success 'running "git rebase --exec git show HEAD -i"' '
 790        git reset --hard execute &&
 791        set_fake_editor &&
 792        git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
 793        (
 794                FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
 795                export FAKE_LINES &&
 796                git rebase -i HEAD~2 >expect
 797        ) &&
 798        sed -e "1,9d" expect >expected &&
 799        test_cmp expected actual
 800'
 801
 802
 803test_expect_success 'running "git rebase -ix git show HEAD"' '
 804        git reset --hard execute &&
 805        set_fake_editor &&
 806        git rebase -ix "git show HEAD" HEAD~2 >actual &&
 807        (
 808                FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
 809                export FAKE_LINES &&
 810                git rebase -i HEAD~2 >expect
 811        ) &&
 812        sed -e "1,9d" expect >expected &&
 813        test_cmp expected actual
 814'
 815
 816
 817test_expect_success 'rebase -ix with several <CMD>' '
 818        git reset --hard execute &&
 819        set_fake_editor &&
 820        git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
 821        (
 822                FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
 823                export FAKE_LINES &&
 824                git rebase -i HEAD~2 >expect
 825        ) &&
 826        sed -e "1,9d" expect >expected &&
 827        test_cmp expected actual
 828'
 829
 830
 831test_expect_success 'rebase -ix with several instances of --exec' '
 832        git reset --hard execute &&
 833        set_fake_editor &&
 834        git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
 835        (
 836                FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
 837                                exec_git_show_HEAD exec_pwd" &&
 838                export FAKE_LINES &&
 839                git rebase -i HEAD~2 >expect
 840        ) &&
 841        sed -e "1,11d" expect >expected &&
 842        test_cmp expected actual
 843'
 844
 845
 846test_expect_success 'rebase -ix with --autosquash' '
 847        git reset --hard execute &&
 848        git checkout -b autosquash &&
 849        echo second >second.txt &&
 850        git add second.txt &&
 851        git commit -m "fixup! two_exec" &&
 852        echo bis >bis.txt &&
 853        git add bis.txt &&
 854        git commit -m "fixup! two_exec" &&
 855        set_fake_editor &&
 856        (
 857                git checkout -b autosquash_actual &&
 858                git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
 859        ) &&
 860        git checkout autosquash &&
 861        (
 862                git checkout -b autosquash_expected &&
 863                FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
 864                export FAKE_LINES &&
 865                git rebase -i HEAD~4 >expect
 866        ) &&
 867        sed -e "1,13d" expect >expected &&
 868        test_cmp expected actual
 869'
 870
 871
 872test_expect_success 'rebase --exec without -i shows error message' '
 873        git reset --hard execute &&
 874        set_fake_editor &&
 875        test_must_fail git rebase --exec "git show HEAD" HEAD~2 2>actual &&
 876        echo "The --exec option must be used with the --interactive option" >expected &&
 877        test_i18ncmp expected actual
 878'
 879
 880
 881test_expect_success 'rebase -i --exec without <CMD>' '
 882        git reset --hard execute &&
 883        set_fake_editor &&
 884        test_must_fail git rebase -i --exec 2>tmp &&
 885        sed -e "1d" tmp >actual &&
 886        test_must_fail git rebase -h >expected &&
 887        test_cmp expected actual &&
 888        git checkout master
 889'
 890
 891test_expect_success 'rebase -i --root re-order and drop commits' '
 892        git checkout E &&
 893        set_fake_editor &&
 894        FAKE_LINES="3 1 2 5" git rebase -i --root &&
 895        test E = $(git cat-file commit HEAD | sed -ne \$p) &&
 896        test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
 897        test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
 898        test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
 899        test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
 900'
 901
 902test_expect_success 'rebase -i --root retain root commit author and message' '
 903        git checkout A &&
 904        echo B >file7 &&
 905        git add file7 &&
 906        GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
 907        set_fake_editor &&
 908        FAKE_LINES="2" git rebase -i --root &&
 909        git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
 910        git cat-file commit HEAD | grep -q "^different author$"
 911'
 912
 913test_expect_success 'rebase -i --root temporary sentinel commit' '
 914        git checkout B &&
 915        set_fake_editor &&
 916        test_must_fail env FAKE_LINES="2" git rebase -i --root &&
 917        git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
 918        git rebase --abort
 919'
 920
 921test_expect_success 'rebase -i --root fixup root commit' '
 922        git checkout B &&
 923        set_fake_editor &&
 924        FAKE_LINES="1 fixup 2" git rebase -i --root &&
 925        test A = $(git cat-file commit HEAD | sed -ne \$p) &&
 926        test B = $(git show HEAD:file1) &&
 927        test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
 928'
 929
 930test_expect_success 'rebase --edit-todo does not works on non-interactive rebase' '
 931        git reset --hard &&
 932        git checkout conflict-branch &&
 933        set_fake_editor &&
 934        test_must_fail git rebase --onto HEAD~2 HEAD~ &&
 935        test_must_fail git rebase --edit-todo &&
 936        git rebase --abort
 937'
 938
 939test_expect_success 'rebase --edit-todo can be used to modify todo' '
 940        git reset --hard &&
 941        git checkout no-conflict-branch^0 &&
 942        set_fake_editor &&
 943        FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
 944        FAKE_LINES="2 1" git rebase --edit-todo &&
 945        git rebase --continue
 946        test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
 947        test L = $(git cat-file commit HEAD | sed -ne \$p)
 948'
 949
 950test_expect_success 'rebase -i produces readable reflog' '
 951        git reset --hard &&
 952        git branch -f branch-reflog-test H &&
 953        set_fake_editor &&
 954        git rebase -i --onto I F branch-reflog-test &&
 955        cat >expect <<-\EOF &&
 956        rebase -i (start): checkout I
 957        rebase -i (pick): G
 958        rebase -i (pick): H
 959        rebase -i (finish): returning to refs/heads/branch-reflog-test
 960        EOF
 961        tail -n 4 .git/logs/HEAD |
 962        sed -e "s/.*    //" >actual &&
 963        test_cmp expect actual
 964'
 965
 966test_expect_success 'rebase -i respects core.commentchar' '
 967        git reset --hard &&
 968        git checkout E^0 &&
 969        test_config core.commentchar "\\" &&
 970        write_script remove-all-but-first.sh <<-\EOF &&
 971        sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
 972        mv "$1.tmp" "$1"
 973        EOF
 974        test_set_editor "$(pwd)/remove-all-but-first.sh" &&
 975        git rebase -i B &&
 976        test B = $(git cat-file commit HEAD^ | sed -ne \$p)
 977'
 978
 979test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
 980        test_when_finished "git branch -D torebase" &&
 981        git checkout -b torebase branch1 &&
 982        upstream=$(git rev-parse ":/J") &&
 983        onto=$(git rev-parse ":/A") &&
 984        git rebase --onto $onto $upstream &&
 985        git reset --hard branch1 &&
 986        git rebase --onto ":/A" ":/J" &&
 987        git checkout branch1
 988'
 989
 990test_expect_success 'rebase -i with --strategy and -X' '
 991        git checkout -b conflict-merge-use-theirs conflict-branch &&
 992        git reset --hard HEAD^ &&
 993        echo five >conflict &&
 994        echo Z >file1 &&
 995        git commit -a -m "one file conflict" &&
 996        EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
 997        test $(git show conflict-branch:conflict) = $(cat conflict) &&
 998        test $(cat file1) = Z
 999'
1000
1001test_expect_success 'rebase -i error on commits with \ in message' '
1002        current_head=$(git rev-parse HEAD)
1003        test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1004        test_commit TO-REMOVE will-conflict old-content &&
1005        test_commit "\temp" will-conflict new-content dummy &&
1006        test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1007        test_expect_code 1 grep  "      emp" error
1008'
1009
1010test_expect_success 'short SHA-1 setup' '
1011        test_when_finished "git checkout master" &&
1012        git checkout --orphan collide &&
1013        git rm -rf . &&
1014        (
1015        unset test_tick &&
1016        test_commit collide1 collide &&
1017        test_commit --notick collide2 collide &&
1018        test_commit --notick collide3 collide
1019        )
1020'
1021
1022test_expect_success 'short SHA-1 collide' '
1023        test_when_finished "reset_rebase && git checkout master" &&
1024        git checkout collide &&
1025        (
1026        unset test_tick &&
1027        test_tick &&
1028        set_fake_editor &&
1029        FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1030        FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1031        )
1032'
1033
1034test_done