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