]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7102-reset.sh
The third batch
[thirdparty/git.git] / t / t7102-reset.sh
CommitLineData
359048d6
CR
1#!/bin/sh
2#
3# Copyright (c) 2007 Carlos Rica
4#
5
d592b315 6test_description='git reset
359048d6 7
d592b315 8Documented tests for git reset'
359048d6 9
01dc8133 10GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
11export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12
359048d6
CR
13. ./test-lib.sh
14
de6029a2 15commit_msg () {
17cc2ef1
AS
16 # String "modify 2nd file (changed)" partly in German
17 # (translated with Google Translate),
de6029a2 18 # encoded in UTF-8, used as a commit log message below.
17cc2ef1 19 msg="modify 2nd file (ge\303\244ndert)\n"
de6029a2
AS
20 if test -n "$1"
21 then
17cc2ef1
AS
22 printf "$msg" | iconv -f utf-8 -t "$1"
23 else
24 printf "$msg"
de6029a2 25 fi
de6029a2
AS
26}
27
ee3efaf6
AS
28# Tested non-UTF-8 encoding
29test_encoding="ISO8859-1"
30
359048d6
CR
31test_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" &&
36
37 echo "2nd file" >second &&
38 git add second &&
39 git commit -m "create 2nd file" &&
40
41 echo "2nd line 1st file" >>first &&
42 git commit -a -m "modify 1st file" &&
d62607d1 43 head5p2=$(git rev-parse --verify HEAD) &&
44 head5p2f=$(git rev-parse --short HEAD:first) &&
359048d6
CR
45
46 git rm first &&
47 git mv second secondfile &&
48 git commit -a -m "remove 1st and rename 2nd" &&
d62607d1 49 head5p1=$(git rev-parse --verify HEAD) &&
50 head5p1s=$(git rev-parse --short HEAD:secondfile) &&
359048d6
CR
51
52 echo "1st line 2nd file" >secondfile &&
53 echo "2nd line 2nd file" >>secondfile &&
e6ce2be2
PT
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 - &&
d62607d1 57 head5=$(git rev-parse --verify HEAD) &&
58 head5s=$(git rev-parse --short HEAD:secondfile) &&
59 head5sl=$(git rev-parse HEAD:secondfile)
359048d6
CR
60'
61# git log --pretty=oneline # to see those SHA1 involved
62
63check_changes () {
64 test "$(git rev-parse HEAD)" = "$1" &&
3af82863
JH
65 git diff | test_cmp .diff_expect - &&
66 git diff --cached | test_cmp .cached_expect - &&
359048d6
CR
67 for FILE in *
68 do
69 echo $FILE':'
70 cat $FILE || return
3af82863 71 done | test_cmp .cat_expect -
359048d6
CR
72}
73
3821eb6c
JH
74# no negated form for various type of resets
75for opt in soft mixed hard merge keep
76do
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
81 '
82done
83
ecaee805 84test_expect_success 'reset --hard message' '
de6029a2 85 hex=$(git log -1 --format="%h") &&
c327762f
CM
86 git reset --hard >.actual &&
87 echo HEAD is now at $hex $(commit_msg) >.expected &&
1108cea7 88 test_cmp .expected .actual
de6029a2
AS
89'
90
ee3efaf6 91test_expect_success 'reset --hard message (ISO8859-1 logoutputencoding)' '
de6029a2 92 hex=$(git log -1 --format="%h") &&
c327762f
CM
93 git -c "i18n.logOutputEncoding=$test_encoding" reset --hard >.actual &&
94 echo HEAD is now at $hex $(commit_msg $test_encoding) >.expected &&
1108cea7 95 test_cmp .expected .actual
de6029a2
AS
96'
97
359048d6 98test_expect_success 'giving a non existing revision should fail' '
31f4c833
JH
99 >.diff_expect &&
100 >.cached_expect &&
101 cat >.cat_expect <<-\EOF &&
102 secondfile:
103 1st line 2nd file
104 2nd line 2nd file
105 EOF
106
d492b31c
SB
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 &&
8b66f785 111 check_changes $head5
359048d6
CR
112'
113
cdf4a751
JS
114test_expect_success 'reset --soft with unmerged index should fail' '
115 touch .git/MERGE_HEAD &&
d62607d1 116 echo "100644 $head5sl 1 un" |
cdf4a751 117 git update-index --index-info &&
d492b31c 118 test_must_fail git reset --soft HEAD &&
cdf4a751
JS
119 rm .git/MERGE_HEAD &&
120 git rm --cached -- un
121'
122
e166fe36 123test_expect_success 'giving paths with options different than --mixed should fail' '
d492b31c
SB
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 &&
8b66f785 128 check_changes $head5
359048d6
CR
129'
130
131test_expect_success 'giving unrecognized options should fail' '
d492b31c
SB
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 &&
8b66f785 140 check_changes $head5
359048d6
CR
141'
142
e166fe36 143test_expect_success 'trying to do reset --soft with pending merge should fail' '
359048d6
CR
144 git branch branch1 &&
145 git branch branch2 &&
146
147 git checkout branch1 &&
148 echo "3rd line in branch1" >>secondfile &&
149 git commit -a -m "change in branch1" &&
150
151 git checkout branch2 &&
152 echo "3rd line in branch2" >>secondfile &&
153 git commit -a -m "change in branch2" &&
154
d492b31c
SB
155 test_must_fail git merge branch1 &&
156 test_must_fail git reset --soft &&
359048d6
CR
157
158 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
159 git commit -a -m "the change in branch2" &&
160
01dc8133 161 git checkout main &&
359048d6 162 git branch -D branch1 branch2 &&
8b66f785 163 check_changes $head5
359048d6
CR
164'
165
e166fe36 166test_expect_success 'trying to do reset --soft with pending checkout merge should fail' '
359048d6
CR
167 git branch branch3 &&
168 git branch branch4 &&
169
170 git checkout branch3 &&
171 echo "3rd line in branch3" >>secondfile &&
172 git commit -a -m "line in branch3" &&
173
174 git checkout branch4 &&
175 echo "3rd line in branch4" >>secondfile &&
176
177 git checkout -m branch3 &&
d492b31c 178 test_must_fail git reset --soft &&
359048d6
CR
179
180 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
181 git commit -a -m "the line in branch3" &&
182
01dc8133 183 git checkout main &&
359048d6 184 git branch -D branch3 branch4 &&
8b66f785 185 check_changes $head5
359048d6
CR
186'
187
e166fe36 188test_expect_success 'resetting to HEAD with no changes should succeed and do nothing' '
359048d6 189 git reset --hard &&
8b66f785 190 check_changes $head5 &&
359048d6 191 git reset --hard HEAD &&
8b66f785 192 check_changes $head5 &&
359048d6 193 git reset --soft &&
8b66f785 194 check_changes $head5 &&
359048d6 195 git reset --soft HEAD &&
8b66f785 196 check_changes $head5 &&
359048d6 197 git reset --mixed &&
8b66f785 198 check_changes $head5 &&
359048d6 199 git reset --mixed HEAD &&
8b66f785 200 check_changes $head5 &&
359048d6 201 git reset &&
8b66f785 202 check_changes $head5 &&
359048d6 203 git reset HEAD &&
8b66f785 204 check_changes $head5
359048d6
CR
205'
206
359048d6 207test_expect_success '--soft reset only should show changes in diff --cached' '
31f4c833
JH
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
218 EOF
219 cat >.cat_expect <<-\EOF &&
220 secondfile:
221 1st line 2nd file
222 2nd line 2nd file
223 EOF
359048d6 224 git reset --soft HEAD^ &&
d62607d1 225 check_changes $head5p1 &&
359048d6 226 test "$(git rev-parse ORIG_HEAD)" = \
8b66f785 227 $head5
359048d6
CR
228'
229
e166fe36 230test_expect_success 'changing files and redo the last commit should succeed' '
31f4c833
JH
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
238 EOF
359048d6
CR
239 echo "3rd line 2nd file" >>secondfile &&
240 git commit -a -C ORIG_HEAD &&
375775bb
AS
241 head4=$(git rev-parse --verify HEAD) &&
242 check_changes $head4 &&
359048d6 243 test "$(git rev-parse ORIG_HEAD)" = \
8b66f785 244 $head5
359048d6
CR
245'
246
e166fe36 247test_expect_success '--hard reset should change the files and undo commits permanently' '
31f4c833
JH
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
256 EOF
359048d6 257 git reset --hard HEAD~2 &&
d62607d1 258 check_changes $head5p2 &&
359048d6 259 test "$(git rev-parse ORIG_HEAD)" = \
375775bb 260 $head4
359048d6
CR
261'
262
e166fe36 263test_expect_success 'redoing changes adding them without commit them should succeed' '
31f4c833
JH
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
289 EOF
290 cat >.cat_expect <<-\EOF &&
291 secondfile:
292 1st line 2nd file
293 2nd line 2nd file
294 EOF
359048d6
CR
295 git rm first &&
296 git mv second secondfile &&
297
298 echo "1st line 2nd file" >secondfile &&
299 echo "2nd line 2nd file" >>secondfile &&
300 git add secondfile &&
d62607d1 301 check_changes $head5p2
359048d6
CR
302'
303
359048d6 304test_expect_success '--mixed reset to HEAD should unadd the files' '
31f4c833
JH
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
321 EOF
322 >.cached_expect &&
323 cat >.cat_expect <<-\EOF &&
324 secondfile:
325 1st line 2nd file
326 2nd line 2nd file
327 EOF
359048d6 328 git reset &&
d62607d1 329 check_changes $head5p2 &&
330 test "$(git rev-parse ORIG_HEAD)" = $head5p2
359048d6
CR
331'
332
359048d6 333test_expect_success 'redoing the last two commits should succeed' '
31f4c833
JH
334 >.diff_expect &&
335 >.cached_expect &&
336 cat >.cat_expect <<-\EOF &&
337 secondfile:
338 1st line 2nd file
339 2nd line 2nd file
340 EOF
359048d6 341 git add secondfile &&
d62607d1 342 git reset --hard $head5p2 &&
359048d6
CR
343 git rm first &&
344 git mv second secondfile &&
345 git commit -a -m "remove 1st and rename 2nd" &&
346
347 echo "1st line 2nd file" >secondfile &&
348 echo "2nd line 2nd file" >>secondfile &&
e6ce2be2
PT
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 - &&
8b66f785 352 check_changes $head5
359048d6
CR
353'
354
359048d6 355test_expect_success '--hard reset to HEAD should clear a failed merge' '
31f4c833
JH
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
363 EOF
359048d6
CR
364 git branch branch1 &&
365 git branch branch2 &&
366
367 git checkout branch1 &&
368 echo "3rd line in branch1" >>secondfile &&
369 git commit -a -m "change in branch1" &&
370
371 git checkout branch2 &&
372 echo "3rd line in branch2" >>secondfile &&
373 git commit -a -m "change in branch2" &&
375775bb 374 head3=$(git rev-parse --verify HEAD) &&
359048d6 375
d492b31c 376 test_must_fail git pull . branch1 &&
359048d6 377 git reset --hard &&
375775bb 378 check_changes $head3
359048d6
CR
379'
380
e166fe36 381test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
31f4c833
JH
382 >.diff_expect &&
383 >.cached_expect &&
384 cat >.cat_expect <<-\EOF &&
385 secondfile:
386 1st line 2nd file
387 2nd line 2nd file
388 EOF
359048d6 389 git reset --hard HEAD^ &&
8b66f785 390 check_changes $head5 &&
359048d6
CR
391
392 git pull . branch1 &&
393 git reset --hard ORIG_HEAD &&
8b66f785 394 check_changes $head5 &&
359048d6 395
01dc8133 396 git checkout main &&
359048d6 397 git branch -D branch1 branch2 &&
8b66f785 398 check_changes $head5
359048d6
CR
399'
400
359048d6 401test_expect_success 'test --mixed <paths>' '
c327762f
CM
402 echo 1 >file1 &&
403 echo 2 >file2 &&
359048d6
CR
404 git add file1 file2 &&
405 test_tick &&
406 git commit -m files &&
d62607d1 407 before1=$(git rev-parse --short HEAD:file1) &&
408 before2=$(git rev-parse --short HEAD:file2) &&
359048d6 409 git rm file2 &&
c327762f
CM
410 echo 3 >file3 &&
411 echo 4 >file4 &&
412 echo 5 >file1 &&
d62607d1 413 after1=$(git rev-parse --short $(git hash-object file1)) &&
414 after4=$(git rev-parse --short $(git hash-object file4)) &&
359048d6 415 git add file1 file3 file4 &&
d94c5e2f
MZ
416 git reset HEAD -- file1 file2 file3 &&
417 test_must_fail git diff --quiet &&
c327762f 418 git diff >output &&
d62607d1 419
c327762f 420 cat >expect <<-EOF &&
d62607d1 421 diff --git a/file1 b/file1
422 index $before1..$after1 100644
423 --- a/file1
424 +++ b/file1
425 @@ -1 +1 @@
426 -1
427 +5
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 @@
434 -2
435 EOF
436
9c5b2fab 437 test_cmp expect output &&
c327762f 438 git diff --cached >output &&
d62607d1 439
c327762f 440 cat >cached_expect <<-EOF &&
d62607d1 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 @@
447 +4
448 EOF
449
9c5b2fab 450 test_cmp cached_expect output
359048d6
CR
451'
452
cbb390cd 453test_expect_success 'test resetting the index at give paths' '
cbb390cd
JH
454 mkdir sub &&
455 >sub/file1 &&
456 >sub/file2 &&
457 git update-index --add sub/file1 sub/file2 &&
458 T=$(git write-tree) &&
d94c5e2f
MZ
459 git reset HEAD sub/file2 &&
460 test_must_fail git diff --quiet &&
cbb390cd
JH
461 U=$(git write-tree) &&
462 echo "$T" &&
463 echo "$U" &&
d492b31c 464 test_must_fail git diff-index --cached --exit-code "$T" &&
cbb390cd 465 test "$T" != "$U"
cbb390cd
JH
466'
467
2e7a9785
JS
468test_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
473'
474
fd56fba9
VD
475test_reset_refreshes_index () {
476
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.
480
481 # Step 0: start with a clean index
482 git reset --hard HEAD &&
483
484 # Step 1: remove file2, but only in the index (no change to worktree)
485 git rm --cached file2 &&
486
487 # Step 2: reset index & leave worktree unchanged from HEAD
488 git $1 reset $2 --mixed HEAD &&
489
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
495}
496
476cca69 497test_expect_success '--mixed refreshes the index' '
45bf7628
VD
498 # Verify default behavior (without --[no-]refresh or reset.refresh)
499 test_reset_refreshes_index &&
fd56fba9 500
2efc9b84 501 # With --quiet
45bf7628 502 test_reset_refreshes_index "" --quiet
fd56fba9
VD
503'
504
505test_expect_success '--mixed --[no-]refresh sets refresh behavior' '
7cff6765 506 # Verify that --[no-]refresh controls index refresh
fd56fba9 507 test_reset_refreshes_index "" --refresh &&
7cff6765 508 ! test_reset_refreshes_index "" --no-refresh
fd56fba9
VD
509'
510
71471b2a
VD
511test_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 &&
517
518 cat >expect <<-\EOF &&
519 Unstaged changes after reset:
520 M file2
521 EOF
522 git update-index --no-skip-worktree file2 &&
523 git add file2 &&
524 git reset --mixed HEAD >output &&
525 test_cmp expect output
526'
527
ff00b682
JH
528test_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) &&
533 {
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 &&
d94c5e2f
MZ
539 git reset HEAD file2 &&
540 test_must_fail git diff --quiet &&
ff00b682
JH
541 git diff-index --exit-code --cached HEAD
542'
543
dfc8f39e 544test_expect_success 'disambiguation (1)' '
dfc8f39e
JH
545 git reset --hard &&
546 >secondfile &&
547 git add secondfile &&
d94c5e2f
MZ
548 git reset secondfile &&
549 test_must_fail git diff --quiet -- secondfile &&
dfc8f39e
JH
550 test -z "$(git diff --cached --name-only)" &&
551 test -f secondfile &&
ca8d148d 552 test_must_be_empty secondfile
dfc8f39e
JH
553'
554
555test_expect_success 'disambiguation (2)' '
dfc8f39e
JH
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
dfc8f39e
JH
563'
564
565test_expect_success 'disambiguation (3)' '
dfc8f39e
JH
566 git reset --hard &&
567 >secondfile &&
568 git add secondfile &&
569 rm -f secondfile &&
d94c5e2f
MZ
570 git reset HEAD secondfile &&
571 test_must_fail git diff --quiet &&
dfc8f39e
JH
572 test -z "$(git diff --cached --name-only)" &&
573 test ! -f secondfile
dfc8f39e
JH
574'
575
576test_expect_success 'disambiguation (4)' '
dfc8f39e
JH
577 git reset --hard &&
578 >secondfile &&
579 git add secondfile &&
580 rm -f secondfile &&
d94c5e2f
MZ
581 git reset -- secondfile &&
582 test_must_fail git diff --quiet &&
dfc8f39e
JH
583 test -z "$(git diff --cached --name-only)" &&
584 test ! -f secondfile
585'
586
2f328c3d
MZ
587test_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
593'
594
b4b313f9
NTND
595test_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 &&
599
600 tree=$(git write-tree) &&
601 git ls-tree $tree new-file >actual &&
d3c6751b 602 test_must_be_empty actual &&
b4b313f9
NTND
603
604 git diff --name-only >actual &&
605 echo new-file >expect &&
606 test_cmp expect actual
607'
608
b7756d41
NTND
609test_expect_success 'reset --mixed sets up work tree' '
610 git init mixed_worktree &&
611 (
612 cd mixed_worktree &&
613 test_commit dummy
614 ) &&
b7756d41 615 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
d3c6751b 616 test_must_be_empty actual
b7756d41
NTND
617'
618
93851746
JK
619test_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
625'
626
359048d6 627test_done