1#!/bin/sh
2#
3# Copyright (c) 2007 Carlos Rica
4#
5
6test_description='git reset
7
8Documented tests for git reset'
9
10. ./test-lib.sh
11
12test_expect_success 'creating initial files and commits' '
13 test_tick &&
14 echo "1st file" >first &&
15 git add first &&
16 git commit -m "create 1st file" &&
17
18 echo "2nd file" >second &&
19 git add second &&
20 git commit -m "create 2nd file" &&
21
22 echo "2nd line 1st file" >>first &&
23 git commit -a -m "modify 1st file" &&
24
25 git rm first &&
26 git mv second secondfile &&
27 git commit -a -m "remove 1st and rename 2nd" &&
28
29 echo "1st line 2nd file" >secondfile &&
30 echo "2nd line 2nd file" >>secondfile &&
31 git commit -a -m "modify 2nd file" &&
32 head5=$(git rev-parse --verify HEAD)
33'
34# git log --pretty=oneline # to see those SHA1 involved
35
36check_changes () {
37 test "$(git rev-parse HEAD)" = "$1" &&
38 git diff | test_cmp .diff_expect - &&
39 git diff --cached | test_cmp .cached_expect - &&
40 for FILE in *
41 do
42 echo $FILE':'
43 cat $FILE || return
44 done | test_cmp .cat_expect -
45}
46
47>.diff_expect
48>.cached_expect
49cat >.cat_expect <<EOF
50secondfile:
511st line 2nd file
522nd line 2nd file
53EOF
54
55test_expect_success 'giving a non existing revision should fail' '
56 test_must_fail git reset aaaaaa &&
57 test_must_fail git reset --mixed aaaaaa &&
58 test_must_fail git reset --soft aaaaaa &&
59 test_must_fail git reset --hard aaaaaa &&
60 check_changes $head5
61'
62
63test_expect_success 'reset --soft with unmerged index should fail' '
64 touch .git/MERGE_HEAD &&
65 echo "100644 44c5b5884550c17758737edcced463447b91d42b 1 un" |
66 git update-index --index-info &&
67 test_must_fail git reset --soft HEAD &&
68 rm .git/MERGE_HEAD &&
69 git rm --cached -- un
70'
71
72test_expect_success \
73 'giving paths with options different than --mixed should fail' '
74 test_must_fail git reset --soft -- first &&
75 test_must_fail git reset --hard -- first &&
76 test_must_fail git reset --soft HEAD^ -- first &&
77 test_must_fail git reset --hard HEAD^ -- first &&
78 check_changes $head5
79'
80
81test_expect_success 'giving unrecognized options should fail' '
82 test_must_fail git reset --other &&
83 test_must_fail git reset -o &&
84 test_must_fail git reset --mixed --other &&
85 test_must_fail git reset --mixed -o &&
86 test_must_fail git reset --soft --other &&
87 test_must_fail git reset --soft -o &&
88 test_must_fail git reset --hard --other &&
89 test_must_fail git reset --hard -o &&
90 check_changes $head5
91'
92
93test_expect_success \
94 'trying to do reset --soft with pending merge should fail' '
95 git branch branch1 &&
96 git branch branch2 &&
97
98 git checkout branch1 &&
99 echo "3rd line in branch1" >>secondfile &&
100 git commit -a -m "change in branch1" &&
101
102 git checkout branch2 &&
103 echo "3rd line in branch2" >>secondfile &&
104 git commit -a -m "change in branch2" &&
105
106 test_must_fail git merge branch1 &&
107 test_must_fail git reset --soft &&
108
109 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
110 git commit -a -m "the change in branch2" &&
111
112 git checkout master &&
113 git branch -D branch1 branch2 &&
114 check_changes $head5
115'
116
117test_expect_success \
118 'trying to do reset --soft with pending checkout merge should fail' '
119 git branch branch3 &&
120 git branch branch4 &&
121
122 git checkout branch3 &&
123 echo "3rd line in branch3" >>secondfile &&
124 git commit -a -m "line in branch3" &&
125
126 git checkout branch4 &&
127 echo "3rd line in branch4" >>secondfile &&
128
129 git checkout -m branch3 &&
130 test_must_fail git reset --soft &&
131
132 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
133 git commit -a -m "the line in branch3" &&
134
135 git checkout master &&
136 git branch -D branch3 branch4 &&
137 check_changes $head5
138'
139
140test_expect_success \
141 'resetting to HEAD with no changes should succeed and do nothing' '
142 git reset --hard &&
143 check_changes $head5 &&
144 git reset --hard HEAD &&
145 check_changes $head5 &&
146 git reset --soft &&
147 check_changes $head5 &&
148 git reset --soft HEAD &&
149 check_changes $head5 &&
150 git reset --mixed &&
151 check_changes $head5 &&
152 git reset --mixed HEAD &&
153 check_changes $head5 &&
154 git reset &&
155 check_changes $head5 &&
156 git reset HEAD &&
157 check_changes $head5
158'
159
160>.diff_expect
161cat >.cached_expect <<EOF
162diff --git a/secondfile b/secondfile
163index 1bbba79..44c5b58 100644
164--- a/secondfile
165+++ b/secondfile
166@@ -1 +1,2 @@
167-2nd file
168+1st line 2nd file
169+2nd line 2nd file
170EOF
171cat >.cat_expect <<EOF
172secondfile:
1731st line 2nd file
1742nd line 2nd file
175EOF
176test_expect_success '--soft reset only should show changes in diff --cached' '
177 git reset --soft HEAD^ &&
178 check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 &&
179 test "$(git rev-parse ORIG_HEAD)" = \
180 $head5
181'
182
183>.diff_expect
184>.cached_expect
185cat >.cat_expect <<EOF
186secondfile:
1871st line 2nd file
1882nd line 2nd file
1893rd line 2nd file
190EOF
191test_expect_success \
192 'changing files and redo the last commit should succeed' '
193 echo "3rd line 2nd file" >>secondfile &&
194 git commit -a -C ORIG_HEAD &&
195 head4=$(git rev-parse --verify HEAD) &&
196 check_changes $head4 &&
197 test "$(git rev-parse ORIG_HEAD)" = \
198 $head5
199'
200
201>.diff_expect
202>.cached_expect
203cat >.cat_expect <<EOF
204first:
2051st file
2062nd line 1st file
207second:
2082nd file
209EOF
210test_expect_success \
211 '--hard reset should change the files and undo commits permanently' '
212 git reset --hard HEAD~2 &&
213 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
214 test "$(git rev-parse ORIG_HEAD)" = \
215 $head4
216'
217
218>.diff_expect
219cat >.cached_expect <<EOF
220diff --git a/first b/first
221deleted file mode 100644
222index 8206c22..0000000
223--- a/first
224+++ /dev/null
225@@ -1,2 +0,0 @@
226-1st file
227-2nd line 1st file
228diff --git a/second b/second
229deleted file mode 100644
230index 1bbba79..0000000
231--- a/second
232+++ /dev/null
233@@ -1 +0,0 @@
234-2nd file
235diff --git a/secondfile b/secondfile
236new file mode 100644
237index 0000000..44c5b58
238--- /dev/null
239+++ b/secondfile
240@@ -0,0 +1,2 @@
241+1st line 2nd file
242+2nd line 2nd file
243EOF
244cat >.cat_expect <<EOF
245secondfile:
2461st line 2nd file
2472nd line 2nd file
248EOF
249test_expect_success \
250 'redoing changes adding them without commit them should succeed' '
251 git rm first &&
252 git mv second secondfile &&
253
254 echo "1st line 2nd file" >secondfile &&
255 echo "2nd line 2nd file" >>secondfile &&
256 git add secondfile &&
257 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
258'
259
260cat >.diff_expect <<EOF
261diff --git a/first b/first
262deleted file mode 100644
263index 8206c22..0000000
264--- a/first
265+++ /dev/null
266@@ -1,2 +0,0 @@
267-1st file
268-2nd line 1st file
269diff --git a/second b/second
270deleted file mode 100644
271index 1bbba79..0000000
272--- a/second
273+++ /dev/null
274@@ -1 +0,0 @@
275-2nd file
276EOF
277>.cached_expect
278cat >.cat_expect <<EOF
279secondfile:
2801st line 2nd file
2812nd line 2nd file
282EOF
283test_expect_success '--mixed reset to HEAD should unadd the files' '
284 git reset &&
285 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
286 test "$(git rev-parse ORIG_HEAD)" = \
287 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
288'
289
290>.diff_expect
291>.cached_expect
292cat >.cat_expect <<EOF
293secondfile:
2941st line 2nd file
2952nd line 2nd file
296EOF
297test_expect_success 'redoing the last two commits should succeed' '
298 git add secondfile &&
299 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
300
301 git rm first &&
302 git mv second secondfile &&
303 git commit -a -m "remove 1st and rename 2nd" &&
304
305 echo "1st line 2nd file" >secondfile &&
306 echo "2nd line 2nd file" >>secondfile &&
307 git commit -a -m "modify 2nd file" &&
308 check_changes $head5
309'
310
311>.diff_expect
312>.cached_expect
313cat >.cat_expect <<EOF
314secondfile:
3151st line 2nd file
3162nd line 2nd file
3173rd line in branch2
318EOF
319test_expect_success '--hard reset to HEAD should clear a failed merge' '
320 git branch branch1 &&
321 git branch branch2 &&
322
323 git checkout branch1 &&
324 echo "3rd line in branch1" >>secondfile &&
325 git commit -a -m "change in branch1" &&
326
327 git checkout branch2 &&
328 echo "3rd line in branch2" >>secondfile &&
329 git commit -a -m "change in branch2" &&
330 head3=$(git rev-parse --verify HEAD) &&
331
332 test_must_fail git pull . branch1 &&
333 git reset --hard &&
334 check_changes $head3
335'
336
337>.diff_expect
338>.cached_expect
339cat >.cat_expect <<EOF
340secondfile:
3411st line 2nd file
3422nd line 2nd file
343EOF
344test_expect_success \
345 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
346 git reset --hard HEAD^ &&
347 check_changes $head5 &&
348
349 git pull . branch1 &&
350 git reset --hard ORIG_HEAD &&
351 check_changes $head5 &&
352
353 git checkout master &&
354 git branch -D branch1 branch2 &&
355 check_changes $head5
356'
357
358cat > expect << EOF
359diff --git a/file1 b/file1
360index d00491f..7ed6ff8 100644
361--- a/file1
362+++ b/file1
363@@ -1 +1 @@
364-1
365+5
366diff --git a/file2 b/file2
367deleted file mode 100644
368index 0cfbf08..0000000
369--- a/file2
370+++ /dev/null
371@@ -1 +0,0 @@
372-2
373EOF
374cat > cached_expect << EOF
375diff --git a/file4 b/file4
376new file mode 100644
377index 0000000..b8626c4
378--- /dev/null
379+++ b/file4
380@@ -0,0 +1 @@
381+4
382EOF
383test_expect_success 'test --mixed <paths>' '
384 echo 1 > file1 &&
385 echo 2 > file2 &&
386 git add file1 file2 &&
387 test_tick &&
388 git commit -m files &&
389 git rm file2 &&
390 echo 3 > file3 &&
391 echo 4 > file4 &&
392 echo 5 > file1 &&
393 git add file1 file3 file4 &&
394 git reset HEAD -- file1 file2 file3 &&
395 test_must_fail git diff --quiet &&
396 git diff > output &&
397 test_cmp output expect &&
398 git diff --cached > output &&
399 test_cmp output cached_expect
400'
401
402test_expect_success 'test resetting the index at give paths' '
403
404 mkdir sub &&
405 >sub/file1 &&
406 >sub/file2 &&
407 git update-index --add sub/file1 sub/file2 &&
408 T=$(git write-tree) &&
409 git reset HEAD sub/file2 &&
410 test_must_fail git diff --quiet &&
411 U=$(git write-tree) &&
412 echo "$T" &&
413 echo "$U" &&
414 test_must_fail git diff-index --cached --exit-code "$T" &&
415 test "$T" != "$U"
416
417'
418
419test_expect_success 'resetting an unmodified path is a no-op' '
420 git reset --hard &&
421 git reset -- file1 &&
422 git diff-files --exit-code &&
423 git diff-index --cached --exit-code HEAD
424'
425
426cat > expect << EOF
427Unstaged changes after reset:
428M file2
429EOF
430
431test_expect_success '--mixed refreshes the index' '
432 echo 123 >> file2 &&
433 git reset --mixed HEAD > output &&
434 test_i18ncmp expect output
435'
436
437test_expect_success 'resetting specific path that is unmerged' '
438 git rm --cached file2 &&
439 F1=$(git rev-parse HEAD:file1) &&
440 F2=$(git rev-parse HEAD:file2) &&
441 F3=$(git rev-parse HEAD:secondfile) &&
442 {
443 echo "100644 $F1 1 file2" &&
444 echo "100644 $F2 2 file2" &&
445 echo "100644 $F3 3 file2"
446 } | git update-index --index-info &&
447 git ls-files -u &&
448 git reset HEAD file2 &&
449 test_must_fail git diff --quiet &&
450 git diff-index --exit-code --cached HEAD
451'
452
453test_expect_success 'disambiguation (1)' '
454
455 git reset --hard &&
456 >secondfile &&
457 git add secondfile &&
458 git reset secondfile &&
459 test_must_fail git diff --quiet -- secondfile &&
460 test -z "$(git diff --cached --name-only)" &&
461 test -f secondfile &&
462 test ! -s secondfile
463
464'
465
466test_expect_success 'disambiguation (2)' '
467
468 git reset --hard &&
469 >secondfile &&
470 git add secondfile &&
471 rm -f secondfile &&
472 test_must_fail git reset secondfile &&
473 test -n "$(git diff --cached --name-only -- secondfile)" &&
474 test ! -f secondfile
475
476'
477
478test_expect_success 'disambiguation (3)' '
479
480 git reset --hard &&
481 >secondfile &&
482 git add secondfile &&
483 rm -f secondfile &&
484 git reset HEAD secondfile &&
485 test_must_fail git diff --quiet &&
486 test -z "$(git diff --cached --name-only)" &&
487 test ! -f secondfile
488
489'
490
491test_expect_success 'disambiguation (4)' '
492
493 git reset --hard &&
494 >secondfile &&
495 git add secondfile &&
496 rm -f secondfile &&
497 git reset -- secondfile &&
498 test_must_fail git diff --quiet &&
499 test -z "$(git diff --cached --name-only)" &&
500 test ! -f secondfile
501'
502
503test_expect_success 'reset with paths accepts tree' '
504 # for simpler tests, drop last commit containing added files
505 git reset --hard HEAD^ &&
506 git reset HEAD^^{tree} -- . &&
507 git diff --cached HEAD^ --exit-code &&
508 git diff HEAD --exit-code
509'
510
511test_done