|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# Copyright (c) 2007 Carlos Rica |
| 4 | +# |
| 5 | + |
| 6 | +test_description='git-reset |
| 7 | +
|
| 8 | +Documented tests for git-reset' |
| 9 | + |
| 10 | +. ./test-lib.sh |
| 11 | + |
| 12 | +test_expect_success 'creating initial files and commits' ' |
| 13 | + test_tick && |
| 14 | + echo "1st file" >first && |
| 15 | + git add first && |
| 16 | + git commit -m "create 1st file" && |
| 17 | +
|
| 18 | + echo "2nd file" >second && |
| 19 | + git add second && |
| 20 | + git commit -m "create 2nd file" && |
| 21 | +
|
| 22 | + echo "2nd line 1st file" >>first && |
| 23 | + git commit -a -m "modify 1st file" && |
| 24 | +
|
| 25 | + git rm first && |
| 26 | + git mv second secondfile && |
| 27 | + git commit -a -m "remove 1st and rename 2nd" && |
| 28 | +
|
| 29 | + echo "1st line 2nd file" >secondfile && |
| 30 | + echo "2nd line 2nd file" >>secondfile && |
| 31 | + git commit -a -m "modify 2nd file" |
| 32 | +' |
| 33 | +# git log --pretty=oneline # to see those SHA1 involved |
| 34 | + |
| 35 | +check_changes () { |
| 36 | + test "$(git rev-parse HEAD)" = "$1" && |
| 37 | + git diff | git diff .diff_expect - && |
| 38 | + git diff --cached | git diff .cached_expect - && |
| 39 | + for FILE in * |
| 40 | + do |
| 41 | + echo $FILE':' |
| 42 | + cat $FILE || return |
| 43 | + done | git diff .cat_expect - |
| 44 | +} |
| 45 | + |
| 46 | +>.diff_expect |
| 47 | +>.cached_expect |
| 48 | +cat >.cat_expect <<EOF |
| 49 | +secondfile: |
| 50 | +1st line 2nd file |
| 51 | +2nd line 2nd file |
| 52 | +EOF |
| 53 | + |
| 54 | +test_expect_success 'giving a non existing revision should fail' ' |
| 55 | + ! git reset aaaaaa && |
| 56 | + ! git reset --mixed aaaaaa && |
| 57 | + ! git reset --soft aaaaaa && |
| 58 | + ! git reset --hard aaaaaa && |
| 59 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 60 | +' |
| 61 | + |
| 62 | +test_expect_success \ |
| 63 | + 'giving paths with options different than --mixed should fail' ' |
| 64 | + ! git reset --soft -- first && |
| 65 | + ! git reset --hard -- first && |
| 66 | + ! git reset --soft HEAD^ -- first && |
| 67 | + ! git reset --hard HEAD^ -- first && |
| 68 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 69 | +' |
| 70 | + |
| 71 | +test_expect_success 'giving unrecognized options should fail' ' |
| 72 | + ! git reset --other && |
| 73 | + ! git reset -o && |
| 74 | + ! git reset --mixed --other && |
| 75 | + ! git reset --mixed -o && |
| 76 | + ! git reset --soft --other && |
| 77 | + ! git reset --soft -o && |
| 78 | + ! git reset --hard --other && |
| 79 | + ! git reset --hard -o && |
| 80 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 81 | +' |
| 82 | + |
| 83 | +test_expect_success \ |
| 84 | + 'trying to do reset --soft with pending merge should fail' ' |
| 85 | + git branch branch1 && |
| 86 | + git branch branch2 && |
| 87 | +
|
| 88 | + git checkout branch1 && |
| 89 | + echo "3rd line in branch1" >>secondfile && |
| 90 | + git commit -a -m "change in branch1" && |
| 91 | +
|
| 92 | + git checkout branch2 && |
| 93 | + echo "3rd line in branch2" >>secondfile && |
| 94 | + git commit -a -m "change in branch2" && |
| 95 | +
|
| 96 | + ! git merge branch1 && |
| 97 | + ! git reset --soft && |
| 98 | +
|
| 99 | + printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile && |
| 100 | + git commit -a -m "the change in branch2" && |
| 101 | +
|
| 102 | + git checkout master && |
| 103 | + git branch -D branch1 branch2 && |
| 104 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 105 | +' |
| 106 | + |
| 107 | +test_expect_success \ |
| 108 | + 'trying to do reset --soft with pending checkout merge should fail' ' |
| 109 | + git branch branch3 && |
| 110 | + git branch branch4 && |
| 111 | +
|
| 112 | + git checkout branch3 && |
| 113 | + echo "3rd line in branch3" >>secondfile && |
| 114 | + git commit -a -m "line in branch3" && |
| 115 | +
|
| 116 | + git checkout branch4 && |
| 117 | + echo "3rd line in branch4" >>secondfile && |
| 118 | +
|
| 119 | + git checkout -m branch3 && |
| 120 | + ! git reset --soft && |
| 121 | +
|
| 122 | + printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile && |
| 123 | + git commit -a -m "the line in branch3" && |
| 124 | +
|
| 125 | + git checkout master && |
| 126 | + git branch -D branch3 branch4 && |
| 127 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 128 | +' |
| 129 | + |
| 130 | +test_expect_success \ |
| 131 | + 'resetting to HEAD with no changes should succeed and do nothing' ' |
| 132 | + git reset --hard && |
| 133 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 134 | + git reset --hard HEAD && |
| 135 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 136 | + git reset --soft && |
| 137 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 138 | + git reset --soft HEAD && |
| 139 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 140 | + git reset --mixed && |
| 141 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 142 | + git reset --mixed HEAD && |
| 143 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 144 | + git reset && |
| 145 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 146 | + git reset HEAD && |
| 147 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 148 | +' |
| 149 | + |
| 150 | +>.diff_expect |
| 151 | +cat >.cached_expect <<EOF |
| 152 | +diff --git a/secondfile b/secondfile |
| 153 | +index 1bbba79..44c5b58 100644 |
| 154 | +--- a/secondfile |
| 155 | ++++ b/secondfile |
| 156 | +@@ -1 +1,2 @@ |
| 157 | +-2nd file |
| 158 | ++1st line 2nd file |
| 159 | ++2nd line 2nd file |
| 160 | +EOF |
| 161 | +cat >.cat_expect <<EOF |
| 162 | +secondfile: |
| 163 | +1st line 2nd file |
| 164 | +2nd line 2nd file |
| 165 | +EOF |
| 166 | +test_expect_success '--soft reset only should show changes in diff --cached' ' |
| 167 | + git reset --soft HEAD^ && |
| 168 | + check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 && |
| 169 | + test "$(git rev-parse ORIG_HEAD)" = \ |
| 170 | + 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 171 | +' |
| 172 | + |
| 173 | +>.diff_expect |
| 174 | +>.cached_expect |
| 175 | +cat >.cat_expect <<EOF |
| 176 | +secondfile: |
| 177 | +1st line 2nd file |
| 178 | +2nd line 2nd file |
| 179 | +3rd line 2nd file |
| 180 | +EOF |
| 181 | +test_expect_success \ |
| 182 | + 'changing files and redo the last commit should succeed' ' |
| 183 | + echo "3rd line 2nd file" >>secondfile && |
| 184 | + git commit -a -C ORIG_HEAD && |
| 185 | + check_changes 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d && |
| 186 | + test "$(git rev-parse ORIG_HEAD)" = \ |
| 187 | + 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 188 | +' |
| 189 | + |
| 190 | +>.diff_expect |
| 191 | +>.cached_expect |
| 192 | +cat >.cat_expect <<EOF |
| 193 | +first: |
| 194 | +1st file |
| 195 | +2nd line 1st file |
| 196 | +second: |
| 197 | +2nd file |
| 198 | +EOF |
| 199 | +test_expect_success \ |
| 200 | + '--hard reset should change the files and undo commits permanently' ' |
| 201 | + git reset --hard HEAD~2 && |
| 202 | + check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e && |
| 203 | + test "$(git rev-parse ORIG_HEAD)" = \ |
| 204 | + 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d |
| 205 | +' |
| 206 | + |
| 207 | +>.diff_expect |
| 208 | +cat >.cached_expect <<EOF |
| 209 | +diff --git a/first b/first |
| 210 | +deleted file mode 100644 |
| 211 | +index 8206c22..0000000 |
| 212 | +--- a/first |
| 213 | ++++ /dev/null |
| 214 | +@@ -1,2 +0,0 @@ |
| 215 | +-1st file |
| 216 | +-2nd line 1st file |
| 217 | +diff --git a/second b/second |
| 218 | +deleted file mode 100644 |
| 219 | +index 1bbba79..0000000 |
| 220 | +--- a/second |
| 221 | ++++ /dev/null |
| 222 | +@@ -1 +0,0 @@ |
| 223 | +-2nd file |
| 224 | +diff --git a/secondfile b/secondfile |
| 225 | +new file mode 100644 |
| 226 | +index 0000000..44c5b58 |
| 227 | +--- /dev/null |
| 228 | ++++ b/secondfile |
| 229 | +@@ -0,0 +1,2 @@ |
| 230 | ++1st line 2nd file |
| 231 | ++2nd line 2nd file |
| 232 | +EOF |
| 233 | +cat >.cat_expect <<EOF |
| 234 | +secondfile: |
| 235 | +1st line 2nd file |
| 236 | +2nd line 2nd file |
| 237 | +EOF |
| 238 | +test_expect_success \ |
| 239 | + 'redoing changes adding them without commit them should succeed' ' |
| 240 | + git rm first && |
| 241 | + git mv second secondfile && |
| 242 | +
|
| 243 | + echo "1st line 2nd file" >secondfile && |
| 244 | + echo "2nd line 2nd file" >>secondfile && |
| 245 | + git add secondfile && |
| 246 | + check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e |
| 247 | +' |
| 248 | + |
| 249 | +cat >.diff_expect <<EOF |
| 250 | +diff --git a/first b/first |
| 251 | +deleted file mode 100644 |
| 252 | +index 8206c22..0000000 |
| 253 | +--- a/first |
| 254 | ++++ /dev/null |
| 255 | +@@ -1,2 +0,0 @@ |
| 256 | +-1st file |
| 257 | +-2nd line 1st file |
| 258 | +diff --git a/second b/second |
| 259 | +deleted file mode 100644 |
| 260 | +index 1bbba79..0000000 |
| 261 | +--- a/second |
| 262 | ++++ /dev/null |
| 263 | +@@ -1 +0,0 @@ |
| 264 | +-2nd file |
| 265 | +EOF |
| 266 | +>.cached_expect |
| 267 | +cat >.cat_expect <<EOF |
| 268 | +secondfile: |
| 269 | +1st line 2nd file |
| 270 | +2nd line 2nd file |
| 271 | +EOF |
| 272 | +test_expect_success '--mixed reset to HEAD should unadd the files' ' |
| 273 | + git reset && |
| 274 | + check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e && |
| 275 | + test "$(git rev-parse ORIG_HEAD)" = \ |
| 276 | + ddaefe00f1da16864591c61fdc7adb5d7cd6b74e |
| 277 | +' |
| 278 | + |
| 279 | +>.diff_expect |
| 280 | +>.cached_expect |
| 281 | +cat >.cat_expect <<EOF |
| 282 | +secondfile: |
| 283 | +1st line 2nd file |
| 284 | +2nd line 2nd file |
| 285 | +EOF |
| 286 | +test_expect_success 'redoing the last two commits should succeed' ' |
| 287 | + git add secondfile && |
| 288 | + git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e && |
| 289 | +
|
| 290 | + git rm first && |
| 291 | + git mv second secondfile && |
| 292 | + git commit -a -m "remove 1st and rename 2nd" && |
| 293 | +
|
| 294 | + echo "1st line 2nd file" >secondfile && |
| 295 | + echo "2nd line 2nd file" >>secondfile && |
| 296 | + git commit -a -m "modify 2nd file" && |
| 297 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 298 | +' |
| 299 | + |
| 300 | +>.diff_expect |
| 301 | +>.cached_expect |
| 302 | +cat >.cat_expect <<EOF |
| 303 | +secondfile: |
| 304 | +1st line 2nd file |
| 305 | +2nd line 2nd file |
| 306 | +3rd line in branch2 |
| 307 | +EOF |
| 308 | +test_expect_success '--hard reset to HEAD should clear a failed merge' ' |
| 309 | + git branch branch1 && |
| 310 | + git branch branch2 && |
| 311 | +
|
| 312 | + git checkout branch1 && |
| 313 | + echo "3rd line in branch1" >>secondfile && |
| 314 | + git commit -a -m "change in branch1" && |
| 315 | +
|
| 316 | + git checkout branch2 && |
| 317 | + echo "3rd line in branch2" >>secondfile && |
| 318 | + git commit -a -m "change in branch2" && |
| 319 | +
|
| 320 | + ! git pull . branch1 && |
| 321 | + git reset --hard && |
| 322 | + check_changes 77abb337073fb4369a7ad69ff6f5ec0e4d6b54bb |
| 323 | +' |
| 324 | + |
| 325 | +>.diff_expect |
| 326 | +>.cached_expect |
| 327 | +cat >.cat_expect <<EOF |
| 328 | +secondfile: |
| 329 | +1st line 2nd file |
| 330 | +2nd line 2nd file |
| 331 | +EOF |
| 332 | +test_expect_success \ |
| 333 | + '--hard reset to ORIG_HEAD should clear a fast-forward merge' ' |
| 334 | + git reset --hard HEAD^ && |
| 335 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc && |
| 336 | +
|
| 337 | + git pull . branch1 && |
| 338 | + git reset --hard ORIG_HEAD && |
| 339 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc && |
| 340 | +
|
| 341 | + git checkout master && |
| 342 | + git branch -D branch1 branch2 && |
| 343 | + check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc |
| 344 | +' |
| 345 | + |
| 346 | +cat > expect << EOF |
| 347 | +diff --git a/file1 b/file1 |
| 348 | +index d00491f..7ed6ff8 100644 |
| 349 | +--- a/file1 |
| 350 | ++++ b/file1 |
| 351 | +@@ -1 +1 @@ |
| 352 | +-1 |
| 353 | ++5 |
| 354 | +diff --git a/file2 b/file2 |
| 355 | +deleted file mode 100644 |
| 356 | +index 0cfbf08..0000000 |
| 357 | +--- a/file2 |
| 358 | ++++ /dev/null |
| 359 | +@@ -1 +0,0 @@ |
| 360 | +-2 |
| 361 | +EOF |
| 362 | +cat > cached_expect << EOF |
| 363 | +diff --git a/file4 b/file4 |
| 364 | +new file mode 100644 |
| 365 | +index 0000000..b8626c4 |
| 366 | +--- /dev/null |
| 367 | ++++ b/file4 |
| 368 | +@@ -0,0 +1 @@ |
| 369 | ++4 |
| 370 | +EOF |
| 371 | +test_expect_success 'test --mixed <paths>' ' |
| 372 | + echo 1 > file1 && |
| 373 | + echo 2 > file2 && |
| 374 | + git add file1 file2 && |
| 375 | + test_tick && |
| 376 | + git commit -m files && |
| 377 | + git rm file2 && |
| 378 | + echo 3 > file3 && |
| 379 | + echo 4 > file4 && |
| 380 | + echo 5 > file1 && |
| 381 | + git add file1 file3 file4 && |
| 382 | + ! git reset HEAD -- file1 file2 file3 && |
| 383 | + git diff > output && |
| 384 | + git diff output expect && |
| 385 | + git diff --cached > output && |
| 386 | + git diff output cached_expect |
| 387 | +' |
| 388 | + |
| 389 | +test_done |
0 commit comments