Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2007 Carlos Rica |
| 4 | # |
| 5 | |
Nanako Shiraishi | d592b31 | 2008-09-03 17:59:31 +0900 | [diff] [blame] | 6 | test_description='git reset |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 7 | |
Nanako Shiraishi | d592b31 | 2008-09-03 17:59:31 +0900 | [diff] [blame] | 8 | Documented tests for git reset' |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 9 | |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 10 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 11 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 12 | |
Patrick Steinhardt | 49eb597 | 2024-05-27 13:46:44 +0200 | [diff] [blame] | 13 | TEST_PASSES_SANITIZE_LEAK=true |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 14 | . ./test-lib.sh |
| 15 | |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 16 | commit_msg () { |
Alexey Shumkin | 17cc2ef | 2013-07-05 16:01:48 +0400 | [diff] [blame] | 17 | # String "modify 2nd file (changed)" partly in German |
| 18 | # (translated with Google Translate), |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 19 | # encoded in UTF-8, used as a commit log message below. |
Alexey Shumkin | 17cc2ef | 2013-07-05 16:01:48 +0400 | [diff] [blame] | 20 | msg="modify 2nd file (ge\303\244ndert)\n" |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 21 | if test -n "$1" |
| 22 | then |
Alexey Shumkin | 17cc2ef | 2013-07-05 16:01:48 +0400 | [diff] [blame] | 23 | printf "$msg" | iconv -f utf-8 -t "$1" |
| 24 | else |
| 25 | printf "$msg" |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 26 | fi |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 27 | } |
| 28 | |
Alexey Shumkin | ee3efaf | 2014-05-21 17:20:04 +0400 | [diff] [blame] | 29 | # Tested non-UTF-8 encoding |
| 30 | test_encoding="ISO8859-1" |
| 31 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 32 | test_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. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 44 | head5p2=$(git rev-parse --verify HEAD) && |
| 45 | head5p2f=$(git rev-parse --short HEAD:first) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 46 | |
| 47 | git rm first && |
| 48 | git mv second secondfile && |
| 49 | git commit -a -m "remove 1st and rename 2nd" && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 50 | head5p1=$(git rev-parse --verify HEAD) && |
| 51 | head5p1s=$(git rev-parse --short HEAD:secondfile) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 52 | |
| 53 | echo "1st line 2nd file" >secondfile && |
| 54 | echo "2nd line 2nd file" >>secondfile && |
Pat Thoyts | e6ce2be | 2013-09-02 15:44:54 +0100 | [diff] [blame] | 55 | # "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. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 58 | head5=$(git rev-parse --verify HEAD) && |
| 59 | head5s=$(git rev-parse --short HEAD:secondfile) && |
| 60 | head5sl=$(git rev-parse HEAD:secondfile) |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 61 | ' |
| 62 | # git log --pretty=oneline # to see those SHA1 involved |
| 63 | |
| 64 | check_changes () { |
| 65 | test "$(git rev-parse HEAD)" = "$1" && |
Junio C Hamano | 3af8286 | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 66 | git diff | test_cmp .diff_expect - && |
| 67 | git diff --cached | test_cmp .cached_expect - && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 68 | for FILE in * |
| 69 | do |
| 70 | echo $FILE':' |
| 71 | cat $FILE || return |
Junio C Hamano | 3af8286 | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 72 | done | test_cmp .cat_expect - |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Junio C Hamano | 3821eb6 | 2023-07-19 06:37:39 -0700 | [diff] [blame] | 75 | # no negated form for various type of resets |
| 76 | for opt in soft mixed hard merge keep |
| 77 | do |
| 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 | ' |
| 83 | done |
| 84 | |
Alexey Shumkin | ecaee80 | 2013-06-26 14:19:50 +0400 | [diff] [blame] | 85 | test_expect_success 'reset --hard message' ' |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 86 | hex=$(git log -1 --format="%h") && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 87 | git reset --hard >.actual && |
| 88 | echo HEAD is now at $hex $(commit_msg) >.expected && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 89 | test_cmp .expected .actual |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 90 | ' |
| 91 | |
Alexey Shumkin | ee3efaf | 2014-05-21 17:20:04 +0400 | [diff] [blame] | 92 | test_expect_success 'reset --hard message (ISO8859-1 logoutputencoding)' ' |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 93 | hex=$(git log -1 --format="%h") && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 94 | git -c "i18n.logOutputEncoding=$test_encoding" reset --hard >.actual && |
| 95 | echo HEAD is now at $hex $(commit_msg $test_encoding) >.expected && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 96 | test_cmp .expected .actual |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 97 | ' |
| 98 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 99 | test_expect_success 'giving a non existing revision should fail' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 100 | >.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 Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 108 | 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 Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 112 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 113 | ' |
| 114 | |
Johannes Schindelin | cdf4a75 | 2007-11-03 14:33:01 +0000 | [diff] [blame] | 115 | test_expect_success 'reset --soft with unmerged index should fail' ' |
| 116 | touch .git/MERGE_HEAD && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 117 | echo "100644 $head5sl 1 un" | |
Johannes Schindelin | cdf4a75 | 2007-11-03 14:33:01 +0000 | [diff] [blame] | 118 | git update-index --index-info && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 119 | test_must_fail git reset --soft HEAD && |
Johannes Schindelin | cdf4a75 | 2007-11-03 14:33:01 +0000 | [diff] [blame] | 120 | rm .git/MERGE_HEAD && |
| 121 | git rm --cached -- un |
| 122 | ' |
| 123 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 124 | test_expect_success 'giving paths with options different than --mixed should fail' ' |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 125 | 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 Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 129 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 130 | ' |
| 131 | |
| 132 | test_expect_success 'giving unrecognized options should fail' ' |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 133 | 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 Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 141 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 142 | ' |
| 143 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 144 | test_expect_success 'trying to do reset --soft with pending merge should fail' ' |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 145 | 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 Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 156 | test_must_fail git merge branch1 && |
| 157 | test_must_fail git reset --soft && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 158 | |
| 159 | printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile && |
| 160 | git commit -a -m "the change in branch2" && |
| 161 | |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 162 | git checkout main && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 163 | git branch -D branch1 branch2 && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 164 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 165 | ' |
| 166 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 167 | test_expect_success 'trying to do reset --soft with pending checkout merge should fail' ' |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 168 | 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 Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 179 | test_must_fail git reset --soft && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 180 | |
| 181 | printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile && |
| 182 | git commit -a -m "the line in branch3" && |
| 183 | |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 184 | git checkout main && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 185 | git branch -D branch3 branch4 && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 186 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 187 | ' |
| 188 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 189 | test_expect_success 'resetting to HEAD with no changes should succeed and do nothing' ' |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 190 | git reset --hard && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 191 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 192 | git reset --hard HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 193 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 194 | git reset --soft && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 195 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 196 | git reset --soft HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 197 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 198 | git reset --mixed && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 199 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 200 | git reset --mixed HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 201 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 202 | git reset && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 203 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 204 | git reset HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 205 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 206 | ' |
| 207 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 208 | test_expect_success '--soft reset only should show changes in diff --cached' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 209 | >.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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 225 | git reset --soft HEAD^ && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 226 | check_changes $head5p1 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 227 | test "$(git rev-parse ORIG_HEAD)" = \ |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 228 | $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 229 | ' |
| 230 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 231 | test_expect_success 'changing files and redo the last commit should succeed' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 232 | >.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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 240 | echo "3rd line 2nd file" >>secondfile && |
| 241 | git commit -a -C ORIG_HEAD && |
Alexey Shumkin | 375775b | 2013-06-26 14:19:47 +0400 | [diff] [blame] | 242 | head4=$(git rev-parse --verify HEAD) && |
| 243 | check_changes $head4 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 244 | test "$(git rev-parse ORIG_HEAD)" = \ |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 245 | $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 246 | ' |
| 247 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 248 | test_expect_success '--hard reset should change the files and undo commits permanently' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 249 | >.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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 258 | git reset --hard HEAD~2 && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 259 | check_changes $head5p2 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 260 | test "$(git rev-parse ORIG_HEAD)" = \ |
Alexey Shumkin | 375775b | 2013-06-26 14:19:47 +0400 | [diff] [blame] | 261 | $head4 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 262 | ' |
| 263 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 264 | test_expect_success 'redoing changes adding them without commit them should succeed' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 265 | >.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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 296 | 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. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 302 | check_changes $head5p2 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 303 | ' |
| 304 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 305 | test_expect_success '--mixed reset to HEAD should unadd the files' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 306 | 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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 329 | git reset && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 330 | check_changes $head5p2 && |
| 331 | test "$(git rev-parse ORIG_HEAD)" = $head5p2 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 332 | ' |
| 333 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 334 | test_expect_success 'redoing the last two commits should succeed' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 335 | >.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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 342 | git add secondfile && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 343 | git reset --hard $head5p2 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 344 | 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 Thoyts | e6ce2be | 2013-09-02 15:44:54 +0100 | [diff] [blame] | 350 | # "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 Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 353 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 354 | ' |
| 355 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 356 | test_expect_success '--hard reset to HEAD should clear a failed merge' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 357 | >.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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 365 | 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 Shumkin | 375775b | 2013-06-26 14:19:47 +0400 | [diff] [blame] | 375 | head3=$(git rev-parse --verify HEAD) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 376 | |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 377 | test_must_fail git pull . branch1 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 378 | git reset --hard && |
Alexey Shumkin | 375775b | 2013-06-26 14:19:47 +0400 | [diff] [blame] | 379 | check_changes $head3 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 380 | ' |
| 381 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 382 | test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 383 | >.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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 390 | git reset --hard HEAD^ && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 391 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 392 | |
| 393 | git pull . branch1 && |
| 394 | git reset --hard ORIG_HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 395 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 396 | |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 397 | git checkout main && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 398 | git branch -D branch1 branch2 && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 399 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 400 | ' |
| 401 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 402 | test_expect_success 'test --mixed <paths>' ' |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 403 | echo 1 >file1 && |
| 404 | echo 2 >file2 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 405 | git add file1 file2 && |
| 406 | test_tick && |
| 407 | git commit -m files && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 408 | before1=$(git rev-parse --short HEAD:file1) && |
| 409 | before2=$(git rev-parse --short HEAD:file2) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 410 | git rm file2 && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 411 | echo 3 >file3 && |
| 412 | echo 4 >file4 && |
| 413 | echo 5 >file1 && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 414 | after1=$(git rev-parse --short $(git hash-object file1)) && |
| 415 | after4=$(git rev-parse --short $(git hash-object file4)) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 416 | git add file1 file3 file4 && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 417 | git reset HEAD -- file1 file2 file3 && |
| 418 | test_must_fail git diff --quiet && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 419 | git diff >output && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 420 | |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 421 | cat >expect <<-EOF && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 422 | 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 Beller | 9c5b2fa | 2017-10-06 12:00:06 -0700 | [diff] [blame] | 438 | test_cmp expect output && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 439 | git diff --cached >output && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 440 | |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 441 | cat >cached_expect <<-EOF && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 442 | 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 Beller | 9c5b2fa | 2017-10-06 12:00:06 -0700 | [diff] [blame] | 451 | test_cmp cached_expect output |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 452 | ' |
| 453 | |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 454 | test_expect_success 'test resetting the index at give paths' ' |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 455 | mkdir sub && |
| 456 | >sub/file1 && |
| 457 | >sub/file2 && |
| 458 | git update-index --add sub/file1 sub/file2 && |
| 459 | T=$(git write-tree) && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 460 | git reset HEAD sub/file2 && |
| 461 | test_must_fail git diff --quiet && |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 462 | U=$(git write-tree) && |
| 463 | echo "$T" && |
| 464 | echo "$U" && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 465 | test_must_fail git diff-index --cached --exit-code "$T" && |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 466 | test "$T" != "$U" |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 467 | ' |
| 468 | |
Johannes Schindelin | 2e7a978 | 2007-11-03 13:12:17 +0000 | [diff] [blame] | 469 | test_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 Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 476 | test_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 Hamano | 476cca6 | 2011-04-12 16:36:18 -0700 | [diff] [blame] | 498 | test_expect_success '--mixed refreshes the index' ' |
Victoria Dye | 45bf762 | 2022-03-23 18:17:58 +0000 | [diff] [blame] | 499 | # Verify default behavior (without --[no-]refresh or reset.refresh) |
| 500 | test_reset_refreshes_index && |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 501 | |
Victoria Dye | 2efc9b8 | 2022-03-23 18:17:59 +0000 | [diff] [blame] | 502 | # With --quiet |
Victoria Dye | 45bf762 | 2022-03-23 18:17:58 +0000 | [diff] [blame] | 503 | test_reset_refreshes_index "" --quiet |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 504 | ' |
| 505 | |
| 506 | test_expect_success '--mixed --[no-]refresh sets refresh behavior' ' |
Victoria Dye | 7cff676 | 2022-03-23 18:18:00 +0000 | [diff] [blame] | 507 | # Verify that --[no-]refresh controls index refresh |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 508 | test_reset_refreshes_index "" --refresh && |
Victoria Dye | 7cff676 | 2022-03-23 18:18:00 +0000 | [diff] [blame] | 509 | ! test_reset_refreshes_index "" --no-refresh |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 510 | ' |
| 511 | |
Victoria Dye | 71471b2 | 2021-10-27 14:39:17 +0000 | [diff] [blame] | 512 | test_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 Hamano | ff00b68 | 2011-07-13 21:36:29 -0700 | [diff] [blame] | 529 | test_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 Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 540 | git reset HEAD file2 && |
| 541 | test_must_fail git diff --quiet && |
Junio C Hamano | ff00b68 | 2011-07-13 21:36:29 -0700 | [diff] [blame] | 542 | git diff-index --exit-code --cached HEAD |
| 543 | ' |
| 544 | |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 545 | test_expect_success 'disambiguation (1)' ' |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 546 | git reset --hard && |
| 547 | >secondfile && |
| 548 | git add secondfile && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 549 | git reset secondfile && |
| 550 | test_must_fail git diff --quiet -- secondfile && |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 551 | test -z "$(git diff --cached --name-only)" && |
| 552 | test -f secondfile && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 553 | test_must_be_empty secondfile |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 554 | ' |
| 555 | |
| 556 | test_expect_success 'disambiguation (2)' ' |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 557 | 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 Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 564 | ' |
| 565 | |
| 566 | test_expect_success 'disambiguation (3)' ' |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 567 | git reset --hard && |
| 568 | >secondfile && |
| 569 | git add secondfile && |
| 570 | rm -f secondfile && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 571 | git reset HEAD secondfile && |
| 572 | test_must_fail git diff --quiet && |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 573 | test -z "$(git diff --cached --name-only)" && |
| 574 | test ! -f secondfile |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 575 | ' |
| 576 | |
| 577 | test_expect_success 'disambiguation (4)' ' |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 578 | git reset --hard && |
| 579 | >secondfile && |
| 580 | git add secondfile && |
| 581 | rm -f secondfile && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 582 | git reset -- secondfile && |
| 583 | test_must_fail git diff --quiet && |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 584 | test -z "$(git diff --cached --name-only)" && |
| 585 | test ! -f secondfile |
| 586 | ' |
| 587 | |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 588 | test_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 Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 596 | test_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ð Bjarmason | d3c6751 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 603 | test_must_be_empty actual && |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 604 | |
| 605 | git diff --name-only >actual && |
| 606 | echo new-file >expect && |
| 607 | test_cmp expect actual |
| 608 | ' |
| 609 | |
Nguyễn Thái Ngọc Duy | b7756d4 | 2014-02-16 09:28:03 +0700 | [diff] [blame] | 610 | test_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 Duy | b7756d4 | 2014-02-16 09:28:03 +0700 | [diff] [blame] | 616 | git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual && |
Ævar Arnfjörð Bjarmason | d3c6751 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 617 | test_must_be_empty actual |
Nguyễn Thái Ngọc Duy | b7756d4 | 2014-02-16 09:28:03 +0700 | [diff] [blame] | 618 | ' |
| 619 | |
Jeff King | 9385174 | 2023-12-06 17:21:45 -0500 | [diff] [blame] | 620 | test_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 Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 628 | test_done |