t7101,t7102,t7201: modernize test formatting
[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 '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
116 '
117
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
128 '
129
130 test_expect_success 'trying to do reset --soft with pending merge should fail' '
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
142         test_must_fail git merge branch1 &&
143         test_must_fail git reset --soft &&
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 &&
150         check_changes $head5
151 '
152
153 test_expect_success 'trying to do reset --soft with pending checkout merge should fail' '
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 &&
165         test_must_fail git reset --soft &&
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 &&
172         check_changes $head5
173 '
174
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
192 '
193
194 >.diff_expect
195 cat >.cached_expect <<EOF
196 diff --git a/secondfile b/secondfile
197 index $head5p1s..$head5s 100644
198 --- a/secondfile
199 +++ b/secondfile
200 @@ -1 +1,2 @@
201 -2nd file
202 +1st line 2nd file
203 +2nd line 2nd file
204 EOF
205 cat >.cat_expect <<EOF
206 secondfile:
207 1st line 2nd file
208 2nd line 2nd file
209 EOF
210 test_expect_success '--soft reset only should show changes in diff --cached' '
211         git reset --soft HEAD^ &&
212         check_changes $head5p1 &&
213         test "$(git rev-parse ORIG_HEAD)" = \
214                         $head5
215 '
216
217 >.diff_expect
218 >.cached_expect
219 cat >.cat_expect <<EOF
220 secondfile:
221 1st line 2nd file
222 2nd line 2nd file
223 3rd line 2nd file
224 EOF
225 test_expect_success 'changing files and redo the last commit should succeed' '
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
232 '
233
234 >.diff_expect
235 >.cached_expect
236 cat >.cat_expect <<EOF
237 first:
238 1st file
239 2nd line 1st file
240 second:
241 2nd file
242 EOF
243 test_expect_success '--hard reset should change the files and undo commits permanently' '
244         git reset --hard HEAD~2 &&
245         check_changes $head5p2 &&
246         test "$(git rev-parse ORIG_HEAD)" = \
247                         $head4
248 '
249
250 >.diff_expect
251 cat >.cached_expect <<EOF
252 diff --git a/first b/first
253 deleted file mode 100644
254 index $head5p2f..0000000
255 --- a/first
256 +++ /dev/null
257 @@ -1,2 +0,0 @@
258 -1st file
259 -2nd line 1st file
260 diff --git a/second b/second
261 deleted file mode 100644
262 index $head5p1s..0000000
263 --- a/second
264 +++ /dev/null
265 @@ -1 +0,0 @@
266 -2nd file
267 diff --git a/secondfile b/secondfile
268 new file mode 100644
269 index 0000000..$head5s
270 --- /dev/null
271 +++ b/secondfile
272 @@ -0,0 +1,2 @@
273 +1st line 2nd file
274 +2nd line 2nd file
275 EOF
276 cat >.cat_expect <<EOF
277 secondfile:
278 1st line 2nd file
279 2nd line 2nd file
280 EOF
281 test_expect_success 'redoing changes adding them without commit them should succeed' '
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 &&
288         check_changes $head5p2
289 '
290
291 cat >.diff_expect <<EOF
292 diff --git a/first b/first
293 deleted file mode 100644
294 index $head5p2f..0000000
295 --- a/first
296 +++ /dev/null
297 @@ -1,2 +0,0 @@
298 -1st file
299 -2nd line 1st file
300 diff --git a/second b/second
301 deleted file mode 100644
302 index $head5p1s..0000000
303 --- a/second
304 +++ /dev/null
305 @@ -1 +0,0 @@
306 -2nd file
307 EOF
308 >.cached_expect
309 cat >.cat_expect <<EOF
310 secondfile:
311 1st line 2nd file
312 2nd line 2nd file
313 EOF
314 test_expect_success '--mixed reset to HEAD should unadd the files' '
315         git reset &&
316         check_changes $head5p2 &&
317         test "$(git rev-parse ORIG_HEAD)" = $head5p2
318 '
319
320 >.diff_expect
321 >.cached_expect
322 cat >.cat_expect <<EOF
323 secondfile:
324 1st line 2nd file
325 2nd line 2nd file
326 EOF
327 test_expect_success 'redoing the last two commits should succeed' '
328         git add secondfile &&
329         git reset --hard $head5p2 &&
330
331         git rm first &&
332         git mv second secondfile &&
333         git commit -a -m "remove 1st and rename 2nd" &&
334
335         echo "1st line 2nd file" >secondfile &&
336         echo "2nd line 2nd file" >>secondfile &&
337         # "git commit -m" would break MinGW, as Windows refuse to pass
338         # $test_encoding encoded parameter to git.
339         commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
340         check_changes $head5
341 '
342
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
351 test_expect_success '--hard reset to HEAD should clear a failed merge' '
352         git branch branch1 &&
353         git branch branch2 &&
354
355         git checkout branch1 &&
356         echo "3rd line in branch1" >>secondfile &&
357         git commit -a -m "change in branch1" &&
358
359         git checkout branch2 &&
360         echo "3rd line in branch2" >>secondfile &&
361         git commit -a -m "change in branch2" &&
362         head3=$(git rev-parse --verify HEAD) &&
363
364         test_must_fail git pull . branch1 &&
365         git reset --hard &&
366         check_changes $head3
367 '
368
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
376 test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
377         git reset --hard HEAD^ &&
378         check_changes $head5 &&
379
380         git pull . branch1 &&
381         git reset --hard ORIG_HEAD &&
382         check_changes $head5 &&
383
384         git checkout master &&
385         git branch -D branch1 branch2 &&
386         check_changes $head5
387 '
388
389 test_expect_success 'test --mixed <paths>' '
390         echo 1 > file1 &&
391         echo 2 > file2 &&
392         git add file1 file2 &&
393         test_tick &&
394         git commit -m files &&
395         before1=$(git rev-parse --short HEAD:file1) &&
396         before2=$(git rev-parse --short HEAD:file2) &&
397         git rm file2 &&
398         echo 3 > file3 &&
399         echo 4 > file4 &&
400         echo 5 > file1 &&
401         after1=$(git rev-parse --short $(git hash-object file1)) &&
402         after4=$(git rev-parse --short $(git hash-object file4)) &&
403         git add file1 file3 file4 &&
404         git reset HEAD -- file1 file2 file3 &&
405         test_must_fail git diff --quiet &&
406         git diff > output &&
407
408         cat > expect <<-EOF &&
409         diff --git a/file1 b/file1
410         index $before1..$after1 100644
411         --- a/file1
412         +++ b/file1
413         @@ -1 +1 @@
414         -1
415         +5
416         diff --git a/file2 b/file2
417         deleted file mode 100644
418         index $before2..0000000
419         --- a/file2
420         +++ /dev/null
421         @@ -1 +0,0 @@
422         -2
423         EOF
424
425         test_cmp expect output &&
426         git diff --cached > output &&
427
428         cat > cached_expect <<-EOF &&
429         diff --git a/file4 b/file4
430         new file mode 100644
431         index 0000000..$after4
432         --- /dev/null
433         +++ b/file4
434         @@ -0,0 +1 @@
435         +4
436         EOF
437
438         test_cmp cached_expect output
439 '
440
441 test_expect_success 'test resetting the index at give paths' '
442
443         mkdir sub &&
444         >sub/file1 &&
445         >sub/file2 &&
446         git update-index --add sub/file1 sub/file2 &&
447         T=$(git write-tree) &&
448         git reset HEAD sub/file2 &&
449         test_must_fail git diff --quiet &&
450         U=$(git write-tree) &&
451         echo "$T" &&
452         echo "$U" &&
453         test_must_fail git diff-index --cached --exit-code "$T" &&
454         test "$T" != "$U"
455
456 '
457
458 test_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
465 cat > expect << EOF
466 Unstaged changes after reset:
467 M       file2
468 EOF
469
470 test_expect_success '--mixed refreshes the index' '
471         echo 123 >> file2 &&
472         git reset --mixed HEAD > output &&
473         test_i18ncmp expect output
474 '
475
476 test_expect_success 'resetting specific path that is unmerged' '
477         git rm --cached file2 &&
478         F1=$(git rev-parse HEAD:file1) &&
479         F2=$(git rev-parse HEAD:file2) &&
480         F3=$(git rev-parse HEAD:secondfile) &&
481         {
482                 echo "100644 $F1 1      file2" &&
483                 echo "100644 $F2 2      file2" &&
484                 echo "100644 $F3 3      file2"
485         } | git update-index --index-info &&
486         git ls-files -u &&
487         git reset HEAD file2 &&
488         test_must_fail git diff --quiet &&
489         git diff-index --exit-code --cached HEAD
490 '
491
492 test_expect_success 'disambiguation (1)' '
493
494         git reset --hard &&
495         >secondfile &&
496         git add secondfile &&
497         git reset secondfile &&
498         test_must_fail git diff --quiet -- secondfile &&
499         test -z "$(git diff --cached --name-only)" &&
500         test -f secondfile &&
501         test_must_be_empty secondfile
502
503 '
504
505 test_expect_success 'disambiguation (2)' '
506
507         git reset --hard &&
508         >secondfile &&
509         git add secondfile &&
510         rm -f secondfile &&
511         test_must_fail git reset secondfile &&
512         test -n "$(git diff --cached --name-only -- secondfile)" &&
513         test ! -f secondfile
514
515 '
516
517 test_expect_success 'disambiguation (3)' '
518
519         git reset --hard &&
520         >secondfile &&
521         git add secondfile &&
522         rm -f secondfile &&
523         git reset HEAD secondfile &&
524         test_must_fail git diff --quiet &&
525         test -z "$(git diff --cached --name-only)" &&
526         test ! -f secondfile
527
528 '
529
530 test_expect_success 'disambiguation (4)' '
531
532         git reset --hard &&
533         >secondfile &&
534         git add secondfile &&
535         rm -f secondfile &&
536         git reset -- secondfile &&
537         test_must_fail git diff --quiet &&
538         test -z "$(git diff --cached --name-only)" &&
539         test ! -f secondfile
540 '
541
542 test_expect_success 'reset with paths accepts tree' '
543         # for simpler tests, drop last commit containing added files
544         git reset --hard HEAD^ &&
545         git reset HEAD^^{tree} -- . &&
546         git diff --cached HEAD^ --exit-code &&
547         git diff HEAD --exit-code
548 '
549
550 test_expect_success 'reset -N keeps removed files as intent-to-add' '
551         echo new-file >new-file &&
552         git add new-file &&
553         git reset -N HEAD &&
554
555         tree=$(git write-tree) &&
556         git ls-tree $tree new-file >actual &&
557         test_must_be_empty actual &&
558
559         git diff --name-only >actual &&
560         echo new-file >expect &&
561         test_cmp expect actual
562 '
563
564 test_expect_success 'reset --mixed sets up work tree' '
565         git init mixed_worktree &&
566         (
567                 cd mixed_worktree &&
568                 test_commit dummy
569         ) &&
570         git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
571         test_must_be_empty actual
572 '
573
574 test_done