t4205 (log-pretty-formats): don't hardcode SHA-1 in expected outputs
[alt-git.git] / t / t7102-reset.sh
blob05dfb27cfa9f7dc881853252dadb1eee2f261417
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" &&
32 head5=$(git rev-parse --verify HEAD)
34 # git log --pretty=oneline # to see those SHA1 involved
36 check_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 *
42 echo $FILE':'
43 cat $FILE || return
44 done | test_cmp .cat_expect -
47 >.diff_expect
48 >.cached_expect
49 cat >.cat_expect <<EOF
50 secondfile:
51 1st line 2nd file
52 2nd line 2nd file
53 EOF
55 test_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
63 test_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
72 test_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
81 test_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
93 test_expect_success \
94 'trying to do reset --soft with pending merge should fail' '
95 git branch branch1 &&
96 git branch branch2 &&
98 git checkout branch1 &&
99 echo "3rd line in branch1" >>secondfile &&
100 git commit -a -m "change in branch1" &&
102 git checkout branch2 &&
103 echo "3rd line in branch2" >>secondfile &&
104 git commit -a -m "change in branch2" &&
106 test_must_fail git merge branch1 &&
107 test_must_fail git reset --soft &&
109 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
110 git commit -a -m "the change in branch2" &&
112 git checkout master &&
113 git branch -D branch1 branch2 &&
114 check_changes $head5
117 test_expect_success \
118 'trying to do reset --soft with pending checkout merge should fail' '
119 git branch branch3 &&
120 git branch branch4 &&
122 git checkout branch3 &&
123 echo "3rd line in branch3" >>secondfile &&
124 git commit -a -m "line in branch3" &&
126 git checkout branch4 &&
127 echo "3rd line in branch4" >>secondfile &&
129 git checkout -m branch3 &&
130 test_must_fail git reset --soft &&
132 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
133 git commit -a -m "the line in branch3" &&
135 git checkout master &&
136 git branch -D branch3 branch4 &&
137 check_changes $head5
140 test_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
160 >.diff_expect
161 cat >.cached_expect <<EOF
162 diff --git a/secondfile b/secondfile
163 index 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
171 cat >.cat_expect <<EOF
172 secondfile:
173 1st line 2nd file
174 2nd line 2nd file
176 test_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
183 >.diff_expect
184 >.cached_expect
185 cat >.cat_expect <<EOF
186 secondfile:
187 1st line 2nd file
188 2nd line 2nd file
189 3rd line 2nd file
191 test_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
201 >.diff_expect
202 >.cached_expect
203 cat >.cat_expect <<EOF
204 first:
205 1st file
206 2nd line 1st file
207 second:
208 2nd file
210 test_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
218 >.diff_expect
219 cat >.cached_expect <<EOF
220 diff --git a/first b/first
221 deleted file mode 100644
222 index 8206c22..0000000
223 --- a/first
224 +++ /dev/null
225 @@ -1,2 +0,0 @@
226 -1st file
227 -2nd line 1st file
228 diff --git a/second b/second
229 deleted file mode 100644
230 index 1bbba79..0000000
231 --- a/second
232 +++ /dev/null
233 @@ -1 +0,0 @@
234 -2nd file
235 diff --git a/secondfile b/secondfile
236 new file mode 100644
237 index 0000000..44c5b58
238 --- /dev/null
239 +++ b/secondfile
240 @@ -0,0 +1,2 @@
241 +1st line 2nd file
242 +2nd line 2nd file
244 cat >.cat_expect <<EOF
245 secondfile:
246 1st line 2nd file
247 2nd line 2nd file
249 test_expect_success \
250 'redoing changes adding them without commit them should succeed' '
251 git rm first &&
252 git mv second secondfile &&
254 echo "1st line 2nd file" >secondfile &&
255 echo "2nd line 2nd file" >>secondfile &&
256 git add secondfile &&
257 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
260 cat >.diff_expect <<EOF
261 diff --git a/first b/first
262 deleted file mode 100644
263 index 8206c22..0000000
264 --- a/first
265 +++ /dev/null
266 @@ -1,2 +0,0 @@
267 -1st file
268 -2nd line 1st file
269 diff --git a/second b/second
270 deleted file mode 100644
271 index 1bbba79..0000000
272 --- a/second
273 +++ /dev/null
274 @@ -1 +0,0 @@
275 -2nd file
277 >.cached_expect
278 cat >.cat_expect <<EOF
279 secondfile:
280 1st line 2nd file
281 2nd line 2nd file
283 test_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
290 >.diff_expect
291 >.cached_expect
292 cat >.cat_expect <<EOF
293 secondfile:
294 1st line 2nd file
295 2nd line 2nd file
297 test_expect_success 'redoing the last two commits should succeed' '
298 git add secondfile &&
299 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
301 git rm first &&
302 git mv second secondfile &&
303 git commit -a -m "remove 1st and rename 2nd" &&
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
311 >.diff_expect
312 >.cached_expect
313 cat >.cat_expect <<EOF
314 secondfile:
315 1st line 2nd file
316 2nd line 2nd file
317 3rd line in branch2
319 test_expect_success '--hard reset to HEAD should clear a failed merge' '
320 git branch branch1 &&
321 git branch branch2 &&
323 git checkout branch1 &&
324 echo "3rd line in branch1" >>secondfile &&
325 git commit -a -m "change in branch1" &&
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) &&
332 test_must_fail git pull . branch1 &&
333 git reset --hard &&
334 check_changes $head3
337 >.diff_expect
338 >.cached_expect
339 cat >.cat_expect <<EOF
340 secondfile:
341 1st line 2nd file
342 2nd line 2nd file
344 test_expect_success \
345 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
346 git reset --hard HEAD^ &&
347 check_changes $head5 &&
349 git pull . branch1 &&
350 git reset --hard ORIG_HEAD &&
351 check_changes $head5 &&
353 git checkout master &&
354 git branch -D branch1 branch2 &&
355 check_changes $head5
358 cat > expect << EOF
359 diff --git a/file1 b/file1
360 index d00491f..7ed6ff8 100644
361 --- a/file1
362 +++ b/file1
363 @@ -1 +1 @@
366 diff --git a/file2 b/file2
367 deleted file mode 100644
368 index 0cfbf08..0000000
369 --- a/file2
370 +++ /dev/null
371 @@ -1 +0,0 @@
374 cat > cached_expect << EOF
375 diff --git a/file4 b/file4
376 new file mode 100644
377 index 0000000..b8626c4
378 --- /dev/null
379 +++ b/file4
380 @@ -0,0 +1 @@
383 test_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
402 test_expect_success 'test resetting the index at give paths' '
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"
419 test_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
426 cat > expect << EOF
427 Unstaged changes after reset:
428 M file2
431 test_expect_success '--mixed refreshes the index' '
432 echo 123 >> file2 &&
433 git reset --mixed HEAD > output &&
434 test_i18ncmp expect output
437 test_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) &&
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
453 test_expect_success 'disambiguation (1)' '
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
466 test_expect_success 'disambiguation (2)' '
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
478 test_expect_success 'disambiguation (3)' '
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
491 test_expect_success 'disambiguation (4)' '
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
503 test_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
511 test_done