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