778daf419c11fc550d968ef9d5ac780e321ad7cb
   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'
  11. ./test-lib.sh
  12
  13. "$TEST_DIRECTORY"/lib-rebase.sh
  14
  15set_fake_editor
  16
  17# set up two branches like this:
  18#
  19# A - B - C - D - E     (master)
  20#   \
  21#     F - G - H         (branch1)
  22#       \
  23#         I             (branch2)
  24#
  25# where A, B, D and G touch the same file.
  26
  27test_expect_success 'setup' '
  28        test_commit A file1 &&
  29        test_commit B file1 &&
  30        test_commit C file2 &&
  31        test_commit D file1 &&
  32        test_commit E file3 &&
  33        git checkout -b branch1 A &&
  34        test_commit F file4 &&
  35        test_commit G file1 &&
  36        test_commit H file5 &&
  37        git checkout -b branch2 F &&
  38        test_commit I file6
  39'
  40
  41test_expect_success 'no changes are a nop' '
  42        git rebase -i F &&
  43        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
  44        test $(git rev-parse I) = $(git rev-parse HEAD)
  45'
  46
  47test_expect_success 'test the [branch] option' '
  48        git checkout -b dead-end &&
  49        git rm file6 &&
  50        git commit -m "stop here" &&
  51        git rebase -i F branch2 &&
  52        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
  53        test $(git rev-parse I) = $(git rev-parse branch2) &&
  54        test $(git rev-parse I) = $(git rev-parse HEAD)
  55'
  56
  57test_expect_success 'test --onto <branch>' '
  58        git checkout -b test-onto branch2 &&
  59        git rebase -i --onto branch1 F &&
  60        test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
  61        test $(git rev-parse HEAD^) = $(git rev-parse branch1) &&
  62        test $(git rev-parse I) = $(git rev-parse branch2)
  63'
  64
  65test_expect_success 'rebase on top of a non-conflicting commit' '
  66        git checkout branch1 &&
  67        git tag original-branch1 &&
  68        git rebase -i branch2 &&
  69        test file6 = $(git diff --name-only original-branch1) &&
  70        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
  71        test $(git rev-parse I) = $(git rev-parse branch2) &&
  72        test $(git rev-parse I) = $(git rev-parse HEAD~2)
  73'
  74
  75test_expect_success 'reflog for the branch shows state before rebase' '
  76        test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
  77'
  78
  79test_expect_success 'exchange two commits' '
  80        FAKE_LINES="2 1" git rebase -i HEAD~2 &&
  81        test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
  82        test G = $(git cat-file commit HEAD | sed -ne \$p)
  83'
  84
  85cat > expect << EOF
  86diff --git a/file1 b/file1
  87index f70f10e..fd79235 100644
  88--- a/file1
  89+++ b/file1
  90@@ -1 +1 @@
  91-A
  92+G
  93EOF
  94
  95cat > expect2 << EOF
  96<<<<<<< HEAD
  97D
  98=======
  99G
 100>>>>>>> 91201e5... G
 101EOF
 102
 103test_expect_success 'stop on conflicting pick' '
 104        git tag new-branch1 &&
 105        test_must_fail git rebase -i master &&
 106        test "$(git rev-parse HEAD~3)" = "$(git rev-parse master)" &&
 107        test_cmp expect .git/rebase-merge/patch &&
 108        test_cmp expect2 file1 &&
 109        test "$(git diff --name-status |
 110                sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
 111        test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
 112        test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
 113'
 114
 115test_expect_success 'abort' '
 116        git rebase --abort &&
 117        test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
 118        test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
 119        ! test -d .git/rebase-merge
 120'
 121
 122test_expect_success 'retain authorship' '
 123        echo A > file7 &&
 124        git add file7 &&
 125        test_tick &&
 126        GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
 127        git tag twerp &&
 128        git rebase -i --onto master HEAD^ &&
 129        git show HEAD | grep "^Author: Twerp Snog"
 130'
 131
 132test_expect_success 'squash' '
 133        git reset --hard twerp &&
 134        echo B > file7 &&
 135        test_tick &&
 136        GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
 137        echo "******************************" &&
 138        FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
 139        test B = $(cat file7) &&
 140        test $(git rev-parse HEAD^) = $(git rev-parse master)
 141'
 142
 143test_expect_success 'retain authorship when squashing' '
 144        git show HEAD | grep "^Author: Twerp Snog"
 145'
 146
 147test_expect_success '-p handles "no changes" gracefully' '
 148        HEAD=$(git rev-parse HEAD) &&
 149        git rebase -i -p HEAD^ &&
 150        git update-index --refresh &&
 151        git diff-files --quiet &&
 152        git diff-index --quiet --cached HEAD -- &&
 153        test $HEAD = $(git rev-parse HEAD)
 154'
 155
 156test_expect_success 'preserve merges with -p' '
 157        git checkout -b to-be-preserved master^ &&
 158        : > unrelated-file &&
 159        git add unrelated-file &&
 160        test_tick &&
 161        git commit -m "unrelated" &&
 162        git checkout -b another-branch master &&
 163        echo B > file1 &&
 164        test_tick &&
 165        git commit -m J file1 &&
 166        test_tick &&
 167        git merge to-be-preserved &&
 168        echo C > file1 &&
 169        test_tick &&
 170        git commit -m K file1 &&
 171        echo D > file1 &&
 172        test_tick &&
 173        git commit -m L1 file1 &&
 174        git checkout HEAD^ &&
 175        echo 1 > unrelated-file &&
 176        test_tick &&
 177        git commit -m L2 unrelated-file &&
 178        test_tick &&
 179        git merge another-branch &&
 180        echo E > file1 &&
 181        test_tick &&
 182        git commit -m M file1 &&
 183        git checkout -b to-be-rebased &&
 184        test_tick &&
 185        git rebase -i -p --onto branch1 master &&
 186        git update-index --refresh &&
 187        git diff-files --quiet &&
 188        git diff-index --quiet --cached HEAD -- &&
 189        test $(git rev-parse HEAD~6) = $(git rev-parse branch1) &&
 190        test $(git rev-parse HEAD~4^2) = $(git rev-parse to-be-preserved) &&
 191        test $(git rev-parse HEAD^^2^) = $(git rev-parse HEAD^^^) &&
 192        test $(git show HEAD~5:file1) = B &&
 193        test $(git show HEAD~3:file1) = C &&
 194        test $(git show HEAD:file1) = E &&
 195        test $(git show HEAD:unrelated-file) = 1
 196'
 197
 198test_expect_success 'edit ancestor with -p' '
 199        FAKE_LINES="1 edit 2 3 4" git rebase -i -p HEAD~3 &&
 200        echo 2 > unrelated-file &&
 201        test_tick &&
 202        git commit -m L2-modified --amend unrelated-file &&
 203        git rebase --continue &&
 204        git update-index --refresh &&
 205        git diff-files --quiet &&
 206        git diff-index --quiet --cached HEAD -- &&
 207        test $(git show HEAD:unrelated-file) = 2
 208'
 209
 210test_expect_success '--continue tries to commit' '
 211        test_tick &&
 212        test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
 213        echo resolved > file1 &&
 214        git add file1 &&
 215        FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
 216        test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
 217        git show HEAD | grep chouette
 218'
 219
 220test_expect_success 'verbose flag is heeded, even after --continue' '
 221        git reset --hard HEAD@{1} &&
 222        test_tick &&
 223        test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
 224        echo resolved > file1 &&
 225        git add file1 &&
 226        git rebase --continue > output &&
 227        grep "^ file1 |    2 +-$" output
 228'
 229
 230test_expect_success 'multi-squash only fires up editor once' '
 231        base=$(git rev-parse HEAD~4) &&
 232        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
 233                git rebase -i $base &&
 234        test $base = $(git rev-parse HEAD^) &&
 235        test 1 = $(git show | grep ONCE | wc -l)
 236'
 237
 238test_expect_success 'squash works as expected' '
 239        for n in one two three four
 240        do
 241                echo $n >> file$n &&
 242                git add file$n &&
 243                git commit -m $n
 244        done &&
 245        one=$(git rev-parse HEAD~3) &&
 246        FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
 247        test $one = $(git rev-parse HEAD~2)
 248'
 249
 250test_expect_success 'interrupted squash works as expected' '
 251        for n in one two three four
 252        do
 253                echo $n >> conflict &&
 254                git add conflict &&
 255                git commit -m $n
 256        done &&
 257        one=$(git rev-parse HEAD~3) &&
 258        (
 259                FAKE_LINES="1 squash 3 2" &&
 260                export FAKE_LINES &&
 261                test_must_fail git rebase -i HEAD~3
 262        ) &&
 263        (echo one; echo two; echo four) > conflict &&
 264        git add conflict &&
 265        test_must_fail git rebase --continue &&
 266        echo resolved > conflict &&
 267        git add conflict &&
 268        git rebase --continue &&
 269        test $one = $(git rev-parse HEAD~2)
 270'
 271
 272test_expect_success 'interrupted squash works as expected (case 2)' '
 273        for n in one two three four
 274        do
 275                echo $n >> conflict &&
 276                git add conflict &&
 277                git commit -m $n
 278        done &&
 279        one=$(git rev-parse HEAD~3) &&
 280        (
 281                FAKE_LINES="3 squash 1 2" &&
 282                export FAKE_LINES &&
 283                test_must_fail git rebase -i HEAD~3
 284        ) &&
 285        (echo one; echo four) > conflict &&
 286        git add conflict &&
 287        test_must_fail git rebase --continue &&
 288        (echo one; echo two; echo four) > conflict &&
 289        git add conflict &&
 290        test_must_fail git rebase --continue &&
 291        echo resolved > conflict &&
 292        git add conflict &&
 293        git rebase --continue &&
 294        test $one = $(git rev-parse HEAD~2)
 295'
 296
 297test_expect_success 'ignore patch if in upstream' '
 298        HEAD=$(git rev-parse HEAD) &&
 299        git checkout -b has-cherry-picked HEAD^ &&
 300        echo unrelated > file7 &&
 301        git add file7 &&
 302        test_tick &&
 303        git commit -m "unrelated change" &&
 304        git cherry-pick $HEAD &&
 305        EXPECT_COUNT=1 git rebase -i $HEAD &&
 306        test $HEAD = $(git rev-parse HEAD^)
 307'
 308
 309test_expect_success '--continue tries to commit, even for "edit"' '
 310        parent=$(git rev-parse HEAD^) &&
 311        test_tick &&
 312        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 313        echo edited > file7 &&
 314        git add file7 &&
 315        FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
 316        test edited = $(git show HEAD:file7) &&
 317        git show HEAD | grep chouette &&
 318        test $parent = $(git rev-parse HEAD^)
 319'
 320
 321test_expect_success 'aborted --continue does not squash commits after "edit"' '
 322        old=$(git rev-parse HEAD) &&
 323        test_tick &&
 324        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 325        echo "edited again" > file7 &&
 326        git add file7 &&
 327        (
 328                FAKE_COMMIT_MESSAGE=" " &&
 329                export FAKE_COMMIT_MESSAGE &&
 330                test_must_fail git rebase --continue
 331        ) &&
 332        test $old = $(git rev-parse HEAD) &&
 333        git rebase --abort
 334'
 335
 336test_expect_success 'auto-amend only edited commits after "edit"' '
 337        test_tick &&
 338        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 339        echo "edited again" > file7 &&
 340        git add file7 &&
 341        FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
 342        echo "and again" > file7 &&
 343        git add file7 &&
 344        test_tick &&
 345        (
 346                FAKE_COMMIT_MESSAGE="and again" &&
 347                export FAKE_COMMIT_MESSAGE &&
 348                test_must_fail git rebase --continue
 349        ) &&
 350        git rebase --abort
 351'
 352
 353test_expect_success 'rebase a detached HEAD' '
 354        grandparent=$(git rev-parse HEAD~2) &&
 355        git checkout $(git rev-parse HEAD) &&
 356        test_tick &&
 357        FAKE_LINES="2 1" git rebase -i HEAD~2 &&
 358        test $grandparent = $(git rev-parse HEAD~2)
 359'
 360
 361test_expect_success 'rebase a commit violating pre-commit' '
 362
 363        mkdir -p .git/hooks &&
 364        PRE_COMMIT=.git/hooks/pre-commit &&
 365        echo "#!/bin/sh" > $PRE_COMMIT &&
 366        echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
 367        chmod a+x $PRE_COMMIT &&
 368        echo "monde! " >> file1 &&
 369        test_tick &&
 370        test_must_fail git commit -m doesnt-verify file1 &&
 371        git commit -m doesnt-verify --no-verify file1 &&
 372        test_tick &&
 373        FAKE_LINES=2 git rebase -i HEAD~2
 374
 375'
 376
 377test_expect_success 'rebase with a file named HEAD in worktree' '
 378
 379        rm -fr .git/hooks &&
 380        git reset --hard &&
 381        git checkout -b branch3 A &&
 382
 383        (
 384                GIT_AUTHOR_NAME="Squashed Away" &&
 385                export GIT_AUTHOR_NAME &&
 386                >HEAD &&
 387                git add HEAD &&
 388                git commit -m "Add head" &&
 389                >BODY &&
 390                git add BODY &&
 391                git commit -m "Add body"
 392        ) &&
 393
 394        FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
 395        test "$(git show -s --pretty=format:%an)" = "Squashed Away"
 396
 397'
 398
 399test_expect_success 'do "noop" when there is nothing to cherry-pick' '
 400
 401        git checkout -b branch4 HEAD &&
 402        GIT_EDITOR=: git commit --amend \
 403                --author="Somebody else <somebody@else.com>" 
 404        test $(git rev-parse branch3) != $(git rev-parse branch4) &&
 405        git rebase -i branch3 &&
 406        test $(git rev-parse branch3) = $(git rev-parse branch4)
 407
 408'
 409
 410test_expect_success 'submodule rebase setup' '
 411        git checkout A &&
 412        mkdir sub &&
 413        (
 414                cd sub && git init && >elif &&
 415                git add elif && git commit -m "submodule initial"
 416        ) &&
 417        echo 1 >file1 &&
 418        git add file1 sub
 419        test_tick &&
 420        git commit -m "One" &&
 421        echo 2 >file1 &&
 422        test_tick &&
 423        git commit -a -m "Two" &&
 424        (
 425                cd sub && echo 3 >elif &&
 426                git commit -a -m "submodule second"
 427        ) &&
 428        test_tick &&
 429        git commit -a -m "Three changes submodule"
 430'
 431
 432test_expect_success 'submodule rebase -i' '
 433        FAKE_LINES="1 squash 2 3" git rebase -i A
 434'
 435
 436test_expect_success 'avoid unnecessary reset' '
 437        git checkout master &&
 438        test-chmtime =123456789 file3 &&
 439        git update-index --refresh &&
 440        HEAD=$(git rev-parse HEAD) &&
 441        git rebase -i HEAD~4 &&
 442        test $HEAD = $(git rev-parse HEAD) &&
 443        MTIME=$(test-chmtime -v +0 file3 | sed 's/[^0-9].*$//') &&
 444        test 123456789 = $MTIME
 445'
 446
 447test_expect_success 'reword' '
 448        git checkout -b reword-branch master &&
 449        FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" git rebase -i A &&
 450        git show HEAD | grep "E changed" &&
 451        test $(git rev-parse master) != $(git rev-parse HEAD) &&
 452        test $(git rev-parse master^) = $(git rev-parse HEAD^) &&
 453        FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" git rebase -i A &&
 454        git show HEAD^ | grep "D changed" &&
 455        FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" git rebase -i A &&
 456        git show HEAD~3 | grep "B changed" &&
 457        FAKE_LINES="1 reword 2 3 4" FAKE_COMMIT_MESSAGE="C changed" git rebase -i A &&
 458        git show HEAD~2 | grep "C changed"
 459'
 460
 461test_done