3 # Copyright (c) 2007 Carlos Rica
6 test_description
='git reset
8 Documented tests for git reset'
13 # String "modify 2nd file (changed)" partly in German(translated with Google Translate),
14 # encoded in UTF-8, used as a commit log message below.
15 msg
=$
(printf "modify 2nd file (ge\303\244ndert)")
18 msg
=$
(echo $msg |
iconv -f utf-8
-t $1)
23 test_expect_success
'creating initial files and commits' '
25 echo "1st file" >first &&
27 git commit -m "create 1st file" &&
29 echo "2nd file" >second &&
31 git commit -m "create 2nd file" &&
33 echo "2nd line 1st file" >>first &&
34 git commit -a -m "modify 1st file" &&
37 git mv second secondfile &&
38 git commit -a -m "remove 1st and rename 2nd" &&
40 echo "1st line 2nd file" >secondfile &&
41 echo "2nd line 2nd file" >>secondfile &&
42 git -c "i18n.commitEncoding=iso8859-1" commit -a -m "$(commit_msg iso8859-1)" &&
43 head5=$(git rev-parse --verify HEAD)
45 # git log --pretty=oneline # to see those SHA1 involved
48 test "$(git rev-parse HEAD)" = "$1" &&
49 git
diff | test_cmp .diff_expect
- &&
50 git
diff --cached | test_cmp .cached_expect
- &&
55 done | test_cmp .cat_expect
-
58 test_expect_success
'reset --hard message' '
59 hex=$(git log -1 --format="%h") &&
60 git reset --hard > .actual &&
61 echo HEAD is now at $hex $(commit_msg) > .expected &&
62 test_cmp .expected .actual
65 test_expect_success
'reset --hard message (iso8859-1 logoutputencoding)' '
66 hex=$(git log -1 --format="%h") &&
67 git -c "i18n.logOutputEncoding=iso8859-1" reset --hard > .actual &&
68 echo HEAD is now at $hex $(commit_msg iso8859-1) > .expected &&
69 test_cmp .expected .actual
74 cat >.cat_expect
<<EOF
80 test_expect_success
'giving a non existing revision should fail' '
81 test_must_fail git reset aaaaaa &&
82 test_must_fail git reset --mixed aaaaaa &&
83 test_must_fail git reset --soft aaaaaa &&
84 test_must_fail git reset --hard aaaaaa &&
88 test_expect_success
'reset --soft with unmerged index should fail' '
89 touch .git/MERGE_HEAD &&
90 echo "100644 44c5b5884550c17758737edcced463447b91d42b 1 un" |
91 git update-index --index-info &&
92 test_must_fail git reset --soft HEAD &&
98 'giving paths with options different than --mixed should fail' '
99 test_must_fail git reset --soft -- first &&
100 test_must_fail git reset --hard -- first &&
101 test_must_fail git reset --soft HEAD^ -- first &&
102 test_must_fail git reset --hard HEAD^ -- first &&
106 test_expect_success
'giving unrecognized options should fail' '
107 test_must_fail git reset --other &&
108 test_must_fail git reset -o &&
109 test_must_fail git reset --mixed --other &&
110 test_must_fail git reset --mixed -o &&
111 test_must_fail git reset --soft --other &&
112 test_must_fail git reset --soft -o &&
113 test_must_fail git reset --hard --other &&
114 test_must_fail git reset --hard -o &&
118 test_expect_success \
119 'trying to do reset --soft with pending merge should fail' '
120 git branch branch1 &&
121 git branch branch2 &&
123 git checkout branch1 &&
124 echo "3rd line in branch1" >>secondfile &&
125 git commit -a -m "change in branch1" &&
127 git checkout branch2 &&
128 echo "3rd line in branch2" >>secondfile &&
129 git commit -a -m "change in branch2" &&
131 test_must_fail git merge branch1 &&
132 test_must_fail git reset --soft &&
134 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
135 git commit -a -m "the change in branch2" &&
137 git checkout master &&
138 git branch -D branch1 branch2 &&
142 test_expect_success \
143 'trying to do reset --soft with pending checkout merge should fail' '
144 git branch branch3 &&
145 git branch branch4 &&
147 git checkout branch3 &&
148 echo "3rd line in branch3" >>secondfile &&
149 git commit -a -m "line in branch3" &&
151 git checkout branch4 &&
152 echo "3rd line in branch4" >>secondfile &&
154 git checkout -m branch3 &&
155 test_must_fail git reset --soft &&
157 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
158 git commit -a -m "the line in branch3" &&
160 git checkout master &&
161 git branch -D branch3 branch4 &&
165 test_expect_success \
166 'resetting to HEAD with no changes should succeed and do nothing' '
168 check_changes $head5 &&
169 git reset --hard HEAD &&
170 check_changes $head5 &&
172 check_changes $head5 &&
173 git reset --soft HEAD &&
174 check_changes $head5 &&
176 check_changes $head5 &&
177 git reset --mixed HEAD &&
178 check_changes $head5 &&
180 check_changes $head5 &&
186 cat >.cached_expect
<<EOF
187 diff --git a/secondfile b/secondfile
188 index 1bbba79..44c5b58 100644
196 cat >.cat_expect
<<EOF
201 test_expect_success
'--soft reset only should show changes in diff --cached' '
202 git reset --soft HEAD^ &&
203 check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 &&
204 test "$(git rev-parse ORIG_HEAD)" = \
210 cat >.cat_expect
<<EOF
216 test_expect_success \
217 'changing files and redo the last commit should succeed' '
218 echo "3rd line 2nd file" >>secondfile &&
219 git commit -a -C ORIG_HEAD &&
220 head4=$(git rev-parse --verify HEAD) &&
221 check_changes $head4 &&
222 test "$(git rev-parse ORIG_HEAD)" = \
228 cat >.cat_expect
<<EOF
235 test_expect_success \
236 '--hard reset should change the files and undo commits permanently' '
237 git reset --hard HEAD~2 &&
238 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
239 test "$(git rev-parse ORIG_HEAD)" = \
244 cat >.cached_expect
<<EOF
245 diff --git a/first b/first
246 deleted file mode 100644
247 index 8206c22..0000000
253 diff --git a/second b/second
254 deleted file mode 100644
255 index 1bbba79..0000000
260 diff --git a/secondfile b/secondfile
262 index 0000000..44c5b58
269 cat >.cat_expect
<<EOF
274 test_expect_success \
275 'redoing changes adding them without commit them should succeed' '
277 git mv second secondfile &&
279 echo "1st line 2nd file" >secondfile &&
280 echo "2nd line 2nd file" >>secondfile &&
281 git add secondfile &&
282 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
285 cat >.diff_expect
<<EOF
286 diff --git a/first b/first
287 deleted file mode 100644
288 index 8206c22..0000000
294 diff --git a/second b/second
295 deleted file mode 100644
296 index 1bbba79..0000000
303 cat >.cat_expect
<<EOF
308 test_expect_success
'--mixed reset to HEAD should unadd the files' '
310 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
311 test "$(git rev-parse ORIG_HEAD)" = \
312 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
317 cat >.cat_expect
<<EOF
322 test_expect_success
'redoing the last two commits should succeed' '
323 git add secondfile &&
324 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
327 git mv second secondfile &&
328 git commit -a -m "remove 1st and rename 2nd" &&
330 echo "1st line 2nd file" >secondfile &&
331 echo "2nd line 2nd file" >>secondfile &&
332 git -c "i18n.commitEncoding=iso8859-1" commit -a -m "$(commit_msg iso8859-1)" &&
338 cat >.cat_expect
<<EOF
344 test_expect_success
'--hard reset to HEAD should clear a failed merge' '
345 git branch branch1 &&
346 git branch branch2 &&
348 git checkout branch1 &&
349 echo "3rd line in branch1" >>secondfile &&
350 git commit -a -m "change in branch1" &&
352 git checkout branch2 &&
353 echo "3rd line in branch2" >>secondfile &&
354 git commit -a -m "change in branch2" &&
355 head3=$(git rev-parse --verify HEAD) &&
357 test_must_fail git pull . branch1 &&
364 cat >.cat_expect
<<EOF
369 test_expect_success \
370 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
371 git reset --hard HEAD^ &&
372 check_changes $head5 &&
374 git pull . branch1 &&
375 git reset --hard ORIG_HEAD &&
376 check_changes $head5 &&
378 git checkout master &&
379 git branch -D branch1 branch2 &&
384 diff --git a/file1 b/file1
385 index d00491f..7ed6ff8 100644
391 diff --git a/file2 b/file2
392 deleted file mode 100644
393 index 0cfbf08..0000000
399 cat > cached_expect
<< EOF
400 diff --git a/file4 b/file4
402 index 0000000..b8626c4
408 test_expect_success
'test --mixed <paths>' '
411 git add file1 file2 &&
413 git commit -m files &&
418 git add file1 file3 file4 &&
419 git reset HEAD -- file1 file2 file3 &&
420 test_must_fail git diff --quiet &&
422 test_cmp output expect &&
423 git diff --cached > output &&
424 test_cmp output cached_expect
427 test_expect_success
'test resetting the index at give paths' '
432 git update-index --add sub/file1 sub/file2 &&
433 T=$(git write-tree) &&
434 git reset HEAD sub/file2 &&
435 test_must_fail git diff --quiet &&
436 U=$(git write-tree) &&
439 test_must_fail git diff-index --cached --exit-code "$T" &&
444 test_expect_success
'resetting an unmodified path is a no-op' '
446 git reset -- file1 &&
447 git diff-files --exit-code &&
448 git diff-index --cached --exit-code HEAD
452 Unstaged changes after reset:
456 test_expect_success
'--mixed refreshes the index' '
458 git reset --mixed HEAD > output &&
459 test_i18ncmp expect output
462 test_expect_success
'resetting specific path that is unmerged' '
463 git rm --cached file2 &&
464 F1=$(git rev-parse HEAD:file1) &&
465 F2=$(git rev-parse HEAD:file2) &&
466 F3=$(git rev-parse HEAD:secondfile) &&
468 echo "100644 $F1 1 file2" &&
469 echo "100644 $F2 2 file2" &&
470 echo "100644 $F3 3 file2"
471 } | git update-index --index-info &&
473 git reset HEAD file2 &&
474 test_must_fail git diff --quiet &&
475 git diff-index --exit-code --cached HEAD
478 test_expect_success
'disambiguation (1)' '
482 git add secondfile &&
483 git reset secondfile &&
484 test_must_fail git diff --quiet -- secondfile &&
485 test -z "$(git diff --cached --name-only)" &&
486 test -f secondfile &&
491 test_expect_success
'disambiguation (2)' '
495 git add secondfile &&
497 test_must_fail git reset secondfile &&
498 test -n "$(git diff --cached --name-only -- secondfile)" &&
503 test_expect_success
'disambiguation (3)' '
507 git add secondfile &&
509 git reset HEAD secondfile &&
510 test_must_fail git diff --quiet &&
511 test -z "$(git diff --cached --name-only)" &&
516 test_expect_success
'disambiguation (4)' '
520 git add secondfile &&
522 git reset -- secondfile &&
523 test_must_fail git diff --quiet &&
524 test -z "$(git diff --cached --name-only)" &&
528 test_expect_success
'reset with paths accepts tree' '
529 # for simpler tests, drop last commit containing added files
530 git reset --hard HEAD^ &&
531 git reset HEAD^^{tree} -- . &&
532 git diff --cached HEAD^ --exit-code &&
533 git diff HEAD --exit-code