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)
280 test_expect_success 'stash an added file' '
284 git stash save "added file" &&
287 test new = "$(cat file3)"
290 test_expect_success 'stash rm then recreate' '
294 git stash save "rm then recreate" &&
295 test bar = "$(cat file)" &&
297 test bar7 = "$(cat file)"
300 test_expect_success 'stash rm and ignore' '
303 echo file >.gitignore &&
304 git stash save "rm and ignore" &&
305 test bar = "$(cat file)" &&
306 test file = "$(cat .gitignore)" &&
309 test file = "$(cat .gitignore)"
312 test_expect_success 'stash rm and ignore (stage .gitignore)' '
315 echo file >.gitignore &&
316 git add .gitignore &&
317 git stash save "rm and ignore (stage .gitignore)" &&
318 test bar = "$(cat file)" &&
319 ! test -r .gitignore &&
322 test file = "$(cat .gitignore)"
325 test_expect_success SYMLINKS 'stash file to symlink' '
329 git stash save "file to symlink" &&
331 test bar = "$(cat file)" &&
333 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
336 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
340 git stash save "file to symlink (stage rm)" &&
342 test bar = "$(cat file)" &&
344 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
347 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
352 git stash save "file to symlink (full stage)" &&
354 test bar = "$(cat file)" &&
356 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
359 # This test creates a commit with a symlink used for the following tests
361 test_expect_success 'stash symlink to file' '
363 test_ln_s_add file filelink &&
364 git commit -m "Add symlink" &&
367 git stash save "symlink to file"
370 test_expect_success SYMLINKS 'this must have re-created the symlink' '
372 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
375 test_expect_success 'unstash must re-create the file' '
377 ! test -h filelink &&
378 test bar = "$(cat file)"
381 test_expect_success 'stash symlink to file (stage rm)' '
385 git stash save "symlink to file (stage rm)"
388 test_expect_success SYMLINKS 'this must have re-created the symlink' '
390 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
393 test_expect_success 'unstash must re-create the file' '
395 ! test -h filelink &&
396 test bar = "$(cat file)"
399 test_expect_success 'stash symlink to file (full stage)' '
404 git stash save "symlink to file (full stage)"
407 test_expect_success SYMLINKS 'this must have re-created the symlink' '
409 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
412 test_expect_success 'unstash must re-create the file' '
414 ! test -h filelink &&
415 test bar = "$(cat file)"
418 test_expect_failure 'stash directory to file' '
421 echo foo >dir/file &&
423 git commit -m "Add file in dir" &&
426 git stash save "directory to file" &&
428 test foo = "$(cat dir/file)" &&
429 test_must_fail git stash apply &&
430 test bar = "$(cat dir)" &&
431 git reset --soft HEAD^
434 test_expect_failure 'stash file to directory' '
438 echo foo >file/file &&
439 git stash save "file to directory" &&
441 test bar = "$(cat file)" &&
444 test foo = "$(cat file/file)"
447 test_expect_success 'stash create - no changes' '
449 test_when_finished "git reset --hard HEAD" &&
451 git stash create >actual &&
452 test_must_be_empty actual
455 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
457 test_when_finished "git reset --hard HEAD" &&
460 STASH_ID=$(git stash create) &&
462 git stash branch stash-branch ${STASH_ID} &&
463 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
464 test $(git ls-files --modified | wc -l) -eq 1
467 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
469 test_when_finished "git reset --hard HEAD" &&
473 test_when_finished "git stash drop" &&
475 STASH_ID=$(git stash create) &&
477 git stash branch stash-branch ${STASH_ID} &&
478 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
479 test $(git ls-files --modified | wc -l) -eq 1
482 test_expect_success 'stash show format defaults to --stat' '
484 test_when_finished "git reset --hard HEAD" &&
488 test_when_finished "git stash drop" &&
490 STASH_ID=$(git stash create) &&
492 cat >expected <<-EOF &&
494 1 file changed, 1 insertion(+)
496 git stash show ${STASH_ID} >actual &&
497 test_i18ncmp expected actual
500 test_expect_success 'stash show - stashes on stack, stash-like argument' '
502 test_when_finished "git reset --hard HEAD" &&
506 test_when_finished "git stash drop" &&
508 STASH_ID=$(git stash create) &&
510 echo "1 0 file" >expected &&
511 git stash show --numstat ${STASH_ID} >actual &&
512 test_cmp expected actual
515 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
517 test_when_finished "git reset --hard HEAD" &&
521 test_when_finished "git stash drop" &&
523 STASH_ID=$(git stash create) &&
525 cat >expected <<-EOF &&
526 diff --git a/file b/file
527 index 7601807..935fbd3 100644
534 git stash show -p ${STASH_ID} >actual &&
535 test_cmp expected actual
538 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
540 test_when_finished "git reset --hard HEAD" &&
543 STASH_ID=$(git stash create) &&
545 echo "1 0 file" >expected &&
546 git stash show --numstat ${STASH_ID} >actual &&
547 test_cmp expected actual
550 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
552 test_when_finished "git reset --hard HEAD" &&
555 STASH_ID=$(git stash create) &&
557 cat >expected <<-EOF &&
558 diff --git a/file b/file
559 index 7601807..71b52c4 100644
566 git stash show -p ${STASH_ID} >actual &&
567 test_cmp expected actual
570 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
572 test_when_finished "git reset --hard HEAD && git stash clear" &&
578 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
580 test bar = "$(cat file)" &&
581 git reset --hard HEAD
584 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
586 test_when_finished "git reset --hard HEAD && git stash clear" &&
592 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
594 test bar = "$(cat file)" &&
595 git reset --hard HEAD
598 test_expect_success 'ref with non-existent reflog' '
604 test_must_fail git rev-parse --quiet --verify does-not-exist &&
605 test_must_fail git stash drop does-not-exist &&
606 test_must_fail git stash drop does-not-exist@{0} &&
607 test_must_fail git stash pop does-not-exist &&
608 test_must_fail git stash pop does-not-exist@{0} &&
609 test_must_fail git stash apply does-not-exist &&
610 test_must_fail git stash apply does-not-exist@{0} &&
611 test_must_fail git stash show does-not-exist &&
612 test_must_fail git stash show does-not-exist@{0} &&
613 test_must_fail git stash branch tmp does-not-exist &&
614 test_must_fail git stash branch tmp does-not-exist@{0} &&
618 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
620 test_must_fail git stash drop stash@{0} &&
625 test_must_fail git stash drop stash@{1} &&
626 test_must_fail git stash pop stash@{1} &&
627 test_must_fail git stash apply stash@{1} &&
628 test_must_fail git stash show stash@{1} &&
629 test_must_fail git stash branch tmp stash@{1} &&
633 test_expect_success 'invalid ref of the form "n", n >= N' '
635 test_must_fail git stash drop 0 &&
640 test_must_fail git stash drop 1 &&
641 test_must_fail git stash pop 1 &&
642 test_must_fail git stash apply 1 &&
643 test_must_fail git stash show 1 &&
644 test_must_fail git stash branch tmp 1 &&
648 test_expect_success 'stash branch should not drop the stash if the branch exists' '
652 git commit -m initial &&
655 test_must_fail git stash branch master stash@{0} &&
656 git rev-parse stash@{0} --
659 test_expect_success 'stash branch should not drop the stash if the apply fails' '
661 git reset HEAD~1 --hard &&
664 git commit -m initial &&
668 test_when_finished "git checkout master" &&
669 test_must_fail git stash branch new_branch stash@{0} &&
670 git rev-parse stash@{0} --
673 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
675 echo 1 >subdir/subfile1 &&
676 echo 2 >subdir/subfile2 &&
677 git add subdir/subfile1 &&
678 git commit -m subdir &&
683 git status >../expect &&
685 sane_unset GIT_MERGE_VERBOSITY &&
688 sed -e 1d >actual && # drop "Saved..."
689 test_i18ncmp expect actual
693 diff --git a/HEAD b/HEAD
695 index 0000000..fe0cbee
702 test_expect_success 'stash where working directory contains "HEAD" file' '
705 echo file-not-a-ref > HEAD &&
709 git diff-files --quiet &&
710 git diff-index --cached --quiet HEAD &&
711 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
712 git diff stash^..stash > output &&
713 test_cmp output expect
716 test_expect_success 'store called with invalid commit' '
717 test_must_fail git stash store foo
720 test_expect_success 'store updates stash ref and reflog' '
725 STASH_ID=$(git stash create) &&
728 git stash store -m quuxery $STASH_ID &&
729 test $(git rev-parse stash) = $STASH_ID &&
730 git reflog --format=%H stash| grep $STASH_ID &&
735 test_expect_success 'handle stash specification with spaces' '
739 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
743 git stash apply "stash@{$stamp}" &&
747 test_expect_success 'setup stash with index and worktree changes' '
752 echo working >file &&
756 test_expect_success 'stash list implies --first-parent -m' '
757 cat >expect <<-EOF &&
760 diff --git a/file b/file
761 index 257cc56..d26b33d 100644
768 git stash list --format=%gd -p >actual &&
769 test_cmp expect actual
772 test_expect_success 'stash list --cc shows combined diff' '
773 cat >expect <<-\EOF &&
777 index 257cc56,9015a7a..d26b33d
780 @@@ -1,1 -1,1 +1,1 @@@
785 git stash list --format=%gd -p --cc >actual &&
786 test_cmp expect actual
789 test_expect_success 'stash is not confused by partial renames' '
794 test_path_is_file renamed &&
795 test_path_is_missing file
798 test_expect_success 'push -m shows right message' '
801 git stash push -m "test message" &&
802 echo "stash@{0}: On master: test message" >expect &&
803 git stash list -1 >actual &&
804 test_cmp expect actual
807 test_expect_success 'push -m also works without space' '
810 git stash push -m"unspaced test message" &&
811 echo "stash@{0}: On master: unspaced test message" >expect &&
812 git stash list -1 >actual &&
813 test_cmp expect actual
816 test_expect_success 'store -m foo shows right message' '
821 STASH_ID=$(git stash create) &&
822 git stash store -m "store m" $STASH_ID &&
823 echo "stash@{0}: store m" >expect &&
824 git stash list -1 >actual &&
825 test_cmp expect actual
828 test_expect_success 'store -mfoo shows right message' '
833 STASH_ID=$(git stash create) &&
834 git stash store -m"store mfoo" $STASH_ID &&
835 echo "stash@{0}: store mfoo" >expect &&
836 git stash list -1 >actual &&
837 test_cmp expect actual
840 test_expect_success 'store --message=foo shows right message' '
845 STASH_ID=$(git stash create) &&
846 git stash store --message="store message=foo" $STASH_ID &&
847 echo "stash@{0}: store message=foo" >expect &&
848 git stash list -1 >actual &&
849 test_cmp expect actual
852 test_expect_success 'store --message foo shows right message' '
857 STASH_ID=$(git stash create) &&
858 git stash store --message "store message foo" $STASH_ID &&
859 echo "stash@{0}: store message foo" >expect &&
860 git stash list -1 >actual &&
861 test_cmp expect actual
864 test_expect_success 'push -mfoo uses right message' '
867 git stash push -m"test mfoo" &&
868 echo "stash@{0}: On master: test mfoo" >expect &&
869 git stash list -1 >actual &&
870 test_cmp expect actual
873 test_expect_success 'push --message foo is synonym for -mfoo' '
876 git stash push --message "test message foo" &&
877 echo "stash@{0}: On master: test message foo" >expect &&
878 git stash list -1 >actual &&
879 test_cmp expect actual
882 test_expect_success 'push --message=foo is synonym for -mfoo' '
885 git stash push --message="test message=foo" &&
886 echo "stash@{0}: On master: test message=foo" >expect &&
887 git stash list -1 >actual &&
888 test_cmp expect actual
891 test_expect_success 'push -m shows right message' '
894 git stash push -m "test m foo" &&
895 echo "stash@{0}: On master: test m foo" >expect &&
896 git stash list -1 >actual &&
897 test_cmp expect actual
900 test_expect_success 'create stores correct message' '
903 STASH_ID=$(git stash create "create test message") &&
904 echo "On master: create test message" >expect &&
905 git show --pretty=%s -s ${STASH_ID} >actual &&
906 test_cmp expect actual
909 test_expect_success 'create with multiple arguments for the message' '
912 STASH_ID=$(git stash create test untracked) &&
913 echo "On master: test untracked" >expect &&
914 git show --pretty=%s -s ${STASH_ID} >actual &&
915 test_cmp expect actual
918 test_expect_success 'create in a detached state' '
919 test_when_finished "git checkout master" &&
920 git checkout HEAD~1 &&
923 STASH_ID=$(git stash create) &&
924 HEAD_ID=$(git rev-parse --short HEAD) &&
925 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
926 git show --pretty=%s -s ${STASH_ID} >actual &&
927 test_cmp expect actual
930 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
934 git stash push -- foo &&
935 test_path_is_file bar &&
936 test_path_is_missing foo &&
938 test_path_is_file foo &&
939 test_path_is_file bar
942 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
949 git stash push -- ../foo
951 test_path_is_file bar &&
952 test_path_is_missing foo &&
954 test_path_is_file foo &&
955 test_path_is_file bar
958 test_expect_success 'stash with multiple pathspec arguments' '
962 git add foo bar extra &&
963 git stash push -- foo bar &&
964 test_path_is_missing bar &&
965 test_path_is_missing foo &&
966 test_path_is_file extra &&
968 test_path_is_file foo &&
969 test_path_is_file bar &&
970 test_path_is_file extra
973 test_expect_success 'stash with file including $IFS character' '
978 git stash push -- "foo b*" &&
979 test_path_is_missing "foo bar" &&
980 test_path_is_file foo &&
981 test_path_is_file bar &&
983 test_path_is_file "foo bar" &&
984 test_path_is_file foo &&
985 test_path_is_file bar
988 test_expect_success 'stash with pathspec matching multiple paths' '
989 echo original >file &&
990 echo original >other-file &&
991 git commit -m "two" file other-file &&
992 echo modified >file &&
993 echo modified >other-file &&
994 git stash push -- "*file" &&
995 echo original >expect &&
996 test_cmp expect file &&
997 test_cmp expect other-file &&
999 echo modified >expect &&
1000 test_cmp expect file &&
1001 test_cmp expect other-file
1004 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1007 git commit -m "tmp" &&
1008 git stash push -p foo >actual &&
1009 echo "No local changes to save" >expect &&
1010 git reset --hard HEAD~ &&
1011 test_i18ncmp expect actual
1014 test_expect_success 'stash push with pathspec shows no changes when there are none' '
1017 git commit -m "tmp" &&
1018 git stash push foo >actual &&
1019 echo "No local changes to save" >expect &&
1020 git reset --hard HEAD~ &&
1021 test_i18ncmp expect actual
1024 test_expect_success 'stash push with pathspec not in the repository errors out' '
1026 test_must_fail git stash push untracked &&
1027 test_path_is_file untracked
1030 test_expect_success 'untracked files are left in place when -u is not given' '
1034 git stash push file &&
1035 test_path_is_file untracked
1038 test_expect_success 'stash without verb with pathspec' '
1043 git stash -- "foo b*" &&
1044 test_path_is_missing "foo bar" &&
1045 test_path_is_file foo &&
1046 test_path_is_file bar &&
1048 test_path_is_file "foo bar" &&
1049 test_path_is_file foo &&
1050 test_path_is_file bar
1053 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1058 git commit -m "test" &&
1061 git stash -k -- foo &&
1062 test "",bar = $(cat foo),$(cat bar) &&
1064 test foo,bar = $(cat foo),$(cat bar)
1067 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1069 >subdir/untracked &&
1072 git add subdir/tracked* &&
1073 git stash -- subdir/ &&
1074 test_path_is_missing subdir/tracked1 &&
1075 test_path_is_missing subdir/tracked2 &&
1076 test_path_is_file subdir/untracked &&
1078 test_path_is_file subdir/tracked1 &&
1079 test_path_is_file subdir/tracked2 &&
1080 test_path_is_file subdir/untracked
1083 test_expect_success 'stash -- <subdir> works with binary files' '
1085 >subdir/untracked &&
1087 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1088 git add subdir/tracked* &&
1089 git stash -- subdir/ &&
1090 test_path_is_missing subdir/tracked &&
1091 test_path_is_missing subdir/tracked-binary &&
1092 test_path_is_file subdir/untracked &&
1094 test_path_is_file subdir/tracked &&
1095 test_path_is_file subdir/tracked-binary &&
1096 test_path_is_file subdir/untracked