]>
Commit | Line | Data |
---|---|---|
359048d6 CR |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Carlos Rica | |
4 | # | |
5 | ||
d592b315 | 6 | test_description='git reset |
359048d6 | 7 | |
d592b315 | 8 | Documented tests for git reset' |
359048d6 CR |
9 | |
10 | . ./test-lib.sh | |
11 | ||
de6029a2 | 12 | commit_msg () { |
17cc2ef1 AS |
13 | # String "modify 2nd file (changed)" partly in German |
14 | # (translated with Google Translate), | |
de6029a2 | 15 | # encoded in UTF-8, used as a commit log message below. |
17cc2ef1 | 16 | msg="modify 2nd file (ge\303\244ndert)\n" |
de6029a2 AS |
17 | if test -n "$1" |
18 | then | |
17cc2ef1 AS |
19 | printf "$msg" | iconv -f utf-8 -t "$1" |
20 | else | |
21 | printf "$msg" | |
de6029a2 | 22 | fi |
de6029a2 AS |
23 | } |
24 | ||
359048d6 CR |
25 | test_expect_success 'creating initial files and commits' ' |
26 | test_tick && | |
27 | echo "1st file" >first && | |
28 | git add first && | |
29 | git commit -m "create 1st file" && | |
30 | ||
31 | echo "2nd file" >second && | |
32 | git add second && | |
33 | git commit -m "create 2nd file" && | |
34 | ||
35 | echo "2nd line 1st file" >>first && | |
36 | git commit -a -m "modify 1st file" && | |
37 | ||
38 | git rm first && | |
39 | git mv second secondfile && | |
40 | git commit -a -m "remove 1st and rename 2nd" && | |
41 | ||
42 | echo "1st line 2nd file" >secondfile && | |
43 | echo "2nd line 2nd file" >>secondfile && | |
14332bcc | 44 | git -c "i18n.commitEncoding=iso8859-1" commit -a -m "$(commit_msg iso8859-1)" && |
8b66f785 | 45 | head5=$(git rev-parse --verify HEAD) |
359048d6 CR |
46 | ' |
47 | # git log --pretty=oneline # to see those SHA1 involved | |
48 | ||
49 | check_changes () { | |
50 | test "$(git rev-parse HEAD)" = "$1" && | |
3af82863 JH |
51 | git diff | test_cmp .diff_expect - && |
52 | git diff --cached | test_cmp .cached_expect - && | |
359048d6 CR |
53 | for FILE in * |
54 | do | |
55 | echo $FILE':' | |
56 | cat $FILE || return | |
3af82863 | 57 | done | test_cmp .cat_expect - |
359048d6 CR |
58 | } |
59 | ||
ecaee805 | 60 | test_expect_success 'reset --hard message' ' |
de6029a2 AS |
61 | hex=$(git log -1 --format="%h") && |
62 | git reset --hard > .actual && | |
63 | echo HEAD is now at $hex $(commit_msg) > .expected && | |
64 | test_cmp .expected .actual | |
65 | ' | |
66 | ||
14332bcc | 67 | test_expect_success 'reset --hard message (iso8859-1 logoutputencoding)' ' |
de6029a2 | 68 | hex=$(git log -1 --format="%h") && |
14332bcc AS |
69 | git -c "i18n.logOutputEncoding=iso8859-1" reset --hard > .actual && |
70 | echo HEAD is now at $hex $(commit_msg iso8859-1) > .expected && | |
de6029a2 AS |
71 | test_cmp .expected .actual |
72 | ' | |
73 | ||
359048d6 CR |
74 | >.diff_expect |
75 | >.cached_expect | |
76 | cat >.cat_expect <<EOF | |
77 | secondfile: | |
78 | 1st line 2nd file | |
79 | 2nd line 2nd file | |
80 | EOF | |
81 | ||
82 | test_expect_success 'giving a non existing revision should fail' ' | |
d492b31c SB |
83 | test_must_fail git reset aaaaaa && |
84 | test_must_fail git reset --mixed aaaaaa && | |
85 | test_must_fail git reset --soft aaaaaa && | |
86 | test_must_fail git reset --hard aaaaaa && | |
8b66f785 | 87 | check_changes $head5 |
359048d6 CR |
88 | ' |
89 | ||
cdf4a751 JS |
90 | test_expect_success 'reset --soft with unmerged index should fail' ' |
91 | touch .git/MERGE_HEAD && | |
92 | echo "100644 44c5b5884550c17758737edcced463447b91d42b 1 un" | | |
93 | git update-index --index-info && | |
d492b31c | 94 | test_must_fail git reset --soft HEAD && |
cdf4a751 JS |
95 | rm .git/MERGE_HEAD && |
96 | git rm --cached -- un | |
97 | ' | |
98 | ||
359048d6 CR |
99 | test_expect_success \ |
100 | 'giving paths with options different than --mixed should fail' ' | |
d492b31c SB |
101 | test_must_fail git reset --soft -- first && |
102 | test_must_fail git reset --hard -- first && | |
103 | test_must_fail git reset --soft HEAD^ -- first && | |
104 | test_must_fail git reset --hard HEAD^ -- first && | |
8b66f785 | 105 | check_changes $head5 |
359048d6 CR |
106 | ' |
107 | ||
108 | test_expect_success 'giving unrecognized options should fail' ' | |
d492b31c SB |
109 | test_must_fail git reset --other && |
110 | test_must_fail git reset -o && | |
111 | test_must_fail git reset --mixed --other && | |
112 | test_must_fail git reset --mixed -o && | |
113 | test_must_fail git reset --soft --other && | |
114 | test_must_fail git reset --soft -o && | |
115 | test_must_fail git reset --hard --other && | |
116 | test_must_fail git reset --hard -o && | |
8b66f785 | 117 | check_changes $head5 |
359048d6 CR |
118 | ' |
119 | ||
120 | test_expect_success \ | |
121 | 'trying to do reset --soft with pending merge should fail' ' | |
122 | git branch branch1 && | |
123 | git branch branch2 && | |
124 | ||
125 | git checkout branch1 && | |
126 | echo "3rd line in branch1" >>secondfile && | |
127 | git commit -a -m "change in branch1" && | |
128 | ||
129 | git checkout branch2 && | |
130 | echo "3rd line in branch2" >>secondfile && | |
131 | git commit -a -m "change in branch2" && | |
132 | ||
d492b31c SB |
133 | test_must_fail git merge branch1 && |
134 | test_must_fail git reset --soft && | |
359048d6 CR |
135 | |
136 | printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile && | |
137 | git commit -a -m "the change in branch2" && | |
138 | ||
139 | git checkout master && | |
140 | git branch -D branch1 branch2 && | |
8b66f785 | 141 | check_changes $head5 |
359048d6 CR |
142 | ' |
143 | ||
144 | test_expect_success \ | |
145 | 'trying to do reset --soft with pending checkout merge should fail' ' | |
146 | git branch branch3 && | |
147 | git branch branch4 && | |
148 | ||
149 | git checkout branch3 && | |
150 | echo "3rd line in branch3" >>secondfile && | |
151 | git commit -a -m "line in branch3" && | |
152 | ||
153 | git checkout branch4 && | |
154 | echo "3rd line in branch4" >>secondfile && | |
155 | ||
156 | git checkout -m branch3 && | |
d492b31c | 157 | test_must_fail git reset --soft && |
359048d6 CR |
158 | |
159 | printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile && | |
160 | git commit -a -m "the line in branch3" && | |
161 | ||
162 | git checkout master && | |
163 | git branch -D branch3 branch4 && | |
8b66f785 | 164 | check_changes $head5 |
359048d6 CR |
165 | ' |
166 | ||
167 | test_expect_success \ | |
168 | 'resetting to HEAD with no changes should succeed and do nothing' ' | |
169 | git reset --hard && | |
8b66f785 | 170 | check_changes $head5 && |
359048d6 | 171 | git reset --hard HEAD && |
8b66f785 | 172 | check_changes $head5 && |
359048d6 | 173 | git reset --soft && |
8b66f785 | 174 | check_changes $head5 && |
359048d6 | 175 | git reset --soft HEAD && |
8b66f785 | 176 | check_changes $head5 && |
359048d6 | 177 | git reset --mixed && |
8b66f785 | 178 | check_changes $head5 && |
359048d6 | 179 | git reset --mixed HEAD && |
8b66f785 | 180 | check_changes $head5 && |
359048d6 | 181 | git reset && |
8b66f785 | 182 | check_changes $head5 && |
359048d6 | 183 | git reset HEAD && |
8b66f785 | 184 | check_changes $head5 |
359048d6 CR |
185 | ' |
186 | ||
187 | >.diff_expect | |
188 | cat >.cached_expect <<EOF | |
189 | diff --git a/secondfile b/secondfile | |
190 | index 1bbba79..44c5b58 100644 | |
191 | --- a/secondfile | |
192 | +++ b/secondfile | |
193 | @@ -1 +1,2 @@ | |
194 | -2nd file | |
195 | +1st line 2nd file | |
196 | +2nd line 2nd file | |
197 | EOF | |
198 | cat >.cat_expect <<EOF | |
199 | secondfile: | |
200 | 1st line 2nd file | |
201 | 2nd line 2nd file | |
202 | EOF | |
203 | test_expect_success '--soft reset only should show changes in diff --cached' ' | |
204 | git reset --soft HEAD^ && | |
205 | check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 && | |
206 | test "$(git rev-parse ORIG_HEAD)" = \ | |
8b66f785 | 207 | $head5 |
359048d6 CR |
208 | ' |
209 | ||
210 | >.diff_expect | |
211 | >.cached_expect | |
212 | cat >.cat_expect <<EOF | |
213 | secondfile: | |
214 | 1st line 2nd file | |
215 | 2nd line 2nd file | |
216 | 3rd line 2nd file | |
217 | EOF | |
218 | test_expect_success \ | |
219 | 'changing files and redo the last commit should succeed' ' | |
220 | echo "3rd line 2nd file" >>secondfile && | |
221 | git commit -a -C ORIG_HEAD && | |
375775bb AS |
222 | head4=$(git rev-parse --verify HEAD) && |
223 | check_changes $head4 && | |
359048d6 | 224 | test "$(git rev-parse ORIG_HEAD)" = \ |
8b66f785 | 225 | $head5 |
359048d6 CR |
226 | ' |
227 | ||
228 | >.diff_expect | |
229 | >.cached_expect | |
230 | cat >.cat_expect <<EOF | |
231 | first: | |
232 | 1st file | |
233 | 2nd line 1st file | |
234 | second: | |
235 | 2nd file | |
236 | EOF | |
237 | test_expect_success \ | |
238 | '--hard reset should change the files and undo commits permanently' ' | |
239 | git reset --hard HEAD~2 && | |
240 | check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e && | |
241 | test "$(git rev-parse ORIG_HEAD)" = \ | |
375775bb | 242 | $head4 |
359048d6 CR |
243 | ' |
244 | ||
245 | >.diff_expect | |
246 | cat >.cached_expect <<EOF | |
247 | diff --git a/first b/first | |
248 | deleted file mode 100644 | |
249 | index 8206c22..0000000 | |
250 | --- a/first | |
251 | +++ /dev/null | |
252 | @@ -1,2 +0,0 @@ | |
253 | -1st file | |
254 | -2nd line 1st file | |
255 | diff --git a/second b/second | |
256 | deleted file mode 100644 | |
257 | index 1bbba79..0000000 | |
258 | --- a/second | |
259 | +++ /dev/null | |
260 | @@ -1 +0,0 @@ | |
261 | -2nd file | |
262 | diff --git a/secondfile b/secondfile | |
263 | new file mode 100644 | |
264 | index 0000000..44c5b58 | |
265 | --- /dev/null | |
266 | +++ b/secondfile | |
267 | @@ -0,0 +1,2 @@ | |
268 | +1st line 2nd file | |
269 | +2nd line 2nd file | |
270 | EOF | |
271 | cat >.cat_expect <<EOF | |
272 | secondfile: | |
273 | 1st line 2nd file | |
274 | 2nd line 2nd file | |
275 | EOF | |
276 | test_expect_success \ | |
277 | 'redoing changes adding them without commit them should succeed' ' | |
278 | git rm first && | |
279 | git mv second secondfile && | |
280 | ||
281 | echo "1st line 2nd file" >secondfile && | |
282 | echo "2nd line 2nd file" >>secondfile && | |
283 | git add secondfile && | |
284 | check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e | |
285 | ' | |
286 | ||
287 | cat >.diff_expect <<EOF | |
288 | diff --git a/first b/first | |
289 | deleted file mode 100644 | |
290 | index 8206c22..0000000 | |
291 | --- a/first | |
292 | +++ /dev/null | |
293 | @@ -1,2 +0,0 @@ | |
294 | -1st file | |
295 | -2nd line 1st file | |
296 | diff --git a/second b/second | |
297 | deleted file mode 100644 | |
298 | index 1bbba79..0000000 | |
299 | --- a/second | |
300 | +++ /dev/null | |
301 | @@ -1 +0,0 @@ | |
302 | -2nd file | |
303 | EOF | |
304 | >.cached_expect | |
305 | cat >.cat_expect <<EOF | |
306 | secondfile: | |
307 | 1st line 2nd file | |
308 | 2nd line 2nd file | |
309 | EOF | |
310 | test_expect_success '--mixed reset to HEAD should unadd the files' ' | |
311 | git reset && | |
312 | check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e && | |
313 | test "$(git rev-parse ORIG_HEAD)" = \ | |
314 | ddaefe00f1da16864591c61fdc7adb5d7cd6b74e | |
315 | ' | |
316 | ||
317 | >.diff_expect | |
318 | >.cached_expect | |
319 | cat >.cat_expect <<EOF | |
320 | secondfile: | |
321 | 1st line 2nd file | |
322 | 2nd line 2nd file | |
323 | EOF | |
324 | test_expect_success 'redoing the last two commits should succeed' ' | |
325 | git add secondfile && | |
326 | git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e && | |
327 | ||
328 | git rm first && | |
329 | git mv second secondfile && | |
330 | git commit -a -m "remove 1st and rename 2nd" && | |
331 | ||
332 | echo "1st line 2nd file" >secondfile && | |
333 | echo "2nd line 2nd file" >>secondfile && | |
14332bcc | 334 | git -c "i18n.commitEncoding=iso8859-1" commit -a -m "$(commit_msg iso8859-1)" && |
8b66f785 | 335 | check_changes $head5 |
359048d6 CR |
336 | ' |
337 | ||
338 | >.diff_expect | |
339 | >.cached_expect | |
340 | cat >.cat_expect <<EOF | |
341 | secondfile: | |
342 | 1st line 2nd file | |
343 | 2nd line 2nd file | |
344 | 3rd line in branch2 | |
345 | EOF | |
346 | test_expect_success '--hard reset to HEAD should clear a failed merge' ' | |
347 | git branch branch1 && | |
348 | git branch branch2 && | |
349 | ||
350 | git checkout branch1 && | |
351 | echo "3rd line in branch1" >>secondfile && | |
352 | git commit -a -m "change in branch1" && | |
353 | ||
354 | git checkout branch2 && | |
355 | echo "3rd line in branch2" >>secondfile && | |
356 | git commit -a -m "change in branch2" && | |
375775bb | 357 | head3=$(git rev-parse --verify HEAD) && |
359048d6 | 358 | |
d492b31c | 359 | test_must_fail git pull . branch1 && |
359048d6 | 360 | git reset --hard && |
375775bb | 361 | check_changes $head3 |
359048d6 CR |
362 | ' |
363 | ||
364 | >.diff_expect | |
365 | >.cached_expect | |
366 | cat >.cat_expect <<EOF | |
367 | secondfile: | |
368 | 1st line 2nd file | |
369 | 2nd line 2nd file | |
370 | EOF | |
371 | test_expect_success \ | |
372 | '--hard reset to ORIG_HEAD should clear a fast-forward merge' ' | |
373 | git reset --hard HEAD^ && | |
8b66f785 | 374 | check_changes $head5 && |
359048d6 CR |
375 | |
376 | git pull . branch1 && | |
377 | git reset --hard ORIG_HEAD && | |
8b66f785 | 378 | check_changes $head5 && |
359048d6 CR |
379 | |
380 | git checkout master && | |
381 | git branch -D branch1 branch2 && | |
8b66f785 | 382 | check_changes $head5 |
359048d6 CR |
383 | ' |
384 | ||
385 | cat > expect << EOF | |
386 | diff --git a/file1 b/file1 | |
387 | index d00491f..7ed6ff8 100644 | |
388 | --- a/file1 | |
389 | +++ b/file1 | |
390 | @@ -1 +1 @@ | |
391 | -1 | |
392 | +5 | |
393 | diff --git a/file2 b/file2 | |
394 | deleted file mode 100644 | |
395 | index 0cfbf08..0000000 | |
396 | --- a/file2 | |
397 | +++ /dev/null | |
398 | @@ -1 +0,0 @@ | |
399 | -2 | |
400 | EOF | |
401 | cat > cached_expect << EOF | |
402 | diff --git a/file4 b/file4 | |
403 | new file mode 100644 | |
404 | index 0000000..b8626c4 | |
405 | --- /dev/null | |
406 | +++ b/file4 | |
407 | @@ -0,0 +1 @@ | |
408 | +4 | |
409 | EOF | |
410 | test_expect_success 'test --mixed <paths>' ' | |
411 | echo 1 > file1 && | |
412 | echo 2 > file2 && | |
413 | git add file1 file2 && | |
414 | test_tick && | |
415 | git commit -m files && | |
416 | git rm file2 && | |
417 | echo 3 > file3 && | |
418 | echo 4 > file4 && | |
419 | echo 5 > file1 && | |
420 | git add file1 file3 file4 && | |
d94c5e2f MZ |
421 | git reset HEAD -- file1 file2 file3 && |
422 | test_must_fail git diff --quiet && | |
359048d6 | 423 | git diff > output && |
3af82863 | 424 | test_cmp output expect && |
359048d6 | 425 | git diff --cached > output && |
3af82863 | 426 | test_cmp output cached_expect |
359048d6 CR |
427 | ' |
428 | ||
cbb390cd JH |
429 | test_expect_success 'test resetting the index at give paths' ' |
430 | ||
431 | mkdir sub && | |
432 | >sub/file1 && | |
433 | >sub/file2 && | |
434 | git update-index --add sub/file1 sub/file2 && | |
435 | T=$(git write-tree) && | |
d94c5e2f MZ |
436 | git reset HEAD sub/file2 && |
437 | test_must_fail git diff --quiet && | |
cbb390cd JH |
438 | U=$(git write-tree) && |
439 | echo "$T" && | |
440 | echo "$U" && | |
d492b31c | 441 | test_must_fail git diff-index --cached --exit-code "$T" && |
cbb390cd JH |
442 | test "$T" != "$U" |
443 | ||
444 | ' | |
445 | ||
2e7a9785 JS |
446 | test_expect_success 'resetting an unmodified path is a no-op' ' |
447 | git reset --hard && | |
448 | git reset -- file1 && | |
449 | git diff-files --exit-code && | |
450 | git diff-index --cached --exit-code HEAD | |
451 | ' | |
452 | ||
620a6cd4 | 453 | cat > expect << EOF |
3deffc52 MM |
454 | Unstaged changes after reset: |
455 | M file2 | |
620a6cd4 JS |
456 | EOF |
457 | ||
476cca69 | 458 | test_expect_success '--mixed refreshes the index' ' |
620a6cd4 JS |
459 | echo 123 >> file2 && |
460 | git reset --mixed HEAD > output && | |
476cca69 | 461 | test_i18ncmp expect output |
620a6cd4 JS |
462 | ' |
463 | ||
ff00b682 JH |
464 | test_expect_success 'resetting specific path that is unmerged' ' |
465 | git rm --cached file2 && | |
466 | F1=$(git rev-parse HEAD:file1) && | |
467 | F2=$(git rev-parse HEAD:file2) && | |
468 | F3=$(git rev-parse HEAD:secondfile) && | |
469 | { | |
470 | echo "100644 $F1 1 file2" && | |
471 | echo "100644 $F2 2 file2" && | |
472 | echo "100644 $F3 3 file2" | |
473 | } | git update-index --index-info && | |
474 | git ls-files -u && | |
d94c5e2f MZ |
475 | git reset HEAD file2 && |
476 | test_must_fail git diff --quiet && | |
ff00b682 JH |
477 | git diff-index --exit-code --cached HEAD |
478 | ' | |
479 | ||
dfc8f39e JH |
480 | test_expect_success 'disambiguation (1)' ' |
481 | ||
482 | git reset --hard && | |
483 | >secondfile && | |
484 | git add secondfile && | |
d94c5e2f MZ |
485 | git reset secondfile && |
486 | test_must_fail git diff --quiet -- secondfile && | |
dfc8f39e JH |
487 | test -z "$(git diff --cached --name-only)" && |
488 | test -f secondfile && | |
ca8d148d | 489 | test_must_be_empty secondfile |
dfc8f39e JH |
490 | |
491 | ' | |
492 | ||
493 | test_expect_success 'disambiguation (2)' ' | |
494 | ||
495 | git reset --hard && | |
496 | >secondfile && | |
497 | git add secondfile && | |
498 | rm -f secondfile && | |
499 | test_must_fail git reset secondfile && | |
500 | test -n "$(git diff --cached --name-only -- secondfile)" && | |
501 | test ! -f secondfile | |
502 | ||
503 | ' | |
504 | ||
505 | test_expect_success 'disambiguation (3)' ' | |
506 | ||
507 | git reset --hard && | |
508 | >secondfile && | |
509 | git add secondfile && | |
510 | rm -f secondfile && | |
d94c5e2f MZ |
511 | git reset HEAD secondfile && |
512 | test_must_fail git diff --quiet && | |
dfc8f39e JH |
513 | test -z "$(git diff --cached --name-only)" && |
514 | test ! -f secondfile | |
515 | ||
516 | ' | |
517 | ||
518 | test_expect_success 'disambiguation (4)' ' | |
519 | ||
520 | git reset --hard && | |
521 | >secondfile && | |
522 | git add secondfile && | |
523 | rm -f secondfile && | |
d94c5e2f MZ |
524 | git reset -- secondfile && |
525 | test_must_fail git diff --quiet && | |
dfc8f39e JH |
526 | test -z "$(git diff --cached --name-only)" && |
527 | test ! -f secondfile | |
528 | ' | |
529 | ||
2f328c3d MZ |
530 | test_expect_success 'reset with paths accepts tree' ' |
531 | # for simpler tests, drop last commit containing added files | |
532 | git reset --hard HEAD^ && | |
533 | git reset HEAD^^{tree} -- . && | |
534 | git diff --cached HEAD^ --exit-code && | |
535 | git diff HEAD --exit-code | |
536 | ' | |
537 | ||
359048d6 | 538 | test_done |