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 check_changes 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d &&
196 test "$(git rev-parse ORIG_HEAD)" = \
202 cat >.cat_expect
<<EOF
209 test_expect_success \
210 '--hard reset should change the files and undo commits permanently' '
211 git reset --hard HEAD~2 &&
212 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
213 test "$(git rev-parse ORIG_HEAD)" = \
214 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d
218 cat >.cached_expect
<<EOF
219 diff --git a/first b/first
220 deleted file mode 100644
221 index 8206c22..0000000
227 diff --git a/second b/second
228 deleted file mode 100644
229 index 1bbba79..0000000
234 diff --git a/secondfile b/secondfile
236 index 0000000..44c5b58
243 cat >.cat_expect
<<EOF
248 test_expect_success \
249 'redoing changes adding them without commit them should succeed' '
251 git mv second secondfile &&
253 echo "1st line 2nd file" >secondfile &&
254 echo "2nd line 2nd file" >>secondfile &&
255 git add secondfile &&
256 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
259 cat >.diff_expect
<<EOF
260 diff --git a/first b/first
261 deleted file mode 100644
262 index 8206c22..0000000
268 diff --git a/second b/second
269 deleted file mode 100644
270 index 1bbba79..0000000
277 cat >.cat_expect
<<EOF
282 test_expect_success
'--mixed reset to HEAD should unadd the files' '
284 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
285 test "$(git rev-parse ORIG_HEAD)" = \
286 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
291 cat >.cat_expect
<<EOF
296 test_expect_success
'redoing the last two commits should succeed' '
297 git add secondfile &&
298 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
301 git mv second secondfile &&
302 git commit -a -m "remove 1st and rename 2nd" &&
304 echo "1st line 2nd file" >secondfile &&
305 echo "2nd line 2nd file" >>secondfile &&
306 git commit -a -m "modify 2nd file" &&
312 cat >.cat_expect
<<EOF
318 test_expect_success
'--hard reset to HEAD should clear a failed merge' '
319 git branch branch1 &&
320 git branch branch2 &&
322 git checkout branch1 &&
323 echo "3rd line in branch1" >>secondfile &&
324 git commit -a -m "change in branch1" &&
326 git checkout branch2 &&
327 echo "3rd line in branch2" >>secondfile &&
328 git commit -a -m "change in branch2" &&
330 test_must_fail git pull . branch1 &&
332 check_changes 77abb337073fb4369a7ad69ff6f5ec0e4d6b54bb
337 cat >.cat_expect
<<EOF
342 test_expect_success \
343 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
344 git reset --hard HEAD^ &&
345 check_changes $head5 &&
347 git pull . branch1 &&
348 git reset --hard ORIG_HEAD &&
349 check_changes $head5 &&
351 git checkout master &&
352 git branch -D branch1 branch2 &&
357 diff --git a/file1 b/file1
358 index d00491f..7ed6ff8 100644
364 diff --git a/file2 b/file2
365 deleted file mode 100644
366 index 0cfbf08..0000000
372 cat > cached_expect
<< EOF
373 diff --git a/file4 b/file4
375 index 0000000..b8626c4
381 test_expect_success
'test --mixed <paths>' '
384 git add file1 file2 &&
386 git commit -m files &&
391 git add file1 file3 file4 &&
392 git reset HEAD -- file1 file2 file3 &&
393 test_must_fail git diff --quiet &&
395 test_cmp output expect &&
396 git diff --cached > output &&
397 test_cmp output cached_expect
400 test_expect_success
'test resetting the index at give paths' '
405 git update-index --add sub/file1 sub/file2 &&
406 T=$(git write-tree) &&
407 git reset HEAD sub/file2 &&
408 test_must_fail git diff --quiet &&
409 U=$(git write-tree) &&
412 test_must_fail git diff-index --cached --exit-code "$T" &&
417 test_expect_success
'resetting an unmodified path is a no-op' '
419 git reset -- file1 &&
420 git diff-files --exit-code &&
421 git diff-index --cached --exit-code HEAD
425 Unstaged changes after reset:
429 test_expect_success
'--mixed refreshes the index' '
431 git reset --mixed HEAD > output &&
432 test_i18ncmp expect output
435 test_expect_success
'resetting specific path that is unmerged' '
436 git rm --cached file2 &&
437 F1=$(git rev-parse HEAD:file1) &&
438 F2=$(git rev-parse HEAD:file2) &&
439 F3=$(git rev-parse HEAD:secondfile) &&
441 echo "100644 $F1 1 file2" &&
442 echo "100644 $F2 2 file2" &&
443 echo "100644 $F3 3 file2"
444 } | git update-index --index-info &&
446 git reset HEAD file2 &&
447 test_must_fail git diff --quiet &&
448 git diff-index --exit-code --cached HEAD
451 test_expect_success
'disambiguation (1)' '
455 git add secondfile &&
456 git reset secondfile &&
457 test_must_fail git diff --quiet -- secondfile &&
458 test -z "$(git diff --cached --name-only)" &&
459 test -f secondfile &&
460 test_must_be_empty secondfile
464 test_expect_success
'disambiguation (2)' '
468 git add secondfile &&
470 test_must_fail git reset secondfile &&
471 test -n "$(git diff --cached --name-only -- secondfile)" &&
476 test_expect_success
'disambiguation (3)' '
480 git add secondfile &&
482 git reset HEAD secondfile &&
483 test_must_fail git diff --quiet &&
484 test -z "$(git diff --cached --name-only)" &&
489 test_expect_success
'disambiguation (4)' '
493 git add secondfile &&
495 git reset -- secondfile &&
496 test_must_fail git diff --quiet &&
497 test -z "$(git diff --cached --name-only)" &&
501 test_expect_success
'reset with paths accepts tree' '
502 # for simpler tests, drop last commit containing added files
503 git reset --hard HEAD^ &&
504 git reset HEAD^^{tree} -- . &&
505 git diff --cached HEAD^ --exit-code &&
506 git diff HEAD --exit-code