Merge branch 'aw/mirror-push' into jk/send-pack
[git/git.git] / t / t7102-reset.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Carlos Rica
4 #
5
6 test_description='git-reset
7
8 Documented tests for git-reset'
9
10 . ./test-lib.sh
11
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" &&
17
18 echo "2nd file" >second &&
19 git add second &&
20 git commit -m "create 2nd file" &&
21
22 echo "2nd line 1st file" >>first &&
23 git commit -a -m "modify 1st file" &&
24
25 git rm first &&
26 git mv second secondfile &&
27 git commit -a -m "remove 1st and rename 2nd" &&
28
29 echo "1st line 2nd file" >secondfile &&
30 echo "2nd line 2nd file" >>secondfile &&
31 git commit -a -m "modify 2nd file"
32 '
33 # git log --pretty=oneline # to see those SHA1 involved
34
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 *
40 do
41 echo $FILE':'
42 cat $FILE || return
43 done | git diff .cat_expect -
44 }
45
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
53
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
60 '
61
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 ! git reset --soft HEAD &&
67 rm .git/MERGE_HEAD &&
68 git rm --cached -- un
69 '
70
71 test_expect_success \
72 'giving paths with options different than --mixed should fail' '
73 ! git reset --soft -- first &&
74 ! git reset --hard -- first &&
75 ! git reset --soft HEAD^ -- first &&
76 ! git reset --hard HEAD^ -- first &&
77 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
78 '
79
80 test_expect_success 'giving unrecognized options should fail' '
81 ! git reset --other &&
82 ! git reset -o &&
83 ! git reset --mixed --other &&
84 ! git reset --mixed -o &&
85 ! git reset --soft --other &&
86 ! git reset --soft -o &&
87 ! git reset --hard --other &&
88 ! git reset --hard -o &&
89 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
90 '
91
92 test_expect_success \
93 'trying to do reset --soft with pending merge should fail' '
94 git branch branch1 &&
95 git branch branch2 &&
96
97 git checkout branch1 &&
98 echo "3rd line in branch1" >>secondfile &&
99 git commit -a -m "change in branch1" &&
100
101 git checkout branch2 &&
102 echo "3rd line in branch2" >>secondfile &&
103 git commit -a -m "change in branch2" &&
104
105 ! git merge branch1 &&
106 ! git reset --soft &&
107
108 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
109 git commit -a -m "the change in branch2" &&
110
111 git checkout master &&
112 git branch -D branch1 branch2 &&
113 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
114 '
115
116 test_expect_success \
117 'trying to do reset --soft with pending checkout merge should fail' '
118 git branch branch3 &&
119 git branch branch4 &&
120
121 git checkout branch3 &&
122 echo "3rd line in branch3" >>secondfile &&
123 git commit -a -m "line in branch3" &&
124
125 git checkout branch4 &&
126 echo "3rd line in branch4" >>secondfile &&
127
128 git checkout -m branch3 &&
129 ! git reset --soft &&
130
131 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
132 git commit -a -m "the line in branch3" &&
133
134 git checkout master &&
135 git branch -D branch3 branch4 &&
136 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
137 '
138
139 test_expect_success \
140 'resetting to HEAD with no changes should succeed and do nothing' '
141 git reset --hard &&
142 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
143 git reset --hard HEAD &&
144 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
145 git reset --soft &&
146 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
147 git reset --soft HEAD &&
148 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
149 git reset --mixed &&
150 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
151 git reset --mixed HEAD &&
152 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
153 git reset &&
154 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
155 git reset HEAD &&
156 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
157 '
158
159 >.diff_expect
160 cat >.cached_expect <<EOF
161 diff --git a/secondfile b/secondfile
162 index 1bbba79..44c5b58 100644
163 --- a/secondfile
164 +++ b/secondfile
165 @@ -1 +1,2 @@
166 -2nd file
167 +1st line 2nd file
168 +2nd line 2nd file
169 EOF
170 cat >.cat_expect <<EOF
171 secondfile:
172 1st line 2nd file
173 2nd line 2nd file
174 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
180 '
181
182 >.diff_expect
183 >.cached_expect
184 cat >.cat_expect <<EOF
185 secondfile:
186 1st line 2nd file
187 2nd line 2nd file
188 3rd line 2nd file
189 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
197 '
198
199 >.diff_expect
200 >.cached_expect
201 cat >.cat_expect <<EOF
202 first:
203 1st file
204 2nd line 1st file
205 second:
206 2nd file
207 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
214 '
215
216 >.diff_expect
217 cat >.cached_expect <<EOF
218 diff --git a/first b/first
219 deleted file mode 100644
220 index 8206c22..0000000
221 --- a/first
222 +++ /dev/null
223 @@ -1,2 +0,0 @@
224 -1st file
225 -2nd line 1st file
226 diff --git a/second b/second
227 deleted file mode 100644
228 index 1bbba79..0000000
229 --- a/second
230 +++ /dev/null
231 @@ -1 +0,0 @@
232 -2nd file
233 diff --git a/secondfile b/secondfile
234 new file mode 100644
235 index 0000000..44c5b58
236 --- /dev/null
237 +++ b/secondfile
238 @@ -0,0 +1,2 @@
239 +1st line 2nd file
240 +2nd line 2nd file
241 EOF
242 cat >.cat_expect <<EOF
243 secondfile:
244 1st line 2nd file
245 2nd line 2nd file
246 EOF
247 test_expect_success \
248 'redoing changes adding them without commit them should succeed' '
249 git rm first &&
250 git mv second secondfile &&
251
252 echo "1st line 2nd file" >secondfile &&
253 echo "2nd line 2nd file" >>secondfile &&
254 git add secondfile &&
255 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
256 '
257
258 cat >.diff_expect <<EOF
259 diff --git a/first b/first
260 deleted file mode 100644
261 index 8206c22..0000000
262 --- a/first
263 +++ /dev/null
264 @@ -1,2 +0,0 @@
265 -1st file
266 -2nd line 1st file
267 diff --git a/second b/second
268 deleted file mode 100644
269 index 1bbba79..0000000
270 --- a/second
271 +++ /dev/null
272 @@ -1 +0,0 @@
273 -2nd file
274 EOF
275 >.cached_expect
276 cat >.cat_expect <<EOF
277 secondfile:
278 1st line 2nd file
279 2nd line 2nd file
280 EOF
281 test_expect_success '--mixed reset to HEAD should unadd the files' '
282 git reset &&
283 check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
284 test "$(git rev-parse ORIG_HEAD)" = \
285 ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
286 '
287
288 >.diff_expect
289 >.cached_expect
290 cat >.cat_expect <<EOF
291 secondfile:
292 1st line 2nd file
293 2nd line 2nd file
294 EOF
295 test_expect_success 'redoing the last two commits should succeed' '
296 git add secondfile &&
297 git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
298
299 git rm first &&
300 git mv second secondfile &&
301 git commit -a -m "remove 1st and rename 2nd" &&
302
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
307 '
308
309 >.diff_expect
310 >.cached_expect
311 cat >.cat_expect <<EOF
312 secondfile:
313 1st line 2nd file
314 2nd line 2nd file
315 3rd line in branch2
316 EOF
317 test_expect_success '--hard reset to HEAD should clear a failed merge' '
318 git branch branch1 &&
319 git branch branch2 &&
320
321 git checkout branch1 &&
322 echo "3rd line in branch1" >>secondfile &&
323 git commit -a -m "change in branch1" &&
324
325 git checkout branch2 &&
326 echo "3rd line in branch2" >>secondfile &&
327 git commit -a -m "change in branch2" &&
328
329 ! git pull . branch1 &&
330 git reset --hard &&
331 check_changes 77abb337073fb4369a7ad69ff6f5ec0e4d6b54bb
332 '
333
334 >.diff_expect
335 >.cached_expect
336 cat >.cat_expect <<EOF
337 secondfile:
338 1st line 2nd file
339 2nd line 2nd file
340 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 &&
345
346 git pull . branch1 &&
347 git reset --hard ORIG_HEAD &&
348 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
349
350 git checkout master &&
351 git branch -D branch1 branch2 &&
352 check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
353 '
354
355 cat > expect << EOF
356 diff --git a/file1 b/file1
357 index d00491f..7ed6ff8 100644
358 --- a/file1
359 +++ b/file1
360 @@ -1 +1 @@
361 -1
362 +5
363 diff --git a/file2 b/file2
364 deleted file mode 100644
365 index 0cfbf08..0000000
366 --- a/file2
367 +++ /dev/null
368 @@ -1 +0,0 @@
369 -2
370 EOF
371 cat > cached_expect << EOF
372 diff --git a/file4 b/file4
373 new file mode 100644
374 index 0000000..b8626c4
375 --- /dev/null
376 +++ b/file4
377 @@ -0,0 +1 @@
378 +4
379 EOF
380 test_expect_success 'test --mixed <paths>' '
381 echo 1 > file1 &&
382 echo 2 > file2 &&
383 git add file1 file2 &&
384 test_tick &&
385 git commit -m files &&
386 git rm file2 &&
387 echo 3 > file3 &&
388 echo 4 > file4 &&
389 echo 5 > file1 &&
390 git add file1 file3 file4 &&
391 ! git reset HEAD -- file1 file2 file3 &&
392 git diff > output &&
393 git diff output expect &&
394 git diff --cached > output &&
395 git diff output cached_expect
396 '
397
398 test_expect_success 'test resetting the index at give paths' '
399
400 mkdir sub &&
401 >sub/file1 &&
402 >sub/file2 &&
403 git update-index --add sub/file1 sub/file2 &&
404 T=$(git write-tree) &&
405 ! git reset HEAD sub/file2 &&
406 U=$(git write-tree) &&
407 echo "$T" &&
408 echo "$U" &&
409 ! git diff-index --cached --exit-code "$T" &&
410 test "$T" != "$U"
411
412 '
413
414 test_expect_success 'resetting an unmodified path is a no-op' '
415 git reset --hard &&
416 git reset -- file1 &&
417 git diff-files --exit-code &&
418 git diff-index --cached --exit-code HEAD
419 '
420
421 cat > expect << EOF
422 file2: needs update
423 EOF
424
425 test_expect_success '--mixed refreshes the index' '
426 echo 123 >> file2 &&
427 git reset --mixed HEAD > output &&
428 git diff --exit-code expect output
429 '
430
431 test_done