]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7102-reset.sh
Merge branch 'ad/t4129-setfacl-target-fix' into maint
[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 72 hex=$(git log -1 --format="%h") &&
c327762f
CM
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") &&
c327762f
CM
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 85test_expect_success 'giving a non existing revision should fail' '
31f4c833
JH
86 >.diff_expect &&
87 >.cached_expect &&
88 cat >.cat_expect <<-\EOF &&
89 secondfile:
90 1st line 2nd file
91 2nd line 2nd file
92 EOF
93
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
e166fe36 110test_expect_success 'giving paths with options different than --mixed should fail' '
d492b31c
SB
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 &&
8b66f785 115 check_changes $head5
359048d6
CR
116'
117
118test_expect_success 'giving unrecognized options should fail' '
d492b31c
SB
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 &&
8b66f785 127 check_changes $head5
359048d6
CR
128'
129
e166fe36 130test_expect_success 'trying to do reset --soft with pending merge should fail' '
359048d6
CR
131 git branch branch1 &&
132 git branch branch2 &&
133
134 git checkout branch1 &&
135 echo "3rd line in branch1" >>secondfile &&
136 git commit -a -m "change in branch1" &&
137
138 git checkout branch2 &&
139 echo "3rd line in branch2" >>secondfile &&
140 git commit -a -m "change in branch2" &&
141
d492b31c
SB
142 test_must_fail git merge branch1 &&
143 test_must_fail git reset --soft &&
359048d6
CR
144
145 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
146 git commit -a -m "the change in branch2" &&
147
148 git checkout master &&
149 git branch -D branch1 branch2 &&
8b66f785 150 check_changes $head5
359048d6
CR
151'
152
e166fe36 153test_expect_success 'trying to do reset --soft with pending checkout merge should fail' '
359048d6
CR
154 git branch branch3 &&
155 git branch branch4 &&
156
157 git checkout branch3 &&
158 echo "3rd line in branch3" >>secondfile &&
159 git commit -a -m "line in branch3" &&
160
161 git checkout branch4 &&
162 echo "3rd line in branch4" >>secondfile &&
163
164 git checkout -m branch3 &&
d492b31c 165 test_must_fail git reset --soft &&
359048d6
CR
166
167 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
168 git commit -a -m "the line in branch3" &&
169
170 git checkout master &&
171 git branch -D branch3 branch4 &&
8b66f785 172 check_changes $head5
359048d6
CR
173'
174
e166fe36 175test_expect_success 'resetting to HEAD with no changes should succeed and do nothing' '
359048d6 176 git reset --hard &&
8b66f785 177 check_changes $head5 &&
359048d6 178 git reset --hard HEAD &&
8b66f785 179 check_changes $head5 &&
359048d6 180 git reset --soft &&
8b66f785 181 check_changes $head5 &&
359048d6 182 git reset --soft HEAD &&
8b66f785 183 check_changes $head5 &&
359048d6 184 git reset --mixed &&
8b66f785 185 check_changes $head5 &&
359048d6 186 git reset --mixed HEAD &&
8b66f785 187 check_changes $head5 &&
359048d6 188 git reset &&
8b66f785 189 check_changes $head5 &&
359048d6 190 git reset HEAD &&
8b66f785 191 check_changes $head5
359048d6
CR
192'
193
359048d6 194test_expect_success '--soft reset only should show changes in diff --cached' '
31f4c833
JH
195 >.diff_expect &&
196 cat >.cached_expect <<-EOF &&
197 diff --git a/secondfile b/secondfile
198 index $head5p1s..$head5s 100644
199 --- a/secondfile
200 +++ b/secondfile
201 @@ -1 +1,2 @@
202 -2nd file
203 +1st line 2nd file
204 +2nd line 2nd file
205 EOF
206 cat >.cat_expect <<-\EOF &&
207 secondfile:
208 1st line 2nd file
209 2nd line 2nd file
210 EOF
359048d6 211 git reset --soft HEAD^ &&
d62607d1 212 check_changes $head5p1 &&
359048d6 213 test "$(git rev-parse ORIG_HEAD)" = \
8b66f785 214 $head5
359048d6
CR
215'
216
e166fe36 217test_expect_success 'changing files and redo the last commit should succeed' '
31f4c833
JH
218 >.diff_expect &&
219 >.cached_expect &&
220 cat >.cat_expect <<-\EOF &&
221 secondfile:
222 1st line 2nd file
223 2nd line 2nd file
224 3rd line 2nd file
225 EOF
359048d6
CR
226 echo "3rd line 2nd file" >>secondfile &&
227 git commit -a -C ORIG_HEAD &&
375775bb
AS
228 head4=$(git rev-parse --verify HEAD) &&
229 check_changes $head4 &&
359048d6 230 test "$(git rev-parse ORIG_HEAD)" = \
8b66f785 231 $head5
359048d6
CR
232'
233
e166fe36 234test_expect_success '--hard reset should change the files and undo commits permanently' '
31f4c833
JH
235 >.diff_expect &&
236 >.cached_expect &&
237 cat >.cat_expect <<-\EOF &&
238 first:
239 1st file
240 2nd line 1st file
241 second:
242 2nd file
243 EOF
359048d6 244 git reset --hard HEAD~2 &&
d62607d1 245 check_changes $head5p2 &&
359048d6 246 test "$(git rev-parse ORIG_HEAD)" = \
375775bb 247 $head4
359048d6
CR
248'
249
e166fe36 250test_expect_success 'redoing changes adding them without commit them should succeed' '
31f4c833
JH
251 >.diff_expect &&
252 cat >.cached_expect <<-EOF &&
253 diff --git a/first b/first
254 deleted file mode 100644
255 index $head5p2f..0000000
256 --- a/first
257 +++ /dev/null
258 @@ -1,2 +0,0 @@
259 -1st file
260 -2nd line 1st file
261 diff --git a/second b/second
262 deleted file mode 100644
263 index $head5p1s..0000000
264 --- a/second
265 +++ /dev/null
266 @@ -1 +0,0 @@
267 -2nd file
268 diff --git a/secondfile b/secondfile
269 new file mode 100644
270 index 0000000..$head5s
271 --- /dev/null
272 +++ b/secondfile
273 @@ -0,0 +1,2 @@
274 +1st line 2nd file
275 +2nd line 2nd file
276 EOF
277 cat >.cat_expect <<-\EOF &&
278 secondfile:
279 1st line 2nd file
280 2nd line 2nd file
281 EOF
359048d6
CR
282 git rm first &&
283 git mv second secondfile &&
284
285 echo "1st line 2nd file" >secondfile &&
286 echo "2nd line 2nd file" >>secondfile &&
287 git add secondfile &&
d62607d1 288 check_changes $head5p2
359048d6
CR
289'
290
359048d6 291test_expect_success '--mixed reset to HEAD should unadd the files' '
31f4c833
JH
292 cat >.diff_expect <<-EOF &&
293 diff --git a/first b/first
294 deleted file mode 100644
295 index $head5p2f..0000000
296 --- a/first
297 +++ /dev/null
298 @@ -1,2 +0,0 @@
299 -1st file
300 -2nd line 1st file
301 diff --git a/second b/second
302 deleted file mode 100644
303 index $head5p1s..0000000
304 --- a/second
305 +++ /dev/null
306 @@ -1 +0,0 @@
307 -2nd file
308 EOF
309 >.cached_expect &&
310 cat >.cat_expect <<-\EOF &&
311 secondfile:
312 1st line 2nd file
313 2nd line 2nd file
314 EOF
359048d6 315 git reset &&
d62607d1 316 check_changes $head5p2 &&
317 test "$(git rev-parse ORIG_HEAD)" = $head5p2
359048d6
CR
318'
319
359048d6 320test_expect_success 'redoing the last two commits should succeed' '
31f4c833
JH
321 >.diff_expect &&
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 add secondfile &&
d62607d1 329 git reset --hard $head5p2 &&
359048d6
CR
330 git rm first &&
331 git mv second secondfile &&
332 git commit -a -m "remove 1st and rename 2nd" &&
333
334 echo "1st line 2nd file" >secondfile &&
335 echo "2nd line 2nd file" >>secondfile &&
e6ce2be2
PT
336 # "git commit -m" would break MinGW, as Windows refuse to pass
337 # $test_encoding encoded parameter to git.
338 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
8b66f785 339 check_changes $head5
359048d6
CR
340'
341
359048d6 342test_expect_success '--hard reset to HEAD should clear a failed merge' '
31f4c833
JH
343 >.diff_expect &&
344 >.cached_expect &&
345 cat >.cat_expect <<-\EOF &&
346 secondfile:
347 1st line 2nd file
348 2nd line 2nd file
349 3rd line in branch2
350 EOF
359048d6
CR
351 git branch branch1 &&
352 git branch branch2 &&
353
354 git checkout branch1 &&
355 echo "3rd line in branch1" >>secondfile &&
356 git commit -a -m "change in branch1" &&
357
358 git checkout branch2 &&
359 echo "3rd line in branch2" >>secondfile &&
360 git commit -a -m "change in branch2" &&
375775bb 361 head3=$(git rev-parse --verify HEAD) &&
359048d6 362
d492b31c 363 test_must_fail git pull . branch1 &&
359048d6 364 git reset --hard &&
375775bb 365 check_changes $head3
359048d6
CR
366'
367
e166fe36 368test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
31f4c833
JH
369 >.diff_expect &&
370 >.cached_expect &&
371 cat >.cat_expect <<-\EOF &&
372 secondfile:
373 1st line 2nd file
374 2nd line 2nd file
375 EOF
359048d6 376 git reset --hard HEAD^ &&
8b66f785 377 check_changes $head5 &&
359048d6
CR
378
379 git pull . branch1 &&
380 git reset --hard ORIG_HEAD &&
8b66f785 381 check_changes $head5 &&
359048d6
CR
382
383 git checkout master &&
384 git branch -D branch1 branch2 &&
8b66f785 385 check_changes $head5
359048d6
CR
386'
387
359048d6 388test_expect_success 'test --mixed <paths>' '
c327762f
CM
389 echo 1 >file1 &&
390 echo 2 >file2 &&
359048d6
CR
391 git add file1 file2 &&
392 test_tick &&
393 git commit -m files &&
d62607d1 394 before1=$(git rev-parse --short HEAD:file1) &&
395 before2=$(git rev-parse --short HEAD:file2) &&
359048d6 396 git rm file2 &&
c327762f
CM
397 echo 3 >file3 &&
398 echo 4 >file4 &&
399 echo 5 >file1 &&
d62607d1 400 after1=$(git rev-parse --short $(git hash-object file1)) &&
401 after4=$(git rev-parse --short $(git hash-object file4)) &&
359048d6 402 git add file1 file3 file4 &&
d94c5e2f
MZ
403 git reset HEAD -- file1 file2 file3 &&
404 test_must_fail git diff --quiet &&
c327762f 405 git diff >output &&
d62607d1 406
c327762f 407 cat >expect <<-EOF &&
d62607d1 408 diff --git a/file1 b/file1
409 index $before1..$after1 100644
410 --- a/file1
411 +++ b/file1
412 @@ -1 +1 @@
413 -1
414 +5
415 diff --git a/file2 b/file2
416 deleted file mode 100644
417 index $before2..0000000
418 --- a/file2
419 +++ /dev/null
420 @@ -1 +0,0 @@
421 -2
422 EOF
423
9c5b2fab 424 test_cmp expect output &&
c327762f 425 git diff --cached >output &&
d62607d1 426
c327762f 427 cat >cached_expect <<-EOF &&
d62607d1 428 diff --git a/file4 b/file4
429 new file mode 100644
430 index 0000000..$after4
431 --- /dev/null
432 +++ b/file4
433 @@ -0,0 +1 @@
434 +4
435 EOF
436
9c5b2fab 437 test_cmp cached_expect output
359048d6
CR
438'
439
cbb390cd 440test_expect_success 'test resetting the index at give paths' '
cbb390cd
JH
441 mkdir sub &&
442 >sub/file1 &&
443 >sub/file2 &&
444 git update-index --add sub/file1 sub/file2 &&
445 T=$(git write-tree) &&
d94c5e2f
MZ
446 git reset HEAD sub/file2 &&
447 test_must_fail git diff --quiet &&
cbb390cd
JH
448 U=$(git write-tree) &&
449 echo "$T" &&
450 echo "$U" &&
d492b31c 451 test_must_fail git diff-index --cached --exit-code "$T" &&
cbb390cd 452 test "$T" != "$U"
cbb390cd
JH
453'
454
2e7a9785
JS
455test_expect_success 'resetting an unmodified path is a no-op' '
456 git reset --hard &&
457 git reset -- file1 &&
458 git diff-files --exit-code &&
459 git diff-index --cached --exit-code HEAD
460'
461
476cca69 462test_expect_success '--mixed refreshes the index' '
31f4c833
JH
463 cat >expect <<-\EOF &&
464 Unstaged changes after reset:
465 M file2
466 EOF
c327762f
CM
467 echo 123 >>file2 &&
468 git reset --mixed HEAD >output &&
476cca69 469 test_i18ncmp expect output
620a6cd4
JS
470'
471
ff00b682
JH
472test_expect_success 'resetting specific path that is unmerged' '
473 git rm --cached file2 &&
474 F1=$(git rev-parse HEAD:file1) &&
475 F2=$(git rev-parse HEAD:file2) &&
476 F3=$(git rev-parse HEAD:secondfile) &&
477 {
478 echo "100644 $F1 1 file2" &&
479 echo "100644 $F2 2 file2" &&
480 echo "100644 $F3 3 file2"
481 } | git update-index --index-info &&
482 git ls-files -u &&
d94c5e2f
MZ
483 git reset HEAD file2 &&
484 test_must_fail git diff --quiet &&
ff00b682
JH
485 git diff-index --exit-code --cached HEAD
486'
487
dfc8f39e 488test_expect_success 'disambiguation (1)' '
dfc8f39e
JH
489 git reset --hard &&
490 >secondfile &&
491 git add secondfile &&
d94c5e2f
MZ
492 git reset secondfile &&
493 test_must_fail git diff --quiet -- secondfile &&
dfc8f39e
JH
494 test -z "$(git diff --cached --name-only)" &&
495 test -f secondfile &&
ca8d148d 496 test_must_be_empty secondfile
dfc8f39e
JH
497'
498
499test_expect_success 'disambiguation (2)' '
dfc8f39e
JH
500 git reset --hard &&
501 >secondfile &&
502 git add secondfile &&
503 rm -f secondfile &&
504 test_must_fail git reset secondfile &&
505 test -n "$(git diff --cached --name-only -- secondfile)" &&
506 test ! -f secondfile
dfc8f39e
JH
507'
508
509test_expect_success 'disambiguation (3)' '
dfc8f39e
JH
510 git reset --hard &&
511 >secondfile &&
512 git add secondfile &&
513 rm -f secondfile &&
d94c5e2f
MZ
514 git reset HEAD secondfile &&
515 test_must_fail git diff --quiet &&
dfc8f39e
JH
516 test -z "$(git diff --cached --name-only)" &&
517 test ! -f secondfile
dfc8f39e
JH
518'
519
520test_expect_success 'disambiguation (4)' '
dfc8f39e
JH
521 git reset --hard &&
522 >secondfile &&
523 git add secondfile &&
524 rm -f secondfile &&
d94c5e2f
MZ
525 git reset -- secondfile &&
526 test_must_fail git diff --quiet &&
dfc8f39e
JH
527 test -z "$(git diff --cached --name-only)" &&
528 test ! -f secondfile
529'
530
2f328c3d
MZ
531test_expect_success 'reset with paths accepts tree' '
532 # for simpler tests, drop last commit containing added files
533 git reset --hard HEAD^ &&
534 git reset HEAD^^{tree} -- . &&
535 git diff --cached HEAD^ --exit-code &&
536 git diff HEAD --exit-code
537'
538
b4b313f9
NTND
539test_expect_success 'reset -N keeps removed files as intent-to-add' '
540 echo new-file >new-file &&
541 git add new-file &&
542 git reset -N HEAD &&
543
544 tree=$(git write-tree) &&
545 git ls-tree $tree new-file >actual &&
d3c6751b 546 test_must_be_empty actual &&
b4b313f9
NTND
547
548 git diff --name-only >actual &&
549 echo new-file >expect &&
550 test_cmp expect actual
551'
552
b7756d41
NTND
553test_expect_success 'reset --mixed sets up work tree' '
554 git init mixed_worktree &&
555 (
556 cd mixed_worktree &&
557 test_commit dummy
558 ) &&
b7756d41 559 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
d3c6751b 560 test_must_be_empty actual
b7756d41
NTND
561'
562
359048d6 563test_done