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