Merge branch 'jk/diff-highlight-blank-match-fix'
[git] / t / t7102-reset.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Carlos Rica
4 #
5
6 test_description='git reset
7
8 Documented tests for git reset'
9
10 . ./test-lib.sh
11
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"
22         fi
23 }
24
25 # Tested non-UTF-8 encoding
26 test_encoding="ISO8859-1"
27
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" &&
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" &&
40         head5p2=$(git rev-parse --verify HEAD) &&
41         head5p2f=$(git rev-parse --short HEAD:first) &&
42
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) &&
48
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)
57 '
58 # git log --pretty=oneline # to see those SHA1 involved
59
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 *
65         do
66                 echo $FILE':'
67                 cat $FILE || return
68         done | test_cmp .cat_expect -
69 }
70
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
76 '
77
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
83 '
84
85 >.diff_expect
86 >.cached_expect
87 cat >.cat_expect <<EOF
88 secondfile:
89 1st line 2nd file
90 2nd line 2nd file
91 EOF
92
93 test_expect_success 'giving a non existing revision should fail' '
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
99 '
100
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
108 '
109
110 test_expect_success \
111         'giving paths with options different than --mixed should fail' '
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 &&
116         check_changes $head5
117 '
118
119 test_expect_success 'giving unrecognized options should fail' '
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 &&
128         check_changes $head5
129 '
130
131 test_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
144         test_must_fail git merge branch1 &&
145         test_must_fail git reset --soft &&
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 &&
152         check_changes $head5
153 '
154
155 test_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 &&
168         test_must_fail git reset --soft &&
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 &&
175         check_changes $head5
176 '
177
178 test_expect_success \
179         'resetting to HEAD with no changes should succeed and do nothing' '
180         git reset --hard &&
181                 check_changes $head5 &&
182         git reset --hard HEAD &&
183                 check_changes $head5 &&
184         git reset --soft &&
185                 check_changes $head5 &&
186         git reset --soft HEAD &&
187                 check_changes $head5 &&
188         git reset --mixed &&
189                 check_changes $head5 &&
190         git reset --mixed HEAD &&
191                 check_changes $head5 &&
192         git reset &&
193                 check_changes $head5 &&
194         git reset HEAD &&
195                 check_changes $head5
196 '
197
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
214 test_expect_success '--soft reset only should show changes in diff --cached' '
215         git reset --soft HEAD^ &&
216         check_changes $head5p1 &&
217         test "$(git rev-parse ORIG_HEAD)" = \
218                         $head5
219 '
220
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
229 test_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 &&
233         head4=$(git rev-parse --verify HEAD) &&
234         check_changes $head4 &&
235         test "$(git rev-parse ORIG_HEAD)" = \
236                         $head5
237 '
238
239 >.diff_expect
240 >.cached_expect
241 cat >.cat_expect <<EOF
242 first:
243 1st file
244 2nd line 1st file
245 second:
246 2nd file
247 EOF
248 test_expect_success \
249         '--hard reset should change the files and undo commits permanently' '
250         git reset --hard HEAD~2 &&
251         check_changes $head5p2 &&
252         test "$(git rev-parse ORIG_HEAD)" = \
253                         $head4
254 '
255
256 >.diff_expect
257 cat >.cached_expect <<EOF
258 diff --git a/first b/first
259 deleted file mode 100644
260 index $head5p2f..0000000
261 --- a/first
262 +++ /dev/null
263 @@ -1,2 +0,0 @@
264 -1st file
265 -2nd line 1st file
266 diff --git a/second b/second
267 deleted file mode 100644
268 index $head5p1s..0000000
269 --- a/second
270 +++ /dev/null
271 @@ -1 +0,0 @@
272 -2nd file
273 diff --git a/secondfile b/secondfile
274 new file mode 100644
275 index 0000000..$head5s
276 --- /dev/null
277 +++ b/secondfile
278 @@ -0,0 +1,2 @@
279 +1st line 2nd file
280 +2nd line 2nd file
281 EOF
282 cat >.cat_expect <<EOF
283 secondfile:
284 1st line 2nd file
285 2nd line 2nd file
286 EOF
287 test_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 &&
295         check_changes $head5p2
296 '
297
298 cat >.diff_expect <<EOF
299 diff --git a/first b/first
300 deleted file mode 100644
301 index $head5p2f..0000000
302 --- a/first
303 +++ /dev/null
304 @@ -1,2 +0,0 @@
305 -1st file
306 -2nd line 1st file
307 diff --git a/second b/second
308 deleted file mode 100644
309 index $head5p1s..0000000
310 --- a/second
311 +++ /dev/null
312 @@ -1 +0,0 @@
313 -2nd file
314 EOF
315 >.cached_expect
316 cat >.cat_expect <<EOF
317 secondfile:
318 1st line 2nd file
319 2nd line 2nd file
320 EOF
321 test_expect_success '--mixed reset to HEAD should unadd the files' '
322         git reset &&
323         check_changes $head5p2 &&
324         test "$(git rev-parse ORIG_HEAD)" = $head5p2
325 '
326
327 >.diff_expect
328 >.cached_expect
329 cat >.cat_expect <<EOF
330 secondfile:
331 1st line 2nd file
332 2nd line 2nd file
333 EOF
334 test_expect_success 'redoing the last two commits should succeed' '
335         git add secondfile &&
336         git reset --hard $head5p2 &&
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 &&
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 - &&
347         check_changes $head5
348 '
349
350 >.diff_expect
351 >.cached_expect
352 cat >.cat_expect <<EOF
353 secondfile:
354 1st line 2nd file
355 2nd line 2nd file
356 3rd line in branch2
357 EOF
358 test_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" &&
369         head3=$(git rev-parse --verify HEAD) &&
370
371         test_must_fail git pull . branch1 &&
372         git reset --hard &&
373         check_changes $head3
374 '
375
376 >.diff_expect
377 >.cached_expect
378 cat >.cat_expect <<EOF
379 secondfile:
380 1st line 2nd file
381 2nd line 2nd file
382 EOF
383 test_expect_success \
384         '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
385         git reset --hard HEAD^ &&
386         check_changes $head5 &&
387
388         git pull . branch1 &&
389         git reset --hard ORIG_HEAD &&
390         check_changes $head5 &&
391
392         git checkout master &&
393         git branch -D branch1 branch2 &&
394         check_changes $head5
395 '
396
397 test_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 &&
403         before1=$(git rev-parse --short HEAD:file1) &&
404         before2=$(git rev-parse --short HEAD:file2) &&
405         git rm file2 &&
406         echo 3 > file3 &&
407         echo 4 > file4 &&
408         echo 5 > file1 &&
409         after1=$(git rev-parse --short $(git hash-object file1)) &&
410         after4=$(git rev-parse --short $(git hash-object file4)) &&
411         git add file1 file3 file4 &&
412         git reset HEAD -- file1 file2 file3 &&
413         test_must_fail git diff --quiet &&
414         git diff > output &&
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
433         test_cmp expect output &&
434         git diff --cached > output &&
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
446         test_cmp cached_expect output
447 '
448
449 test_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) &&
456         git reset HEAD sub/file2 &&
457         test_must_fail git diff --quiet &&
458         U=$(git write-tree) &&
459         echo "$T" &&
460         echo "$U" &&
461         test_must_fail git diff-index --cached --exit-code "$T" &&
462         test "$T" != "$U"
463
464 '
465
466 test_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
473 cat > expect << EOF
474 Unstaged changes after reset:
475 M       file2
476 EOF
477
478 test_expect_success '--mixed refreshes the index' '
479         echo 123 >> file2 &&
480         git reset --mixed HEAD > output &&
481         test_i18ncmp expect output
482 '
483
484 test_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 &&
495         git reset HEAD file2 &&
496         test_must_fail git diff --quiet &&
497         git diff-index --exit-code --cached HEAD
498 '
499
500 test_expect_success 'disambiguation (1)' '
501
502         git reset --hard &&
503         >secondfile &&
504         git add secondfile &&
505         git reset secondfile &&
506         test_must_fail git diff --quiet -- secondfile &&
507         test -z "$(git diff --cached --name-only)" &&
508         test -f secondfile &&
509         test_must_be_empty secondfile
510
511 '
512
513 test_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
525 test_expect_success 'disambiguation (3)' '
526
527         git reset --hard &&
528         >secondfile &&
529         git add secondfile &&
530         rm -f secondfile &&
531         git reset HEAD secondfile &&
532         test_must_fail git diff --quiet &&
533         test -z "$(git diff --cached --name-only)" &&
534         test ! -f secondfile
535
536 '
537
538 test_expect_success 'disambiguation (4)' '
539
540         git reset --hard &&
541         >secondfile &&
542         git add secondfile &&
543         rm -f secondfile &&
544         git reset -- secondfile &&
545         test_must_fail git diff --quiet &&
546         test -z "$(git diff --cached --name-only)" &&
547         test ! -f secondfile
548 '
549
550 test_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
558 test_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 &&
565         test_must_be_empty actual &&
566
567         git diff --name-only >actual &&
568         echo new-file >expect &&
569         test_cmp expect actual
570 '
571
572 test_expect_success 'reset --mixed sets up work tree' '
573         git init mixed_worktree &&
574         (
575                 cd mixed_worktree &&
576                 test_commit dummy
577         ) &&
578         git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
579         test_must_be_empty actual
580 '
581
582 test_done