Merge 'install-wincred' into HEAD
[git/mingw/4msysgit.git] / t / t7102-reset.sh
blob215b12b7707344759aa285b7e97aca7054fedbf6
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 test_expect_success 'creating initial files and commits' '
26 test_tick &&
27 echo "1st file" >first &&
28 git add first &&
29 git commit -m "create 1st file" &&
31 echo "2nd file" >second &&
32 git add second &&
33 git commit -m "create 2nd file" &&
35 echo "2nd line 1st file" >>first &&
36 git commit -a -m "modify 1st file" &&
38 git rm first &&
39 git mv second secondfile &&
40 git commit -a -m "remove 1st and rename 2nd" &&
42 echo "1st line 2nd file" >secondfile &&
43 echo "2nd line 2nd file" >>secondfile &&
44 # "git commit -m" would break MinGW, as Windows refuse to pass
45 # iso8859-1 encoded parameter to git.
46 commit_msg iso8859-1 | git -c "i18n.commitEncoding=iso8859-1" commit -a -F - &&
47 head5=$(git rev-parse --verify HEAD)
49 # git log --pretty=oneline # to see those SHA1 involved
51 check_changes () {
52 test "$(git rev-parse HEAD)" = "$1" &&
53 git diff | test_cmp .diff_expect - &&
54 git diff --cached | test_cmp .cached_expect - &&
55 for FILE in *
57 echo $FILE':'
58 cat $FILE || return
59 done | test_cmp .cat_expect -
62 test_expect_success 'reset --hard message' '
63 hex=$(git log -1 --format="%h") &&
64 git reset --hard > .actual &&
65 echo HEAD is now at $hex $(commit_msg) > .expected &&
66 test_cmp .expected .actual
69 test_expect_success 'reset --hard message (iso8859-1 logoutputencoding)' '
70 hex=$(git log -1 --format="%h") &&
71 git -c "i18n.logOutputEncoding=iso8859-1" reset --hard > .actual &&
72 echo HEAD is now at $hex $(commit_msg iso8859-1) > .expected &&
73 test_cmp .expected .actual
76 >.diff_expect
77 >.cached_expect
78 cat >.cat_expect <<EOF
79 secondfile:
80 1st line 2nd file
81 2nd line 2nd file
82 EOF
84 test_expect_success 'giving a non existing revision should fail' '
85 test_must_fail git reset aaaaaa &&
86 test_must_fail git reset --mixed aaaaaa &&
87 test_must_fail git reset --soft aaaaaa &&
88 test_must_fail git reset --hard aaaaaa &&
89 check_changes $head5
92 test_expect_success 'reset --soft with unmerged index should fail' '
93 touch .git/MERGE_HEAD &&
94 echo "100644 44c5b5884550c17758737edcced463447b91d42b 1 un" |
95 git update-index --index-info &&
96 test_must_fail git reset --soft HEAD &&
97 rm .git/MERGE_HEAD &&
98 git rm --cached -- un
101 test_expect_success \
102 'giving paths with options different than --mixed should fail' '
103 test_must_fail git reset --soft -- first &&
104 test_must_fail git reset --hard -- first &&
105 test_must_fail git reset --soft HEAD^ -- first &&
106 test_must_fail git reset --hard HEAD^ -- first &&
107 check_changes $head5
110 test_expect_success 'giving unrecognized options should fail' '
111 test_must_fail git reset --other &&
112 test_must_fail git reset -o &&
113 test_must_fail git reset --mixed --other &&
114 test_must_fail git reset --mixed -o &&
115 test_must_fail git reset --soft --other &&
116 test_must_fail git reset --soft -o &&
117 test_must_fail git reset --hard --other &&
118 test_must_fail git reset --hard -o &&
119 check_changes $head5
122 test_expect_success \
123 'trying to do reset --soft with pending merge should fail' '
124 git branch branch1 &&
125 git branch branch2 &&
127 git checkout branch1 &&
128 echo "3rd line in branch1" >>secondfile &&
129 git commit -a -m "change in branch1" &&
131 git checkout branch2 &&
132 echo "3rd line in branch2" >>secondfile &&
133 git commit -a -m "change in branch2" &&
135 test_must_fail git merge branch1 &&
136 test_must_fail git reset --soft &&
138 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
139 git commit -a -m "the change in branch2" &&
141 git checkout master &&
142 git branch -D branch1 branch2 &&
143 check_changes $head5
146 test_expect_success \
147 'trying to do reset --soft with pending checkout merge should fail' '
148 git branch branch3 &&
149 git branch branch4 &&
151 git checkout branch3 &&
152 echo "3rd line in branch3" >>secondfile &&
153 git commit -a -m "line in branch3" &&
155 git checkout branch4 &&
156 echo "3rd line in branch4" >>secondfile &&
158 git checkout -m branch3 &&
159 test_must_fail git reset --soft &&
161 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
162 git commit -a -m "the line in branch3" &&
164 git checkout master &&
165 git branch -D branch3 branch4 &&
166 check_changes $head5
169 test_expect_success \
170 'resetting to HEAD with no changes should succeed and do nothing' '
171 git reset --hard &&
172 check_changes $head5 &&
173 git reset --hard HEAD &&
174 check_changes $head5 &&
175 git reset --soft &&
176 check_changes $head5 &&
177 git reset --soft HEAD &&
178 check_changes $head5 &&
179 git reset --mixed &&
180 check_changes $head5 &&
181 git reset --mixed HEAD &&
182 check_changes $head5 &&
183 git reset &&
184 check_changes $head5 &&
185 git reset HEAD &&
186 check_changes $head5
189 >.diff_expect
190 cat >.cached_expect <<EOF
191 diff --git a/secondfile b/secondfile
192 index 1bbba79..44c5b58 100644
193 --- a/secondfile
194 +++ b/secondfile
195 @@ -1 +1,2 @@
196 -2nd file
197 +1st line 2nd file
198 +2nd line 2nd file
200 cat >.cat_expect <<EOF
201 secondfile:
202 1st line 2nd file
203 2nd line 2nd file
205 test_expect_success '--soft reset only should show changes in diff --cached' '
206 git reset --soft HEAD^ &&
207 check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 &&
208 test "$(git rev-parse ORIG_HEAD)" = \
209 $head5
212 >.diff_expect
213 >.cached_expect
214 cat >.cat_expect <<EOF
215 secondfile:
216 1st line 2nd file
217 2nd line 2nd file
218 3rd line 2nd file
220 test_expect_success \
221 'changing files and redo the last commit should succeed' '
222 echo "3rd line 2nd file" >>secondfile &&
223 git commit -a -C ORIG_HEAD &&
224 head4=$(git rev-parse --verify HEAD) &&
225 check_changes $head4 &&
226 test "$(git rev-parse ORIG_HEAD)" = \
227 $head5
230 >.diff_expect
231 >.cached_expect
232 cat >.cat_expect <<EOF
233 first:
234 1st file
235 2nd line 1st file
236 second:
237 2nd file
239 test_expect_success \
240 '--hard reset should change the files and undo commits permanently' '
241 git reset --hard HEAD~2 &&
242 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
243 test "$(git rev-parse ORIG_HEAD)" = \
244 $head4
247 >.diff_expect
248 cat >.cached_expect <<EOF
249 diff --git a/first b/first
250 deleted file mode 100644
251 index 8206c22..0000000
252 --- a/first
253 +++ /dev/null
254 @@ -1,2 +0,0 @@
255 -1st file
256 -2nd line 1st file
257 diff --git a/second b/second
258 deleted file mode 100644
259 index 1bbba79..0000000
260 --- a/second
261 +++ /dev/null
262 @@ -1 +0,0 @@
263 -2nd file
264 diff --git a/secondfile b/secondfile
265 new file mode 100644
266 index 0000000..44c5b58
267 --- /dev/null
268 +++ b/secondfile
269 @@ -0,0 +1,2 @@
270 +1st line 2nd file
271 +2nd line 2nd file
273 cat >.cat_expect <<EOF
274 secondfile:
275 1st line 2nd file
276 2nd line 2nd file
278 test_expect_success \
279 'redoing changes adding them without commit them should succeed' '
280 git rm first &&
281 git mv second secondfile &&
283 echo "1st line 2nd file" >secondfile &&
284 echo "2nd line 2nd file" >>secondfile &&
285 git add secondfile &&
286 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
289 cat >.diff_expect <<EOF
290 diff --git a/first b/first
291 deleted file mode 100644
292 index 8206c22..0000000
293 --- a/first
294 +++ /dev/null
295 @@ -1,2 +0,0 @@
296 -1st file
297 -2nd line 1st file
298 diff --git a/second b/second
299 deleted file mode 100644
300 index 1bbba79..0000000
301 --- a/second
302 +++ /dev/null
303 @@ -1 +0,0 @@
304 -2nd file
306 >.cached_expect
307 cat >.cat_expect <<EOF
308 secondfile:
309 1st line 2nd file
310 2nd line 2nd file
312 test_expect_success '--mixed reset to HEAD should unadd the files' '
313 git reset &&
314 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
315 test "$(git rev-parse ORIG_HEAD)" = \
316 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
319 >.diff_expect
320 >.cached_expect
321 cat >.cat_expect <<EOF
322 secondfile:
323 1st line 2nd file
324 2nd line 2nd file
326 test_expect_success 'redoing the last two commits should succeed' '
327 git add secondfile &&
328 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
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 # iso8859-1 encoded parameter to git.
338 commit_msg iso8859-1 | git -c "i18n.commitEncoding=iso8859-1" commit -a -F - &&
339 check_changes $head5
342 >.diff_expect
343 >.cached_expect
344 cat >.cat_expect <<EOF
345 secondfile:
346 1st line 2nd file
347 2nd line 2nd file
348 3rd line in branch2
350 test_expect_success '--hard reset to HEAD should clear a failed merge' '
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 >.diff_expect
369 >.cached_expect
370 cat >.cat_expect <<EOF
371 secondfile:
372 1st line 2nd file
373 2nd line 2nd file
375 test_expect_success \
376 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
377 git reset --hard HEAD^ &&
378 check_changes $head5 &&
380 git pull . branch1 &&
381 git reset --hard ORIG_HEAD &&
382 check_changes $head5 &&
384 git checkout master &&
385 git branch -D branch1 branch2 &&
386 check_changes $head5
389 cat > expect << EOF
390 diff --git a/file1 b/file1
391 index d00491f..7ed6ff8 100644
392 --- a/file1
393 +++ b/file1
394 @@ -1 +1 @@
397 diff --git a/file2 b/file2
398 deleted file mode 100644
399 index 0cfbf08..0000000
400 --- a/file2
401 +++ /dev/null
402 @@ -1 +0,0 @@
405 cat > cached_expect << EOF
406 diff --git a/file4 b/file4
407 new file mode 100644
408 index 0000000..b8626c4
409 --- /dev/null
410 +++ b/file4
411 @@ -0,0 +1 @@
414 test_expect_success 'test --mixed <paths>' '
415 echo 1 > file1 &&
416 echo 2 > file2 &&
417 git add file1 file2 &&
418 test_tick &&
419 git commit -m files &&
420 git rm file2 &&
421 echo 3 > file3 &&
422 echo 4 > file4 &&
423 echo 5 > file1 &&
424 git add file1 file3 file4 &&
425 git reset HEAD -- file1 file2 file3 &&
426 test_must_fail git diff --quiet &&
427 git diff > output &&
428 test_cmp output expect &&
429 git diff --cached > output &&
430 test_cmp output cached_expect
433 test_expect_success 'test resetting the index at give paths' '
435 mkdir sub &&
436 >sub/file1 &&
437 >sub/file2 &&
438 git update-index --add sub/file1 sub/file2 &&
439 T=$(git write-tree) &&
440 git reset HEAD sub/file2 &&
441 test_must_fail git diff --quiet &&
442 U=$(git write-tree) &&
443 echo "$T" &&
444 echo "$U" &&
445 test_must_fail git diff-index --cached --exit-code "$T" &&
446 test "$T" != "$U"
450 test_expect_success 'resetting an unmodified path is a no-op' '
451 git reset --hard &&
452 git reset -- file1 &&
453 git diff-files --exit-code &&
454 git diff-index --cached --exit-code HEAD
457 cat > expect << EOF
458 Unstaged changes after reset:
459 M file2
462 test_expect_success '--mixed refreshes the index' '
463 echo 123 >> file2 &&
464 git reset --mixed HEAD > output &&
465 test_i18ncmp expect output
468 test_expect_success 'resetting specific path that is unmerged' '
469 git rm --cached file2 &&
470 F1=$(git rev-parse HEAD:file1) &&
471 F2=$(git rev-parse HEAD:file2) &&
472 F3=$(git rev-parse HEAD:secondfile) &&
474 echo "100644 $F1 1 file2" &&
475 echo "100644 $F2 2 file2" &&
476 echo "100644 $F3 3 file2"
477 } | git update-index --index-info &&
478 git ls-files -u &&
479 git reset HEAD file2 &&
480 test_must_fail git diff --quiet &&
481 git diff-index --exit-code --cached HEAD
484 test_expect_success 'disambiguation (1)' '
486 git reset --hard &&
487 >secondfile &&
488 git add secondfile &&
489 git reset secondfile &&
490 test_must_fail git diff --quiet -- secondfile &&
491 test -z "$(git diff --cached --name-only)" &&
492 test -f secondfile &&
493 test_must_be_empty secondfile
497 test_expect_success 'disambiguation (2)' '
499 git reset --hard &&
500 >secondfile &&
501 git add secondfile &&
502 rm -f secondfile &&
503 test_must_fail git reset secondfile &&
504 test -n "$(git diff --cached --name-only -- secondfile)" &&
505 test ! -f secondfile
509 test_expect_success 'disambiguation (3)' '
511 git reset --hard &&
512 >secondfile &&
513 git add secondfile &&
514 rm -f secondfile &&
515 git reset HEAD secondfile &&
516 test_must_fail git diff --quiet &&
517 test -z "$(git diff --cached --name-only)" &&
518 test ! -f secondfile
522 test_expect_success 'disambiguation (4)' '
524 git reset --hard &&
525 >secondfile &&
526 git add secondfile &&
527 rm -f secondfile &&
528 git reset -- secondfile &&
529 test_must_fail git diff --quiet &&
530 test -z "$(git diff --cached --name-only)" &&
531 test ! -f secondfile
534 test_expect_success 'reset with paths accepts tree' '
535 # for simpler tests, drop last commit containing added files
536 git reset --hard HEAD^ &&
537 git reset HEAD^^{tree} -- . &&
538 git diff --cached HEAD^ --exit-code &&
539 git diff HEAD --exit-code
542 test_expect_success 'reset --mixed sets up work tree' '
543 git init mixed_worktree &&
545 cd mixed_worktree &&
546 test_commit dummy
547 ) &&
548 : >expect &&
549 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
550 test_cmp expect actual
553 test_done