t / t3404-rebase-interactive.shon commit Merge branch 'lt/racy-empty' into maint (c636d0e)
   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# set up two branches like this:
  14#
  15# A - B - C - D - E
  16#   \
  17#     F - G - H
  18#       \
  19#         I
  20#
  21# where B, D and G touch the same file.
  22
  23test_expect_success 'setup' '
  24        : > file1 &&
  25        git add file1 &&
  26        test_tick &&
  27        git commit -m A &&
  28        git tag A &&
  29        echo 1 > file1 &&
  30        test_tick &&
  31        git commit -m B file1 &&
  32        : > file2 &&
  33        git add file2 &&
  34        test_tick &&
  35        git commit -m C &&
  36        echo 2 > file1 &&
  37        test_tick &&
  38        git commit -m D file1 &&
  39        : > file3 &&
  40        git add file3 &&
  41        test_tick &&
  42        git commit -m E &&
  43        git checkout -b branch1 A &&
  44        : > file4 &&
  45        git add file4 &&
  46        test_tick &&
  47        git commit -m F &&
  48        git tag F &&
  49        echo 3 > file1 &&
  50        test_tick &&
  51        git commit -m G file1 &&
  52        : > file5 &&
  53        git add file5 &&
  54        test_tick &&
  55        git commit -m H &&
  56        git checkout -b branch2 F &&
  57        : > file6 &&
  58        git add file6 &&
  59        test_tick &&
  60        git commit -m I &&
  61        git tag I
  62'
  63
  64echo "#!$SHELL_PATH" >fake-editor.sh
  65cat >> fake-editor.sh <<\EOF
  66case "$1" in
  67*/COMMIT_EDITMSG)
  68        test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
  69        test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
  70        exit
  71        ;;
  72esac
  73test -z "$EXPECT_COUNT" ||
  74        test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
  75        exit
  76test -z "$FAKE_LINES" && exit
  77grep -v '^#' < "$1" > "$1".tmp
  78rm -f "$1"
  79cat "$1".tmp
  80action=pick
  81for line in $FAKE_LINES; do
  82        case $line in
  83        squash|edit)
  84                action="$line";;
  85        *)
  86                echo sed -n "${line}s/^pick/$action/p"
  87                sed -n "${line}p" < "$1".tmp
  88                sed -n "${line}s/^pick/$action/p" < "$1".tmp >> "$1"
  89                action=pick;;
  90        esac
  91done
  92EOF
  93
  94test_set_editor "$(pwd)/fake-editor.sh"
  95chmod a+x fake-editor.sh
  96
  97test_expect_success 'no changes are a nop' '
  98        git rebase -i F &&
  99        test $(git rev-parse I) = $(git rev-parse HEAD)
 100'
 101
 102test_expect_success 'test the [branch] option' '
 103        git checkout -b dead-end &&
 104        git rm file6 &&
 105        git commit -m "stop here" &&
 106        git rebase -i F branch2 &&
 107        test $(git rev-parse I) = $(git rev-parse HEAD)
 108'
 109
 110test_expect_success 'rebase on top of a non-conflicting commit' '
 111        git checkout branch1 &&
 112        git tag original-branch1 &&
 113        git rebase -i branch2 &&
 114        test file6 = $(git diff --name-only original-branch1) &&
 115        test $(git rev-parse I) = $(git rev-parse HEAD~2)
 116'
 117
 118test_expect_success 'reflog for the branch shows state before rebase' '
 119        test $(git rev-parse branch1@{1}) = $(git rev-parse original-branch1)
 120'
 121
 122test_expect_success 'exchange two commits' '
 123        FAKE_LINES="2 1" git rebase -i HEAD~2 &&
 124        test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
 125        test G = $(git cat-file commit HEAD | sed -ne \$p)
 126'
 127
 128cat > expect << EOF
 129diff --git a/file1 b/file1
 130index e69de29..00750ed 100644
 131--- a/file1
 132+++ b/file1
 133@@ -0,0 +1 @@
 134+3
 135EOF
 136
 137cat > expect2 << EOF
 138<<<<<<< HEAD:file1
 1392
 140=======
 1413
 142>>>>>>> b7ca976... G:file1
 143EOF
 144
 145test_expect_success 'stop on conflicting pick' '
 146        git tag new-branch1 &&
 147        ! git rebase -i master &&
 148        test_cmp expect .git/.dotest-merge/patch &&
 149        test_cmp expect2 file1 &&
 150        test 4 = $(grep -v "^#" < .git/.dotest-merge/done | wc -l) &&
 151        test 0 = $(grep -c "^[^#]" < .git/.dotest-merge/git-rebase-todo)
 152'
 153
 154test_expect_success 'abort' '
 155        git rebase --abort &&
 156        test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
 157        ! test -d .git/.dotest-merge
 158'
 159
 160test_expect_success 'retain authorship' '
 161        echo A > file7 &&
 162        git add file7 &&
 163        test_tick &&
 164        GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
 165        git tag twerp &&
 166        git rebase -i --onto master HEAD^ &&
 167        git show HEAD | grep "^Author: Twerp Snog"
 168'
 169
 170test_expect_success 'squash' '
 171        git reset --hard twerp &&
 172        echo B > file7 &&
 173        test_tick &&
 174        GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
 175        echo "******************************" &&
 176        FAKE_LINES="1 squash 2" git rebase -i --onto master HEAD~2 &&
 177        test B = $(cat file7) &&
 178        test $(git rev-parse HEAD^) = $(git rev-parse master)
 179'
 180
 181test_expect_success 'retain authorship when squashing' '
 182        git show HEAD | grep "^Author: Twerp Snog"
 183'
 184
 185test_expect_success '-p handles "no changes" gracefully' '
 186        HEAD=$(git rev-parse HEAD) &&
 187        git rebase -i -p HEAD^ &&
 188        test $HEAD = $(git rev-parse HEAD)
 189'
 190
 191test_expect_success 'preserve merges with -p' '
 192        git checkout -b to-be-preserved master^ &&
 193        : > unrelated-file &&
 194        git add unrelated-file &&
 195        test_tick &&
 196        git commit -m "unrelated" &&
 197        git checkout -b to-be-rebased master &&
 198        echo B > file1 &&
 199        test_tick &&
 200        git commit -m J file1 &&
 201        test_tick &&
 202        git merge to-be-preserved &&
 203        echo C > file1 &&
 204        test_tick &&
 205        git commit -m K file1 &&
 206        test_tick &&
 207        git rebase -i -p --onto branch1 master &&
 208        test $(git rev-parse HEAD^^2) = $(git rev-parse to-be-preserved) &&
 209        test $(git rev-parse HEAD~3) = $(git rev-parse branch1) &&
 210        test $(git show HEAD:file1) = C &&
 211        test $(git show HEAD~2:file1) = B
 212'
 213
 214test_expect_success '--continue tries to commit' '
 215        test_tick &&
 216        ! git rebase -i --onto new-branch1 HEAD^ &&
 217        echo resolved > file1 &&
 218        git add file1 &&
 219        FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
 220        test $(git rev-parse HEAD^) = $(git rev-parse new-branch1) &&
 221        git show HEAD | grep chouette
 222'
 223
 224test_expect_success 'verbose flag is heeded, even after --continue' '
 225        git reset --hard HEAD@{1} &&
 226        test_tick &&
 227        ! git rebase -v -i --onto new-branch1 HEAD^ &&
 228        echo resolved > file1 &&
 229        git add file1 &&
 230        git rebase --continue > output &&
 231        grep "^ file1 |    2 +-$" output
 232'
 233
 234test_expect_success 'multi-squash only fires up editor once' '
 235        base=$(git rev-parse HEAD~4) &&
 236        FAKE_COMMIT_AMEND="ONCE" FAKE_LINES="1 squash 2 squash 3 squash 4" \
 237                git rebase -i $base &&
 238        test $base = $(git rev-parse HEAD^) &&
 239        test 1 = $(git show | grep ONCE | wc -l)
 240'
 241
 242test_expect_success 'squash works as expected' '
 243        for n in one two three four
 244        do
 245                echo $n >> file$n &&
 246                git add file$n &&
 247                git commit -m $n
 248        done &&
 249        one=$(git rev-parse HEAD~3) &&
 250        FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
 251        test $one = $(git rev-parse HEAD~2)
 252'
 253
 254test_expect_success 'interrupted squash works as expected' '
 255        for n in one two three four
 256        do
 257                echo $n >> conflict &&
 258                git add conflict &&
 259                git commit -m $n
 260        done &&
 261        one=$(git rev-parse HEAD~3) &&
 262        ! FAKE_LINES="1 squash 3 2" git rebase -i HEAD~3 &&
 263        (echo one; echo two; echo four) > conflict &&
 264        git add conflict &&
 265        ! 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        ! FAKE_LINES="3 squash 1 2" git rebase -i HEAD~3 &&
 281        (echo one; echo four) > conflict &&
 282        git add conflict &&
 283        ! git rebase --continue &&
 284        (echo one; echo two; echo four) > conflict &&
 285        git add conflict &&
 286        ! git rebase --continue &&
 287        echo resolved > conflict &&
 288        git add conflict &&
 289        git rebase --continue &&
 290        test $one = $(git rev-parse HEAD~2)
 291'
 292
 293test_expect_success 'ignore patch if in upstream' '
 294        HEAD=$(git rev-parse HEAD) &&
 295        git checkout -b has-cherry-picked HEAD^ &&
 296        echo unrelated > file7 &&
 297        git add file7 &&
 298        test_tick &&
 299        git commit -m "unrelated change" &&
 300        git cherry-pick $HEAD &&
 301        EXPECT_COUNT=1 git rebase -i $HEAD &&
 302        test $HEAD = $(git rev-parse HEAD^)
 303'
 304
 305test_expect_success '--continue tries to commit, even for "edit"' '
 306        parent=$(git rev-parse HEAD^) &&
 307        test_tick &&
 308        FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 309        echo edited > file7 &&
 310        git add file7 &&
 311        FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue &&
 312        test edited = $(git show HEAD:file7) &&
 313        git show HEAD | grep chouette &&
 314        test $parent = $(git rev-parse HEAD^)
 315'
 316
 317test_expect_success 'rebase a detached HEAD' '
 318        grandparent=$(git rev-parse HEAD~2) &&
 319        git checkout $(git rev-parse HEAD) &&
 320        test_tick &&
 321        FAKE_LINES="2 1" git rebase -i HEAD~2 &&
 322        test $grandparent = $(git rev-parse HEAD~2)
 323'
 324
 325test_expect_success 'rebase a commit violating pre-commit' '
 326
 327        mkdir -p .git/hooks &&
 328        PRE_COMMIT=.git/hooks/pre-commit &&
 329        echo "#!/bin/sh" > $PRE_COMMIT &&
 330        echo "test -z \"\$(git diff --cached --check)\"" >> $PRE_COMMIT &&
 331        chmod a+x $PRE_COMMIT &&
 332        echo "monde! " >> file1 &&
 333        test_tick &&
 334        ! git commit -m doesnt-verify file1 &&
 335        git commit -m doesnt-verify --no-verify file1 &&
 336        test_tick &&
 337        FAKE_LINES=2 git rebase -i HEAD~2
 338
 339'
 340
 341test_expect_success 'rebase with a file named HEAD in worktree' '
 342
 343        rm -fr .git/hooks &&
 344        git reset --hard &&
 345        git checkout -b branch3 A &&
 346
 347        (
 348                GIT_AUTHOR_NAME="Squashed Away" &&
 349                export GIT_AUTHOR_NAME &&
 350                >HEAD &&
 351                git add HEAD &&
 352                git commit -m "Add head" &&
 353                >BODY &&
 354                git add BODY &&
 355                git commit -m "Add body"
 356        ) &&
 357
 358        FAKE_LINES="1 squash 2" git rebase -i to-be-rebased &&
 359        test "$(git show -s --pretty=format:%an)" = "Squashed Away"
 360
 361'
 362
 363test_done