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