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 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
'stash drop complains of extra options' '
97 test_must_fail git stash drop --foo
100 test_expect_success
'drop top stash' '
102 git stash list > stashlist1 &&
106 git stash list > stashlist2 &&
107 test_cmp stashlist1 stashlist2 &&
109 test 3 = $(cat file) &&
110 test 1 = $(git show :file) &&
111 test 1 = $(git show HEAD:file)
114 test_expect_success
'drop middle stash' '
121 git stash drop stash@{1} &&
122 test 2 = $(git stash list | wc -l) &&
124 test 9 = $(cat file) &&
125 test 1 = $(git show :file) &&
126 test 1 = $(git show HEAD:file) &&
130 test 3 = $(cat file) &&
131 test 1 = $(git show :file) &&
132 test 1 = $(git show HEAD:file)
135 test_expect_success
'stash pop' '
138 test 3 = $(cat file) &&
139 test 1 = $(git show :file) &&
140 test 1 = $(git show HEAD:file) &&
141 test 0 = $(git stash list | wc -l)
145 diff --git a/file2 b/file2
147 index 0000000..1fe912c
155 diff --git a/file b/file
156 index 257cc56..5716ca5 100644
165 diff --git a/file b/file
166 index 7601807..5716ca5 100644
172 diff --git a/file2 b/file2
174 index 0000000..1fe912c
181 test_expect_success
'stash branch' '
183 git commit file -m first &&
189 git commit file -m second &&
190 git stash branch stashbranch &&
191 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
192 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
193 git diff --cached > output &&
194 test_cmp output expect &&
196 test_cmp output expect1 &&
198 git commit -m alternate\ second &&
199 git diff master..stashbranch > output &&
200 test_cmp output expect2 &&
201 test 0 = $(git stash list | wc -l)
204 test_expect_success
'apply -q is quiet' '
207 git stash apply -q > output.out 2>&1 &&
208 test_must_be_empty output.out
211 test_expect_success
'save -q is quiet' '
212 git stash save --quiet > output.out 2>&1 &&
213 test_must_be_empty output.out
216 test_expect_success
'pop -q is quiet' '
217 git stash pop -q > output.out 2>&1 &&
218 test_must_be_empty output.out
221 test_expect_success
'pop -q --index works and is quiet' '
224 git stash save --quiet &&
225 git stash pop -q --index > output.out 2>&1 &&
226 test foo = "$(git show :file)" &&
227 test_must_be_empty output.out
230 test_expect_success
'drop -q is quiet' '
232 git stash drop -q > output.out 2>&1 &&
233 test_must_be_empty output.out
236 test_expect_success
'stash -k' '
241 test bar,bar4 = $(cat file),$(cat file2)
244 test_expect_success
'stash --no-keep-index' '
246 echo bar44 > file2 &&
248 git stash --no-keep-index &&
249 test bar,bar2 = $(cat file),$(cat file2)
252 test_expect_success
'stash --invalid-option' '
256 test_must_fail git stash --invalid-option &&
257 test_must_fail git stash save --invalid-option &&
258 test bar5,bar6 = $(cat file),$(cat file2) &&
259 git stash -- -message-starting-with-dash &&
260 test bar,bar2 = $(cat file),$(cat file2)
263 test_expect_success
'stash an added file' '
267 git stash save "added file" &&
270 test new = "$(cat file3)"
273 test_expect_success
'stash rm then recreate' '
277 git stash save "rm then recreate" &&
278 test bar = "$(cat file)" &&
280 test bar7 = "$(cat file)"
283 test_expect_success
'stash rm and ignore' '
286 echo file >.gitignore &&
287 git stash save "rm and ignore" &&
288 test bar = "$(cat file)" &&
289 test file = "$(cat .gitignore)" &&
292 test file = "$(cat .gitignore)"
295 test_expect_success
'stash rm and ignore (stage .gitignore)' '
298 echo file >.gitignore &&
299 git add .gitignore &&
300 git stash save "rm and ignore (stage .gitignore)" &&
301 test bar = "$(cat file)" &&
302 ! test -r .gitignore &&
305 test file = "$(cat .gitignore)"
308 test_expect_success SYMLINKS
'stash file to symlink' '
312 git stash save "file to symlink" &&
314 test bar = "$(cat file)" &&
316 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
319 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
323 git stash save "file to symlink (stage rm)" &&
325 test bar = "$(cat file)" &&
327 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
330 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
335 git stash save "file to symlink (full stage)" &&
337 test bar = "$(cat file)" &&
339 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
342 # This test creates a commit with a symlink used for the following tests
344 test_expect_success
'stash symlink to file' '
346 test_ln_s_add file filelink &&
347 git commit -m "Add symlink" &&
350 git stash save "symlink to file"
353 test_expect_success SYMLINKS
'this must have re-created the symlink' '
355 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
358 test_expect_success
'unstash must re-create the file' '
360 ! test -h filelink &&
361 test bar = "$(cat file)"
364 test_expect_success
'stash symlink to file (stage rm)' '
368 git stash save "symlink to file (stage rm)"
371 test_expect_success SYMLINKS
'this must have re-created the symlink' '
373 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
376 test_expect_success
'unstash must re-create the file' '
378 ! test -h filelink &&
379 test bar = "$(cat file)"
382 test_expect_success
'stash symlink to file (full stage)' '
387 git stash save "symlink to file (full stage)"
390 test_expect_success SYMLINKS
'this must have re-created the symlink' '
392 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
395 test_expect_success
'unstash must re-create the file' '
397 ! test -h filelink &&
398 test bar = "$(cat file)"
401 test_expect_failure
'stash directory to file' '
404 echo foo >dir/file &&
406 git commit -m "Add file in dir" &&
409 git stash save "directory to file" &&
411 test foo = "$(cat dir/file)" &&
412 test_must_fail git stash apply &&
413 test bar = "$(cat dir)" &&
414 git reset --soft HEAD^
417 test_expect_failure
'stash file to directory' '
421 echo foo >file/file &&
422 git stash save "file to directory" &&
424 test bar = "$(cat file)" &&
427 test foo = "$(cat file/file)"
430 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
432 test_when_finished "git reset --hard HEAD" &&
435 STASH_ID=$(git stash create) &&
437 git stash branch stash-branch ${STASH_ID} &&
438 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
439 test $(git ls-files --modified | wc -l) -eq 1
442 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
444 test_when_finished "git reset --hard HEAD" &&
448 test_when_finished "git stash drop" &&
450 STASH_ID=$(git stash create) &&
452 git stash branch stash-branch ${STASH_ID} &&
453 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
454 test $(git ls-files --modified | wc -l) -eq 1
457 test_expect_success
'stash show format defaults to --stat' '
459 test_when_finished "git reset --hard HEAD" &&
463 test_when_finished "git stash drop" &&
465 STASH_ID=$(git stash create) &&
467 cat >expected <<-EOF &&
469 1 file changed, 1 insertion(+)
471 git stash show ${STASH_ID} >actual &&
472 test_i18ncmp expected actual
475 test_expect_success
'stash show - stashes on stack, stash-like argument' '
477 test_when_finished "git reset --hard HEAD" &&
481 test_when_finished "git stash drop" &&
483 STASH_ID=$(git stash create) &&
485 echo "1 0 file" >expected &&
486 git stash show --numstat ${STASH_ID} >actual &&
487 test_cmp expected actual
490 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
492 test_when_finished "git reset --hard HEAD" &&
496 test_when_finished "git stash drop" &&
498 STASH_ID=$(git stash create) &&
500 cat >expected <<-EOF &&
501 diff --git a/file b/file
502 index 7601807..935fbd3 100644
509 git stash show -p ${STASH_ID} >actual &&
510 test_cmp expected actual
513 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
515 test_when_finished "git reset --hard HEAD" &&
518 STASH_ID=$(git stash create) &&
520 echo "1 0 file" >expected &&
521 git stash show --numstat ${STASH_ID} >actual &&
522 test_cmp expected actual
525 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
527 test_when_finished "git reset --hard HEAD" &&
530 STASH_ID=$(git stash create) &&
532 cat >expected <<-EOF &&
533 diff --git a/file b/file
534 index 7601807..71b52c4 100644
541 git stash show -p ${STASH_ID} >actual &&
542 test_cmp expected actual
545 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
547 test_when_finished "git reset --hard HEAD && git stash clear" &&
553 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
555 test bar = "$(cat file)" &&
556 git reset --hard HEAD
559 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
561 test_when_finished "git reset --hard HEAD && git stash clear" &&
567 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
569 test bar = "$(cat file)" &&
570 git reset --hard HEAD
573 test_expect_success
'ref with non-existent reflog' '
579 test_must_fail git rev-parse --quiet --verify does-not-exist &&
580 test_must_fail git stash drop does-not-exist &&
581 test_must_fail git stash drop does-not-exist@{0} &&
582 test_must_fail git stash pop does-not-exist &&
583 test_must_fail git stash pop does-not-exist@{0} &&
584 test_must_fail git stash apply does-not-exist &&
585 test_must_fail git stash apply does-not-exist@{0} &&
586 test_must_fail git stash show does-not-exist &&
587 test_must_fail git stash show does-not-exist@{0} &&
588 test_must_fail git stash branch tmp does-not-exist &&
589 test_must_fail git stash branch tmp does-not-exist@{0} &&
593 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
595 test_must_fail git stash drop stash@{0} &&
600 test_must_fail git stash drop stash@{1} &&
601 test_must_fail git stash pop stash@{1} &&
602 test_must_fail git stash apply stash@{1} &&
603 test_must_fail git stash show stash@{1} &&
604 test_must_fail git stash branch tmp stash@{1} &&
608 test_expect_success
'stash branch should not drop the stash if the branch exists' '
612 git commit -m initial &&
615 test_must_fail git stash branch master stash@{0} &&
616 git rev-parse stash@{0} --
619 test_expect_success
'stash apply shows status same as git status (relative to current directory)' '
621 echo 1 >subdir/subfile1 &&
622 echo 2 >subdir/subfile2 &&
623 git add subdir/subfile1 &&
624 git commit -m subdir &&
629 git status >../expect &&
631 sane_unset GIT_MERGE_VERBOSITY &&
634 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
635 test_i18ncmp expect actual
639 diff --git a/HEAD b/HEAD
641 index 0000000..fe0cbee
648 test_expect_success
'stash where working directory contains "HEAD" file' '
651 echo file-not-a-ref > HEAD &&
655 git diff-files --quiet &&
656 git diff-index --cached --quiet HEAD &&
657 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
658 git diff stash^..stash > output &&
659 test_cmp output expect
662 test_expect_success
'store called with invalid commit' '
663 test_must_fail git stash store foo
666 test_expect_success
'store updates stash ref and reflog' '
671 STASH_ID=$(git stash create) &&
674 git stash store -m quuxery $STASH_ID &&
675 test $(cat .git/refs/stash) = $STASH_ID &&
676 git reflog --format=%H stash| grep $STASH_ID &&
681 test_expect_success
'handle stash specification with spaces' '
685 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
689 git stash apply "stash@{$stamp}" &&
693 test_expect_success
'setup stash with index and worktree changes' '
698 echo working >file &&
702 test_expect_success
'stash list implies --first-parent -m' '
703 cat >expect <<-EOF &&
706 diff --git a/file b/file
707 index 257cc56..d26b33d 100644
714 git stash list --format=%gd -p >actual &&
715 test_cmp expect actual
718 test_expect_success
'stash list --cc shows combined diff' '
719 cat >expect <<-\EOF &&
723 index 257cc56,9015a7a..d26b33d
726 @@@ -1,1 -1,1 +1,1 @@@
731 git stash list --format=%gd -p --cc >actual &&
732 test_cmp expect actual