Start the 2.46 cycle
[git/gitster.git] / t / t7102-reset.sh
blob62d9f846ce86c54745eb87619d14fefc005d6fe3
1 #!/bin/sh
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
13 . ./test-lib.sh
15 commit_msg () {
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"
20 if test -n "$1"
21 then
22 printf "$msg" | iconv -f utf-8 -t "$1"
23 else
24 printf "$msg"
28 # Tested non-UTF-8 encoding
29 test_encoding="ISO8859-1"
31 test_expect_success 'creating initial files and commits' '
32 test_tick &&
33 echo "1st file" >first &&
34 git add first &&
35 git commit -m "create 1st file" &&
37 echo "2nd file" >second &&
38 git add 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) &&
46 git rm 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
63 check_changes () {
64 test "$(git rev-parse HEAD)" = "$1" &&
65 git diff | test_cmp .diff_expect - &&
66 git diff --cached | test_cmp .cached_expect - &&
67 for FILE in *
69 echo $FILE':'
70 cat $FILE || return
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
82 done
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' '
99 >.diff_expect &&
100 >.cached_expect &&
101 cat >.cat_expect <<-\EOF &&
102 secondfile:
103 1st line 2nd file
104 2nd line 2nd file
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 &&
111 check_changes $head5
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 &&
128 check_changes $head5
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 &&
140 check_changes $head5
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" &&
161 git checkout main &&
162 git branch -D branch1 branch2 &&
163 check_changes $head5
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" &&
183 git checkout main &&
184 git branch -D branch3 branch4 &&
185 check_changes $head5
188 test_expect_success 'resetting to HEAD with no changes should succeed and do nothing' '
189 git reset --hard &&
190 check_changes $head5 &&
191 git reset --hard HEAD &&
192 check_changes $head5 &&
193 git reset --soft &&
194 check_changes $head5 &&
195 git reset --soft HEAD &&
196 check_changes $head5 &&
197 git reset --mixed &&
198 check_changes $head5 &&
199 git reset --mixed HEAD &&
200 check_changes $head5 &&
201 git reset &&
202 check_changes $head5 &&
203 git reset HEAD &&
204 check_changes $head5
207 test_expect_success '--soft reset only should show changes in diff --cached' '
208 >.diff_expect &&
209 cat >.cached_expect <<-EOF &&
210 diff --git a/secondfile b/secondfile
211 index $head5p1s..$head5s 100644
212 --- a/secondfile
213 +++ b/secondfile
214 @@ -1 +1,2 @@
215 -2nd file
216 +1st line 2nd file
217 +2nd line 2nd file
219 cat >.cat_expect <<-\EOF &&
220 secondfile:
221 1st line 2nd file
222 2nd line 2nd file
224 git reset --soft HEAD^ &&
225 check_changes $head5p1 &&
226 test "$(git rev-parse ORIG_HEAD)" = \
227 $head5
230 test_expect_success 'changing files and redo the last commit should succeed' '
231 >.diff_expect &&
232 >.cached_expect &&
233 cat >.cat_expect <<-\EOF &&
234 secondfile:
235 1st line 2nd file
236 2nd line 2nd file
237 3rd line 2nd file
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)" = \
244 $head5
247 test_expect_success '--hard reset should change the files and undo commits permanently' '
248 >.diff_expect &&
249 >.cached_expect &&
250 cat >.cat_expect <<-\EOF &&
251 first:
252 1st file
253 2nd line 1st file
254 second:
255 2nd file
257 git reset --hard HEAD~2 &&
258 check_changes $head5p2 &&
259 test "$(git rev-parse ORIG_HEAD)" = \
260 $head4
263 test_expect_success 'redoing changes adding them without commit them should succeed' '
264 >.diff_expect &&
265 cat >.cached_expect <<-EOF &&
266 diff --git a/first b/first
267 deleted file mode 100644
268 index $head5p2f..0000000
269 --- a/first
270 +++ /dev/null
271 @@ -1,2 +0,0 @@
272 -1st file
273 -2nd line 1st file
274 diff --git a/second b/second
275 deleted file mode 100644
276 index $head5p1s..0000000
277 --- a/second
278 +++ /dev/null
279 @@ -1 +0,0 @@
280 -2nd file
281 diff --git a/secondfile b/secondfile
282 new file mode 100644
283 index 0000000..$head5s
284 --- /dev/null
285 +++ b/secondfile
286 @@ -0,0 +1,2 @@
287 +1st line 2nd file
288 +2nd line 2nd file
290 cat >.cat_expect <<-\EOF &&
291 secondfile:
292 1st line 2nd file
293 2nd line 2nd file
295 git rm first &&
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
309 --- a/first
310 +++ /dev/null
311 @@ -1,2 +0,0 @@
312 -1st file
313 -2nd line 1st file
314 diff --git a/second b/second
315 deleted file mode 100644
316 index $head5p1s..0000000
317 --- a/second
318 +++ /dev/null
319 @@ -1 +0,0 @@
320 -2nd file
322 >.cached_expect &&
323 cat >.cat_expect <<-\EOF &&
324 secondfile:
325 1st line 2nd file
326 2nd line 2nd file
328 git reset &&
329 check_changes $head5p2 &&
330 test "$(git rev-parse ORIG_HEAD)" = $head5p2
333 test_expect_success 'redoing the last two commits should succeed' '
334 >.diff_expect &&
335 >.cached_expect &&
336 cat >.cat_expect <<-\EOF &&
337 secondfile:
338 1st line 2nd file
339 2nd line 2nd file
341 git add secondfile &&
342 git reset --hard $head5p2 &&
343 git rm first &&
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 - &&
352 check_changes $head5
355 test_expect_success '--hard reset to HEAD should clear a failed merge' '
356 >.diff_expect &&
357 >.cached_expect &&
358 cat >.cat_expect <<-\EOF &&
359 secondfile:
360 1st line 2nd file
361 2nd line 2nd file
362 3rd line in branch2
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 &&
377 git reset --hard &&
378 check_changes $head3
381 test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
382 >.diff_expect &&
383 >.cached_expect &&
384 cat >.cat_expect <<-\EOF &&
385 secondfile:
386 1st line 2nd file
387 2nd line 2nd file
389 git reset --hard HEAD^ &&
390 check_changes $head5 &&
392 git pull . branch1 &&
393 git reset --hard ORIG_HEAD &&
394 check_changes $head5 &&
396 git checkout main &&
397 git branch -D branch1 branch2 &&
398 check_changes $head5
401 test_expect_success 'test --mixed <paths>' '
402 echo 1 >file1 &&
403 echo 2 >file2 &&
404 git add file1 file2 &&
405 test_tick &&
406 git commit -m files &&
407 before1=$(git rev-parse --short HEAD:file1) &&
408 before2=$(git rev-parse --short HEAD:file2) &&
409 git rm file2 &&
410 echo 3 >file3 &&
411 echo 4 >file4 &&
412 echo 5 >file1 &&
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 &&
418 git diff >output &&
420 cat >expect <<-EOF &&
421 diff --git a/file1 b/file1
422 index $before1..$after1 100644
423 --- a/file1
424 +++ b/file1
425 @@ -1 +1 @@
428 diff --git a/file2 b/file2
429 deleted file mode 100644
430 index $before2..0000000
431 --- a/file2
432 +++ /dev/null
433 @@ -1 +0,0 @@
437 test_cmp expect output &&
438 git diff --cached >output &&
440 cat >cached_expect <<-EOF &&
441 diff --git a/file4 b/file4
442 new file mode 100644
443 index 0000000..$after4
444 --- /dev/null
445 +++ b/file4
446 @@ -0,0 +1 @@
450 test_cmp cached_expect output
453 test_expect_success 'test resetting the index at give paths' '
454 mkdir sub &&
455 >sub/file1 &&
456 >sub/file2 &&
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) &&
462 echo "$T" &&
463 echo "$U" &&
464 test_must_fail git diff-index --cached --exit-code "$T" &&
465 test "$T" != "$U"
468 test_expect_success 'resetting an unmodified path is a no-op' '
469 git reset --hard &&
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 &&
501 # With --quiet
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' '
512 echo 123 >>file2 &&
513 git add file2 &&
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:
520 M file2
522 git update-index --no-skip-worktree file2 &&
523 git add 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 &&
538 git ls-files -u &&
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)' '
545 git reset --hard &&
546 >secondfile &&
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)' '
556 git reset --hard &&
557 >secondfile &&
558 git add secondfile &&
559 rm -f secondfile &&
560 test_must_fail git reset secondfile &&
561 test -n "$(git diff --cached --name-only -- secondfile)" &&
562 test ! -f secondfile
565 test_expect_success 'disambiguation (3)' '
566 git reset --hard &&
567 >secondfile &&
568 git add secondfile &&
569 rm -f secondfile &&
570 git reset HEAD secondfile &&
571 test_must_fail git diff --quiet &&
572 test -z "$(git diff --cached --name-only)" &&
573 test ! -f secondfile
576 test_expect_success 'disambiguation (4)' '
577 git reset --hard &&
578 >secondfile &&
579 git add secondfile &&
580 rm -f secondfile &&
581 git reset -- secondfile &&
582 test_must_fail git diff --quiet &&
583 test -z "$(git diff --cached --name-only)" &&
584 test ! -f secondfile
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 &&
597 git add new-file &&
598 git reset -N HEAD &&
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 &&
612 cd mixed_worktree &&
613 test_commit dummy
614 ) &&
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
627 test_done