Merge branch 'bw/test-fix-grep-gnuism'
[alt-git.git] / t / t7102-reset.sh
blobb096dc88c2f4caf28887cf8fea5d700f0132f5a3
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 test_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" &&
18 echo "2nd file" >second &&
19 git add second &&
20 git commit -m "create 2nd file" &&
22 echo "2nd line 1st file" >>first &&
23 git commit -a -m "modify 1st file" &&
25 git rm first &&
26 git mv second secondfile &&
27 git commit -a -m "remove 1st and rename 2nd" &&
29 echo "1st line 2nd file" >secondfile &&
30 echo "2nd line 2nd file" >>secondfile &&
31 git commit -a -m "modify 2nd file"
33 # git log --pretty=oneline # to see those SHA1 involved
35 check_changes () {
36 test "$(git rev-parse HEAD)" = "$1" &&
37 git diff | test_cmp .diff_expect - &&
38 git diff --cached | test_cmp .cached_expect - &&
39 for FILE in *
41 echo $FILE':'
42 cat $FILE || return
43 done | test_cmp .cat_expect -
46 >.diff_expect
47 >.cached_expect
48 cat >.cat_expect <<EOF
49 secondfile:
50 1st line 2nd file
51 2nd line 2nd file
52 EOF
54 test_expect_success 'giving a non existing revision should fail' '
55 test_must_fail git reset aaaaaa &&
56 test_must_fail git reset --mixed aaaaaa &&
57 test_must_fail git reset --soft aaaaaa &&
58 test_must_fail git reset --hard aaaaaa &&
59 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
62 test_expect_success 'reset --soft with unmerged index should fail' '
63 touch .git/MERGE_HEAD &&
64 echo "100644 44c5b5884550c17758737edcced463447b91d42b 1 un" |
65 git update-index --index-info &&
66 test_must_fail git reset --soft HEAD &&
67 rm .git/MERGE_HEAD &&
68 git rm --cached -- un
71 test_expect_success \
72 'giving paths with options different than --mixed should fail' '
73 test_must_fail git reset --soft -- first &&
74 test_must_fail git reset --hard -- first &&
75 test_must_fail git reset --soft HEAD^ -- first &&
76 test_must_fail git reset --hard HEAD^ -- first &&
77 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
80 test_expect_success 'giving unrecognized options should fail' '
81 test_must_fail git reset --other &&
82 test_must_fail git reset -o &&
83 test_must_fail git reset --mixed --other &&
84 test_must_fail git reset --mixed -o &&
85 test_must_fail git reset --soft --other &&
86 test_must_fail git reset --soft -o &&
87 test_must_fail git reset --hard --other &&
88 test_must_fail git reset --hard -o &&
89 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
92 test_expect_success \
93 'trying to do reset --soft with pending merge should fail' '
94 git branch branch1 &&
95 git branch branch2 &&
97 git checkout branch1 &&
98 echo "3rd line in branch1" >>secondfile &&
99 git commit -a -m "change in branch1" &&
101 git checkout branch2 &&
102 echo "3rd line in branch2" >>secondfile &&
103 git commit -a -m "change in branch2" &&
105 test_must_fail git merge branch1 &&
106 test_must_fail git reset --soft &&
108 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
109 git commit -a -m "the change in branch2" &&
111 git checkout master &&
112 git branch -D branch1 branch2 &&
113 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
116 test_expect_success \
117 'trying to do reset --soft with pending checkout merge should fail' '
118 git branch branch3 &&
119 git branch branch4 &&
121 git checkout branch3 &&
122 echo "3rd line in branch3" >>secondfile &&
123 git commit -a -m "line in branch3" &&
125 git checkout branch4 &&
126 echo "3rd line in branch4" >>secondfile &&
128 git checkout -m branch3 &&
129 test_must_fail git reset --soft &&
131 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
132 git commit -a -m "the line in branch3" &&
134 git checkout master &&
135 git branch -D branch3 branch4 &&
136 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
139 test_expect_success \
140 'resetting to HEAD with no changes should succeed and do nothing' '
141 git reset --hard &&
142 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
143 git reset --hard HEAD &&
144 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
145 git reset --soft &&
146 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
147 git reset --soft HEAD &&
148 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
149 git reset --mixed &&
150 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
151 git reset --mixed HEAD &&
152 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
153 git reset &&
154 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
155 git reset HEAD &&
156 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
159 >.diff_expect
160 cat >.cached_expect <<EOF
161 diff --git a/secondfile b/secondfile
162 index 1bbba79..44c5b58 100644
163 --- a/secondfile
164 +++ b/secondfile
165 @@ -1 +1,2 @@
166 -2nd file
167 +1st line 2nd file
168 +2nd line 2nd file
170 cat >.cat_expect <<EOF
171 secondfile:
172 1st line 2nd file
173 2nd line 2nd file
175 test_expect_success '--soft reset only should show changes in diff --cached' '
176 git reset --soft HEAD^ &&
177 check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 &&
178 test "$(git rev-parse ORIG_HEAD)" = \
179 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
182 >.diff_expect
183 >.cached_expect
184 cat >.cat_expect <<EOF
185 secondfile:
186 1st line 2nd file
187 2nd line 2nd file
188 3rd line 2nd file
190 test_expect_success \
191 'changing files and redo the last commit should succeed' '
192 echo "3rd line 2nd file" >>secondfile &&
193 git commit -a -C ORIG_HEAD &&
194 check_changes 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d &&
195 test "$(git rev-parse ORIG_HEAD)" = \
196 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
199 >.diff_expect
200 >.cached_expect
201 cat >.cat_expect <<EOF
202 first:
203 1st file
204 2nd line 1st file
205 second:
206 2nd file
208 test_expect_success \
209 '--hard reset should change the files and undo commits permanently' '
210 git reset --hard HEAD~2 &&
211 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
212 test "$(git rev-parse ORIG_HEAD)" = \
213 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d
216 >.diff_expect
217 cat >.cached_expect <<EOF
218 diff --git a/first b/first
219 deleted file mode 100644
220 index 8206c22..0000000
221 --- a/first
222 +++ /dev/null
223 @@ -1,2 +0,0 @@
224 -1st file
225 -2nd line 1st file
226 diff --git a/second b/second
227 deleted file mode 100644
228 index 1bbba79..0000000
229 --- a/second
230 +++ /dev/null
231 @@ -1 +0,0 @@
232 -2nd file
233 diff --git a/secondfile b/secondfile
234 new file mode 100644
235 index 0000000..44c5b58
236 --- /dev/null
237 +++ b/secondfile
238 @@ -0,0 +1,2 @@
239 +1st line 2nd file
240 +2nd line 2nd file
242 cat >.cat_expect <<EOF
243 secondfile:
244 1st line 2nd file
245 2nd line 2nd file
247 test_expect_success \
248 'redoing changes adding them without commit them should succeed' '
249 git rm first &&
250 git mv second secondfile &&
252 echo "1st line 2nd file" >secondfile &&
253 echo "2nd line 2nd file" >>secondfile &&
254 git add secondfile &&
255 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
258 cat >.diff_expect <<EOF
259 diff --git a/first b/first
260 deleted file mode 100644
261 index 8206c22..0000000
262 --- a/first
263 +++ /dev/null
264 @@ -1,2 +0,0 @@
265 -1st file
266 -2nd line 1st file
267 diff --git a/second b/second
268 deleted file mode 100644
269 index 1bbba79..0000000
270 --- a/second
271 +++ /dev/null
272 @@ -1 +0,0 @@
273 -2nd file
275 >.cached_expect
276 cat >.cat_expect <<EOF
277 secondfile:
278 1st line 2nd file
279 2nd line 2nd file
281 test_expect_success '--mixed reset to HEAD should unadd the files' '
282 git reset &&
283 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
284 test "$(git rev-parse ORIG_HEAD)" = \
285 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
288 >.diff_expect
289 >.cached_expect
290 cat >.cat_expect <<EOF
291 secondfile:
292 1st line 2nd file
293 2nd line 2nd file
295 test_expect_success 'redoing the last two commits should succeed' '
296 git add secondfile &&
297 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
299 git rm first &&
300 git mv second secondfile &&
301 git commit -a -m "remove 1st and rename 2nd" &&
303 echo "1st line 2nd file" >secondfile &&
304 echo "2nd line 2nd file" >>secondfile &&
305 git commit -a -m "modify 2nd file" &&
306 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
309 >.diff_expect
310 >.cached_expect
311 cat >.cat_expect <<EOF
312 secondfile:
313 1st line 2nd file
314 2nd line 2nd file
315 3rd line in branch2
317 test_expect_success '--hard reset to HEAD should clear a failed merge' '
318 git branch branch1 &&
319 git branch branch2 &&
321 git checkout branch1 &&
322 echo "3rd line in branch1" >>secondfile &&
323 git commit -a -m "change in branch1" &&
325 git checkout branch2 &&
326 echo "3rd line in branch2" >>secondfile &&
327 git commit -a -m "change in branch2" &&
329 test_must_fail git pull . branch1 &&
330 git reset --hard &&
331 check_changes 77abb337073fb4369a7ad69ff6f5ec0e4d6b54bb
334 >.diff_expect
335 >.cached_expect
336 cat >.cat_expect <<EOF
337 secondfile:
338 1st line 2nd file
339 2nd line 2nd file
341 test_expect_success \
342 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
343 git reset --hard HEAD^ &&
344 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
346 git pull . branch1 &&
347 git reset --hard ORIG_HEAD &&
348 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
350 git checkout master &&
351 git branch -D branch1 branch2 &&
352 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
355 cat > expect << EOF
356 diff --git a/file1 b/file1
357 index d00491f..7ed6ff8 100644
358 --- a/file1
359 +++ b/file1
360 @@ -1 +1 @@
363 diff --git a/file2 b/file2
364 deleted file mode 100644
365 index 0cfbf08..0000000
366 --- a/file2
367 +++ /dev/null
368 @@ -1 +0,0 @@
371 cat > cached_expect << EOF
372 diff --git a/file4 b/file4
373 new file mode 100644
374 index 0000000..b8626c4
375 --- /dev/null
376 +++ b/file4
377 @@ -0,0 +1 @@
380 test_expect_success 'test --mixed <paths>' '
381 echo 1 > file1 &&
382 echo 2 > file2 &&
383 git add file1 file2 &&
384 test_tick &&
385 git commit -m files &&
386 git rm file2 &&
387 echo 3 > file3 &&
388 echo 4 > file4 &&
389 echo 5 > file1 &&
390 git add file1 file3 file4 &&
391 test_must_fail git reset HEAD -- file1 file2 file3 &&
392 git diff > output &&
393 test_cmp output expect &&
394 git diff --cached > output &&
395 test_cmp output cached_expect
398 test_expect_success 'test resetting the index at give paths' '
400 mkdir sub &&
401 >sub/file1 &&
402 >sub/file2 &&
403 git update-index --add sub/file1 sub/file2 &&
404 T=$(git write-tree) &&
405 test_must_fail git reset HEAD sub/file2 &&
406 U=$(git write-tree) &&
407 echo "$T" &&
408 echo "$U" &&
409 test_must_fail git diff-index --cached --exit-code "$T" &&
410 test "$T" != "$U"
414 test_expect_success 'resetting an unmodified path is a no-op' '
415 git reset --hard &&
416 git reset -- file1 &&
417 git diff-files --exit-code &&
418 git diff-index --cached --exit-code HEAD
421 cat > expect << EOF
422 Unstaged changes after reset:
423 M file2
426 test_expect_success '--mixed refreshes the index' '
427 echo 123 >> file2 &&
428 git reset --mixed HEAD > output &&
429 test_i18ncmp expect output
432 test_expect_success 'resetting specific path that is unmerged' '
433 git rm --cached file2 &&
434 F1=$(git rev-parse HEAD:file1) &&
435 F2=$(git rev-parse HEAD:file2) &&
436 F3=$(git rev-parse HEAD:secondfile) &&
438 echo "100644 $F1 1 file2" &&
439 echo "100644 $F2 2 file2" &&
440 echo "100644 $F3 3 file2"
441 } | git update-index --index-info &&
442 git ls-files -u &&
443 test_must_fail git reset HEAD file2 &&
444 git diff-index --exit-code --cached HEAD
447 test_expect_success 'disambiguation (1)' '
449 git reset --hard &&
450 >secondfile &&
451 git add secondfile &&
452 test_must_fail git reset secondfile &&
453 test -z "$(git diff --cached --name-only)" &&
454 test -f secondfile &&
455 test ! -s secondfile
459 test_expect_success 'disambiguation (2)' '
461 git reset --hard &&
462 >secondfile &&
463 git add secondfile &&
464 rm -f secondfile &&
465 test_must_fail git reset secondfile &&
466 test -n "$(git diff --cached --name-only -- secondfile)" &&
467 test ! -f secondfile
471 test_expect_success 'disambiguation (3)' '
473 git reset --hard &&
474 >secondfile &&
475 git add secondfile &&
476 rm -f secondfile &&
477 test_must_fail git reset HEAD secondfile &&
478 test -z "$(git diff --cached --name-only)" &&
479 test ! -f secondfile
483 test_expect_success 'disambiguation (4)' '
485 git reset --hard &&
486 >secondfile &&
487 git add secondfile &&
488 rm -f secondfile &&
489 test_must_fail git reset -- secondfile &&
490 test -z "$(git diff --cached --name-only)" &&
491 test ! -f secondfile
494 test_done