3 # Copyright (c) 2007 Carlos Rica
6 test_description='git reset
8 Documented tests for git reset'
12 test_expect_success 'creating initial files and commits' '
14 echo "1st file" >first &&
16 git commit -m "create 1st file" &&
18 echo "2nd file" >second &&
20 git commit -m "create 2nd file" &&
22 echo "2nd line 1st file" >>first &&
23 git commit -a -m "modify 1st file" &&
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
36 test "$(git rev-parse HEAD)" = "$1" &&
37 git diff | test_cmp .diff_expect - &&
38 git diff --cached | test_cmp .cached_expect - &&
43 done | test_cmp .cat_expect -
48 cat >.cat_expect <<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 &&
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
93 'trying to do reset --soft with pending merge should fail' '
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' '
142 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
143 git reset --hard HEAD &&
144 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
146 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
147 git reset --soft HEAD &&
148 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
150 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
151 git reset --mixed HEAD &&
152 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
154 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
156 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
160 cat >.cached_expect <<EOF
161 diff --git a/secondfile b/secondfile
162 index 1bbba79..44c5b58 100644
170 cat >.cat_expect <<EOF
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
184 cat >.cat_expect <<EOF
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
201 cat >.cat_expect <<EOF
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
217 cat >.cached_expect <<EOF
218 diff --git a/first b/first
219 deleted file mode 100644
220 index 8206c22..0000000
226 diff --git a/second b/second
227 deleted file mode 100644
228 index 1bbba79..0000000
233 diff --git a/secondfile b/secondfile
235 index 0000000..44c5b58
242 cat >.cat_expect <<EOF
247 test_expect_success \
248 'redoing changes adding them without commit them should succeed' '
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
267 diff --git a/second b/second
268 deleted file mode 100644
269 index 1bbba79..0000000
276 cat >.cat_expect <<EOF
281 test_expect_success '--mixed reset to HEAD should unadd the files' '
283 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
284 test "$(git rev-parse ORIG_HEAD)" = \
285 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
290 cat >.cat_expect <<EOF
295 test_expect_success 'redoing the last two commits should succeed' '
296 git add secondfile &&
297 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
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
311 cat >.cat_expect <<EOF
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 &&
331 check_changes 77abb337073fb4369a7ad69ff6f5ec0e4d6b54bb
336 cat >.cat_expect <<EOF
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
356 diff --git a/file1 b/file1
357 index d00491f..7ed6ff8 100644
363 diff --git a/file2 b/file2
364 deleted file mode 100644
365 index 0cfbf08..0000000
371 cat > cached_expect << EOF
372 diff --git a/file4 b/file4
374 index 0000000..b8626c4
380 test_expect_success 'test --mixed <paths>' '
383 git add file1 file2 &&
385 git commit -m files &&
390 git add file1 file3 file4 &&
391 git reset HEAD -- file1 file2 file3 &&
392 test_must_fail git diff --quiet &&
394 test_cmp output expect &&
395 git diff --cached > output &&
396 test_cmp output cached_expect
399 test_expect_success 'test resetting the index at give paths' '
404 git update-index --add sub/file1 sub/file2 &&
405 T=$(git write-tree) &&
406 git reset HEAD sub/file2 &&
407 test_must_fail git diff --quiet &&
408 U=$(git write-tree) &&
411 test_must_fail git diff-index --cached --exit-code "$T" &&
416 test_expect_success 'resetting an unmodified path is a no-op' '
418 git reset -- file1 &&
419 git diff-files --exit-code &&
420 git diff-index --cached --exit-code HEAD
424 Unstaged changes after reset:
428 test_expect_success '--mixed refreshes the index' '
430 git reset --mixed HEAD > output &&
431 test_i18ncmp expect output
434 test_expect_success 'resetting specific path that is unmerged' '
435 git rm --cached file2 &&
436 F1=$(git rev-parse HEAD:file1) &&
437 F2=$(git rev-parse HEAD:file2) &&
438 F3=$(git rev-parse HEAD:secondfile) &&
440 echo "100644 $F1 1 file2" &&
441 echo "100644 $F2 2 file2" &&
442 echo "100644 $F3 3 file2"
443 } | git update-index --index-info &&
445 git reset HEAD file2 &&
446 test_must_fail git diff --quiet &&
447 git diff-index --exit-code --cached HEAD
450 test_expect_success 'disambiguation (1)' '
454 git add secondfile &&
455 git reset secondfile &&
456 test_must_fail git diff --quiet -- secondfile &&
457 test -z "$(git diff --cached --name-only)" &&
458 test -f secondfile &&
463 test_expect_success 'disambiguation (2)' '
467 git add secondfile &&
469 test_must_fail git reset secondfile &&
470 test -n "$(git diff --cached --name-only -- secondfile)" &&
475 test_expect_success 'disambiguation (3)' '
479 git add secondfile &&
481 git reset HEAD secondfile &&
482 test_must_fail git diff --quiet &&
483 test -z "$(git diff --cached --name-only)" &&
488 test_expect_success 'disambiguation (4)' '
492 git add secondfile &&
494 git reset -- secondfile &&
495 test_must_fail git diff --quiet &&
496 test -z "$(git diff --cached --name-only)" &&
500 test_expect_success 'reset with paths accepts tree' '
501 # for simpler tests, drop last commit containing added files
502 git reset --hard HEAD^ &&
503 git reset HEAD^^{tree} -- . &&
504 git diff --cached HEAD^ --exit-code &&
505 git diff HEAD --exit-code