t / t3404-rebase-interactive.shon commit Merge branch 'ag/rebase-i-in-c' into js/rebase-in-c-5.5-work-with-rebase-i-in-c (5ab7e0f)
   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 run 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. Unsetting 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 --keep-empty' '
  71        git checkout -b emptybranch master &&
  72        git commit --allow-empty -m "empty" &&
  73        git rebase --keep-empty -i HEAD~2 &&
  74        git log --oneline >actual &&
  75        test_line_count = 6 actual
  76'
  77
  78cat > expect <<EOF
  79error: nothing to do
  80EOF
  81
  82test_expect_success 'rebase -i with empty HEAD' '
  83        set_fake_editor &&
  84        test_must_fail env FAKE_LINES="1 exec_true" git rebase -i HEAD^ >actual 2>&1 &&
  85        test_i18ncmp expect actual
  86'
  87
  88test_expect_success 'rebase -i with the exec command' '
  89        git checkout master &&
  90        (
  91        set_fake_editor &&
  92        FAKE_LINES="1 exec_>touch-one
  93                2 exec_>touch-two exec_false exec_>touch-three
  94                3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
  95        export FAKE_LINES &&
  96        test_must_fail git rebase -i A
  97        ) &&
  98        test_path_is_file touch-one &&
  99        test_path_is_file touch-two &&
 100        test_path_is_missing touch-three " (should have stopped before)" &&
 101        test_cmp_rev C HEAD &&
 102        git rebase --continue &&
 103        test_path_is_file touch-three &&
 104        test_path_is_file "touch-file  name with spaces" &&
 105        test_path_is_file touch-after-semicolon &&
 106        test_cmp_rev master HEAD &&
 107        rm -f touch-*
 108'
 109
 110test_expect_success 'rebase -i with the exec command runs from tree root' '
 111        git checkout master &&
 112        mkdir subdir && (cd subdir &&
 113        set_fake_editor &&
 114        FAKE_LINES="1 exec_>touch-subdir" \
 115                git rebase -i HEAD^
 116        ) &&
 117        test_path_is_file touch-subdir &&
 118        rm -fr subdir
 119'
 120
 121test_expect_success 'rebase -i with exec allows git commands in subdirs' '
 122        test_when_finished "rm -rf subdir" &&
 123        test_when_finished "git rebase --abort ||:" &&
 124        git checkout master &&
 125        mkdir subdir && (cd subdir &&
 126        set_fake_editor &&
 127        FAKE_LINES="1 exec_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
 128                git rebase -i HEAD^
 129        )
 130'
 131
 132test_expect_success 'rebase -i sets work tree properly' '
 133        test_when_finished "rm -rf subdir" &&
 134        test_when_finished "test_might_fail git rebase --abort" &&
 135        mkdir subdir &&
 136        git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
 137                >actual &&
 138        ! grep "/subdir$" actual
 139'
 140
 141test_expect_success 'rebase -i with the exec command checks tree cleanness' '
 142        git checkout master &&
 143        set_fake_editor &&
 144        test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" git rebase -i HEAD^ &&
 145        test_cmp_rev master^ HEAD &&
 146        git reset --hard &&
 147        git rebase --continue
 148'
 149
 150test_expect_success 'rebase -i with exec of inexistent command' '
 151        git checkout master &&
 152        test_when_finished "git rebase --abort" &&
 153        set_fake_editor &&
 154        test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
 155        git rebase -i HEAD^ >actual 2>&1 &&
 156        ! grep "Maybe git-rebase is broken" actual
 157'
 158
 159test_expect_success 'no changes are a nop' '
 160        git checkout branch2 &&
 161        set_fake_editor &&
 162        git rebase -i F &&
 163        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
 164        test $(git rev-parse I) = $(git rev-parse HEAD)
 165'
 166
 167test_expect_success 'test the [branch] option' '
 168        git checkout -b dead-end &&
 169        git rm file6 &&
 170        git commit -m "stop here" &&
 171        set_fake_editor &&
 172        git rebase -i F branch2 &&
 173        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
 174        test $(git rev-parse I) = $(git rev-parse branch2) &&
 175        test $(git rev-parse I) = $(git rev-parse HEAD)
 176'
 177
 178test_expect_success 'test --onto <branch>' '
 179        git checkout -b test-onto branch2 &&
 180        set_fake_editor &&
 181        git rebase -i --onto branch1 F &&
 182        test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
 183        test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
 184        test $(git rev-parse I) = $(git rev-parse branch2)
 185'
 186
 187test_expect_success 'rebase on top of a non-conflicting commit' '
 188        git checkout branch1 &&
 189        git tag original-branch1 &&
 190        set_fake_editor &&
 191        git rebase -i branch2 &&
 192        test file6 = $(git diff --name-only original-branch1) &&
 193        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
 194        test $(git rev-parse I) = $(git rev-parse branch2) &&
 195        test $(git rev-parse I) = $(git rev-parse HEAD~2)
 196'
 197
 198test_expect_success 'reflog for the branch shows state before rebase' '
 199        test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
 200'
 201
 202test_expect_success 'reflog for the branch shows correct finish message' '
 203        printf "rebase -i (finish): refs/heads/branch1 onto %s\n" \
 204                "$(git rev-parse branch2)" >expected &&
 205        git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
 206        test_cmp expected actual
 207'
 208
 209test_expect_success 'exchange two commits' '
 210        set_fake_editor &&
 211        FAKE_LINES="2 1" git rebase -i HEAD~2 &&
 212        test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
 213        test G = $(git cat-file commit HEAD | sed -ne \$p)
 214'
 215
 216cat > expect << EOF
 217diff --git a/file1 b/file1
 218index f70f10e..fd79235 100644
 219--- a/file1
 220+++ b/file1
 221@@ -1 +1 @@
 222-A
 223+G
 224EOF
 225
 226cat > expect2 << EOF
 227<<<<<<< HEAD
 228D
 229=======
 230G
 231>>>>>>> 5d18e54... G
 232EOF
 233
 234test_expect_success 'stop on conflicting pick' '
 235        git tag new-branch1 &&
 236        set_fake_editor &&
 237        test_must_fail git rebase -i master &&
 238        test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
 239        test_cmp expect .git/rebase-merge/patch &&
 240        test_cmp expect2 file1 &&
 241        test "$(git diff --name-status |
 242                sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
 243        test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
 244        test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
 245'
 246
 247test_expect_success 'show conflicted patch' '
 248        GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
 249        grep "show.*REBASE_HEAD" stderr &&
 250        # the original stopped-sha1 is abbreviated
 251        stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
 252        test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
 253'
 254
 255test_expect_success 'abort' '
 256        git rebase --abort &&
 257        test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
 258        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
 259        test_path_is_missing .git/rebase-merge
 260'
 261
 262test_expect_success 'abort with error when new base cannot be checked out' '
 263        git rm --cached file1 &&
 264        git commit -m "remove file in base" &&
 265        set_fake_editor &&
 266        test_must_fail git rebase -i master > output 2>&1 &&
 267        test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" \
 268                output &&
 269        test_i18ngrep "file1" output &&
 270        test_path_is_missing .git/rebase-merge &&
 271        git reset --hard HEAD^
 272'
 273
 274test_expect_success 'retain authorship' '
 275        echo A > file7 &&
 276        git add file7 &&
 277        test_tick &&
 278        GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
 279        git tag twerp &&
 280        set_fake_editor &&
 281        git rebase -i --onto master HEAD^ &&
 282        git show HEAD | grep "^Author: Twerp Snog"
 283'
 284
 285test_expect_success 'retain authorship w/ conflicts' '
 286        oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
 287        test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
 288
 289        git reset --hard twerp &&
 290        test_commit a conflict a conflict-a &&
 291        git reset --hard twerp &&
 292
 293        GIT_AUTHOR_NAME=AttributeMe &&
 294        export GIT_AUTHOR_NAME &&
 295        test_commit b conflict b conflict-b &&
 296        GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
 297
 298        set_fake_editor &&
 299        test_must_fail git rebase -i conflict-a &&
 300        echo resolved >conflict &&
 301        git add conflict &&
 302        git rebase --continue &&
 303        test $(git rev-parse conflict-a^0) = $(git rev-parse HEAD^) &&
 304        git show >out &&
 305        grep AttributeMe out
 306'
 307
 308test_expect_success 'squash' '
 309        git reset --hard twerp &&
 310        echo B > file7 &&
 311        test_tick &&
 312        GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
 313        echo "******************************" &&
 314        set_fake_editor &&
 315        FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
 316                git rebase -i --onto master HEAD~2 &&
 317        test B = $(cat file7) &&
 318        test $(git rev-parse HEAD^) = $(git rev-parse master)
 319'
 320
 321test_expect_success 'retain authorship when squashing' '
 322        git show HEAD | grep "^Author: Twerp Snog"
 323'
 324
 325test_expect_success '-p handles "no changes" gracefully' '
 326        HEAD=$(git rev-parse HEAD) &&
 327        set_fake_editor &&
 328        git rebase -i -p HEAD^ &&
 329        git update-index --refresh &&
 330        git diff-files --quiet &&
 331        git diff-index --quiet --cached HEAD -- &&
 332        test $HEAD = $(git rev-parse HEAD)
 333'
 334
 335test_expect_failure 'exchange two commits with -p' '
 336        git checkout H &&
 337        set_fake_editor &&
 338        FAKE_LINES="2 1" git rebase -i -p HEAD~2 &&
 339        test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
 340        test G = $(git cat-file commit HEAD | sed -ne \$p)
 341'
 342
 343test_expect_success 'preserve merges with -p' '
 344        git checkout -b to-be-preserved master^ &&
 345        : > unrelated-file &&
 346        git add unrelated-file &&
 347        test_tick &&
 348        git commit -m "unrelated" &&
 349        git checkout -b another-branch master &&
 350        echo B > file1 &&
 351        test_tick &&
 352        git commit -m J file1 &&
 353        test_tick &&
 354        git merge to-be-preserved &&
 355        echo C > file1 &&
 356        test_tick &&
 357        git commit -m K file1 &&
 358        echo D > file1 &&
 359        test_tick &&
 360        git commit -m L1 file1 &&
 361        git checkout HEAD^ &&
 362        echo 1 > unrelated-file &&
 363        test_tick &&
 364        git commit -m L2 unrelated-file &&
 365        test_tick &&
 366        git merge another-branch &&
 367        echo E > file1 &&
 368        test_tick &&
 369        git commit -m M file1 &&
 370        git checkout -b to-be-rebased &&
 371        test_tick &&
 372        set_fake_editor &&
 373        git rebase -i -p --onto branch1 master &&
 374        git update-index --refresh &&
 375        git diff-files --quiet &&
 376        git diff-index --quiet --cached HEAD -- &&
 377        test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
 378        test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
 379        test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
 380        test $(git show HEAD~5:file1) = B &&
 381        test $(git show HEAD~3:file1) = C &&
 382        test $(git show HEAD:file1) = E &&
 383        test $(git show HEAD:unrelated-file) = 1
 384'
 385
 386test_expect_success 'edit ancestor with -p' '
 387        set_fake_editor &&
 388        FAKE_LINES="1 2 edit 3 4" git rebase -i -p HEAD~3 &&
 389        echo 2 > unrelated-file &&
 390        test_tick &&
 391        git commit -m L2-modified --amend unrelated-file &&
 392        git rebase --continue &&
 393        git update-index --refresh &&
 394        git diff-files --quiet &&
 395        git diff-index --quiet --cached HEAD -- &&
 396        test $(git show HEAD:unrelated-file) = 2
 397'
 398
 399test_expect_success '--continue tries to commit' '
 400        test_tick &&
 401        set_fake_editor &&
 402        test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
 403        echo resolved > file1 &&
 404        git add file1 &&
 405        FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
 406        test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
 407        git show HEAD | grep chouette
 408'
 409
 410test_expect_success 'verbose flag is heeded, even after --continue' '
 411        git reset --hard master@{1} &&
 412        test_tick &&
 413        set_fake_editor &&
 414        test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
 415        echo resolved > file1 &&
 416        git add file1 &&
 417        git rebase --continue > output &&
 418        grep "^ file1 | 2 +-$" output
 419'
 420
 421test_expect_success C_LOCALE_OUTPUT 'multi-squash only fires up editor once' '
 422        base=$(git rev-parse HEAD~4) &&
 423        set_fake_editor &&
 424        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
 425                EXPECT_HEADER_COUNT=4 \
 426                git rebase -i $base &&
 427        test $base = $(git rev-parse HEAD^) &&
 428        test 1 = $(git show | grep ONCE | wc -l)
 429'
 430
 431test_expect_success C_LOCALE_OUTPUT 'multi-fixup does not fire up editor' '
 432        git checkout -b multi-fixup E &&
 433        base=$(git rev-parse HEAD~4) &&
 434        set_fake_editor &&
 435        FAKE_COMMIT_AMEND="NEVER" FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
 436                git rebase -i $base &&
 437        test $base = $(git rev-parse HEAD^) &&
 438        test 0 = $(git show | grep NEVER | wc -l) &&
 439        git checkout to-be-rebased &&
 440        git branch -D multi-fixup
 441'
 442
 443test_expect_success 'commit message used after conflict' '
 444        git checkout -b conflict-fixup conflict-branch &&
 445        base=$(git rev-parse HEAD~4) &&
 446        set_fake_editor &&
 447        test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" git rebase -i $base &&
 448        echo three > conflict &&
 449        git add conflict &&
 450        FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
 451                git rebase --continue &&
 452        test $base = $(git rev-parse HEAD^) &&
 453        test 1 = $(git show | grep ONCE | wc -l) &&
 454        git checkout to-be-rebased &&
 455        git branch -D conflict-fixup
 456'
 457
 458test_expect_success 'commit message retained after conflict' '
 459        git checkout -b conflict-squash conflict-branch &&
 460        base=$(git rev-parse HEAD~4) &&
 461        set_fake_editor &&
 462        test_must_fail env FAKE_LINES="1 fixup 3 squash 4" git rebase -i $base &&
 463        echo three > conflict &&
 464        git add conflict &&
 465        FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
 466                git rebase --continue &&
 467        test $base = $(git rev-parse HEAD^) &&
 468        test 2 = $(git show | grep TWICE | wc -l) &&
 469        git checkout to-be-rebased &&
 470        git branch -D conflict-squash
 471'
 472
 473cat > expect-squash-fixup << EOF
 474B
 475
 476D
 477
 478ONCE
 479EOF
 480
 481test_expect_success C_LOCALE_OUTPUT 'squash and fixup generate correct log messages' '
 482        git checkout -b squash-fixup E &&
 483        base=$(git rev-parse HEAD~4) &&
 484        set_fake_editor &&
 485        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
 486                EXPECT_HEADER_COUNT=4 \
 487                git rebase -i $base &&
 488        git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
 489        test_cmp expect-squash-fixup actual-squash-fixup &&
 490        git cat-file commit HEAD@{2} |
 491                grep "^# This is a combination of 3 commits\."  &&
 492        git cat-file commit HEAD@{3} |
 493                grep "^# This is a combination of 2 commits\."  &&
 494        git checkout to-be-rebased &&
 495        git branch -D squash-fixup
 496'
 497
 498test_expect_success C_LOCALE_OUTPUT 'squash ignores comments' '
 499        git checkout -b skip-comments E &&
 500        base=$(git rev-parse HEAD~4) &&
 501        set_fake_editor &&
 502        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
 503                EXPECT_HEADER_COUNT=4 \
 504                git rebase -i $base &&
 505        test $base = $(git rev-parse HEAD^) &&
 506        test 1 = $(git show | grep ONCE | wc -l) &&
 507        git checkout to-be-rebased &&
 508        git branch -D skip-comments
 509'
 510
 511test_expect_success C_LOCALE_OUTPUT 'squash ignores blank lines' '
 512        git checkout -b skip-blank-lines E &&
 513        base=$(git rev-parse HEAD~4) &&
 514        set_fake_editor &&
 515        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
 516                EXPECT_HEADER_COUNT=4 \
 517                git rebase -i $base &&
 518        test $base = $(git rev-parse HEAD^) &&
 519        test 1 = $(git show | grep ONCE | wc -l) &&
 520        git checkout to-be-rebased &&
 521        git branch -D skip-blank-lines
 522'
 523
 524test_expect_success 'squash works as expected' '
 525        git checkout -b squash-works no-conflict-branch &&
 526        one=$(git rev-parse HEAD~3) &&
 527        set_fake_editor &&
 528        FAKE_LINES="1 squash 3 2" EXPECT_HEADER_COUNT=2 \
 529                git rebase -i HEAD~3 &&
 530        test $one = $(git rev-parse HEAD~2)
 531'
 532
 533test_expect_success 'interrupted squash works as expected' '
 534        git checkout -b interrupted-squash conflict-branch &&
 535        one=$(git rev-parse HEAD~3) &&
 536        set_fake_editor &&
 537        test_must_fail env FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
 538        test_write_lines one two four > conflict &&
 539        git add conflict &&
 540        test_must_fail git rebase --continue &&
 541        echo resolved > conflict &&
 542        git add conflict &&
 543        git rebase --continue &&
 544        test $one = $(git rev-parse HEAD~2)
 545'
 546
 547test_expect_success 'interrupted squash works as expected (case 2)' '
 548        git checkout -b interrupted-squash2 conflict-branch &&
 549        one=$(git rev-parse HEAD~3) &&
 550        set_fake_editor &&
 551        test_must_fail env FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
 552        test_write_lines one four > conflict &&
 553        git add conflict &&
 554        test_must_fail git rebase --continue &&
 555        test_write_lines one two four > conflict &&
 556        git add conflict &&
 557        test_must_fail git rebase --continue &&
 558        echo resolved > conflict &&
 559        git add conflict &&
 560        git rebase --continue &&
 561        test $one = $(git rev-parse HEAD~2)
 562'
 563
 564test_expect_success '--continue tries to commit, even for "edit"' '
 565        echo unrelated > file7 &&
 566        git add file7 &&
 567        test_tick &&
 568        git commit -m "unrelated change" &&
 569        parent=$(git rev-parse HEAD^) &&
 570        test_tick &&
 571        set_fake_editor &&
 572        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 573        echo edited > file7 &&
 574        git add file7 &&
 575        FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
 576        test edited = $(git show HEAD:file7) &&
 577        git show HEAD | grep chouette &&
 578        test $parent = $(git rev-parse HEAD^)
 579'
 580
 581test_expect_success 'aborted --continue does not squash commits after "edit"' '
 582        test_when_finished "git rebase --abort" &&
 583        old=$(git rev-parse HEAD) &&
 584        test_tick &&
 585        set_fake_editor &&
 586        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 587        echo "edited again" > file7 &&
 588        git add file7 &&
 589        echo all the things >>conflict &&
 590        test_must_fail git rebase --continue &&
 591        test $old = $(git rev-parse HEAD)
 592'
 593
 594test_expect_success 'auto-amend only edited commits after "edit"' '
 595        test_tick &&
 596        set_fake_editor &&
 597        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 598        echo "edited again" > file7 &&
 599        git add file7 &&
 600        FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
 601        echo "and again" > file7 &&
 602        git add file7 &&
 603        test_tick &&
 604        test_must_fail env FAKE_COMMIT_MESSAGE="and again" git rebase --continue &&
 605        git rebase --abort
 606'
 607
 608test_expect_success 'clean error after failed "exec"' '
 609        test_tick &&
 610        test_when_finished "git rebase --abort || :" &&
 611        set_fake_editor &&
 612        test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ &&
 613        echo "edited again" > file7 &&
 614        git add file7 &&
 615        test_must_fail git rebase --continue 2>error &&
 616        test_i18ngrep "you have staged changes in your working tree" error
 617'
 618
 619test_expect_success 'rebase a detached HEAD' '
 620        grandparent=$(git rev-parse HEAD~2) &&
 621        git checkout $(git rev-parse HEAD) &&
 622        test_tick &&
 623        set_fake_editor &&
 624        FAKE_LINES="2 1" git rebase -i HEAD~2 &&
 625        test $grandparent = $(git rev-parse HEAD~2)
 626'
 627
 628test_expect_success 'rebase a commit violating pre-commit' '
 629
 630        mkdir -p .git/hooks &&
 631        write_script .git/hooks/pre-commit <<-\EOF &&
 632        test -z "$(git diff --cached --check)"
 633        EOF
 634        echo "monde! " >> file1 &&
 635        test_tick &&
 636        test_must_fail git commit -m doesnt-verify file1 &&
 637        git commit -m doesnt-verify --no-verify file1 &&
 638        test_tick &&
 639        set_fake_editor &&
 640        FAKE_LINES=2 git rebase -i HEAD~2
 641
 642'
 643
 644test_expect_success 'rebase with a file named HEAD in worktree' '
 645
 646        rm -fr .git/hooks &&
 647        git reset --hard &&
 648        git checkout -b branch3 A &&
 649
 650        (
 651                GIT_AUTHOR_NAME="Squashed Away" &&
 652                export GIT_AUTHOR_NAME &&
 653                >HEAD &&
 654                git add HEAD &&
 655                git commit -m "Add head" &&
 656                >BODY &&
 657                git add BODY &&
 658                git commit -m "Add body"
 659        ) &&
 660
 661        set_fake_editor &&
 662        FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
 663        test "$(git show -s --pretty=format:%an)" = "Squashed Away"
 664
 665'
 666
 667test_expect_success 'do "noop" when there is nothing to cherry-pick' '
 668
 669        git checkout -b branch4 HEAD &&
 670        GIT_EDITOR=: git commit --amend \
 671                --author="Somebody else <somebody@else.com>" &&
 672        test $(git rev-parse branch3) != $(git rev-parse branch4) &&
 673        set_fake_editor &&
 674        git rebase -i branch3 &&
 675        test $(git rev-parse branch3) = $(git rev-parse branch4)
 676
 677'
 678
 679test_expect_success 'submodule rebase setup' '
 680        git checkout A &&
 681        mkdir sub &&
 682        (
 683                cd sub && git init && >elif &&
 684                git add elif && git commit -m "submodule initial"
 685        ) &&
 686        echo 1 >file1 &&
 687        git add file1 sub &&
 688        test_tick &&
 689        git commit -m "One" &&
 690        echo 2 >file1 &&
 691        test_tick &&
 692        git commit -a -m "Two" &&
 693        (
 694                cd sub && echo 3 >elif &&
 695                git commit -a -m "submodule second"
 696        ) &&
 697        test_tick &&
 698        set_fake_editor &&
 699        git commit -a -m "Three changes submodule"
 700'
 701
 702test_expect_success 'submodule rebase -i' '
 703        set_fake_editor &&
 704        FAKE_LINES="1 squash 2 3" git rebase -i A
 705'
 706
 707test_expect_success 'submodule conflict setup' '
 708        git tag submodule-base &&
 709        git checkout HEAD^ &&
 710        (
 711                cd sub && git checkout HEAD^ && echo 4 >elif &&
 712                git add elif && git commit -m "submodule conflict"
 713        ) &&
 714        git add sub &&
 715        test_tick &&
 716        git commit -m "Conflict in submodule" &&
 717        git tag submodule-topic
 718'
 719
 720test_expect_success 'rebase -i continue with only submodule staged' '
 721        set_fake_editor &&
 722        test_must_fail git rebase -i submodule-base &&
 723        git add sub &&
 724        git rebase --continue &&
 725        test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
 726'
 727
 728test_expect_success 'rebase -i continue with unstaged submodule' '
 729        git checkout submodule-topic &&
 730        git reset --hard &&
 731        set_fake_editor &&
 732        test_must_fail git rebase -i submodule-base &&
 733        git reset &&
 734        git rebase --continue &&
 735        test $(git rev-parse submodule-base) = $(git rev-parse HEAD)
 736'
 737
 738test_expect_success 'avoid unnecessary reset' '
 739        git checkout master &&
 740        git reset --hard &&
 741        test-tool chmtime =123456789 file3 &&
 742        git update-index --refresh &&
 743        HEAD=$(git rev-parse HEAD) &&
 744        set_fake_editor &&
 745        git rebase -i HEAD~4 &&
 746        test $HEAD = $(git rev-parse HEAD) &&
 747        MTIME=$(test-tool chmtime --get file3) &&
 748        test 123456789 = $MTIME
 749'
 750
 751test_expect_success 'reword' '
 752        git checkout -b reword-branch master &&
 753        set_fake_editor &&
 754        FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
 755        git show HEAD | grep "E changed" &&
 756        test $(git rev-parse master) != $(git rev-parse HEAD) &&
 757        test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
 758        FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
 759        git show HEAD^ | grep "D changed" &&
 760        FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
 761        git show HEAD~3 | grep "B changed" &&
 762        FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
 763        git show HEAD~2 | grep "C changed"
 764'
 765
 766test_expect_success 'rebase -i can copy notes' '
 767        git config notes.rewrite.rebase true &&
 768        git config notes.rewriteRef "refs/notes/*" &&
 769        test_commit n1 &&
 770        test_commit n2 &&
 771        test_commit n3 &&
 772        git notes add -m"a note" n3 &&
 773        set_fake_editor &&
 774        git rebase -i --onto n1 n2 &&
 775        test "a note" = "$(git notes show HEAD)"
 776'
 777
 778cat >expect <<EOF
 779an earlier note
 780
 781a note
 782EOF
 783
 784test_expect_success 'rebase -i can copy notes over a fixup' '
 785        git reset --hard n3 &&
 786        git notes add -m"an earlier note" n2 &&
 787        set_fake_editor &&
 788        GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 fixup 2" git rebase -i n1 &&
 789        git notes show > output &&
 790        test_cmp expect output
 791'
 792
 793test_expect_success 'rebase while detaching HEAD' '
 794        git symbolic-ref HEAD &&
 795        grandparent=$(git rev-parse HEAD~2) &&
 796        test_tick &&
 797        set_fake_editor &&
 798        FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 &&
 799        test $grandparent = $(git rev-parse HEAD~2) &&
 800        test_must_fail git symbolic-ref HEAD
 801'
 802
 803test_tick # Ensure that the rebased commits get a different timestamp.
 804test_expect_success 'always cherry-pick with --no-ff' '
 805        git checkout no-ff-branch &&
 806        git tag original-no-ff-branch &&
 807        set_fake_editor &&
 808        git rebase -i --no-ff A &&
 809        touch empty &&
 810        for p in 0 1 2
 811        do
 812                test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
 813                git diff HEAD~$p original-no-ff-branch~$p > out &&
 814                test_cmp empty out
 815        done &&
 816        test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
 817        git diff HEAD~3 original-no-ff-branch~3 > out &&
 818        test_cmp empty out
 819'
 820
 821test_expect_success 'set up commits with funny messages' '
 822        git checkout -b funny A &&
 823        echo >>file1 &&
 824        test_tick &&
 825        git commit -a -m "end with slash\\" &&
 826        echo >>file1 &&
 827        test_tick &&
 828        git commit -a -m "something (\000) that looks like octal" &&
 829        echo >>file1 &&
 830        test_tick &&
 831        git commit -a -m "something (\n) that looks like a newline" &&
 832        echo >>file1 &&
 833        test_tick &&
 834        git commit -a -m "another commit"
 835'
 836
 837test_expect_success 'rebase-i history with funny messages' '
 838        git rev-list A..funny >expect &&
 839        test_tick &&
 840        set_fake_editor &&
 841        FAKE_LINES="1 2 3 4" git rebase -i A &&
 842        git rev-list A.. >actual &&
 843        test_cmp expect actual
 844'
 845
 846test_expect_success 'prepare for rebase -i --exec' '
 847        git checkout master &&
 848        git checkout -b execute &&
 849        test_commit one_exec main.txt one_exec &&
 850        test_commit two_exec main.txt two_exec &&
 851        test_commit three_exec main.txt three_exec
 852'
 853
 854test_expect_success 'running "git rebase -i --exec git show HEAD"' '
 855        set_fake_editor &&
 856        git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
 857        (
 858                FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
 859                export FAKE_LINES &&
 860                git rebase -i HEAD~2 >expect
 861        ) &&
 862        sed -e "1,9d" expect >expected &&
 863        test_cmp expected actual
 864'
 865
 866test_expect_success 'running "git rebase --exec git show HEAD -i"' '
 867        git reset --hard execute &&
 868        set_fake_editor &&
 869        git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
 870        (
 871                FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
 872                export FAKE_LINES &&
 873                git rebase -i HEAD~2 >expect
 874        ) &&
 875        sed -e "1,9d" expect >expected &&
 876        test_cmp expected actual
 877'
 878
 879test_expect_success 'running "git rebase -ix git show HEAD"' '
 880        git reset --hard execute &&
 881        set_fake_editor &&
 882        git rebase -ix "git show HEAD" HEAD~2 >actual &&
 883        (
 884                FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
 885                export FAKE_LINES &&
 886                git rebase -i HEAD~2 >expect
 887        ) &&
 888        sed -e "1,9d" expect >expected &&
 889        test_cmp expected actual
 890'
 891
 892
 893test_expect_success 'rebase -ix with several <CMD>' '
 894        git reset --hard execute &&
 895        set_fake_editor &&
 896        git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
 897        (
 898                FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
 899                export FAKE_LINES &&
 900                git rebase -i HEAD~2 >expect
 901        ) &&
 902        sed -e "1,9d" expect >expected &&
 903        test_cmp expected actual
 904'
 905
 906test_expect_success 'rebase -ix with several instances of --exec' '
 907        git reset --hard execute &&
 908        set_fake_editor &&
 909        git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
 910        (
 911                FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
 912                                exec_git_show_HEAD exec_pwd" &&
 913                export FAKE_LINES &&
 914                git rebase -i HEAD~2 >expect
 915        ) &&
 916        sed -e "1,11d" expect >expected &&
 917        test_cmp expected actual
 918'
 919
 920test_expect_success C_LOCALE_OUTPUT 'rebase -ix with --autosquash' '
 921        git reset --hard execute &&
 922        git checkout -b autosquash &&
 923        echo second >second.txt &&
 924        git add second.txt &&
 925        git commit -m "fixup! two_exec" &&
 926        echo bis >bis.txt &&
 927        git add bis.txt &&
 928        git commit -m "fixup! two_exec" &&
 929        set_fake_editor &&
 930        (
 931                git checkout -b autosquash_actual &&
 932                git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual
 933        ) &&
 934        git checkout autosquash &&
 935        (
 936                git checkout -b autosquash_expected &&
 937                FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
 938                export FAKE_LINES &&
 939                git rebase -i HEAD~4 >expect
 940        ) &&
 941        sed -e "1,13d" expect >expected &&
 942        test_cmp expected actual
 943'
 944
 945test_expect_success 'rebase --exec works without -i ' '
 946        git reset --hard execute &&
 947        rm -rf exec_output &&
 948        EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output"  HEAD~2 2>actual &&
 949        test_i18ngrep  "Successfully rebased and updated" actual &&
 950        test_line_count = 2 exec_output &&
 951        test_path_is_missing invoked_editor
 952'
 953
 954test_expect_success 'rebase -i --exec without <CMD>' '
 955        git reset --hard execute &&
 956        set_fake_editor &&
 957        test_must_fail git rebase -i --exec 2>actual &&
 958        test_i18ngrep "requires a value" actual &&
 959        git checkout master
 960'
 961
 962test_expect_success 'rebase -i --root re-order and drop commits' '
 963        git checkout E &&
 964        set_fake_editor &&
 965        FAKE_LINES="3 1 2 5" git rebase -i --root &&
 966        test E = $(git cat-file commit HEAD | sed -ne \$p) &&
 967        test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
 968        test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
 969        test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
 970        test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
 971'
 972
 973test_expect_success 'rebase -i --root retain root commit author and message' '
 974        git checkout A &&
 975        echo B >file7 &&
 976        git add file7 &&
 977        GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
 978        set_fake_editor &&
 979        FAKE_LINES="2" git rebase -i --root &&
 980        git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
 981        git cat-file commit HEAD | grep -q "^different author$"
 982'
 983
 984test_expect_success 'rebase -i --root temporary sentinel commit' '
 985        git checkout B &&
 986        set_fake_editor &&
 987        test_must_fail env FAKE_LINES="2" git rebase -i --root &&
 988        git cat-file commit HEAD | grep "^tree 4b825dc642cb" &&
 989        git rebase --abort
 990'
 991
 992test_expect_success 'rebase -i --root fixup root commit' '
 993        git checkout B &&
 994        set_fake_editor &&
 995        FAKE_LINES="1 fixup 2" git rebase -i --root &&
 996        test A = $(git cat-file commit HEAD | sed -ne \$p) &&
 997        test B = $(git show HEAD:file1) &&
 998        test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
 999'
1000
1001test_expect_success 'rebase -i --root reword root commit' '
1002        test_when_finished "test_might_fail git rebase --abort" &&
1003        git checkout -b reword-root-branch master &&
1004        set_fake_editor &&
1005        FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
1006        git rebase -i --root &&
1007        git show HEAD^ | grep "A changed" &&
1008        test -z "$(git show -s --format=%p HEAD^)"
1009'
1010
1011test_expect_success 'rebase -i --root when root has untracked file confilct' '
1012        test_when_finished "reset_rebase" &&
1013        git checkout -b failing-root-pick A &&
1014        echo x >file2 &&
1015        git rm file1 &&
1016        git commit -m "remove file 1 add file 2" &&
1017        echo z >file1 &&
1018        set_fake_editor &&
1019        test_must_fail env FAKE_LINES="1 2" git rebase -i --root &&
1020        rm file1 &&
1021        git rebase --continue &&
1022        test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
1023        test "$(git rev-list --count HEAD)" = 2
1024'
1025
1026test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
1027        test_when_finished "reset_rebase" &&
1028        echo z>file1 &&
1029        set_fake_editor &&
1030        test_must_fail env FAKE_LINES="reword 1 2" \
1031                FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
1032        rm file1 &&
1033        FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue &&
1034        test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
1035        test "$(git rev-list --count HEAD)" = 2
1036'
1037
1038test_expect_success C_LOCALE_OUTPUT 'rebase --edit-todo does not work on non-interactive rebase' '
1039        git checkout reword-root-branch &&
1040        git reset --hard &&
1041        git checkout conflict-branch &&
1042        set_fake_editor &&
1043        test_must_fail git rebase --onto HEAD~2 HEAD~ &&
1044        test_must_fail git rebase --edit-todo &&
1045        git rebase --abort
1046'
1047
1048test_expect_success 'rebase --edit-todo can be used to modify todo' '
1049        git reset --hard &&
1050        git checkout no-conflict-branch^0 &&
1051        set_fake_editor &&
1052        FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
1053        FAKE_LINES="2 1" git rebase --edit-todo &&
1054        git rebase --continue &&
1055        test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1056        test L = $(git cat-file commit HEAD | sed -ne \$p)
1057'
1058
1059test_expect_success 'rebase -i produces readable reflog' '
1060        git reset --hard &&
1061        git branch -f branch-reflog-test H &&
1062        set_fake_editor &&
1063        git rebase -i --onto I F branch-reflog-test &&
1064        cat >expect <<-\EOF &&
1065        rebase -i (finish): returning to refs/heads/branch-reflog-test
1066        rebase -i (pick): H
1067        rebase -i (pick): G
1068        rebase -i (start): checkout I
1069        EOF
1070        git reflog -n4 HEAD |
1071        sed "s/[^:]*: //" >actual &&
1072        test_cmp expect actual
1073'
1074
1075test_expect_success 'rebase -i respects core.commentchar' '
1076        git reset --hard &&
1077        git checkout E^0 &&
1078        test_config core.commentchar "\\" &&
1079        write_script remove-all-but-first.sh <<-\EOF &&
1080        sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
1081        mv "$1.tmp" "$1"
1082        EOF
1083        test_set_editor "$(pwd)/remove-all-but-first.sh" &&
1084        git rebase -i B &&
1085        test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1086'
1087
1088test_expect_success 'rebase -i respects core.commentchar=auto' '
1089        test_config core.commentchar auto &&
1090        write_script copy-edit-script.sh <<-\EOF &&
1091        cp "$1" edit-script
1092        EOF
1093        test_set_editor "$(pwd)/copy-edit-script.sh" &&
1094        test_when_finished "git rebase --abort || :" &&
1095        git rebase -i HEAD^ &&
1096        test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
1097'
1098
1099test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
1100        test_when_finished "git branch -D torebase" &&
1101        git checkout -b torebase branch1 &&
1102        upstream=$(git rev-parse ":/J") &&
1103        onto=$(git rev-parse ":/A") &&
1104        git rebase --onto $onto $upstream &&
1105        git reset --hard branch1 &&
1106        git rebase --onto ":/A" ":/J" &&
1107        git checkout branch1
1108'
1109
1110test_expect_success 'rebase -i with --strategy and -X' '
1111        git checkout -b conflict-merge-use-theirs conflict-branch &&
1112        git reset --hard HEAD^ &&
1113        echo five >conflict &&
1114        echo Z >file1 &&
1115        git commit -a -m "one file conflict" &&
1116        EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
1117        test $(git show conflict-branch:conflict) = $(cat conflict) &&
1118        test $(cat file1) = Z
1119'
1120
1121test_expect_success 'interrupted rebase -i with --strategy and -X' '
1122        git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
1123        git reset --hard HEAD^ &&
1124        >breakpoint &&
1125        git add breakpoint &&
1126        git commit -m "breakpoint for interactive mode" &&
1127        echo five >conflict &&
1128        echo Z >file1 &&
1129        git commit -a -m "one file conflict" &&
1130        set_fake_editor &&
1131        FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive -Xours conflict-branch &&
1132        git rebase --continue &&
1133        test $(git show conflict-branch:conflict) = $(cat conflict) &&
1134        test $(cat file1) = Z
1135'
1136
1137test_expect_success 'rebase -i error on commits with \ in message' '
1138        current_head=$(git rev-parse HEAD) &&
1139        test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
1140        test_commit TO-REMOVE will-conflict old-content &&
1141        test_commit "\temp" will-conflict new-content dummy &&
1142        test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
1143        test_expect_code 1 grep  "      emp" error
1144'
1145
1146test_expect_success 'short SHA-1 setup' '
1147        test_when_finished "git checkout master" &&
1148        git checkout --orphan collide &&
1149        git rm -rf . &&
1150        (
1151        unset test_tick &&
1152        test_commit collide1 collide &&
1153        test_commit --notick collide2 collide &&
1154        test_commit --notick collide3 collide
1155        )
1156'
1157
1158test_expect_success 'short SHA-1 collide' '
1159        test_when_finished "reset_rebase && git checkout master" &&
1160        git checkout collide &&
1161        (
1162        unset test_tick &&
1163        test_tick &&
1164        set_fake_editor &&
1165        FAKE_COMMIT_MESSAGE="collide2 ac4f2ee" \
1166        FAKE_LINES="reword 1 2" git rebase -i HEAD~2
1167        )
1168'
1169
1170test_expect_success 'respect core.abbrev' '
1171        git config core.abbrev 12 &&
1172        set_cat_todo_editor &&
1173        test_must_fail git rebase -i HEAD~4 >todo-list &&
1174        test 4 = $(grep -c "pick [0-9a-f]\{12,\}" todo-list)
1175'
1176
1177test_expect_success 'todo count' '
1178        write_script dump-raw.sh <<-\EOF &&
1179                cat "$1"
1180        EOF
1181        test_set_editor "$(pwd)/dump-raw.sh" &&
1182        git rebase -i HEAD~4 >actual &&
1183        test_i18ngrep "^# Rebase ..* onto ..* ([0-9]" actual
1184'
1185
1186test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
1187        git checkout --force branch2 &&
1188        git clean -f &&
1189        set_fake_editor &&
1190        FAKE_LINES="edit 1 2" git rebase -i A &&
1191        test_cmp_rev HEAD F &&
1192        test_path_is_missing file6 &&
1193        >file6 &&
1194        test_must_fail git rebase --continue &&
1195        test_cmp_rev HEAD F &&
1196        rm file6 &&
1197        git rebase --continue &&
1198        test_cmp_rev HEAD I
1199'
1200
1201test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
1202        git checkout --force branch2 &&
1203        git clean -f &&
1204        git tag original-branch2 &&
1205        set_fake_editor &&
1206        FAKE_LINES="edit 1 squash 2" git rebase -i A &&
1207        test_cmp_rev HEAD F &&
1208        test_path_is_missing file6 &&
1209        >file6 &&
1210        test_must_fail git rebase --continue &&
1211        test_cmp_rev HEAD F &&
1212        rm file6 &&
1213        git rebase --continue &&
1214        test $(git cat-file commit HEAD | sed -ne \$p) = I &&
1215        git reset --hard original-branch2
1216'
1217
1218test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
1219        git checkout --force branch2 &&
1220        git clean -f &&
1221        set_fake_editor &&
1222        FAKE_LINES="edit 1 2" git rebase -i --no-ff A &&
1223        test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1224        test_path_is_missing file6 &&
1225        >file6 &&
1226        test_must_fail git rebase --continue &&
1227        test $(git cat-file commit HEAD | sed -ne \$p) = F &&
1228        rm file6 &&
1229        git rebase --continue &&
1230        test $(git cat-file commit HEAD | sed -ne \$p) = I
1231'
1232
1233test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
1234        git checkout -b commit-to-skip &&
1235        for double in X 3 1
1236        do
1237                test_seq 5 | sed "s/$double/&&/" >seq &&
1238                git add seq &&
1239                test_tick &&
1240                git commit -m seq-$double
1241        done &&
1242        git tag seq-onto &&
1243        git reset --hard HEAD~2 &&
1244        git cherry-pick seq-onto &&
1245        set_fake_editor &&
1246        test_must_fail env FAKE_LINES= git rebase -i seq-onto &&
1247        test -d .git/rebase-merge &&
1248        git rebase --continue &&
1249        git diff --exit-code seq-onto &&
1250        test ! -d .git/rebase-merge &&
1251        test ! -f .git/CHERRY_PICK_HEAD
1252'
1253
1254rebase_setup_and_clean () {
1255        test_when_finished "
1256                git checkout master &&
1257                test_might_fail git branch -D $1 &&
1258                test_might_fail git rebase --abort
1259        " &&
1260        git checkout -b $1 master
1261}
1262
1263test_expect_success 'drop' '
1264        rebase_setup_and_clean drop-test &&
1265        set_fake_editor &&
1266        FAKE_LINES="1 drop 2 3 drop 4 5" git rebase -i --root &&
1267        test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1268        test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
1269        test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
1270'
1271
1272test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
1273        test_config rebase.missingCommitsCheck ignore &&
1274        rebase_setup_and_clean missing-commit &&
1275        set_fake_editor &&
1276        FAKE_LINES="1 2 3 4" \
1277                git rebase -i --root 2>actual &&
1278        test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1279        test_i18ngrep \
1280                "Successfully rebased and updated refs/heads/missing-commit" \
1281                actual
1282'
1283
1284cat >expect <<EOF
1285Warning: some commits may have been dropped accidentally.
1286Dropped commits (newer to older):
1287 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1288To avoid this message, use "drop" to explicitly remove a commit.
1289
1290Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1291The possible behaviours are: ignore, warn, error.
1292
1293Rebasing (1/4)
1294Rebasing (2/4)
1295Rebasing (3/4)
1296Rebasing (4/4)
1297Successfully rebased and updated refs/heads/missing-commit.
1298EOF
1299
1300cr_to_nl () {
1301        tr '\015' '\012'
1302}
1303
1304test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
1305        test_config rebase.missingCommitsCheck warn &&
1306        rebase_setup_and_clean missing-commit &&
1307        set_fake_editor &&
1308        FAKE_LINES="1 2 3 4" \
1309                git rebase -i --root 2>actual.2 &&
1310        cr_to_nl <actual.2 >actual &&
1311        test_i18ncmp expect actual &&
1312        test D = $(git cat-file commit HEAD | sed -ne \$p)
1313'
1314
1315cat >expect <<EOF
1316Warning: some commits may have been dropped accidentally.
1317Dropped commits (newer to older):
1318 - $(git rev-list --pretty=oneline --abbrev-commit -1 master)
1319 - $(git rev-list --pretty=oneline --abbrev-commit -1 master~2)
1320To avoid this message, use "drop" to explicitly remove a commit.
1321
1322Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1323The possible behaviours are: ignore, warn, error.
1324
1325You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.
1326Or you can abort the rebase with 'git rebase --abort'.
1327EOF
1328
1329test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
1330        test_config rebase.missingCommitsCheck error &&
1331        rebase_setup_and_clean missing-commit &&
1332        set_fake_editor &&
1333        test_must_fail env FAKE_LINES="1 2 4" \
1334                git rebase -i --root 2>actual &&
1335        test_i18ncmp expect actual &&
1336        cp .git/rebase-merge/git-rebase-todo.backup \
1337                .git/rebase-merge/git-rebase-todo &&
1338        FAKE_LINES="1 2 drop 3 4 drop 5" \
1339                git rebase --edit-todo &&
1340        git rebase --continue &&
1341        test D = $(git cat-file commit HEAD | sed -ne \$p) &&
1342        test B = $(git cat-file commit HEAD^ | sed -ne \$p)
1343'
1344
1345test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
1346        rebase_setup_and_clean abbrevcmd &&
1347        test_commit "first" file1.txt "first line" first &&
1348        test_commit "second" file1.txt "another line" second &&
1349        test_commit "fixup! first" file2.txt "first line again" first_fixup &&
1350        test_commit "squash! second" file1.txt "another line here" second_squash &&
1351        cat >expected <<-EOF &&
1352        p $(git rev-list --abbrev-commit -1 first) first
1353        f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
1354        x git show HEAD
1355        p $(git rev-list --abbrev-commit -1 second) second
1356        s $(git rev-list --abbrev-commit -1 second_squash) squash! second
1357        x git show HEAD
1358        EOF
1359        git checkout abbrevcmd &&
1360        set_cat_todo_editor &&
1361        test_config rebase.abbreviateCommands true &&
1362        test_must_fail git rebase -i --exec "git show HEAD" \
1363                --autosquash master >actual &&
1364        test_cmp expected actual
1365'
1366
1367test_expect_success 'static check of bad command' '
1368        rebase_setup_and_clean bad-cmd &&
1369        set_fake_editor &&
1370        test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
1371                git rebase -i --root 2>actual &&
1372        test_i18ngrep "badcmd $(git rev-list --oneline -1 master~1)" actual &&
1373        test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1374        FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo &&
1375        git rebase --continue &&
1376        test E = $(git cat-file commit HEAD | sed -ne \$p) &&
1377        test C = $(git cat-file commit HEAD^ | sed -ne \$p)
1378'
1379
1380test_expect_success 'tabs and spaces are accepted in the todolist' '
1381        rebase_setup_and_clean indented-comment &&
1382        write_script add-indent.sh <<-\EOF &&
1383        (
1384                # Turn single spaces into space/tab mix
1385                sed "1s/ /      /g; 2s/ /  /g; 3s/ /    /g" "$1"
1386                printf "\n\t# comment\n #more\n\t # comment\n"
1387        ) >"$1.new"
1388        mv "$1.new" "$1"
1389        EOF
1390        test_set_editor "$(pwd)/add-indent.sh" &&
1391        git rebase -i HEAD^^^ &&
1392        test E = $(git cat-file commit HEAD | sed -ne \$p)
1393'
1394
1395test_expect_success 'static check of bad SHA-1' '
1396        rebase_setup_and_clean bad-sha &&
1397        set_fake_editor &&
1398        test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
1399                git rebase -i --root 2>actual &&
1400        test_i18ngrep "edit XXXXXXX False commit" actual &&
1401        test_i18ngrep "You can fix this with .git rebase --edit-todo.." actual &&
1402        FAKE_LINES="1 2 4 5 6" git rebase --edit-todo &&
1403        git rebase --continue &&
1404        test E = $(git cat-file commit HEAD | sed -ne \$p)
1405'
1406
1407test_expect_success 'editor saves as CR/LF' '
1408        git checkout -b with-crlf &&
1409        write_script add-crs.sh <<-\EOF &&
1410        sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
1411        mv -f "$1".new "$1"
1412        EOF
1413        (
1414                test_set_editor "$(pwd)/add-crs.sh" &&
1415                git rebase -i HEAD^
1416        )
1417'
1418
1419SQ="'"
1420test_expect_success 'rebase -i --gpg-sign=<key-id>' '
1421        test_when_finished "test_might_fail git rebase --abort" &&
1422        set_fake_editor &&
1423        FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1424                >out 2>err &&
1425        test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1426'
1427
1428test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
1429        test_when_finished "test_might_fail git rebase --abort" &&
1430        test_config commit.gpgsign true &&
1431        set_fake_editor &&
1432        FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" HEAD^ \
1433                >out 2>err &&
1434        test_i18ngrep "$SQ-S\"S I Gner\"$SQ" err
1435'
1436
1437test_done