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 | git
diff .diff_expect
- &&
38 git
diff --cached | git
diff .cached_expect
- &&
43 done | git
diff .cat_expect
-
48 cat >.cat_expect
<<EOF
54 test_expect_success
'giving a non existing revision should fail' '
56 ! git reset --mixed aaaaaa &&
57 ! git reset --soft aaaaaa &&
58 ! git reset --hard aaaaaa &&
59 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
63 'giving paths with options different than --mixed should fail' '
64 ! git reset --soft -- first &&
65 ! git reset --hard -- first &&
66 ! git reset --soft HEAD^ -- first &&
67 ! git reset --hard HEAD^ -- first &&
68 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
71 test_expect_success
'giving unrecognized options should fail' '
72 ! git reset --other &&
74 ! git reset --mixed --other &&
75 ! git reset --mixed -o &&
76 ! git reset --soft --other &&
77 ! git reset --soft -o &&
78 ! git reset --hard --other &&
79 ! git reset --hard -o &&
80 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
84 'trying to do reset --soft with pending merge should fail' '
88 git checkout branch1 &&
89 echo "3rd line in branch1" >>secondfile &&
90 git commit -a -m "change in branch1" &&
92 git checkout branch2 &&
93 echo "3rd line in branch2" >>secondfile &&
94 git commit -a -m "change in branch2" &&
96 ! git merge branch1 &&
99 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
100 git commit -a -m "the change in branch2" &&
102 git checkout master &&
103 git branch -D branch1 branch2 &&
104 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
107 test_expect_success \
108 'trying to do reset --soft with pending checkout merge should fail' '
109 git branch branch3 &&
110 git branch branch4 &&
112 git checkout branch3 &&
113 echo "3rd line in branch3" >>secondfile &&
114 git commit -a -m "line in branch3" &&
116 git checkout branch4 &&
117 echo "3rd line in branch4" >>secondfile &&
119 git checkout -m branch3 &&
120 ! git reset --soft &&
122 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
123 git commit -a -m "the line in branch3" &&
125 git checkout master &&
126 git branch -D branch3 branch4 &&
127 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
130 test_expect_success \
131 'resetting to HEAD with no changes should succeed and do nothing' '
133 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
134 git reset --hard HEAD &&
135 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
137 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
138 git reset --soft HEAD &&
139 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
141 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
142 git reset --mixed HEAD &&
143 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
145 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
147 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
151 cat >.cached_expect
<<EOF
152 diff --git a/secondfile b/secondfile
153 index 1bbba79..44c5b58 100644
161 cat >.cat_expect
<<EOF
166 test_expect_success
'--soft reset only should show changes in diff --cached' '
167 git reset --soft HEAD^ &&
168 check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 &&
169 test "$(git rev-parse ORIG_HEAD)" = \
170 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
175 cat >.cat_expect
<<EOF
181 test_expect_success \
182 'changing files and redo the last commit should succeed' '
183 echo "3rd line 2nd file" >>secondfile &&
184 git commit -a -C ORIG_HEAD &&
185 check_changes 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d &&
186 test "$(git rev-parse ORIG_HEAD)" = \
187 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
192 cat >.cat_expect
<<EOF
199 test_expect_success \
200 '--hard reset should change the files and undo commits permanently' '
201 git reset --hard HEAD~2 &&
202 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
203 test "$(git rev-parse ORIG_HEAD)" = \
204 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d
208 cat >.cached_expect
<<EOF
209 diff --git a/first b/first
210 deleted file mode 100644
211 index 8206c22..0000000
217 diff --git a/second b/second
218 deleted file mode 100644
219 index 1bbba79..0000000
224 diff --git a/secondfile b/secondfile
226 index 0000000..44c5b58
233 cat >.cat_expect
<<EOF
238 test_expect_success \
239 'redoing changes adding them without commit them should succeed' '
241 git mv second secondfile &&
243 echo "1st line 2nd file" >secondfile &&
244 echo "2nd line 2nd file" >>secondfile &&
245 git add secondfile &&
246 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
249 cat >.diff_expect
<<EOF
250 diff --git a/first b/first
251 deleted file mode 100644
252 index 8206c22..0000000
258 diff --git a/second b/second
259 deleted file mode 100644
260 index 1bbba79..0000000
267 cat >.cat_expect
<<EOF
272 test_expect_success
'--mixed reset to HEAD should unadd the files' '
274 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
275 test "$(git rev-parse ORIG_HEAD)" = \
276 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
281 cat >.cat_expect
<<EOF
286 test_expect_success
'redoing the last two commits should succeed' '
287 git add secondfile &&
288 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
291 git mv second secondfile &&
292 git commit -a -m "remove 1st and rename 2nd" &&
294 echo "1st line 2nd file" >secondfile &&
295 echo "2nd line 2nd file" >>secondfile &&
296 git commit -a -m "modify 2nd file" &&
297 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
302 cat >.cat_expect
<<EOF
308 test_expect_success
'--hard reset to HEAD should clear a failed merge' '
309 git branch branch1 &&
310 git branch branch2 &&
312 git checkout branch1 &&
313 echo "3rd line in branch1" >>secondfile &&
314 git commit -a -m "change in branch1" &&
316 git checkout branch2 &&
317 echo "3rd line in branch2" >>secondfile &&
318 git commit -a -m "change in branch2" &&
320 ! git pull . branch1 &&
322 check_changes 77abb337073fb4369a7ad69ff6f5ec0e4d6b54bb
327 cat >.cat_expect
<<EOF
332 test_expect_success \
333 '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
334 git reset --hard HEAD^ &&
335 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
337 git pull . branch1 &&
338 git reset --hard ORIG_HEAD &&
339 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
341 git checkout master &&
342 git branch -D branch1 branch2 &&
343 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
347 diff --git a/file1 b/file1
348 index d00491f..7ed6ff8 100644
354 diff --git a/file2 b/file2
355 deleted file mode 100644
356 index 0cfbf08..0000000
362 cat > cached_expect
<< EOF
363 diff --git a/file4 b/file4
365 index 0000000..b8626c4
371 test_expect_success
'test --mixed <paths>' '
374 git add file1 file2 &&
376 git commit -m files &&
381 git add file1 file3 file4 &&
382 ! git reset HEAD -- file1 file2 file3 &&
384 git diff output expect &&
385 git diff --cached > output &&
386 git diff output cached_expect
389 test_expect_success
'test resetting the index at give paths' '
394 git update-index --add sub/file1 sub/file2 &&
395 T=$(git write-tree) &&
396 ! git reset HEAD sub/file2 &&
397 U=$(git write-tree) &&
400 ! git diff-index --cached --exit-code "$T" &&
405 test_expect_success
'resetting an unmodified path is a no-op' '
407 git reset -- file1 &&
408 git diff-files --exit-code &&
409 git diff-index --cached --exit-code HEAD