t / t7110-reset-merge.shon commit Merge branch 'sg/t5318-cleanup' (5866e58)
   1#!/bin/sh
   2#
   3# Copyright (c) 2009 Christian Couder
   4#
   5
   6test_description='Tests for "git reset" with "--merge" and "--keep" options'
   7
   8. ./test-lib.sh
   9
  10test_expect_success setup '
  11    for i in 1 2 3; do echo line $i; done >file1 &&
  12    cat file1 >file2 &&
  13    git add file1 file2 &&
  14    test_tick &&
  15    git commit -m "Initial commit" &&
  16    git tag initial &&
  17    echo line 4 >>file1 &&
  18    cat file1 >file2 &&
  19    test_tick &&
  20    git commit -m "add line 4 to file1" file1 &&
  21    git tag second
  22'
  23
  24# The next test will test the following:
  25#
  26#           working index HEAD target         working index HEAD
  27#           ----------------------------------------------------
  28# file1:     C       C     C    D     --merge  D       D     D
  29# file2:     C       D     D    D     --merge  C       D     D
  30test_expect_success 'reset --merge is ok with changes in file it does not touch' '
  31    git reset --merge HEAD^ &&
  32    ! grep 4 file1 &&
  33    grep 4 file2 &&
  34    test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
  35    test -z "$(git diff --cached)"
  36'
  37
  38test_expect_success 'reset --merge is ok when switching back' '
  39    git reset --merge second &&
  40    grep 4 file1 &&
  41    grep 4 file2 &&
  42    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
  43    test -z "$(git diff --cached)"
  44'
  45
  46# The next test will test the following:
  47#
  48#           working index HEAD target         working index HEAD
  49#           ----------------------------------------------------
  50# file1:     C       C     C    D     --keep   D       D     D
  51# file2:     C       D     D    D     --keep   C       D     D
  52test_expect_success 'reset --keep is ok with changes in file it does not touch' '
  53    git reset --hard second &&
  54    cat file1 >file2 &&
  55    git reset --keep HEAD^ &&
  56    ! grep 4 file1 &&
  57    grep 4 file2 &&
  58    test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
  59    test -z "$(git diff --cached)"
  60'
  61
  62test_expect_success 'reset --keep is ok when switching back' '
  63    git reset --keep second &&
  64    grep 4 file1 &&
  65    grep 4 file2 &&
  66    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
  67    test -z "$(git diff --cached)"
  68'
  69
  70# The next test will test the following:
  71#
  72#           working index HEAD target         working index HEAD
  73#           ----------------------------------------------------
  74# file1:     B       B     C    D     --merge  D       D     D
  75# file2:     C       D     D    D     --merge  C       D     D
  76test_expect_success 'reset --merge discards changes added to index (1)' '
  77    git reset --hard second &&
  78    cat file1 >file2 &&
  79    echo "line 5" >> file1 &&
  80    git add file1 &&
  81    git reset --merge HEAD^ &&
  82    ! grep 4 file1 &&
  83    ! grep 5 file1 &&
  84    grep 4 file2 &&
  85    test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
  86    test -z "$(git diff --cached)"
  87'
  88
  89test_expect_success 'reset --merge is ok again when switching back (1)' '
  90    git reset --hard initial &&
  91    echo "line 5" >> file2 &&
  92    git add file2 &&
  93    git reset --merge second &&
  94    ! grep 4 file2 &&
  95    ! grep 5 file1 &&
  96    grep 4 file1 &&
  97    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
  98    test -z "$(git diff --cached)"
  99'
 100
 101# The next test will test the following:
 102#
 103#           working index HEAD target         working index HEAD
 104#           ----------------------------------------------------
 105# file1:     B       B     C    D     --keep   (disallowed)
 106test_expect_success 'reset --keep fails with changes in index in files it touches' '
 107    git reset --hard second &&
 108    echo "line 5" >> file1 &&
 109    git add file1 &&
 110    test_must_fail git reset --keep HEAD^
 111'
 112
 113# The next test will test the following:
 114#
 115#           working index HEAD target         working index HEAD
 116#           ----------------------------------------------------
 117# file1:     C       C     C    D     --merge  D       D     D
 118# file2:     C       C     D    D     --merge  D       D     D
 119test_expect_success 'reset --merge discards changes added to index (2)' '
 120    git reset --hard second &&
 121    echo "line 4" >> file2 &&
 122    git add file2 &&
 123    git reset --merge HEAD^ &&
 124    ! grep 4 file2 &&
 125    test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
 126    test -z "$(git diff)" &&
 127    test -z "$(git diff --cached)"
 128'
 129
 130test_expect_success 'reset --merge is ok again when switching back (2)' '
 131    git reset --hard initial &&
 132    git reset --merge second &&
 133    ! grep 4 file2 &&
 134    grep 4 file1 &&
 135    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
 136    test -z "$(git diff --cached)"
 137'
 138
 139# The next test will test the following:
 140#
 141#           working index HEAD target         working index HEAD
 142#           ----------------------------------------------------
 143# file1:     C       C     C    D     --keep   D       D     D
 144# file2:     C       C     D    D     --keep   C       D     D
 145test_expect_success 'reset --keep keeps changes it does not touch' '
 146    git reset --hard second &&
 147    echo "line 4" >> file2 &&
 148    git add file2 &&
 149    git reset --keep HEAD^ &&
 150    grep 4 file2 &&
 151    test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
 152    test -z "$(git diff --cached)"
 153'
 154
 155test_expect_success 'reset --keep keeps changes when switching back' '
 156    git reset --keep second &&
 157    grep 4 file2 &&
 158    grep 4 file1 &&
 159    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
 160    test -z "$(git diff --cached)"
 161'
 162
 163# The next test will test the following:
 164#
 165#           working index HEAD target         working index HEAD
 166#           ----------------------------------------------------
 167# file1:     A       B     B    C     --merge  (disallowed)
 168test_expect_success 'reset --merge fails with changes in file it touches' '
 169    git reset --hard second &&
 170    echo "line 5" >> file1 &&
 171    test_tick &&
 172    git commit -m "add line 5" file1 &&
 173    sed -e "s/line 1/changed line 1/" <file1 >file3 &&
 174    mv file3 file1 &&
 175    test_must_fail git reset --merge HEAD^ 2>err.log &&
 176    grep file1 err.log | grep "not uptodate"
 177'
 178
 179# The next test will test the following:
 180#
 181#           working index HEAD target         working index HEAD
 182#           ----------------------------------------------------
 183# file1:     A       B     B    C     --keep   (disallowed)
 184test_expect_success 'reset --keep fails with changes in file it touches' '
 185    git reset --hard second &&
 186    echo "line 5" >> file1 &&
 187    test_tick &&
 188    git commit -m "add line 5" file1 &&
 189    sed -e "s/line 1/changed line 1/" <file1 >file3 &&
 190    mv file3 file1 &&
 191    test_must_fail git reset --keep HEAD^ 2>err.log &&
 192    grep file1 err.log | grep "not uptodate"
 193'
 194
 195test_expect_success 'setup 3 different branches' '
 196    git reset --hard second &&
 197    git branch branch1 &&
 198    git branch branch2 &&
 199    git branch branch3 &&
 200    git checkout branch1 &&
 201    echo "line 5 in branch1" >> file1 &&
 202    test_tick &&
 203    git commit -a -m "change in branch1" &&
 204    git checkout branch2 &&
 205    echo "line 5 in branch2" >> file1 &&
 206    test_tick &&
 207    git commit -a -m "change in branch2" &&
 208    git tag third &&
 209    git checkout branch3 &&
 210    echo a new file >file3 &&
 211    rm -f file1 &&
 212    git add file3 &&
 213    test_tick &&
 214    git commit -a -m "change in branch3"
 215'
 216
 217# The next test will test the following:
 218#
 219#           working index HEAD target         working index HEAD
 220#           ----------------------------------------------------
 221# file1:     X       U     B    C     --merge  C       C     C
 222test_expect_success '"reset --merge HEAD^" is ok with pending merge' '
 223    git checkout third &&
 224    test_must_fail git merge branch1 &&
 225    git reset --merge HEAD^ &&
 226    test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
 227    test -z "$(git diff --cached)" &&
 228    test -z "$(git diff)"
 229'
 230
 231# The next test will test the following:
 232#
 233#           working index HEAD target         working index HEAD
 234#           ----------------------------------------------------
 235# file1:     X       U     B    C     --keep   (disallowed)
 236test_expect_success '"reset --keep HEAD^" fails with pending merge' '
 237    git reset --hard third &&
 238    test_must_fail git merge branch1 &&
 239    test_must_fail git reset --keep HEAD^ 2>err.log &&
 240    test_i18ngrep "middle of a merge" err.log
 241'
 242
 243# The next test will test the following:
 244#
 245#           working index HEAD target         working index HEAD
 246#           ----------------------------------------------------
 247# file1:     X       U     B    B     --merge  B       B     B
 248test_expect_success '"reset --merge HEAD" is ok with pending merge' '
 249    git reset --hard third &&
 250    test_must_fail git merge branch1 &&
 251    git reset --merge HEAD &&
 252    test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
 253    test -z "$(git diff --cached)" &&
 254    test -z "$(git diff)"
 255'
 256
 257# The next test will test the following:
 258#
 259#           working index HEAD target         working index HEAD
 260#           ----------------------------------------------------
 261# file1:     X       U     B    B     --keep   (disallowed)
 262test_expect_success '"reset --keep HEAD" fails with pending merge' '
 263    git reset --hard third &&
 264    test_must_fail git merge branch1 &&
 265    test_must_fail git reset --keep HEAD 2>err.log &&
 266    test_i18ngrep "middle of a merge" err.log
 267'
 268
 269test_expect_success '--merge is ok with added/deleted merge' '
 270    git reset --hard third &&
 271    rm -f file2 &&
 272    test_must_fail git merge branch3 &&
 273    ! test -f file2 &&
 274    test -f file3 &&
 275    git diff --exit-code file3 &&
 276    git diff --exit-code branch3 file3 &&
 277    git reset --merge HEAD &&
 278    ! test -f file3 &&
 279    ! test -f file2 &&
 280    git diff --exit-code --cached
 281'
 282
 283test_expect_success '--keep fails with added/deleted merge' '
 284    git reset --hard third &&
 285    rm -f file2 &&
 286    test_must_fail git merge branch3 &&
 287    ! test -f file2 &&
 288    test -f file3 &&
 289    git diff --exit-code file3 &&
 290    git diff --exit-code branch3 file3 &&
 291    test_must_fail git reset --keep HEAD 2>err.log &&
 292    test_i18ngrep "middle of a merge" err.log
 293'
 294
 295test_done