t / t3903-stash.shon commit Git 2.15 (cb5918a)
   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 output expect
  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 > stashlist1 &&
 103        echo 7 > file &&
 104        git stash &&
 105        git stash drop &&
 106        git stash list > stashlist2 &&
 107        test_cmp stashlist1 stashlist2 &&
 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 output expect &&
 214        git diff > output &&
 215        test_cmp output expect1 &&
 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 rm then recreate' '
 291        git reset --hard &&
 292        git rm file &&
 293        echo bar7 >file &&
 294        git stash save "rm then recreate" &&
 295        test bar = "$(cat file)" &&
 296        git stash apply &&
 297        test bar7 = "$(cat file)"
 298'
 299
 300test_expect_success 'stash rm and ignore' '
 301        git reset --hard &&
 302        git rm file &&
 303        echo file >.gitignore &&
 304        git stash save "rm and ignore" &&
 305        test bar = "$(cat file)" &&
 306        test file = "$(cat .gitignore)" &&
 307        git stash apply &&
 308        ! test -r file &&
 309        test file = "$(cat .gitignore)"
 310'
 311
 312test_expect_success 'stash rm and ignore (stage .gitignore)' '
 313        git reset --hard &&
 314        git rm file &&
 315        echo file >.gitignore &&
 316        git add .gitignore &&
 317        git stash save "rm and ignore (stage .gitignore)" &&
 318        test bar = "$(cat file)" &&
 319        ! test -r .gitignore &&
 320        git stash apply &&
 321        ! test -r file &&
 322        test file = "$(cat .gitignore)"
 323'
 324
 325test_expect_success SYMLINKS 'stash file to symlink' '
 326        git reset --hard &&
 327        rm file &&
 328        ln -s file2 file &&
 329        git stash save "file to symlink" &&
 330        test -f file &&
 331        test bar = "$(cat file)" &&
 332        git stash apply &&
 333        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 334'
 335
 336test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
 337        git reset --hard &&
 338        git rm file &&
 339        ln -s file2 file &&
 340        git stash save "file to symlink (stage rm)" &&
 341        test -f file &&
 342        test bar = "$(cat file)" &&
 343        git stash apply &&
 344        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 345'
 346
 347test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
 348        git reset --hard &&
 349        rm file &&
 350        ln -s file2 file &&
 351        git add file &&
 352        git stash save "file to symlink (full stage)" &&
 353        test -f file &&
 354        test bar = "$(cat file)" &&
 355        git stash apply &&
 356        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 357'
 358
 359# This test creates a commit with a symlink used for the following tests
 360
 361test_expect_success 'stash symlink to file' '
 362        git reset --hard &&
 363        test_ln_s_add file filelink &&
 364        git commit -m "Add symlink" &&
 365        rm filelink &&
 366        cp file filelink &&
 367        git stash save "symlink to file"
 368'
 369
 370test_expect_success SYMLINKS 'this must have re-created the symlink' '
 371        test -h filelink &&
 372        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
 373'
 374
 375test_expect_success 'unstash must re-create the file' '
 376        git stash apply &&
 377        ! test -h filelink &&
 378        test bar = "$(cat file)"
 379'
 380
 381test_expect_success 'stash symlink to file (stage rm)' '
 382        git reset --hard &&
 383        git rm filelink &&
 384        cp file filelink &&
 385        git stash save "symlink to file (stage rm)"
 386'
 387
 388test_expect_success SYMLINKS 'this must have re-created the symlink' '
 389        test -h filelink &&
 390        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
 391'
 392
 393test_expect_success 'unstash must re-create the file' '
 394        git stash apply &&
 395        ! test -h filelink &&
 396        test bar = "$(cat file)"
 397'
 398
 399test_expect_success 'stash symlink to file (full stage)' '
 400        git reset --hard &&
 401        rm filelink &&
 402        cp file filelink &&
 403        git add filelink &&
 404        git stash save "symlink to file (full stage)"
 405'
 406
 407test_expect_success SYMLINKS 'this must have re-created the symlink' '
 408        test -h filelink &&
 409        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
 410'
 411
 412test_expect_success 'unstash must re-create the file' '
 413        git stash apply &&
 414        ! test -h filelink &&
 415        test bar = "$(cat file)"
 416'
 417
 418test_expect_failure 'stash directory to file' '
 419        git reset --hard &&
 420        mkdir dir &&
 421        echo foo >dir/file &&
 422        git add dir/file &&
 423        git commit -m "Add file in dir" &&
 424        rm -fr dir &&
 425        echo bar >dir &&
 426        git stash save "directory to file" &&
 427        test -d dir &&
 428        test foo = "$(cat dir/file)" &&
 429        test_must_fail git stash apply &&
 430        test bar = "$(cat dir)" &&
 431        git reset --soft HEAD^
 432'
 433
 434test_expect_failure 'stash file to directory' '
 435        git reset --hard &&
 436        rm file &&
 437        mkdir file &&
 438        echo foo >file/file &&
 439        git stash save "file to directory" &&
 440        test -f file &&
 441        test bar = "$(cat file)" &&
 442        git stash apply &&
 443        test -f file/file &&
 444        test foo = "$(cat file/file)"
 445'
 446
 447test_expect_success 'stash create - no changes' '
 448        git stash clear &&
 449        test_when_finished "git reset --hard HEAD" &&
 450        git reset --hard &&
 451        git stash create >actual &&
 452        test_must_be_empty actual
 453'
 454
 455test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
 456        git stash clear &&
 457        test_when_finished "git reset --hard HEAD" &&
 458        git reset --hard &&
 459        echo foo >> file &&
 460        STASH_ID=$(git stash create) &&
 461        git reset --hard &&
 462        git stash branch stash-branch ${STASH_ID} &&
 463        test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
 464        test $(git ls-files --modified | wc -l) -eq 1
 465'
 466
 467test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 468        git stash clear &&
 469        test_when_finished "git reset --hard HEAD" &&
 470        git reset --hard &&
 471        echo foo >> file &&
 472        git stash &&
 473        test_when_finished "git stash drop" &&
 474        echo bar >> file &&
 475        STASH_ID=$(git stash create) &&
 476        git reset --hard &&
 477        git stash branch stash-branch ${STASH_ID} &&
 478        test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
 479        test $(git ls-files --modified | wc -l) -eq 1
 480'
 481
 482test_expect_success 'stash show format defaults to --stat' '
 483        git stash clear &&
 484        test_when_finished "git reset --hard HEAD" &&
 485        git reset --hard &&
 486        echo foo >> file &&
 487        git stash &&
 488        test_when_finished "git stash drop" &&
 489        echo bar >> file &&
 490        STASH_ID=$(git stash create) &&
 491        git reset --hard &&
 492        cat >expected <<-EOF &&
 493         file | 1 +
 494         1 file changed, 1 insertion(+)
 495        EOF
 496        git stash show ${STASH_ID} >actual &&
 497        test_i18ncmp expected actual
 498'
 499
 500test_expect_success 'stash show - stashes on stack, stash-like argument' '
 501        git stash clear &&
 502        test_when_finished "git reset --hard HEAD" &&
 503        git reset --hard &&
 504        echo foo >> file &&
 505        git stash &&
 506        test_when_finished "git stash drop" &&
 507        echo bar >> file &&
 508        STASH_ID=$(git stash create) &&
 509        git reset --hard &&
 510        echo "1 0       file" >expected &&
 511        git stash show --numstat ${STASH_ID} >actual &&
 512        test_cmp expected actual
 513'
 514
 515test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
 516        git stash clear &&
 517        test_when_finished "git reset --hard HEAD" &&
 518        git reset --hard &&
 519        echo foo >> file &&
 520        git stash &&
 521        test_when_finished "git stash drop" &&
 522        echo bar >> file &&
 523        STASH_ID=$(git stash create) &&
 524        git reset --hard &&
 525        cat >expected <<-EOF &&
 526        diff --git a/file b/file
 527        index 7601807..935fbd3 100644
 528        --- a/file
 529        +++ b/file
 530        @@ -1 +1,2 @@
 531         baz
 532        +bar
 533        EOF
 534        git stash show -p ${STASH_ID} >actual &&
 535        test_cmp expected actual
 536'
 537
 538test_expect_success 'stash show - no stashes on stack, stash-like argument' '
 539        git stash clear &&
 540        test_when_finished "git reset --hard HEAD" &&
 541        git reset --hard &&
 542        echo foo >> file &&
 543        STASH_ID=$(git stash create) &&
 544        git reset --hard &&
 545        echo "1 0       file" >expected &&
 546        git stash show --numstat ${STASH_ID} >actual &&
 547        test_cmp expected actual
 548'
 549
 550test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 551        git stash clear &&
 552        test_when_finished "git reset --hard HEAD" &&
 553        git reset --hard &&
 554        echo foo >> file &&
 555        STASH_ID=$(git stash create) &&
 556        git reset --hard &&
 557        cat >expected <<-EOF &&
 558        diff --git a/file b/file
 559        index 7601807..71b52c4 100644
 560        --- a/file
 561        +++ b/file
 562        @@ -1 +1,2 @@
 563         baz
 564        +foo
 565        EOF
 566        git stash show -p ${STASH_ID} >actual &&
 567        test_cmp expected actual
 568'
 569
 570test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
 571        git stash clear &&
 572        test_when_finished "git reset --hard HEAD && git stash clear" &&
 573        git reset --hard &&
 574        echo foo > file &&
 575        git stash &&
 576        echo bar > file &&
 577        git stash &&
 578        test_must_fail git stash drop $(git rev-parse stash@{0}) &&
 579        git stash pop &&
 580        test bar = "$(cat file)" &&
 581        git reset --hard HEAD
 582'
 583
 584test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
 585        git stash clear &&
 586        test_when_finished "git reset --hard HEAD && git stash clear" &&
 587        git reset --hard &&
 588        echo foo > file &&
 589        git stash &&
 590        echo bar > file &&
 591        git stash &&
 592        test_must_fail git stash pop $(git rev-parse stash@{0}) &&
 593        git stash pop &&
 594        test bar = "$(cat file)" &&
 595        git reset --hard HEAD
 596'
 597
 598test_expect_success 'ref with non-existent reflog' '
 599        git stash clear &&
 600        echo bar5 > file &&
 601        echo bar6 > file2 &&
 602        git add file2 &&
 603        git stash &&
 604        test_must_fail git rev-parse --quiet --verify does-not-exist &&
 605        test_must_fail git stash drop does-not-exist &&
 606        test_must_fail git stash drop does-not-exist@{0} &&
 607        test_must_fail git stash pop does-not-exist &&
 608        test_must_fail git stash pop does-not-exist@{0} &&
 609        test_must_fail git stash apply does-not-exist &&
 610        test_must_fail git stash apply does-not-exist@{0} &&
 611        test_must_fail git stash show does-not-exist &&
 612        test_must_fail git stash show does-not-exist@{0} &&
 613        test_must_fail git stash branch tmp does-not-exist &&
 614        test_must_fail git stash branch tmp does-not-exist@{0} &&
 615        git stash drop
 616'
 617
 618test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 619        git stash clear &&
 620        test_must_fail git stash drop stash@{0} &&
 621        echo bar5 > file &&
 622        echo bar6 > file2 &&
 623        git add file2 &&
 624        git stash &&
 625        test_must_fail git stash drop stash@{1} &&
 626        test_must_fail git stash pop stash@{1} &&
 627        test_must_fail git stash apply stash@{1} &&
 628        test_must_fail git stash show stash@{1} &&
 629        test_must_fail git stash branch tmp stash@{1} &&
 630        git stash drop
 631'
 632
 633test_expect_success 'invalid ref of the form "n", n >= N' '
 634        git stash clear &&
 635        test_must_fail git stash drop 0 &&
 636        echo bar5 >file &&
 637        echo bar6 >file2 &&
 638        git add file2 &&
 639        git stash &&
 640        test_must_fail git stash drop 1 &&
 641        test_must_fail git stash pop 1 &&
 642        test_must_fail git stash apply 1 &&
 643        test_must_fail git stash show 1 &&
 644        test_must_fail git stash branch tmp 1 &&
 645        git stash drop
 646'
 647
 648test_expect_success 'stash branch should not drop the stash if the branch exists' '
 649        git stash clear &&
 650        echo foo >file &&
 651        git add file &&
 652        git commit -m initial &&
 653        echo bar >file &&
 654        git stash &&
 655        test_must_fail git stash branch master stash@{0} &&
 656        git rev-parse stash@{0} --
 657'
 658
 659test_expect_success 'stash branch should not drop the stash if the apply fails' '
 660        git stash clear &&
 661        git reset HEAD~1 --hard &&
 662        echo foo >file &&
 663        git add file &&
 664        git commit -m initial &&
 665        echo bar >file &&
 666        git stash &&
 667        echo baz >file &&
 668        test_when_finished "git checkout master" &&
 669        test_must_fail git stash branch new_branch stash@{0} &&
 670        git rev-parse stash@{0} --
 671'
 672
 673test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
 674        git stash clear &&
 675        echo 1 >subdir/subfile1 &&
 676        echo 2 >subdir/subfile2 &&
 677        git add subdir/subfile1 &&
 678        git commit -m subdir &&
 679        (
 680                cd subdir &&
 681                echo x >subfile1 &&
 682                echo x >../file &&
 683                git status >../expect &&
 684                git stash &&
 685                sane_unset GIT_MERGE_VERBOSITY &&
 686                git stash apply
 687        ) |
 688        sed -e 1d >actual && # drop "Saved..."
 689        test_i18ncmp expect actual
 690'
 691
 692cat > expect << EOF
 693diff --git a/HEAD b/HEAD
 694new file mode 100644
 695index 0000000..fe0cbee
 696--- /dev/null
 697+++ b/HEAD
 698@@ -0,0 +1 @@
 699+file-not-a-ref
 700EOF
 701
 702test_expect_success 'stash where working directory contains "HEAD" file' '
 703        git stash clear &&
 704        git reset --hard &&
 705        echo file-not-a-ref > HEAD &&
 706        git add HEAD &&
 707        test_tick &&
 708        git stash &&
 709        git diff-files --quiet &&
 710        git diff-index --cached --quiet HEAD &&
 711        test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
 712        git diff stash^..stash > output &&
 713        test_cmp output expect
 714'
 715
 716test_expect_success 'store called with invalid commit' '
 717        test_must_fail git stash store foo
 718'
 719
 720test_expect_success 'store updates stash ref and reflog' '
 721        git stash clear &&
 722        git reset --hard &&
 723        echo quux >bazzy &&
 724        git add bazzy &&
 725        STASH_ID=$(git stash create) &&
 726        git reset --hard &&
 727        ! grep quux bazzy &&
 728        git stash store -m quuxery $STASH_ID &&
 729        test $(cat .git/refs/stash) = $STASH_ID &&
 730        git reflog --format=%H stash| grep $STASH_ID &&
 731        git stash pop &&
 732        grep quux bazzy
 733'
 734
 735test_expect_success 'handle stash specification with spaces' '
 736        git stash clear &&
 737        echo pig >file &&
 738        git stash &&
 739        stamp=$(git log -g --format="%cd" -1 refs/stash) &&
 740        test_tick &&
 741        echo cow >file &&
 742        git stash &&
 743        git stash apply "stash@{$stamp}" &&
 744        grep pig file
 745'
 746
 747test_expect_success 'setup stash with index and worktree changes' '
 748        git stash clear &&
 749        git reset --hard &&
 750        echo index >file &&
 751        git add file &&
 752        echo working >file &&
 753        git stash
 754'
 755
 756test_expect_success 'stash list implies --first-parent -m' '
 757        cat >expect <<-EOF &&
 758        stash@{0}
 759
 760        diff --git a/file b/file
 761        index 257cc56..d26b33d 100644
 762        --- a/file
 763        +++ b/file
 764        @@ -1 +1 @@
 765        -foo
 766        +working
 767        EOF
 768        git stash list --format=%gd -p >actual &&
 769        test_cmp expect actual
 770'
 771
 772test_expect_success 'stash list --cc shows combined diff' '
 773        cat >expect <<-\EOF &&
 774        stash@{0}
 775
 776        diff --cc file
 777        index 257cc56,9015a7a..d26b33d
 778        --- a/file
 779        +++ b/file
 780        @@@ -1,1 -1,1 +1,1 @@@
 781        - foo
 782         -index
 783        ++working
 784        EOF
 785        git stash list --format=%gd -p --cc >actual &&
 786        test_cmp expect actual
 787'
 788
 789test_expect_success 'stash is not confused by partial renames' '
 790        mv file renamed &&
 791        git add renamed &&
 792        git stash &&
 793        git stash apply &&
 794        test_path_is_file renamed &&
 795        test_path_is_missing file
 796'
 797
 798test_expect_success 'push -m shows right message' '
 799        >foo &&
 800        git add foo &&
 801        git stash push -m "test message" &&
 802        echo "stash@{0}: On master: test message" >expect &&
 803        git stash list -1 >actual &&
 804        test_cmp expect actual
 805'
 806
 807test_expect_success 'create stores correct message' '
 808        >foo &&
 809        git add foo &&
 810        STASH_ID=$(git stash create "create test message") &&
 811        echo "On master: create test message" >expect &&
 812        git show --pretty=%s -s ${STASH_ID} >actual &&
 813        test_cmp expect actual
 814'
 815
 816test_expect_success 'create with multiple arguments for the message' '
 817        >foo &&
 818        git add foo &&
 819        STASH_ID=$(git stash create test untracked) &&
 820        echo "On master: test untracked" >expect &&
 821        git show --pretty=%s -s ${STASH_ID} >actual &&
 822        test_cmp expect actual
 823'
 824
 825test_expect_success 'create in a detached state' '
 826        test_when_finished "git checkout master" &&
 827        git checkout HEAD~1 &&
 828        >foo &&
 829        git add foo &&
 830        STASH_ID=$(git stash create) &&
 831        HEAD_ID=$(git rev-parse --short HEAD) &&
 832        echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
 833        git show --pretty=%s -s ${STASH_ID} >actual &&
 834        test_cmp expect actual
 835'
 836
 837test_expect_success 'stash -- <pathspec> stashes and restores the file' '
 838        >foo &&
 839        >bar &&
 840        git add foo bar &&
 841        git stash push -- foo &&
 842        test_path_is_file bar &&
 843        test_path_is_missing foo &&
 844        git stash pop &&
 845        test_path_is_file foo &&
 846        test_path_is_file bar
 847'
 848
 849test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
 850        mkdir sub &&
 851        >foo &&
 852        >bar &&
 853        git add foo bar &&
 854        (
 855                cd sub &&
 856                git stash push -- ../foo
 857        ) &&
 858        test_path_is_file bar &&
 859        test_path_is_missing foo &&
 860        git stash pop &&
 861        test_path_is_file foo &&
 862        test_path_is_file bar
 863'
 864
 865test_expect_success 'stash with multiple pathspec arguments' '
 866        >foo &&
 867        >bar &&
 868        >extra &&
 869        git add foo bar extra &&
 870        git stash push -- foo bar &&
 871        test_path_is_missing bar &&
 872        test_path_is_missing foo &&
 873        test_path_is_file extra &&
 874        git stash pop &&
 875        test_path_is_file foo &&
 876        test_path_is_file bar &&
 877        test_path_is_file extra
 878'
 879
 880test_expect_success 'stash with file including $IFS character' '
 881        >"foo bar" &&
 882        >foo &&
 883        >bar &&
 884        git add foo* &&
 885        git stash push -- "foo b*" &&
 886        test_path_is_missing "foo bar" &&
 887        test_path_is_file foo &&
 888        test_path_is_file bar &&
 889        git stash pop &&
 890        test_path_is_file "foo bar" &&
 891        test_path_is_file foo &&
 892        test_path_is_file bar
 893'
 894
 895test_expect_success 'stash with pathspec matching multiple paths' '
 896       echo original >file &&
 897       echo original >other-file &&
 898       git commit -m "two" file other-file &&
 899       echo modified >file &&
 900       echo modified >other-file &&
 901       git stash push -- "*file" &&
 902       echo original >expect &&
 903       test_cmp expect file &&
 904       test_cmp expect other-file &&
 905       git stash pop &&
 906       echo modified >expect &&
 907       test_cmp expect file &&
 908       test_cmp expect other-file
 909'
 910
 911test_expect_success 'stash push -p with pathspec shows no changes only once' '
 912        >foo &&
 913        git add foo &&
 914        git commit -m "tmp" &&
 915        git stash push -p foo >actual &&
 916        echo "No local changes to save" >expect &&
 917        git reset --hard HEAD~ &&
 918        test_i18ncmp expect actual
 919'
 920
 921test_expect_success 'stash push with pathspec shows no changes when there are none' '
 922        >foo &&
 923        git add foo &&
 924        git commit -m "tmp" &&
 925        git stash push foo >actual &&
 926        echo "No local changes to save" >expect &&
 927        git reset --hard HEAD~ &&
 928        test_i18ncmp expect actual
 929'
 930
 931test_expect_success 'stash push with pathspec not in the repository errors out' '
 932        >untracked &&
 933        test_must_fail git stash push untracked &&
 934        test_path_is_file untracked
 935'
 936
 937test_expect_success 'untracked files are left in place when -u is not given' '
 938        >file &&
 939        git add file &&
 940        >untracked &&
 941        git stash push file &&
 942        test_path_is_file untracked
 943'
 944
 945test_expect_success 'stash without verb with pathspec' '
 946        >"foo bar" &&
 947        >foo &&
 948        >bar &&
 949        git add foo* &&
 950        git stash -- "foo b*" &&
 951        test_path_is_missing "foo bar" &&
 952        test_path_is_file foo &&
 953        test_path_is_file bar &&
 954        git stash pop &&
 955        test_path_is_file "foo bar" &&
 956        test_path_is_file foo &&
 957        test_path_is_file bar
 958'
 959
 960test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
 961        git reset &&
 962        >foo &&
 963        >bar &&
 964        git add foo bar &&
 965        git commit -m "test" &&
 966        echo "foo" >foo &&
 967        echo "bar" >bar &&
 968        git stash -k -- foo &&
 969        test "",bar = $(cat foo),$(cat bar) &&
 970        git stash pop &&
 971        test foo,bar = $(cat foo),$(cat bar)
 972'
 973
 974test_done