Migrate git-clean.sh to use git-rev-parse --parseopt.
[git/git-p4.git] / t / t7102-reset.sh
blobcea9afb764226d3086450cb7c53c881bb9803eec
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"
33 # git log --pretty=oneline # to see those SHA1 involved
35 check_changes () {
36 test "$(git rev-parse HEAD)" = "$1" &&
37 git diff | git diff .diff_expect - &&
38 git diff --cached | git diff .cached_expect - &&
39 for FILE in *
41 echo $FILE':'
42 cat $FILE || return
43 done | git diff .cat_expect -
46 >.diff_expect
47 >.cached_expect
48 cat >.cat_expect <<EOF
49 secondfile:
50 1st line 2nd file
51 2nd line 2nd file
52 EOF
54 test_expect_success 'giving a non existing revision should fail' '
55 ! git reset aaaaaa &&
56 ! git reset --mixed aaaaaa &&
57 ! git reset --soft aaaaaa &&
58 ! git reset --hard aaaaaa &&
59 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
62 test_expect_success \
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 &&
73 ! git reset -o &&
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
83 test_expect_success \
84 'trying to do reset --soft with pending merge should fail' '
85 git branch branch1 &&
86 git branch branch2 &&
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 &&
97 ! git reset --soft &&
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' '
132 git reset --hard &&
133 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
134 git reset --hard HEAD &&
135 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
136 git reset --soft &&
137 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
138 git reset --soft HEAD &&
139 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
140 git reset --mixed &&
141 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
142 git reset --mixed HEAD &&
143 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
144 git reset &&
145 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
146 git reset HEAD &&
147 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
150 >.diff_expect
151 cat >.cached_expect <<EOF
152 diff --git a/secondfile b/secondfile
153 index 1bbba79..44c5b58 100644
154 --- a/secondfile
155 +++ b/secondfile
156 @@ -1 +1,2 @@
157 -2nd file
158 +1st line 2nd file
159 +2nd line 2nd file
161 cat >.cat_expect <<EOF
162 secondfile:
163 1st line 2nd file
164 2nd line 2nd file
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
173 >.diff_expect
174 >.cached_expect
175 cat >.cat_expect <<EOF
176 secondfile:
177 1st line 2nd file
178 2nd line 2nd file
179 3rd line 2nd file
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
190 >.diff_expect
191 >.cached_expect
192 cat >.cat_expect <<EOF
193 first:
194 1st file
195 2nd line 1st file
196 second:
197 2nd file
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
207 >.diff_expect
208 cat >.cached_expect <<EOF
209 diff --git a/first b/first
210 deleted file mode 100644
211 index 8206c22..0000000
212 --- a/first
213 +++ /dev/null
214 @@ -1,2 +0,0 @@
215 -1st file
216 -2nd line 1st file
217 diff --git a/second b/second
218 deleted file mode 100644
219 index 1bbba79..0000000
220 --- a/second
221 +++ /dev/null
222 @@ -1 +0,0 @@
223 -2nd file
224 diff --git a/secondfile b/secondfile
225 new file mode 100644
226 index 0000000..44c5b58
227 --- /dev/null
228 +++ b/secondfile
229 @@ -0,0 +1,2 @@
230 +1st line 2nd file
231 +2nd line 2nd file
233 cat >.cat_expect <<EOF
234 secondfile:
235 1st line 2nd file
236 2nd line 2nd file
238 test_expect_success \
239 'redoing changes adding them without commit them should succeed' '
240 git rm first &&
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
253 --- a/first
254 +++ /dev/null
255 @@ -1,2 +0,0 @@
256 -1st file
257 -2nd line 1st file
258 diff --git a/second b/second
259 deleted file mode 100644
260 index 1bbba79..0000000
261 --- a/second
262 +++ /dev/null
263 @@ -1 +0,0 @@
264 -2nd file
266 >.cached_expect
267 cat >.cat_expect <<EOF
268 secondfile:
269 1st line 2nd file
270 2nd line 2nd file
272 test_expect_success '--mixed reset to HEAD should unadd the files' '
273 git reset &&
274 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
275 test "$(git rev-parse ORIG_HEAD)" = \
276 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
279 >.diff_expect
280 >.cached_expect
281 cat >.cat_expect <<EOF
282 secondfile:
283 1st line 2nd file
284 2nd line 2nd file
286 test_expect_success 'redoing the last two commits should succeed' '
287 git add secondfile &&
288 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
290 git rm first &&
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
300 >.diff_expect
301 >.cached_expect
302 cat >.cat_expect <<EOF
303 secondfile:
304 1st line 2nd file
305 2nd line 2nd file
306 3rd line in branch2
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 &&
321 git reset --hard &&
322 check_changes 77abb337073fb4369a7ad69ff6f5ec0e4d6b54bb
325 >.diff_expect
326 >.cached_expect
327 cat >.cat_expect <<EOF
328 secondfile:
329 1st line 2nd file
330 2nd line 2nd file
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
346 cat > expect << EOF
347 diff --git a/file1 b/file1
348 index d00491f..7ed6ff8 100644
349 --- a/file1
350 +++ b/file1
351 @@ -1 +1 @@
354 diff --git a/file2 b/file2
355 deleted file mode 100644
356 index 0cfbf08..0000000
357 --- a/file2
358 +++ /dev/null
359 @@ -1 +0,0 @@
362 cat > cached_expect << EOF
363 diff --git a/file4 b/file4
364 new file mode 100644
365 index 0000000..b8626c4
366 --- /dev/null
367 +++ b/file4
368 @@ -0,0 +1 @@
371 test_expect_success 'test --mixed <paths>' '
372 echo 1 > file1 &&
373 echo 2 > file2 &&
374 git add file1 file2 &&
375 test_tick &&
376 git commit -m files &&
377 git rm file2 &&
378 echo 3 > file3 &&
379 echo 4 > file4 &&
380 echo 5 > file1 &&
381 git add file1 file3 file4 &&
382 ! git reset HEAD -- file1 file2 file3 &&
383 git diff > output &&
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' '
391 mkdir sub &&
392 >sub/file1 &&
393 >sub/file2 &&
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) &&
398 echo "$T" &&
399 echo "$U" &&
400 ! git diff-index --cached --exit-code "$T" &&
401 test "$T" != "$U"
405 test_expect_success 'resetting an unmodified path is a no-op' '
406 git reset --hard &&
407 git reset -- file1 &&
408 git diff-files --exit-code &&
409 git diff-index --cached --exit-code HEAD
412 test_done