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 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 13 | . ./test-lib.sh |
| 14 | |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 15 | commit_msg () { |
Alexey Shumkin | 17cc2ef | 2013-07-05 16:01:48 +0400 | [diff] [blame] | 16 | # String "modify 2nd file (changed)" partly in German |
| 17 | # (translated with Google Translate), |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 18 | # encoded in UTF-8, used as a commit log message below. |
Alexey Shumkin | 17cc2ef | 2013-07-05 16:01:48 +0400 | [diff] [blame] | 19 | msg="modify 2nd file (ge\303\244ndert)\n" |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 20 | if test -n "$1" |
| 21 | then |
Alexey Shumkin | 17cc2ef | 2013-07-05 16:01:48 +0400 | [diff] [blame] | 22 | printf "$msg" | iconv -f utf-8 -t "$1" |
| 23 | else |
| 24 | printf "$msg" |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 25 | fi |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 26 | } |
| 27 | |
Alexey Shumkin | ee3efaf | 2014-05-21 17:20:04 +0400 | [diff] [blame] | 28 | # Tested non-UTF-8 encoding |
| 29 | test_encoding="ISO8859-1" |
| 30 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 31 | test_expect_success 'creating initial files and commits' ' |
| 32 | test_tick && |
| 33 | echo "1st file" >first && |
| 34 | git add first && |
| 35 | git commit -m "create 1st file" && |
| 36 | |
| 37 | echo "2nd file" >second && |
| 38 | git add second && |
| 39 | git commit -m "create 2nd file" && |
| 40 | |
| 41 | echo "2nd line 1st file" >>first && |
| 42 | git commit -a -m "modify 1st file" && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 43 | head5p2=$(git rev-parse --verify HEAD) && |
| 44 | head5p2f=$(git rev-parse --short HEAD:first) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 45 | |
| 46 | git rm first && |
| 47 | git mv second secondfile && |
| 48 | git commit -a -m "remove 1st and rename 2nd" && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 49 | head5p1=$(git rev-parse --verify HEAD) && |
| 50 | head5p1s=$(git rev-parse --short HEAD:secondfile) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 51 | |
| 52 | echo "1st line 2nd file" >secondfile && |
| 53 | echo "2nd line 2nd file" >>secondfile && |
Pat Thoyts | e6ce2be | 2013-09-02 15:44:54 +0100 | [diff] [blame] | 54 | # "git commit -m" would break MinGW, as Windows refuse to pass |
| 55 | # $test_encoding encoded parameter to git. |
| 56 | 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] | 57 | head5=$(git rev-parse --verify HEAD) && |
| 58 | head5s=$(git rev-parse --short HEAD:secondfile) && |
| 59 | head5sl=$(git rev-parse HEAD:secondfile) |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 60 | ' |
| 61 | # git log --pretty=oneline # to see those SHA1 involved |
| 62 | |
| 63 | check_changes () { |
| 64 | test "$(git rev-parse HEAD)" = "$1" && |
Junio C Hamano | 3af8286 | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 65 | git diff | test_cmp .diff_expect - && |
| 66 | git diff --cached | test_cmp .cached_expect - && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 67 | for FILE in * |
| 68 | do |
| 69 | echo $FILE':' |
| 70 | cat $FILE || return |
Junio C Hamano | 3af8286 | 2008-05-23 22:28:56 -0700 | [diff] [blame] | 71 | done | test_cmp .cat_expect - |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Alexey Shumkin | ecaee80 | 2013-06-26 14:19:50 +0400 | [diff] [blame] | 74 | test_expect_success 'reset --hard message' ' |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 75 | hex=$(git log -1 --format="%h") && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 76 | git reset --hard >.actual && |
| 77 | echo HEAD is now at $hex $(commit_msg) >.expected && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 78 | test_cmp .expected .actual |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 79 | ' |
| 80 | |
Alexey Shumkin | ee3efaf | 2014-05-21 17:20:04 +0400 | [diff] [blame] | 81 | test_expect_success 'reset --hard message (ISO8859-1 logoutputencoding)' ' |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 82 | hex=$(git log -1 --format="%h") && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 83 | git -c "i18n.logOutputEncoding=$test_encoding" reset --hard >.actual && |
| 84 | 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] | 85 | test_cmp .expected .actual |
Alexey Shumkin | de6029a | 2013-06-26 14:19:49 +0400 | [diff] [blame] | 86 | ' |
| 87 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 88 | test_expect_success 'giving a non existing revision should fail' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 89 | >.diff_expect && |
| 90 | >.cached_expect && |
| 91 | cat >.cat_expect <<-\EOF && |
| 92 | secondfile: |
| 93 | 1st line 2nd file |
| 94 | 2nd line 2nd file |
| 95 | EOF |
| 96 | |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 97 | test_must_fail git reset aaaaaa && |
| 98 | test_must_fail git reset --mixed aaaaaa && |
| 99 | test_must_fail git reset --soft aaaaaa && |
| 100 | test_must_fail git reset --hard aaaaaa && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 101 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 102 | ' |
| 103 | |
Johannes Schindelin | cdf4a75 | 2007-11-03 14:33:01 +0000 | [diff] [blame] | 104 | test_expect_success 'reset --soft with unmerged index should fail' ' |
| 105 | touch .git/MERGE_HEAD && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 106 | echo "100644 $head5sl 1 un" | |
Johannes Schindelin | cdf4a75 | 2007-11-03 14:33:01 +0000 | [diff] [blame] | 107 | git update-index --index-info && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 108 | test_must_fail git reset --soft HEAD && |
Johannes Schindelin | cdf4a75 | 2007-11-03 14:33:01 +0000 | [diff] [blame] | 109 | rm .git/MERGE_HEAD && |
| 110 | git rm --cached -- un |
| 111 | ' |
| 112 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 113 | test_expect_success 'giving paths with options different than --mixed should fail' ' |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 114 | test_must_fail git reset --soft -- first && |
| 115 | test_must_fail git reset --hard -- first && |
| 116 | test_must_fail git reset --soft HEAD^ -- first && |
| 117 | test_must_fail git reset --hard HEAD^ -- first && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 118 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 119 | ' |
| 120 | |
| 121 | test_expect_success 'giving unrecognized options should fail' ' |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 122 | test_must_fail git reset --other && |
| 123 | test_must_fail git reset -o && |
| 124 | test_must_fail git reset --mixed --other && |
| 125 | test_must_fail git reset --mixed -o && |
| 126 | test_must_fail git reset --soft --other && |
| 127 | test_must_fail git reset --soft -o && |
| 128 | test_must_fail git reset --hard --other && |
| 129 | test_must_fail git reset --hard -o && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 130 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 131 | ' |
| 132 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 133 | 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] | 134 | git branch branch1 && |
| 135 | git branch branch2 && |
| 136 | |
| 137 | git checkout branch1 && |
| 138 | echo "3rd line in branch1" >>secondfile && |
| 139 | git commit -a -m "change in branch1" && |
| 140 | |
| 141 | git checkout branch2 && |
| 142 | echo "3rd line in branch2" >>secondfile && |
| 143 | git commit -a -m "change in branch2" && |
| 144 | |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 145 | test_must_fail git merge branch1 && |
| 146 | test_must_fail git reset --soft && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 147 | |
| 148 | printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile && |
| 149 | git commit -a -m "the change in branch2" && |
| 150 | |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 151 | git checkout main && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 152 | git branch -D branch1 branch2 && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 153 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 154 | ' |
| 155 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 156 | 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] | 157 | git branch branch3 && |
| 158 | git branch branch4 && |
| 159 | |
| 160 | git checkout branch3 && |
| 161 | echo "3rd line in branch3" >>secondfile && |
| 162 | git commit -a -m "line in branch3" && |
| 163 | |
| 164 | git checkout branch4 && |
| 165 | echo "3rd line in branch4" >>secondfile && |
| 166 | |
| 167 | git checkout -m branch3 && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 168 | test_must_fail git reset --soft && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 169 | |
| 170 | printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile && |
| 171 | git commit -a -m "the line in branch3" && |
| 172 | |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 173 | git checkout main && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 174 | git branch -D branch3 branch4 && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 175 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 176 | ' |
| 177 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 178 | 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] | 179 | git reset --hard && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 180 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 181 | git reset --hard HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 182 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 183 | git reset --soft && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 184 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 185 | git reset --soft HEAD && |
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 | git reset --mixed && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 188 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 189 | git reset --mixed HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 190 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 191 | git reset && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 192 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 193 | git reset HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 194 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 195 | ' |
| 196 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 197 | 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] | 198 | >.diff_expect && |
| 199 | cat >.cached_expect <<-EOF && |
| 200 | diff --git a/secondfile b/secondfile |
| 201 | index $head5p1s..$head5s 100644 |
| 202 | --- a/secondfile |
| 203 | +++ b/secondfile |
| 204 | @@ -1 +1,2 @@ |
| 205 | -2nd file |
| 206 | +1st line 2nd file |
| 207 | +2nd line 2nd file |
| 208 | EOF |
| 209 | cat >.cat_expect <<-\EOF && |
| 210 | secondfile: |
| 211 | 1st line 2nd file |
| 212 | 2nd line 2nd file |
| 213 | EOF |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 214 | git reset --soft HEAD^ && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 215 | check_changes $head5p1 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 216 | test "$(git rev-parse ORIG_HEAD)" = \ |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 217 | $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 218 | ' |
| 219 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 220 | 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] | 221 | >.diff_expect && |
| 222 | >.cached_expect && |
| 223 | cat >.cat_expect <<-\EOF && |
| 224 | secondfile: |
| 225 | 1st line 2nd file |
| 226 | 2nd line 2nd file |
| 227 | 3rd line 2nd file |
| 228 | EOF |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 229 | echo "3rd line 2nd file" >>secondfile && |
| 230 | git commit -a -C ORIG_HEAD && |
Alexey Shumkin | 375775b | 2013-06-26 14:19:47 +0400 | [diff] [blame] | 231 | head4=$(git rev-parse --verify HEAD) && |
| 232 | check_changes $head4 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 233 | test "$(git rev-parse ORIG_HEAD)" = \ |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 234 | $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 235 | ' |
| 236 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 237 | 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] | 238 | >.diff_expect && |
| 239 | >.cached_expect && |
| 240 | cat >.cat_expect <<-\EOF && |
| 241 | first: |
| 242 | 1st file |
| 243 | 2nd line 1st file |
| 244 | second: |
| 245 | 2nd file |
| 246 | EOF |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 247 | git reset --hard HEAD~2 && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 248 | check_changes $head5p2 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 249 | test "$(git rev-parse ORIG_HEAD)" = \ |
Alexey Shumkin | 375775b | 2013-06-26 14:19:47 +0400 | [diff] [blame] | 250 | $head4 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 251 | ' |
| 252 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 253 | 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] | 254 | >.diff_expect && |
| 255 | cat >.cached_expect <<-EOF && |
| 256 | diff --git a/first b/first |
| 257 | deleted file mode 100644 |
| 258 | index $head5p2f..0000000 |
| 259 | --- a/first |
| 260 | +++ /dev/null |
| 261 | @@ -1,2 +0,0 @@ |
| 262 | -1st file |
| 263 | -2nd line 1st file |
| 264 | diff --git a/second b/second |
| 265 | deleted file mode 100644 |
| 266 | index $head5p1s..0000000 |
| 267 | --- a/second |
| 268 | +++ /dev/null |
| 269 | @@ -1 +0,0 @@ |
| 270 | -2nd file |
| 271 | diff --git a/secondfile b/secondfile |
| 272 | new file mode 100644 |
| 273 | index 0000000..$head5s |
| 274 | --- /dev/null |
| 275 | +++ b/secondfile |
| 276 | @@ -0,0 +1,2 @@ |
| 277 | +1st line 2nd file |
| 278 | +2nd line 2nd file |
| 279 | EOF |
| 280 | cat >.cat_expect <<-\EOF && |
| 281 | secondfile: |
| 282 | 1st line 2nd file |
| 283 | 2nd line 2nd file |
| 284 | EOF |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 285 | git rm first && |
| 286 | git mv second secondfile && |
| 287 | |
| 288 | echo "1st line 2nd file" >secondfile && |
| 289 | echo "2nd line 2nd file" >>secondfile && |
| 290 | git add secondfile && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 291 | check_changes $head5p2 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 292 | ' |
| 293 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 294 | test_expect_success '--mixed reset to HEAD should unadd the files' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 295 | cat >.diff_expect <<-EOF && |
| 296 | diff --git a/first b/first |
| 297 | deleted file mode 100644 |
| 298 | index $head5p2f..0000000 |
| 299 | --- a/first |
| 300 | +++ /dev/null |
| 301 | @@ -1,2 +0,0 @@ |
| 302 | -1st file |
| 303 | -2nd line 1st file |
| 304 | diff --git a/second b/second |
| 305 | deleted file mode 100644 |
| 306 | index $head5p1s..0000000 |
| 307 | --- a/second |
| 308 | +++ /dev/null |
| 309 | @@ -1 +0,0 @@ |
| 310 | -2nd file |
| 311 | EOF |
| 312 | >.cached_expect && |
| 313 | cat >.cat_expect <<-\EOF && |
| 314 | secondfile: |
| 315 | 1st line 2nd file |
| 316 | 2nd line 2nd file |
| 317 | EOF |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 318 | git reset && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 319 | check_changes $head5p2 && |
| 320 | test "$(git rev-parse ORIG_HEAD)" = $head5p2 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 321 | ' |
| 322 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 323 | test_expect_success 'redoing the last two commits should succeed' ' |
Junio C Hamano | 31f4c83 | 2020-10-21 22:55:58 -0700 | [diff] [blame] | 324 | >.diff_expect && |
| 325 | >.cached_expect && |
| 326 | cat >.cat_expect <<-\EOF && |
| 327 | secondfile: |
| 328 | 1st line 2nd file |
| 329 | 2nd line 2nd file |
| 330 | EOF |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 331 | git add secondfile && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 332 | git reset --hard $head5p2 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 333 | git rm first && |
| 334 | git mv second secondfile && |
| 335 | git commit -a -m "remove 1st and rename 2nd" && |
| 336 | |
| 337 | echo "1st line 2nd file" >secondfile && |
| 338 | echo "2nd line 2nd file" >>secondfile && |
Pat Thoyts | e6ce2be | 2013-09-02 15:44:54 +0100 | [diff] [blame] | 339 | # "git commit -m" would break MinGW, as Windows refuse to pass |
| 340 | # $test_encoding encoded parameter to git. |
| 341 | 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] | 342 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 343 | ' |
| 344 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 345 | 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] | 346 | >.diff_expect && |
| 347 | >.cached_expect && |
| 348 | cat >.cat_expect <<-\EOF && |
| 349 | secondfile: |
| 350 | 1st line 2nd file |
| 351 | 2nd line 2nd file |
| 352 | 3rd line in branch2 |
| 353 | EOF |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 354 | git branch branch1 && |
| 355 | git branch branch2 && |
| 356 | |
| 357 | git checkout branch1 && |
| 358 | echo "3rd line in branch1" >>secondfile && |
| 359 | git commit -a -m "change in branch1" && |
| 360 | |
| 361 | git checkout branch2 && |
| 362 | echo "3rd line in branch2" >>secondfile && |
| 363 | git commit -a -m "change in branch2" && |
Alexey Shumkin | 375775b | 2013-06-26 14:19:47 +0400 | [diff] [blame] | 364 | head3=$(git rev-parse --verify HEAD) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 365 | |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 366 | test_must_fail git pull . branch1 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 367 | git reset --hard && |
Alexey Shumkin | 375775b | 2013-06-26 14:19:47 +0400 | [diff] [blame] | 368 | check_changes $head3 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 369 | ' |
| 370 | |
Charvi Mendiratta | e166fe3 | 2020-10-20 17:13:15 +0530 | [diff] [blame] | 371 | 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] | 372 | >.diff_expect && |
| 373 | >.cached_expect && |
| 374 | cat >.cat_expect <<-\EOF && |
| 375 | secondfile: |
| 376 | 1st line 2nd file |
| 377 | 2nd line 2nd file |
| 378 | EOF |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 379 | git reset --hard HEAD^ && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 380 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 381 | |
| 382 | git pull . branch1 && |
| 383 | git reset --hard ORIG_HEAD && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 384 | check_changes $head5 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 385 | |
Johannes Schindelin | 01dc813 | 2020-11-18 23:44:39 +0000 | [diff] [blame] | 386 | git checkout main && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 387 | git branch -D branch1 branch2 && |
Alexey Shumkin | 8b66f78 | 2013-01-24 13:10:26 +0400 | [diff] [blame] | 388 | check_changes $head5 |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 389 | ' |
| 390 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 391 | test_expect_success 'test --mixed <paths>' ' |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 392 | echo 1 >file1 && |
| 393 | echo 2 >file2 && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 394 | git add file1 file2 && |
| 395 | test_tick && |
| 396 | git commit -m files && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 397 | before1=$(git rev-parse --short HEAD:file1) && |
| 398 | before2=$(git rev-parse --short HEAD:file2) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 399 | git rm file2 && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 400 | echo 3 >file3 && |
| 401 | echo 4 >file4 && |
| 402 | echo 5 >file1 && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 403 | after1=$(git rev-parse --short $(git hash-object file1)) && |
| 404 | after4=$(git rev-parse --short $(git hash-object file4)) && |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 405 | git add file1 file3 file4 && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 406 | git reset HEAD -- file1 file2 file3 && |
| 407 | test_must_fail git diff --quiet && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 408 | git diff >output && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 409 | |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 410 | cat >expect <<-EOF && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 411 | diff --git a/file1 b/file1 |
| 412 | index $before1..$after1 100644 |
| 413 | --- a/file1 |
| 414 | +++ b/file1 |
| 415 | @@ -1 +1 @@ |
| 416 | -1 |
| 417 | +5 |
| 418 | diff --git a/file2 b/file2 |
| 419 | deleted file mode 100644 |
| 420 | index $before2..0000000 |
| 421 | --- a/file2 |
| 422 | +++ /dev/null |
| 423 | @@ -1 +0,0 @@ |
| 424 | -2 |
| 425 | EOF |
| 426 | |
Stefan Beller | 9c5b2fa | 2017-10-06 12:00:06 -0700 | [diff] [blame] | 427 | test_cmp expect output && |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 428 | git diff --cached >output && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 429 | |
Charvi Mendiratta | c327762 | 2020-10-20 17:13:17 +0530 | [diff] [blame] | 430 | cat >cached_expect <<-EOF && |
brian m. carlson | d62607d | 2020-07-29 23:14:02 +0000 | [diff] [blame] | 431 | diff --git a/file4 b/file4 |
| 432 | new file mode 100644 |
| 433 | index 0000000..$after4 |
| 434 | --- /dev/null |
| 435 | +++ b/file4 |
| 436 | @@ -0,0 +1 @@ |
| 437 | +4 |
| 438 | EOF |
| 439 | |
Stefan Beller | 9c5b2fa | 2017-10-06 12:00:06 -0700 | [diff] [blame] | 440 | test_cmp cached_expect output |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 441 | ' |
| 442 | |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 443 | test_expect_success 'test resetting the index at give paths' ' |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 444 | mkdir sub && |
| 445 | >sub/file1 && |
| 446 | >sub/file2 && |
| 447 | git update-index --add sub/file1 sub/file2 && |
| 448 | T=$(git write-tree) && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 449 | git reset HEAD sub/file2 && |
| 450 | test_must_fail git diff --quiet && |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 451 | U=$(git write-tree) && |
| 452 | echo "$T" && |
| 453 | echo "$U" && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 454 | test_must_fail git diff-index --cached --exit-code "$T" && |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 455 | test "$T" != "$U" |
Junio C Hamano | cbb390c | 2007-09-13 20:54:14 -0700 | [diff] [blame] | 456 | ' |
| 457 | |
Johannes Schindelin | 2e7a978 | 2007-11-03 13:12:17 +0000 | [diff] [blame] | 458 | test_expect_success 'resetting an unmodified path is a no-op' ' |
| 459 | git reset --hard && |
| 460 | git reset -- file1 && |
| 461 | git diff-files --exit-code && |
| 462 | git diff-index --cached --exit-code HEAD |
| 463 | ' |
| 464 | |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 465 | test_reset_refreshes_index () { |
| 466 | |
| 467 | # To test whether the index is refreshed in `git reset --mixed` with |
| 468 | # the given options, create a scenario where we clearly see different |
| 469 | # results depending on whether the refresh occurred or not. |
| 470 | |
| 471 | # Step 0: start with a clean index |
| 472 | git reset --hard HEAD && |
| 473 | |
| 474 | # Step 1: remove file2, but only in the index (no change to worktree) |
| 475 | git rm --cached file2 && |
| 476 | |
| 477 | # Step 2: reset index & leave worktree unchanged from HEAD |
| 478 | git $1 reset $2 --mixed HEAD && |
| 479 | |
| 480 | # Step 3: verify whether the index is refreshed by checking whether |
| 481 | # file2 still has staged changes in the index differing from HEAD (if |
| 482 | # the refresh occurred, there should be no such changes) |
| 483 | git diff-files >output.log && |
| 484 | test_must_be_empty output.log |
| 485 | } |
| 486 | |
Junio C Hamano | 476cca6 | 2011-04-12 16:36:18 -0700 | [diff] [blame] | 487 | test_expect_success '--mixed refreshes the index' ' |
Victoria Dye | 45bf762 | 2022-03-23 18:17:58 +0000 | [diff] [blame] | 488 | # Verify default behavior (without --[no-]refresh or reset.refresh) |
| 489 | test_reset_refreshes_index && |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 490 | |
Victoria Dye | 2efc9b8 | 2022-03-23 18:17:59 +0000 | [diff] [blame] | 491 | # With --quiet |
Victoria Dye | 45bf762 | 2022-03-23 18:17:58 +0000 | [diff] [blame] | 492 | test_reset_refreshes_index "" --quiet |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 493 | ' |
| 494 | |
| 495 | test_expect_success '--mixed --[no-]refresh sets refresh behavior' ' |
Victoria Dye | 7cff676 | 2022-03-23 18:18:00 +0000 | [diff] [blame] | 496 | # Verify that --[no-]refresh controls index refresh |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 497 | test_reset_refreshes_index "" --refresh && |
Victoria Dye | 7cff676 | 2022-03-23 18:18:00 +0000 | [diff] [blame] | 498 | ! test_reset_refreshes_index "" --no-refresh |
Victoria Dye | fd56fba | 2022-03-15 01:49:39 +0000 | [diff] [blame] | 499 | ' |
| 500 | |
Victoria Dye | 71471b2 | 2021-10-27 14:39:17 +0000 | [diff] [blame] | 501 | test_expect_success '--mixed preserves skip-worktree' ' |
| 502 | echo 123 >>file2 && |
| 503 | git add file2 && |
| 504 | git update-index --skip-worktree file2 && |
| 505 | git reset --mixed HEAD >output && |
| 506 | test_must_be_empty output && |
| 507 | |
| 508 | cat >expect <<-\EOF && |
| 509 | Unstaged changes after reset: |
| 510 | M file2 |
| 511 | EOF |
| 512 | git update-index --no-skip-worktree file2 && |
| 513 | git add file2 && |
| 514 | git reset --mixed HEAD >output && |
| 515 | test_cmp expect output |
| 516 | ' |
| 517 | |
Junio C Hamano | ff00b68 | 2011-07-13 21:36:29 -0700 | [diff] [blame] | 518 | test_expect_success 'resetting specific path that is unmerged' ' |
| 519 | git rm --cached file2 && |
| 520 | F1=$(git rev-parse HEAD:file1) && |
| 521 | F2=$(git rev-parse HEAD:file2) && |
| 522 | F3=$(git rev-parse HEAD:secondfile) && |
| 523 | { |
| 524 | echo "100644 $F1 1 file2" && |
| 525 | echo "100644 $F2 2 file2" && |
| 526 | echo "100644 $F3 3 file2" |
| 527 | } | git update-index --index-info && |
| 528 | git ls-files -u && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 529 | git reset HEAD file2 && |
| 530 | test_must_fail git diff --quiet && |
Junio C Hamano | ff00b68 | 2011-07-13 21:36:29 -0700 | [diff] [blame] | 531 | git diff-index --exit-code --cached HEAD |
| 532 | ' |
| 533 | |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 534 | test_expect_success 'disambiguation (1)' ' |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 535 | git reset --hard && |
| 536 | >secondfile && |
| 537 | git add secondfile && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 538 | git reset secondfile && |
| 539 | test_must_fail git diff --quiet -- secondfile && |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 540 | test -z "$(git diff --cached --name-only)" && |
| 541 | test -f secondfile && |
Junio C Hamano | ca8d148 | 2013-06-09 11:29:20 -0700 | [diff] [blame] | 542 | test_must_be_empty secondfile |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 543 | ' |
| 544 | |
| 545 | test_expect_success 'disambiguation (2)' ' |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 546 | git reset --hard && |
| 547 | >secondfile && |
| 548 | git add secondfile && |
| 549 | rm -f secondfile && |
| 550 | test_must_fail git reset secondfile && |
| 551 | test -n "$(git diff --cached --name-only -- secondfile)" && |
| 552 | test ! -f secondfile |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 553 | ' |
| 554 | |
| 555 | test_expect_success 'disambiguation (3)' ' |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 556 | git reset --hard && |
| 557 | >secondfile && |
| 558 | git add secondfile && |
| 559 | rm -f secondfile && |
Martin von Zweigbergk | d94c5e2 | 2013-01-14 21:47:34 -0800 | [diff] [blame] | 560 | git reset HEAD secondfile && |
| 561 | test_must_fail git diff --quiet && |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 562 | test -z "$(git diff --cached --name-only)" && |
| 563 | test ! -f secondfile |
Junio C Hamano | dfc8f39 | 2008-06-25 18:16:36 -0700 | [diff] [blame] | 564 | ' |
| 565 | |
| 566 | test_expect_success 'disambiguation (4)' ' |
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 -- 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 |
| 575 | ' |
| 576 | |
Martin von Zweigbergk | 2f328c3 | 2013-01-14 21:47:49 -0800 | [diff] [blame] | 577 | test_expect_success 'reset with paths accepts tree' ' |
| 578 | # for simpler tests, drop last commit containing added files |
| 579 | git reset --hard HEAD^ && |
| 580 | git reset HEAD^^{tree} -- . && |
| 581 | git diff --cached HEAD^ --exit-code && |
| 582 | git diff HEAD --exit-code |
| 583 | ' |
| 584 | |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 585 | test_expect_success 'reset -N keeps removed files as intent-to-add' ' |
| 586 | echo new-file >new-file && |
| 587 | git add new-file && |
| 588 | git reset -N HEAD && |
| 589 | |
| 590 | tree=$(git write-tree) && |
| 591 | git ls-tree $tree new-file >actual && |
Ævar Arnfjörð Bjarmason | d3c6751 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 592 | test_must_be_empty actual && |
Nguyễn Thái Ngọc Duy | b4b313f | 2014-02-04 09:20:09 +0700 | [diff] [blame] | 593 | |
| 594 | git diff --name-only >actual && |
| 595 | echo new-file >expect && |
| 596 | test_cmp expect actual |
| 597 | ' |
| 598 | |
Nguyễn Thái Ngọc Duy | b7756d4 | 2014-02-16 09:28:03 +0700 | [diff] [blame] | 599 | test_expect_success 'reset --mixed sets up work tree' ' |
| 600 | git init mixed_worktree && |
| 601 | ( |
| 602 | cd mixed_worktree && |
| 603 | test_commit dummy |
| 604 | ) && |
Nguyễn Thái Ngọc Duy | b7756d4 | 2014-02-16 09:28:03 +0700 | [diff] [blame] | 605 | 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] | 606 | test_must_be_empty actual |
Nguyễn Thái Ngọc Duy | b7756d4 | 2014-02-16 09:28:03 +0700 | [diff] [blame] | 607 | ' |
| 608 | |
Carlos Rica | 359048d | 2007-09-11 03:09:52 +0200 | [diff] [blame] | 609 | test_done |