t / t3903-stash.shon commit Merge branch 'cb/ls-files-cdup' (e091512)
   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        test_tick &&
  14        git commit -m initial &&
  15        echo 2 > file &&
  16        git add file &&
  17        echo 3 > file &&
  18        test_tick &&
  19        git stash &&
  20        git diff-files --quiet &&
  21        git diff-index --cached --quiet HEAD
  22'
  23
  24cat > expect << EOF
  25diff --git a/file b/file
  26index 0cfbf08..00750ed 100644
  27--- a/file
  28+++ b/file
  29@@ -1 +1 @@
  30-2
  31+3
  32EOF
  33
  34test_expect_success 'parents of stash' '
  35        test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
  36        git diff stash^2..stash > output &&
  37        test_cmp output expect
  38'
  39
  40test_expect_success 'apply needs clean working directory' '
  41        echo 4 > other-file &&
  42        git add other-file &&
  43        echo 5 > other-file &&
  44        test_must_fail git stash apply
  45'
  46
  47test_expect_success 'apply stashed changes' '
  48        git add other-file &&
  49        test_tick &&
  50        git commit -m other-file &&
  51        git stash apply &&
  52        test 3 = $(cat file) &&
  53        test 1 = $(git show :file) &&
  54        test 1 = $(git show HEAD:file)
  55'
  56
  57test_expect_success 'apply stashed changes (including index)' '
  58        git reset --hard HEAD^ &&
  59        echo 6 > other-file &&
  60        git add other-file &&
  61        test_tick &&
  62        git commit -m other-file &&
  63        git stash apply --index &&
  64        test 3 = $(cat file) &&
  65        test 2 = $(git show :file) &&
  66        test 1 = $(git show HEAD:file)
  67'
  68
  69test_expect_success 'unstashing in a subdirectory' '
  70        git reset --hard HEAD &&
  71        mkdir subdir &&
  72        cd subdir &&
  73        git stash apply &&
  74        cd ..
  75'
  76
  77test_expect_success 'drop top stash' '
  78        git reset --hard &&
  79        git stash list > stashlist1 &&
  80        echo 7 > file &&
  81        git stash &&
  82        git stash drop &&
  83        git stash list > stashlist2 &&
  84        diff stashlist1 stashlist2 &&
  85        git stash apply &&
  86        test 3 = $(cat file) &&
  87        test 1 = $(git show :file) &&
  88        test 1 = $(git show HEAD:file)
  89'
  90
  91test_expect_success 'drop middle stash' '
  92        git reset --hard &&
  93        echo 8 > file &&
  94        git stash &&
  95        echo 9 > file &&
  96        git stash &&
  97        git stash drop stash@{1} &&
  98        test 2 = $(git stash list | wc -l) &&
  99        git stash apply &&
 100        test 9 = $(cat file) &&
 101        test 1 = $(git show :file) &&
 102        test 1 = $(git show HEAD:file) &&
 103        git reset --hard &&
 104        git stash drop &&
 105        git stash apply &&
 106        test 3 = $(cat file) &&
 107        test 1 = $(git show :file) &&
 108        test 1 = $(git show HEAD:file)
 109'
 110
 111test_expect_success 'stash pop' '
 112        git reset --hard &&
 113        git stash pop &&
 114        test 3 = $(cat file) &&
 115        test 1 = $(git show :file) &&
 116        test 1 = $(git show HEAD:file) &&
 117        test 0 = $(git stash list | wc -l)
 118'
 119
 120cat > expect << EOF
 121diff --git a/file2 b/file2
 122new file mode 100644
 123index 0000000..1fe912c
 124--- /dev/null
 125+++ b/file2
 126@@ -0,0 +1 @@
 127+bar2
 128EOF
 129
 130cat > expect1 << EOF
 131diff --git a/file b/file
 132index 257cc56..5716ca5 100644
 133--- a/file
 134+++ b/file
 135@@ -1 +1 @@
 136-foo
 137+bar
 138EOF
 139
 140cat > expect2 << EOF
 141diff --git a/file b/file
 142index 7601807..5716ca5 100644
 143--- a/file
 144+++ b/file
 145@@ -1 +1 @@
 146-baz
 147+bar
 148diff --git a/file2 b/file2
 149new file mode 100644
 150index 0000000..1fe912c
 151--- /dev/null
 152+++ b/file2
 153@@ -0,0 +1 @@
 154+bar2
 155EOF
 156
 157test_expect_success 'stash branch' '
 158        echo foo > file &&
 159        git commit file -m first
 160        echo bar > file &&
 161        echo bar2 > file2 &&
 162        git add file2 &&
 163        git stash &&
 164        echo baz > file &&
 165        git commit file -m second &&
 166        git stash branch stashbranch &&
 167        test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
 168        test $(git rev-parse HEAD) = $(git rev-parse master^) &&
 169        git diff --cached > output &&
 170        test_cmp output expect &&
 171        git diff > output &&
 172        test_cmp output expect1 &&
 173        git add file &&
 174        git commit -m alternate\ second &&
 175        git diff master..stashbranch > output &&
 176        test_cmp output expect2 &&
 177        test 0 = $(git stash list | wc -l)
 178'
 179
 180test_expect_success 'apply -q is quiet' '
 181        echo foo > file &&
 182        git stash &&
 183        git stash apply -q > output.out 2>&1 &&
 184        test ! -s output.out
 185'
 186
 187test_expect_success 'save -q is quiet' '
 188        git stash save --quiet > output.out 2>&1 &&
 189        test ! -s output.out
 190'
 191
 192test_expect_success 'pop -q is quiet' '
 193        git stash pop -q > output.out 2>&1 &&
 194        test ! -s output.out
 195'
 196
 197test_expect_success 'pop -q --index works and is quiet' '
 198        echo foo > file &&
 199        git add file &&
 200        git stash save --quiet &&
 201        git stash pop -q --index > output.out 2>&1 &&
 202        test foo = "$(git show :file)" &&
 203        test ! -s output.out
 204'
 205
 206test_expect_success 'drop -q is quiet' '
 207        git stash &&
 208        git stash drop -q > output.out 2>&1 &&
 209        test ! -s output.out
 210'
 211
 212test_expect_success 'stash -k' '
 213        echo bar3 > file &&
 214        echo bar4 > file2 &&
 215        git add file2 &&
 216        git stash -k &&
 217        test bar,bar4 = $(cat file),$(cat file2)
 218'
 219
 220test_expect_success 'stash --invalid-option' '
 221        echo bar5 > file &&
 222        echo bar6 > file2 &&
 223        git add file2 &&
 224        test_must_fail git stash --invalid-option &&
 225        test_must_fail git stash save --invalid-option &&
 226        test bar5,bar6 = $(cat file),$(cat file2) &&
 227        git stash -- -message-starting-with-dash &&
 228        test bar,bar2 = $(cat file),$(cat file2)
 229'
 230
 231test_expect_success 'stash an added file' '
 232        git reset --hard &&
 233        echo new >file3 &&
 234        git add file3 &&
 235        git stash save "added file" &&
 236        ! test -r file3 &&
 237        git stash apply &&
 238        test new = "$(cat file3)"
 239'
 240
 241test_expect_success 'stash rm then recreate' '
 242        git reset --hard &&
 243        git rm file &&
 244        echo bar7 >file &&
 245        git stash save "rm then recreate" &&
 246        test bar = "$(cat file)" &&
 247        git stash apply &&
 248        test bar7 = "$(cat file)"
 249'
 250
 251test_expect_success 'stash rm and ignore' '
 252        git reset --hard &&
 253        git rm file &&
 254        echo file >.gitignore &&
 255        git stash save "rm and ignore" &&
 256        test bar = "$(cat file)" &&
 257        test file = "$(cat .gitignore)"
 258        git stash apply &&
 259        ! test -r file &&
 260        test file = "$(cat .gitignore)"
 261'
 262
 263test_expect_success 'stash rm and ignore (stage .gitignore)' '
 264        git reset --hard &&
 265        git rm file &&
 266        echo file >.gitignore &&
 267        git add .gitignore &&
 268        git stash save "rm and ignore (stage .gitignore)" &&
 269        test bar = "$(cat file)" &&
 270        ! test -r .gitignore
 271        git stash apply &&
 272        ! test -r file &&
 273        test file = "$(cat .gitignore)"
 274'
 275
 276test_expect_success SYMLINKS 'stash file to symlink' '
 277        git reset --hard &&
 278        rm file &&
 279        ln -s file2 file &&
 280        git stash save "file to symlink" &&
 281        test -f file &&
 282        test bar = "$(cat file)" &&
 283        git stash apply &&
 284        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 285'
 286
 287test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
 288        git reset --hard &&
 289        git rm file &&
 290        ln -s file2 file &&
 291        git stash save "file to symlink (stage rm)" &&
 292        test -f file &&
 293        test bar = "$(cat file)" &&
 294        git stash apply &&
 295        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 296'
 297
 298test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
 299        git reset --hard &&
 300        rm file &&
 301        ln -s file2 file &&
 302        git add file &&
 303        git stash save "file to symlink (full stage)" &&
 304        test -f file &&
 305        test bar = "$(cat file)" &&
 306        git stash apply &&
 307        case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
 308'
 309
 310# This test creates a commit with a symlink used for the following tests
 311
 312test_expect_success SYMLINKS 'stash symlink to file' '
 313        git reset --hard &&
 314        ln -s file filelink &&
 315        git add filelink &&
 316        git commit -m "Add symlink" &&
 317        rm filelink &&
 318        cp file filelink &&
 319        git stash save "symlink to file" &&
 320        test -h filelink &&
 321        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
 322        git stash apply &&
 323        ! test -h filelink &&
 324        test bar = "$(cat file)"
 325'
 326
 327test_expect_success SYMLINKS 'stash symlink to file (stage rm)' '
 328        git reset --hard &&
 329        git rm filelink &&
 330        cp file filelink &&
 331        git stash save "symlink to file (stage rm)" &&
 332        test -h filelink &&
 333        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
 334        git stash apply &&
 335        ! test -h filelink &&
 336        test bar = "$(cat file)"
 337'
 338
 339test_expect_success SYMLINKS 'stash symlink to file (full stage)' '
 340        git reset --hard &&
 341        rm filelink &&
 342        cp file filelink &&
 343        git add filelink &&
 344        git stash save "symlink to file (full stage)" &&
 345        test -h filelink &&
 346        case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
 347        git stash apply &&
 348        ! test -h filelink &&
 349        test bar = "$(cat file)"
 350'
 351
 352test_expect_failure 'stash directory to file' '
 353        git reset --hard &&
 354        mkdir dir &&
 355        echo foo >dir/file &&
 356        git add dir/file &&
 357        git commit -m "Add file in dir" &&
 358        rm -fr dir &&
 359        echo bar >dir &&
 360        git stash save "directory to file" &&
 361        test -d dir &&
 362        test foo = "$(cat dir/file)" &&
 363        test_must_fail git stash apply &&
 364        test bar = "$(cat dir)" &&
 365        git reset --soft HEAD^
 366'
 367
 368test_expect_failure 'stash file to directory' '
 369        git reset --hard &&
 370        rm file &&
 371        mkdir file &&
 372        echo foo >file/file &&
 373        git stash save "file to directory" &&
 374        test -f file &&
 375        test bar = "$(cat file)" &&
 376        git stash apply &&
 377        test -f file/file &&
 378        test foo = "$(cat file/file)"
 379'
 380
 381test_done