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