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