3 # Copyright (c) 2007 Johannes E Schindelin
6 test_description
='Test git stash'
10 test_expect_success
'stash some dirty working directory' '
14 git commit -m initial &&
20 git diff-files --quiet &&
21 git diff-index --cached --quiet HEAD
25 diff --git a/file b/file
26 index 0cfbf08..00750ed 100644
34 test_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
40 test_expect_success
'applying bogus stash does nothing' '
41 test_must_fail git stash apply stash@{1} &&
46 test_expect_success
'apply does not need clean working directory' '
55 test_expect_success
'apply does not clobber working directory changes' '
58 test_must_fail git stash apply &&
63 test_expect_success
'apply stashed changes' '
68 git commit -m other-file &&
70 test 3 = $(cat file) &&
71 test 1 = $(git show :file) &&
72 test 1 = $(git show HEAD:file)
75 test_expect_success
'apply stashed changes (including index)' '
76 git reset --hard HEAD^ &&
77 echo 6 > other-file &&
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)
87 test_expect_success
'unstashing in a subdirectory' '
88 git reset --hard HEAD &&
96 test_expect_success
'drop top stash' '
98 git stash list > stashlist1 &&
102 git stash list > stashlist2 &&
103 test_cmp stashlist1 stashlist2 &&
105 test 3 = $(cat file) &&
106 test 1 = $(git show :file) &&
107 test 1 = $(git show HEAD:file)
110 test_expect_success
'drop middle stash' '
116 git stash drop stash@{1} &&
117 test 2 = $(git stash list | wc -l) &&
119 test 9 = $(cat file) &&
120 test 1 = $(git show :file) &&
121 test 1 = $(git show HEAD:file) &&
125 test 3 = $(cat file) &&
126 test 1 = $(git show :file) &&
127 test 1 = $(git show HEAD:file)
130 test_expect_success
'stash pop' '
133 test 3 = $(cat file) &&
134 test 1 = $(git show :file) &&
135 test 1 = $(git show HEAD:file) &&
136 test 0 = $(git stash list | wc -l)
140 diff --git a/file2 b/file2
142 index 0000000..1fe912c
150 diff --git a/file b/file
151 index 257cc56..5716ca5 100644
160 diff --git a/file b/file
161 index 7601807..5716ca5 100644
167 diff --git a/file2 b/file2
169 index 0000000..1fe912c
176 test_expect_success
'stash branch' '
178 git commit file -m first &&
184 git commit file -m second &&
185 git stash branch stashbranch &&
186 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
187 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
188 git diff --cached > output &&
189 test_cmp output expect &&
191 test_cmp output expect1 &&
193 git commit -m alternate\ second &&
194 git diff master..stashbranch > output &&
195 test_cmp output expect2 &&
196 test 0 = $(git stash list | wc -l)
199 test_expect_success
'apply -q is quiet' '
202 git stash apply -q > output.out 2>&1 &&
206 test_expect_success
'save -q is quiet' '
207 git stash save --quiet > output.out 2>&1 &&
211 test_expect_success
'pop -q is quiet' '
212 git stash pop -q > output.out 2>&1 &&
216 test_expect_success
'pop -q --index works and is quiet' '
219 git stash save --quiet &&
220 git stash pop -q --index > output.out 2>&1 &&
221 test foo = "$(git show :file)" &&
225 test_expect_success
'drop -q is quiet' '
227 git stash drop -q > output.out 2>&1 &&
231 test_expect_success
'stash -k' '
236 test bar,bar4 = $(cat file),$(cat file2)
239 test_expect_success
'stash --no-keep-index' '
241 echo bar44 > file2 &&
243 git stash --no-keep-index &&
244 test bar,bar2 = $(cat file),$(cat file2)
247 test_expect_success
'stash --invalid-option' '
251 test_must_fail git stash --invalid-option &&
252 test_must_fail git stash save --invalid-option &&
253 test bar5,bar6 = $(cat file),$(cat file2) &&
254 git stash -- -message-starting-with-dash &&
255 test bar,bar2 = $(cat file),$(cat file2)
258 test_expect_success
'stash an added file' '
262 git stash save "added file" &&
265 test new = "$(cat file3)"
268 test_expect_success
'stash rm then recreate' '
272 git stash save "rm then recreate" &&
273 test bar = "$(cat file)" &&
275 test bar7 = "$(cat file)"
278 test_expect_success
'stash rm and ignore' '
281 echo file >.gitignore &&
282 git stash save "rm and ignore" &&
283 test bar = "$(cat file)" &&
284 test file = "$(cat .gitignore)" &&
287 test file = "$(cat .gitignore)"
290 test_expect_success
'stash rm and ignore (stage .gitignore)' '
293 echo file >.gitignore &&
294 git add .gitignore &&
295 git stash save "rm and ignore (stage .gitignore)" &&
296 test bar = "$(cat file)" &&
297 ! test -r .gitignore &&
300 test file = "$(cat .gitignore)"
303 test_expect_success SYMLINKS
'stash file to symlink' '
307 git stash save "file to symlink" &&
309 test bar = "$(cat file)" &&
311 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
314 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
318 git stash save "file to symlink (stage rm)" &&
320 test bar = "$(cat file)" &&
322 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
325 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
330 git stash save "file to symlink (full stage)" &&
332 test bar = "$(cat file)" &&
334 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
337 # This test creates a commit with a symlink used for the following tests
339 test_expect_success SYMLINKS
'stash symlink to file' '
341 ln -s file filelink &&
343 git commit -m "Add symlink" &&
346 git stash save "symlink to file" &&
348 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
350 ! test -h filelink &&
351 test bar = "$(cat file)"
354 test_expect_success SYMLINKS
'stash symlink to file (stage rm)' '
358 git stash save "symlink to file (stage rm)" &&
360 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
362 ! test -h filelink &&
363 test bar = "$(cat file)"
366 test_expect_success SYMLINKS
'stash symlink to file (full stage)' '
371 git stash save "symlink to file (full stage)" &&
373 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
375 ! test -h filelink &&
376 test bar = "$(cat file)"
379 test_expect_failure
'stash directory to file' '
382 echo foo >dir/file &&
384 git commit -m "Add file in dir" &&
387 git stash save "directory to file" &&
389 test foo = "$(cat dir/file)" &&
390 test_must_fail git stash apply &&
391 test bar = "$(cat dir)" &&
392 git reset --soft HEAD^
395 test_expect_failure
'stash file to directory' '
399 echo foo >file/file &&
400 git stash save "file to directory" &&
402 test bar = "$(cat file)" &&
405 test foo = "$(cat file/file)"
408 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
410 test_when_finished "git reset --hard HEAD" &&
413 STASH_ID=$(git stash create) &&
415 git stash branch stash-branch ${STASH_ID} &&
416 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
417 test $(git ls-files --modified | wc -l) -eq 1
420 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
422 test_when_finished "git reset --hard HEAD" &&
426 test_when_finished "git stash drop" &&
428 STASH_ID=$(git stash create) &&
430 git stash branch stash-branch ${STASH_ID} &&
431 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
432 test $(git ls-files --modified | wc -l) -eq 1
435 test_expect_success
'stash show - stashes on stack, stash-like argument' '
437 test_when_finished "git reset --hard HEAD" &&
441 test_when_finished "git stash drop" &&
443 STASH_ID=$(git stash create) &&
445 cat >expected <<-EOF &&
447 1 files changed, 1 insertions(+), 0 deletions(-)
449 git stash show ${STASH_ID} >actual &&
450 test_cmp expected actual
453 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
455 test_when_finished "git reset --hard HEAD" &&
459 test_when_finished "git stash drop" &&
461 STASH_ID=$(git stash create) &&
463 cat >expected <<-EOF &&
464 diff --git a/file b/file
465 index 7601807..935fbd3 100644
472 git stash show -p ${STASH_ID} >actual &&
473 test_cmp expected actual
476 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
478 test_when_finished "git reset --hard HEAD" &&
481 STASH_ID=$(git stash create) &&
483 cat >expected <<-EOF &&
485 1 files changed, 1 insertions(+), 0 deletions(-)
487 git stash show ${STASH_ID} >actual &&
488 test_cmp expected actual
491 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
493 test_when_finished "git reset --hard HEAD" &&
496 STASH_ID=$(git stash create) &&
498 cat >expected <<-EOF &&
499 diff --git a/file b/file
500 index 7601807..71b52c4 100644
507 git stash show -p ${STASH_ID} >actual &&
508 test_cmp expected actual
511 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
513 test_when_finished "git reset --hard HEAD && git stash clear" &&
519 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
521 test bar = "$(cat file)" &&
522 git reset --hard HEAD
525 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
527 test_when_finished "git reset --hard HEAD && git stash clear" &&
533 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
535 test bar = "$(cat file)" &&
536 git reset --hard HEAD
539 test_expect_success
'ref with non-existent reflog' '
545 ! "git rev-parse --quiet --verify does-not-exist" &&
546 test_must_fail git stash drop does-not-exist &&
547 test_must_fail git stash drop does-not-exist@{0} &&
548 test_must_fail git stash pop does-not-exist &&
549 test_must_fail git stash pop does-not-exist@{0} &&
550 test_must_fail git stash apply does-not-exist &&
551 test_must_fail git stash apply does-not-exist@{0} &&
552 test_must_fail git stash show does-not-exist &&
553 test_must_fail git stash show does-not-exist@{0} &&
554 test_must_fail git stash branch tmp does-not-exist &&
555 test_must_fail git stash branch tmp does-not-exist@{0} &&
559 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
561 test_must_fail git stash drop stash@{0} &&
566 test_must_fail git stash drop stash@{1} &&
567 test_must_fail git stash pop stash@{1} &&
568 test_must_fail git stash apply stash@{1} &&
569 test_must_fail git stash show stash@{1} &&
570 test_must_fail git stash branch tmp stash@{1} &&
574 test_expect_success
'stash branch should not drop the stash if the branch exists' '
578 git commit -m initial &&
581 test_must_fail git stash branch master stash@{0} &&
582 git rev-parse stash@{0} --
585 test_expect_success
'stash apply shows status same as git status (relative to current directory)' '
587 echo 1 >subdir/subfile1 &&
588 echo 2 >subdir/subfile2 &&
589 git add subdir/subfile1 &&
590 git commit -m subdir &&
595 git status >../expect &&
597 sane_unset GIT_MERGE_VERBOSITY &&
600 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
601 test_cmp expect actual