]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7102-reset.sh
Merge branch 'ea/blame-use-oideq'
[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
CR
9
10. ./test-lib.sh
11
de6029a2 12commit_msg () {
17cc2ef1
AS
13 # String "modify 2nd file (changed)" partly in German
14 # (translated with Google Translate),
de6029a2 15 # encoded in UTF-8, used as a commit log message below.
17cc2ef1 16 msg="modify 2nd file (ge\303\244ndert)\n"
de6029a2
AS
17 if test -n "$1"
18 then
17cc2ef1
AS
19 printf "$msg" | iconv -f utf-8 -t "$1"
20 else
21 printf "$msg"
de6029a2 22 fi
de6029a2
AS
23}
24
ee3efaf6
AS
25# Tested non-UTF-8 encoding
26test_encoding="ISO8859-1"
27
359048d6
CR
28test_expect_success 'creating initial files and commits' '
29 test_tick &&
30 echo "1st file" >first &&
31 git add first &&
32 git commit -m "create 1st file" &&
33
34 echo "2nd file" >second &&
35 git add second &&
36 git commit -m "create 2nd file" &&
37
38 echo "2nd line 1st file" >>first &&
39 git commit -a -m "modify 1st file" &&
d62607d1 40 head5p2=$(git rev-parse --verify HEAD) &&
41 head5p2f=$(git rev-parse --short HEAD:first) &&
359048d6
CR
42
43 git rm first &&
44 git mv second secondfile &&
45 git commit -a -m "remove 1st and rename 2nd" &&
d62607d1 46 head5p1=$(git rev-parse --verify HEAD) &&
47 head5p1s=$(git rev-parse --short HEAD:secondfile) &&
359048d6
CR
48
49 echo "1st line 2nd file" >secondfile &&
50 echo "2nd line 2nd file" >>secondfile &&
e6ce2be2
PT
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 - &&
d62607d1 54 head5=$(git rev-parse --verify HEAD) &&
55 head5s=$(git rev-parse --short HEAD:secondfile) &&
56 head5sl=$(git rev-parse HEAD:secondfile)
359048d6
CR
57'
58# git log --pretty=oneline # to see those SHA1 involved
59
60check_changes () {
61 test "$(git rev-parse HEAD)" = "$1" &&
3af82863
JH
62 git diff | test_cmp .diff_expect - &&
63 git diff --cached | test_cmp .cached_expect - &&
359048d6
CR
64 for FILE in *
65 do
66 echo $FILE':'
67 cat $FILE || return
3af82863 68 done | test_cmp .cat_expect -
359048d6
CR
69}
70
ecaee805 71test_expect_success 'reset --hard message' '
de6029a2
AS
72 hex=$(git log -1 --format="%h") &&
73 git reset --hard > .actual &&
74 echo HEAD is now at $hex $(commit_msg) > .expected &&
1edbaac3 75 test_i18ncmp .expected .actual
de6029a2
AS
76'
77
ee3efaf6 78test_expect_success 'reset --hard message (ISO8859-1 logoutputencoding)' '
de6029a2 79 hex=$(git log -1 --format="%h") &&
ee3efaf6
AS
80 git -c "i18n.logOutputEncoding=$test_encoding" reset --hard > .actual &&
81 echo HEAD is now at $hex $(commit_msg $test_encoding) > .expected &&
1edbaac3 82 test_i18ncmp .expected .actual
de6029a2
AS
83'
84
359048d6
CR
85>.diff_expect
86>.cached_expect
87cat >.cat_expect <<EOF
88secondfile:
891st line 2nd file
902nd line 2nd file
91EOF
92
93test_expect_success 'giving a non existing revision should fail' '
d492b31c
SB
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 &&
8b66f785 98 check_changes $head5
359048d6
CR
99'
100
cdf4a751
JS
101test_expect_success 'reset --soft with unmerged index should fail' '
102 touch .git/MERGE_HEAD &&
d62607d1 103 echo "100644 $head5sl 1 un" |
cdf4a751 104 git update-index --index-info &&
d492b31c 105 test_must_fail git reset --soft HEAD &&
cdf4a751
JS
106 rm .git/MERGE_HEAD &&
107 git rm --cached -- un
108'
109
359048d6
CR
110test_expect_success \
111 'giving paths with options different than --mixed should fail' '
d492b31c
SB
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 &&
8b66f785 116 check_changes $head5
359048d6
CR
117'
118
119test_expect_success 'giving unrecognized options should fail' '
d492b31c
SB
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 &&
8b66f785 128 check_changes $head5
359048d6
CR
129'
130
131test_expect_success \
132 'trying to do reset --soft with pending merge should fail' '
133 git branch branch1 &&
134 git branch branch2 &&
135
136 git checkout branch1 &&
137 echo "3rd line in branch1" >>secondfile &&
138 git commit -a -m "change in branch1" &&
139
140 git checkout branch2 &&
141 echo "3rd line in branch2" >>secondfile &&
142 git commit -a -m "change in branch2" &&
143
d492b31c
SB
144 test_must_fail git merge branch1 &&
145 test_must_fail git reset --soft &&
359048d6
CR
146
147 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
148 git commit -a -m "the change in branch2" &&
149
150 git checkout master &&
151 git branch -D branch1 branch2 &&
8b66f785 152 check_changes $head5
359048d6
CR
153'
154
155test_expect_success \
156 'trying to do reset --soft with pending checkout merge should fail' '
157 git branch branch3 &&
158 git branch branch4 &&
159
160 git checkout branch3 &&
161 echo "3rd line in branch3" >>secondfile &&
162 git commit -a -m "line in branch3" &&
163
164 git checkout branch4 &&
165 echo "3rd line in branch4" >>secondfile &&
166
167 git checkout -m branch3 &&
d492b31c 168 test_must_fail git reset --soft &&
359048d6
CR
169
170 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
171 git commit -a -m "the line in branch3" &&
172
173 git checkout master &&
174 git branch -D branch3 branch4 &&
8b66f785 175 check_changes $head5
359048d6
CR
176'
177
178test_expect_success \
179 'resetting to HEAD with no changes should succeed and do nothing' '
180 git reset --hard &&
8b66f785 181 check_changes $head5 &&
359048d6 182 git reset --hard HEAD &&
8b66f785 183 check_changes $head5 &&
359048d6 184 git reset --soft &&
8b66f785 185 check_changes $head5 &&
359048d6 186 git reset --soft HEAD &&
8b66f785 187 check_changes $head5 &&
359048d6 188 git reset --mixed &&
8b66f785 189 check_changes $head5 &&
359048d6 190 git reset --mixed HEAD &&
8b66f785 191 check_changes $head5 &&
359048d6 192 git reset &&
8b66f785 193 check_changes $head5 &&
359048d6 194 git reset HEAD &&
8b66f785 195 check_changes $head5
359048d6
CR
196'
197
198>.diff_expect
199cat >.cached_expect <<EOF
200diff --git a/secondfile b/secondfile
d62607d1 201index $head5p1s..$head5s 100644
359048d6
CR
202--- a/secondfile
203+++ b/secondfile
204@@ -1 +1,2 @@
205-2nd file
206+1st line 2nd file
207+2nd line 2nd file
208EOF
209cat >.cat_expect <<EOF
210secondfile:
2111st line 2nd file
2122nd line 2nd file
213EOF
214test_expect_success '--soft reset only should show changes in diff --cached' '
215 git reset --soft HEAD^ &&
d62607d1 216 check_changes $head5p1 &&
359048d6 217 test "$(git rev-parse ORIG_HEAD)" = \
8b66f785 218 $head5
359048d6
CR
219'
220
221>.diff_expect
222>.cached_expect
223cat >.cat_expect <<EOF
224secondfile:
2251st line 2nd file
2262nd line 2nd file
2273rd line 2nd file
228EOF
229test_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 &&
375775bb
AS
233 head4=$(git rev-parse --verify HEAD) &&
234 check_changes $head4 &&
359048d6 235 test "$(git rev-parse ORIG_HEAD)" = \
8b66f785 236 $head5
359048d6
CR
237'
238
239>.diff_expect
240>.cached_expect
241cat >.cat_expect <<EOF
242first:
2431st file
2442nd line 1st file
245second:
2462nd file
247EOF
248test_expect_success \
249 '--hard reset should change the files and undo commits permanently' '
250 git reset --hard HEAD~2 &&
d62607d1 251 check_changes $head5p2 &&
359048d6 252 test "$(git rev-parse ORIG_HEAD)" = \
375775bb 253 $head4
359048d6
CR
254'
255
256>.diff_expect
257cat >.cached_expect <<EOF
258diff --git a/first b/first
259deleted file mode 100644
d62607d1 260index $head5p2f..0000000
359048d6
CR
261--- a/first
262+++ /dev/null
263@@ -1,2 +0,0 @@
264-1st file
265-2nd line 1st file
266diff --git a/second b/second
267deleted file mode 100644
d62607d1 268index $head5p1s..0000000
359048d6
CR
269--- a/second
270+++ /dev/null
271@@ -1 +0,0 @@
272-2nd file
273diff --git a/secondfile b/secondfile
274new file mode 100644
d62607d1 275index 0000000..$head5s
359048d6
CR
276--- /dev/null
277+++ b/secondfile
278@@ -0,0 +1,2 @@
279+1st line 2nd file
280+2nd line 2nd file
281EOF
282cat >.cat_expect <<EOF
283secondfile:
2841st line 2nd file
2852nd line 2nd file
286EOF
287test_expect_success \
288 'redoing changes adding them without commit them should succeed' '
289 git rm first &&
290 git mv second secondfile &&
291
292 echo "1st line 2nd file" >secondfile &&
293 echo "2nd line 2nd file" >>secondfile &&
294 git add secondfile &&
d62607d1 295 check_changes $head5p2
359048d6
CR
296'
297
298cat >.diff_expect <<EOF
299diff --git a/first b/first
300deleted file mode 100644
d62607d1 301index $head5p2f..0000000
359048d6
CR
302--- a/first
303+++ /dev/null
304@@ -1,2 +0,0 @@
305-1st file
306-2nd line 1st file
307diff --git a/second b/second
308deleted file mode 100644
d62607d1 309index $head5p1s..0000000
359048d6
CR
310--- a/second
311+++ /dev/null
312@@ -1 +0,0 @@
313-2nd file
314EOF
315>.cached_expect
316cat >.cat_expect <<EOF
317secondfile:
3181st line 2nd file
3192nd line 2nd file
320EOF
321test_expect_success '--mixed reset to HEAD should unadd the files' '
322 git reset &&
d62607d1 323 check_changes $head5p2 &&
324 test "$(git rev-parse ORIG_HEAD)" = $head5p2
359048d6
CR
325'
326
327>.diff_expect
328>.cached_expect
329cat >.cat_expect <<EOF
330secondfile:
3311st line 2nd file
3322nd line 2nd file
333EOF
334test_expect_success 'redoing the last two commits should succeed' '
335 git add secondfile &&
d62607d1 336 git reset --hard $head5p2 &&
359048d6
CR
337
338 git rm first &&
339 git mv second secondfile &&
340 git commit -a -m "remove 1st and rename 2nd" &&
341
342 echo "1st line 2nd file" >secondfile &&
343 echo "2nd line 2nd file" >>secondfile &&
e6ce2be2
PT
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 - &&
8b66f785 347 check_changes $head5
359048d6
CR
348'
349
350>.diff_expect
351>.cached_expect
352cat >.cat_expect <<EOF
353secondfile:
3541st line 2nd file
3552nd line 2nd file
3563rd line in branch2
357EOF
358test_expect_success '--hard reset to HEAD should clear a failed merge' '
359 git branch branch1 &&
360 git branch branch2 &&
361
362 git checkout branch1 &&
363 echo "3rd line in branch1" >>secondfile &&
364 git commit -a -m "change in branch1" &&
365
366 git checkout branch2 &&
367 echo "3rd line in branch2" >>secondfile &&
368 git commit -a -m "change in branch2" &&
375775bb 369 head3=$(git rev-parse --verify HEAD) &&
359048d6 370
d492b31c 371 test_must_fail git pull . branch1 &&
359048d6 372 git reset --hard &&
375775bb 373 check_changes $head3
359048d6
CR
374'
375
376>.diff_expect
377>.cached_expect
378cat >.cat_expect <<EOF
379secondfile:
3801st line 2nd file
3812nd line 2nd file
382EOF
383test_expect_success \
384 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
385 git reset --hard HEAD^ &&
8b66f785 386 check_changes $head5 &&
359048d6
CR
387
388 git pull . branch1 &&
389 git reset --hard ORIG_HEAD &&
8b66f785 390 check_changes $head5 &&
359048d6
CR
391
392 git checkout master &&
393 git branch -D branch1 branch2 &&
8b66f785 394 check_changes $head5
359048d6
CR
395'
396
359048d6
CR
397test_expect_success 'test --mixed <paths>' '
398 echo 1 > file1 &&
399 echo 2 > file2 &&
400 git add file1 file2 &&
401 test_tick &&
402 git commit -m files &&
d62607d1 403 before1=$(git rev-parse --short HEAD:file1) &&
404 before2=$(git rev-parse --short HEAD:file2) &&
359048d6
CR
405 git rm file2 &&
406 echo 3 > file3 &&
407 echo 4 > file4 &&
408 echo 5 > file1 &&
d62607d1 409 after1=$(git rev-parse --short $(git hash-object file1)) &&
410 after4=$(git rev-parse --short $(git hash-object file4)) &&
359048d6 411 git add file1 file3 file4 &&
d94c5e2f
MZ
412 git reset HEAD -- file1 file2 file3 &&
413 test_must_fail git diff --quiet &&
359048d6 414 git diff > output &&
d62607d1 415
416 cat > expect <<-EOF &&
417 diff --git a/file1 b/file1
418 index $before1..$after1 100644
419 --- a/file1
420 +++ b/file1
421 @@ -1 +1 @@
422 -1
423 +5
424 diff --git a/file2 b/file2
425 deleted file mode 100644
426 index $before2..0000000
427 --- a/file2
428 +++ /dev/null
429 @@ -1 +0,0 @@
430 -2
431 EOF
432
9c5b2fab 433 test_cmp expect output &&
359048d6 434 git diff --cached > output &&
d62607d1 435
436 cat > cached_expect <<-EOF &&
437 diff --git a/file4 b/file4
438 new file mode 100644
439 index 0000000..$after4
440 --- /dev/null
441 +++ b/file4
442 @@ -0,0 +1 @@
443 +4
444 EOF
445
9c5b2fab 446 test_cmp cached_expect output
359048d6
CR
447'
448
cbb390cd
JH
449test_expect_success 'test resetting the index at give paths' '
450
451 mkdir sub &&
452 >sub/file1 &&
453 >sub/file2 &&
454 git update-index --add sub/file1 sub/file2 &&
455 T=$(git write-tree) &&
d94c5e2f
MZ
456 git reset HEAD sub/file2 &&
457 test_must_fail git diff --quiet &&
cbb390cd
JH
458 U=$(git write-tree) &&
459 echo "$T" &&
460 echo "$U" &&
d492b31c 461 test_must_fail git diff-index --cached --exit-code "$T" &&
cbb390cd
JH
462 test "$T" != "$U"
463
464'
465
2e7a9785
JS
466test_expect_success 'resetting an unmodified path is a no-op' '
467 git reset --hard &&
468 git reset -- file1 &&
469 git diff-files --exit-code &&
470 git diff-index --cached --exit-code HEAD
471'
472
620a6cd4 473cat > expect << EOF
3deffc52
MM
474Unstaged changes after reset:
475M file2
620a6cd4
JS
476EOF
477
476cca69 478test_expect_success '--mixed refreshes the index' '
620a6cd4
JS
479 echo 123 >> file2 &&
480 git reset --mixed HEAD > output &&
476cca69 481 test_i18ncmp expect output
620a6cd4
JS
482'
483
ff00b682
JH
484test_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) &&
489 {
490 echo "100644 $F1 1 file2" &&
491 echo "100644 $F2 2 file2" &&
492 echo "100644 $F3 3 file2"
493 } | git update-index --index-info &&
494 git ls-files -u &&
d94c5e2f
MZ
495 git reset HEAD file2 &&
496 test_must_fail git diff --quiet &&
ff00b682
JH
497 git diff-index --exit-code --cached HEAD
498'
499
dfc8f39e
JH
500test_expect_success 'disambiguation (1)' '
501
502 git reset --hard &&
503 >secondfile &&
504 git add secondfile &&
d94c5e2f
MZ
505 git reset secondfile &&
506 test_must_fail git diff --quiet -- secondfile &&
dfc8f39e
JH
507 test -z "$(git diff --cached --name-only)" &&
508 test -f secondfile &&
ca8d148d 509 test_must_be_empty secondfile
dfc8f39e
JH
510
511'
512
513test_expect_success 'disambiguation (2)' '
514
515 git reset --hard &&
516 >secondfile &&
517 git add secondfile &&
518 rm -f secondfile &&
519 test_must_fail git reset secondfile &&
520 test -n "$(git diff --cached --name-only -- secondfile)" &&
521 test ! -f secondfile
522
523'
524
525test_expect_success 'disambiguation (3)' '
526
527 git reset --hard &&
528 >secondfile &&
529 git add secondfile &&
530 rm -f secondfile &&
d94c5e2f
MZ
531 git reset HEAD secondfile &&
532 test_must_fail git diff --quiet &&
dfc8f39e
JH
533 test -z "$(git diff --cached --name-only)" &&
534 test ! -f secondfile
535
536'
537
538test_expect_success 'disambiguation (4)' '
539
540 git reset --hard &&
541 >secondfile &&
542 git add secondfile &&
543 rm -f secondfile &&
d94c5e2f
MZ
544 git reset -- secondfile &&
545 test_must_fail git diff --quiet &&
dfc8f39e
JH
546 test -z "$(git diff --cached --name-only)" &&
547 test ! -f secondfile
548'
549
2f328c3d
MZ
550test_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
556'
557
b4b313f9
NTND
558test_expect_success 'reset -N keeps removed files as intent-to-add' '
559 echo new-file >new-file &&
560 git add new-file &&
561 git reset -N HEAD &&
562
563 tree=$(git write-tree) &&
564 git ls-tree $tree new-file >actual &&
d3c6751b 565 test_must_be_empty actual &&
b4b313f9
NTND
566
567 git diff --name-only >actual &&
568 echo new-file >expect &&
569 test_cmp expect actual
570'
571
b7756d41
NTND
572test_expect_success 'reset --mixed sets up work tree' '
573 git init mixed_worktree &&
574 (
575 cd mixed_worktree &&
576 test_commit dummy
577 ) &&
b7756d41 578 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
d3c6751b 579 test_must_be_empty actual
b7756d41
NTND
580'
581
359048d6 582test_done