t / t3903-stash.shon commit t5310: increase the number of bitmapped commits (1ac96cd)
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Johannes E Schindelin
   4#
   5
   6test_description='Test git stash'
   7
   8. ./test-lib.sh
   9
  10test_expect_success 'stash some dirty working directory' '
  11        echo 1 >file &&
  12        git add file &&
  13        echo unrelated >other-file &&
  14        git add other-file &&
  15        test_tick &&
  16        git commit -m initial &&
  17        echo 2 >file &&
  18        git add file &&
  19        echo 3 >file &&
  20        test_tick &&
  21        git stash &&
  22        git diff-files --quiet &&
  23        git diff-index --cached --quiet HEAD
  24'
  25
  26cat >expect <<EOF
  27diff --git a/file b/file
  28index 0cfbf08..00750ed 100644
  29--- a/file
  30+++ b/file
  31@@ -1 +1 @@
  32-2
  33+3
  34EOF
  35
  36test_expect_success 'parents of stash' '
  37        test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
  38        git diff stash^2..stash >output &&
  39        test_cmp expect output
  40'
  41
  42test_expect_success 'applying bogus stash does nothing' '
  43        test_must_fail git stash apply stash@{1} &&
  44        echo 1 >expect &&
  45        test_cmp expect file
  46'
  47
  48test_expect_success 'apply does not need clean working directory' '
  49        echo 4 >other-file &&
  50        git stash apply &&
  51        echo 3 >expect &&
  52        test_cmp expect file
  53'
  54
  55test_expect_success 'apply does not clobber working directory changes' '
  56        git reset --hard &&
  57        echo 4 >file &&
  58        test_must_fail git stash apply &&
  59        echo 4 >expect &&
  60        test_cmp expect file
  61'
  62
  63test_expect_success 'apply stashed changes' '
  64        git reset --hard &&
  65        echo 5 >other-file &&
  66        git add other-file &&
  67        test_tick &&
  68        git commit -m other-file &&
  69        git stash apply &&
  70        test 3 = $(cat file) &&
  71        test 1 = $(git show :file) &&
  72        test 1 = $(git show HEAD:file)
  73'
  74
  75test_expect_success 'apply stashed changes (including index)' '
  76        git reset --hard HEAD^ &&
  77        echo 6 >other-file &&
  78        git add other-file &&
  79        test_tick &&
  80        git commit -m other-file &&
  81        git stash apply --index &&
  82        test 3 = $(cat file) &&
  83        test 2 = $(git show :file) &&
  84        test 1 = $(git show HEAD:file)
  85'
  86
  87test_expect_success 'unstashing in a subdirectory' '
  88        git reset --hard HEAD &&
  89        mkdir subdir &&
  90        (
  91                cd subdir &&
  92                git stash apply
  93        )
  94'
  95
  96test_expect_success 'stash drop complains of extra options' '
  97        test_must_fail git stash drop --foo
  98'
  99
 100test_expect_success 'drop top stash' '
 101        git reset --hard &&
 102        git stash list >expected &&
 103        echo 7 >file &&
 104        git stash &&
 105        git stash drop &&
 106        git stash list >actual &&
 107        test_cmp expected actual &&
 108        git stash apply &&
 109        test 3 = $(cat file) &&
 110        test 1 = $(git show :file) &&
 111        test 1 = $(git show HEAD:file)
 112'
 113
 114test_expect_success 'drop middle stash' '
 115        git reset --hard &&
 116        echo 8 >file &&
 117        git stash &&
 118        echo 9 >file &&
 119        git stash &&
 120        git stash drop stash@{1} &&
 121        test 2 = $(git stash list | wc -l) &&
 122        git stash apply &&
 123        test 9 = $(cat file) &&
 124        test 1 = $(git show :file) &&
 125        test 1 = $(git show HEAD:file) &&
 126        git reset --hard &&
 127        git stash drop &&
 128        git stash apply &&
 129        test 3 = $(cat file) &&
 130        test 1 = $(git show :file) &&
 131        test 1 = $(git show HEAD:file)
 132'
 133
 134test_expect_success 'drop middle stash by index' '
 135        git reset --hard &&
 136        echo 8 >file &&
 137        git stash &&
 138        echo 9 >file &&
 139        git stash &&
 140        git stash drop 1 &&
 141        test 2 = $(git stash list | wc -l) &&
 142        git stash apply &&
 143        test 9 = $(cat file) &&
 144        test 1 = $(git show :file) &&
 145        test 1 = $(git show HEAD:file) &&
 146        git reset --hard &&
 147        git stash drop &&
 148        git stash apply &&
 149        test 3 = $(cat file) &&
 150        test 1 = $(git show :file) &&
 151        test 1 = $(git show HEAD:file)
 152'
 153
 154test_expect_success 'stash pop' '
 155        git reset --hard &&
 156        git stash pop &&
 157        test 3 = $(cat file) &&
 158        test 1 = $(git show :file) &&
 159        test 1 = $(git show HEAD:file) &&
 160        test 0 = $(git stash list | wc -l)
 161'
 162
 163cat >expect <<EOF
 164diff --git a/file2 b/file2
 165new file mode 100644
 166index 0000000..1fe912c
 167--- /dev/null
 168+++ b/file2
 169@@ -0,0 +1 @@
 170+bar2
 171EOF
 172
 173cat >expect1 <<EOF
 174diff --git a/file b/file
 175index 257cc56..5716ca5 100644
 176--- a/file
 177+++ b/file
 178@@ -1 +1 @@
 179-foo
 180+bar
 181EOF
 182
 183cat >expect2 <<EOF
 184diff --git a/file b/file
 185index 7601807..5716ca5 100644
 186--- a/file
 187+++ b/file
 188@@ -1 +1 @@
 189-baz
 190+bar
 191diff --git a/file2 b/file2
 192new file mode 100644
 193index 0000000..1fe912c
 194--- /dev/null
 195+++ b/file2
 196@@ -0,0 +1 @@
 197+bar2
 198EOF
 199
 200test_expect_success 'stash branch' '
 201        echo foo >file &&
 202        git commit file -m first &&
 203        echo bar >file &&
 204        echo bar2 >file2 &&
 205        git add file2 &&
 206        git stash &&
 207        echo baz >file &&
 208        git commit file -m second &&
 209        git stash branch stashbranch &&
 210        test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
 211        test $(git rev-parse HEAD) = $(git rev-parse master^) &&
 212        git diff --cached >output &&
 213        test_cmp expect output &&
 214        git diff >output &&
 215        test_cmp expect1 output &&
 216        git add file &&
 217        git commit -m alternate\ second &&
 218        git diff master..stashbranch >output &&
 219        test_cmp output expect2 &&
 220        test 0 = $(git stash list | wc -l)
 221'
 222
 223test_expect_success 'apply -q is quiet' '
 224        echo foo >file &&
 225        git stash &&
 226        git stash apply -q >output.out 2>&1 &&
 227        test_must_be_empty output.out
 228'
 229
 230test_expect_success 'save -q is quiet' '
 231        git stash save --quiet >output.out 2>&1 &&
 232        test_must_be_empty output.out
 233'
 234
 235test_expect_success 'pop -q is quiet' '
 236        git stash pop -q >output.out 2>&1 &&
 237        test_must_be_empty output.out
 238'
 239
 240test_expect_success 'pop -q --index works and is quiet' '
 241        echo foo >file &&
 242        git add file &&
 243        git stash save --quiet &&
 244        git stash pop -q --index >output.out 2>&1 &&
 245        test foo = "$(git show :file)" &&
 246        test_must_be_empty output.out
 247'
 248
 249test_expect_success 'drop -q is quiet' '
 250        git stash &&
 251        git stash drop -q >output.out 2>&1 &&
 252        test_must_be_empty output.out
 253'
 254
 255test_expect_success 'stash -k' '
 256        echo bar3 >file &&
 257        echo bar4 >file2 &&
 258        git add file2 &&
 259        git stash -k &&
 260        test bar,bar4 = $(cat file),$(cat file2)
 261'
 262
 263test_expect_success 'stash --no-keep-index' '
 264        echo bar33 >file &&
 265        echo bar44 >file2 &&
 266        git add file2 &&
 267        git stash --no-keep-index &&
 268        test bar,bar2 = $(cat file),$(cat file2)
 269'
 270
 271test_expect_success 'stash --invalid-option' '
 272        echo bar5 >file &&
 273        echo bar6 >file2 &&
 274        git add file2 &&
 275        test_must_fail git stash --invalid-option &&
 276        test_must_fail git stash save --invalid-option &&
 277        test bar5,bar6 = $(cat file),$(cat file2)
 278'
 279
 280test_expect_success 'stash an added file' '
 281        git reset --hard &&
 282        echo new >file3 &&
 283        git add file3 &&
 284        git stash save "added file" &&
 285        ! test -r file3 &&
 286        git stash apply &&
 287        test new = "$(cat file3)"
 288'
 289
 290test_expect_success 'stash --intent-to-add file' '
 291        git reset --hard &&
 292        echo new >file4 &&
 293        git add --intent-to-add file4 &&
 294        test_when_finished "git rm -f file4" &&
 295        test_must_fail git stash
 296'
 297
 298test_expect_success 'stash rm then recreate' '
 299        git reset --hard &&
 300        git rm file &&
 301        echo bar7 >file &&
 302        git stash save "rm then recreate" &&
 303        test bar = "$(cat file)" &&
 304        git stash apply &&
 305        test bar7 = "$(cat file)"
 306'
 307
 308test_expect_success 'stash rm and ignore' '
 309        git reset --hard &&
 310        git rm file &&
 311        echo file >.gitignore &&
 312        git stash save "rm and ignore" &&
 313        test bar = "$(cat file)" &&
 314        test file = "$(cat .gitignore)" &&
 315        git stash apply &&
 316        ! test -r file &&
 317        test file = "$(cat .gitignore)"
 318'
 319
 320test_expect_success 'stash rm and ignore (stage .gitignore)' '
 321        git reset --hard &&
 322        git rm file &&
 323        echo file >.gitignore &&
 324        git add .gitignore &&
 325        git stash save "rm and ignore (stage .gitignore)" &&
 326        test bar = "$(cat file)" &&
 327        ! test -r .gitignore &&
 328        git stash apply &&
 329        ! test -r file &&
 330        test file = "$(cat .gitignore)"
 331'
 332
 333test_expect_success SYMLINKS 'stash file to symlink' '
 334        git reset --hard &&
 335        rm file &&
 336        ln -s file2 file &&
 337        git stash save "file to symlink" &&
 338        test -f file &&
 339        test bar = "$(cat file)" &&
 340        git stash apply &&
 341        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 342'
 343
 344test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
 345        git reset --hard &&
 346        git rm file &&
 347        ln -s file2 file &&
 348        git stash save "file to symlink (stage rm)" &&
 349        test -f file &&
 350        test bar = "$(cat file)" &&
 351        git stash apply &&
 352        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 353'
 354
 355test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
 356        git reset --hard &&
 357        rm file &&
 358        ln -s file2 file &&
 359        git add file &&
 360        git stash save "file to symlink (full stage)" &&
 361        test -f file &&
 362        test bar = "$(cat file)" &&
 363        git stash apply &&
 364        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 365'
 366
 367# This test creates a commit with a symlink used for the following tests
 368
 369test_expect_success 'stash symlink to file' '
 370        git reset --hard &&
 371        test_ln_s_add file filelink &&
 372        git commit -m "Add symlink" &&
 373        rm filelink &&
 374        cp file filelink &&
 375        git stash save "symlink to file"
 376'
 377
 378test_expect_success SYMLINKS 'this must have re-created the symlink' '
 379        test -h filelink &&
 380        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
 381'
 382
 383test_expect_success 'unstash must re-create the file' '
 384        git stash apply &&
 385        ! test -h filelink &&
 386        test bar = "$(cat file)"
 387'
 388
 389test_expect_success 'stash symlink to file (stage rm)' '
 390        git reset --hard &&
 391        git rm filelink &&
 392        cp file filelink &&
 393        git stash save "symlink to file (stage rm)"
 394'
 395
 396test_expect_success SYMLINKS 'this must have re-created the symlink' '
 397        test -h filelink &&
 398        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
 399'
 400
 401test_expect_success 'unstash must re-create the file' '
 402        git stash apply &&
 403        ! test -h filelink &&
 404        test bar = "$(cat file)"
 405'
 406
 407test_expect_success 'stash symlink to file (full stage)' '
 408        git reset --hard &&
 409        rm filelink &&
 410        cp file filelink &&
 411        git add filelink &&
 412        git stash save "symlink to file (full stage)"
 413'
 414
 415test_expect_success SYMLINKS 'this must have re-created the symlink' '
 416        test -h filelink &&
 417        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
 418'
 419
 420test_expect_success 'unstash must re-create the file' '
 421        git stash apply &&
 422        ! test -h filelink &&
 423        test bar = "$(cat file)"
 424'
 425
 426test_expect_failure 'stash directory to file' '
 427        git reset --hard &&
 428        mkdir dir &&
 429        echo foo >dir/file &&
 430        git add dir/file &&
 431        git commit -m "Add file in dir" &&
 432        rm -fr dir &&
 433        echo bar >dir &&
 434        git stash save "directory to file" &&
 435        test -d dir &&
 436        test foo = "$(cat dir/file)" &&
 437        test_must_fail git stash apply &&
 438        test bar = "$(cat dir)" &&
 439        git reset --soft HEAD^
 440'
 441
 442test_expect_failure 'stash file to directory' '
 443        git reset --hard &&
 444        rm file &&
 445        mkdir file &&
 446        echo foo >file/file &&
 447        git stash save "file to directory" &&
 448        test -f file &&
 449        test bar = "$(cat file)" &&
 450        git stash apply &&
 451        test -f file/file &&
 452        test foo = "$(cat file/file)"
 453'
 454
 455test_expect_success 'giving too many ref arguments does not modify files' '
 456        git stash clear &&
 457        test_when_finished "git reset --hard HEAD" &&
 458        echo foo >file2 &&
 459        git stash &&
 460        echo bar >file2 &&
 461        git stash &&
 462        test-tool chmtime =123456789 file2 &&
 463        for type in apply pop "branch stash-branch"
 464        do
 465                test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
 466                test_i18ngrep "Too many revisions" err &&
 467                test 123456789 = $(test-tool chmtime -g file2) || return 1
 468        done
 469'
 470
 471test_expect_success 'drop: too many arguments errors out (does nothing)' '
 472        git stash list >expect &&
 473        test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
 474        test_i18ngrep "Too many revisions" err &&
 475        git stash list >actual &&
 476        test_cmp expect actual
 477'
 478
 479test_expect_success 'show: too many arguments errors out (does nothing)' '
 480        test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
 481        test_i18ngrep "Too many revisions" err &&
 482        test_must_be_empty out
 483'
 484
 485test_expect_success 'stash create - no changes' '
 486        git stash clear &&
 487        test_when_finished "git reset --hard HEAD" &&
 488        git reset --hard &&
 489        git stash create >actual &&
 490        test_must_be_empty actual
 491'
 492
 493test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
 494        git stash clear &&
 495        test_when_finished "git reset --hard HEAD" &&
 496        git reset --hard &&
 497        echo foo >>file &&
 498        STASH_ID=$(git stash create) &&
 499        git reset --hard &&
 500        git stash branch stash-branch ${STASH_ID} &&
 501        test_when_finished "git reset --hard HEAD && git checkout master &&
 502        git branch -D stash-branch" &&
 503        test $(git ls-files --modified | wc -l) -eq 1
 504'
 505
 506test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 507        git stash clear &&
 508        test_when_finished "git reset --hard HEAD" &&
 509        git reset --hard &&
 510        echo foo >>file &&
 511        git stash &&
 512        test_when_finished "git stash drop" &&
 513        echo bar >>file &&
 514        STASH_ID=$(git stash create) &&
 515        git reset --hard &&
 516        git stash branch stash-branch ${STASH_ID} &&
 517        test_when_finished "git reset --hard HEAD && git checkout master &&
 518        git branch -D stash-branch" &&
 519        test $(git ls-files --modified | wc -l) -eq 1
 520'
 521
 522test_expect_success 'stash branch complains with no arguments' '
 523        test_must_fail git stash branch 2>err &&
 524        test_i18ngrep "No branch name specified" err
 525'
 526
 527test_expect_success 'stash show format defaults to --stat' '
 528        git stash clear &&
 529        test_when_finished "git reset --hard HEAD" &&
 530        git reset --hard &&
 531        echo foo >>file &&
 532        git stash &&
 533        test_when_finished "git stash drop" &&
 534        echo bar >>file &&
 535        STASH_ID=$(git stash create) &&
 536        git reset --hard &&
 537        cat >expected <<-EOF &&
 538         file | 1 +
 539         1 file changed, 1 insertion(+)
 540        EOF
 541        git stash show ${STASH_ID} >actual &&
 542        test_i18ncmp expected actual
 543'
 544
 545test_expect_success 'stash show - stashes on stack, stash-like argument' '
 546        git stash clear &&
 547        test_when_finished "git reset --hard HEAD" &&
 548        git reset --hard &&
 549        echo foo >>file &&
 550        git stash &&
 551        test_when_finished "git stash drop" &&
 552        echo bar >>file &&
 553        STASH_ID=$(git stash create) &&
 554        git reset --hard &&
 555        echo "1 0       file" >expected &&
 556        git stash show --numstat ${STASH_ID} >actual &&
 557        test_cmp expected actual
 558'
 559
 560test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
 561        git stash clear &&
 562        test_when_finished "git reset --hard HEAD" &&
 563        git reset --hard &&
 564        echo foo >>file &&
 565        git stash &&
 566        test_when_finished "git stash drop" &&
 567        echo bar >>file &&
 568        STASH_ID=$(git stash create) &&
 569        git reset --hard &&
 570        cat >expected <<-EOF &&
 571        diff --git a/file b/file
 572        index 7601807..935fbd3 100644
 573        --- a/file
 574        +++ b/file
 575        @@ -1 +1,2 @@
 576         baz
 577        +bar
 578        EOF
 579        git stash show -p ${STASH_ID} >actual &&
 580        test_cmp expected actual
 581'
 582
 583test_expect_success 'stash show - no stashes on stack, stash-like argument' '
 584        git stash clear &&
 585        test_when_finished "git reset --hard HEAD" &&
 586        git reset --hard &&
 587        echo foo >>file &&
 588        STASH_ID=$(git stash create) &&
 589        git reset --hard &&
 590        echo "1 0       file" >expected &&
 591        git stash show --numstat ${STASH_ID} >actual &&
 592        test_cmp expected actual
 593'
 594
 595test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 596        git stash clear &&
 597        test_when_finished "git reset --hard HEAD" &&
 598        git reset --hard &&
 599        echo foo >>file &&
 600        STASH_ID=$(git stash create) &&
 601        git reset --hard &&
 602        cat >expected <<-EOF &&
 603        diff --git a/file b/file
 604        index 7601807..71b52c4 100644
 605        --- a/file
 606        +++ b/file
 607        @@ -1 +1,2 @@
 608         baz
 609        +foo
 610        EOF
 611        git stash show -p ${STASH_ID} >actual &&
 612        test_cmp expected actual
 613'
 614
 615test_expect_success 'stash show --patience shows diff' '
 616        git reset --hard &&
 617        echo foo >>file &&
 618        STASH_ID=$(git stash create) &&
 619        git reset --hard &&
 620        cat >expected <<-EOF &&
 621        diff --git a/file b/file
 622        index 7601807..71b52c4 100644
 623        --- a/file
 624        +++ b/file
 625        @@ -1 +1,2 @@
 626         baz
 627        +foo
 628        EOF
 629        git stash show --patience ${STASH_ID} >actual &&
 630        test_cmp expected actual
 631'
 632
 633test_expect_success 'drop: fail early if specified stash is not a stash ref' '
 634        git stash clear &&
 635        test_when_finished "git reset --hard HEAD && git stash clear" &&
 636        git reset --hard &&
 637        echo foo >file &&
 638        git stash &&
 639        echo bar >file &&
 640        git stash &&
 641        test_must_fail git stash drop $(git rev-parse stash@{0}) &&
 642        git stash pop &&
 643        test bar = "$(cat file)" &&
 644        git reset --hard HEAD
 645'
 646
 647test_expect_success 'pop: fail early if specified stash is not a stash ref' '
 648        git stash clear &&
 649        test_when_finished "git reset --hard HEAD && git stash clear" &&
 650        git reset --hard &&
 651        echo foo >file &&
 652        git stash &&
 653        echo bar >file &&
 654        git stash &&
 655        test_must_fail git stash pop $(git rev-parse stash@{0}) &&
 656        git stash pop &&
 657        test bar = "$(cat file)" &&
 658        git reset --hard HEAD
 659'
 660
 661test_expect_success 'ref with non-existent reflog' '
 662        git stash clear &&
 663        echo bar5 >file &&
 664        echo bar6 >file2 &&
 665        git add file2 &&
 666        git stash &&
 667        test_must_fail git rev-parse --quiet --verify does-not-exist &&
 668        test_must_fail git stash drop does-not-exist &&
 669        test_must_fail git stash drop does-not-exist@{0} &&
 670        test_must_fail git stash pop does-not-exist &&
 671        test_must_fail git stash pop does-not-exist@{0} &&
 672        test_must_fail git stash apply does-not-exist &&
 673        test_must_fail git stash apply does-not-exist@{0} &&
 674        test_must_fail git stash show does-not-exist &&
 675        test_must_fail git stash show does-not-exist@{0} &&
 676        test_must_fail git stash branch tmp does-not-exist &&
 677        test_must_fail git stash branch tmp does-not-exist@{0} &&
 678        git stash drop
 679'
 680
 681test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 682        git stash clear &&
 683        test_must_fail git stash drop stash@{0} &&
 684        echo bar5 >file &&
 685        echo bar6 >file2 &&
 686        git add file2 &&
 687        git stash &&
 688        test_must_fail git stash drop stash@{1} &&
 689        test_must_fail git stash pop stash@{1} &&
 690        test_must_fail git stash apply stash@{1} &&
 691        test_must_fail git stash show stash@{1} &&
 692        test_must_fail git stash branch tmp stash@{1} &&
 693        git stash drop
 694'
 695
 696test_expect_success 'invalid ref of the form "n", n >= N' '
 697        git stash clear &&
 698        test_must_fail git stash drop 0 &&
 699        echo bar5 >file &&
 700        echo bar6 >file2 &&
 701        git add file2 &&
 702        git stash &&
 703        test_must_fail git stash drop 1 &&
 704        test_must_fail git stash pop 1 &&
 705        test_must_fail git stash apply 1 &&
 706        test_must_fail git stash show 1 &&
 707        test_must_fail git stash branch tmp 1 &&
 708        git stash drop
 709'
 710
 711test_expect_success 'branch: do not drop the stash if the branch exists' '
 712        git stash clear &&
 713        echo foo >file &&
 714        git add file &&
 715        git commit -m initial &&
 716        echo bar >file &&
 717        git stash &&
 718        test_must_fail git stash branch master stash@{0} &&
 719        git rev-parse stash@{0} --
 720'
 721
 722test_expect_success 'branch: should not drop the stash if the apply fails' '
 723        git stash clear &&
 724        git reset HEAD~1 --hard &&
 725        echo foo >file &&
 726        git add file &&
 727        git commit -m initial &&
 728        echo bar >file &&
 729        git stash &&
 730        echo baz >file &&
 731        test_when_finished "git checkout master" &&
 732        test_must_fail git stash branch new_branch stash@{0} &&
 733        git rev-parse stash@{0} --
 734'
 735
 736test_expect_success 'apply: show same status as git status (relative to ./)' '
 737        git stash clear &&
 738        echo 1 >subdir/subfile1 &&
 739        echo 2 >subdir/subfile2 &&
 740        git add subdir/subfile1 &&
 741        git commit -m subdir &&
 742        (
 743                cd subdir &&
 744                echo x >subfile1 &&
 745                echo x >../file &&
 746                git status >../expect &&
 747                git stash &&
 748                sane_unset GIT_MERGE_VERBOSITY &&
 749                git stash apply
 750        ) |
 751        sed -e 1d >actual && # drop "Saved..."
 752        test_i18ncmp expect actual
 753'
 754
 755cat >expect <<EOF
 756diff --git a/HEAD b/HEAD
 757new file mode 100644
 758index 0000000..fe0cbee
 759--- /dev/null
 760+++ b/HEAD
 761@@ -0,0 +1 @@
 762+file-not-a-ref
 763EOF
 764
 765test_expect_success 'stash where working directory contains "HEAD" file' '
 766        git stash clear &&
 767        git reset --hard &&
 768        echo file-not-a-ref >HEAD &&
 769        git add HEAD &&
 770        test_tick &&
 771        git stash &&
 772        git diff-files --quiet &&
 773        git diff-index --cached --quiet HEAD &&
 774        test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
 775        git diff stash^..stash >output &&
 776        test_cmp expect output
 777'
 778
 779test_expect_success 'store called with invalid commit' '
 780        test_must_fail git stash store foo
 781'
 782
 783test_expect_success 'store updates stash ref and reflog' '
 784        git stash clear &&
 785        git reset --hard &&
 786        echo quux >bazzy &&
 787        git add bazzy &&
 788        STASH_ID=$(git stash create) &&
 789        git reset --hard &&
 790        test_path_is_missing bazzy &&
 791        git stash store -m quuxery $STASH_ID &&
 792        test $(git rev-parse stash) = $STASH_ID &&
 793        git reflog --format=%H stash| grep $STASH_ID &&
 794        git stash pop &&
 795        grep quux bazzy
 796'
 797
 798test_expect_success 'handle stash specification with spaces' '
 799        git stash clear &&
 800        echo pig >file &&
 801        git stash &&
 802        stamp=$(git log -g --format="%cd" -1 refs/stash) &&
 803        test_tick &&
 804        echo cow >file &&
 805        git stash &&
 806        git stash apply "stash@{$stamp}" &&
 807        grep pig file
 808'
 809
 810test_expect_success 'setup stash with index and worktree changes' '
 811        git stash clear &&
 812        git reset --hard &&
 813        echo index >file &&
 814        git add file &&
 815        echo working >file &&
 816        git stash
 817'
 818
 819test_expect_success 'stash list implies --first-parent -m' '
 820        cat >expect <<-EOF &&
 821        stash@{0}
 822
 823        diff --git a/file b/file
 824        index 257cc56..d26b33d 100644
 825        --- a/file
 826        +++ b/file
 827        @@ -1 +1 @@
 828        -foo
 829        +working
 830        EOF
 831        git stash list --format=%gd -p >actual &&
 832        test_cmp expect actual
 833'
 834
 835test_expect_success 'stash list --cc shows combined diff' '
 836        cat >expect <<-\EOF &&
 837        stash@{0}
 838
 839        diff --cc file
 840        index 257cc56,9015a7a..d26b33d
 841        --- a/file
 842        +++ b/file
 843        @@@ -1,1 -1,1 +1,1 @@@
 844        - foo
 845         -index
 846        ++working
 847        EOF
 848        git stash list --format=%gd -p --cc >actual &&
 849        test_cmp expect actual
 850'
 851
 852test_expect_success 'stash is not confused by partial renames' '
 853        mv file renamed &&
 854        git add renamed &&
 855        git stash &&
 856        git stash apply &&
 857        test_path_is_file renamed &&
 858        test_path_is_missing file
 859'
 860
 861test_expect_success 'push -m shows right message' '
 862        >foo &&
 863        git add foo &&
 864        git stash push -m "test message" &&
 865        echo "stash@{0}: On master: test message" >expect &&
 866        git stash list -1 >actual &&
 867        test_cmp expect actual
 868'
 869
 870test_expect_success 'push -m also works without space' '
 871        >foo &&
 872        git add foo &&
 873        git stash push -m"unspaced test message" &&
 874        echo "stash@{0}: On master: unspaced test message" >expect &&
 875        git stash list -1 >actual &&
 876        test_cmp expect actual
 877'
 878
 879test_expect_success 'store -m foo shows right message' '
 880        git stash clear &&
 881        git reset --hard &&
 882        echo quux >bazzy &&
 883        git add bazzy &&
 884        STASH_ID=$(git stash create) &&
 885        git stash store -m "store m" $STASH_ID &&
 886        echo "stash@{0}: store m" >expect &&
 887        git stash list -1 >actual &&
 888        test_cmp expect actual
 889'
 890
 891test_expect_success 'store -mfoo shows right message' '
 892        git stash clear &&
 893        git reset --hard &&
 894        echo quux >bazzy &&
 895        git add bazzy &&
 896        STASH_ID=$(git stash create) &&
 897        git stash store -m"store mfoo" $STASH_ID &&
 898        echo "stash@{0}: store mfoo" >expect &&
 899        git stash list -1 >actual &&
 900        test_cmp expect actual
 901'
 902
 903test_expect_success 'store --message=foo shows right message' '
 904        git stash clear &&
 905        git reset --hard &&
 906        echo quux >bazzy &&
 907        git add bazzy &&
 908        STASH_ID=$(git stash create) &&
 909        git stash store --message="store message=foo" $STASH_ID &&
 910        echo "stash@{0}: store message=foo" >expect &&
 911        git stash list -1 >actual &&
 912        test_cmp expect actual
 913'
 914
 915test_expect_success 'store --message foo shows right message' '
 916        git stash clear &&
 917        git reset --hard &&
 918        echo quux >bazzy &&
 919        git add bazzy &&
 920        STASH_ID=$(git stash create) &&
 921        git stash store --message "store message foo" $STASH_ID &&
 922        echo "stash@{0}: store message foo" >expect &&
 923        git stash list -1 >actual &&
 924        test_cmp expect actual
 925'
 926
 927test_expect_success 'push -mfoo uses right message' '
 928        >foo &&
 929        git add foo &&
 930        git stash push -m"test mfoo" &&
 931        echo "stash@{0}: On master: test mfoo" >expect &&
 932        git stash list -1 >actual &&
 933        test_cmp expect actual
 934'
 935
 936test_expect_success 'push --message foo is synonym for -mfoo' '
 937        >foo &&
 938        git add foo &&
 939        git stash push --message "test message foo" &&
 940        echo "stash@{0}: On master: test message foo" >expect &&
 941        git stash list -1 >actual &&
 942        test_cmp expect actual
 943'
 944
 945test_expect_success 'push --message=foo is synonym for -mfoo' '
 946        >foo &&
 947        git add foo &&
 948        git stash push --message="test message=foo" &&
 949        echo "stash@{0}: On master: test message=foo" >expect &&
 950        git stash list -1 >actual &&
 951        test_cmp expect actual
 952'
 953
 954test_expect_success 'push -m shows right message' '
 955        >foo &&
 956        git add foo &&
 957        git stash push -m "test m foo" &&
 958        echo "stash@{0}: On master: test m foo" >expect &&
 959        git stash list -1 >actual &&
 960        test_cmp expect actual
 961'
 962
 963test_expect_success 'create stores correct message' '
 964        >foo &&
 965        git add foo &&
 966        STASH_ID=$(git stash create "create test message") &&
 967        echo "On master: create test message" >expect &&
 968        git show --pretty=%s -s ${STASH_ID} >actual &&
 969        test_cmp expect actual
 970'
 971
 972test_expect_success 'create with multiple arguments for the message' '
 973        >foo &&
 974        git add foo &&
 975        STASH_ID=$(git stash create test untracked) &&
 976        echo "On master: test untracked" >expect &&
 977        git show --pretty=%s -s ${STASH_ID} >actual &&
 978        test_cmp expect actual
 979'
 980
 981test_expect_success 'create in a detached state' '
 982        test_when_finished "git checkout master" &&
 983        git checkout HEAD~1 &&
 984        >foo &&
 985        git add foo &&
 986        STASH_ID=$(git stash create) &&
 987        HEAD_ID=$(git rev-parse --short HEAD) &&
 988        echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
 989        git show --pretty=%s -s ${STASH_ID} >actual &&
 990        test_cmp expect actual
 991'
 992
 993test_expect_success 'stash -- <pathspec> stashes and restores the file' '
 994        >foo &&
 995        >bar &&
 996        git add foo bar &&
 997        git stash push -- foo &&
 998        test_path_is_file bar &&
 999        test_path_is_missing foo &&
1000        git stash pop &&
1001        test_path_is_file foo &&
1002        test_path_is_file bar
1003'
1004
1005test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1006        mkdir sub &&
1007        >foo &&
1008        >bar &&
1009        git add foo bar &&
1010        (
1011                cd sub &&
1012                git stash push -- ../foo
1013        ) &&
1014        test_path_is_file bar &&
1015        test_path_is_missing foo &&
1016        git stash pop &&
1017        test_path_is_file foo &&
1018        test_path_is_file bar
1019'
1020
1021test_expect_success 'stash with multiple pathspec arguments' '
1022        >foo &&
1023        >bar &&
1024        >extra &&
1025        git add foo bar extra &&
1026        git stash push -- foo bar &&
1027        test_path_is_missing bar &&
1028        test_path_is_missing foo &&
1029        test_path_is_file extra &&
1030        git stash pop &&
1031        test_path_is_file foo &&
1032        test_path_is_file bar &&
1033        test_path_is_file extra
1034'
1035
1036test_expect_success 'stash with file including $IFS character' '
1037        >"foo bar" &&
1038        >foo &&
1039        >bar &&
1040        git add foo* &&
1041        git stash push -- "foo b*" &&
1042        test_path_is_missing "foo bar" &&
1043        test_path_is_file foo &&
1044        test_path_is_file bar &&
1045        git stash pop &&
1046        test_path_is_file "foo bar" &&
1047        test_path_is_file foo &&
1048        test_path_is_file bar
1049'
1050
1051test_expect_success 'stash with pathspec matching multiple paths' '
1052       echo original >file &&
1053       echo original >other-file &&
1054       git commit -m "two" file other-file &&
1055       echo modified >file &&
1056       echo modified >other-file &&
1057       git stash push -- "*file" &&
1058       echo original >expect &&
1059       test_cmp expect file &&
1060       test_cmp expect other-file &&
1061       git stash pop &&
1062       echo modified >expect &&
1063       test_cmp expect file &&
1064       test_cmp expect other-file
1065'
1066
1067test_expect_success 'stash push -p with pathspec shows no changes only once' '
1068        >foo &&
1069        git add foo &&
1070        git commit -m "tmp" &&
1071        git stash push -p foo >actual &&
1072        echo "No local changes to save" >expect &&
1073        git reset --hard HEAD~ &&
1074        test_i18ncmp expect actual
1075'
1076
1077test_expect_success 'push <pathspec>: show no changes when there are none' '
1078        >foo &&
1079        git add foo &&
1080        git commit -m "tmp" &&
1081        git stash push foo >actual &&
1082        echo "No local changes to save" >expect &&
1083        git reset --hard HEAD~ &&
1084        test_i18ncmp expect actual
1085'
1086
1087test_expect_success 'push: <pathspec> not in the repository errors out' '
1088        >untracked &&
1089        test_must_fail git stash push untracked &&
1090        test_path_is_file untracked
1091'
1092
1093test_expect_success 'push: -q is quiet with changes' '
1094        >foo &&
1095        git add foo &&
1096        git stash push -q >output 2>&1 &&
1097        test_must_be_empty output
1098'
1099
1100test_expect_success 'push: -q is quiet with no changes' '
1101        git stash push -q >output 2>&1 &&
1102        test_must_be_empty output
1103'
1104
1105test_expect_success 'push: -q is quiet even if there is no initial commit' '
1106        git init foo_dir &&
1107        test_when_finished rm -rf foo_dir &&
1108        (
1109                cd foo_dir &&
1110                >bar &&
1111                test_must_fail git stash push -q >output 2>&1 &&
1112                test_must_be_empty output
1113        )
1114'
1115
1116test_expect_success 'untracked files are left in place when -u is not given' '
1117        >file &&
1118        git add file &&
1119        >untracked &&
1120        git stash push file &&
1121        test_path_is_file untracked
1122'
1123
1124test_expect_success 'stash without verb with pathspec' '
1125        >"foo bar" &&
1126        >foo &&
1127        >bar &&
1128        git add foo* &&
1129        git stash -- "foo b*" &&
1130        test_path_is_missing "foo bar" &&
1131        test_path_is_file foo &&
1132        test_path_is_file bar &&
1133        git stash pop &&
1134        test_path_is_file "foo bar" &&
1135        test_path_is_file foo &&
1136        test_path_is_file bar
1137'
1138
1139test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1140        git reset &&
1141        >foo &&
1142        >bar &&
1143        git add foo bar &&
1144        git commit -m "test" &&
1145        echo "foo" >foo &&
1146        echo "bar" >bar &&
1147        git stash -k -- foo &&
1148        test "",bar = $(cat foo),$(cat bar) &&
1149        git stash pop &&
1150        test foo,bar = $(cat foo),$(cat bar)
1151'
1152
1153test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1154        git reset &&
1155        >subdir/untracked &&
1156        >subdir/tracked1 &&
1157        >subdir/tracked2 &&
1158        git add subdir/tracked* &&
1159        git stash -- subdir/ &&
1160        test_path_is_missing subdir/tracked1 &&
1161        test_path_is_missing subdir/tracked2 &&
1162        test_path_is_file subdir/untracked &&
1163        git stash pop &&
1164        test_path_is_file subdir/tracked1 &&
1165        test_path_is_file subdir/tracked2 &&
1166        test_path_is_file subdir/untracked
1167'
1168
1169test_expect_success 'stash -- <subdir> works with binary files' '
1170        git reset &&
1171        >subdir/untracked &&
1172        >subdir/tracked &&
1173        cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1174        git add subdir/tracked* &&
1175        git stash -- subdir/ &&
1176        test_path_is_missing subdir/tracked &&
1177        test_path_is_missing subdir/tracked-binary &&
1178        test_path_is_file subdir/untracked &&
1179        git stash pop &&
1180        test_path_is_file subdir/tracked &&
1181        test_path_is_file subdir/tracked-binary &&
1182        test_path_is_file subdir/untracked
1183'
1184
1185test_expect_success 'stash with user.name and user.email set works' '
1186        test_config user.name "A U Thor" &&
1187        test_config user.email "a.u@thor" &&
1188        git stash
1189'
1190
1191test_expect_success 'stash works when user.name and user.email are not set' '
1192        git reset &&
1193        >1 &&
1194        git add 1 &&
1195        echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1196        git stash &&
1197        git show -s --format="%an <%ae>" refs/stash >actual &&
1198        test_cmp expect actual &&
1199        >2 &&
1200        git add 2 &&
1201        test_config user.useconfigonly true &&
1202        test_config stash.usebuiltin true &&
1203        (
1204                sane_unset GIT_AUTHOR_NAME &&
1205                sane_unset GIT_AUTHOR_EMAIL &&
1206                sane_unset GIT_COMMITTER_NAME &&
1207                sane_unset GIT_COMMITTER_EMAIL &&
1208                test_unconfig user.email &&
1209                test_unconfig user.name &&
1210                test_must_fail git commit -m "should fail" &&
1211                echo "git stash <git@stash>" >expect &&
1212                >2 &&
1213                git stash &&
1214                git show -s --format="%an <%ae>" refs/stash >actual &&
1215                test_cmp expect actual
1216        )
1217'
1218
1219test_done