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
11 TEST_PASSES_SANITIZE_LEAK
=true
13 .
"$TEST_DIRECTORY"/lib-unique-files.sh
15 test_expect_success
'usage on cmd and subcommand invalid option' '
16 test_expect_code 129 git stash --invalid-option 2>usage &&
17 grep "or: git stash" usage &&
19 test_expect_code 129 git stash push --invalid-option 2>usage &&
20 ! grep "or: git stash" usage
23 test_expect_success
'usage on main command -h emits a summary of subcommands' '
24 test_expect_code 129 git stash -h >usage &&
25 grep -F "usage: git stash list" usage &&
26 grep -F "or: git stash show" usage
29 test_expect_success
'usage for subcommands should emit subcommand usage' '
30 test_expect_code 129 git stash push -h >usage &&
31 grep -F "usage: git stash [push" usage
37 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
38 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
39 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
40 "$i" >"$i.compare" ||
return 1
42 test_cmp
"$1.compare" "$2.compare" &&
43 rm -f "$1.compare" "$2.compare"
49 echo unrelated
>other-file
&&
52 git commit
-m initial
&&
58 git diff-files
--quiet &&
59 git diff-index
--cached --quiet HEAD
62 test_expect_success
'stash some dirty working directory' '
67 diff --git a/file b/file
68 index 0cfbf08..00750ed 100644
76 test_expect_success
'parents of stash' '
77 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
78 git diff stash^2..stash >output &&
79 diff_cmp expect output
82 test_expect_success
'applying bogus stash does nothing' '
83 test_must_fail git stash apply stash@{1} &&
88 test_expect_success
'apply does not need clean working directory' '
95 test_expect_success
'apply does not clobber working directory changes' '
98 test_must_fail git stash apply &&
103 test_expect_success
'apply stashed changes' '
105 echo 5 >other-file &&
106 git add other-file &&
108 git commit -m other-file &&
110 test 3 = $(cat file) &&
111 test 1 = $(git show :file) &&
112 test 1 = $(git show HEAD:file)
115 test_expect_success
'apply stashed changes (including index)' '
116 git reset --hard HEAD^ &&
117 echo 6 >other-file &&
118 git add other-file &&
120 git commit -m other-file &&
121 git stash apply --index &&
122 test 3 = $(cat file) &&
123 test 2 = $(git show :file) &&
124 test 1 = $(git show HEAD:file)
127 test_expect_success
'unstashing in a subdirectory' '
128 git reset --hard HEAD &&
136 test_expect_success
'stash drop complains of extra options' '
137 test_must_fail git stash drop --foo
140 test_expect_success
'drop top stash' '
142 git stash list >expected &&
146 git stash list >actual &&
147 test_cmp expected actual &&
149 test 3 = $(cat file) &&
150 test 1 = $(git show :file) &&
151 test 1 = $(git show HEAD:file)
154 test_expect_success
'drop middle stash' '
160 git stash drop stash@{1} &&
161 test 2 = $(git stash list | wc -l) &&
163 test 9 = $(cat file) &&
164 test 1 = $(git show :file) &&
165 test 1 = $(git show HEAD:file) &&
169 test 3 = $(cat file) &&
170 test 1 = $(git show :file) &&
171 test 1 = $(git show HEAD:file)
174 test_expect_success
'drop middle stash by index' '
181 test 2 = $(git stash list | wc -l) &&
183 test 9 = $(cat file) &&
184 test 1 = $(git show :file) &&
185 test 1 = $(git show HEAD:file) &&
189 test 3 = $(cat file) &&
190 test 1 = $(git show :file) &&
191 test 1 = $(git show HEAD:file)
194 test_expect_success
'drop stash reflog updates refs/stash' '
196 git rev-parse refs/stash >expect &&
199 git stash drop stash@{0} &&
200 git rev-parse refs/stash >actual &&
201 test_cmp expect actual
204 test_expect_success
'drop stash reflog updates refs/stash with rewrite' '
212 old_oid="$(git -C repo rev-parse stash@{0})" &&
214 new_oid="$(git -C repo rev-parse stash@{0})" &&
216 cat >expect <<-EOF &&
220 git -C repo reflog show refs/stash --format=%H >actual &&
221 test_cmp expect actual &&
223 git -C repo stash drop stash@{1} &&
224 git -C repo reflog show refs/stash --format=%H >actual &&
225 cat >expect <<-EOF &&
228 test_cmp expect actual
231 test_expect_success
'stash pop' '
234 test 3 = $(cat file) &&
235 test 1 = $(git show :file) &&
236 test 1 = $(git show HEAD:file) &&
237 test 0 = $(git stash list | wc -l)
241 diff --git a/file2 b/file2
243 index 0000000..1fe912c
251 diff --git a/file b/file
252 index 257cc56..5716ca5 100644
261 diff --git a/file b/file
262 index 7601807..5716ca5 100644
268 diff --git a/file2 b/file2
270 index 0000000..1fe912c
277 test_expect_success
'stash branch' '
279 git commit file -m first &&
285 git commit file -m second &&
286 git stash branch stashbranch &&
287 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
288 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
289 git diff --cached >output &&
290 diff_cmp expect output &&
292 diff_cmp expect1 output &&
294 git commit -m alternate\ second &&
295 git diff main..stashbranch >output &&
296 diff_cmp output expect2 &&
297 test 0 = $(git stash list | wc -l)
300 test_expect_success
'apply -q is quiet' '
303 git stash apply -q >output.out 2>&1 &&
304 test_must_be_empty output.out
307 test_expect_success
'apply --index -q is quiet' '
308 # Added file, deleted file, modified file all staged for commit
309 echo foo >new-file &&
311 git add new-file file &&
315 git stash apply --index -q >output.out 2>&1 &&
316 test_must_be_empty output.out
319 test_expect_success
'save -q is quiet' '
320 git stash save --quiet >output.out 2>&1 &&
321 test_must_be_empty output.out
324 test_expect_success
'pop -q works and is quiet' '
325 git stash pop -q >output.out 2>&1 &&
327 git show :file >actual &&
328 test_cmp expect actual &&
329 test_must_be_empty output.out
332 test_expect_success
'pop -q --index works and is quiet' '
335 git stash save --quiet &&
336 git stash pop -q --index >output.out 2>&1 &&
337 git diff-files file2 >file2.diff &&
338 test_must_be_empty file2.diff &&
339 test foo = "$(git show :file)" &&
340 test_must_be_empty output.out
343 test_expect_success
'drop -q is quiet' '
345 git stash drop -q >output.out 2>&1 &&
346 test_must_be_empty output.out
349 test_expect_success
'stash push -q --staged refreshes the index' '
353 git stash push -q --staged &&
354 git diff-files >output.out &&
355 test_must_be_empty output.out
358 test_expect_success
'stash apply -q --index refreshes the index' '
359 echo test >other-file &&
360 git add other-file &&
361 echo another-change >other-file &&
362 git diff-files >expect &&
365 git stash apply -q --index &&
366 git diff-files >actual &&
367 test_cmp expect actual
370 test_expect_success
'stash -k' '
375 test bar,bar4 = $(cat file),$(cat file2)
378 test_expect_success
'stash --no-keep-index' '
382 git stash --no-keep-index &&
383 test bar,bar2 = $(cat file),$(cat file2)
386 test_expect_success
'stash --staged' '
390 git stash --staged &&
391 test bar3,bar2 = $(cat file),$(cat file2) &&
394 test bar,bar4 = $(cat file),$(cat file2)
397 test_expect_success
'stash --staged with binary file' '
400 git stash --staged &&
402 printf "\0" >expect &&
406 test_expect_success
'dont assume push with non-option args' '
407 test_must_fail git stash -q drop 2>err &&
408 test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
411 test_expect_success
'stash --invalid-option' '
415 test_must_fail git stash --invalid-option &&
416 test_must_fail git stash save --invalid-option &&
417 test bar5,bar6 = $(cat file),$(cat file2)
420 test_expect_success
'stash an added file' '
424 git stash save "added file" &&
427 test new = "$(cat file3)"
430 test_expect_success
'stash --intent-to-add file' '
433 git add --intent-to-add file4 &&
434 test_when_finished "git rm -f file4" &&
435 test_must_fail git stash
438 test_expect_success
'stash rm then recreate' '
442 git stash save "rm then recreate" &&
443 test bar = "$(cat file)" &&
445 test bar7 = "$(cat file)"
448 test_expect_success
'stash rm and ignore' '
451 echo file >.gitignore &&
452 git stash save "rm and ignore" &&
453 test bar = "$(cat file)" &&
454 test file = "$(cat .gitignore)" &&
457 test file = "$(cat .gitignore)"
460 test_expect_success
'stash rm and ignore (stage .gitignore)' '
463 echo file >.gitignore &&
464 git add .gitignore &&
465 git stash save "rm and ignore (stage .gitignore)" &&
466 test bar = "$(cat file)" &&
467 ! test -r .gitignore &&
470 test file = "$(cat .gitignore)"
473 test_expect_success SYMLINKS
'stash file to symlink' '
477 git stash save "file to symlink" &&
478 test_path_is_file_not_symlink file &&
479 test bar = "$(cat file)" &&
481 test_path_is_symlink file &&
482 test "$(test_readlink file)" = file2
485 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
489 git stash save "file to symlink (stage rm)" &&
490 test_path_is_file_not_symlink file &&
491 test bar = "$(cat file)" &&
493 test_path_is_symlink file &&
494 test "$(test_readlink file)" = file2
497 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
502 git stash save "file to symlink (full stage)" &&
503 test_path_is_file_not_symlink file &&
504 test bar = "$(cat file)" &&
506 test_path_is_symlink file &&
507 test "$(test_readlink file)" = file2
510 # This test creates a commit with a symlink used for the following tests
512 test_expect_success
'stash symlink to file' '
514 test_ln_s_add file filelink &&
515 git commit -m "Add symlink" &&
518 git stash save "symlink to file"
521 test_expect_success SYMLINKS
'this must have re-created the symlink' '
523 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
526 test_expect_success
'unstash must re-create the file' '
528 ! test -h filelink &&
529 test bar = "$(cat file)"
532 test_expect_success
'stash symlink to file (stage rm)' '
536 git stash save "symlink to file (stage rm)"
539 test_expect_success SYMLINKS
'this must have re-created the symlink' '
541 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
544 test_expect_success
'unstash must re-create the file' '
546 ! test -h filelink &&
547 test bar = "$(cat file)"
550 test_expect_success
'stash symlink to file (full stage)' '
555 git stash save "symlink to file (full stage)"
558 test_expect_success SYMLINKS
'this must have re-created the symlink' '
560 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
563 test_expect_success
'unstash must re-create the file' '
565 ! test -h filelink &&
566 test bar = "$(cat file)"
569 test_expect_failure
'stash directory to file' '
572 echo foo >dir/file &&
574 git commit -m "Add file in dir" &&
577 git stash save "directory to file" &&
578 test_path_is_dir dir &&
579 test foo = "$(cat dir/file)" &&
580 test_must_fail git stash apply &&
581 test bar = "$(cat dir)" &&
582 git reset --soft HEAD^
585 test_expect_failure
'stash file to directory' '
589 echo foo >file/file &&
590 git stash save "file to directory" &&
591 test_path_is_file file &&
592 test bar = "$(cat file)" &&
594 test_path_is_file file/file &&
595 test foo = "$(cat file/file)"
598 test_expect_success
'giving too many ref arguments does not modify files' '
600 test_when_finished "git reset --hard HEAD" &&
605 test-tool chmtime =123456789 file2 &&
606 for type in apply pop "branch stash-branch"
608 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
609 test_grep "Too many revisions" err &&
610 test 123456789 = $(test-tool chmtime -g file2) || return 1
614 test_expect_success
'drop: too many arguments errors out (does nothing)' '
615 git stash list >expect &&
616 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
617 test_grep "Too many revisions" err &&
618 git stash list >actual &&
619 test_cmp expect actual
622 test_expect_success
'show: too many arguments errors out (does nothing)' '
623 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
624 test_grep "Too many revisions" err &&
625 test_must_be_empty out
628 test_expect_success
'stash create - no changes' '
630 test_when_finished "git reset --hard HEAD" &&
632 git stash create >actual &&
633 test_must_be_empty actual
636 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
638 test_when_finished "git reset --hard HEAD" &&
641 STASH_ID=$(git stash create) &&
643 git stash branch stash-branch ${STASH_ID} &&
644 test_when_finished "git reset --hard HEAD && git checkout main &&
645 git branch -D stash-branch" &&
646 test $(git ls-files --modified | wc -l) -eq 1
649 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
651 test_when_finished "git reset --hard HEAD" &&
655 test_when_finished "git stash drop" &&
657 STASH_ID=$(git stash create) &&
659 git stash branch stash-branch ${STASH_ID} &&
660 test_when_finished "git reset --hard HEAD && git checkout main &&
661 git branch -D stash-branch" &&
662 test $(git ls-files --modified | wc -l) -eq 1
665 test_expect_success
'stash branch complains with no arguments' '
666 test_must_fail git stash branch 2>err &&
667 test_grep "No branch name specified" err
670 test_expect_success
'stash show format defaults to --stat' '
672 test_when_finished "git reset --hard HEAD" &&
676 test_when_finished "git stash drop" &&
678 STASH_ID=$(git stash create) &&
680 cat >expected <<-EOF &&
682 1 file changed, 1 insertion(+)
684 git stash show ${STASH_ID} >actual &&
685 test_cmp expected actual
688 test_expect_success
'stash show - stashes on stack, stash-like argument' '
690 test_when_finished "git reset --hard HEAD" &&
694 test_when_finished "git stash drop" &&
696 STASH_ID=$(git stash create) &&
698 echo "1 0 file" >expected &&
699 git stash show --numstat ${STASH_ID} >actual &&
700 test_cmp expected actual
703 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
705 test_when_finished "git reset --hard HEAD" &&
709 test_when_finished "git stash drop" &&
711 STASH_ID=$(git stash create) &&
713 cat >expected <<-EOF &&
714 diff --git a/file b/file
715 index 7601807..935fbd3 100644
722 git stash show -p ${STASH_ID} >actual &&
723 diff_cmp expected actual
726 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
728 test_when_finished "git reset --hard HEAD" &&
731 STASH_ID=$(git stash create) &&
733 echo "1 0 file" >expected &&
734 git stash show --numstat ${STASH_ID} >actual &&
735 test_cmp expected actual
738 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
740 test_when_finished "git reset --hard HEAD" &&
743 STASH_ID=$(git stash create) &&
745 cat >expected <<-EOF &&
746 diff --git a/file b/file
747 index 7601807..71b52c4 100644
754 git stash show -p ${STASH_ID} >actual &&
755 diff_cmp expected actual
758 test_expect_success
'stash show --patience shows diff' '
761 STASH_ID=$(git stash create) &&
763 cat >expected <<-EOF &&
764 diff --git a/file b/file
765 index 7601807..71b52c4 100644
772 git stash show --patience ${STASH_ID} >actual &&
773 diff_cmp expected actual
776 test_expect_success
'drop: fail early if specified stash is not a stash ref' '
778 test_when_finished "git reset --hard HEAD && git stash clear" &&
784 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
786 test bar = "$(cat file)" &&
787 git reset --hard HEAD
790 test_expect_success
'pop: fail early if specified stash is not a stash ref' '
792 test_when_finished "git reset --hard HEAD && git stash clear" &&
798 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
800 test bar = "$(cat file)" &&
801 git reset --hard HEAD
804 test_expect_success
'ref with non-existent reflog' '
810 test_must_fail git rev-parse --quiet --verify does-not-exist &&
811 test_must_fail git stash drop does-not-exist &&
812 test_must_fail git stash drop does-not-exist@{0} &&
813 test_must_fail git stash pop does-not-exist &&
814 test_must_fail git stash pop does-not-exist@{0} &&
815 test_must_fail git stash apply does-not-exist &&
816 test_must_fail git stash apply does-not-exist@{0} &&
817 test_must_fail git stash show does-not-exist &&
818 test_must_fail git stash show does-not-exist@{0} &&
819 test_must_fail git stash branch tmp does-not-exist &&
820 test_must_fail git stash branch tmp does-not-exist@{0} &&
824 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
826 test_must_fail git stash drop stash@{0} &&
831 test_must_fail git stash drop stash@{1} &&
832 test_must_fail git stash pop stash@{1} &&
833 test_must_fail git stash apply stash@{1} &&
834 test_must_fail git stash show stash@{1} &&
835 test_must_fail git stash branch tmp stash@{1} &&
839 test_expect_success
'invalid ref of the form "n", n >= N' '
841 test_must_fail git stash drop 0 &&
846 test_must_fail git stash drop 1 &&
847 test_must_fail git stash pop 1 &&
848 test_must_fail git stash apply 1 &&
849 test_must_fail git stash show 1 &&
850 test_must_fail git stash branch tmp 1 &&
854 test_expect_success
'valid ref of the form "n", n < N' '
861 git stash branch tmp 0 &&
869 test_must_fail git stash drop
872 test_expect_success
'branch: do not drop the stash if the branch exists' '
876 git commit -m initial &&
879 test_must_fail git stash branch main stash@{0} &&
880 git rev-parse stash@{0} --
883 test_expect_success
'branch: should not drop the stash if the apply fails' '
885 git reset HEAD~1 --hard &&
888 git commit -m initial &&
892 test_when_finished "git checkout main" &&
893 test_must_fail git stash branch new_branch stash@{0} &&
894 git rev-parse stash@{0} --
897 test_expect_success
'apply: show same status as git status (relative to ./)' '
899 echo 1 >subdir/subfile1 &&
900 echo 2 >subdir/subfile2 &&
901 git add subdir/subfile1 &&
902 git commit -m subdir &&
907 git status >../expect &&
909 sane_unset GIT_MERGE_VERBOSITY &&
912 sed -e 1d >actual && # drop "Saved..."
913 test_cmp expect actual
917 diff --git a/HEAD b/HEAD
919 index 0000000..fe0cbee
926 test_expect_success
'stash where working directory contains "HEAD" file' '
929 echo file-not-a-ref >HEAD &&
933 git diff-files --quiet &&
934 git diff-index --cached --quiet HEAD &&
935 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
936 git diff stash^..stash >output &&
937 diff_cmp expect output
940 test_expect_success
'store called with invalid commit' '
941 test_must_fail git stash store foo
944 test_expect_success
'store called with non-stash commit' '
945 test_must_fail git stash store HEAD
948 test_expect_success
'store updates stash ref and reflog' '
953 STASH_ID=$(git stash create) &&
955 test_path_is_missing bazzy &&
956 git stash store -m quuxery $STASH_ID &&
957 test $(git rev-parse stash) = $STASH_ID &&
958 git reflog --format=%H stash| grep $STASH_ID &&
963 test_expect_success
'handle stash specification with spaces' '
967 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
971 git stash apply "stash@{$stamp}" &&
975 test_expect_success
'setup stash with index and worktree changes' '
980 echo working >file &&
984 test_expect_success
'stash list -p shows simple diff' '
985 cat >expect <<-EOF &&
988 diff --git a/file b/file
989 index 257cc56..d26b33d 100644
996 git stash list --format=%gd -p >actual &&
997 diff_cmp expect actual
1000 test_expect_success
'stash list --cc shows combined diff' '
1001 cat >expect <<-\EOF &&
1005 index 257cc56,9015a7a..d26b33d
1008 @@@ -1,1 -1,1 +1,1 @@@
1013 git stash list --format=%gd -p --cc >actual &&
1014 diff_cmp expect actual
1017 test_expect_success
'stash is not confused by partial renames' '
1022 test_path_is_file renamed &&
1023 test_path_is_missing file
1026 test_expect_success
'push -m shows right message' '
1029 git stash push -m "test message" &&
1030 echo "stash@{0}: On main: test message" >expect &&
1031 git stash list -1 >actual &&
1032 test_cmp expect actual
1035 test_expect_success
'push -m also works without space' '
1038 git stash push -m"unspaced test message" &&
1039 echo "stash@{0}: On main: unspaced test message" >expect &&
1040 git stash list -1 >actual &&
1041 test_cmp expect actual
1044 test_expect_success
'store -m foo shows right message' '
1049 STASH_ID=$(git stash create) &&
1050 git stash store -m "store m" $STASH_ID &&
1051 echo "stash@{0}: store m" >expect &&
1052 git stash list -1 >actual &&
1053 test_cmp expect actual
1056 test_expect_success
'store -mfoo shows right message' '
1061 STASH_ID=$(git stash create) &&
1062 git stash store -m"store mfoo" $STASH_ID &&
1063 echo "stash@{0}: store mfoo" >expect &&
1064 git stash list -1 >actual &&
1065 test_cmp expect actual
1068 test_expect_success
'store --message=foo shows right message' '
1073 STASH_ID=$(git stash create) &&
1074 git stash store --message="store message=foo" $STASH_ID &&
1075 echo "stash@{0}: store message=foo" >expect &&
1076 git stash list -1 >actual &&
1077 test_cmp expect actual
1080 test_expect_success
'store --message foo shows right message' '
1085 STASH_ID=$(git stash create) &&
1086 git stash store --message "store message foo" $STASH_ID &&
1087 echo "stash@{0}: store message foo" >expect &&
1088 git stash list -1 >actual &&
1089 test_cmp expect actual
1092 test_expect_success
'push -mfoo uses right message' '
1095 git stash push -m"test mfoo" &&
1096 echo "stash@{0}: On main: test mfoo" >expect &&
1097 git stash list -1 >actual &&
1098 test_cmp expect actual
1101 test_expect_success
'push --message foo is synonym for -mfoo' '
1104 git stash push --message "test message foo" &&
1105 echo "stash@{0}: On main: test message foo" >expect &&
1106 git stash list -1 >actual &&
1107 test_cmp expect actual
1110 test_expect_success
'push --message=foo is synonym for -mfoo' '
1113 git stash push --message="test message=foo" &&
1114 echo "stash@{0}: On main: test message=foo" >expect &&
1115 git stash list -1 >actual &&
1116 test_cmp expect actual
1119 test_expect_success
'push -m shows right message' '
1122 git stash push -m "test m foo" &&
1123 echo "stash@{0}: On main: test m foo" >expect &&
1124 git stash list -1 >actual &&
1125 test_cmp expect actual
1128 test_expect_success
'create stores correct message' '
1131 STASH_ID=$(git stash create "create test message") &&
1132 echo "On main: create test message" >expect &&
1133 git show --pretty=%s -s ${STASH_ID} >actual &&
1134 test_cmp expect actual
1137 test_expect_success
'create when branch name has /' '
1138 test_when_finished "git checkout main" &&
1139 git checkout -b some/topic &&
1142 STASH_ID=$(git stash create "create test message") &&
1143 echo "On some/topic: create test message" >expect &&
1144 git show --pretty=%s -s ${STASH_ID} >actual &&
1145 test_cmp expect actual
1148 test_expect_success
'create with multiple arguments for the message' '
1151 STASH_ID=$(git stash create test untracked) &&
1152 echo "On main: test untracked" >expect &&
1153 git show --pretty=%s -s ${STASH_ID} >actual &&
1154 test_cmp expect actual
1157 test_expect_success
'create in a detached state' '
1158 test_when_finished "git checkout main" &&
1159 git checkout HEAD~1 &&
1162 STASH_ID=$(git stash create) &&
1163 HEAD_ID=$(git rev-parse --short HEAD) &&
1164 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1165 git show --pretty=%s -s ${STASH_ID} >actual &&
1166 test_cmp expect actual
1169 test_expect_success
'stash -- <pathspec> stashes and restores the file' '
1173 git stash push -- foo &&
1174 test_path_is_file bar &&
1175 test_path_is_missing foo &&
1177 test_path_is_file foo &&
1178 test_path_is_file bar
1181 test_expect_success
'stash -- <pathspec> stashes in subdirectory' '
1188 git stash push -- ../foo
1190 test_path_is_file bar &&
1191 test_path_is_missing foo &&
1193 test_path_is_file foo &&
1194 test_path_is_file bar
1197 test_expect_success
'stash with multiple pathspec arguments' '
1201 git add foo bar extra &&
1202 git stash push -- foo bar &&
1203 test_path_is_missing bar &&
1204 test_path_is_missing foo &&
1205 test_path_is_file extra &&
1207 test_path_is_file foo &&
1208 test_path_is_file bar &&
1209 test_path_is_file extra
1212 test_expect_success
'stash with file including $IFS character' '
1217 git stash push -- "foo b*" &&
1218 test_path_is_missing "foo bar" &&
1219 test_path_is_file foo &&
1220 test_path_is_file bar &&
1222 test_path_is_file "foo bar" &&
1223 test_path_is_file foo &&
1224 test_path_is_file bar
1227 test_expect_success
'stash with pathspec matching multiple paths' '
1228 echo original >file &&
1229 echo original >other-file &&
1230 git commit -m "two" file other-file &&
1231 echo modified >file &&
1232 echo modified >other-file &&
1233 git stash push -- "*file" &&
1234 echo original >expect &&
1235 test_cmp expect file &&
1236 test_cmp expect other-file &&
1238 echo modified >expect &&
1239 test_cmp expect file &&
1240 test_cmp expect other-file
1243 test_expect_success
'stash push -p with pathspec shows no changes only once' '
1246 git commit -m "tmp" &&
1247 git stash push -p foo >actual &&
1248 echo "No local changes to save" >expect &&
1249 git reset --hard HEAD~ &&
1250 test_cmp expect actual
1253 test_expect_success
'push <pathspec>: show no changes when there are none' '
1256 git commit -m "tmp" &&
1257 git stash push foo >actual &&
1258 echo "No local changes to save" >expect &&
1259 git reset --hard HEAD~ &&
1260 test_cmp expect actual
1263 test_expect_success
'push: <pathspec> not in the repository errors out' '
1265 test_must_fail git stash push untracked &&
1266 test_path_is_file untracked
1269 test_expect_success
'push: -q is quiet with changes' '
1272 git stash push -q >output 2>&1 &&
1273 test_must_be_empty output
1276 test_expect_success
'push: -q is quiet with no changes' '
1277 git stash push -q >output 2>&1 &&
1278 test_must_be_empty output
1281 test_expect_success
'push: -q is quiet even if there is no initial commit' '
1283 test_when_finished rm -rf foo_dir &&
1287 test_must_fail git stash push -q >output 2>&1 &&
1288 test_must_be_empty output
1292 test_expect_success
'untracked files are left in place when -u is not given' '
1296 git stash push file &&
1297 test_path_is_file untracked
1300 test_expect_success
'stash without verb with pathspec' '
1305 git stash -- "foo b*" &&
1306 test_path_is_missing "foo bar" &&
1307 test_path_is_file foo &&
1308 test_path_is_file bar &&
1310 test_path_is_file "foo bar" &&
1311 test_path_is_file foo &&
1312 test_path_is_file bar
1315 test_expect_success
'stash -k -- <pathspec> leaves unstaged files intact' '
1320 git commit -m "test" &&
1323 git stash -k -- foo &&
1324 test "",bar = $(cat foo),$(cat bar) &&
1326 test foo,bar = $(cat foo),$(cat bar)
1329 test_expect_success
'stash -- <subdir> leaves untracked files in subdir intact' '
1331 >subdir/untracked &&
1334 git add subdir/tracked* &&
1335 git stash -- subdir/ &&
1336 test_path_is_missing subdir/tracked1 &&
1337 test_path_is_missing subdir/tracked2 &&
1338 test_path_is_file subdir/untracked &&
1340 test_path_is_file subdir/tracked1 &&
1341 test_path_is_file subdir/tracked2 &&
1342 test_path_is_file subdir/untracked
1345 test_expect_success
'stash -- <subdir> works with binary files' '
1347 >subdir/untracked &&
1349 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1350 git add subdir/tracked* &&
1351 git stash -- subdir/ &&
1352 test_path_is_missing subdir/tracked &&
1353 test_path_is_missing subdir/tracked-binary &&
1354 test_path_is_file subdir/untracked &&
1356 test_path_is_file subdir/tracked &&
1357 test_path_is_file subdir/tracked-binary &&
1358 test_path_is_file subdir/untracked
1361 test_expect_success
'stash with user.name and user.email set works' '
1362 test_config user.name "A U Thor" &&
1363 test_config user.email "a.u@thor" &&
1367 test_expect_success
'stash works when user.name and user.email are not set' '
1371 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1373 git show -s --format="%an <%ae>" refs/stash >actual &&
1374 test_cmp expect actual &&
1377 test_config user.useconfigonly true &&
1379 sane_unset GIT_AUTHOR_NAME &&
1380 sane_unset GIT_AUTHOR_EMAIL &&
1381 sane_unset GIT_COMMITTER_NAME &&
1382 sane_unset GIT_COMMITTER_EMAIL &&
1383 test_unconfig user.email &&
1384 test_unconfig user.name &&
1385 test_must_fail git commit -m "should fail" &&
1386 echo "git stash <git@stash>" >expect &&
1389 git show -s --format="%an <%ae>" refs/stash >actual &&
1390 test_cmp expect actual
1394 test_expect_success
'stash --keep-index with file deleted in index does not resurrect it on disk' '
1395 test_commit to-remove to-remove &&
1397 git stash --keep-index &&
1398 test_path_is_missing to-remove
1401 test_expect_success
'stash --keep-index --include-untracked with empty tree' '
1402 test_when_finished "rm -rf empty" &&
1406 git commit --allow-empty --message "empty" &&
1407 echo content >file &&
1408 git stash push --keep-index --include-untracked &&
1409 test_path_is_missing file &&
1411 echo content >expect &&
1412 test_cmp expect file
1416 test_expect_success
'stash apply should succeed with unmodified file' '
1419 git commit -m base &&
1421 # now stash a modification
1422 echo modified >file &&
1425 # make the file stat dirty
1432 test_expect_success
'stash handles skip-worktree entries nicely' '
1434 echo changed >A.t &&
1436 git update-index --skip-worktree A.t &&
1440 git rev-parse --verify refs/stash:A.t
1444 BATCH_CONFIGURATION
='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1446 test_expect_success
'stash with core.fsyncmethod=batch' "
1447 test_create_unique_files 2 4 files_base_dir &&
1448 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1450 # The files were untracked, so use the third parent,
1451 # which contains the untracked files
1452 git ls-tree -r stash^3 -- ./files_base_dir/ |
1453 test_parse_ls_tree_oids >stashed_files_oids &&
1455 # We created 2 dirs with 4 files each (8 files total) above
1456 test_line_count = 8 stashed_files_oids &&
1457 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1458 test_cmp stashed_files_oids stashed_files_actual
1462 test_expect_success
'git stash succeeds despite directory/file change' '
1463 test_create_repo directory_file_switch_v1 &&
1465 cd directory_file_switch_v1 &&
1468 test_write_lines this file has some words >filler &&
1470 git commit -m filler &&
1474 echo contents >filler/file &&
1479 test_expect_success
'git stash can pop file -> directory saved changes' '
1480 test_create_repo directory_file_switch_v2 &&
1482 cd directory_file_switch_v2 &&
1485 test_write_lines this file has some words >filler &&
1487 git commit -m filler &&
1491 echo contents >filler/file &&
1492 cp filler/file expect &&
1493 git stash push --include-untracked &&
1494 git stash apply --index &&
1495 test_cmp expect filler/file
1499 test_expect_success
'git stash can pop directory -> file saved changes' '
1500 test_create_repo directory_file_switch_v3 &&
1502 cd directory_file_switch_v3 &&
1506 test_write_lines some words >filler/file1 &&
1507 test_write_lines and stuff >filler/file2 &&
1509 git commit -m filler &&
1511 git rm -rf filler &&
1512 echo contents >filler &&
1514 git stash push --include-untracked &&
1515 git stash apply --index &&
1516 test_cmp expect filler
1520 test_expect_success
'restore untracked files even when we hit conflicts' '
1521 git init restore_untracked_after_conflict &&
1523 cd restore_untracked_after_conflict &&
1528 git commit -m first &&
1530 echo something >c &&
1532 git stash push --include-untracked &&
1536 git commit -m second &&
1538 test_must_fail git stash pop &&
1544 test_expect_success
'stash create reports a locked index' '
1545 test_when_finished "rm -rf repo" &&
1549 test_commit A A.file &&
1550 echo change >A.file &&
1551 touch .git/index.lock &&
1553 cat >expect <<-EOF &&
1554 error: could not write index
1556 test_must_fail git stash create 2>err &&
1561 test_expect_success
'stash push reports a locked index' '
1562 test_when_finished "rm -rf repo" &&
1566 test_commit A A.file &&
1567 echo change >A.file &&
1568 touch .git/index.lock &&
1570 cat >expect <<-EOF &&
1571 error: could not write index
1573 test_must_fail git stash push 2>err &&
1578 test_expect_success
'stash apply reports a locked index' '
1579 test_when_finished "rm -rf repo" &&
1583 test_commit A A.file &&
1584 echo change >A.file &&
1586 touch .git/index.lock &&
1588 cat >expect <<-EOF &&
1589 error: could not write index
1591 test_must_fail git stash apply 2>err &&