3 # Copyright (c) 2007 Johannes E Schindelin
6 test_description
='Test git stash'
10 test_expect_success
'stash some dirty working directory' '
13 echo unrelated >other-file &&
16 git commit -m initial &&
22 git diff-files --quiet &&
23 git diff-index --cached --quiet HEAD
27 diff --git a/file b/file
28 index 0cfbf08..00750ed 100644
36 test_expect_success
'parents of stash' '
37 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
38 git diff stash^2..stash > output &&
39 test_cmp output expect
42 test_expect_success
'applying bogus stash does nothing' '
43 test_must_fail git stash apply stash@{1} &&
48 test_expect_success
'apply requires a clean index' '
49 test_when_finished "git reset --hard" &&
50 echo changed >other-file &&
52 test_must_fail git stash apply
55 test_expect_success
'apply does not need clean working directory' '
62 test_expect_success
'apply does not clobber working directory changes' '
65 test_must_fail git stash apply &&
70 test_expect_success
'apply stashed changes' '
75 git commit -m other-file &&
77 test 3 = $(cat file) &&
78 test 1 = $(git show :file) &&
79 test 1 = $(git show HEAD:file)
82 test_expect_success
'apply stashed changes (including index)' '
83 git reset --hard HEAD^ &&
84 echo 6 > other-file &&
87 git commit -m other-file &&
88 git stash apply --index &&
89 test 3 = $(cat file) &&
90 test 2 = $(git show :file) &&
91 test 1 = $(git show HEAD:file)
94 test_expect_success
'unstashing in a subdirectory' '
95 git reset --hard HEAD &&
103 test_expect_success
'drop top stash' '
105 git stash list > stashlist1 &&
109 git stash list > stashlist2 &&
110 test_cmp stashlist1 stashlist2 &&
112 test 3 = $(cat file) &&
113 test 1 = $(git show :file) &&
114 test 1 = $(git show HEAD:file)
117 test_expect_success
'drop middle stash' '
123 git stash drop stash@{1} &&
124 test 2 = $(git stash list | wc -l) &&
126 test 9 = $(cat file) &&
127 test 1 = $(git show :file) &&
128 test 1 = $(git show HEAD:file) &&
132 test 3 = $(cat file) &&
133 test 1 = $(git show :file) &&
134 test 1 = $(git show HEAD:file)
137 test_expect_success
'stash pop' '
140 test 3 = $(cat file) &&
141 test 1 = $(git show :file) &&
142 test 1 = $(git show HEAD:file) &&
143 test 0 = $(git stash list | wc -l)
147 diff --git a/file2 b/file2
149 index 0000000..1fe912c
157 diff --git a/file b/file
158 index 257cc56..5716ca5 100644
167 diff --git a/file b/file
168 index 7601807..5716ca5 100644
174 diff --git a/file2 b/file2
176 index 0000000..1fe912c
183 test_expect_success
'stash branch' '
185 git commit file -m first &&
191 git commit file -m second &&
192 git stash branch stashbranch &&
193 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
194 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
195 git diff --cached > output &&
196 test_cmp output expect &&
198 test_cmp output expect1 &&
200 git commit -m alternate\ second &&
201 git diff master..stashbranch > output &&
202 test_cmp output expect2 &&
203 test 0 = $(git stash list | wc -l)
206 test_expect_success
'apply -q is quiet' '
209 git stash apply -q > output.out 2>&1 &&
210 test_must_be_empty output.out
213 test_expect_success
'save -q is quiet' '
214 git stash save --quiet > output.out 2>&1 &&
215 test_must_be_empty output.out
218 test_expect_success
'pop -q is quiet' '
219 git stash pop -q > output.out 2>&1 &&
220 test_must_be_empty output.out
223 test_expect_success
'pop -q --index works and is quiet' '
226 git stash save --quiet &&
227 git stash pop -q --index > output.out 2>&1 &&
228 test foo = "$(git show :file)" &&
229 test_must_be_empty output.out
232 test_expect_success
'drop -q is quiet' '
234 git stash drop -q > output.out 2>&1 &&
235 test_must_be_empty output.out
238 test_expect_success
'stash -k' '
243 test bar,bar4 = $(cat file),$(cat file2)
246 test_expect_success
'stash --no-keep-index' '
248 echo bar44 > file2 &&
250 git stash --no-keep-index &&
251 test bar,bar2 = $(cat file),$(cat file2)
254 test_expect_success
'stash --invalid-option' '
258 test_must_fail git stash --invalid-option &&
259 test_must_fail git stash save --invalid-option &&
260 test bar5,bar6 = $(cat file),$(cat file2) &&
261 git stash -- -message-starting-with-dash &&
262 test bar,bar2 = $(cat file),$(cat file2)
265 test_expect_success
'stash an added file' '
269 git stash save "added file" &&
272 test new = "$(cat file3)"
275 test_expect_success
'stash rm then recreate' '
279 git stash save "rm then recreate" &&
280 test bar = "$(cat file)" &&
282 test bar7 = "$(cat file)"
285 test_expect_success
'stash rm and ignore' '
288 echo file >.gitignore &&
289 git stash save "rm and ignore" &&
290 test bar = "$(cat file)" &&
291 test file = "$(cat .gitignore)" &&
294 test file = "$(cat .gitignore)"
297 test_expect_success
'stash rm and ignore (stage .gitignore)' '
300 echo file >.gitignore &&
301 git add .gitignore &&
302 git stash save "rm and ignore (stage .gitignore)" &&
303 test bar = "$(cat file)" &&
304 ! test -r .gitignore &&
307 test file = "$(cat .gitignore)"
310 test_expect_success SYMLINKS
'stash file to symlink' '
314 git stash save "file to symlink" &&
316 test bar = "$(cat file)" &&
318 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
321 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
325 git stash save "file to symlink (stage rm)" &&
327 test bar = "$(cat file)" &&
329 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
332 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
337 git stash save "file to symlink (full stage)" &&
339 test bar = "$(cat file)" &&
341 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
344 # This test creates a commit with a symlink used for the following tests
346 test_expect_success
'stash symlink to file' '
348 test_ln_s_add file filelink &&
349 git commit -m "Add symlink" &&
352 git stash save "symlink to file"
355 test_expect_success SYMLINKS
'this must have re-created the symlink' '
357 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
360 test_expect_success
'unstash must re-create the file' '
362 ! test -h filelink &&
363 test bar = "$(cat file)"
366 test_expect_success
'stash symlink to file (stage rm)' '
370 git stash save "symlink to file (stage rm)"
373 test_expect_success SYMLINKS
'this must have re-created the symlink' '
375 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
378 test_expect_success
'unstash must re-create the file' '
380 ! test -h filelink &&
381 test bar = "$(cat file)"
384 test_expect_success
'stash symlink to file (full stage)' '
389 git stash save "symlink to file (full stage)"
392 test_expect_success SYMLINKS
'this must have re-created the symlink' '
394 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
397 test_expect_success
'unstash must re-create the file' '
399 ! test -h filelink &&
400 test bar = "$(cat file)"
403 test_expect_failure
'stash directory to file' '
406 echo foo >dir/file &&
408 git commit -m "Add file in dir" &&
411 git stash save "directory to file" &&
413 test foo = "$(cat dir/file)" &&
414 test_must_fail git stash apply &&
415 test bar = "$(cat dir)" &&
416 git reset --soft HEAD^
419 test_expect_failure
'stash file to directory' '
423 echo foo >file/file &&
424 git stash save "file to directory" &&
426 test bar = "$(cat file)" &&
429 test foo = "$(cat file/file)"
432 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
434 test_when_finished "git reset --hard HEAD" &&
437 STASH_ID=$(git stash create) &&
439 git stash branch stash-branch ${STASH_ID} &&
440 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
441 test $(git ls-files --modified | wc -l) -eq 1
444 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
446 test_when_finished "git reset --hard HEAD" &&
450 test_when_finished "git stash drop" &&
452 STASH_ID=$(git stash create) &&
454 git stash branch stash-branch ${STASH_ID} &&
455 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
456 test $(git ls-files --modified | wc -l) -eq 1
459 test_expect_success
'stash show format defaults to --stat' '
461 test_when_finished "git reset --hard HEAD" &&
465 test_when_finished "git stash drop" &&
467 STASH_ID=$(git stash create) &&
469 cat >expected <<-EOF &&
471 1 file changed, 1 insertion(+)
473 git stash show ${STASH_ID} >actual &&
474 test_i18ncmp expected actual
477 test_expect_success
'stash show - stashes on stack, stash-like argument' '
479 test_when_finished "git reset --hard HEAD" &&
483 test_when_finished "git stash drop" &&
485 STASH_ID=$(git stash create) &&
487 echo "1 0 file" >expected &&
488 git stash show --numstat ${STASH_ID} >actual &&
489 test_cmp expected actual
492 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
494 test_when_finished "git reset --hard HEAD" &&
498 test_when_finished "git stash drop" &&
500 STASH_ID=$(git stash create) &&
502 cat >expected <<-EOF &&
503 diff --git a/file b/file
504 index 7601807..935fbd3 100644
511 git stash show -p ${STASH_ID} >actual &&
512 test_cmp expected actual
515 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
517 test_when_finished "git reset --hard HEAD" &&
520 STASH_ID=$(git stash create) &&
522 echo "1 0 file" >expected &&
523 git stash show --numstat ${STASH_ID} >actual &&
524 test_cmp expected actual
527 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
529 test_when_finished "git reset --hard HEAD" &&
532 STASH_ID=$(git stash create) &&
534 cat >expected <<-EOF &&
535 diff --git a/file b/file
536 index 7601807..71b52c4 100644
543 git stash show -p ${STASH_ID} >actual &&
544 test_cmp expected actual
547 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
549 test_when_finished "git reset --hard HEAD && git stash clear" &&
555 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
557 test bar = "$(cat file)" &&
558 git reset --hard HEAD
561 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
563 test_when_finished "git reset --hard HEAD && git stash clear" &&
569 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
571 test bar = "$(cat file)" &&
572 git reset --hard HEAD
575 test_expect_success
'ref with non-existent reflog' '
581 test_must_fail git rev-parse --quiet --verify does-not-exist &&
582 test_must_fail git stash drop does-not-exist &&
583 test_must_fail git stash drop does-not-exist@{0} &&
584 test_must_fail git stash pop does-not-exist &&
585 test_must_fail git stash pop does-not-exist@{0} &&
586 test_must_fail git stash apply does-not-exist &&
587 test_must_fail git stash apply does-not-exist@{0} &&
588 test_must_fail git stash show does-not-exist &&
589 test_must_fail git stash show does-not-exist@{0} &&
590 test_must_fail git stash branch tmp does-not-exist &&
591 test_must_fail git stash branch tmp does-not-exist@{0} &&
595 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
597 test_must_fail git stash drop stash@{0} &&
602 test_must_fail git stash drop stash@{1} &&
603 test_must_fail git stash pop stash@{1} &&
604 test_must_fail git stash apply stash@{1} &&
605 test_must_fail git stash show stash@{1} &&
606 test_must_fail git stash branch tmp stash@{1} &&
610 test_expect_success
'stash branch should not drop the stash if the branch exists' '
614 git commit -m initial &&
617 test_must_fail git stash branch master stash@{0} &&
618 git rev-parse stash@{0} --
621 test_expect_success
'stash apply shows status same as git status (relative to current directory)' '
623 echo 1 >subdir/subfile1 &&
624 echo 2 >subdir/subfile2 &&
625 git add subdir/subfile1 &&
626 git commit -m subdir &&
631 git status >../expect &&
633 sane_unset GIT_MERGE_VERBOSITY &&
636 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
637 test_i18ncmp expect actual
641 diff --git a/HEAD b/HEAD
643 index 0000000..fe0cbee
650 test_expect_success
'stash where working directory contains "HEAD" file' '
653 echo file-not-a-ref > HEAD &&
657 git diff-files --quiet &&
658 git diff-index --cached --quiet HEAD &&
659 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
660 git diff stash^..stash > output &&
661 test_cmp output expect
664 test_expect_success
'store called with invalid commit' '
665 test_must_fail git stash store foo
668 test_expect_success
'store updates stash ref and reflog' '
673 STASH_ID=$(git stash create) &&
676 git stash store -m quuxery $STASH_ID &&
677 test $(cat .git/refs/stash) = $STASH_ID &&
678 grep $STASH_ID .git/logs/refs/stash &&
683 test_expect_success
'handle stash specification with spaces' '
687 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
691 git stash apply "stash@{$stamp}" &&
695 test_expect_success
'setup stash with index and worktree changes' '
700 echo working >file &&
704 test_expect_success
'stash list implies --first-parent -m' '
705 cat >expect <<-EOF &&
708 diff --git a/file b/file
709 index 257cc56..d26b33d 100644
716 git stash list --format=%gd -p >actual &&
717 test_cmp expect actual
720 test_expect_success
'stash list --cc shows combined diff' '
721 cat >expect <<-\EOF &&
725 index 257cc56,9015a7a..d26b33d
728 @@@ -1,1 -1,1 +1,1 @@@
733 git stash list --format=%gd -p --cc >actual &&
734 test_cmp expect actual