t / t7102-reset.shon commit Add strbuf_read_file(). (a9390b9)
   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 | 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
  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        ! git reset aaaaaa &&
  56        ! git reset --mixed aaaaaa &&
  57        ! git reset --soft aaaaaa &&
  58        ! git reset --hard aaaaaa &&
  59        check_changes 3ec39651e7f44ea531a5de18a9fa791c0fd370fc
  60'
  61
  62test_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
  71test_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
  83test_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
 107test_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
 130test_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
 151cat >.cached_expect <<EOF
 152diff --git a/secondfile b/secondfile
 153index 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
 160EOF
 161cat >.cat_expect <<EOF
 162secondfile:
 1631st line 2nd file
 1642nd line 2nd file
 165EOF
 166test_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
 175cat >.cat_expect <<EOF
 176secondfile:
 1771st line 2nd file
 1782nd line 2nd file
 1793rd line 2nd file
 180EOF
 181test_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
 192cat >.cat_expect <<EOF
 193first:
 1941st file
 1952nd line 1st file
 196second:
 1972nd file
 198EOF
 199test_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
 208cat >.cached_expect <<EOF
 209diff --git a/first b/first
 210deleted file mode 100644
 211index 8206c22..0000000
 212--- a/first
 213+++ /dev/null
 214@@ -1,2 +0,0 @@
 215-1st file
 216-2nd line 1st file
 217diff --git a/second b/second
 218deleted file mode 100644
 219index 1bbba79..0000000
 220--- a/second
 221+++ /dev/null
 222@@ -1 +0,0 @@
 223-2nd file
 224diff --git a/secondfile b/secondfile
 225new file mode 100644
 226index 0000000..44c5b58
 227--- /dev/null
 228+++ b/secondfile
 229@@ -0,0 +1,2 @@
 230+1st line 2nd file
 231+2nd line 2nd file
 232EOF
 233cat >.cat_expect <<EOF
 234secondfile:
 2351st line 2nd file
 2362nd line 2nd file
 237EOF
 238test_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
 249cat >.diff_expect <<EOF
 250diff --git a/first b/first
 251deleted file mode 100644
 252index 8206c22..0000000
 253--- a/first
 254+++ /dev/null
 255@@ -1,2 +0,0 @@
 256-1st file
 257-2nd line 1st file
 258diff --git a/second b/second
 259deleted file mode 100644
 260index 1bbba79..0000000
 261--- a/second
 262+++ /dev/null
 263@@ -1 +0,0 @@
 264-2nd file
 265EOF
 266>.cached_expect
 267cat >.cat_expect <<EOF
 268secondfile:
 2691st line 2nd file
 2702nd line 2nd file
 271EOF
 272test_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
 281cat >.cat_expect <<EOF
 282secondfile:
 2831st line 2nd file
 2842nd line 2nd file
 285EOF
 286test_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
 302cat >.cat_expect <<EOF
 303secondfile:
 3041st line 2nd file
 3052nd line 2nd file
 3063rd line in branch2
 307EOF
 308test_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
 327cat >.cat_expect <<EOF
 328secondfile:
 3291st line 2nd file
 3302nd line 2nd file
 331EOF
 332test_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
 346cat > expect << EOF
 347diff --git a/file1 b/file1
 348index d00491f..7ed6ff8 100644
 349--- a/file1
 350+++ b/file1
 351@@ -1 +1 @@
 352-1
 353+5
 354diff --git a/file2 b/file2
 355deleted file mode 100644
 356index 0cfbf08..0000000
 357--- a/file2
 358+++ /dev/null
 359@@ -1 +0,0 @@
 360-2
 361EOF
 362cat > cached_expect << EOF
 363diff --git a/file4 b/file4
 364new file mode 100644
 365index 0000000..b8626c4
 366--- /dev/null
 367+++ b/file4
 368@@ -0,0 +1 @@
 369+4
 370EOF
 371test_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
 389test_expect_success 'test resetting the index at give paths' '
 390
 391        mkdir sub &&
 392        >sub/file1 &&
 393        >sub/file2 &&
 394        git update-index --add sub/file1 sub/file2 &&
 395        T=$(git write-tree) &&
 396        ! git reset HEAD sub/file2 &&
 397        U=$(git write-tree) &&
 398        echo "$T" &&
 399        echo "$U" &&
 400        ! git diff-index --cached --exit-code "$T" &&
 401        test "$T" != "$U"
 402
 403'
 404
 405test_done