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