t / t7102-reset.shon commit Merge branch 'jk/maint-config-param' (fe01ef3)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Carlos Rica
   4#
   5
   6test_description='git reset
   7
   8Documented tests for git reset'
   9
  10. ./test-lib.sh
  11
  12test_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
  35check_changes () {
  36        test "$(git rev-parse HEAD)" = "$1" &&
  37        git diff | test_cmp .diff_expect - &&
  38        git diff --cached | test_cmp .cached_expect - &&
  39        for FILE in *
  40        do
  41                echo $FILE':'
  42                cat $FILE || return
  43        done | test_cmp .cat_expect -
  44}
  45
  46>.diff_expect
  47>.cached_expect
  48cat >.cat_expect <<EOF
  49secondfile:
  501st line 2nd file
  512nd line 2nd file
  52EOF
  53
  54test_expect_success 'giving a non existing revision should fail' '
  55        test_must_fail git reset aaaaaa &&
  56        test_must_fail git reset --mixed aaaaaa &&
  57        test_must_fail git reset --soft aaaaaa &&
  58        test_must_fail git reset --hard aaaaaa &&
  59        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
  60'
  61
  62test_expect_success 'reset --soft with unmerged index should fail' '
  63        touch .git/MERGE_HEAD &&
  64        echo "100644 44c5b5884550c17758737edcced463447b91d42b 1 un" |
  65                git update-index --index-info &&
  66        test_must_fail git reset --soft HEAD &&
  67        rm .git/MERGE_HEAD &&
  68        git rm --cached -- un
  69'
  70
  71test_expect_success \
  72        'giving paths with options different than --mixed should fail' '
  73        test_must_fail git reset --soft -- first &&
  74        test_must_fail git reset --hard -- first &&
  75        test_must_fail git reset --soft HEAD^ -- first &&
  76        test_must_fail git reset --hard HEAD^ -- first &&
  77        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
  78'
  79
  80test_expect_success 'giving unrecognized options should fail' '
  81        test_must_fail git reset --other &&
  82        test_must_fail git reset -o &&
  83        test_must_fail git reset --mixed --other &&
  84        test_must_fail git reset --mixed -o &&
  85        test_must_fail git reset --soft --other &&
  86        test_must_fail git reset --soft -o &&
  87        test_must_fail git reset --hard --other &&
  88        test_must_fail git reset --hard -o &&
  89        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
  90'
  91
  92test_expect_success \
  93        'trying to do reset --soft with pending merge should fail' '
  94        git branch branch1 &&
  95        git branch branch2 &&
  96
  97        git checkout branch1 &&
  98        echo "3rd line in branch1" >>secondfile &&
  99        git commit -a -m "change in branch1" &&
 100
 101        git checkout branch2 &&
 102        echo "3rd line in branch2" >>secondfile &&
 103        git commit -a -m "change in branch2" &&
 104
 105        test_must_fail git merge branch1 &&
 106        test_must_fail git reset --soft &&
 107
 108        printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
 109        git commit -a -m "the change in branch2" &&
 110
 111        git checkout master &&
 112        git branch -D branch1 branch2 &&
 113        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
 114'
 115
 116test_expect_success \
 117        'trying to do reset --soft with pending checkout merge should fail' '
 118        git branch branch3 &&
 119        git branch branch4 &&
 120
 121        git checkout branch3 &&
 122        echo "3rd line in branch3" >>secondfile &&
 123        git commit -a -m "line in branch3" &&
 124
 125        git checkout branch4 &&
 126        echo "3rd line in branch4" >>secondfile &&
 127
 128        git checkout -m branch3 &&
 129        test_must_fail git reset --soft &&
 130
 131        printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
 132        git commit -a -m "the line in branch3" &&
 133
 134        git checkout master &&
 135        git branch -D branch3 branch4 &&
 136        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
 137'
 138
 139test_expect_success \
 140        'resetting to HEAD with no changes should succeed and do nothing' '
 141        git reset --hard &&
 142                check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 143        git reset --hard HEAD &&
 144                check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 145        git reset --soft &&
 146                check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 147        git reset --soft HEAD &&
 148                check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 149        git reset --mixed &&
 150                check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 151        git reset --mixed HEAD &&
 152                check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 153        git reset &&
 154                check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 155        git reset HEAD &&
 156                check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
 157'
 158
 159>.diff_expect
 160cat >.cached_expect <<EOF
 161diff --git a/secondfile b/secondfile
 162index 1bbba79..44c5b58 100644
 163--- a/secondfile
 164+++ b/secondfile
 165@@ -1 +1,2 @@
 166-2nd file
 167+1st line 2nd file
 168+2nd line 2nd file
 169EOF
 170cat >.cat_expect <<EOF
 171secondfile:
 1721st line 2nd file
 1732nd line 2nd file
 174EOF
 175test_expect_success '--soft reset only should show changes in diff --cached' '
 176        git reset --soft HEAD^ &&
 177        check_changes d1a4bc3abce4829628ae2dcb0d60ef3d1a78b1c4 &&
 178        test "$(git rev-parse ORIG_HEAD)" = \
 179                        3ec39651e7f44ea531a5de18a9fa791c0fd370fc
 180'
 181
 182>.diff_expect
 183>.cached_expect
 184cat >.cat_expect <<EOF
 185secondfile:
 1861st line 2nd file
 1872nd line 2nd file
 1883rd line 2nd file
 189EOF
 190test_expect_success \
 191        'changing files and redo the last commit should succeed' '
 192        echo "3rd line 2nd file" >>secondfile &&
 193        git commit -a -C ORIG_HEAD &&
 194        check_changes 3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d &&
 195        test "$(git rev-parse ORIG_HEAD)" = \
 196                        3ec39651e7f44ea531a5de18a9fa791c0fd370fc
 197'
 198
 199>.diff_expect
 200>.cached_expect
 201cat >.cat_expect <<EOF
 202first:
 2031st file
 2042nd line 1st file
 205second:
 2062nd file
 207EOF
 208test_expect_success \
 209        '--hard reset should change the files and undo commits permanently' '
 210        git reset --hard HEAD~2 &&
 211        check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
 212        test "$(git rev-parse ORIG_HEAD)" = \
 213                        3d3b7be011a58ca0c179ae45d94e6c83c0b0cd0d
 214'
 215
 216>.diff_expect
 217cat >.cached_expect <<EOF
 218diff --git a/first b/first
 219deleted file mode 100644
 220index 8206c22..0000000
 221--- a/first
 222+++ /dev/null
 223@@ -1,2 +0,0 @@
 224-1st file
 225-2nd line 1st file
 226diff --git a/second b/second
 227deleted file mode 100644
 228index 1bbba79..0000000
 229--- a/second
 230+++ /dev/null
 231@@ -1 +0,0 @@
 232-2nd file
 233diff --git a/secondfile b/secondfile
 234new file mode 100644
 235index 0000000..44c5b58
 236--- /dev/null
 237+++ b/secondfile
 238@@ -0,0 +1,2 @@
 239+1st line 2nd file
 240+2nd line 2nd file
 241EOF
 242cat >.cat_expect <<EOF
 243secondfile:
 2441st line 2nd file
 2452nd line 2nd file
 246EOF
 247test_expect_success \
 248        'redoing changes adding them without commit them should succeed' '
 249        git rm first &&
 250        git mv second secondfile &&
 251
 252        echo "1st line 2nd file" >secondfile &&
 253        echo "2nd line 2nd file" >>secondfile &&
 254        git add secondfile &&
 255        check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
 256'
 257
 258cat >.diff_expect <<EOF
 259diff --git a/first b/first
 260deleted file mode 100644
 261index 8206c22..0000000
 262--- a/first
 263+++ /dev/null
 264@@ -1,2 +0,0 @@
 265-1st file
 266-2nd line 1st file
 267diff --git a/second b/second
 268deleted file mode 100644
 269index 1bbba79..0000000
 270--- a/second
 271+++ /dev/null
 272@@ -1 +0,0 @@
 273-2nd file
 274EOF
 275>.cached_expect
 276cat >.cat_expect <<EOF
 277secondfile:
 2781st line 2nd file
 2792nd line 2nd file
 280EOF
 281test_expect_success '--mixed reset to HEAD should unadd the files' '
 282        git reset &&
 283        check_changes ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
 284        test "$(git rev-parse ORIG_HEAD)" = \
 285                        ddaefe00f1da16864591c61fdc7adb5d7cd6b74e
 286'
 287
 288>.diff_expect
 289>.cached_expect
 290cat >.cat_expect <<EOF
 291secondfile:
 2921st line 2nd file
 2932nd line 2nd file
 294EOF
 295test_expect_success 'redoing the last two commits should succeed' '
 296        git add secondfile &&
 297        git reset --hard ddaefe00f1da16864591c61fdc7adb5d7cd6b74e &&
 298
 299        git rm first &&
 300        git mv second secondfile &&
 301        git commit -a -m "remove 1st and rename 2nd" &&
 302
 303        echo "1st line 2nd file" >secondfile &&
 304        echo "2nd line 2nd file" >>secondfile &&
 305        git commit -a -m "modify 2nd file" &&
 306        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
 307'
 308
 309>.diff_expect
 310>.cached_expect
 311cat >.cat_expect <<EOF
 312secondfile:
 3131st line 2nd file
 3142nd line 2nd file
 3153rd line in branch2
 316EOF
 317test_expect_success '--hard reset to HEAD should clear a failed merge' '
 318        git branch branch1 &&
 319        git branch branch2 &&
 320
 321        git checkout branch1 &&
 322        echo "3rd line in branch1" >>secondfile &&
 323        git commit -a -m "change in branch1" &&
 324
 325        git checkout branch2 &&
 326        echo "3rd line in branch2" >>secondfile &&
 327        git commit -a -m "change in branch2" &&
 328
 329        test_must_fail git pull . branch1 &&
 330        git reset --hard &&
 331        check_changes 77abb337073fb4369a7ad69ff6f5ec0e4d6b54bb
 332'
 333
 334>.diff_expect
 335>.cached_expect
 336cat >.cat_expect <<EOF
 337secondfile:
 3381st line 2nd file
 3392nd line 2nd file
 340EOF
 341test_expect_success \
 342        '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
 343        git reset --hard HEAD^ &&
 344        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 345
 346        git pull . branch1 &&
 347        git reset --hard ORIG_HEAD &&
 348        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc &&
 349
 350        git checkout master &&
 351        git branch -D branch1 branch2 &&
 352        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
 353'
 354
 355cat > expect << EOF
 356diff --git a/file1 b/file1
 357index d00491f..7ed6ff8 100644
 358--- a/file1
 359+++ b/file1
 360@@ -1 +1 @@
 361-1
 362+5
 363diff --git a/file2 b/file2
 364deleted file mode 100644
 365index 0cfbf08..0000000
 366--- a/file2
 367+++ /dev/null
 368@@ -1 +0,0 @@
 369-2
 370EOF
 371cat > cached_expect << EOF
 372diff --git a/file4 b/file4
 373new file mode 100644
 374index 0000000..b8626c4
 375--- /dev/null
 376+++ b/file4
 377@@ -0,0 +1 @@
 378+4
 379EOF
 380test_expect_success 'test --mixed <paths>' '
 381        echo 1 > file1 &&
 382        echo 2 > file2 &&
 383        git add file1 file2 &&
 384        test_tick &&
 385        git commit -m files &&
 386        git rm file2 &&
 387        echo 3 > file3 &&
 388        echo 4 > file4 &&
 389        echo 5 > file1 &&
 390        git add file1 file3 file4 &&
 391        test_must_fail git reset HEAD -- file1 file2 file3 &&
 392        git diff > output &&
 393        test_cmp output expect &&
 394        git diff --cached > output &&
 395        test_cmp output cached_expect
 396'
 397
 398test_expect_success 'test resetting the index at give paths' '
 399
 400        mkdir sub &&
 401        >sub/file1 &&
 402        >sub/file2 &&
 403        git update-index --add sub/file1 sub/file2 &&
 404        T=$(git write-tree) &&
 405        test_must_fail git reset HEAD sub/file2 &&
 406        U=$(git write-tree) &&
 407        echo "$T" &&
 408        echo "$U" &&
 409        test_must_fail git diff-index --cached --exit-code "$T" &&
 410        test "$T" != "$U"
 411
 412'
 413
 414test_expect_success 'resetting an unmodified path is a no-op' '
 415        git reset --hard &&
 416        git reset -- file1 &&
 417        git diff-files --exit-code &&
 418        git diff-index --cached --exit-code HEAD
 419'
 420
 421cat > expect << EOF
 422Unstaged changes after reset:
 423M       file2
 424EOF
 425
 426test_expect_success '--mixed refreshes the index' '
 427        echo 123 >> file2 &&
 428        git reset --mixed HEAD > output &&
 429        test_i18ncmp expect output
 430'
 431
 432test_expect_success 'disambiguation (1)' '
 433
 434        git reset --hard &&
 435        >secondfile &&
 436        git add secondfile &&
 437        test_must_fail git reset secondfile &&
 438        test -z "$(git diff --cached --name-only)" &&
 439        test -f secondfile &&
 440        test ! -s secondfile
 441
 442'
 443
 444test_expect_success 'disambiguation (2)' '
 445
 446        git reset --hard &&
 447        >secondfile &&
 448        git add secondfile &&
 449        rm -f secondfile &&
 450        test_must_fail git reset secondfile &&
 451        test -n "$(git diff --cached --name-only -- secondfile)" &&
 452        test ! -f secondfile
 453
 454'
 455
 456test_expect_success 'disambiguation (3)' '
 457
 458        git reset --hard &&
 459        >secondfile &&
 460        git add secondfile &&
 461        rm -f secondfile &&
 462        test_must_fail git reset HEAD secondfile &&
 463        test -z "$(git diff --cached --name-only)" &&
 464        test ! -f secondfile
 465
 466'
 467
 468test_expect_success 'disambiguation (4)' '
 469
 470        git reset --hard &&
 471        >secondfile &&
 472        git add secondfile &&
 473        rm -f secondfile &&
 474        test_must_fail git reset -- secondfile &&
 475        test -z "$(git diff --cached --name-only)" &&
 476        test ! -f secondfile
 477'
 478
 479test_done