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 # Tested non-UTF-8 encoding
26 test_encoding
="ISO8859-1"
28 test_expect_success
'creating initial files and commits' '
30 echo "1st file" >first &&
32 git commit -m "create 1st file" &&
34 echo "2nd file" >second &&
36 git commit -m "create 2nd file" &&
38 echo "2nd line 1st file" >>first &&
39 git commit -a -m "modify 1st file" &&
40 head5p2=$(git rev-parse --verify HEAD) &&
41 head5p2f=$(git rev-parse --short HEAD:first) &&
44 git mv second secondfile &&
45 git commit -a -m "remove 1st and rename 2nd" &&
46 head5p1=$(git rev-parse --verify HEAD) &&
47 head5p1s=$(git rev-parse --short HEAD:secondfile) &&
49 echo "1st line 2nd file" >secondfile &&
50 echo "2nd line 2nd file" >>secondfile &&
51 # "git commit -m" would break MinGW, as Windows refuse to pass
52 # $test_encoding encoded parameter to git.
53 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
54 head5=$(git rev-parse --verify HEAD) &&
55 head5s=$(git rev-parse --short HEAD:secondfile) &&
56 head5sl=$(git rev-parse HEAD:secondfile)
58 # git log --pretty=oneline # to see those SHA1 involved
61 test "$(git rev-parse HEAD)" = "$1" &&
62 git
diff | test_cmp .diff_expect
- &&
63 git
diff --cached | test_cmp .cached_expect
- &&
68 done | test_cmp .cat_expect
-
71 test_expect_success
'reset --hard message' '
72 hex=$(git log -1 --format="%h") &&
73 git reset --hard >.actual &&
74 echo HEAD is now at $hex $(commit_msg) >.expected &&
75 test_i18ncmp .expected .actual
78 test_expect_success
'reset --hard message (ISO8859-1 logoutputencoding)' '
79 hex=$(git log -1 --format="%h") &&
80 git -c "i18n.logOutputEncoding=$test_encoding" reset --hard >.actual &&
81 echo HEAD is now at $hex $(commit_msg $test_encoding) >.expected &&
82 test_i18ncmp .expected .actual
85 test_expect_success
'giving a non existing revision should fail' '
88 cat >.cat_expect <<-\EOF &&
94 test_must_fail git reset aaaaaa &&
95 test_must_fail git reset --mixed aaaaaa &&
96 test_must_fail git reset --soft aaaaaa &&
97 test_must_fail git reset --hard aaaaaa &&
101 test_expect_success
'reset --soft with unmerged index should fail' '
102 touch .git/MERGE_HEAD &&
103 echo "100644 $head5sl 1 un" |
104 git update-index --index-info &&
105 test_must_fail git reset --soft HEAD &&
106 rm .git/MERGE_HEAD &&
107 git rm --cached -- un
110 test_expect_success
'giving paths with options different than --mixed should fail' '
111 test_must_fail git reset --soft -- first &&
112 test_must_fail git reset --hard -- first &&
113 test_must_fail git reset --soft HEAD^ -- first &&
114 test_must_fail git reset --hard HEAD^ -- first &&
118 test_expect_success
'giving unrecognized options should fail' '
119 test_must_fail git reset --other &&
120 test_must_fail git reset -o &&
121 test_must_fail git reset --mixed --other &&
122 test_must_fail git reset --mixed -o &&
123 test_must_fail git reset --soft --other &&
124 test_must_fail git reset --soft -o &&
125 test_must_fail git reset --hard --other &&
126 test_must_fail git reset --hard -o &&
130 test_expect_success
'trying to do reset --soft with pending merge should fail' '
131 git branch branch1 &&
132 git branch branch2 &&
134 git checkout branch1 &&
135 echo "3rd line in branch1" >>secondfile &&
136 git commit -a -m "change in branch1" &&
138 git checkout branch2 &&
139 echo "3rd line in branch2" >>secondfile &&
140 git commit -a -m "change in branch2" &&
142 test_must_fail git merge branch1 &&
143 test_must_fail git reset --soft &&
145 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
146 git commit -a -m "the change in branch2" &&
148 git checkout master &&
149 git branch -D branch1 branch2 &&
153 test_expect_success
'trying to do reset --soft with pending checkout merge should fail' '
154 git branch branch3 &&
155 git branch branch4 &&
157 git checkout branch3 &&
158 echo "3rd line in branch3" >>secondfile &&
159 git commit -a -m "line in branch3" &&
161 git checkout branch4 &&
162 echo "3rd line in branch4" >>secondfile &&
164 git checkout -m branch3 &&
165 test_must_fail git reset --soft &&
167 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
168 git commit -a -m "the line in branch3" &&
170 git checkout master &&
171 git branch -D branch3 branch4 &&
175 test_expect_success
'resetting to HEAD with no changes should succeed and do nothing' '
177 check_changes $head5 &&
178 git reset --hard HEAD &&
179 check_changes $head5 &&
181 check_changes $head5 &&
182 git reset --soft HEAD &&
183 check_changes $head5 &&
185 check_changes $head5 &&
186 git reset --mixed HEAD &&
187 check_changes $head5 &&
189 check_changes $head5 &&
194 test_expect_success
'--soft reset only should show changes in diff --cached' '
196 cat >.cached_expect <<-EOF &&
197 diff --git a/secondfile b/secondfile
198 index $head5p1s..$head5s 100644
206 cat >.cat_expect <<-\EOF &&
211 git reset --soft HEAD^ &&
212 check_changes $head5p1 &&
213 test "$(git rev-parse ORIG_HEAD)" = \
217 test_expect_success
'changing files and redo the last commit should succeed' '
220 cat >.cat_expect <<-\EOF &&
226 echo "3rd line 2nd file" >>secondfile &&
227 git commit -a -C ORIG_HEAD &&
228 head4=$(git rev-parse --verify HEAD) &&
229 check_changes $head4 &&
230 test "$(git rev-parse ORIG_HEAD)" = \
234 test_expect_success
'--hard reset should change the files and undo commits permanently' '
237 cat >.cat_expect <<-\EOF &&
244 git reset --hard HEAD~2 &&
245 check_changes $head5p2 &&
246 test "$(git rev-parse ORIG_HEAD)" = \
250 test_expect_success
'redoing changes adding them without commit them should succeed' '
252 cat >.cached_expect <<-EOF &&
253 diff --git a/first b/first
254 deleted file mode 100644
255 index $head5p2f..0000000
261 diff --git a/second b/second
262 deleted file mode 100644
263 index $head5p1s..0000000
268 diff --git a/secondfile b/secondfile
270 index 0000000..$head5s
277 cat >.cat_expect <<-\EOF &&
283 git mv second secondfile &&
285 echo "1st line 2nd file" >secondfile &&
286 echo "2nd line 2nd file" >>secondfile &&
287 git add secondfile &&
288 check_changes $head5p2
291 test_expect_success
'--mixed reset to HEAD should unadd the files' '
292 cat >.diff_expect <<-EOF &&
293 diff --git a/first b/first
294 deleted file mode 100644
295 index $head5p2f..0000000
301 diff --git a/second b/second
302 deleted file mode 100644
303 index $head5p1s..0000000
310 cat >.cat_expect <<-\EOF &&
316 check_changes $head5p2 &&
317 test "$(git rev-parse ORIG_HEAD)" = $head5p2
320 test_expect_success
'redoing the last two commits should succeed' '
323 cat >.cat_expect <<-\EOF &&
328 git add secondfile &&
329 git reset --hard $head5p2 &&
331 git mv second secondfile &&
332 git commit -a -m "remove 1st and rename 2nd" &&
334 echo "1st line 2nd file" >secondfile &&
335 echo "2nd line 2nd file" >>secondfile &&
336 # "git commit -m" would break MinGW, as Windows refuse to pass
337 # $test_encoding encoded parameter to git.
338 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
342 test_expect_success
'--hard reset to HEAD should clear a failed merge' '
345 cat >.cat_expect <<-\EOF &&
351 git branch branch1 &&
352 git branch branch2 &&
354 git checkout branch1 &&
355 echo "3rd line in branch1" >>secondfile &&
356 git commit -a -m "change in branch1" &&
358 git checkout branch2 &&
359 echo "3rd line in branch2" >>secondfile &&
360 git commit -a -m "change in branch2" &&
361 head3=$(git rev-parse --verify HEAD) &&
363 test_must_fail git pull . branch1 &&
368 test_expect_success
'--hard reset to ORIG_HEAD should clear a fast-forward merge' '
371 cat >.cat_expect <<-\EOF &&
376 git reset --hard HEAD^ &&
377 check_changes $head5 &&
379 git pull . branch1 &&
380 git reset --hard ORIG_HEAD &&
381 check_changes $head5 &&
383 git checkout master &&
384 git branch -D branch1 branch2 &&
388 test_expect_success
'test --mixed <paths>' '
391 git add file1 file2 &&
393 git commit -m files &&
394 before1=$(git rev-parse --short HEAD:file1) &&
395 before2=$(git rev-parse --short HEAD:file2) &&
400 after1=$(git rev-parse --short $(git hash-object file1)) &&
401 after4=$(git rev-parse --short $(git hash-object file4)) &&
402 git add file1 file3 file4 &&
403 git reset HEAD -- file1 file2 file3 &&
404 test_must_fail git diff --quiet &&
407 cat >expect <<-EOF &&
408 diff --git a/file1 b/file1
409 index $before1..$after1 100644
415 diff --git a/file2 b/file2
416 deleted file mode 100644
417 index $before2..0000000
424 test_cmp expect output &&
425 git diff --cached >output &&
427 cat >cached_expect <<-EOF &&
428 diff --git a/file4 b/file4
430 index 0000000..$after4
437 test_cmp cached_expect output
440 test_expect_success
'test resetting the index at give paths' '
444 git update-index --add sub/file1 sub/file2 &&
445 T=$(git write-tree) &&
446 git reset HEAD sub/file2 &&
447 test_must_fail git diff --quiet &&
448 U=$(git write-tree) &&
451 test_must_fail git diff-index --cached --exit-code "$T" &&
455 test_expect_success
'resetting an unmodified path is a no-op' '
457 git reset -- file1 &&
458 git diff-files --exit-code &&
459 git diff-index --cached --exit-code HEAD
462 test_expect_success
'--mixed refreshes the index' '
463 cat >expect <<-\EOF &&
464 Unstaged changes after reset:
468 git reset --mixed HEAD >output &&
469 test_i18ncmp expect output
472 test_expect_success
'resetting specific path that is unmerged' '
473 git rm --cached file2 &&
474 F1=$(git rev-parse HEAD:file1) &&
475 F2=$(git rev-parse HEAD:file2) &&
476 F3=$(git rev-parse HEAD:secondfile) &&
478 echo "100644 $F1 1 file2" &&
479 echo "100644 $F2 2 file2" &&
480 echo "100644 $F3 3 file2"
481 } | git update-index --index-info &&
483 git reset HEAD file2 &&
484 test_must_fail git diff --quiet &&
485 git diff-index --exit-code --cached HEAD
488 test_expect_success
'disambiguation (1)' '
491 git add secondfile &&
492 git reset secondfile &&
493 test_must_fail git diff --quiet -- secondfile &&
494 test -z "$(git diff --cached --name-only)" &&
495 test -f secondfile &&
496 test_must_be_empty secondfile
499 test_expect_success
'disambiguation (2)' '
502 git add secondfile &&
504 test_must_fail git reset secondfile &&
505 test -n "$(git diff --cached --name-only -- secondfile)" &&
509 test_expect_success
'disambiguation (3)' '
512 git add secondfile &&
514 git reset HEAD secondfile &&
515 test_must_fail git diff --quiet &&
516 test -z "$(git diff --cached --name-only)" &&
520 test_expect_success
'disambiguation (4)' '
523 git add secondfile &&
525 git reset -- secondfile &&
526 test_must_fail git diff --quiet &&
527 test -z "$(git diff --cached --name-only)" &&
531 test_expect_success
'reset with paths accepts tree' '
532 # for simpler tests, drop last commit containing added files
533 git reset --hard HEAD^ &&
534 git reset HEAD^^{tree} -- . &&
535 git diff --cached HEAD^ --exit-code &&
536 git diff HEAD --exit-code
539 test_expect_success
'reset -N keeps removed files as intent-to-add' '
540 echo new-file >new-file &&
544 tree=$(git write-tree) &&
545 git ls-tree $tree new-file >actual &&
546 test_must_be_empty 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 &&
559 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
560 test_must_be_empty actual