Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2009 Christian Couder |
| 4 | # |
| 5 | |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 6 | test_description='Tests for "git reset" with "--merge" and "--keep" options' |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | test_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 |
| 30 | test_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 | |
| 38 | test_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 | # ---------------------------------------------------- |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 50 | # file1: C C C D --keep D D D |
| 51 | # file2: C D D D --keep C D D |
| 52 | test_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 | |
| 62 | test_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 | # ---------------------------------------------------- |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 74 | # file1: B B C D --merge D D D |
| 75 | # file2: C D D D --merge C D D |
| 76 | test_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 | |
| 89 | test_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 | # ---------------------------------------------------- |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 105 | # file1: B B C D --keep (disallowed) |
| 106 | test_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 | # ---------------------------------------------------- |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 117 | # file1: C C C D --merge D D D |
| 118 | # file2: C C D D --merge D D D |
| 119 | test_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 | |
| 130 | test_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 | # ---------------------------------------------------- |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 143 | # file1: C C C D --keep D D D |
| 144 | # file2: C C D D --keep C D D |
| 145 | test_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 | |
| 155 | test_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 | # ---------------------------------------------------- |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 167 | # file1: A B B C --merge (disallowed) |
| 168 | test_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 | |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 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) |
| 184 | test_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 | |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 195 | test_expect_success 'setup 3 different branches' ' |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 196 | git reset --hard second && |
| 197 | git branch branch1 && |
| 198 | git branch branch2 && |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 199 | git branch branch3 && |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 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" && |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 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" |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 215 | ' |
| 216 | |
| 217 | # The next test will test the following: |
| 218 | # |
| 219 | # working index HEAD target working index HEAD |
| 220 | # ---------------------------------------------------- |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 221 | # file1: X U B C --merge C C C |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 222 | test_expect_success '"reset --merge HEAD^" is ok with pending merge' ' |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 223 | git checkout third && |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 224 | test_must_fail git merge branch1 && |
Stephan Beyer | d0f379c | 2009-12-30 06:54:47 +0100 | [diff] [blame] | 225 | git reset --merge HEAD^ && |
| 226 | test "$(git rev-parse HEAD)" = "$(git rev-parse second)" && |
| 227 | test -z "$(git diff --cached)" && |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 228 | test -z "$(git diff)" |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 229 | ' |
| 230 | |
| 231 | # The next test will test the following: |
| 232 | # |
| 233 | # working index HEAD target working index HEAD |
| 234 | # ---------------------------------------------------- |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 235 | # file1: X U B C --keep (disallowed) |
Junio C Hamano | 476cca6 | 2011-04-12 16:36:18 -0700 | [diff] [blame] | 236 | test_expect_success '"reset --keep HEAD^" fails with pending merge' ' |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 237 | git reset --hard third && |
| 238 | test_must_fail git merge branch1 && |
| 239 | test_must_fail git reset --keep HEAD^ 2>err.log && |
Junio C Hamano | 476cca6 | 2011-04-12 16:36:18 -0700 | [diff] [blame] | 240 | test_i18ngrep "middle of a merge" err.log |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 241 | ' |
| 242 | |
| 243 | # The next test will test the following: |
| 244 | # |
| 245 | # working index HEAD target working index HEAD |
| 246 | # ---------------------------------------------------- |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 247 | # file1: X U B B --merge B B B |
| 248 | test_expect_success '"reset --merge HEAD" is ok with pending merge' ' |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 249 | git reset --hard third && |
| 250 | test_must_fail git merge branch1 && |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 251 | git reset --merge HEAD && |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 252 | test "$(git rev-parse HEAD)" = "$(git rev-parse third)" && |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 253 | test -z "$(git diff --cached)" && |
| 254 | test -z "$(git diff)" |
| 255 | ' |
| 256 | |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 257 | # The next test will test the following: |
| 258 | # |
| 259 | # working index HEAD target working index HEAD |
| 260 | # ---------------------------------------------------- |
Christian Couder | 812d2a3 | 2010-01-19 05:26:01 +0100 | [diff] [blame] | 261 | # file1: X U B B --keep (disallowed) |
Junio C Hamano | 476cca6 | 2011-04-12 16:36:18 -0700 | [diff] [blame] | 262 | test_expect_success '"reset --keep HEAD" fails with pending merge' ' |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 263 | git reset --hard third && |
| 264 | test_must_fail git merge branch1 && |
Christian Couder | 812d2a3 | 2010-01-19 05:26:01 +0100 | [diff] [blame] | 265 | test_must_fail git reset --keep HEAD 2>err.log && |
Junio C Hamano | 476cca6 | 2011-04-12 16:36:18 -0700 | [diff] [blame] | 266 | test_i18ngrep "middle of a merge" err.log |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 267 | ' |
| 268 | |
Christian Couder | 812d2a3 | 2010-01-19 05:26:01 +0100 | [diff] [blame] | 269 | test_expect_success '--merge is ok with added/deleted merge' ' |
Junio C Hamano | e11d7b5 | 2009-12-31 23:04:04 -0800 | [diff] [blame] | 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 |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 281 | ' |
| 282 | |
Junio C Hamano | 476cca6 | 2011-04-12 16:36:18 -0700 | [diff] [blame] | 283 | test_expect_success '--keep fails with added/deleted merge' ' |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 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 && |
Christian Couder | 812d2a3 | 2010-01-19 05:26:01 +0100 | [diff] [blame] | 291 | test_must_fail git reset --keep HEAD 2>err.log && |
Junio C Hamano | 476cca6 | 2011-04-12 16:36:18 -0700 | [diff] [blame] | 292 | test_i18ngrep "middle of a merge" err.log |
Christian Couder | ffbc5dc | 2010-01-19 05:25:58 +0100 | [diff] [blame] | 293 | ' |
| 294 | |
Christian Couder | c939669 | 2009-12-30 06:54:46 +0100 | [diff] [blame] | 295 | test_done |