blob: 2add26d76844deb04aee4568bc467f66df965d57 [file] [log] [blame]
Carlos Rica359048d2007-09-11 03:09:52 +02001#!/bin/sh
2#
3# Copyright (c) 2007 Carlos Rica
4#
5
Nanako Shiraishid592b312008-09-03 17:59:31 +09006test_description='git reset
Carlos Rica359048d2007-09-11 03:09:52 +02007
Nanako Shiraishid592b312008-09-03 17:59:31 +09008Documented tests for git reset'
Carlos Rica359048d2007-09-11 03:09:52 +02009
Johannes Schindelin01dc8132020-11-18 23:44:39 +000010GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +000011export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12
Patrick Steinhardt49eb5972024-05-27 13:46:44 +020013TEST_PASSES_SANITIZE_LEAK=true
Carlos Rica359048d2007-09-11 03:09:52 +020014. ./test-lib.sh
15
Alexey Shumkinde6029a2013-06-26 14:19:49 +040016commit_msg () {
Alexey Shumkin17cc2ef2013-07-05 16:01:48 +040017 # String "modify 2nd file (changed)" partly in German
18 # (translated with Google Translate),
Alexey Shumkinde6029a2013-06-26 14:19:49 +040019 # encoded in UTF-8, used as a commit log message below.
Alexey Shumkin17cc2ef2013-07-05 16:01:48 +040020 msg="modify 2nd file (ge\303\244ndert)\n"
Alexey Shumkinde6029a2013-06-26 14:19:49 +040021 if test -n "$1"
22 then
Alexey Shumkin17cc2ef2013-07-05 16:01:48 +040023 printf "$msg" | iconv -f utf-8 -t "$1"
24 else
25 printf "$msg"
Alexey Shumkinde6029a2013-06-26 14:19:49 +040026 fi
Alexey Shumkinde6029a2013-06-26 14:19:49 +040027}
28
Alexey Shumkinee3efaf2014-05-21 17:20:04 +040029# Tested non-UTF-8 encoding
30test_encoding="ISO8859-1"
31
Carlos Rica359048d2007-09-11 03:09:52 +020032test_expect_success 'creating initial files and commits' '
33 test_tick &&
34 echo "1st file" >first &&
35 git add first &&
36 git commit -m "create 1st file" &&
37
38 echo "2nd file" >second &&
39 git add second &&
40 git commit -m "create 2nd file" &&
41
42 echo "2nd line 1st file" >>first &&
43 git commit -a -m "modify 1st file" &&
brian m. carlsond62607d2020-07-29 23:14:02 +000044 head5p2=$(git rev-parse --verify HEAD) &&
45 head5p2f=$(git rev-parse --short HEAD:first) &&
Carlos Rica359048d2007-09-11 03:09:52 +020046
47 git rm first &&
48 git mv second secondfile &&
49 git commit -a -m "remove 1st and rename 2nd" &&
brian m. carlsond62607d2020-07-29 23:14:02 +000050 head5p1=$(git rev-parse --verify HEAD) &&
51 head5p1s=$(git rev-parse --short HEAD:secondfile) &&
Carlos Rica359048d2007-09-11 03:09:52 +020052
53 echo "1st line 2nd file" >secondfile &&
54 echo "2nd line 2nd file" >>secondfile &&
Pat Thoytse6ce2be2013-09-02 15:44:54 +010055 # "git commit -m" would break MinGW, as Windows refuse to pass
56 # $test_encoding encoded parameter to git.
57 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
brian m. carlsond62607d2020-07-29 23:14:02 +000058 head5=$(git rev-parse --verify HEAD) &&
59 head5s=$(git rev-parse --short HEAD:secondfile) &&
60 head5sl=$(git rev-parse HEAD:secondfile)
Carlos Rica359048d2007-09-11 03:09:52 +020061'
62# git log --pretty=oneline # to see those SHA1 involved
63
64check_changes () {
65 test "$(git rev-parse HEAD)" = "$1" &&
Junio C Hamano3af82862008-05-23 22:28:56 -070066 git diff | test_cmp .diff_expect - &&
67 git diff --cached | test_cmp .cached_expect - &&
Carlos Rica359048d2007-09-11 03:09:52 +020068 for FILE in *
69 do
70 echo $FILE':'
71 cat $FILE || return
Junio C Hamano3af82862008-05-23 22:28:56 -070072 done | test_cmp .cat_expect -
Carlos Rica359048d2007-09-11 03:09:52 +020073}
74
Junio C Hamano3821eb62023-07-19 06:37:39 -070075# no negated form for various type of resets
76for opt in soft mixed hard merge keep
77do
78 test_expect_success "no 'git reset --no-$opt'" '
79 test_when_finished "rm -f err" &&
80 test_must_fail git reset --no-$opt 2>err &&
81 grep "error: unknown option .no-$opt." err
82 '
83done
84
Alexey Shumkinecaee802013-06-26 14:19:50 +040085test_expect_success 'reset --hard message' '
Alexey Shumkinde6029a2013-06-26 14:19:49 +040086 hex=$(git log -1 --format="%h") &&
Charvi Mendirattac3277622020-10-20 17:13:17 +053087 git reset --hard >.actual &&
88 echo HEAD is now at $hex $(commit_msg) >.expected &&
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +010089 test_cmp .expected .actual
Alexey Shumkinde6029a2013-06-26 14:19:49 +040090'
91
Alexey Shumkinee3efaf2014-05-21 17:20:04 +040092test_expect_success 'reset --hard message (ISO8859-1 logoutputencoding)' '
Alexey Shumkinde6029a2013-06-26 14:19:49 +040093 hex=$(git log -1 --format="%h") &&
Charvi Mendirattac3277622020-10-20 17:13:17 +053094 git -c "i18n.logOutputEncoding=$test_encoding" reset --hard >.actual &&
95 echo HEAD is now at $hex $(commit_msg $test_encoding) >.expected &&
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +010096 test_cmp .expected .actual
Alexey Shumkinde6029a2013-06-26 14:19:49 +040097'
98
Carlos Rica359048d2007-09-11 03:09:52 +020099test_expect_success 'giving a non existing revision should fail' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700100 >.diff_expect &&
101 >.cached_expect &&
102 cat >.cat_expect <<-\EOF &&
103 secondfile:
104 1st line 2nd file
105 2nd line 2nd file
106 EOF
107
Stephan Beyerd492b312008-07-12 17:47:52 +0200108 test_must_fail git reset aaaaaa &&
109 test_must_fail git reset --mixed aaaaaa &&
110 test_must_fail git reset --soft aaaaaa &&
111 test_must_fail git reset --hard aaaaaa &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400112 check_changes $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200113'
114
Johannes Schindelincdf4a752007-11-03 14:33:01 +0000115test_expect_success 'reset --soft with unmerged index should fail' '
116 touch .git/MERGE_HEAD &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000117 echo "100644 $head5sl 1 un" |
Johannes Schindelincdf4a752007-11-03 14:33:01 +0000118 git update-index --index-info &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200119 test_must_fail git reset --soft HEAD &&
Johannes Schindelincdf4a752007-11-03 14:33:01 +0000120 rm .git/MERGE_HEAD &&
121 git rm --cached -- un
122'
123
Charvi Mendirattae166fe32020-10-20 17:13:15 +0530124test_expect_success 'giving paths with options different than --mixed should fail' '
Stephan Beyerd492b312008-07-12 17:47:52 +0200125 test_must_fail git reset --soft -- first &&
126 test_must_fail git reset --hard -- first &&
127 test_must_fail git reset --soft HEAD^ -- first &&
128 test_must_fail git reset --hard HEAD^ -- first &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400129 check_changes $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200130'
131
132test_expect_success 'giving unrecognized options should fail' '
Stephan Beyerd492b312008-07-12 17:47:52 +0200133 test_must_fail git reset --other &&
134 test_must_fail git reset -o &&
135 test_must_fail git reset --mixed --other &&
136 test_must_fail git reset --mixed -o &&
137 test_must_fail git reset --soft --other &&
138 test_must_fail git reset --soft -o &&
139 test_must_fail git reset --hard --other &&
140 test_must_fail git reset --hard -o &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400141 check_changes $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200142'
143
Charvi Mendirattae166fe32020-10-20 17:13:15 +0530144test_expect_success 'trying to do reset --soft with pending merge should fail' '
Carlos Rica359048d2007-09-11 03:09:52 +0200145 git branch branch1 &&
146 git branch branch2 &&
147
148 git checkout branch1 &&
149 echo "3rd line in branch1" >>secondfile &&
150 git commit -a -m "change in branch1" &&
151
152 git checkout branch2 &&
153 echo "3rd line in branch2" >>secondfile &&
154 git commit -a -m "change in branch2" &&
155
Stephan Beyerd492b312008-07-12 17:47:52 +0200156 test_must_fail git merge branch1 &&
157 test_must_fail git reset --soft &&
Carlos Rica359048d2007-09-11 03:09:52 +0200158
159 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
160 git commit -a -m "the change in branch2" &&
161
Johannes Schindelin01dc8132020-11-18 23:44:39 +0000162 git checkout main &&
Carlos Rica359048d2007-09-11 03:09:52 +0200163 git branch -D branch1 branch2 &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400164 check_changes $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200165'
166
Charvi Mendirattae166fe32020-10-20 17:13:15 +0530167test_expect_success 'trying to do reset --soft with pending checkout merge should fail' '
Carlos Rica359048d2007-09-11 03:09:52 +0200168 git branch branch3 &&
169 git branch branch4 &&
170
171 git checkout branch3 &&
172 echo "3rd line in branch3" >>secondfile &&
173 git commit -a -m "line in branch3" &&
174
175 git checkout branch4 &&
176 echo "3rd line in branch4" >>secondfile &&
177
178 git checkout -m branch3 &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200179 test_must_fail git reset --soft &&
Carlos Rica359048d2007-09-11 03:09:52 +0200180
181 printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
182 git commit -a -m "the line in branch3" &&
183
Johannes Schindelin01dc8132020-11-18 23:44:39 +0000184 git checkout main &&
Carlos Rica359048d2007-09-11 03:09:52 +0200185 git branch -D branch3 branch4 &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400186 check_changes $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200187'
188
Charvi Mendirattae166fe32020-10-20 17:13:15 +0530189test_expect_success 'resetting to HEAD with no changes should succeed and do nothing' '
Carlos Rica359048d2007-09-11 03:09:52 +0200190 git reset --hard &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400191 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200192 git reset --hard HEAD &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400193 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200194 git reset --soft &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400195 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200196 git reset --soft HEAD &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400197 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200198 git reset --mixed &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400199 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200200 git reset --mixed HEAD &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400201 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200202 git reset &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400203 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200204 git reset HEAD &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400205 check_changes $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200206'
207
Carlos Rica359048d2007-09-11 03:09:52 +0200208test_expect_success '--soft reset only should show changes in diff --cached' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700209 >.diff_expect &&
210 cat >.cached_expect <<-EOF &&
211 diff --git a/secondfile b/secondfile
212 index $head5p1s..$head5s 100644
213 --- a/secondfile
214 +++ b/secondfile
215 @@ -1 +1,2 @@
216 -2nd file
217 +1st line 2nd file
218 +2nd line 2nd file
219 EOF
220 cat >.cat_expect <<-\EOF &&
221 secondfile:
222 1st line 2nd file
223 2nd line 2nd file
224 EOF
Carlos Rica359048d2007-09-11 03:09:52 +0200225 git reset --soft HEAD^ &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000226 check_changes $head5p1 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200227 test "$(git rev-parse ORIG_HEAD)" = \
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400228 $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200229'
230
Charvi Mendirattae166fe32020-10-20 17:13:15 +0530231test_expect_success 'changing files and redo the last commit should succeed' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700232 >.diff_expect &&
233 >.cached_expect &&
234 cat >.cat_expect <<-\EOF &&
235 secondfile:
236 1st line 2nd file
237 2nd line 2nd file
238 3rd line 2nd file
239 EOF
Carlos Rica359048d2007-09-11 03:09:52 +0200240 echo "3rd line 2nd file" >>secondfile &&
241 git commit -a -C ORIG_HEAD &&
Alexey Shumkin375775b2013-06-26 14:19:47 +0400242 head4=$(git rev-parse --verify HEAD) &&
243 check_changes $head4 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200244 test "$(git rev-parse ORIG_HEAD)" = \
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400245 $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200246'
247
Charvi Mendirattae166fe32020-10-20 17:13:15 +0530248test_expect_success '--hard reset should change the files and undo commits permanently' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700249 >.diff_expect &&
250 >.cached_expect &&
251 cat >.cat_expect <<-\EOF &&
252 first:
253 1st file
254 2nd line 1st file
255 second:
256 2nd file
257 EOF
Carlos Rica359048d2007-09-11 03:09:52 +0200258 git reset --hard HEAD~2 &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000259 check_changes $head5p2 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200260 test "$(git rev-parse ORIG_HEAD)" = \
Alexey Shumkin375775b2013-06-26 14:19:47 +0400261 $head4
Carlos Rica359048d2007-09-11 03:09:52 +0200262'
263
Charvi Mendirattae166fe32020-10-20 17:13:15 +0530264test_expect_success 'redoing changes adding them without commit them should succeed' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700265 >.diff_expect &&
266 cat >.cached_expect <<-EOF &&
267 diff --git a/first b/first
268 deleted file mode 100644
269 index $head5p2f..0000000
270 --- a/first
271 +++ /dev/null
272 @@ -1,2 +0,0 @@
273 -1st file
274 -2nd line 1st file
275 diff --git a/second b/second
276 deleted file mode 100644
277 index $head5p1s..0000000
278 --- a/second
279 +++ /dev/null
280 @@ -1 +0,0 @@
281 -2nd file
282 diff --git a/secondfile b/secondfile
283 new file mode 100644
284 index 0000000..$head5s
285 --- /dev/null
286 +++ b/secondfile
287 @@ -0,0 +1,2 @@
288 +1st line 2nd file
289 +2nd line 2nd file
290 EOF
291 cat >.cat_expect <<-\EOF &&
292 secondfile:
293 1st line 2nd file
294 2nd line 2nd file
295 EOF
Carlos Rica359048d2007-09-11 03:09:52 +0200296 git rm first &&
297 git mv second secondfile &&
298
299 echo "1st line 2nd file" >secondfile &&
300 echo "2nd line 2nd file" >>secondfile &&
301 git add secondfile &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000302 check_changes $head5p2
Carlos Rica359048d2007-09-11 03:09:52 +0200303'
304
Carlos Rica359048d2007-09-11 03:09:52 +0200305test_expect_success '--mixed reset to HEAD should unadd the files' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700306 cat >.diff_expect <<-EOF &&
307 diff --git a/first b/first
308 deleted file mode 100644
309 index $head5p2f..0000000
310 --- a/first
311 +++ /dev/null
312 @@ -1,2 +0,0 @@
313 -1st file
314 -2nd line 1st file
315 diff --git a/second b/second
316 deleted file mode 100644
317 index $head5p1s..0000000
318 --- a/second
319 +++ /dev/null
320 @@ -1 +0,0 @@
321 -2nd file
322 EOF
323 >.cached_expect &&
324 cat >.cat_expect <<-\EOF &&
325 secondfile:
326 1st line 2nd file
327 2nd line 2nd file
328 EOF
Carlos Rica359048d2007-09-11 03:09:52 +0200329 git reset &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000330 check_changes $head5p2 &&
331 test "$(git rev-parse ORIG_HEAD)" = $head5p2
Carlos Rica359048d2007-09-11 03:09:52 +0200332'
333
Carlos Rica359048d2007-09-11 03:09:52 +0200334test_expect_success 'redoing the last two commits should succeed' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700335 >.diff_expect &&
336 >.cached_expect &&
337 cat >.cat_expect <<-\EOF &&
338 secondfile:
339 1st line 2nd file
340 2nd line 2nd file
341 EOF
Carlos Rica359048d2007-09-11 03:09:52 +0200342 git add secondfile &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000343 git reset --hard $head5p2 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200344 git rm first &&
345 git mv second secondfile &&
346 git commit -a -m "remove 1st and rename 2nd" &&
347
348 echo "1st line 2nd file" >secondfile &&
349 echo "2nd line 2nd file" >>secondfile &&
Pat Thoytse6ce2be2013-09-02 15:44:54 +0100350 # "git commit -m" would break MinGW, as Windows refuse to pass
351 # $test_encoding encoded parameter to git.
352 commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400353 check_changes $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200354'
355
Carlos Rica359048d2007-09-11 03:09:52 +0200356test_expect_success '--hard reset to HEAD should clear a failed merge' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700357 >.diff_expect &&
358 >.cached_expect &&
359 cat >.cat_expect <<-\EOF &&
360 secondfile:
361 1st line 2nd file
362 2nd line 2nd file
363 3rd line in branch2
364 EOF
Carlos Rica359048d2007-09-11 03:09:52 +0200365 git branch branch1 &&
366 git branch branch2 &&
367
368 git checkout branch1 &&
369 echo "3rd line in branch1" >>secondfile &&
370 git commit -a -m "change in branch1" &&
371
372 git checkout branch2 &&
373 echo "3rd line in branch2" >>secondfile &&
374 git commit -a -m "change in branch2" &&
Alexey Shumkin375775b2013-06-26 14:19:47 +0400375 head3=$(git rev-parse --verify HEAD) &&
Carlos Rica359048d2007-09-11 03:09:52 +0200376
Stephan Beyerd492b312008-07-12 17:47:52 +0200377 test_must_fail git pull . branch1 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200378 git reset --hard &&
Alexey Shumkin375775b2013-06-26 14:19:47 +0400379 check_changes $head3
Carlos Rica359048d2007-09-11 03:09:52 +0200380'
381
Charvi Mendirattae166fe32020-10-20 17:13:15 +0530382test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
Junio C Hamano31f4c832020-10-21 22:55:58 -0700383 >.diff_expect &&
384 >.cached_expect &&
385 cat >.cat_expect <<-\EOF &&
386 secondfile:
387 1st line 2nd file
388 2nd line 2nd file
389 EOF
Carlos Rica359048d2007-09-11 03:09:52 +0200390 git reset --hard HEAD^ &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400391 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200392
393 git pull . branch1 &&
394 git reset --hard ORIG_HEAD &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400395 check_changes $head5 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200396
Johannes Schindelin01dc8132020-11-18 23:44:39 +0000397 git checkout main &&
Carlos Rica359048d2007-09-11 03:09:52 +0200398 git branch -D branch1 branch2 &&
Alexey Shumkin8b66f782013-01-24 13:10:26 +0400399 check_changes $head5
Carlos Rica359048d2007-09-11 03:09:52 +0200400'
401
Carlos Rica359048d2007-09-11 03:09:52 +0200402test_expect_success 'test --mixed <paths>' '
Charvi Mendirattac3277622020-10-20 17:13:17 +0530403 echo 1 >file1 &&
404 echo 2 >file2 &&
Carlos Rica359048d2007-09-11 03:09:52 +0200405 git add file1 file2 &&
406 test_tick &&
407 git commit -m files &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000408 before1=$(git rev-parse --short HEAD:file1) &&
409 before2=$(git rev-parse --short HEAD:file2) &&
Carlos Rica359048d2007-09-11 03:09:52 +0200410 git rm file2 &&
Charvi Mendirattac3277622020-10-20 17:13:17 +0530411 echo 3 >file3 &&
412 echo 4 >file4 &&
413 echo 5 >file1 &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000414 after1=$(git rev-parse --short $(git hash-object file1)) &&
415 after4=$(git rev-parse --short $(git hash-object file4)) &&
Carlos Rica359048d2007-09-11 03:09:52 +0200416 git add file1 file3 file4 &&
Martin von Zweigbergkd94c5e22013-01-14 21:47:34 -0800417 git reset HEAD -- file1 file2 file3 &&
418 test_must_fail git diff --quiet &&
Charvi Mendirattac3277622020-10-20 17:13:17 +0530419 git diff >output &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000420
Charvi Mendirattac3277622020-10-20 17:13:17 +0530421 cat >expect <<-EOF &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000422 diff --git a/file1 b/file1
423 index $before1..$after1 100644
424 --- a/file1
425 +++ b/file1
426 @@ -1 +1 @@
427 -1
428 +5
429 diff --git a/file2 b/file2
430 deleted file mode 100644
431 index $before2..0000000
432 --- a/file2
433 +++ /dev/null
434 @@ -1 +0,0 @@
435 -2
436 EOF
437
Stefan Beller9c5b2fa2017-10-06 12:00:06 -0700438 test_cmp expect output &&
Charvi Mendirattac3277622020-10-20 17:13:17 +0530439 git diff --cached >output &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000440
Charvi Mendirattac3277622020-10-20 17:13:17 +0530441 cat >cached_expect <<-EOF &&
brian m. carlsond62607d2020-07-29 23:14:02 +0000442 diff --git a/file4 b/file4
443 new file mode 100644
444 index 0000000..$after4
445 --- /dev/null
446 +++ b/file4
447 @@ -0,0 +1 @@
448 +4
449 EOF
450
Stefan Beller9c5b2fa2017-10-06 12:00:06 -0700451 test_cmp cached_expect output
Carlos Rica359048d2007-09-11 03:09:52 +0200452'
453
Junio C Hamanocbb390c2007-09-13 20:54:14 -0700454test_expect_success 'test resetting the index at give paths' '
Junio C Hamanocbb390c2007-09-13 20:54:14 -0700455 mkdir sub &&
456 >sub/file1 &&
457 >sub/file2 &&
458 git update-index --add sub/file1 sub/file2 &&
459 T=$(git write-tree) &&
Martin von Zweigbergkd94c5e22013-01-14 21:47:34 -0800460 git reset HEAD sub/file2 &&
461 test_must_fail git diff --quiet &&
Junio C Hamanocbb390c2007-09-13 20:54:14 -0700462 U=$(git write-tree) &&
463 echo "$T" &&
464 echo "$U" &&
Stephan Beyerd492b312008-07-12 17:47:52 +0200465 test_must_fail git diff-index --cached --exit-code "$T" &&
Junio C Hamanocbb390c2007-09-13 20:54:14 -0700466 test "$T" != "$U"
Junio C Hamanocbb390c2007-09-13 20:54:14 -0700467'
468
Johannes Schindelin2e7a9782007-11-03 13:12:17 +0000469test_expect_success 'resetting an unmodified path is a no-op' '
470 git reset --hard &&
471 git reset -- file1 &&
472 git diff-files --exit-code &&
473 git diff-index --cached --exit-code HEAD
474'
475
Victoria Dyefd56fba2022-03-15 01:49:39 +0000476test_reset_refreshes_index () {
477
478 # To test whether the index is refreshed in `git reset --mixed` with
479 # the given options, create a scenario where we clearly see different
480 # results depending on whether the refresh occurred or not.
481
482 # Step 0: start with a clean index
483 git reset --hard HEAD &&
484
485 # Step 1: remove file2, but only in the index (no change to worktree)
486 git rm --cached file2 &&
487
488 # Step 2: reset index & leave worktree unchanged from HEAD
489 git $1 reset $2 --mixed HEAD &&
490
491 # Step 3: verify whether the index is refreshed by checking whether
492 # file2 still has staged changes in the index differing from HEAD (if
493 # the refresh occurred, there should be no such changes)
494 git diff-files >output.log &&
495 test_must_be_empty output.log
496}
497
Junio C Hamano476cca62011-04-12 16:36:18 -0700498test_expect_success '--mixed refreshes the index' '
Victoria Dye45bf7622022-03-23 18:17:58 +0000499 # Verify default behavior (without --[no-]refresh or reset.refresh)
500 test_reset_refreshes_index &&
Victoria Dyefd56fba2022-03-15 01:49:39 +0000501
Victoria Dye2efc9b82022-03-23 18:17:59 +0000502 # With --quiet
Victoria Dye45bf7622022-03-23 18:17:58 +0000503 test_reset_refreshes_index "" --quiet
Victoria Dyefd56fba2022-03-15 01:49:39 +0000504'
505
506test_expect_success '--mixed --[no-]refresh sets refresh behavior' '
Victoria Dye7cff6762022-03-23 18:18:00 +0000507 # Verify that --[no-]refresh controls index refresh
Victoria Dyefd56fba2022-03-15 01:49:39 +0000508 test_reset_refreshes_index "" --refresh &&
Victoria Dye7cff6762022-03-23 18:18:00 +0000509 ! test_reset_refreshes_index "" --no-refresh
Victoria Dyefd56fba2022-03-15 01:49:39 +0000510'
511
Victoria Dye71471b22021-10-27 14:39:17 +0000512test_expect_success '--mixed preserves skip-worktree' '
513 echo 123 >>file2 &&
514 git add file2 &&
515 git update-index --skip-worktree file2 &&
516 git reset --mixed HEAD >output &&
517 test_must_be_empty output &&
518
519 cat >expect <<-\EOF &&
520 Unstaged changes after reset:
521 M file2
522 EOF
523 git update-index --no-skip-worktree file2 &&
524 git add file2 &&
525 git reset --mixed HEAD >output &&
526 test_cmp expect output
527'
528
Junio C Hamanoff00b682011-07-13 21:36:29 -0700529test_expect_success 'resetting specific path that is unmerged' '
530 git rm --cached file2 &&
531 F1=$(git rev-parse HEAD:file1) &&
532 F2=$(git rev-parse HEAD:file2) &&
533 F3=$(git rev-parse HEAD:secondfile) &&
534 {
535 echo "100644 $F1 1 file2" &&
536 echo "100644 $F2 2 file2" &&
537 echo "100644 $F3 3 file2"
538 } | git update-index --index-info &&
539 git ls-files -u &&
Martin von Zweigbergkd94c5e22013-01-14 21:47:34 -0800540 git reset HEAD file2 &&
541 test_must_fail git diff --quiet &&
Junio C Hamanoff00b682011-07-13 21:36:29 -0700542 git diff-index --exit-code --cached HEAD
543'
544
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700545test_expect_success 'disambiguation (1)' '
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700546 git reset --hard &&
547 >secondfile &&
548 git add secondfile &&
Martin von Zweigbergkd94c5e22013-01-14 21:47:34 -0800549 git reset secondfile &&
550 test_must_fail git diff --quiet -- secondfile &&
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700551 test -z "$(git diff --cached --name-only)" &&
552 test -f secondfile &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700553 test_must_be_empty secondfile
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700554'
555
556test_expect_success 'disambiguation (2)' '
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700557 git reset --hard &&
558 >secondfile &&
559 git add secondfile &&
560 rm -f secondfile &&
561 test_must_fail git reset secondfile &&
562 test -n "$(git diff --cached --name-only -- secondfile)" &&
563 test ! -f secondfile
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700564'
565
566test_expect_success 'disambiguation (3)' '
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700567 git reset --hard &&
568 >secondfile &&
569 git add secondfile &&
570 rm -f secondfile &&
Martin von Zweigbergkd94c5e22013-01-14 21:47:34 -0800571 git reset HEAD secondfile &&
572 test_must_fail git diff --quiet &&
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700573 test -z "$(git diff --cached --name-only)" &&
574 test ! -f secondfile
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700575'
576
577test_expect_success 'disambiguation (4)' '
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700578 git reset --hard &&
579 >secondfile &&
580 git add secondfile &&
581 rm -f secondfile &&
Martin von Zweigbergkd94c5e22013-01-14 21:47:34 -0800582 git reset -- secondfile &&
583 test_must_fail git diff --quiet &&
Junio C Hamanodfc8f392008-06-25 18:16:36 -0700584 test -z "$(git diff --cached --name-only)" &&
585 test ! -f secondfile
586'
587
Martin von Zweigbergk2f328c32013-01-14 21:47:49 -0800588test_expect_success 'reset with paths accepts tree' '
589 # for simpler tests, drop last commit containing added files
590 git reset --hard HEAD^ &&
591 git reset HEAD^^{tree} -- . &&
592 git diff --cached HEAD^ --exit-code &&
593 git diff HEAD --exit-code
594'
595
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700596test_expect_success 'reset -N keeps removed files as intent-to-add' '
597 echo new-file >new-file &&
598 git add new-file &&
599 git reset -N HEAD &&
600
601 tree=$(git write-tree) &&
602 git ls-tree $tree new-file >actual &&
Ævar Arnfjörð Bjarmasond3c67512018-07-27 17:48:11 +0000603 test_must_be_empty actual &&
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700604
605 git diff --name-only >actual &&
606 echo new-file >expect &&
607 test_cmp expect actual
608'
609
Nguyễn Thái Ngọc Duyb7756d42014-02-16 09:28:03 +0700610test_expect_success 'reset --mixed sets up work tree' '
611 git init mixed_worktree &&
612 (
613 cd mixed_worktree &&
614 test_commit dummy
615 ) &&
Nguyễn Thái Ngọc Duyb7756d42014-02-16 09:28:03 +0700616 git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
Ævar Arnfjörð Bjarmasond3c67512018-07-27 17:48:11 +0000617 test_must_be_empty actual
Nguyễn Thái Ngọc Duyb7756d42014-02-16 09:28:03 +0700618'
619
Jeff King93851742023-12-06 17:21:45 -0500620test_expect_success 'reset handles --end-of-options' '
621 git update-ref refs/heads/--foo HEAD^ &&
622 git log -1 --format=%s refs/heads/--foo >expect &&
623 git reset --hard --end-of-options --foo &&
624 git log -1 --format=%s HEAD >actual &&
625 test_cmp expect actual
626'
627
Carlos Rica359048d2007-09-11 03:09:52 +0200628test_done