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
87 cat >.cat_expect
<<EOF
93 test_expect_success
'giving a non existing revision should fail' '
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 \
111 'giving paths with options different than --mixed should fail' '
112 test_must_fail git reset --soft -- first &&
113 test_must_fail git reset --hard -- first &&
114 test_must_fail git reset --soft HEAD^ -- first &&
115 test_must_fail git reset --hard HEAD^ -- first &&
119 test_expect_success
'giving unrecognized options should fail' '
120 test_must_fail git reset --other &&
121 test_must_fail git reset -o &&
122 test_must_fail git reset --mixed --other &&
123 test_must_fail git reset --mixed -o &&
124 test_must_fail git reset --soft --other &&
125 test_must_fail git reset --soft -o &&
126 test_must_fail git reset --hard --other &&
127 test_must_fail git reset --hard -o &&
131 test_expect_success \
132 'trying to do reset --soft with pending merge should fail' '
133 git branch branch1 &&
134 git branch branch2 &&
136 git checkout branch1 &&
137 echo "3rd line in branch1" >>secondfile &&
138 git commit -a -m "change in branch1" &&
140 git checkout branch2 &&
141 echo "3rd line in branch2" >>secondfile &&
142 git commit -a -m "change in branch2" &&
144 test_must_fail git merge branch1 &&
145 test_must_fail git reset --soft &&
147 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
148 git commit -a -m "the change in branch2" &&
150 git checkout master &&
151 git branch -D branch1 branch2 &&
155 test_expect_success \
156 'trying to do reset --soft with pending checkout merge should fail' '
157 git branch branch3 &&
158 git branch branch4 &&
160 git checkout branch3 &&
161 echo "3rd line in branch3" >>secondfile &&
162 git commit -a -m "line in branch3" &&
164 git checkout branch4 &&
165 echo "3rd line in branch4" >>secondfile &&
167 git checkout -m branch3 &&
168 test_must_fail git reset --soft &&
170 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
171 git commit -a -m "the line in branch3" &&
173 git checkout master &&
174 git branch -D branch3 branch4 &&
178 test_expect_success \
179 'resetting to HEAD with no changes should succeed and do nothing' '
181 check_changes $head5 &&
182 git reset --hard HEAD &&
183 check_changes $head5 &&
185 check_changes $head5 &&
186 git reset --soft HEAD &&
187 check_changes $head5 &&
189 check_changes $head5 &&
190 git reset --mixed HEAD &&
191 check_changes $head5 &&
193 check_changes $head5 &&
199 cat >.cached_expect
<<EOF
200 diff --git a/secondfile b/secondfile
201 index $head5p1s..$head5s 100644
209 cat >.cat_expect
<<EOF
214 test_expect_success
'--soft reset only should show changes in diff --cached' '
215 git reset --soft HEAD^ &&
216 check_changes $head5p1 &&
217 test "$(git rev-parse ORIG_HEAD)" = \
223 cat >.cat_expect
<<EOF
229 test_expect_success \
230 'changing files and redo the last commit should succeed' '
231 echo "3rd line 2nd file" >>secondfile &&
232 git commit -a -C ORIG_HEAD &&
233 head4=$(git rev-parse --verify HEAD) &&
234 check_changes $head4 &&
235 test "$(git rev-parse ORIG_HEAD)" = \
241 cat >.cat_expect
<<EOF
248 test_expect_success \
249 '--hard reset should change the files and undo commits permanently' '
250 git reset --hard HEAD~2 &&
251 check_changes $head5p2 &&
252 test "$(git rev-parse ORIG_HEAD)" = \
257 cat >.cached_expect
<<EOF
258 diff --git a/first b/first
259 deleted file mode 100644
260 index $head5p2f..0000000
266 diff --git a/second b/second
267 deleted file mode 100644
268 index $head5p1s..0000000
273 diff --git a/secondfile b/secondfile
275 index 0000000..$head5s
282 cat >.cat_expect
<<EOF
287 test_expect_success \
288 'redoing changes adding them without commit them should succeed' '
290 git mv second secondfile &&
292 echo "1st line 2nd file" >secondfile &&
293 echo "2nd line 2nd file" >>secondfile &&
294 git add secondfile &&
295 check_changes $head5p2
298 cat >.diff_expect
<<EOF
299 diff --git a/first b/first
300 deleted file mode 100644
301 index $head5p2f..0000000
307 diff --git a/second b/second
308 deleted file mode 100644
309 index $head5p1s..0000000
316 cat >.cat_expect
<<EOF
321 test_expect_success
'--mixed reset to HEAD should unadd the files' '
323 check_changes $head5p2 &&
324 test "$(git rev-parse ORIG_HEAD)" = $head5p2
329 cat >.cat_expect
<<EOF
334 test_expect_success
'redoing the last two commits should succeed' '
335 git add secondfile &&
336 git reset --hard $head5p2 &&
339 git mv second secondfile &&
340 git commit -a -m "remove 1st and rename 2nd" &&
342 echo "1st line 2nd file" >secondfile &&
343 echo "2nd line 2nd file" >>secondfile &&
344 # "git commit -m" would break MinGW, as Windows refuse to pass
345 # $test_encoding encoded parameter to git.
346 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
352 cat >.cat_expect
<<EOF
358 test_expect_success
'--hard reset to HEAD should clear a failed merge' '
359 git branch branch1 &&
360 git branch branch2 &&
362 git checkout branch1 &&
363 echo "3rd line in branch1" >>secondfile &&
364 git commit -a -m "change in branch1" &&
366 git checkout branch2 &&
367 echo "3rd line in branch2" >>secondfile &&
368 git commit -a -m "change in branch2" &&
369 head3=$(git rev-parse --verify HEAD) &&
371 test_must_fail git pull . branch1 &&
378 cat >.cat_expect
<<EOF
383 test_expect_success \
384 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
385 git reset --hard HEAD^ &&
386 check_changes $head5 &&
388 git pull . branch1 &&
389 git reset --hard ORIG_HEAD &&
390 check_changes $head5 &&
392 git checkout master &&
393 git branch -D branch1 branch2 &&
397 test_expect_success
'test --mixed <paths>' '
400 git add file1 file2 &&
402 git commit -m files &&
403 before1=$(git rev-parse --short HEAD:file1) &&
404 before2=$(git rev-parse --short HEAD:file2) &&
409 after1=$(git rev-parse --short $(git hash-object file1)) &&
410 after4=$(git rev-parse --short $(git hash-object file4)) &&
411 git add file1 file3 file4 &&
412 git reset HEAD -- file1 file2 file3 &&
413 test_must_fail git diff --quiet &&
416 cat > expect <<-EOF &&
417 diff --git a/file1 b/file1
418 index $before1..$after1 100644
424 diff --git a/file2 b/file2
425 deleted file mode 100644
426 index $before2..0000000
433 test_cmp expect output &&
434 git diff --cached > output &&
436 cat > cached_expect <<-EOF &&
437 diff --git a/file4 b/file4
439 index 0000000..$after4
446 test_cmp cached_expect output
449 test_expect_success
'test resetting the index at give paths' '
454 git update-index --add sub/file1 sub/file2 &&
455 T=$(git write-tree) &&
456 git reset HEAD sub/file2 &&
457 test_must_fail git diff --quiet &&
458 U=$(git write-tree) &&
461 test_must_fail git diff-index --cached --exit-code "$T" &&
466 test_expect_success
'resetting an unmodified path is a no-op' '
468 git reset -- file1 &&
469 git diff-files --exit-code &&
470 git diff-index --cached --exit-code HEAD
474 Unstaged changes after reset:
478 test_expect_success
'--mixed refreshes the index' '
480 git reset --mixed HEAD > output &&
481 test_i18ncmp expect output
484 test_expect_success
'resetting specific path that is unmerged' '
485 git rm --cached file2 &&
486 F1=$(git rev-parse HEAD:file1) &&
487 F2=$(git rev-parse HEAD:file2) &&
488 F3=$(git rev-parse HEAD:secondfile) &&
490 echo "100644 $F1 1 file2" &&
491 echo "100644 $F2 2 file2" &&
492 echo "100644 $F3 3 file2"
493 } | git update-index --index-info &&
495 git reset HEAD file2 &&
496 test_must_fail git diff --quiet &&
497 git diff-index --exit-code --cached HEAD
500 test_expect_success
'disambiguation (1)' '
504 git add secondfile &&
505 git reset secondfile &&
506 test_must_fail git diff --quiet -- secondfile &&
507 test -z "$(git diff --cached --name-only)" &&
508 test -f secondfile &&
509 test_must_be_empty secondfile
513 test_expect_success
'disambiguation (2)' '
517 git add secondfile &&
519 test_must_fail git reset secondfile &&
520 test -n "$(git diff --cached --name-only -- secondfile)" &&
525 test_expect_success
'disambiguation (3)' '
529 git add secondfile &&
531 git reset HEAD secondfile &&
532 test_must_fail git diff --quiet &&
533 test -z "$(git diff --cached --name-only)" &&
538 test_expect_success
'disambiguation (4)' '
542 git add secondfile &&
544 git reset -- secondfile &&
545 test_must_fail git diff --quiet &&
546 test -z "$(git diff --cached --name-only)" &&
550 test_expect_success
'reset with paths accepts tree' '
551 # for simpler tests, drop last commit containing added files
552 git reset --hard HEAD^ &&
553 git reset HEAD^^{tree} -- . &&
554 git diff --cached HEAD^ --exit-code &&
555 git diff HEAD --exit-code
558 test_expect_success
'reset -N keeps removed files as intent-to-add' '
559 echo new-file >new-file &&
563 tree=$(git write-tree) &&
564 git ls-tree $tree new-file >actual &&
565 test_must_be_empty actual &&
567 git diff --name-only >actual &&
568 echo new-file >expect &&
569 test_cmp expect actual
572 test_expect_success
'reset --mixed sets up work tree' '
573 git init mixed_worktree &&
578 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
579 test_must_be_empty actual