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