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
12 .
"$TEST_DIRECTORY"/lib-unique-files.sh
14 test_expect_success
'usage on cmd and subcommand invalid option' '
15 test_expect_code 129 git stash --invalid-option 2>usage &&
16 grep "or: git stash" usage &&
18 test_expect_code 129 git stash push --invalid-option 2>usage &&
19 ! grep "or: git stash" usage
22 test_expect_success
'usage on main command -h emits a summary of subcommands' '
23 test_expect_code 129 git stash -h >usage &&
24 grep -F "usage: git stash list" usage &&
25 grep -F "or: git stash show" usage
28 test_expect_success
'usage for subcommands should emit subcommand usage' '
29 test_expect_code 129 git stash push -h >usage &&
30 grep -F "usage: git stash [push" usage
36 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
37 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
38 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
39 "$i" >"$i.compare" ||
return 1
41 test_cmp
"$1.compare" "$2.compare" &&
42 rm -f "$1.compare" "$2.compare"
48 echo unrelated
>other-file
&&
51 git commit
-m initial
&&
57 git diff-files
--quiet &&
58 git diff-index
--cached --quiet HEAD
61 test_expect_success
'stash some dirty working directory' '
66 diff --git a/file b/file
67 index 0cfbf08..00750ed 100644
75 test_expect_success
'parents of stash' '
76 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
77 git diff stash^2..stash >output &&
78 diff_cmp expect output
81 test_expect_success
'applying bogus stash does nothing' '
82 test_must_fail git stash apply stash@{1} &&
87 test_expect_success
'apply does not need clean working directory' '
94 test_expect_success
'apply does not clobber working directory changes' '
97 test_must_fail git stash apply &&
102 test_expect_success
'apply stashed changes' '
104 echo 5 >other-file &&
105 git add other-file &&
107 git commit -m other-file &&
109 test 3 = $(cat file) &&
110 test 1 = $(git show :file) &&
111 test 1 = $(git show HEAD:file)
114 test_expect_success
'apply stashed changes (including index)' '
115 git reset --hard HEAD^ &&
116 echo 6 >other-file &&
117 git add other-file &&
119 git commit -m other-file &&
120 git stash apply --index &&
121 test 3 = $(cat file) &&
122 test 2 = $(git show :file) &&
123 test 1 = $(git show HEAD:file)
126 test_expect_success
'unstashing in a subdirectory' '
127 git reset --hard HEAD &&
135 test_expect_success
'stash drop complains of extra options' '
136 test_must_fail git stash drop --foo
139 test_expect_success
'drop top stash' '
141 git stash list >expected &&
145 git stash list >actual &&
146 test_cmp expected actual &&
148 test 3 = $(cat file) &&
149 test 1 = $(git show :file) &&
150 test 1 = $(git show HEAD:file)
153 test_expect_success
'drop middle stash' '
159 git stash drop stash@{1} &&
160 test 2 = $(git stash list | wc -l) &&
162 test 9 = $(cat file) &&
163 test 1 = $(git show :file) &&
164 test 1 = $(git show HEAD:file) &&
168 test 3 = $(cat file) &&
169 test 1 = $(git show :file) &&
170 test 1 = $(git show HEAD:file)
173 test_expect_success
'drop middle stash by index' '
180 test 2 = $(git stash list | wc -l) &&
182 test 9 = $(cat file) &&
183 test 1 = $(git show :file) &&
184 test 1 = $(git show HEAD:file) &&
188 test 3 = $(cat file) &&
189 test 1 = $(git show :file) &&
190 test 1 = $(git show HEAD:file)
193 test_expect_success
'drop stash reflog updates refs/stash' '
195 git rev-parse refs/stash >expect &&
198 git stash drop stash@{0} &&
199 git rev-parse refs/stash >actual &&
200 test_cmp expect actual
203 test_expect_success REFFILES
'drop stash reflog updates refs/stash with rewrite' '
211 old_oid="$(git -C repo rev-parse stash@{0})" &&
213 new_oid="$(git -C repo rev-parse stash@{0})" &&
215 cat >expect <<-EOF &&
216 $(test_oid zero) $old_oid
219 cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
220 test_cmp expect actual &&
222 git -C repo stash drop stash@{1} &&
223 cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
224 cat >expect <<-EOF &&
225 $(test_oid zero) $new_oid
227 test_cmp expect actual
230 test_expect_success
'stash pop' '
233 test 3 = $(cat file) &&
234 test 1 = $(git show :file) &&
235 test 1 = $(git show HEAD:file) &&
236 test 0 = $(git stash list | wc -l)
240 diff --git a/file2 b/file2
242 index 0000000..1fe912c
250 diff --git a/file b/file
251 index 257cc56..5716ca5 100644
260 diff --git a/file b/file
261 index 7601807..5716ca5 100644
267 diff --git a/file2 b/file2
269 index 0000000..1fe912c
276 test_expect_success
'stash branch' '
278 git commit file -m first &&
284 git commit file -m second &&
285 git stash branch stashbranch &&
286 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
287 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
288 git diff --cached >output &&
289 diff_cmp expect output &&
291 diff_cmp expect1 output &&
293 git commit -m alternate\ second &&
294 git diff main..stashbranch >output &&
295 diff_cmp output expect2 &&
296 test 0 = $(git stash list | wc -l)
299 test_expect_success
'apply -q is quiet' '
302 git stash apply -q >output.out 2>&1 &&
303 test_must_be_empty output.out
306 test_expect_success
'apply --index -q is quiet' '
307 # Added file, deleted file, modified file all staged for commit
308 echo foo >new-file &&
310 git add new-file file &&
314 git stash apply --index -q >output.out 2>&1 &&
315 test_must_be_empty output.out
318 test_expect_success
'save -q is quiet' '
319 git stash save --quiet >output.out 2>&1 &&
320 test_must_be_empty output.out
323 test_expect_success
'pop -q works and is quiet' '
324 git stash pop -q >output.out 2>&1 &&
326 git show :file >actual &&
327 test_cmp expect actual &&
328 test_must_be_empty output.out
331 test_expect_success
'pop -q --index works and is quiet' '
334 git stash save --quiet &&
335 git stash pop -q --index >output.out 2>&1 &&
336 git diff-files file2 >file2.diff &&
337 test_must_be_empty file2.diff &&
338 test foo = "$(git show :file)" &&
339 test_must_be_empty output.out
342 test_expect_success
'drop -q is quiet' '
344 git stash drop -q >output.out 2>&1 &&
345 test_must_be_empty output.out
348 test_expect_success
'stash push -q --staged refreshes the index' '
352 git stash push -q --staged &&
353 git diff-files >output.out &&
354 test_must_be_empty output.out
357 test_expect_success
'stash apply -q --index refreshes the index' '
358 echo test >other-file &&
359 git add other-file &&
360 echo another-change >other-file &&
361 git diff-files >expect &&
364 git stash apply -q --index &&
365 git diff-files >actual &&
366 test_cmp expect actual
369 test_expect_success
'stash -k' '
374 test bar,bar4 = $(cat file),$(cat file2)
377 test_expect_success
'stash --no-keep-index' '
381 git stash --no-keep-index &&
382 test bar,bar2 = $(cat file),$(cat file2)
385 test_expect_success
'stash --staged' '
389 git stash --staged &&
390 test bar3,bar2 = $(cat file),$(cat file2) &&
393 test bar,bar4 = $(cat file),$(cat file2)
396 test_expect_success
'dont assume push with non-option args' '
397 test_must_fail git stash -q drop 2>err &&
398 test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
401 test_expect_success
'stash --invalid-option' '
405 test_must_fail git stash --invalid-option &&
406 test_must_fail git stash save --invalid-option &&
407 test bar5,bar6 = $(cat file),$(cat file2)
410 test_expect_success
'stash an added file' '
414 git stash save "added file" &&
417 test new = "$(cat file3)"
420 test_expect_success
'stash --intent-to-add file' '
423 git add --intent-to-add file4 &&
424 test_when_finished "git rm -f file4" &&
425 test_must_fail git stash
428 test_expect_success
'stash rm then recreate' '
432 git stash save "rm then recreate" &&
433 test bar = "$(cat file)" &&
435 test bar7 = "$(cat file)"
438 test_expect_success
'stash rm and ignore' '
441 echo file >.gitignore &&
442 git stash save "rm and ignore" &&
443 test bar = "$(cat file)" &&
444 test file = "$(cat .gitignore)" &&
447 test file = "$(cat .gitignore)"
450 test_expect_success
'stash rm and ignore (stage .gitignore)' '
453 echo file >.gitignore &&
454 git add .gitignore &&
455 git stash save "rm and ignore (stage .gitignore)" &&
456 test bar = "$(cat file)" &&
457 ! test -r .gitignore &&
460 test file = "$(cat .gitignore)"
463 test_expect_success SYMLINKS
'stash file to symlink' '
467 git stash save "file to symlink" &&
468 test_path_is_file_not_symlink file &&
469 test bar = "$(cat file)" &&
471 test_path_is_symlink file &&
472 test "$(test_readlink file)" = file2
475 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
479 git stash save "file to symlink (stage rm)" &&
480 test_path_is_file_not_symlink file &&
481 test bar = "$(cat file)" &&
483 test_path_is_symlink file &&
484 test "$(test_readlink file)" = file2
487 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
492 git stash save "file to symlink (full stage)" &&
493 test_path_is_file_not_symlink file &&
494 test bar = "$(cat file)" &&
496 test_path_is_symlink file &&
497 test "$(test_readlink file)" = file2
500 # This test creates a commit with a symlink used for the following tests
502 test_expect_success
'stash symlink to file' '
504 test_ln_s_add file filelink &&
505 git commit -m "Add symlink" &&
508 git stash save "symlink to file"
511 test_expect_success SYMLINKS
'this must have re-created the symlink' '
513 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
516 test_expect_success
'unstash must re-create the file' '
518 ! test -h filelink &&
519 test bar = "$(cat file)"
522 test_expect_success
'stash symlink to file (stage rm)' '
526 git stash save "symlink to file (stage rm)"
529 test_expect_success SYMLINKS
'this must have re-created the symlink' '
531 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
534 test_expect_success
'unstash must re-create the file' '
536 ! test -h filelink &&
537 test bar = "$(cat file)"
540 test_expect_success
'stash symlink to file (full stage)' '
545 git stash save "symlink to file (full stage)"
548 test_expect_success SYMLINKS
'this must have re-created the symlink' '
550 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
553 test_expect_success
'unstash must re-create the file' '
555 ! test -h filelink &&
556 test bar = "$(cat file)"
559 test_expect_failure
'stash directory to file' '
562 echo foo >dir/file &&
564 git commit -m "Add file in dir" &&
567 git stash save "directory to file" &&
568 test_path_is_dir dir &&
569 test foo = "$(cat dir/file)" &&
570 test_must_fail git stash apply &&
571 test bar = "$(cat dir)" &&
572 git reset --soft HEAD^
575 test_expect_failure
'stash file to directory' '
579 echo foo >file/file &&
580 git stash save "file to directory" &&
581 test_path_is_file file &&
582 test bar = "$(cat file)" &&
584 test_path_is_file file/file &&
585 test foo = "$(cat file/file)"
588 test_expect_success
'giving too many ref arguments does not modify files' '
590 test_when_finished "git reset --hard HEAD" &&
595 test-tool chmtime =123456789 file2 &&
596 for type in apply pop "branch stash-branch"
598 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
599 test_grep "Too many revisions" err &&
600 test 123456789 = $(test-tool chmtime -g file2) || return 1
604 test_expect_success
'drop: too many arguments errors out (does nothing)' '
605 git stash list >expect &&
606 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
607 test_grep "Too many revisions" err &&
608 git stash list >actual &&
609 test_cmp expect actual
612 test_expect_success
'show: too many arguments errors out (does nothing)' '
613 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
614 test_grep "Too many revisions" err &&
615 test_must_be_empty out
618 test_expect_success
'stash create - no changes' '
620 test_when_finished "git reset --hard HEAD" &&
622 git stash create >actual &&
623 test_must_be_empty actual
626 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
628 test_when_finished "git reset --hard HEAD" &&
631 STASH_ID=$(git stash create) &&
633 git stash branch stash-branch ${STASH_ID} &&
634 test_when_finished "git reset --hard HEAD && git checkout main &&
635 git branch -D stash-branch" &&
636 test $(git ls-files --modified | wc -l) -eq 1
639 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
641 test_when_finished "git reset --hard HEAD" &&
645 test_when_finished "git stash drop" &&
647 STASH_ID=$(git stash create) &&
649 git stash branch stash-branch ${STASH_ID} &&
650 test_when_finished "git reset --hard HEAD && git checkout main &&
651 git branch -D stash-branch" &&
652 test $(git ls-files --modified | wc -l) -eq 1
655 test_expect_success
'stash branch complains with no arguments' '
656 test_must_fail git stash branch 2>err &&
657 test_grep "No branch name specified" err
660 test_expect_success
'stash show format defaults to --stat' '
662 test_when_finished "git reset --hard HEAD" &&
666 test_when_finished "git stash drop" &&
668 STASH_ID=$(git stash create) &&
670 cat >expected <<-EOF &&
672 1 file changed, 1 insertion(+)
674 git stash show ${STASH_ID} >actual &&
675 test_cmp expected actual
678 test_expect_success
'stash show - stashes on stack, stash-like argument' '
680 test_when_finished "git reset --hard HEAD" &&
684 test_when_finished "git stash drop" &&
686 STASH_ID=$(git stash create) &&
688 echo "1 0 file" >expected &&
689 git stash show --numstat ${STASH_ID} >actual &&
690 test_cmp expected actual
693 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
695 test_when_finished "git reset --hard HEAD" &&
699 test_when_finished "git stash drop" &&
701 STASH_ID=$(git stash create) &&
703 cat >expected <<-EOF &&
704 diff --git a/file b/file
705 index 7601807..935fbd3 100644
712 git stash show -p ${STASH_ID} >actual &&
713 diff_cmp expected actual
716 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
718 test_when_finished "git reset --hard HEAD" &&
721 STASH_ID=$(git stash create) &&
723 echo "1 0 file" >expected &&
724 git stash show --numstat ${STASH_ID} >actual &&
725 test_cmp expected actual
728 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
730 test_when_finished "git reset --hard HEAD" &&
733 STASH_ID=$(git stash create) &&
735 cat >expected <<-EOF &&
736 diff --git a/file b/file
737 index 7601807..71b52c4 100644
744 git stash show -p ${STASH_ID} >actual &&
745 diff_cmp expected actual
748 test_expect_success
'stash show --patience shows diff' '
751 STASH_ID=$(git stash create) &&
753 cat >expected <<-EOF &&
754 diff --git a/file b/file
755 index 7601807..71b52c4 100644
762 git stash show --patience ${STASH_ID} >actual &&
763 diff_cmp expected actual
766 test_expect_success
'drop: fail early if specified stash is not a stash ref' '
768 test_when_finished "git reset --hard HEAD && git stash clear" &&
774 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
776 test bar = "$(cat file)" &&
777 git reset --hard HEAD
780 test_expect_success
'pop: fail early if specified stash is not a stash ref' '
782 test_when_finished "git reset --hard HEAD && git stash clear" &&
788 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
790 test bar = "$(cat file)" &&
791 git reset --hard HEAD
794 test_expect_success
'ref with non-existent reflog' '
800 test_must_fail git rev-parse --quiet --verify does-not-exist &&
801 test_must_fail git stash drop does-not-exist &&
802 test_must_fail git stash drop does-not-exist@{0} &&
803 test_must_fail git stash pop does-not-exist &&
804 test_must_fail git stash pop does-not-exist@{0} &&
805 test_must_fail git stash apply does-not-exist &&
806 test_must_fail git stash apply does-not-exist@{0} &&
807 test_must_fail git stash show does-not-exist &&
808 test_must_fail git stash show does-not-exist@{0} &&
809 test_must_fail git stash branch tmp does-not-exist &&
810 test_must_fail git stash branch tmp does-not-exist@{0} &&
814 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
816 test_must_fail git stash drop stash@{0} &&
821 test_must_fail git stash drop stash@{1} &&
822 test_must_fail git stash pop stash@{1} &&
823 test_must_fail git stash apply stash@{1} &&
824 test_must_fail git stash show stash@{1} &&
825 test_must_fail git stash branch tmp stash@{1} &&
829 test_expect_success
'invalid ref of the form "n", n >= N' '
831 test_must_fail git stash drop 0 &&
836 test_must_fail git stash drop 1 &&
837 test_must_fail git stash pop 1 &&
838 test_must_fail git stash apply 1 &&
839 test_must_fail git stash show 1 &&
840 test_must_fail git stash branch tmp 1 &&
844 test_expect_success
'valid ref of the form "n", n < N' '
851 git stash branch tmp 0 &&
859 test_must_fail git stash drop
862 test_expect_success
'branch: do not drop the stash if the branch exists' '
866 git commit -m initial &&
869 test_must_fail git stash branch main stash@{0} &&
870 git rev-parse stash@{0} --
873 test_expect_success
'branch: should not drop the stash if the apply fails' '
875 git reset HEAD~1 --hard &&
878 git commit -m initial &&
882 test_when_finished "git checkout main" &&
883 test_must_fail git stash branch new_branch stash@{0} &&
884 git rev-parse stash@{0} --
887 test_expect_success
'apply: show same status as git status (relative to ./)' '
889 echo 1 >subdir/subfile1 &&
890 echo 2 >subdir/subfile2 &&
891 git add subdir/subfile1 &&
892 git commit -m subdir &&
897 git status >../expect &&
899 sane_unset GIT_MERGE_VERBOSITY &&
902 sed -e 1d >actual && # drop "Saved..."
903 test_cmp expect actual
907 diff --git a/HEAD b/HEAD
909 index 0000000..fe0cbee
916 test_expect_success
'stash where working directory contains "HEAD" file' '
919 echo file-not-a-ref >HEAD &&
923 git diff-files --quiet &&
924 git diff-index --cached --quiet HEAD &&
925 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
926 git diff stash^..stash >output &&
927 diff_cmp expect output
930 test_expect_success
'store called with invalid commit' '
931 test_must_fail git stash store foo
934 test_expect_success
'store called with non-stash commit' '
935 test_must_fail git stash store HEAD
938 test_expect_success
'store updates stash ref and reflog' '
943 STASH_ID=$(git stash create) &&
945 test_path_is_missing bazzy &&
946 git stash store -m quuxery $STASH_ID &&
947 test $(git rev-parse stash) = $STASH_ID &&
948 git reflog --format=%H stash| grep $STASH_ID &&
953 test_expect_success
'handle stash specification with spaces' '
957 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
961 git stash apply "stash@{$stamp}" &&
965 test_expect_success
'setup stash with index and worktree changes' '
970 echo working >file &&
974 test_expect_success
'stash list -p shows simple diff' '
975 cat >expect <<-EOF &&
978 diff --git a/file b/file
979 index 257cc56..d26b33d 100644
986 git stash list --format=%gd -p >actual &&
987 diff_cmp expect actual
990 test_expect_success
'stash list --cc shows combined diff' '
991 cat >expect <<-\EOF &&
995 index 257cc56,9015a7a..d26b33d
998 @@@ -1,1 -1,1 +1,1 @@@
1003 git stash list --format=%gd -p --cc >actual &&
1004 diff_cmp expect actual
1007 test_expect_success
'stash is not confused by partial renames' '
1012 test_path_is_file renamed &&
1013 test_path_is_missing file
1016 test_expect_success
'push -m shows right message' '
1019 git stash push -m "test message" &&
1020 echo "stash@{0}: On main: test message" >expect &&
1021 git stash list -1 >actual &&
1022 test_cmp expect actual
1025 test_expect_success
'push -m also works without space' '
1028 git stash push -m"unspaced test message" &&
1029 echo "stash@{0}: On main: unspaced test message" >expect &&
1030 git stash list -1 >actual &&
1031 test_cmp expect actual
1034 test_expect_success
'store -m foo shows right message' '
1039 STASH_ID=$(git stash create) &&
1040 git stash store -m "store m" $STASH_ID &&
1041 echo "stash@{0}: store m" >expect &&
1042 git stash list -1 >actual &&
1043 test_cmp expect actual
1046 test_expect_success
'store -mfoo shows right message' '
1051 STASH_ID=$(git stash create) &&
1052 git stash store -m"store mfoo" $STASH_ID &&
1053 echo "stash@{0}: store mfoo" >expect &&
1054 git stash list -1 >actual &&
1055 test_cmp expect actual
1058 test_expect_success
'store --message=foo shows right message' '
1063 STASH_ID=$(git stash create) &&
1064 git stash store --message="store message=foo" $STASH_ID &&
1065 echo "stash@{0}: store message=foo" >expect &&
1066 git stash list -1 >actual &&
1067 test_cmp expect actual
1070 test_expect_success
'store --message foo shows right message' '
1075 STASH_ID=$(git stash create) &&
1076 git stash store --message "store message foo" $STASH_ID &&
1077 echo "stash@{0}: store message foo" >expect &&
1078 git stash list -1 >actual &&
1079 test_cmp expect actual
1082 test_expect_success
'push -mfoo uses right message' '
1085 git stash push -m"test mfoo" &&
1086 echo "stash@{0}: On main: test mfoo" >expect &&
1087 git stash list -1 >actual &&
1088 test_cmp expect actual
1091 test_expect_success
'push --message foo is synonym for -mfoo' '
1094 git stash push --message "test message foo" &&
1095 echo "stash@{0}: On main: test message foo" >expect &&
1096 git stash list -1 >actual &&
1097 test_cmp expect actual
1100 test_expect_success
'push --message=foo is synonym for -mfoo' '
1103 git stash push --message="test message=foo" &&
1104 echo "stash@{0}: On main: test message=foo" >expect &&
1105 git stash list -1 >actual &&
1106 test_cmp expect actual
1109 test_expect_success
'push -m shows right message' '
1112 git stash push -m "test m foo" &&
1113 echo "stash@{0}: On main: test m foo" >expect &&
1114 git stash list -1 >actual &&
1115 test_cmp expect actual
1118 test_expect_success
'create stores correct message' '
1121 STASH_ID=$(git stash create "create test message") &&
1122 echo "On main: create test message" >expect &&
1123 git show --pretty=%s -s ${STASH_ID} >actual &&
1124 test_cmp expect actual
1127 test_expect_success
'create when branch name has /' '
1128 test_when_finished "git checkout main" &&
1129 git checkout -b some/topic &&
1132 STASH_ID=$(git stash create "create test message") &&
1133 echo "On some/topic: create test message" >expect &&
1134 git show --pretty=%s -s ${STASH_ID} >actual &&
1135 test_cmp expect actual
1138 test_expect_success
'create with multiple arguments for the message' '
1141 STASH_ID=$(git stash create test untracked) &&
1142 echo "On main: test untracked" >expect &&
1143 git show --pretty=%s -s ${STASH_ID} >actual &&
1144 test_cmp expect actual
1147 test_expect_success
'create in a detached state' '
1148 test_when_finished "git checkout main" &&
1149 git checkout HEAD~1 &&
1152 STASH_ID=$(git stash create) &&
1153 HEAD_ID=$(git rev-parse --short HEAD) &&
1154 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1155 git show --pretty=%s -s ${STASH_ID} >actual &&
1156 test_cmp expect actual
1159 test_expect_success
'stash -- <pathspec> stashes and restores the file' '
1163 git stash push -- foo &&
1164 test_path_is_file bar &&
1165 test_path_is_missing foo &&
1167 test_path_is_file foo &&
1168 test_path_is_file bar
1171 test_expect_success
'stash -- <pathspec> stashes in subdirectory' '
1178 git stash push -- ../foo
1180 test_path_is_file bar &&
1181 test_path_is_missing foo &&
1183 test_path_is_file foo &&
1184 test_path_is_file bar
1187 test_expect_success
'stash with multiple pathspec arguments' '
1191 git add foo bar extra &&
1192 git stash push -- foo bar &&
1193 test_path_is_missing bar &&
1194 test_path_is_missing foo &&
1195 test_path_is_file extra &&
1197 test_path_is_file foo &&
1198 test_path_is_file bar &&
1199 test_path_is_file extra
1202 test_expect_success
'stash with file including $IFS character' '
1207 git stash push -- "foo b*" &&
1208 test_path_is_missing "foo bar" &&
1209 test_path_is_file foo &&
1210 test_path_is_file bar &&
1212 test_path_is_file "foo bar" &&
1213 test_path_is_file foo &&
1214 test_path_is_file bar
1217 test_expect_success
'stash with pathspec matching multiple paths' '
1218 echo original >file &&
1219 echo original >other-file &&
1220 git commit -m "two" file other-file &&
1221 echo modified >file &&
1222 echo modified >other-file &&
1223 git stash push -- "*file" &&
1224 echo original >expect &&
1225 test_cmp expect file &&
1226 test_cmp expect other-file &&
1228 echo modified >expect &&
1229 test_cmp expect file &&
1230 test_cmp expect other-file
1233 test_expect_success
'stash push -p with pathspec shows no changes only once' '
1236 git commit -m "tmp" &&
1237 git stash push -p foo >actual &&
1238 echo "No local changes to save" >expect &&
1239 git reset --hard HEAD~ &&
1240 test_cmp expect actual
1243 test_expect_success
'push <pathspec>: show no changes when there are none' '
1246 git commit -m "tmp" &&
1247 git stash push 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> not in the repository errors out' '
1255 test_must_fail git stash push untracked &&
1256 test_path_is_file untracked
1259 test_expect_success
'push: -q is quiet with changes' '
1262 git stash push -q >output 2>&1 &&
1263 test_must_be_empty output
1266 test_expect_success
'push: -q is quiet with no changes' '
1267 git stash push -q >output 2>&1 &&
1268 test_must_be_empty output
1271 test_expect_success
'push: -q is quiet even if there is no initial commit' '
1273 test_when_finished rm -rf foo_dir &&
1277 test_must_fail git stash push -q >output 2>&1 &&
1278 test_must_be_empty output
1282 test_expect_success
'untracked files are left in place when -u is not given' '
1286 git stash push file &&
1287 test_path_is_file untracked
1290 test_expect_success
'stash without verb with pathspec' '
1295 git stash -- "foo b*" &&
1296 test_path_is_missing "foo bar" &&
1297 test_path_is_file foo &&
1298 test_path_is_file bar &&
1300 test_path_is_file "foo bar" &&
1301 test_path_is_file foo &&
1302 test_path_is_file bar
1305 test_expect_success
'stash -k -- <pathspec> leaves unstaged files intact' '
1310 git commit -m "test" &&
1313 git stash -k -- foo &&
1314 test "",bar = $(cat foo),$(cat bar) &&
1316 test foo,bar = $(cat foo),$(cat bar)
1319 test_expect_success
'stash -- <subdir> leaves untracked files in subdir intact' '
1321 >subdir/untracked &&
1324 git add subdir/tracked* &&
1325 git stash -- subdir/ &&
1326 test_path_is_missing subdir/tracked1 &&
1327 test_path_is_missing subdir/tracked2 &&
1328 test_path_is_file subdir/untracked &&
1330 test_path_is_file subdir/tracked1 &&
1331 test_path_is_file subdir/tracked2 &&
1332 test_path_is_file subdir/untracked
1335 test_expect_success
'stash -- <subdir> works with binary files' '
1337 >subdir/untracked &&
1339 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1340 git add subdir/tracked* &&
1341 git stash -- subdir/ &&
1342 test_path_is_missing subdir/tracked &&
1343 test_path_is_missing subdir/tracked-binary &&
1344 test_path_is_file subdir/untracked &&
1346 test_path_is_file subdir/tracked &&
1347 test_path_is_file subdir/tracked-binary &&
1348 test_path_is_file subdir/untracked
1351 test_expect_success
'stash with user.name and user.email set works' '
1352 test_config user.name "A U Thor" &&
1353 test_config user.email "a.u@thor" &&
1357 test_expect_success
'stash works when user.name and user.email are not set' '
1361 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1363 git show -s --format="%an <%ae>" refs/stash >actual &&
1364 test_cmp expect actual &&
1367 test_config user.useconfigonly true &&
1369 sane_unset GIT_AUTHOR_NAME &&
1370 sane_unset GIT_AUTHOR_EMAIL &&
1371 sane_unset GIT_COMMITTER_NAME &&
1372 sane_unset GIT_COMMITTER_EMAIL &&
1373 test_unconfig user.email &&
1374 test_unconfig user.name &&
1375 test_must_fail git commit -m "should fail" &&
1376 echo "git stash <git@stash>" >expect &&
1379 git show -s --format="%an <%ae>" refs/stash >actual &&
1380 test_cmp expect actual
1384 test_expect_success
'stash --keep-index with file deleted in index does not resurrect it on disk' '
1385 test_commit to-remove to-remove &&
1387 git stash --keep-index &&
1388 test_path_is_missing to-remove
1391 test_expect_success
'stash apply should succeed with unmodified file' '
1394 git commit -m base &&
1396 # now stash a modification
1397 echo modified >file &&
1400 # make the file stat dirty
1407 test_expect_success
'stash handles skip-worktree entries nicely' '
1409 echo changed >A.t &&
1411 git update-index --skip-worktree A.t &&
1415 git rev-parse --verify refs/stash:A.t
1419 BATCH_CONFIGURATION
='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1421 test_expect_success
'stash with core.fsyncmethod=batch' "
1422 test_create_unique_files 2 4 files_base_dir &&
1423 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1425 # The files were untracked, so use the third parent,
1426 # which contains the untracked files
1427 git ls-tree -r stash^3 -- ./files_base_dir/ |
1428 test_parse_ls_tree_oids >stashed_files_oids &&
1430 # We created 2 dirs with 4 files each (8 files total) above
1431 test_line_count = 8 stashed_files_oids &&
1432 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1433 test_cmp stashed_files_oids stashed_files_actual
1437 test_expect_success
'git stash succeeds despite directory/file change' '
1438 test_create_repo directory_file_switch_v1 &&
1440 cd directory_file_switch_v1 &&
1443 test_write_lines this file has some words >filler &&
1445 git commit -m filler &&
1449 echo contents >filler/file &&
1454 test_expect_success
'git stash can pop file -> directory saved changes' '
1455 test_create_repo directory_file_switch_v2 &&
1457 cd directory_file_switch_v2 &&
1460 test_write_lines this file has some words >filler &&
1462 git commit -m filler &&
1466 echo contents >filler/file &&
1467 cp filler/file expect &&
1468 git stash push --include-untracked &&
1469 git stash apply --index &&
1470 test_cmp expect filler/file
1474 test_expect_success
'git stash can pop directory -> file saved changes' '
1475 test_create_repo directory_file_switch_v3 &&
1477 cd directory_file_switch_v3 &&
1481 test_write_lines some words >filler/file1 &&
1482 test_write_lines and stuff >filler/file2 &&
1484 git commit -m filler &&
1486 git rm -rf filler &&
1487 echo contents >filler &&
1489 git stash push --include-untracked &&
1490 git stash apply --index &&
1491 test_cmp expect filler
1495 test_expect_success
'restore untracked files even when we hit conflicts' '
1496 git init restore_untracked_after_conflict &&
1498 cd restore_untracked_after_conflict &&
1503 git commit -m first &&
1505 echo something >c &&
1507 git stash push --include-untracked &&
1511 git commit -m second &&
1513 test_must_fail git stash pop &&