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