t / t3903-stash.shon commit t3903: modernize style (6a0b88b)
   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 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 'giving too many ref arguments does not modify files' '
 448        git stash clear &&
 449        test_when_finished "git reset --hard HEAD" &&
 450        echo foo >file2 &&
 451        git stash &&
 452        echo bar >file2 &&
 453        git stash &&
 454        test-tool chmtime =123456789 file2 &&
 455        for type in apply pop "branch stash-branch"
 456        do
 457                test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
 458                test_i18ngrep "Too many revisions" err &&
 459                test 123456789 = $(test-tool chmtime -g file2) || return 1
 460        done
 461'
 462
 463test_expect_success 'drop: too many arguments errors out (does nothing)' '
 464        git stash list >expect &&
 465        test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
 466        test_i18ngrep "Too many revisions" err &&
 467        git stash list >actual &&
 468        test_cmp expect actual
 469'
 470
 471test_expect_success 'show: too many arguments errors out (does nothing)' '
 472        test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
 473        test_i18ngrep "Too many revisions" err &&
 474        test_must_be_empty out
 475'
 476
 477test_expect_success 'stash create - no changes' '
 478        git stash clear &&
 479        test_when_finished "git reset --hard HEAD" &&
 480        git reset --hard &&
 481        git stash create >actual &&
 482        test_must_be_empty actual
 483'
 484
 485test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
 486        git stash clear &&
 487        test_when_finished "git reset --hard HEAD" &&
 488        git reset --hard &&
 489        echo foo >>file &&
 490        STASH_ID=$(git stash create) &&
 491        git reset --hard &&
 492        git stash branch stash-branch ${STASH_ID} &&
 493        test_when_finished "git reset --hard HEAD && git checkout master &&
 494        git branch -D stash-branch" &&
 495        test $(git ls-files --modified | wc -l) -eq 1
 496'
 497
 498test_expect_success 'stash branch - stashes on stack, stash-like argument' '
 499        git stash clear &&
 500        test_when_finished "git reset --hard HEAD" &&
 501        git reset --hard &&
 502        echo foo >>file &&
 503        git stash &&
 504        test_when_finished "git stash drop" &&
 505        echo bar >>file &&
 506        STASH_ID=$(git stash create) &&
 507        git reset --hard &&
 508        git stash branch stash-branch ${STASH_ID} &&
 509        test_when_finished "git reset --hard HEAD && git checkout master &&
 510        git branch -D stash-branch" &&
 511        test $(git ls-files --modified | wc -l) -eq 1
 512'
 513
 514test_expect_success 'stash branch complains with no arguments' '
 515        test_must_fail git stash branch 2>err &&
 516        test_i18ngrep "No branch name specified" err
 517'
 518
 519test_expect_success 'stash show format defaults to --stat' '
 520        git stash clear &&
 521        test_when_finished "git reset --hard HEAD" &&
 522        git reset --hard &&
 523        echo foo >>file &&
 524        git stash &&
 525        test_when_finished "git stash drop" &&
 526        echo bar >>file &&
 527        STASH_ID=$(git stash create) &&
 528        git reset --hard &&
 529        cat >expected <<-EOF &&
 530         file | 1 +
 531         1 file changed, 1 insertion(+)
 532        EOF
 533        git stash show ${STASH_ID} >actual &&
 534        test_i18ncmp expected actual
 535'
 536
 537test_expect_success 'stash show - stashes on stack, stash-like argument' '
 538        git stash clear &&
 539        test_when_finished "git reset --hard HEAD" &&
 540        git reset --hard &&
 541        echo foo >>file &&
 542        git stash &&
 543        test_when_finished "git stash drop" &&
 544        echo bar >>file &&
 545        STASH_ID=$(git stash create) &&
 546        git reset --hard &&
 547        echo "1 0       file" >expected &&
 548        git stash show --numstat ${STASH_ID} >actual &&
 549        test_cmp expected actual
 550'
 551
 552test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
 553        git stash clear &&
 554        test_when_finished "git reset --hard HEAD" &&
 555        git reset --hard &&
 556        echo foo >>file &&
 557        git stash &&
 558        test_when_finished "git stash drop" &&
 559        echo bar >>file &&
 560        STASH_ID=$(git stash create) &&
 561        git reset --hard &&
 562        cat >expected <<-EOF &&
 563        diff --git a/file b/file
 564        index 7601807..935fbd3 100644
 565        --- a/file
 566        +++ b/file
 567        @@ -1 +1,2 @@
 568         baz
 569        +bar
 570        EOF
 571        git stash show -p ${STASH_ID} >actual &&
 572        test_cmp expected actual
 573'
 574
 575test_expect_success 'stash show - no stashes on stack, stash-like argument' '
 576        git stash clear &&
 577        test_when_finished "git reset --hard HEAD" &&
 578        git reset --hard &&
 579        echo foo >>file &&
 580        STASH_ID=$(git stash create) &&
 581        git reset --hard &&
 582        echo "1 0       file" >expected &&
 583        git stash show --numstat ${STASH_ID} >actual &&
 584        test_cmp expected actual
 585'
 586
 587test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
 588        git stash clear &&
 589        test_when_finished "git reset --hard HEAD" &&
 590        git reset --hard &&
 591        echo foo >>file &&
 592        STASH_ID=$(git stash create) &&
 593        git reset --hard &&
 594        cat >expected <<-EOF &&
 595        diff --git a/file b/file
 596        index 7601807..71b52c4 100644
 597        --- a/file
 598        +++ b/file
 599        @@ -1 +1,2 @@
 600         baz
 601        +foo
 602        EOF
 603        git stash show -p ${STASH_ID} >actual &&
 604        test_cmp expected actual
 605'
 606
 607test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
 608        git stash clear &&
 609        test_when_finished "git reset --hard HEAD && git stash clear" &&
 610        git reset --hard &&
 611        echo foo >file &&
 612        git stash &&
 613        echo bar >file &&
 614        git stash &&
 615        test_must_fail git stash drop $(git rev-parse stash@{0}) &&
 616        git stash pop &&
 617        test bar = "$(cat file)" &&
 618        git reset --hard HEAD
 619'
 620
 621test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
 622        git stash clear &&
 623        test_when_finished "git reset --hard HEAD && git stash clear" &&
 624        git reset --hard &&
 625        echo foo >file &&
 626        git stash &&
 627        echo bar >file &&
 628        git stash &&
 629        test_must_fail git stash pop $(git rev-parse stash@{0}) &&
 630        git stash pop &&
 631        test bar = "$(cat file)" &&
 632        git reset --hard HEAD
 633'
 634
 635test_expect_success 'ref with non-existent reflog' '
 636        git stash clear &&
 637        echo bar5 >file &&
 638        echo bar6 >file2 &&
 639        git add file2 &&
 640        git stash &&
 641        test_must_fail git rev-parse --quiet --verify does-not-exist &&
 642        test_must_fail git stash drop does-not-exist &&
 643        test_must_fail git stash drop does-not-exist@{0} &&
 644        test_must_fail git stash pop does-not-exist &&
 645        test_must_fail git stash pop does-not-exist@{0} &&
 646        test_must_fail git stash apply does-not-exist &&
 647        test_must_fail git stash apply does-not-exist@{0} &&
 648        test_must_fail git stash show does-not-exist &&
 649        test_must_fail git stash show does-not-exist@{0} &&
 650        test_must_fail git stash branch tmp does-not-exist &&
 651        test_must_fail git stash branch tmp does-not-exist@{0} &&
 652        git stash drop
 653'
 654
 655test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 656        git stash clear &&
 657        test_must_fail git stash drop stash@{0} &&
 658        echo bar5 >file &&
 659        echo bar6 >file2 &&
 660        git add file2 &&
 661        git stash &&
 662        test_must_fail git stash drop stash@{1} &&
 663        test_must_fail git stash pop stash@{1} &&
 664        test_must_fail git stash apply stash@{1} &&
 665        test_must_fail git stash show stash@{1} &&
 666        test_must_fail git stash branch tmp stash@{1} &&
 667        git stash drop
 668'
 669
 670test_expect_success 'invalid ref of the form "n", n >= N' '
 671        git stash clear &&
 672        test_must_fail git stash drop 0 &&
 673        echo bar5 >file &&
 674        echo bar6 >file2 &&
 675        git add file2 &&
 676        git stash &&
 677        test_must_fail git stash drop 1 &&
 678        test_must_fail git stash pop 1 &&
 679        test_must_fail git stash apply 1 &&
 680        test_must_fail git stash show 1 &&
 681        test_must_fail git stash branch tmp 1 &&
 682        git stash drop
 683'
 684
 685test_expect_success 'stash branch should not drop the stash if the branch exists' '
 686        git stash clear &&
 687        echo foo >file &&
 688        git add file &&
 689        git commit -m initial &&
 690        echo bar >file &&
 691        git stash &&
 692        test_must_fail git stash branch master stash@{0} &&
 693        git rev-parse stash@{0} --
 694'
 695
 696test_expect_success 'stash branch should not drop the stash if the apply fails' '
 697        git stash clear &&
 698        git reset HEAD~1 --hard &&
 699        echo foo >file &&
 700        git add file &&
 701        git commit -m initial &&
 702        echo bar >file &&
 703        git stash &&
 704        echo baz >file &&
 705        test_when_finished "git checkout master" &&
 706        test_must_fail git stash branch new_branch stash@{0} &&
 707        git rev-parse stash@{0} --
 708'
 709
 710test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
 711        git stash clear &&
 712        echo 1 >subdir/subfile1 &&
 713        echo 2 >subdir/subfile2 &&
 714        git add subdir/subfile1 &&
 715        git commit -m subdir &&
 716        (
 717                cd subdir &&
 718                echo x >subfile1 &&
 719                echo x >../file &&
 720                git status >../expect &&
 721                git stash &&
 722                sane_unset GIT_MERGE_VERBOSITY &&
 723                git stash apply
 724        ) |
 725        sed -e 1d >actual && # drop "Saved..."
 726        test_i18ncmp expect actual
 727'
 728
 729cat >expect <<EOF
 730diff --git a/HEAD b/HEAD
 731new file mode 100644
 732index 0000000..fe0cbee
 733--- /dev/null
 734+++ b/HEAD
 735@@ -0,0 +1 @@
 736+file-not-a-ref
 737EOF
 738
 739test_expect_success 'stash where working directory contains "HEAD" file' '
 740        git stash clear &&
 741        git reset --hard &&
 742        echo file-not-a-ref >HEAD &&
 743        git add HEAD &&
 744        test_tick &&
 745        git stash &&
 746        git diff-files --quiet &&
 747        git diff-index --cached --quiet HEAD &&
 748        test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
 749        git diff stash^..stash >output &&
 750        test_cmp expect output
 751'
 752
 753test_expect_success 'store called with invalid commit' '
 754        test_must_fail git stash store foo
 755'
 756
 757test_expect_success 'store updates stash ref and reflog' '
 758        git stash clear &&
 759        git reset --hard &&
 760        echo quux >bazzy &&
 761        git add bazzy &&
 762        STASH_ID=$(git stash create) &&
 763        git reset --hard &&
 764        test_path_is_missing bazzy &&
 765        git stash store -m quuxery $STASH_ID &&
 766        test $(git rev-parse stash) = $STASH_ID &&
 767        git reflog --format=%H stash| grep $STASH_ID &&
 768        git stash pop &&
 769        grep quux bazzy
 770'
 771
 772test_expect_success 'handle stash specification with spaces' '
 773        git stash clear &&
 774        echo pig >file &&
 775        git stash &&
 776        stamp=$(git log -g --format="%cd" -1 refs/stash) &&
 777        test_tick &&
 778        echo cow >file &&
 779        git stash &&
 780        git stash apply "stash@{$stamp}" &&
 781        grep pig file
 782'
 783
 784test_expect_success 'setup stash with index and worktree changes' '
 785        git stash clear &&
 786        git reset --hard &&
 787        echo index >file &&
 788        git add file &&
 789        echo working >file &&
 790        git stash
 791'
 792
 793test_expect_success 'stash list implies --first-parent -m' '
 794        cat >expect <<-EOF &&
 795        stash@{0}
 796
 797        diff --git a/file b/file
 798        index 257cc56..d26b33d 100644
 799        --- a/file
 800        +++ b/file
 801        @@ -1 +1 @@
 802        -foo
 803        +working
 804        EOF
 805        git stash list --format=%gd -p >actual &&
 806        test_cmp expect actual
 807'
 808
 809test_expect_success 'stash list --cc shows combined diff' '
 810        cat >expect <<-\EOF &&
 811        stash@{0}
 812
 813        diff --cc file
 814        index 257cc56,9015a7a..d26b33d
 815        --- a/file
 816        +++ b/file
 817        @@@ -1,1 -1,1 +1,1 @@@
 818        - foo
 819         -index
 820        ++working
 821        EOF
 822        git stash list --format=%gd -p --cc >actual &&
 823        test_cmp expect actual
 824'
 825
 826test_expect_success 'stash is not confused by partial renames' '
 827        mv file renamed &&
 828        git add renamed &&
 829        git stash &&
 830        git stash apply &&
 831        test_path_is_file renamed &&
 832        test_path_is_missing file
 833'
 834
 835test_expect_success 'push -m shows right message' '
 836        >foo &&
 837        git add foo &&
 838        git stash push -m "test message" &&
 839        echo "stash@{0}: On master: test message" >expect &&
 840        git stash list -1 >actual &&
 841        test_cmp expect actual
 842'
 843
 844test_expect_success 'push -m also works without space' '
 845        >foo &&
 846        git add foo &&
 847        git stash push -m"unspaced test message" &&
 848        echo "stash@{0}: On master: unspaced test message" >expect &&
 849        git stash list -1 >actual &&
 850        test_cmp expect actual
 851'
 852
 853test_expect_success 'store -m foo shows right message' '
 854        git stash clear &&
 855        git reset --hard &&
 856        echo quux >bazzy &&
 857        git add bazzy &&
 858        STASH_ID=$(git stash create) &&
 859        git stash store -m "store m" $STASH_ID &&
 860        echo "stash@{0}: store m" >expect &&
 861        git stash list -1 >actual &&
 862        test_cmp expect actual
 863'
 864
 865test_expect_success 'store -mfoo shows right message' '
 866        git stash clear &&
 867        git reset --hard &&
 868        echo quux >bazzy &&
 869        git add bazzy &&
 870        STASH_ID=$(git stash create) &&
 871        git stash store -m"store mfoo" $STASH_ID &&
 872        echo "stash@{0}: store mfoo" >expect &&
 873        git stash list -1 >actual &&
 874        test_cmp expect actual
 875'
 876
 877test_expect_success 'store --message=foo shows right message' '
 878        git stash clear &&
 879        git reset --hard &&
 880        echo quux >bazzy &&
 881        git add bazzy &&
 882        STASH_ID=$(git stash create) &&
 883        git stash store --message="store message=foo" $STASH_ID &&
 884        echo "stash@{0}: store message=foo" >expect &&
 885        git stash list -1 >actual &&
 886        test_cmp expect actual
 887'
 888
 889test_expect_success 'store --message foo shows right message' '
 890        git stash clear &&
 891        git reset --hard &&
 892        echo quux >bazzy &&
 893        git add bazzy &&
 894        STASH_ID=$(git stash create) &&
 895        git stash store --message "store message foo" $STASH_ID &&
 896        echo "stash@{0}: store message foo" >expect &&
 897        git stash list -1 >actual &&
 898        test_cmp expect actual
 899'
 900
 901test_expect_success 'push -mfoo uses right message' '
 902        >foo &&
 903        git add foo &&
 904        git stash push -m"test mfoo" &&
 905        echo "stash@{0}: On master: test mfoo" >expect &&
 906        git stash list -1 >actual &&
 907        test_cmp expect actual
 908'
 909
 910test_expect_success 'push --message foo is synonym for -mfoo' '
 911        >foo &&
 912        git add foo &&
 913        git stash push --message "test message foo" &&
 914        echo "stash@{0}: On master: test message foo" >expect &&
 915        git stash list -1 >actual &&
 916        test_cmp expect actual
 917'
 918
 919test_expect_success 'push --message=foo is synonym for -mfoo' '
 920        >foo &&
 921        git add foo &&
 922        git stash push --message="test message=foo" &&
 923        echo "stash@{0}: On master: test message=foo" >expect &&
 924        git stash list -1 >actual &&
 925        test_cmp expect actual
 926'
 927
 928test_expect_success 'push -m shows right message' '
 929        >foo &&
 930        git add foo &&
 931        git stash push -m "test m foo" &&
 932        echo "stash@{0}: On master: test m foo" >expect &&
 933        git stash list -1 >actual &&
 934        test_cmp expect actual
 935'
 936
 937test_expect_success 'create stores correct message' '
 938        >foo &&
 939        git add foo &&
 940        STASH_ID=$(git stash create "create test message") &&
 941        echo "On master: create test message" >expect &&
 942        git show --pretty=%s -s ${STASH_ID} >actual &&
 943        test_cmp expect actual
 944'
 945
 946test_expect_success 'create with multiple arguments for the message' '
 947        >foo &&
 948        git add foo &&
 949        STASH_ID=$(git stash create test untracked) &&
 950        echo "On master: test untracked" >expect &&
 951        git show --pretty=%s -s ${STASH_ID} >actual &&
 952        test_cmp expect actual
 953'
 954
 955test_expect_success 'create in a detached state' '
 956        test_when_finished "git checkout master" &&
 957        git checkout HEAD~1 &&
 958        >foo &&
 959        git add foo &&
 960        STASH_ID=$(git stash create) &&
 961        HEAD_ID=$(git rev-parse --short HEAD) &&
 962        echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
 963        git show --pretty=%s -s ${STASH_ID} >actual &&
 964        test_cmp expect actual
 965'
 966
 967test_expect_success 'stash -- <pathspec> stashes and restores the file' '
 968        >foo &&
 969        >bar &&
 970        git add foo bar &&
 971        git stash push -- foo &&
 972        test_path_is_file bar &&
 973        test_path_is_missing foo &&
 974        git stash pop &&
 975        test_path_is_file foo &&
 976        test_path_is_file bar
 977'
 978
 979test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
 980        mkdir sub &&
 981        >foo &&
 982        >bar &&
 983        git add foo bar &&
 984        (
 985                cd sub &&
 986                git stash push -- ../foo
 987        ) &&
 988        test_path_is_file bar &&
 989        test_path_is_missing foo &&
 990        git stash pop &&
 991        test_path_is_file foo &&
 992        test_path_is_file bar
 993'
 994
 995test_expect_success 'stash with multiple pathspec arguments' '
 996        >foo &&
 997        >bar &&
 998        >extra &&
 999        git add foo bar extra &&
1000        git stash push -- foo bar &&
1001        test_path_is_missing bar &&
1002        test_path_is_missing foo &&
1003        test_path_is_file extra &&
1004        git stash pop &&
1005        test_path_is_file foo &&
1006        test_path_is_file bar &&
1007        test_path_is_file extra
1008'
1009
1010test_expect_success 'stash with file including $IFS character' '
1011        >"foo bar" &&
1012        >foo &&
1013        >bar &&
1014        git add foo* &&
1015        git stash push -- "foo b*" &&
1016        test_path_is_missing "foo bar" &&
1017        test_path_is_file foo &&
1018        test_path_is_file bar &&
1019        git stash pop &&
1020        test_path_is_file "foo bar" &&
1021        test_path_is_file foo &&
1022        test_path_is_file bar
1023'
1024
1025test_expect_success 'stash with pathspec matching multiple paths' '
1026       echo original >file &&
1027       echo original >other-file &&
1028       git commit -m "two" file other-file &&
1029       echo modified >file &&
1030       echo modified >other-file &&
1031       git stash push -- "*file" &&
1032       echo original >expect &&
1033       test_cmp expect file &&
1034       test_cmp expect other-file &&
1035       git stash pop &&
1036       echo modified >expect &&
1037       test_cmp expect file &&
1038       test_cmp expect other-file
1039'
1040
1041test_expect_success 'stash push -p with pathspec shows no changes only once' '
1042        >foo &&
1043        git add foo &&
1044        git commit -m "tmp" &&
1045        git stash push -p foo >actual &&
1046        echo "No local changes to save" >expect &&
1047        git reset --hard HEAD~ &&
1048        test_i18ncmp expect actual
1049'
1050
1051test_expect_success 'stash push with pathspec shows no changes when there are none' '
1052        >foo &&
1053        git add foo &&
1054        git commit -m "tmp" &&
1055        git stash push foo >actual &&
1056        echo "No local changes to save" >expect &&
1057        git reset --hard HEAD~ &&
1058        test_i18ncmp expect actual
1059'
1060
1061test_expect_success 'stash push with pathspec not in the repository errors out' '
1062        >untracked &&
1063        test_must_fail git stash push untracked &&
1064        test_path_is_file untracked
1065'
1066
1067test_expect_success 'untracked files are left in place when -u is not given' '
1068        >file &&
1069        git add file &&
1070        >untracked &&
1071        git stash push file &&
1072        test_path_is_file untracked
1073'
1074
1075test_expect_success 'stash without verb with pathspec' '
1076        >"foo bar" &&
1077        >foo &&
1078        >bar &&
1079        git add foo* &&
1080        git stash -- "foo b*" &&
1081        test_path_is_missing "foo bar" &&
1082        test_path_is_file foo &&
1083        test_path_is_file bar &&
1084        git stash pop &&
1085        test_path_is_file "foo bar" &&
1086        test_path_is_file foo &&
1087        test_path_is_file bar
1088'
1089
1090test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1091        git reset &&
1092        >foo &&
1093        >bar &&
1094        git add foo bar &&
1095        git commit -m "test" &&
1096        echo "foo" >foo &&
1097        echo "bar" >bar &&
1098        git stash -k -- foo &&
1099        test "",bar = $(cat foo),$(cat bar) &&
1100        git stash pop &&
1101        test foo,bar = $(cat foo),$(cat bar)
1102'
1103
1104test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1105        git reset &&
1106        >subdir/untracked &&
1107        >subdir/tracked1 &&
1108        >subdir/tracked2 &&
1109        git add subdir/tracked* &&
1110        git stash -- subdir/ &&
1111        test_path_is_missing subdir/tracked1 &&
1112        test_path_is_missing subdir/tracked2 &&
1113        test_path_is_file subdir/untracked &&
1114        git stash pop &&
1115        test_path_is_file subdir/tracked1 &&
1116        test_path_is_file subdir/tracked2 &&
1117        test_path_is_file subdir/untracked
1118'
1119
1120test_expect_success 'stash -- <subdir> works with binary files' '
1121        git reset &&
1122        >subdir/untracked &&
1123        >subdir/tracked &&
1124        cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1125        git add subdir/tracked* &&
1126        git stash -- subdir/ &&
1127        test_path_is_missing subdir/tracked &&
1128        test_path_is_missing subdir/tracked-binary &&
1129        test_path_is_file subdir/untracked &&
1130        git stash pop &&
1131        test_path_is_file subdir/tracked &&
1132        test_path_is_file subdir/tracked-binary &&
1133        test_path_is_file subdir/untracked
1134'
1135
1136test_expect_success 'stash with user.name and user.email set works' '
1137        test_config user.name "A U Thor" &&
1138        test_config user.email "a.u@thor" &&
1139        git stash
1140'
1141
1142test_expect_success 'stash works when user.name and user.email are not set' '
1143        git reset &&
1144        >1 &&
1145        git add 1 &&
1146        echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1147        git stash &&
1148        git show -s --format="%an <%ae>" refs/stash >actual &&
1149        test_cmp expect actual &&
1150        >2 &&
1151        git add 2 &&
1152        test_config user.useconfigonly true &&
1153        test_config stash.usebuiltin true &&
1154        (
1155                sane_unset GIT_AUTHOR_NAME &&
1156                sane_unset GIT_AUTHOR_EMAIL &&
1157                sane_unset GIT_COMMITTER_NAME &&
1158                sane_unset GIT_COMMITTER_EMAIL &&
1159                test_unconfig user.email &&
1160                test_unconfig user.name &&
1161                test_must_fail git commit -m "should fail" &&
1162                echo "git stash <git@stash>" >expect &&
1163                >2 &&
1164                git stash &&
1165                git show -s --format="%an <%ae>" refs/stash >actual &&
1166                test_cmp expect actual
1167        )
1168'
1169
1170test_done