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' '
120 git stash drop stash@{1} &&
121 test 2 = $(git stash list | wc -l) &&
123 test 9 = $(cat file) &&
124 test 1 = $(git show :file) &&
125 test 1 = $(git show HEAD:file) &&
129 test 3 = $(cat file) &&
130 test 1 = $(git show :file) &&
131 test 1 = $(git show HEAD:file)
134 test_expect_success 'drop middle stash by index' '
141 test 2 = $(git stash list | wc -l) &&
143 test 9 = $(cat file) &&
144 test 1 = $(git show :file) &&
145 test 1 = $(git show HEAD:file) &&
149 test 3 = $(cat file) &&
150 test 1 = $(git show :file) &&
151 test 1 = $(git show HEAD:file)
154 test_expect_success '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)
164 diff --git a/file2 b/file2
166 index 0000000..1fe912c
174 diff --git a/file b/file
175 index 257cc56..5716ca5 100644
184 diff --git a/file b/file
185 index 7601807..5716ca5 100644
191 diff --git a/file2 b/file2
193 index 0000000..1fe912c
200 test_expect_success 'stash branch' '
202 git commit file -m first &&
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 output expect &&
215 test_cmp output expect1 &&
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)
223 test_expect_success 'apply -q is quiet' '
226 git stash apply -q > output.out 2>&1 &&
227 test_must_be_empty output.out
230 test_expect_success 'save -q is quiet' '
231 git stash save --quiet > output.out 2>&1 &&
232 test_must_be_empty output.out
235 test_expect_success 'pop -q is quiet' '
236 git stash pop -q > output.out 2>&1 &&
237 test_must_be_empty output.out
240 test_expect_success 'pop -q --index works and is quiet' '
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
249 test_expect_success 'drop -q is quiet' '
251 git stash drop -q > output.out 2>&1 &&
252 test_must_be_empty output.out
255 test_expect_success 'stash -k' '
260 test bar,bar4 = $(cat file),$(cat file2)
263 test_expect_success 'stash --no-keep-index' '
265 echo bar44 > file2 &&
267 git stash --no-keep-index &&
268 test bar,bar2 = $(cat file),$(cat file2)
271 test_expect_success 'stash --invalid-option' '
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 git stash -- -message-starting-with-dash &&
279 test bar,bar2 = $(cat file),$(cat file2)
282 test_expect_success 'stash an added file' '
286 git stash save "added file" &&
289 test new = "$(cat file3)"
292 test_expect_success 'stash rm then recreate' '
296 git stash save "rm then recreate" &&
297 test bar = "$(cat file)" &&
299 test bar7 = "$(cat file)"
302 test_expect_success 'stash rm and ignore' '
305 echo file >.gitignore &&
306 git stash save "rm and ignore" &&
307 test bar = "$(cat file)" &&
308 test file = "$(cat .gitignore)" &&
311 test file = "$(cat .gitignore)"
314 test_expect_success 'stash rm and ignore (stage .gitignore)' '
317 echo file >.gitignore &&
318 git add .gitignore &&
319 git stash save "rm and ignore (stage .gitignore)" &&
320 test bar = "$(cat file)" &&
321 ! test -r .gitignore &&
324 test file = "$(cat .gitignore)"
327 test_expect_success SYMLINKS 'stash file to symlink' '
331 git stash save "file to symlink" &&
333 test bar = "$(cat file)" &&
335 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
338 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
342 git stash save "file to symlink (stage rm)" &&
344 test bar = "$(cat file)" &&
346 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
349 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
354 git stash save "file to symlink (full stage)" &&
356 test bar = "$(cat file)" &&
358 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
361 # This test creates a commit with a symlink used for the following tests
363 test_expect_success 'stash symlink to file' '
365 test_ln_s_add file filelink &&
366 git commit -m "Add symlink" &&
369 git stash save "symlink to file"
372 test_expect_success SYMLINKS 'this must have re-created the symlink' '
374 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
377 test_expect_success 'unstash must re-create the file' '
379 ! test -h filelink &&
380 test bar = "$(cat file)"
383 test_expect_success 'stash symlink to file (stage rm)' '
387 git stash save "symlink to file (stage rm)"
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_success 'stash symlink to file (full stage)' '
406 git stash save "symlink to file (full stage)"
409 test_expect_success SYMLINKS 'this must have re-created the symlink' '
411 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
414 test_expect_success 'unstash must re-create the file' '
416 ! test -h filelink &&
417 test bar = "$(cat file)"
420 test_expect_failure 'stash directory to file' '
423 echo foo >dir/file &&
425 git commit -m "Add file in dir" &&
428 git stash save "directory to file" &&
430 test foo = "$(cat dir/file)" &&
431 test_must_fail git stash apply &&
432 test bar = "$(cat dir)" &&
433 git reset --soft HEAD^
436 test_expect_failure 'stash file to directory' '
440 echo foo >file/file &&
441 git stash save "file to directory" &&
443 test bar = "$(cat file)" &&
446 test foo = "$(cat file/file)"
449 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
451 test_when_finished "git reset --hard HEAD" &&
454 STASH_ID=$(git stash create) &&
456 git stash branch stash-branch ${STASH_ID} &&
457 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
458 test $(git ls-files --modified | wc -l) -eq 1
461 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
463 test_when_finished "git reset --hard HEAD" &&
467 test_when_finished "git stash drop" &&
469 STASH_ID=$(git stash create) &&
471 git stash branch stash-branch ${STASH_ID} &&
472 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
473 test $(git ls-files --modified | wc -l) -eq 1
476 test_expect_success 'stash show format defaults to --stat' '
478 test_when_finished "git reset --hard HEAD" &&
482 test_when_finished "git stash drop" &&
484 STASH_ID=$(git stash create) &&
486 cat >expected <<-EOF &&
488 1 file changed, 1 insertion(+)
490 git stash show ${STASH_ID} >actual &&
491 test_i18ncmp expected actual
494 test_expect_success 'stash show - stashes on stack, stash-like argument' '
496 test_when_finished "git reset --hard HEAD" &&
500 test_when_finished "git stash drop" &&
502 STASH_ID=$(git stash create) &&
504 echo "1 0 file" >expected &&
505 git stash show --numstat ${STASH_ID} >actual &&
506 test_cmp expected actual
509 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
511 test_when_finished "git reset --hard HEAD" &&
515 test_when_finished "git stash drop" &&
517 STASH_ID=$(git stash create) &&
519 cat >expected <<-EOF &&
520 diff --git a/file b/file
521 index 7601807..935fbd3 100644
528 git stash show -p ${STASH_ID} >actual &&
529 test_cmp expected actual
532 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
534 test_when_finished "git reset --hard HEAD" &&
537 STASH_ID=$(git stash create) &&
539 echo "1 0 file" >expected &&
540 git stash show --numstat ${STASH_ID} >actual &&
541 test_cmp expected actual
544 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
546 test_when_finished "git reset --hard HEAD" &&
549 STASH_ID=$(git stash create) &&
551 cat >expected <<-EOF &&
552 diff --git a/file b/file
553 index 7601807..71b52c4 100644
560 git stash show -p ${STASH_ID} >actual &&
561 test_cmp expected actual
564 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
566 test_when_finished "git reset --hard HEAD && git stash clear" &&
572 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
574 test bar = "$(cat file)" &&
575 git reset --hard HEAD
578 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
580 test_when_finished "git reset --hard HEAD && git stash clear" &&
586 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
588 test bar = "$(cat file)" &&
589 git reset --hard HEAD
592 test_expect_success 'ref with non-existent reflog' '
598 test_must_fail git rev-parse --quiet --verify does-not-exist &&
599 test_must_fail git stash drop does-not-exist &&
600 test_must_fail git stash drop does-not-exist@{0} &&
601 test_must_fail git stash pop does-not-exist &&
602 test_must_fail git stash pop does-not-exist@{0} &&
603 test_must_fail git stash apply does-not-exist &&
604 test_must_fail git stash apply does-not-exist@{0} &&
605 test_must_fail git stash show does-not-exist &&
606 test_must_fail git stash show does-not-exist@{0} &&
607 test_must_fail git stash branch tmp does-not-exist &&
608 test_must_fail git stash branch tmp does-not-exist@{0} &&
612 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
614 test_must_fail git stash drop stash@{0} &&
619 test_must_fail git stash drop stash@{1} &&
620 test_must_fail git stash pop stash@{1} &&
621 test_must_fail git stash apply stash@{1} &&
622 test_must_fail git stash show stash@{1} &&
623 test_must_fail git stash branch tmp stash@{1} &&
627 test_expect_success 'invalid ref of the form "n", n >= N' '
629 test_must_fail git stash drop 0 &&
634 test_must_fail git stash drop 1 &&
635 test_must_fail git stash pop 1 &&
636 test_must_fail git stash apply 1 &&
637 test_must_fail git stash show 1 &&
638 test_must_fail git stash branch tmp 1 &&
642 test_expect_success 'stash branch should not drop the stash if the branch exists' '
646 git commit -m initial &&
649 test_must_fail git stash branch master stash@{0} &&
650 git rev-parse stash@{0} --
653 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
655 echo 1 >subdir/subfile1 &&
656 echo 2 >subdir/subfile2 &&
657 git add subdir/subfile1 &&
658 git commit -m subdir &&
663 git status >../expect &&
665 sane_unset GIT_MERGE_VERBOSITY &&
668 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
669 test_i18ncmp expect actual
673 diff --git a/HEAD b/HEAD
675 index 0000000..fe0cbee
682 test_expect_success 'stash where working directory contains "HEAD" file' '
685 echo file-not-a-ref > HEAD &&
689 git diff-files --quiet &&
690 git diff-index --cached --quiet HEAD &&
691 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
692 git diff stash^..stash > output &&
693 test_cmp output expect
696 test_expect_success 'store called with invalid commit' '
697 test_must_fail git stash store foo
700 test_expect_success 'store updates stash ref and reflog' '
705 STASH_ID=$(git stash create) &&
708 git stash store -m quuxery $STASH_ID &&
709 test $(cat .git/refs/stash) = $STASH_ID &&
710 git reflog --format=%H stash| grep $STASH_ID &&
715 test_expect_success 'handle stash specification with spaces' '
719 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
723 git stash apply "stash@{$stamp}" &&
727 test_expect_success 'setup stash with index and worktree changes' '
732 echo working >file &&
736 test_expect_success 'stash list implies --first-parent -m' '
737 cat >expect <<-EOF &&
740 diff --git a/file b/file
741 index 257cc56..d26b33d 100644
748 git stash list --format=%gd -p >actual &&
749 test_cmp expect actual
752 test_expect_success 'stash list --cc shows combined diff' '
753 cat >expect <<-\EOF &&
757 index 257cc56,9015a7a..d26b33d
760 @@@ -1,1 -1,1 +1,1 @@@
765 git stash list --format=%gd -p --cc >actual &&
766 test_cmp expect actual