3 # Copyright (c) 2007 Johannes E Schindelin
6 test_description
='Test git stash'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13 test_expect_success
'usage on cmd and subcommand invalid option' '
14 test_expect_code 129 git stash --invalid-option 2>usage &&
15 grep "or: git stash" usage &&
17 test_expect_code 129 git stash push --invalid-option 2>usage &&
18 ! grep "or: git stash" usage
21 test_expect_success
'usage on main command -h emits a summary of subcommands' '
22 test_expect_code 129 git stash -h >usage &&
23 grep -F "usage: git stash list" usage &&
24 grep -F "or: git stash show" usage
27 test_expect_failure
'usage for subcommands should emit subcommand usage' '
28 test_expect_code 129 git stash push -h >usage &&
29 grep -F "usage: git stash [push" usage
35 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
36 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
37 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
38 "$i" >"$i.compare" ||
return 1
40 test_cmp
"$1.compare" "$2.compare" &&
41 rm -f "$1.compare" "$2.compare"
47 echo unrelated
>other-file
&&
50 git commit
-m initial
&&
56 git diff-files
--quiet &&
57 git diff-index
--cached --quiet HEAD
60 test_expect_success
'stash some dirty working directory' '
65 diff --git a/file b/file
66 index 0cfbf08..00750ed 100644
74 test_expect_success
'parents of stash' '
75 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
76 git diff stash^2..stash >output &&
77 diff_cmp expect output
80 test_expect_success
'applying bogus stash does nothing' '
81 test_must_fail git stash apply stash@{1} &&
86 test_expect_success
'apply does not need clean working directory' '
93 test_expect_success
'apply does not clobber working directory changes' '
96 test_must_fail git stash apply &&
101 test_expect_success
'apply stashed changes' '
103 echo 5 >other-file &&
104 git add other-file &&
106 git commit -m other-file &&
108 test 3 = $(cat file) &&
109 test 1 = $(git show :file) &&
110 test 1 = $(git show HEAD:file)
113 test_expect_success
'apply stashed changes (including index)' '
114 git reset --hard HEAD^ &&
115 echo 6 >other-file &&
116 git add other-file &&
118 git commit -m other-file &&
119 git stash apply --index &&
120 test 3 = $(cat file) &&
121 test 2 = $(git show :file) &&
122 test 1 = $(git show HEAD:file)
125 test_expect_success
'unstashing in a subdirectory' '
126 git reset --hard HEAD &&
134 test_expect_success
'stash drop complains of extra options' '
135 test_must_fail git stash drop --foo
138 test_expect_success
'drop top stash' '
140 git stash list >expected &&
144 git stash list >actual &&
145 test_cmp expected actual &&
147 test 3 = $(cat file) &&
148 test 1 = $(git show :file) &&
149 test 1 = $(git show HEAD:file)
152 test_expect_success
'drop middle stash' '
158 git stash drop stash@{1} &&
159 test 2 = $(git stash list | wc -l) &&
161 test 9 = $(cat file) &&
162 test 1 = $(git show :file) &&
163 test 1 = $(git show HEAD:file) &&
167 test 3 = $(cat file) &&
168 test 1 = $(git show :file) &&
169 test 1 = $(git show HEAD:file)
172 test_expect_success
'drop middle stash by index' '
179 test 2 = $(git stash list | wc -l) &&
181 test 9 = $(cat file) &&
182 test 1 = $(git show :file) &&
183 test 1 = $(git show HEAD:file) &&
187 test 3 = $(cat file) &&
188 test 1 = $(git show :file) &&
189 test 1 = $(git show HEAD:file)
192 test_expect_success
'drop stash reflog updates refs/stash' '
194 git rev-parse refs/stash >expect &&
197 git stash drop stash@{0} &&
198 git rev-parse refs/stash >actual &&
199 test_cmp expect actual
202 test_expect_success REFFILES
'drop stash reflog updates refs/stash with rewrite' '
210 old_oid="$(git -C repo rev-parse stash@{0})" &&
212 new_oid="$(git -C repo rev-parse stash@{0})" &&
214 cat >expect <<-EOF &&
215 $(test_oid zero) $old_oid
218 cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
219 test_cmp expect actual &&
221 git -C repo stash drop stash@{1} &&
222 cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
223 cat >expect <<-EOF &&
224 $(test_oid zero) $new_oid
226 test_cmp expect actual
229 test_expect_success
'stash pop' '
232 test 3 = $(cat file) &&
233 test 1 = $(git show :file) &&
234 test 1 = $(git show HEAD:file) &&
235 test 0 = $(git stash list | wc -l)
239 diff --git a/file2 b/file2
241 index 0000000..1fe912c
249 diff --git a/file b/file
250 index 257cc56..5716ca5 100644
259 diff --git a/file b/file
260 index 7601807..5716ca5 100644
266 diff --git a/file2 b/file2
268 index 0000000..1fe912c
275 test_expect_success
'stash branch' '
277 git commit file -m first &&
283 git commit file -m second &&
284 git stash branch stashbranch &&
285 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
286 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
287 git diff --cached >output &&
288 diff_cmp expect output &&
290 diff_cmp expect1 output &&
292 git commit -m alternate\ second &&
293 git diff main..stashbranch >output &&
294 diff_cmp output expect2 &&
295 test 0 = $(git stash list | wc -l)
298 test_expect_success
'apply -q is quiet' '
301 git stash apply -q >output.out 2>&1 &&
302 test_must_be_empty output.out
305 test_expect_success
'apply --index -q is quiet' '
306 # Added file, deleted file, modified file all staged for commit
307 echo foo >new-file &&
309 git add new-file file &&
313 git stash apply --index -q >output.out 2>&1 &&
314 test_must_be_empty output.out
317 test_expect_success
'save -q is quiet' '
318 git stash save --quiet >output.out 2>&1 &&
319 test_must_be_empty output.out
322 test_expect_success
'pop -q works and is quiet' '
323 git stash pop -q >output.out 2>&1 &&
325 git show :file >actual &&
326 test_cmp expect actual &&
327 test_must_be_empty output.out
330 test_expect_success
'pop -q --index works and is quiet' '
333 git stash save --quiet &&
334 git stash pop -q --index >output.out 2>&1 &&
335 git diff-files file2 >file2.diff &&
336 test_must_be_empty file2.diff &&
337 test foo = "$(git show :file)" &&
338 test_must_be_empty output.out
341 test_expect_success
'drop -q is quiet' '
343 git stash drop -q >output.out 2>&1 &&
344 test_must_be_empty output.out
347 test_expect_success
'stash push -q --staged refreshes the index' '
351 git stash push -q --staged &&
352 git diff-files >output.out &&
353 test_must_be_empty output.out
356 test_expect_success
'stash apply -q --index refreshes the index' '
357 echo test >other-file &&
358 git add other-file &&
359 echo another-change >other-file &&
360 git diff-files >expect &&
363 git stash apply -q --index &&
364 git diff-files >actual &&
365 test_cmp expect actual
368 test_expect_success
'stash -k' '
373 test bar,bar4 = $(cat file),$(cat file2)
376 test_expect_success
'stash --no-keep-index' '
380 git stash --no-keep-index &&
381 test bar,bar2 = $(cat file),$(cat file2)
384 test_expect_success
'stash --staged' '
388 git stash --staged &&
389 test bar3,bar2 = $(cat file),$(cat file2) &&
392 test bar,bar4 = $(cat file),$(cat file2)
395 test_expect_success
'dont assume push with non-option args' '
396 test_must_fail git stash -q drop 2>err &&
397 test_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
400 test_expect_success
'stash --invalid-option' '
404 test_must_fail git stash --invalid-option &&
405 test_must_fail git stash save --invalid-option &&
406 test bar5,bar6 = $(cat file),$(cat file2)
409 test_expect_success
'stash an added file' '
413 git stash save "added file" &&
416 test new = "$(cat file3)"
419 test_expect_success
'stash --intent-to-add file' '
422 git add --intent-to-add file4 &&
423 test_when_finished "git rm -f file4" &&
424 test_must_fail git stash
427 test_expect_success
'stash rm then recreate' '
431 git stash save "rm then recreate" &&
432 test bar = "$(cat file)" &&
434 test bar7 = "$(cat file)"
437 test_expect_success
'stash rm and ignore' '
440 echo file >.gitignore &&
441 git stash save "rm and ignore" &&
442 test bar = "$(cat file)" &&
443 test file = "$(cat .gitignore)" &&
446 test file = "$(cat .gitignore)"
449 test_expect_success
'stash rm and ignore (stage .gitignore)' '
452 echo file >.gitignore &&
453 git add .gitignore &&
454 git stash save "rm and ignore (stage .gitignore)" &&
455 test bar = "$(cat file)" &&
456 ! test -r .gitignore &&
459 test file = "$(cat .gitignore)"
462 test_expect_success SYMLINKS
'stash file to symlink' '
466 git stash save "file to symlink" &&
467 test_path_is_file_not_symlink file &&
468 test bar = "$(cat file)" &&
470 test_path_is_symlink file &&
471 test "$(test_readlink file)" = file2
474 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
478 git stash save "file to symlink (stage rm)" &&
479 test_path_is_file_not_symlink file &&
480 test bar = "$(cat file)" &&
482 test_path_is_symlink file &&
483 test "$(test_readlink file)" = file2
486 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
491 git stash save "file to symlink (full stage)" &&
492 test_path_is_file_not_symlink file &&
493 test bar = "$(cat file)" &&
495 test_path_is_symlink file &&
496 test "$(test_readlink file)" = file2
499 # This test creates a commit with a symlink used for the following tests
501 test_expect_success
'stash symlink to file' '
503 test_ln_s_add file filelink &&
504 git commit -m "Add symlink" &&
507 git stash save "symlink to file"
510 test_expect_success SYMLINKS
'this must have re-created the symlink' '
512 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
515 test_expect_success
'unstash must re-create the file' '
517 ! test -h filelink &&
518 test bar = "$(cat file)"
521 test_expect_success
'stash symlink to file (stage rm)' '
525 git stash save "symlink to file (stage rm)"
528 test_expect_success SYMLINKS
'this must have re-created the symlink' '
530 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
533 test_expect_success
'unstash must re-create the file' '
535 ! test -h filelink &&
536 test bar = "$(cat file)"
539 test_expect_success
'stash symlink to file (full stage)' '
544 git stash save "symlink to file (full stage)"
547 test_expect_success SYMLINKS
'this must have re-created the symlink' '
549 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
552 test_expect_success
'unstash must re-create the file' '
554 ! test -h filelink &&
555 test bar = "$(cat file)"
558 test_expect_failure
'stash directory to file' '
561 echo foo >dir/file &&
563 git commit -m "Add file in dir" &&
566 git stash save "directory to file" &&
567 test_path_is_dir dir &&
568 test foo = "$(cat dir/file)" &&
569 test_must_fail git stash apply &&
570 test bar = "$(cat dir)" &&
571 git reset --soft HEAD^
574 test_expect_failure
'stash file to directory' '
578 echo foo >file/file &&
579 git stash save "file to directory" &&
580 test_path_is_file file &&
581 test bar = "$(cat file)" &&
583 test_path_is_file file/file &&
584 test foo = "$(cat file/file)"
587 test_expect_success
'giving too many ref arguments does not modify files' '
589 test_when_finished "git reset --hard HEAD" &&
594 test-tool chmtime =123456789 file2 &&
595 for type in apply pop "branch stash-branch"
597 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
598 test_i18ngrep "Too many revisions" err &&
599 test 123456789 = $(test-tool chmtime -g file2) || return 1
603 test_expect_success
'drop: too many arguments errors out (does nothing)' '
604 git stash list >expect &&
605 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
606 test_i18ngrep "Too many revisions" err &&
607 git stash list >actual &&
608 test_cmp expect actual
611 test_expect_success
'show: too many arguments errors out (does nothing)' '
612 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
613 test_i18ngrep "Too many revisions" err &&
614 test_must_be_empty out
617 test_expect_success
'stash create - no changes' '
619 test_when_finished "git reset --hard HEAD" &&
621 git stash create >actual &&
622 test_must_be_empty actual
625 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
627 test_when_finished "git reset --hard HEAD" &&
630 STASH_ID=$(git stash create) &&
632 git stash branch stash-branch ${STASH_ID} &&
633 test_when_finished "git reset --hard HEAD && git checkout main &&
634 git branch -D stash-branch" &&
635 test $(git ls-files --modified | wc -l) -eq 1
638 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
640 test_when_finished "git reset --hard HEAD" &&
644 test_when_finished "git stash drop" &&
646 STASH_ID=$(git stash create) &&
648 git stash branch stash-branch ${STASH_ID} &&
649 test_when_finished "git reset --hard HEAD && git checkout main &&
650 git branch -D stash-branch" &&
651 test $(git ls-files --modified | wc -l) -eq 1
654 test_expect_success
'stash branch complains with no arguments' '
655 test_must_fail git stash branch 2>err &&
656 test_i18ngrep "No branch name specified" err
659 test_expect_success
'stash show format defaults to --stat' '
661 test_when_finished "git reset --hard HEAD" &&
665 test_when_finished "git stash drop" &&
667 STASH_ID=$(git stash create) &&
669 cat >expected <<-EOF &&
671 1 file changed, 1 insertion(+)
673 git stash show ${STASH_ID} >actual &&
674 test_cmp expected actual
677 test_expect_success
'stash show - stashes on stack, stash-like argument' '
679 test_when_finished "git reset --hard HEAD" &&
683 test_when_finished "git stash drop" &&
685 STASH_ID=$(git stash create) &&
687 echo "1 0 file" >expected &&
688 git stash show --numstat ${STASH_ID} >actual &&
689 test_cmp expected actual
692 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
694 test_when_finished "git reset --hard HEAD" &&
698 test_when_finished "git stash drop" &&
700 STASH_ID=$(git stash create) &&
702 cat >expected <<-EOF &&
703 diff --git a/file b/file
704 index 7601807..935fbd3 100644
711 git stash show -p ${STASH_ID} >actual &&
712 diff_cmp expected actual
715 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
717 test_when_finished "git reset --hard HEAD" &&
720 STASH_ID=$(git stash create) &&
722 echo "1 0 file" >expected &&
723 git stash show --numstat ${STASH_ID} >actual &&
724 test_cmp expected actual
727 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
729 test_when_finished "git reset --hard HEAD" &&
732 STASH_ID=$(git stash create) &&
734 cat >expected <<-EOF &&
735 diff --git a/file b/file
736 index 7601807..71b52c4 100644
743 git stash show -p ${STASH_ID} >actual &&
744 diff_cmp expected actual
747 test_expect_success
'stash show --patience shows diff' '
750 STASH_ID=$(git stash create) &&
752 cat >expected <<-EOF &&
753 diff --git a/file b/file
754 index 7601807..71b52c4 100644
761 git stash show --patience ${STASH_ID} >actual &&
762 diff_cmp expected actual
765 test_expect_success
'drop: fail early if specified stash is not a stash ref' '
767 test_when_finished "git reset --hard HEAD && git stash clear" &&
773 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
775 test bar = "$(cat file)" &&
776 git reset --hard HEAD
779 test_expect_success
'pop: fail early if specified stash is not a stash ref' '
781 test_when_finished "git reset --hard HEAD && git stash clear" &&
787 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
789 test bar = "$(cat file)" &&
790 git reset --hard HEAD
793 test_expect_success
'ref with non-existent reflog' '
799 test_must_fail git rev-parse --quiet --verify does-not-exist &&
800 test_must_fail git stash drop does-not-exist &&
801 test_must_fail git stash drop does-not-exist@{0} &&
802 test_must_fail git stash pop does-not-exist &&
803 test_must_fail git stash pop does-not-exist@{0} &&
804 test_must_fail git stash apply does-not-exist &&
805 test_must_fail git stash apply does-not-exist@{0} &&
806 test_must_fail git stash show does-not-exist &&
807 test_must_fail git stash show does-not-exist@{0} &&
808 test_must_fail git stash branch tmp does-not-exist &&
809 test_must_fail git stash branch tmp does-not-exist@{0} &&
813 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
815 test_must_fail git stash drop stash@{0} &&
820 test_must_fail git stash drop stash@{1} &&
821 test_must_fail git stash pop stash@{1} &&
822 test_must_fail git stash apply stash@{1} &&
823 test_must_fail git stash show stash@{1} &&
824 test_must_fail git stash branch tmp stash@{1} &&
828 test_expect_success
'invalid ref of the form "n", n >= N' '
830 test_must_fail git stash drop 0 &&
835 test_must_fail git stash drop 1 &&
836 test_must_fail git stash pop 1 &&
837 test_must_fail git stash apply 1 &&
838 test_must_fail git stash show 1 &&
839 test_must_fail git stash branch tmp 1 &&
843 test_expect_success
'valid ref of the form "n", n < N' '
850 git stash branch tmp 0 &&
858 test_must_fail git stash drop
861 test_expect_success
'branch: do not drop the stash if the branch exists' '
865 git commit -m initial &&
868 test_must_fail git stash branch main stash@{0} &&
869 git rev-parse stash@{0} --
872 test_expect_success
'branch: should not drop the stash if the apply fails' '
874 git reset HEAD~1 --hard &&
877 git commit -m initial &&
881 test_when_finished "git checkout main" &&
882 test_must_fail git stash branch new_branch stash@{0} &&
883 git rev-parse stash@{0} --
886 test_expect_success
'apply: show same status as git status (relative to ./)' '
888 echo 1 >subdir/subfile1 &&
889 echo 2 >subdir/subfile2 &&
890 git add subdir/subfile1 &&
891 git commit -m subdir &&
896 git status >../expect &&
898 sane_unset GIT_MERGE_VERBOSITY &&
901 sed -e 1d >actual && # drop "Saved..."
902 test_cmp expect actual
906 diff --git a/HEAD b/HEAD
908 index 0000000..fe0cbee
915 test_expect_success
'stash where working directory contains "HEAD" file' '
918 echo file-not-a-ref >HEAD &&
922 git diff-files --quiet &&
923 git diff-index --cached --quiet HEAD &&
924 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
925 git diff stash^..stash >output &&
926 diff_cmp expect output
929 test_expect_success
'store called with invalid commit' '
930 test_must_fail git stash store foo
933 test_expect_success
'store updates stash ref and reflog' '
938 STASH_ID=$(git stash create) &&
940 test_path_is_missing bazzy &&
941 git stash store -m quuxery $STASH_ID &&
942 test $(git rev-parse stash) = $STASH_ID &&
943 git reflog --format=%H stash| grep $STASH_ID &&
948 test_expect_success
'handle stash specification with spaces' '
952 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
956 git stash apply "stash@{$stamp}" &&
960 test_expect_success
'setup stash with index and worktree changes' '
965 echo working >file &&
969 test_expect_success
'stash list -p shows simple diff' '
970 cat >expect <<-EOF &&
973 diff --git a/file b/file
974 index 257cc56..d26b33d 100644
981 git stash list --format=%gd -p >actual &&
982 diff_cmp expect actual
985 test_expect_success
'stash list --cc shows combined diff' '
986 cat >expect <<-\EOF &&
990 index 257cc56,9015a7a..d26b33d
993 @@@ -1,1 -1,1 +1,1 @@@
998 git stash list --format=%gd -p --cc >actual &&
999 diff_cmp expect actual
1002 test_expect_success
'stash is not confused by partial renames' '
1007 test_path_is_file renamed &&
1008 test_path_is_missing file
1011 test_expect_success
'push -m shows right message' '
1014 git stash push -m "test message" &&
1015 echo "stash@{0}: On main: test message" >expect &&
1016 git stash list -1 >actual &&
1017 test_cmp expect actual
1020 test_expect_success
'push -m also works without space' '
1023 git stash push -m"unspaced test message" &&
1024 echo "stash@{0}: On main: unspaced test message" >expect &&
1025 git stash list -1 >actual &&
1026 test_cmp expect actual
1029 test_expect_success
'store -m foo shows right message' '
1034 STASH_ID=$(git stash create) &&
1035 git stash store -m "store m" $STASH_ID &&
1036 echo "stash@{0}: store m" >expect &&
1037 git stash list -1 >actual &&
1038 test_cmp expect actual
1041 test_expect_success
'store -mfoo shows right message' '
1046 STASH_ID=$(git stash create) &&
1047 git stash store -m"store mfoo" $STASH_ID &&
1048 echo "stash@{0}: store mfoo" >expect &&
1049 git stash list -1 >actual &&
1050 test_cmp expect actual
1053 test_expect_success
'store --message=foo shows right message' '
1058 STASH_ID=$(git stash create) &&
1059 git stash store --message="store message=foo" $STASH_ID &&
1060 echo "stash@{0}: store message=foo" >expect &&
1061 git stash list -1 >actual &&
1062 test_cmp expect actual
1065 test_expect_success
'store --message foo shows right message' '
1070 STASH_ID=$(git stash create) &&
1071 git stash store --message "store message foo" $STASH_ID &&
1072 echo "stash@{0}: store message foo" >expect &&
1073 git stash list -1 >actual &&
1074 test_cmp expect actual
1077 test_expect_success
'push -mfoo uses right message' '
1080 git stash push -m"test mfoo" &&
1081 echo "stash@{0}: On main: test mfoo" >expect &&
1082 git stash list -1 >actual &&
1083 test_cmp expect actual
1086 test_expect_success
'push --message foo is synonym for -mfoo' '
1089 git stash push --message "test message foo" &&
1090 echo "stash@{0}: On main: test message foo" >expect &&
1091 git stash list -1 >actual &&
1092 test_cmp expect actual
1095 test_expect_success
'push --message=foo is synonym for -mfoo' '
1098 git stash push --message="test message=foo" &&
1099 echo "stash@{0}: On main: test message=foo" >expect &&
1100 git stash list -1 >actual &&
1101 test_cmp expect actual
1104 test_expect_success
'push -m shows right message' '
1107 git stash push -m "test m foo" &&
1108 echo "stash@{0}: On main: test m foo" >expect &&
1109 git stash list -1 >actual &&
1110 test_cmp expect actual
1113 test_expect_success
'create stores correct message' '
1116 STASH_ID=$(git stash create "create test message") &&
1117 echo "On main: create test message" >expect &&
1118 git show --pretty=%s -s ${STASH_ID} >actual &&
1119 test_cmp expect actual
1122 test_expect_success
'create when branch name has /' '
1123 test_when_finished "git checkout main" &&
1124 git checkout -b some/topic &&
1127 STASH_ID=$(git stash create "create test message") &&
1128 echo "On some/topic: create test message" >expect &&
1129 git show --pretty=%s -s ${STASH_ID} >actual &&
1130 test_cmp expect actual
1133 test_expect_success
'create with multiple arguments for the message' '
1136 STASH_ID=$(git stash create test untracked) &&
1137 echo "On main: test untracked" >expect &&
1138 git show --pretty=%s -s ${STASH_ID} >actual &&
1139 test_cmp expect actual
1142 test_expect_success
'create in a detached state' '
1143 test_when_finished "git checkout main" &&
1144 git checkout HEAD~1 &&
1147 STASH_ID=$(git stash create) &&
1148 HEAD_ID=$(git rev-parse --short HEAD) &&
1149 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1150 git show --pretty=%s -s ${STASH_ID} >actual &&
1151 test_cmp expect actual
1154 test_expect_success
'stash -- <pathspec> stashes and restores the file' '
1158 git stash push -- foo &&
1159 test_path_is_file bar &&
1160 test_path_is_missing foo &&
1162 test_path_is_file foo &&
1163 test_path_is_file bar
1166 test_expect_success
'stash -- <pathspec> stashes in subdirectory' '
1173 git stash push -- ../foo
1175 test_path_is_file bar &&
1176 test_path_is_missing foo &&
1178 test_path_is_file foo &&
1179 test_path_is_file bar
1182 test_expect_success
'stash with multiple pathspec arguments' '
1186 git add foo bar extra &&
1187 git stash push -- foo bar &&
1188 test_path_is_missing bar &&
1189 test_path_is_missing foo &&
1190 test_path_is_file extra &&
1192 test_path_is_file foo &&
1193 test_path_is_file bar &&
1194 test_path_is_file extra
1197 test_expect_success
'stash with file including $IFS character' '
1202 git stash push -- "foo b*" &&
1203 test_path_is_missing "foo bar" &&
1204 test_path_is_file foo &&
1205 test_path_is_file bar &&
1207 test_path_is_file "foo bar" &&
1208 test_path_is_file foo &&
1209 test_path_is_file bar
1212 test_expect_success
'stash with pathspec matching multiple paths' '
1213 echo original >file &&
1214 echo original >other-file &&
1215 git commit -m "two" file other-file &&
1216 echo modified >file &&
1217 echo modified >other-file &&
1218 git stash push -- "*file" &&
1219 echo original >expect &&
1220 test_cmp expect file &&
1221 test_cmp expect other-file &&
1223 echo modified >expect &&
1224 test_cmp expect file &&
1225 test_cmp expect other-file
1228 test_expect_success
'stash push -p with pathspec shows no changes only once' '
1231 git commit -m "tmp" &&
1232 git stash push -p foo >actual &&
1233 echo "No local changes to save" >expect &&
1234 git reset --hard HEAD~ &&
1235 test_cmp expect actual
1238 test_expect_success
'push <pathspec>: show no changes when there are none' '
1241 git commit -m "tmp" &&
1242 git stash push foo >actual &&
1243 echo "No local changes to save" >expect &&
1244 git reset --hard HEAD~ &&
1245 test_cmp expect actual
1248 test_expect_success
'push: <pathspec> not in the repository errors out' '
1250 test_must_fail git stash push untracked &&
1251 test_path_is_file untracked
1254 test_expect_success
'push: -q is quiet with changes' '
1257 git stash push -q >output 2>&1 &&
1258 test_must_be_empty output
1261 test_expect_success
'push: -q is quiet with no changes' '
1262 git stash push -q >output 2>&1 &&
1263 test_must_be_empty output
1266 test_expect_success
'push: -q is quiet even if there is no initial commit' '
1268 test_when_finished rm -rf foo_dir &&
1272 test_must_fail git stash push -q >output 2>&1 &&
1273 test_must_be_empty output
1277 test_expect_success
'untracked files are left in place when -u is not given' '
1281 git stash push file &&
1282 test_path_is_file untracked
1285 test_expect_success
'stash without verb with pathspec' '
1290 git stash -- "foo b*" &&
1291 test_path_is_missing "foo bar" &&
1292 test_path_is_file foo &&
1293 test_path_is_file bar &&
1295 test_path_is_file "foo bar" &&
1296 test_path_is_file foo &&
1297 test_path_is_file bar
1300 test_expect_success
'stash -k -- <pathspec> leaves unstaged files intact' '
1305 git commit -m "test" &&
1308 git stash -k -- foo &&
1309 test "",bar = $(cat foo),$(cat bar) &&
1311 test foo,bar = $(cat foo),$(cat bar)
1314 test_expect_success
'stash -- <subdir> leaves untracked files in subdir intact' '
1316 >subdir/untracked &&
1319 git add subdir/tracked* &&
1320 git stash -- subdir/ &&
1321 test_path_is_missing subdir/tracked1 &&
1322 test_path_is_missing subdir/tracked2 &&
1323 test_path_is_file subdir/untracked &&
1325 test_path_is_file subdir/tracked1 &&
1326 test_path_is_file subdir/tracked2 &&
1327 test_path_is_file subdir/untracked
1330 test_expect_success
'stash -- <subdir> works with binary files' '
1332 >subdir/untracked &&
1334 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1335 git add subdir/tracked* &&
1336 git stash -- subdir/ &&
1337 test_path_is_missing subdir/tracked &&
1338 test_path_is_missing subdir/tracked-binary &&
1339 test_path_is_file subdir/untracked &&
1341 test_path_is_file subdir/tracked &&
1342 test_path_is_file subdir/tracked-binary &&
1343 test_path_is_file subdir/untracked
1346 test_expect_success
'stash with user.name and user.email set works' '
1347 test_config user.name "A U Thor" &&
1348 test_config user.email "a.u@thor" &&
1352 test_expect_success
'stash works when user.name and user.email are not set' '
1356 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1358 git show -s --format="%an <%ae>" refs/stash >actual &&
1359 test_cmp expect actual &&
1362 test_config user.useconfigonly true &&
1364 sane_unset GIT_AUTHOR_NAME &&
1365 sane_unset GIT_AUTHOR_EMAIL &&
1366 sane_unset GIT_COMMITTER_NAME &&
1367 sane_unset GIT_COMMITTER_EMAIL &&
1368 test_unconfig user.email &&
1369 test_unconfig user.name &&
1370 test_must_fail git commit -m "should fail" &&
1371 echo "git stash <git@stash>" >expect &&
1374 git show -s --format="%an <%ae>" refs/stash >actual &&
1375 test_cmp expect actual
1379 test_expect_success
'stash --keep-index with file deleted in index does not resurrect it on disk' '
1380 test_commit to-remove to-remove &&
1382 git stash --keep-index &&
1383 test_path_is_missing to-remove
1386 test_expect_success
'stash apply should succeed with unmodified file' '
1389 git commit -m base &&
1391 # now stash a modification
1392 echo modified >file &&
1395 # make the file stat dirty
1402 test_expect_success
'stash handles skip-worktree entries nicely' '
1404 echo changed >A.t &&
1406 git update-index --skip-worktree A.t &&
1410 git rev-parse --verify refs/stash:A.t
1413 test_expect_success
'git stash succeeds despite directory/file change' '
1414 test_create_repo directory_file_switch_v1 &&
1416 cd directory_file_switch_v1 &&
1419 test_write_lines this file has some words >filler &&
1421 git commit -m filler &&
1425 echo contents >filler/file &&
1430 test_expect_success
'git stash can pop file -> directory saved changes' '
1431 test_create_repo directory_file_switch_v2 &&
1433 cd directory_file_switch_v2 &&
1436 test_write_lines this file has some words >filler &&
1438 git commit -m filler &&
1442 echo contents >filler/file &&
1443 cp filler/file expect &&
1444 git stash push --include-untracked &&
1445 git stash apply --index &&
1446 test_cmp expect filler/file
1450 test_expect_success
'git stash can pop directory -> file saved changes' '
1451 test_create_repo directory_file_switch_v3 &&
1453 cd directory_file_switch_v3 &&
1457 test_write_lines some words >filler/file1 &&
1458 test_write_lines and stuff >filler/file2 &&
1460 git commit -m filler &&
1462 git rm -rf filler &&
1463 echo contents >filler &&
1465 git stash push --include-untracked &&
1466 git stash apply --index &&
1467 test_cmp expect filler
1471 test_expect_success
'restore untracked files even when we hit conflicts' '
1472 git init restore_untracked_after_conflict &&
1474 cd restore_untracked_after_conflict &&
1479 git commit -m first &&
1481 echo something >c &&
1483 git stash push --include-untracked &&
1487 git commit -m second &&
1489 test_must_fail git stash pop &&