3 # Copyright (c) 2007 Carlos Rica
6 test_description
='git reset
8 Documented tests for git reset'
10 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
11 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
16 # String "modify 2nd file (changed)" partly in German
17 # (translated with Google Translate),
18 # encoded in UTF-8, used as a commit log message below.
19 msg
="modify 2nd file (ge\303\244ndert)\n"
22 printf "$msg" |
iconv -f utf-8
-t "$1"
28 # Tested non-UTF-8 encoding
29 test_encoding
="ISO8859-1"
31 test_expect_success
'creating initial files and commits' '
33 echo "1st file" >first &&
35 git commit -m "create 1st file" &&
37 echo "2nd file" >second &&
39 git commit -m "create 2nd file" &&
41 echo "2nd line 1st file" >>first &&
42 git commit -a -m "modify 1st file" &&
43 head5p2=$(git rev-parse --verify HEAD) &&
44 head5p2f=$(git rev-parse --short HEAD:first) &&
47 git mv second secondfile &&
48 git commit -a -m "remove 1st and rename 2nd" &&
49 head5p1=$(git rev-parse --verify HEAD) &&
50 head5p1s=$(git rev-parse --short HEAD:secondfile) &&
52 echo "1st line 2nd file" >secondfile &&
53 echo "2nd line 2nd file" >>secondfile &&
54 # "git commit -m" would break MinGW, as Windows refuse to pass
55 # $test_encoding encoded parameter to git.
56 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
57 head5=$(git rev-parse --verify HEAD) &&
58 head5s=$(git rev-parse --short HEAD:secondfile) &&
59 head5sl=$(git rev-parse HEAD:secondfile)
61 # git log --pretty=oneline # to see those SHA1 involved
64 test "$(git rev-parse HEAD)" = "$1" &&
65 git
diff | test_cmp .diff_expect
- &&
66 git
diff --cached | test_cmp .cached_expect
- &&
71 done | test_cmp .cat_expect
-
74 # no negated form for various type of resets
75 for opt
in soft mixed hard merge keep
77 test_expect_success
"no 'git reset --no-$opt'" '
78 test_when_finished "rm -f err" &&
79 test_must_fail git reset --no-$opt 2>err &&
80 grep "error: unknown option .no-$opt." err
84 test_expect_success
'reset --hard message' '
85 hex=$(git log -1 --format="%h") &&
86 git reset --hard >.actual &&
87 echo HEAD is now at $hex $(commit_msg) >.expected &&
88 test_cmp .expected .actual
91 test_expect_success
'reset --hard message (ISO8859-1 logoutputencoding)' '
92 hex=$(git log -1 --format="%h") &&
93 git -c "i18n.logOutputEncoding=$test_encoding" reset --hard >.actual &&
94 echo HEAD is now at $hex $(commit_msg $test_encoding) >.expected &&
95 test_cmp .expected .actual
98 test_expect_success
'giving a non existing revision should fail' '
101 cat >.cat_expect <<-\EOF &&
107 test_must_fail git reset aaaaaa &&
108 test_must_fail git reset --mixed aaaaaa &&
109 test_must_fail git reset --soft aaaaaa &&
110 test_must_fail git reset --hard aaaaaa &&
114 test_expect_success
'reset --soft with unmerged index should fail' '
115 touch .git/MERGE_HEAD &&
116 echo "100644 $head5sl 1 un" |
117 git update-index --index-info &&
118 test_must_fail git reset --soft HEAD &&
119 rm .git/MERGE_HEAD &&
120 git rm --cached -- un
123 test_expect_success
'giving paths with options different than --mixed should fail' '
124 test_must_fail git reset --soft -- first &&
125 test_must_fail git reset --hard -- first &&
126 test_must_fail git reset --soft HEAD^ -- first &&
127 test_must_fail git reset --hard HEAD^ -- first &&
131 test_expect_success
'giving unrecognized options should fail' '
132 test_must_fail git reset --other &&
133 test_must_fail git reset -o &&
134 test_must_fail git reset --mixed --other &&
135 test_must_fail git reset --mixed -o &&
136 test_must_fail git reset --soft --other &&
137 test_must_fail git reset --soft -o &&
138 test_must_fail git reset --hard --other &&
139 test_must_fail git reset --hard -o &&
143 test_expect_success
'trying to do reset --soft with pending merge should fail' '
144 git branch branch1 &&
145 git branch branch2 &&
147 git checkout branch1 &&
148 echo "3rd line in branch1" >>secondfile &&
149 git commit -a -m "change in branch1" &&
151 git checkout branch2 &&
152 echo "3rd line in branch2" >>secondfile &&
153 git commit -a -m "change in branch2" &&
155 test_must_fail git merge branch1 &&
156 test_must_fail git reset --soft &&
158 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
159 git commit -a -m "the change in branch2" &&
162 git branch -D branch1 branch2 &&
166 test_expect_success
'trying to do reset --soft with pending checkout merge should fail' '
167 git branch branch3 &&
168 git branch branch4 &&
170 git checkout branch3 &&
171 echo "3rd line in branch3" >>secondfile &&
172 git commit -a -m "line in branch3" &&
174 git checkout branch4 &&
175 echo "3rd line in branch4" >>secondfile &&
177 git checkout -m branch3 &&
178 test_must_fail git reset --soft &&
180 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
181 git commit -a -m "the line in branch3" &&
184 git branch -D branch3 branch4 &&
188 test_expect_success
'resetting to HEAD with no changes should succeed and do nothing' '
190 check_changes $head5 &&
191 git reset --hard HEAD &&
192 check_changes $head5 &&
194 check_changes $head5 &&
195 git reset --soft HEAD &&
196 check_changes $head5 &&
198 check_changes $head5 &&
199 git reset --mixed HEAD &&
200 check_changes $head5 &&
202 check_changes $head5 &&
207 test_expect_success
'--soft reset only should show changes in diff --cached' '
209 cat >.cached_expect <<-EOF &&
210 diff --git a/secondfile b/secondfile
211 index $head5p1s..$head5s 100644
219 cat >.cat_expect <<-\EOF &&
224 git reset --soft HEAD^ &&
225 check_changes $head5p1 &&
226 test "$(git rev-parse ORIG_HEAD)" = \
230 test_expect_success
'changing files and redo the last commit should succeed' '
233 cat >.cat_expect <<-\EOF &&
239 echo "3rd line 2nd file" >>secondfile &&
240 git commit -a -C ORIG_HEAD &&
241 head4=$(git rev-parse --verify HEAD) &&
242 check_changes $head4 &&
243 test "$(git rev-parse ORIG_HEAD)" = \
247 test_expect_success
'--hard reset should change the files and undo commits permanently' '
250 cat >.cat_expect <<-\EOF &&
257 git reset --hard HEAD~2 &&
258 check_changes $head5p2 &&
259 test "$(git rev-parse ORIG_HEAD)" = \
263 test_expect_success
'redoing changes adding them without commit them should succeed' '
265 cat >.cached_expect <<-EOF &&
266 diff --git a/first b/first
267 deleted file mode 100644
268 index $head5p2f..0000000
274 diff --git a/second b/second
275 deleted file mode 100644
276 index $head5p1s..0000000
281 diff --git a/secondfile b/secondfile
283 index 0000000..$head5s
290 cat >.cat_expect <<-\EOF &&
296 git mv second secondfile &&
298 echo "1st line 2nd file" >secondfile &&
299 echo "2nd line 2nd file" >>secondfile &&
300 git add secondfile &&
301 check_changes $head5p2
304 test_expect_success
'--mixed reset to HEAD should unadd the files' '
305 cat >.diff_expect <<-EOF &&
306 diff --git a/first b/first
307 deleted file mode 100644
308 index $head5p2f..0000000
314 diff --git a/second b/second
315 deleted file mode 100644
316 index $head5p1s..0000000
323 cat >.cat_expect <<-\EOF &&
329 check_changes $head5p2 &&
330 test "$(git rev-parse ORIG_HEAD)" = $head5p2
333 test_expect_success
'redoing the last two commits should succeed' '
336 cat >.cat_expect <<-\EOF &&
341 git add secondfile &&
342 git reset --hard $head5p2 &&
344 git mv second secondfile &&
345 git commit -a -m "remove 1st and rename 2nd" &&
347 echo "1st line 2nd file" >secondfile &&
348 echo "2nd line 2nd file" >>secondfile &&
349 # "git commit -m" would break MinGW, as Windows refuse to pass
350 # $test_encoding encoded parameter to git.
351 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
355 test_expect_success
'--hard reset to HEAD should clear a failed merge' '
358 cat >.cat_expect <<-\EOF &&
364 git branch branch1 &&
365 git branch branch2 &&
367 git checkout branch1 &&
368 echo "3rd line in branch1" >>secondfile &&
369 git commit -a -m "change in branch1" &&
371 git checkout branch2 &&
372 echo "3rd line in branch2" >>secondfile &&
373 git commit -a -m "change in branch2" &&
374 head3=$(git rev-parse --verify HEAD) &&
376 test_must_fail git pull . branch1 &&
381 test_expect_success
'--hard reset to ORIG_HEAD should clear a fast-forward merge' '
384 cat >.cat_expect <<-\EOF &&
389 git reset --hard HEAD^ &&
390 check_changes $head5 &&
392 git pull . branch1 &&
393 git reset --hard ORIG_HEAD &&
394 check_changes $head5 &&
397 git branch -D branch1 branch2 &&
401 test_expect_success
'test --mixed <paths>' '
404 git add file1 file2 &&
406 git commit -m files &&
407 before1=$(git rev-parse --short HEAD:file1) &&
408 before2=$(git rev-parse --short HEAD:file2) &&
413 after1=$(git rev-parse --short $(git hash-object file1)) &&
414 after4=$(git rev-parse --short $(git hash-object file4)) &&
415 git add file1 file3 file4 &&
416 git reset HEAD -- file1 file2 file3 &&
417 test_must_fail git diff --quiet &&
420 cat >expect <<-EOF &&
421 diff --git a/file1 b/file1
422 index $before1..$after1 100644
428 diff --git a/file2 b/file2
429 deleted file mode 100644
430 index $before2..0000000
437 test_cmp expect output &&
438 git diff --cached >output &&
440 cat >cached_expect <<-EOF &&
441 diff --git a/file4 b/file4
443 index 0000000..$after4
450 test_cmp cached_expect output
453 test_expect_success
'test resetting the index at give paths' '
457 git update-index --add sub/file1 sub/file2 &&
458 T=$(git write-tree) &&
459 git reset HEAD sub/file2 &&
460 test_must_fail git diff --quiet &&
461 U=$(git write-tree) &&
464 test_must_fail git diff-index --cached --exit-code "$T" &&
468 test_expect_success
'resetting an unmodified path is a no-op' '
470 git reset -- file1 &&
471 git diff-files --exit-code &&
472 git diff-index --cached --exit-code HEAD
475 test_reset_refreshes_index
() {
477 # To test whether the index is refreshed in `git reset --mixed` with
478 # the given options, create a scenario where we clearly see different
479 # results depending on whether the refresh occurred or not.
481 # Step 0: start with a clean index
482 git
reset --hard HEAD
&&
484 # Step 1: remove file2, but only in the index (no change to worktree)
485 git
rm --cached file2
&&
487 # Step 2: reset index & leave worktree unchanged from HEAD
488 git
$1 reset $2 --mixed HEAD
&&
490 # Step 3: verify whether the index is refreshed by checking whether
491 # file2 still has staged changes in the index differing from HEAD (if
492 # the refresh occurred, there should be no such changes)
493 git diff-files
>output.log
&&
494 test_must_be_empty output.log
497 test_expect_success
'--mixed refreshes the index' '
498 # Verify default behavior (without --[no-]refresh or reset.refresh)
499 test_reset_refreshes_index &&
502 test_reset_refreshes_index "" --quiet
505 test_expect_success
'--mixed --[no-]refresh sets refresh behavior' '
506 # Verify that --[no-]refresh controls index refresh
507 test_reset_refreshes_index "" --refresh &&
508 ! test_reset_refreshes_index "" --no-refresh
511 test_expect_success
'--mixed preserves skip-worktree' '
514 git update-index --skip-worktree file2 &&
515 git reset --mixed HEAD >output &&
516 test_must_be_empty output &&
518 cat >expect <<-\EOF &&
519 Unstaged changes after reset:
522 git update-index --no-skip-worktree file2 &&
524 git reset --mixed HEAD >output &&
525 test_cmp expect output
528 test_expect_success
'resetting specific path that is unmerged' '
529 git rm --cached file2 &&
530 F1=$(git rev-parse HEAD:file1) &&
531 F2=$(git rev-parse HEAD:file2) &&
532 F3=$(git rev-parse HEAD:secondfile) &&
534 echo "100644 $F1 1 file2" &&
535 echo "100644 $F2 2 file2" &&
536 echo "100644 $F3 3 file2"
537 } | git update-index --index-info &&
539 git reset HEAD file2 &&
540 test_must_fail git diff --quiet &&
541 git diff-index --exit-code --cached HEAD
544 test_expect_success
'disambiguation (1)' '
547 git add secondfile &&
548 git reset secondfile &&
549 test_must_fail git diff --quiet -- secondfile &&
550 test -z "$(git diff --cached --name-only)" &&
551 test -f secondfile &&
552 test_must_be_empty secondfile
555 test_expect_success
'disambiguation (2)' '
558 git add secondfile &&
560 test_must_fail git reset secondfile &&
561 test -n "$(git diff --cached --name-only -- secondfile)" &&
565 test_expect_success
'disambiguation (3)' '
568 git add secondfile &&
570 git reset HEAD secondfile &&
571 test_must_fail git diff --quiet &&
572 test -z "$(git diff --cached --name-only)" &&
576 test_expect_success
'disambiguation (4)' '
579 git add secondfile &&
581 git reset -- secondfile &&
582 test_must_fail git diff --quiet &&
583 test -z "$(git diff --cached --name-only)" &&
587 test_expect_success
'reset with paths accepts tree' '
588 # for simpler tests, drop last commit containing added files
589 git reset --hard HEAD^ &&
590 git reset HEAD^^{tree} -- . &&
591 git diff --cached HEAD^ --exit-code &&
592 git diff HEAD --exit-code
595 test_expect_success
'reset -N keeps removed files as intent-to-add' '
596 echo new-file >new-file &&
600 tree=$(git write-tree) &&
601 git ls-tree $tree new-file >actual &&
602 test_must_be_empty actual &&
604 git diff --name-only >actual &&
605 echo new-file >expect &&
606 test_cmp expect actual
609 test_expect_success
'reset --mixed sets up work tree' '
610 git init mixed_worktree &&
615 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
616 test_must_be_empty actual
619 test_expect_success
'reset handles --end-of-options' '
620 git update-ref refs/heads/--foo HEAD^ &&
621 git log -1 --format=%s refs/heads/--foo >expect &&
622 git reset --hard --end-of-options --foo &&
623 git log -1 --format=%s HEAD >actual &&
624 test_cmp expect actual