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
14 # (translated with Google Translate),
15 # encoded in UTF-8, used as a commit log message below.
16 msg
="modify 2nd file (ge\303\244ndert)\n"
19 printf "$msg" |
iconv -f utf-8
-t "$1"
25 test_expect_success
'creating initial files and commits' '
27 echo "1st file" >first &&
29 git commit -m "create 1st file" &&
31 echo "2nd file" >second &&
33 git commit -m "create 2nd file" &&
35 echo "2nd line 1st file" >>first &&
36 git commit -a -m "modify 1st file" &&
39 git mv second secondfile &&
40 git commit -a -m "remove 1st and rename 2nd" &&
42 echo "1st line 2nd file" >secondfile &&
43 echo "2nd line 2nd file" >>secondfile &&
44 git -c "i18n.commitEncoding=iso8859-1" commit -a -m "$(commit_msg iso8859-1)" &&
45 head5=$(git rev-parse --verify HEAD)
47 # git log --pretty=oneline # to see those SHA1 involved
50 test "$(git rev-parse HEAD)" = "$1" &&
51 git
diff | test_cmp .diff_expect
- &&
52 git
diff --cached | test_cmp .cached_expect
- &&
57 done | test_cmp .cat_expect
-
60 test_expect_success
'reset --hard message' '
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
67 test_expect_success
'reset --hard message (iso8859-1 logoutputencoding)' '
68 hex=$(git log -1 --format="%h") &&
69 git -c "i18n.logOutputEncoding=iso8859-1" reset --hard > .actual &&
70 echo HEAD is now at $hex $(commit_msg iso8859-1) > .expected &&
71 test_cmp .expected .actual
76 cat >.cat_expect
<<EOF
82 test_expect_success
'giving a non existing revision should fail' '
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 &&
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 &&
94 test_must_fail git reset --soft HEAD &&
100 'giving paths with options different than --mixed should fail' '
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 &&
108 test_expect_success
'giving unrecognized options should fail' '
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 &&
120 test_expect_success \
121 'trying to do reset --soft with pending merge should fail' '
122 git branch branch1 &&
123 git branch branch2 &&
125 git checkout branch1 &&
126 echo "3rd line in branch1" >>secondfile &&
127 git commit -a -m "change in branch1" &&
129 git checkout branch2 &&
130 echo "3rd line in branch2" >>secondfile &&
131 git commit -a -m "change in branch2" &&
133 test_must_fail git merge branch1 &&
134 test_must_fail git reset --soft &&
136 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
137 git commit -a -m "the change in branch2" &&
139 git checkout master &&
140 git branch -D branch1 branch2 &&
144 test_expect_success \
145 'trying to do reset --soft with pending checkout merge should fail' '
146 git branch branch3 &&
147 git branch branch4 &&
149 git checkout branch3 &&
150 echo "3rd line in branch3" >>secondfile &&
151 git commit -a -m "line in branch3" &&
153 git checkout branch4 &&
154 echo "3rd line in branch4" >>secondfile &&
156 git checkout -m branch3 &&
157 test_must_fail git reset --soft &&
159 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
160 git commit -a -m "the line in branch3" &&
162 git checkout master &&
163 git branch -D branch3 branch4 &&
167 test_expect_success \
168 'resetting to HEAD with no changes should succeed and do nothing' '
170 check_changes $head5 &&
171 git reset --hard HEAD &&
172 check_changes $head5 &&
174 check_changes $head5 &&
175 git reset --soft HEAD &&
176 check_changes $head5 &&
178 check_changes $head5 &&
179 git reset --mixed HEAD &&
180 check_changes $head5 &&
182 check_changes $head5 &&
188 cat >.cached_expect
<<EOF
189 diff --git a/secondfile b/secondfile
190 index 1bbba79..44c5b58 100644
198 cat >.cat_expect
<<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)" = \
212 cat >.cat_expect
<<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 &&
222 head4=$(git rev-parse --verify HEAD) &&
223 check_changes $head4 &&
224 test "$(git rev-parse ORIG_HEAD)" = \
230 cat >.cat_expect
<<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)" = \
246 cat >.cached_expect
<<EOF
247 diff --git a/first b/first
248 deleted file mode 100644
249 index 8206c22..0000000
255 diff --git a/second b/second
256 deleted file mode 100644
257 index 1bbba79..0000000
262 diff --git a/secondfile b/secondfile
264 index 0000000..44c5b58
271 cat >.cat_expect
<<EOF
276 test_expect_success \
277 'redoing changes adding them without commit them should succeed' '
279 git mv second secondfile &&
281 echo "1st line 2nd file" >secondfile &&
282 echo "2nd line 2nd file" >>secondfile &&
283 git add secondfile &&
284 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
287 cat >.diff_expect
<<EOF
288 diff --git a/first b/first
289 deleted file mode 100644
290 index 8206c22..0000000
296 diff --git a/second b/second
297 deleted file mode 100644
298 index 1bbba79..0000000
305 cat >.cat_expect
<<EOF
310 test_expect_success
'--mixed reset to HEAD should unadd the files' '
312 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
313 test "$(git rev-parse ORIG_HEAD)" = \
314 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
319 cat >.cat_expect
<<EOF
324 test_expect_success
'redoing the last two commits should succeed' '
325 git add secondfile &&
326 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
329 git mv second secondfile &&
330 git commit -a -m "remove 1st and rename 2nd" &&
332 echo "1st line 2nd file" >secondfile &&
333 echo "2nd line 2nd file" >>secondfile &&
334 git -c "i18n.commitEncoding=iso8859-1" commit -a -m "$(commit_msg iso8859-1)" &&
340 cat >.cat_expect
<<EOF
346 test_expect_success
'--hard reset to HEAD should clear a failed merge' '
347 git branch branch1 &&
348 git branch branch2 &&
350 git checkout branch1 &&
351 echo "3rd line in branch1" >>secondfile &&
352 git commit -a -m "change in branch1" &&
354 git checkout branch2 &&
355 echo "3rd line in branch2" >>secondfile &&
356 git commit -a -m "change in branch2" &&
357 head3=$(git rev-parse --verify HEAD) &&
359 test_must_fail git pull . branch1 &&
366 cat >.cat_expect
<<EOF
371 test_expect_success \
372 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
373 git reset --hard HEAD^ &&
374 check_changes $head5 &&
376 git pull . branch1 &&
377 git reset --hard ORIG_HEAD &&
378 check_changes $head5 &&
380 git checkout master &&
381 git branch -D branch1 branch2 &&
386 diff --git a/file1 b/file1
387 index d00491f..7ed6ff8 100644
393 diff --git a/file2 b/file2
394 deleted file mode 100644
395 index 0cfbf08..0000000
401 cat > cached_expect
<< EOF
402 diff --git a/file4 b/file4
404 index 0000000..b8626c4
410 test_expect_success
'test --mixed <paths>' '
413 git add file1 file2 &&
415 git commit -m files &&
420 git add file1 file3 file4 &&
421 git reset HEAD -- file1 file2 file3 &&
422 test_must_fail git diff --quiet &&
424 test_cmp output expect &&
425 git diff --cached > output &&
426 test_cmp output cached_expect
429 test_expect_success
'test resetting the index at give paths' '
434 git update-index --add sub/file1 sub/file2 &&
435 T=$(git write-tree) &&
436 git reset HEAD sub/file2 &&
437 test_must_fail git diff --quiet &&
438 U=$(git write-tree) &&
441 test_must_fail git diff-index --cached --exit-code "$T" &&
446 test_expect_success
'resetting an unmodified path is a no-op' '
448 git reset -- file1 &&
449 git diff-files --exit-code &&
450 git diff-index --cached --exit-code HEAD
454 Unstaged changes after reset:
458 test_expect_success
'--mixed refreshes the index' '
460 git reset --mixed HEAD > output &&
461 test_i18ncmp expect output
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) &&
470 echo "100644 $F1 1 file2" &&
471 echo "100644 $F2 2 file2" &&
472 echo "100644 $F3 3 file2"
473 } | git update-index --index-info &&
475 git reset HEAD file2 &&
476 test_must_fail git diff --quiet &&
477 git diff-index --exit-code --cached HEAD
480 test_expect_success
'disambiguation (1)' '
484 git add secondfile &&
485 git reset secondfile &&
486 test_must_fail git diff --quiet -- secondfile &&
487 test -z "$(git diff --cached --name-only)" &&
488 test -f secondfile &&
489 test_must_be_empty secondfile
493 test_expect_success
'disambiguation (2)' '
497 git add secondfile &&
499 test_must_fail git reset secondfile &&
500 test -n "$(git diff --cached --name-only -- secondfile)" &&
505 test_expect_success
'disambiguation (3)' '
509 git add secondfile &&
511 git reset HEAD secondfile &&
512 test_must_fail git diff --quiet &&
513 test -z "$(git diff --cached --name-only)" &&
518 test_expect_success
'disambiguation (4)' '
522 git add secondfile &&
524 git reset -- secondfile &&
525 test_must_fail git diff --quiet &&
526 test -z "$(git diff --cached --name-only)" &&
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
538 test_expect_success
'reset -N keeps removed files as intent-to-add' '
539 echo new-file >new-file &&
543 tree=$(git write-tree) &&
544 git ls-tree $tree new-file >actual &&
546 test_cmp expect actual &&
548 git diff --name-only >actual &&
549 echo new-file >expect &&
550 test_cmp expect actual
553 test_expect_success
'reset --mixed sets up work tree' '
554 git init mixed_worktree &&
560 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
561 test_cmp expect actual