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 '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 &&
195 cat >.cached_expect <<EOF
196 diff --git a/secondfile b/secondfile
197 index $head5p1s..$head5s 100644
205 cat >.cat_expect <<EOF
210 test_expect_success '--soft reset only should show changes in diff --cached' '
211 git reset --soft HEAD^ &&
212 check_changes $head5p1 &&
213 test "$(git rev-parse ORIG_HEAD)" = \
219 cat >.cat_expect <<EOF
225 test_expect_success 'changing files and redo the last commit should succeed' '
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)" = \
236 cat >.cat_expect <<EOF
243 test_expect_success '--hard reset should change the files and undo commits permanently' '
244 git reset --hard HEAD~2 &&
245 check_changes $head5p2 &&
246 test "$(git rev-parse ORIG_HEAD)" = \
251 cat >.cached_expect <<EOF
252 diff --git a/first b/first
253 deleted file mode 100644
254 index $head5p2f..0000000
260 diff --git a/second b/second
261 deleted file mode 100644
262 index $head5p1s..0000000
267 diff --git a/secondfile b/secondfile
269 index 0000000..$head5s
276 cat >.cat_expect <<EOF
281 test_expect_success 'redoing changes adding them without commit them should succeed' '
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 cat >.diff_expect <<EOF
292 diff --git a/first b/first
293 deleted file mode 100644
294 index $head5p2f..0000000
300 diff --git a/second b/second
301 deleted file mode 100644
302 index $head5p1s..0000000
309 cat >.cat_expect <<EOF
314 test_expect_success '--mixed reset to HEAD should unadd the files' '
316 check_changes $head5p2 &&
317 test "$(git rev-parse ORIG_HEAD)" = $head5p2
322 cat >.cat_expect <<EOF
327 test_expect_success 'redoing the last two commits should succeed' '
328 git add secondfile &&
329 git reset --hard $head5p2 &&
332 git mv second secondfile &&
333 git commit -a -m "remove 1st and rename 2nd" &&
335 echo "1st line 2nd file" >secondfile &&
336 echo "2nd line 2nd file" >>secondfile &&
337 # "git commit -m" would break MinGW, as Windows refuse to pass
338 # $test_encoding encoded parameter to git.
339 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
345 cat >.cat_expect <<EOF
351 test_expect_success '--hard reset to HEAD should clear a failed merge' '
352 git branch branch1 &&
353 git branch branch2 &&
355 git checkout branch1 &&
356 echo "3rd line in branch1" >>secondfile &&
357 git commit -a -m "change in branch1" &&
359 git checkout branch2 &&
360 echo "3rd line in branch2" >>secondfile &&
361 git commit -a -m "change in branch2" &&
362 head3=$(git rev-parse --verify HEAD) &&
364 test_must_fail git pull . branch1 &&
371 cat >.cat_expect <<EOF
376 test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
377 git reset --hard HEAD^ &&
378 check_changes $head5 &&
380 git pull . branch1 &&
381 git reset --hard ORIG_HEAD &&
382 check_changes $head5 &&
384 git checkout master &&
385 git branch -D branch1 branch2 &&
389 test_expect_success 'test --mixed <paths>' '
392 git add file1 file2 &&
394 git commit -m files &&
395 before1=$(git rev-parse --short HEAD:file1) &&
396 before2=$(git rev-parse --short HEAD:file2) &&
401 after1=$(git rev-parse --short $(git hash-object file1)) &&
402 after4=$(git rev-parse --short $(git hash-object file4)) &&
403 git add file1 file3 file4 &&
404 git reset HEAD -- file1 file2 file3 &&
405 test_must_fail git diff --quiet &&
408 cat >expect <<-EOF &&
409 diff --git a/file1 b/file1
410 index $before1..$after1 100644
416 diff --git a/file2 b/file2
417 deleted file mode 100644
418 index $before2..0000000
425 test_cmp expect output &&
426 git diff --cached >output &&
428 cat >cached_expect <<-EOF &&
429 diff --git a/file4 b/file4
431 index 0000000..$after4
438 test_cmp cached_expect output
441 test_expect_success 'test resetting the index at give paths' '
445 git update-index --add sub/file1 sub/file2 &&
446 T=$(git write-tree) &&
447 git reset HEAD sub/file2 &&
448 test_must_fail git diff --quiet &&
449 U=$(git write-tree) &&
452 test_must_fail git diff-index --cached --exit-code "$T" &&
456 test_expect_success 'resetting an unmodified path is a no-op' '
458 git reset -- file1 &&
459 git diff-files --exit-code &&
460 git diff-index --cached --exit-code HEAD
464 Unstaged changes after reset:
468 test_expect_success '--mixed refreshes the index' '
470 git reset --mixed HEAD >output &&
471 test_i18ncmp expect output
474 test_expect_success 'resetting specific path that is unmerged' '
475 git rm --cached file2 &&
476 F1=$(git rev-parse HEAD:file1) &&
477 F2=$(git rev-parse HEAD:file2) &&
478 F3=$(git rev-parse HEAD:secondfile) &&
480 echo "100644 $F1 1 file2" &&
481 echo "100644 $F2 2 file2" &&
482 echo "100644 $F3 3 file2"
483 } | git update-index --index-info &&
485 git reset HEAD file2 &&
486 test_must_fail git diff --quiet &&
487 git diff-index --exit-code --cached HEAD
490 test_expect_success 'disambiguation (1)' '
493 git add secondfile &&
494 git reset secondfile &&
495 test_must_fail git diff --quiet -- secondfile &&
496 test -z "$(git diff --cached --name-only)" &&
497 test -f secondfile &&
498 test_must_be_empty secondfile
501 test_expect_success 'disambiguation (2)' '
504 git add secondfile &&
506 test_must_fail git reset secondfile &&
507 test -n "$(git diff --cached --name-only -- secondfile)" &&
511 test_expect_success 'disambiguation (3)' '
514 git add secondfile &&
516 git reset HEAD secondfile &&
517 test_must_fail git diff --quiet &&
518 test -z "$(git diff --cached --name-only)" &&
522 test_expect_success 'disambiguation (4)' '
525 git add secondfile &&
527 git reset -- secondfile &&
528 test_must_fail git diff --quiet &&
529 test -z "$(git diff --cached --name-only)" &&
533 test_expect_success 'reset with paths accepts tree' '
534 # for simpler tests, drop last commit containing added files
535 git reset --hard HEAD^ &&
536 git reset HEAD^^{tree} -- . &&
537 git diff --cached HEAD^ --exit-code &&
538 git diff HEAD --exit-code
541 test_expect_success 'reset -N keeps removed files as intent-to-add' '
542 echo new-file >new-file &&
546 tree=$(git write-tree) &&
547 git ls-tree $tree new-file >actual &&
548 test_must_be_empty actual &&
550 git diff --name-only >actual &&
551 echo new-file >expect &&
552 test_cmp expect actual
555 test_expect_success 'reset --mixed sets up work tree' '
556 git init mixed_worktree &&
561 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
562 test_must_be_empty actual