.github/workflows/main.yml: run static-analysis on bionic
[git/debian.git] / t / t7102-reset.sh
blobb1affb001f599cbfdc074d2a19bf8816a76c4c32
1 #!/bin/sh
3 # Copyright (c) 2007 Carlos Rica
6 test_description='git reset
8 Documented tests for git reset'
10 . ./test-lib.sh
12 commit_msg () {
13 # String "modify 2nd file (changed)" partly in German
14 # (translated with Google Translate),
15 # encoded in UTF-8, used as a commit log message below.
16 msg="modify 2nd file (ge\303\244ndert)\n"
17 if test -n "$1"
18 then
19 printf "$msg" | iconv -f utf-8 -t "$1"
20 else
21 printf "$msg"
25 # Tested non-UTF-8 encoding
26 test_encoding="ISO8859-1"
28 test_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" &&
34 echo "2nd file" >second &&
35 git add second &&
36 git commit -m "create 2nd file" &&
38 echo "2nd line 1st file" >>first &&
39 git commit -a -m "modify 1st file" &&
40 head5p2=$(git rev-parse --verify HEAD) &&
41 head5p2f=$(git rev-parse --short HEAD:first) &&
43 git rm first &&
44 git mv second secondfile &&
45 git commit -a -m "remove 1st and rename 2nd" &&
46 head5p1=$(git rev-parse --verify HEAD) &&
47 head5p1s=$(git rev-parse --short HEAD:secondfile) &&
49 echo "1st line 2nd file" >secondfile &&
50 echo "2nd line 2nd file" >>secondfile &&
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 - &&
54 head5=$(git rev-parse --verify HEAD) &&
55 head5s=$(git rev-parse --short HEAD:secondfile) &&
56 head5sl=$(git rev-parse HEAD:secondfile)
58 # git log --pretty=oneline # to see those SHA1 involved
60 check_changes () {
61 test "$(git rev-parse HEAD)" = "$1" &&
62 git diff | test_cmp .diff_expect - &&
63 git diff --cached | test_cmp .cached_expect - &&
64 for FILE in *
66 echo $FILE':'
67 cat $FILE || return
68 done | test_cmp .cat_expect -
71 test_expect_success 'reset --hard message' '
72 hex=$(git log -1 --format="%h") &&
73 git reset --hard >.actual &&
74 echo HEAD is now at $hex $(commit_msg) >.expected &&
75 test_i18ncmp .expected .actual
78 test_expect_success 'reset --hard message (ISO8859-1 logoutputencoding)' '
79 hex=$(git log -1 --format="%h") &&
80 git -c "i18n.logOutputEncoding=$test_encoding" reset --hard >.actual &&
81 echo HEAD is now at $hex $(commit_msg $test_encoding) >.expected &&
82 test_i18ncmp .expected .actual
85 test_expect_success 'giving a non existing revision should fail' '
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
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 &&
98 check_changes $head5
101 test_expect_success 'reset --soft with unmerged index should fail' '
102 touch .git/MERGE_HEAD &&
103 echo "100644 $head5sl 1 un" |
104 git update-index --index-info &&
105 test_must_fail git reset --soft HEAD &&
106 rm .git/MERGE_HEAD &&
107 git rm --cached -- un
110 test_expect_success 'giving paths with options different than --mixed should fail' '
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 &&
115 check_changes $head5
118 test_expect_success 'giving unrecognized options should fail' '
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 &&
127 check_changes $head5
130 test_expect_success 'trying to do reset --soft with pending merge should fail' '
131 git branch branch1 &&
132 git branch branch2 &&
134 git checkout branch1 &&
135 echo "3rd line in branch1" >>secondfile &&
136 git commit -a -m "change in branch1" &&
138 git checkout branch2 &&
139 echo "3rd line in branch2" >>secondfile &&
140 git commit -a -m "change in branch2" &&
142 test_must_fail git merge branch1 &&
143 test_must_fail git reset --soft &&
145 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
146 git commit -a -m "the change in branch2" &&
148 git checkout master &&
149 git branch -D branch1 branch2 &&
150 check_changes $head5
153 test_expect_success 'trying to do reset --soft with pending checkout merge should fail' '
154 git branch branch3 &&
155 git branch branch4 &&
157 git checkout branch3 &&
158 echo "3rd line in branch3" >>secondfile &&
159 git commit -a -m "line in branch3" &&
161 git checkout branch4 &&
162 echo "3rd line in branch4" >>secondfile &&
164 git checkout -m branch3 &&
165 test_must_fail git reset --soft &&
167 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
168 git commit -a -m "the line in branch3" &&
170 git checkout master &&
171 git branch -D branch3 branch4 &&
172 check_changes $head5
175 test_expect_success 'resetting to HEAD with no changes should succeed and do nothing' '
176 git reset --hard &&
177 check_changes $head5 &&
178 git reset --hard HEAD &&
179 check_changes $head5 &&
180 git reset --soft &&
181 check_changes $head5 &&
182 git reset --soft HEAD &&
183 check_changes $head5 &&
184 git reset --mixed &&
185 check_changes $head5 &&
186 git reset --mixed HEAD &&
187 check_changes $head5 &&
188 git reset &&
189 check_changes $head5 &&
190 git reset HEAD &&
191 check_changes $head5
194 test_expect_success '--soft reset only should show changes in diff --cached' '
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
206 cat >.cat_expect <<-\EOF &&
207 secondfile:
208 1st line 2nd file
209 2nd line 2nd file
211 git reset --soft HEAD^ &&
212 check_changes $head5p1 &&
213 test "$(git rev-parse ORIG_HEAD)" = \
214 $head5
217 test_expect_success 'changing files and redo the last commit should succeed' '
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
226 echo "3rd line 2nd file" >>secondfile &&
227 git commit -a -C ORIG_HEAD &&
228 head4=$(git rev-parse --verify HEAD) &&
229 check_changes $head4 &&
230 test "$(git rev-parse ORIG_HEAD)" = \
231 $head5
234 test_expect_success '--hard reset should change the files and undo commits permanently' '
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
244 git reset --hard HEAD~2 &&
245 check_changes $head5p2 &&
246 test "$(git rev-parse ORIG_HEAD)" = \
247 $head4
250 test_expect_success 'redoing changes adding them without commit them should succeed' '
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
277 cat >.cat_expect <<-\EOF &&
278 secondfile:
279 1st line 2nd file
280 2nd line 2nd file
282 git rm first &&
283 git mv second secondfile &&
285 echo "1st line 2nd file" >secondfile &&
286 echo "2nd line 2nd file" >>secondfile &&
287 git add secondfile &&
288 check_changes $head5p2
291 test_expect_success '--mixed reset to HEAD should unadd the files' '
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
309 >.cached_expect &&
310 cat >.cat_expect <<-\EOF &&
311 secondfile:
312 1st line 2nd file
313 2nd line 2nd file
315 git reset &&
316 check_changes $head5p2 &&
317 test "$(git rev-parse ORIG_HEAD)" = $head5p2
320 test_expect_success 'redoing the last two commits should succeed' '
321 >.diff_expect &&
322 >.cached_expect &&
323 cat >.cat_expect <<-\EOF &&
324 secondfile:
325 1st line 2nd file
326 2nd line 2nd file
328 git add secondfile &&
329 git reset --hard $head5p2 &&
330 git rm first &&
331 git mv second secondfile &&
332 git commit -a -m "remove 1st and rename 2nd" &&
334 echo "1st line 2nd file" >secondfile &&
335 echo "2nd line 2nd file" >>secondfile &&
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 - &&
339 check_changes $head5
342 test_expect_success '--hard reset to HEAD should clear a failed merge' '
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
351 git branch branch1 &&
352 git branch branch2 &&
354 git checkout branch1 &&
355 echo "3rd line in branch1" >>secondfile &&
356 git commit -a -m "change in branch1" &&
358 git checkout branch2 &&
359 echo "3rd line in branch2" >>secondfile &&
360 git commit -a -m "change in branch2" &&
361 head3=$(git rev-parse --verify HEAD) &&
363 test_must_fail git pull . branch1 &&
364 git reset --hard &&
365 check_changes $head3
368 test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
369 >.diff_expect &&
370 >.cached_expect &&
371 cat >.cat_expect <<-\EOF &&
372 secondfile:
373 1st line 2nd file
374 2nd line 2nd file
376 git reset --hard HEAD^ &&
377 check_changes $head5 &&
379 git pull . branch1 &&
380 git reset --hard ORIG_HEAD &&
381 check_changes $head5 &&
383 git checkout master &&
384 git branch -D branch1 branch2 &&
385 check_changes $head5
388 test_expect_success 'test --mixed <paths>' '
389 echo 1 >file1 &&
390 echo 2 >file2 &&
391 git add file1 file2 &&
392 test_tick &&
393 git commit -m files &&
394 before1=$(git rev-parse --short HEAD:file1) &&
395 before2=$(git rev-parse --short HEAD:file2) &&
396 git rm file2 &&
397 echo 3 >file3 &&
398 echo 4 >file4 &&
399 echo 5 >file1 &&
400 after1=$(git rev-parse --short $(git hash-object file1)) &&
401 after4=$(git rev-parse --short $(git hash-object file4)) &&
402 git add file1 file3 file4 &&
403 git reset HEAD -- file1 file2 file3 &&
404 test_must_fail git diff --quiet &&
405 git diff >output &&
407 cat >expect <<-EOF &&
408 diff --git a/file1 b/file1
409 index $before1..$after1 100644
410 --- a/file1
411 +++ b/file1
412 @@ -1 +1 @@
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 @@
424 test_cmp expect output &&
425 git diff --cached >output &&
427 cat >cached_expect <<-EOF &&
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 @@
437 test_cmp cached_expect output
440 test_expect_success 'test resetting the index at give paths' '
441 mkdir sub &&
442 >sub/file1 &&
443 >sub/file2 &&
444 git update-index --add sub/file1 sub/file2 &&
445 T=$(git write-tree) &&
446 git reset HEAD sub/file2 &&
447 test_must_fail git diff --quiet &&
448 U=$(git write-tree) &&
449 echo "$T" &&
450 echo "$U" &&
451 test_must_fail git diff-index --cached --exit-code "$T" &&
452 test "$T" != "$U"
455 test_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
462 test_expect_success '--mixed refreshes the index' '
463 cat >expect <<-\EOF &&
464 Unstaged changes after reset:
465 M file2
467 echo 123 >>file2 &&
468 git reset --mixed HEAD >output &&
469 test_i18ncmp expect output
472 test_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) &&
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 &&
483 git reset HEAD file2 &&
484 test_must_fail git diff --quiet &&
485 git diff-index --exit-code --cached HEAD
488 test_expect_success 'disambiguation (1)' '
489 git reset --hard &&
490 >secondfile &&
491 git add secondfile &&
492 git reset secondfile &&
493 test_must_fail git diff --quiet -- secondfile &&
494 test -z "$(git diff --cached --name-only)" &&
495 test -f secondfile &&
496 test_must_be_empty secondfile
499 test_expect_success 'disambiguation (2)' '
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
509 test_expect_success 'disambiguation (3)' '
510 git reset --hard &&
511 >secondfile &&
512 git add secondfile &&
513 rm -f secondfile &&
514 git reset HEAD secondfile &&
515 test_must_fail git diff --quiet &&
516 test -z "$(git diff --cached --name-only)" &&
517 test ! -f secondfile
520 test_expect_success 'disambiguation (4)' '
521 git reset --hard &&
522 >secondfile &&
523 git add secondfile &&
524 rm -f secondfile &&
525 git reset -- secondfile &&
526 test_must_fail git diff --quiet &&
527 test -z "$(git diff --cached --name-only)" &&
528 test ! -f secondfile
531 test_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
539 test_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 &&
544 tree=$(git write-tree) &&
545 git ls-tree $tree new-file >actual &&
546 test_must_be_empty actual &&
548 git diff --name-only >actual &&
549 echo new-file >expect &&
550 test_cmp expect actual
553 test_expect_success 'reset --mixed sets up work tree' '
554 git init mixed_worktree &&
556 cd mixed_worktree &&
557 test_commit dummy
558 ) &&
559 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
560 test_must_be_empty actual
563 test_done