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 test_must_fail git reset HEAD -- file1 file2 file3 &&
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' '
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) &&
409 test_must_fail git diff-index --cached --exit-code "$T" &&
414 test_expect_success 'resetting an unmodified path is a no-op' '
416 git reset -- file1 &&
417 git diff-files --exit-code &&
418 git diff-index --cached --exit-code HEAD
422 Unstaged changes after reset:
426 test_expect_success '--mixed refreshes the index' '
428 git reset --mixed HEAD > output &&
429 test_cmp expect output
432 test_expect_success 'disambiguation (1)' '
436 git add secondfile &&
437 test_must_fail git reset secondfile &&
438 test -z "$(git diff --cached --name-only)" &&
439 test -f secondfile &&
444 test_expect_success 'disambiguation (2)' '
448 git add secondfile &&
450 test_must_fail git reset secondfile &&
451 test -n "$(git diff --cached --name-only -- secondfile)" &&
456 test_expect_success 'disambiguation (3)' '
460 git add secondfile &&
462 test_must_fail git reset HEAD secondfile &&
463 test -z "$(git diff --cached --name-only)" &&
468 test_expect_success 'disambiguation (4)' '
472 git add secondfile &&
474 test_must_fail git reset -- secondfile &&
475 test -z "$(git diff --cached --name-only)" &&