3 # Copyright (c) 2007 Carlos Rica
6 test_description
='git reset
8 Documented tests for git reset'
12 test_expect_success
'creating initial files and commits' '
14 echo "1st file" >first &&
16 git commit -m "create 1st file" &&
18 echo "2nd file" >second &&
20 git commit -m "create 2nd file" &&
22 echo "2nd line 1st file" >>first &&
23 git commit -a -m "modify 1st file" &&
26 git mv second secondfile &&
27 git commit -a -m "remove 1st and rename 2nd" &&
29 echo "1st line 2nd file" >secondfile &&
30 echo "2nd line 2nd file" >>secondfile &&
31 git commit -a -m "modify 2nd file" &&
32 head5=$(git rev-parse --verify HEAD)
34 # git log --pretty=oneline # to see those SHA1 involved
37 test "$(git rev-parse HEAD)" = "$1" &&
38 git
diff | test_cmp .diff_expect
- &&
39 git
diff --cached | test_cmp .cached_expect
- &&
44 done | test_cmp .cat_expect
-
49 cat >.cat_expect
<<EOF
55 test_expect_success
'giving a non existing revision should fail' '
56 test_must_fail git reset aaaaaa &&
57 test_must_fail git reset --mixed aaaaaa &&
58 test_must_fail git reset --soft aaaaaa &&
59 test_must_fail git reset --hard aaaaaa &&
63 test_expect_success
'reset --soft with unmerged index should fail' '
64 touch .git/MERGE_HEAD &&
65 echo "100644 44c5b5884550c17758737edcced463447b91d42b 1 un" |
66 git update-index --index-info &&
67 test_must_fail git reset --soft HEAD &&
73 'giving paths with options different than --mixed should fail' '
74 test_must_fail git reset --soft -- first &&
75 test_must_fail git reset --hard -- first &&
76 test_must_fail git reset --soft HEAD^ -- first &&
77 test_must_fail git reset --hard HEAD^ -- first &&
81 test_expect_success
'giving unrecognized options should fail' '
82 test_must_fail git reset --other &&
83 test_must_fail git reset -o &&
84 test_must_fail git reset --mixed --other &&
85 test_must_fail git reset --mixed -o &&
86 test_must_fail git reset --soft --other &&
87 test_must_fail git reset --soft -o &&
88 test_must_fail git reset --hard --other &&
89 test_must_fail git reset --hard -o &&
94 'trying to do reset --soft with pending merge should fail' '
98 git checkout branch1 &&
99 echo "3rd line in branch1" >>secondfile &&
100 git commit -a -m "change in branch1" &&
102 git checkout branch2 &&
103 echo "3rd line in branch2" >>secondfile &&
104 git commit -a -m "change in branch2" &&
106 test_must_fail git merge branch1 &&
107 test_must_fail git reset --soft &&
109 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
110 git commit -a -m "the change in branch2" &&
112 git checkout master &&
113 git branch -D branch1 branch2 &&
117 test_expect_success \
118 'trying to do reset --soft with pending checkout merge should fail' '
119 git branch branch3 &&
120 git branch branch4 &&
122 git checkout branch3 &&
123 echo "3rd line in branch3" >>secondfile &&
124 git commit -a -m "line in branch3" &&
126 git checkout branch4 &&
127 echo "3rd line in branch4" >>secondfile &&
129 git checkout -m branch3 &&
130 test_must_fail git reset --soft &&
132 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
133 git commit -a -m "the line in branch3" &&
135 git checkout master &&
136 git branch -D branch3 branch4 &&
140 test_expect_success \
141 'resetting to HEAD with no changes should succeed and do nothing' '
143 check_changes $head5 &&
144 git reset --hard HEAD &&
145 check_changes $head5 &&
147 check_changes $head5 &&
148 git reset --soft HEAD &&
149 check_changes $head5 &&
151 check_changes $head5 &&
152 git reset --mixed HEAD &&
153 check_changes $head5 &&
155 check_changes $head5 &&
161 cat >.cached_expect
<<EOF
162 diff --git a/secondfile b/secondfile
163 index 1bbba79..44c5b58 100644
171 cat >.cat_expect
<<EOF
176 test_expect_success
'--soft reset only should show changes in diff --cached' '
177 git reset --soft HEAD^ &&
178 check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 &&
179 test "$(git rev-parse ORIG_HEAD)" = \
185 cat >.cat_expect
<<EOF
191 test_expect_success \
192 'changing files and redo the last commit should succeed' '
193 echo "3rd line 2nd file" >>secondfile &&
194 git commit -a -C ORIG_HEAD &&
195 head4=$(git rev-parse --verify HEAD) &&
196 check_changes $head4 &&
197 test "$(git rev-parse ORIG_HEAD)" = \
203 cat >.cat_expect
<<EOF
210 test_expect_success \
211 '--hard reset should change the files and undo commits permanently' '
212 git reset --hard HEAD~2 &&
213 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
214 test "$(git rev-parse ORIG_HEAD)" = \
219 cat >.cached_expect
<<EOF
220 diff --git a/first b/first
221 deleted file mode 100644
222 index 8206c22..0000000
228 diff --git a/second b/second
229 deleted file mode 100644
230 index 1bbba79..0000000
235 diff --git a/secondfile b/secondfile
237 index 0000000..44c5b58
244 cat >.cat_expect
<<EOF
249 test_expect_success \
250 'redoing changes adding them without commit them should succeed' '
252 git mv second secondfile &&
254 echo "1st line 2nd file" >secondfile &&
255 echo "2nd line 2nd file" >>secondfile &&
256 git add secondfile &&
257 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
260 cat >.diff_expect
<<EOF
261 diff --git a/first b/first
262 deleted file mode 100644
263 index 8206c22..0000000
269 diff --git a/second b/second
270 deleted file mode 100644
271 index 1bbba79..0000000
278 cat >.cat_expect
<<EOF
283 test_expect_success
'--mixed reset to HEAD should unadd the files' '
285 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
286 test "$(git rev-parse ORIG_HEAD)" = \
287 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
292 cat >.cat_expect
<<EOF
297 test_expect_success
'redoing the last two commits should succeed' '
298 git add secondfile &&
299 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
302 git mv second secondfile &&
303 git commit -a -m "remove 1st and rename 2nd" &&
305 echo "1st line 2nd file" >secondfile &&
306 echo "2nd line 2nd file" >>secondfile &&
307 git commit -a -m "modify 2nd file" &&
313 cat >.cat_expect
<<EOF
319 test_expect_success
'--hard reset to HEAD should clear a failed merge' '
320 git branch branch1 &&
321 git branch branch2 &&
323 git checkout branch1 &&
324 echo "3rd line in branch1" >>secondfile &&
325 git commit -a -m "change in branch1" &&
327 git checkout branch2 &&
328 echo "3rd line in branch2" >>secondfile &&
329 git commit -a -m "change in branch2" &&
330 head3=$(git rev-parse --verify HEAD) &&
332 test_must_fail git pull . branch1 &&
339 cat >.cat_expect
<<EOF
344 test_expect_success \
345 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
346 git reset --hard HEAD^ &&
347 check_changes $head5 &&
349 git pull . branch1 &&
350 git reset --hard ORIG_HEAD &&
351 check_changes $head5 &&
353 git checkout master &&
354 git branch -D branch1 branch2 &&
359 diff --git a/file1 b/file1
360 index d00491f..7ed6ff8 100644
366 diff --git a/file2 b/file2
367 deleted file mode 100644
368 index 0cfbf08..0000000
374 cat > cached_expect
<< EOF
375 diff --git a/file4 b/file4
377 index 0000000..b8626c4
383 test_expect_success
'test --mixed <paths>' '
386 git add file1 file2 &&
388 git commit -m files &&
393 git add file1 file3 file4 &&
394 git reset HEAD -- file1 file2 file3 &&
395 test_must_fail git diff --quiet &&
397 test_cmp output expect &&
398 git diff --cached > output &&
399 test_cmp output cached_expect
402 test_expect_success
'test resetting the index at give paths' '
407 git update-index --add sub/file1 sub/file2 &&
408 T=$(git write-tree) &&
409 git reset HEAD sub/file2 &&
410 test_must_fail git diff --quiet &&
411 U=$(git write-tree) &&
414 test_must_fail git diff-index --cached --exit-code "$T" &&
419 test_expect_success
'resetting an unmodified path is a no-op' '
421 git reset -- file1 &&
422 git diff-files --exit-code &&
423 git diff-index --cached --exit-code HEAD
427 Unstaged changes after reset:
431 test_expect_success
'--mixed refreshes the index' '
433 git reset --mixed HEAD > output &&
434 test_i18ncmp expect output
437 test_expect_success
'resetting specific path that is unmerged' '
438 git rm --cached file2 &&
439 F1=$(git rev-parse HEAD:file1) &&
440 F2=$(git rev-parse HEAD:file2) &&
441 F3=$(git rev-parse HEAD:secondfile) &&
443 echo "100644 $F1 1 file2" &&
444 echo "100644 $F2 2 file2" &&
445 echo "100644 $F3 3 file2"
446 } | git update-index --index-info &&
448 git reset HEAD file2 &&
449 test_must_fail git diff --quiet &&
450 git diff-index --exit-code --cached HEAD
453 test_expect_success
'disambiguation (1)' '
457 git add secondfile &&
458 git reset secondfile &&
459 test_must_fail git diff --quiet -- secondfile &&
460 test -z "$(git diff --cached --name-only)" &&
461 test -f secondfile &&
466 test_expect_success
'disambiguation (2)' '
470 git add secondfile &&
472 test_must_fail git reset secondfile &&
473 test -n "$(git diff --cached --name-only -- secondfile)" &&
478 test_expect_success
'disambiguation (3)' '
482 git add secondfile &&
484 git reset HEAD secondfile &&
485 test_must_fail git diff --quiet &&
486 test -z "$(git diff --cached --name-only)" &&
491 test_expect_success
'disambiguation (4)' '
495 git add secondfile &&
497 git reset -- secondfile &&
498 test_must_fail git diff --quiet &&
499 test -z "$(git diff --cached --name-only)" &&
503 test_expect_success
'reset with paths accepts tree' '
504 # for simpler tests, drop last commit containing added files
505 git reset --hard HEAD^ &&
506 git reset HEAD^^{tree} -- . &&
507 git diff --cached HEAD^ --exit-code &&
508 git diff HEAD --exit-code