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