mailmap: plug memory leak in read_mailmap_blob()
[git/gitster.git] / t / t3903-stash.sh
bloba7f71f8126fce48ef58e32d89f628a91bf234899
1 #!/bin/sh
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-lib.sh
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
33 diff_cmp () {
34 for i in "$1" "$2"
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
40 done &&
41 test_cmp "$1.compare" "$2.compare" &&
42 rm -f "$1.compare" "$2.compare"
45 setup_stash() {
46 echo 1 >file &&
47 git add file &&
48 echo unrelated >other-file &&
49 git add other-file &&
50 test_tick &&
51 git commit -m initial &&
52 echo 2 >file &&
53 git add file &&
54 echo 3 >file &&
55 test_tick &&
56 git stash &&
57 git diff-files --quiet &&
58 git diff-index --cached --quiet HEAD
61 test_expect_success 'stash some dirty working directory' '
62 setup_stash
65 cat >expect <<EOF
66 diff --git a/file b/file
67 index 0cfbf08..00750ed 100644
68 --- a/file
69 +++ b/file
70 @@ -1 +1 @@
73 EOF
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} &&
83 echo 1 >expect &&
84 test_cmp expect file
87 test_expect_success 'apply does not need clean working directory' '
88 echo 4 >other-file &&
89 git stash apply &&
90 echo 3 >expect &&
91 test_cmp expect file
94 test_expect_success 'apply does not clobber working directory changes' '
95 git reset --hard &&
96 echo 4 >file &&
97 test_must_fail git stash apply &&
98 echo 4 >expect &&
99 test_cmp expect file
102 test_expect_success 'apply stashed changes' '
103 git reset --hard &&
104 echo 5 >other-file &&
105 git add other-file &&
106 test_tick &&
107 git commit -m other-file &&
108 git stash apply &&
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 &&
118 test_tick &&
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 &&
128 mkdir subdir &&
130 cd subdir &&
131 git stash apply
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' '
140 git reset --hard &&
141 git stash list >expected &&
142 echo 7 >file &&
143 git stash &&
144 git stash drop &&
145 git stash list >actual &&
146 test_cmp expected actual &&
147 git stash apply &&
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' '
154 git reset --hard &&
155 echo 8 >file &&
156 git stash &&
157 echo 9 >file &&
158 git stash &&
159 git stash drop stash@{1} &&
160 test 2 = $(git stash list | wc -l) &&
161 git stash apply &&
162 test 9 = $(cat file) &&
163 test 1 = $(git show :file) &&
164 test 1 = $(git show HEAD:file) &&
165 git reset --hard &&
166 git stash drop &&
167 git stash apply &&
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' '
174 git reset --hard &&
175 echo 8 >file &&
176 git stash &&
177 echo 9 >file &&
178 git stash &&
179 git stash drop 1 &&
180 test 2 = $(git stash list | wc -l) &&
181 git stash apply &&
182 test 9 = $(cat file) &&
183 test 1 = $(git show :file) &&
184 test 1 = $(git show HEAD:file) &&
185 git reset --hard &&
186 git stash drop &&
187 git stash apply &&
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' '
194 git reset --hard &&
195 git rev-parse refs/stash >expect &&
196 echo 9 >file &&
197 git stash &&
198 git stash drop stash@{0} &&
199 git rev-parse refs/stash >actual &&
200 test_cmp expect actual
203 test_expect_success 'drop stash reflog updates refs/stash with rewrite' '
204 git init repo &&
206 cd repo &&
207 setup_stash
208 ) &&
209 echo 9 >repo/file &&
211 old_oid="$(git -C repo rev-parse stash@{0})" &&
212 git -C repo stash &&
213 new_oid="$(git -C repo rev-parse stash@{0})" &&
215 cat >expect <<-EOF &&
216 $new_oid
217 $old_oid
219 git -C repo reflog show refs/stash --format=%H >actual &&
220 test_cmp expect actual &&
222 git -C repo stash drop stash@{1} &&
223 git -C repo reflog show refs/stash --format=%H >actual &&
224 cat >expect <<-EOF &&
225 $new_oid
227 test_cmp expect actual
230 test_expect_success 'stash pop' '
231 git reset --hard &&
232 git 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)
239 cat >expect <<EOF
240 diff --git a/file2 b/file2
241 new file mode 100644
242 index 0000000..1fe912c
243 --- /dev/null
244 +++ b/file2
245 @@ -0,0 +1 @@
246 +bar2
249 cat >expect1 <<EOF
250 diff --git a/file b/file
251 index 257cc56..5716ca5 100644
252 --- a/file
253 +++ b/file
254 @@ -1 +1 @@
255 -foo
256 +bar
259 cat >expect2 <<EOF
260 diff --git a/file b/file
261 index 7601807..5716ca5 100644
262 --- a/file
263 +++ b/file
264 @@ -1 +1 @@
265 -baz
266 +bar
267 diff --git a/file2 b/file2
268 new file mode 100644
269 index 0000000..1fe912c
270 --- /dev/null
271 +++ b/file2
272 @@ -0,0 +1 @@
273 +bar2
276 test_expect_success 'stash branch' '
277 echo foo >file &&
278 git commit file -m first &&
279 echo bar >file &&
280 echo bar2 >file2 &&
281 git add file2 &&
282 git stash &&
283 echo baz >file &&
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 &&
290 git diff >output &&
291 diff_cmp expect1 output &&
292 git add file &&
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' '
300 echo foo >file &&
301 git stash &&
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 &&
309 echo test >file &&
310 git add new-file file &&
311 git rm other-file &&
313 git stash &&
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 &&
325 echo bar >expect &&
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' '
332 echo foo >file &&
333 git add file &&
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' '
343 git stash &&
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' '
349 git reset --hard &&
350 echo test >file &&
351 git add file &&
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 &&
362 git stash &&
364 git stash apply -q --index &&
365 git diff-files >actual &&
366 test_cmp expect actual
369 test_expect_success 'stash -k' '
370 echo bar3 >file &&
371 echo bar4 >file2 &&
372 git add file2 &&
373 git stash -k &&
374 test bar,bar4 = $(cat file),$(cat file2)
377 test_expect_success 'stash --no-keep-index' '
378 echo bar33 >file &&
379 echo bar44 >file2 &&
380 git add file2 &&
381 git stash --no-keep-index &&
382 test bar,bar2 = $(cat file),$(cat file2)
385 test_expect_success 'stash --staged' '
386 echo bar3 >file &&
387 echo bar4 >file2 &&
388 git add file2 &&
389 git stash --staged &&
390 test bar3,bar2 = $(cat file),$(cat file2) &&
391 git reset --hard &&
392 git stash pop &&
393 test bar,bar4 = $(cat file),$(cat file2)
396 test_expect_success 'stash --staged with binary file' '
397 printf "\0" >file &&
398 git add file &&
399 git stash --staged &&
400 git stash pop &&
401 printf "\0" >expect &&
402 test_cmp expect file
405 test_expect_success 'dont assume push with non-option args' '
406 test_must_fail git stash -q drop 2>err &&
407 test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
410 test_expect_success 'stash --invalid-option' '
411 echo bar5 >file &&
412 echo bar6 >file2 &&
413 git add file2 &&
414 test_must_fail git stash --invalid-option &&
415 test_must_fail git stash save --invalid-option &&
416 test bar5,bar6 = $(cat file),$(cat file2)
419 test_expect_success 'stash an added file' '
420 git reset --hard &&
421 echo new >file3 &&
422 git add file3 &&
423 git stash save "added file" &&
424 ! test -r file3 &&
425 git stash apply &&
426 test new = "$(cat file3)"
429 test_expect_success 'stash --intent-to-add file' '
430 git reset --hard &&
431 echo new >file4 &&
432 git add --intent-to-add file4 &&
433 test_when_finished "git rm -f file4" &&
434 test_must_fail git stash
437 test_expect_success 'stash rm then recreate' '
438 git reset --hard &&
439 git rm file &&
440 echo bar7 >file &&
441 git stash save "rm then recreate" &&
442 test bar = "$(cat file)" &&
443 git stash apply &&
444 test bar7 = "$(cat file)"
447 test_expect_success 'stash rm and ignore' '
448 git reset --hard &&
449 git rm file &&
450 echo file >.gitignore &&
451 git stash save "rm and ignore" &&
452 test bar = "$(cat file)" &&
453 test file = "$(cat .gitignore)" &&
454 git stash apply &&
455 ! test -r file &&
456 test file = "$(cat .gitignore)"
459 test_expect_success 'stash rm and ignore (stage .gitignore)' '
460 git reset --hard &&
461 git rm file &&
462 echo file >.gitignore &&
463 git add .gitignore &&
464 git stash save "rm and ignore (stage .gitignore)" &&
465 test bar = "$(cat file)" &&
466 ! test -r .gitignore &&
467 git stash apply &&
468 ! test -r file &&
469 test file = "$(cat .gitignore)"
472 test_expect_success SYMLINKS 'stash file to symlink' '
473 git reset --hard &&
474 rm file &&
475 ln -s file2 file &&
476 git stash save "file to symlink" &&
477 test_path_is_file_not_symlink file &&
478 test bar = "$(cat file)" &&
479 git stash apply &&
480 test_path_is_symlink file &&
481 test "$(test_readlink file)" = file2
484 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
485 git reset --hard &&
486 git rm file &&
487 ln -s file2 file &&
488 git stash save "file to symlink (stage rm)" &&
489 test_path_is_file_not_symlink file &&
490 test bar = "$(cat file)" &&
491 git stash apply &&
492 test_path_is_symlink file &&
493 test "$(test_readlink file)" = file2
496 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
497 git reset --hard &&
498 rm file &&
499 ln -s file2 file &&
500 git add file &&
501 git stash save "file to symlink (full stage)" &&
502 test_path_is_file_not_symlink file &&
503 test bar = "$(cat file)" &&
504 git stash apply &&
505 test_path_is_symlink file &&
506 test "$(test_readlink file)" = file2
509 # This test creates a commit with a symlink used for the following tests
511 test_expect_success 'stash symlink to file' '
512 git reset --hard &&
513 test_ln_s_add file filelink &&
514 git commit -m "Add symlink" &&
515 rm filelink &&
516 cp file filelink &&
517 git stash save "symlink to file"
520 test_expect_success SYMLINKS 'this must have re-created the symlink' '
521 test -h filelink &&
522 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
525 test_expect_success 'unstash must re-create the file' '
526 git stash apply &&
527 ! test -h filelink &&
528 test bar = "$(cat file)"
531 test_expect_success 'stash symlink to file (stage rm)' '
532 git reset --hard &&
533 git rm filelink &&
534 cp file filelink &&
535 git stash save "symlink to file (stage rm)"
538 test_expect_success SYMLINKS 'this must have re-created the symlink' '
539 test -h filelink &&
540 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
543 test_expect_success 'unstash must re-create the file' '
544 git stash apply &&
545 ! test -h filelink &&
546 test bar = "$(cat file)"
549 test_expect_success 'stash symlink to file (full stage)' '
550 git reset --hard &&
551 rm filelink &&
552 cp file filelink &&
553 git add filelink &&
554 git stash save "symlink to file (full stage)"
557 test_expect_success SYMLINKS 'this must have re-created the symlink' '
558 test -h filelink &&
559 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
562 test_expect_success 'unstash must re-create the file' '
563 git stash apply &&
564 ! test -h filelink &&
565 test bar = "$(cat file)"
568 test_expect_failure 'stash directory to file' '
569 git reset --hard &&
570 mkdir dir &&
571 echo foo >dir/file &&
572 git add dir/file &&
573 git commit -m "Add file in dir" &&
574 rm -fr dir &&
575 echo bar >dir &&
576 git stash save "directory to file" &&
577 test_path_is_dir dir &&
578 test foo = "$(cat dir/file)" &&
579 test_must_fail git stash apply &&
580 test bar = "$(cat dir)" &&
581 git reset --soft HEAD^
584 test_expect_failure 'stash file to directory' '
585 git reset --hard &&
586 rm file &&
587 mkdir file &&
588 echo foo >file/file &&
589 git stash save "file to directory" &&
590 test_path_is_file file &&
591 test bar = "$(cat file)" &&
592 git stash apply &&
593 test_path_is_file file/file &&
594 test foo = "$(cat file/file)"
597 test_expect_success 'giving too many ref arguments does not modify files' '
598 git stash clear &&
599 test_when_finished "git reset --hard HEAD" &&
600 echo foo >file2 &&
601 git stash &&
602 echo bar >file2 &&
603 git stash &&
604 test-tool chmtime =123456789 file2 &&
605 for type in apply pop "branch stash-branch"
607 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
608 test_grep "Too many revisions" err &&
609 test 123456789 = $(test-tool chmtime -g file2) || return 1
610 done
613 test_expect_success 'drop: too many arguments errors out (does nothing)' '
614 git stash list >expect &&
615 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
616 test_grep "Too many revisions" err &&
617 git stash list >actual &&
618 test_cmp expect actual
621 test_expect_success 'show: too many arguments errors out (does nothing)' '
622 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
623 test_grep "Too many revisions" err &&
624 test_must_be_empty out
627 test_expect_success 'stash create - no changes' '
628 git stash clear &&
629 test_when_finished "git reset --hard HEAD" &&
630 git reset --hard &&
631 git stash create >actual &&
632 test_must_be_empty actual
635 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
636 git stash clear &&
637 test_when_finished "git reset --hard HEAD" &&
638 git reset --hard &&
639 echo foo >>file &&
640 STASH_ID=$(git stash create) &&
641 git reset --hard &&
642 git stash branch stash-branch ${STASH_ID} &&
643 test_when_finished "git reset --hard HEAD && git checkout main &&
644 git branch -D stash-branch" &&
645 test $(git ls-files --modified | wc -l) -eq 1
648 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
649 git stash clear &&
650 test_when_finished "git reset --hard HEAD" &&
651 git reset --hard &&
652 echo foo >>file &&
653 git stash &&
654 test_when_finished "git stash drop" &&
655 echo bar >>file &&
656 STASH_ID=$(git stash create) &&
657 git reset --hard &&
658 git stash branch stash-branch ${STASH_ID} &&
659 test_when_finished "git reset --hard HEAD && git checkout main &&
660 git branch -D stash-branch" &&
661 test $(git ls-files --modified | wc -l) -eq 1
664 test_expect_success 'stash branch complains with no arguments' '
665 test_must_fail git stash branch 2>err &&
666 test_grep "No branch name specified" err
669 test_expect_success 'stash show format defaults to --stat' '
670 git stash clear &&
671 test_when_finished "git reset --hard HEAD" &&
672 git reset --hard &&
673 echo foo >>file &&
674 git stash &&
675 test_when_finished "git stash drop" &&
676 echo bar >>file &&
677 STASH_ID=$(git stash create) &&
678 git reset --hard &&
679 cat >expected <<-EOF &&
680 file | 1 +
681 1 file changed, 1 insertion(+)
683 git stash show ${STASH_ID} >actual &&
684 test_cmp expected actual
687 test_expect_success 'stash show - stashes on stack, stash-like argument' '
688 git stash clear &&
689 test_when_finished "git reset --hard HEAD" &&
690 git reset --hard &&
691 echo foo >>file &&
692 git stash &&
693 test_when_finished "git stash drop" &&
694 echo bar >>file &&
695 STASH_ID=$(git stash create) &&
696 git reset --hard &&
697 echo "1 0 file" >expected &&
698 git stash show --numstat ${STASH_ID} >actual &&
699 test_cmp expected actual
702 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
703 git stash clear &&
704 test_when_finished "git reset --hard HEAD" &&
705 git reset --hard &&
706 echo foo >>file &&
707 git stash &&
708 test_when_finished "git stash drop" &&
709 echo bar >>file &&
710 STASH_ID=$(git stash create) &&
711 git reset --hard &&
712 cat >expected <<-EOF &&
713 diff --git a/file b/file
714 index 7601807..935fbd3 100644
715 --- a/file
716 +++ b/file
717 @@ -1 +1,2 @@
719 +bar
721 git stash show -p ${STASH_ID} >actual &&
722 diff_cmp expected actual
725 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
726 git stash clear &&
727 test_when_finished "git reset --hard HEAD" &&
728 git reset --hard &&
729 echo foo >>file &&
730 STASH_ID=$(git stash create) &&
731 git reset --hard &&
732 echo "1 0 file" >expected &&
733 git stash show --numstat ${STASH_ID} >actual &&
734 test_cmp expected actual
737 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
738 git stash clear &&
739 test_when_finished "git reset --hard HEAD" &&
740 git reset --hard &&
741 echo foo >>file &&
742 STASH_ID=$(git stash create) &&
743 git reset --hard &&
744 cat >expected <<-EOF &&
745 diff --git a/file b/file
746 index 7601807..71b52c4 100644
747 --- a/file
748 +++ b/file
749 @@ -1 +1,2 @@
751 +foo
753 git stash show -p ${STASH_ID} >actual &&
754 diff_cmp expected actual
757 test_expect_success 'stash show --patience shows diff' '
758 git reset --hard &&
759 echo foo >>file &&
760 STASH_ID=$(git stash create) &&
761 git reset --hard &&
762 cat >expected <<-EOF &&
763 diff --git a/file b/file
764 index 7601807..71b52c4 100644
765 --- a/file
766 +++ b/file
767 @@ -1 +1,2 @@
769 +foo
771 git stash show --patience ${STASH_ID} >actual &&
772 diff_cmp expected actual
775 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
776 git stash clear &&
777 test_when_finished "git reset --hard HEAD && git stash clear" &&
778 git reset --hard &&
779 echo foo >file &&
780 git stash &&
781 echo bar >file &&
782 git stash &&
783 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
784 git stash pop &&
785 test bar = "$(cat file)" &&
786 git reset --hard HEAD
789 test_expect_success 'pop: fail early if specified stash is not a stash ref' '
790 git stash clear &&
791 test_when_finished "git reset --hard HEAD && git stash clear" &&
792 git reset --hard &&
793 echo foo >file &&
794 git stash &&
795 echo bar >file &&
796 git stash &&
797 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
798 git stash pop &&
799 test bar = "$(cat file)" &&
800 git reset --hard HEAD
803 test_expect_success 'ref with non-existent reflog' '
804 git stash clear &&
805 echo bar5 >file &&
806 echo bar6 >file2 &&
807 git add file2 &&
808 git stash &&
809 test_must_fail git rev-parse --quiet --verify does-not-exist &&
810 test_must_fail git stash drop does-not-exist &&
811 test_must_fail git stash drop does-not-exist@{0} &&
812 test_must_fail git stash pop does-not-exist &&
813 test_must_fail git stash pop does-not-exist@{0} &&
814 test_must_fail git stash apply does-not-exist &&
815 test_must_fail git stash apply does-not-exist@{0} &&
816 test_must_fail git stash show does-not-exist &&
817 test_must_fail git stash show does-not-exist@{0} &&
818 test_must_fail git stash branch tmp does-not-exist &&
819 test_must_fail git stash branch tmp does-not-exist@{0} &&
820 git stash drop
823 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
824 git stash clear &&
825 test_must_fail git stash drop stash@{0} &&
826 echo bar5 >file &&
827 echo bar6 >file2 &&
828 git add file2 &&
829 git stash &&
830 test_must_fail git stash drop stash@{1} &&
831 test_must_fail git stash pop stash@{1} &&
832 test_must_fail git stash apply stash@{1} &&
833 test_must_fail git stash show stash@{1} &&
834 test_must_fail git stash branch tmp stash@{1} &&
835 git stash drop
838 test_expect_success 'invalid ref of the form "n", n >= N' '
839 git stash clear &&
840 test_must_fail git stash drop 0 &&
841 echo bar5 >file &&
842 echo bar6 >file2 &&
843 git add file2 &&
844 git stash &&
845 test_must_fail git stash drop 1 &&
846 test_must_fail git stash pop 1 &&
847 test_must_fail git stash apply 1 &&
848 test_must_fail git stash show 1 &&
849 test_must_fail git stash branch tmp 1 &&
850 git stash drop
853 test_expect_success 'valid ref of the form "n", n < N' '
854 git stash clear &&
855 echo bar5 >file &&
856 echo bar6 >file2 &&
857 git add file2 &&
858 git stash &&
859 git stash show 0 &&
860 git stash branch tmp 0 &&
861 git checkout main &&
862 git stash &&
863 git stash apply 0 &&
864 git reset --hard &&
865 git stash pop 0 &&
866 git stash &&
867 git stash drop 0 &&
868 test_must_fail git stash drop
871 test_expect_success 'branch: do not drop the stash if the branch exists' '
872 git stash clear &&
873 echo foo >file &&
874 git add file &&
875 git commit -m initial &&
876 echo bar >file &&
877 git stash &&
878 test_must_fail git stash branch main stash@{0} &&
879 git rev-parse stash@{0} --
882 test_expect_success 'branch: should not drop the stash if the apply fails' '
883 git stash clear &&
884 git reset HEAD~1 --hard &&
885 echo foo >file &&
886 git add file &&
887 git commit -m initial &&
888 echo bar >file &&
889 git stash &&
890 echo baz >file &&
891 test_when_finished "git checkout main" &&
892 test_must_fail git stash branch new_branch stash@{0} &&
893 git rev-parse stash@{0} --
896 test_expect_success 'apply: show same status as git status (relative to ./)' '
897 git stash clear &&
898 echo 1 >subdir/subfile1 &&
899 echo 2 >subdir/subfile2 &&
900 git add subdir/subfile1 &&
901 git commit -m subdir &&
903 cd subdir &&
904 echo x >subfile1 &&
905 echo x >../file &&
906 git status >../expect &&
907 git stash &&
908 sane_unset GIT_MERGE_VERBOSITY &&
909 git stash apply
911 sed -e 1d >actual && # drop "Saved..."
912 test_cmp expect actual
915 cat >expect <<EOF
916 diff --git a/HEAD b/HEAD
917 new file mode 100644
918 index 0000000..fe0cbee
919 --- /dev/null
920 +++ b/HEAD
921 @@ -0,0 +1 @@
922 +file-not-a-ref
925 test_expect_success 'stash where working directory contains "HEAD" file' '
926 git stash clear &&
927 git reset --hard &&
928 echo file-not-a-ref >HEAD &&
929 git add HEAD &&
930 test_tick &&
931 git stash &&
932 git diff-files --quiet &&
933 git diff-index --cached --quiet HEAD &&
934 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
935 git diff stash^..stash >output &&
936 diff_cmp expect output
939 test_expect_success 'store called with invalid commit' '
940 test_must_fail git stash store foo
943 test_expect_success 'store called with non-stash commit' '
944 test_must_fail git stash store HEAD
947 test_expect_success 'store updates stash ref and reflog' '
948 git stash clear &&
949 git reset --hard &&
950 echo quux >bazzy &&
951 git add bazzy &&
952 STASH_ID=$(git stash create) &&
953 git reset --hard &&
954 test_path_is_missing bazzy &&
955 git stash store -m quuxery $STASH_ID &&
956 test $(git rev-parse stash) = $STASH_ID &&
957 git reflog --format=%H stash| grep $STASH_ID &&
958 git stash pop &&
959 grep quux bazzy
962 test_expect_success 'handle stash specification with spaces' '
963 git stash clear &&
964 echo pig >file &&
965 git stash &&
966 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
967 test_tick &&
968 echo cow >file &&
969 git stash &&
970 git stash apply "stash@{$stamp}" &&
971 grep pig file
974 test_expect_success 'setup stash with index and worktree changes' '
975 git stash clear &&
976 git reset --hard &&
977 echo index >file &&
978 git add file &&
979 echo working >file &&
980 git stash
983 test_expect_success 'stash list -p shows simple diff' '
984 cat >expect <<-EOF &&
985 stash@{0}
987 diff --git a/file b/file
988 index 257cc56..d26b33d 100644
989 --- a/file
990 +++ b/file
991 @@ -1 +1 @@
992 -foo
993 +working
995 git stash list --format=%gd -p >actual &&
996 diff_cmp expect actual
999 test_expect_success 'stash list --cc shows combined diff' '
1000 cat >expect <<-\EOF &&
1001 stash@{0}
1003 diff --cc file
1004 index 257cc56,9015a7a..d26b33d
1005 --- a/file
1006 +++ b/file
1007 @@@ -1,1 -1,1 +1,1 @@@
1008 - foo
1009 -index
1010 ++working
1012 git stash list --format=%gd -p --cc >actual &&
1013 diff_cmp expect actual
1016 test_expect_success 'stash is not confused by partial renames' '
1017 mv file renamed &&
1018 git add renamed &&
1019 git stash &&
1020 git stash apply &&
1021 test_path_is_file renamed &&
1022 test_path_is_missing file
1025 test_expect_success 'push -m shows right message' '
1026 >foo &&
1027 git add foo &&
1028 git stash push -m "test message" &&
1029 echo "stash@{0}: On main: test message" >expect &&
1030 git stash list -1 >actual &&
1031 test_cmp expect actual
1034 test_expect_success 'push -m also works without space' '
1035 >foo &&
1036 git add foo &&
1037 git stash push -m"unspaced test message" &&
1038 echo "stash@{0}: On main: unspaced test message" >expect &&
1039 git stash list -1 >actual &&
1040 test_cmp expect actual
1043 test_expect_success 'store -m foo shows right message' '
1044 git stash clear &&
1045 git reset --hard &&
1046 echo quux >bazzy &&
1047 git add bazzy &&
1048 STASH_ID=$(git stash create) &&
1049 git stash store -m "store m" $STASH_ID &&
1050 echo "stash@{0}: store m" >expect &&
1051 git stash list -1 >actual &&
1052 test_cmp expect actual
1055 test_expect_success 'store -mfoo shows right message' '
1056 git stash clear &&
1057 git reset --hard &&
1058 echo quux >bazzy &&
1059 git add bazzy &&
1060 STASH_ID=$(git stash create) &&
1061 git stash store -m"store mfoo" $STASH_ID &&
1062 echo "stash@{0}: store mfoo" >expect &&
1063 git stash list -1 >actual &&
1064 test_cmp expect actual
1067 test_expect_success 'store --message=foo shows right message' '
1068 git stash clear &&
1069 git reset --hard &&
1070 echo quux >bazzy &&
1071 git add bazzy &&
1072 STASH_ID=$(git stash create) &&
1073 git stash store --message="store message=foo" $STASH_ID &&
1074 echo "stash@{0}: store message=foo" >expect &&
1075 git stash list -1 >actual &&
1076 test_cmp expect actual
1079 test_expect_success 'store --message foo shows right message' '
1080 git stash clear &&
1081 git reset --hard &&
1082 echo quux >bazzy &&
1083 git add bazzy &&
1084 STASH_ID=$(git stash create) &&
1085 git stash store --message "store message foo" $STASH_ID &&
1086 echo "stash@{0}: store message foo" >expect &&
1087 git stash list -1 >actual &&
1088 test_cmp expect actual
1091 test_expect_success 'push -mfoo uses right message' '
1092 >foo &&
1093 git add foo &&
1094 git stash push -m"test mfoo" &&
1095 echo "stash@{0}: On main: test mfoo" >expect &&
1096 git stash list -1 >actual &&
1097 test_cmp expect actual
1100 test_expect_success 'push --message foo is synonym for -mfoo' '
1101 >foo &&
1102 git add foo &&
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 --message=foo is synonym for -mfoo' '
1110 >foo &&
1111 git add foo &&
1112 git stash push --message="test message=foo" &&
1113 echo "stash@{0}: On main: test message=foo" >expect &&
1114 git stash list -1 >actual &&
1115 test_cmp expect actual
1118 test_expect_success 'push -m shows right message' '
1119 >foo &&
1120 git add foo &&
1121 git stash push -m "test m foo" &&
1122 echo "stash@{0}: On main: test m foo" >expect &&
1123 git stash list -1 >actual &&
1124 test_cmp expect actual
1127 test_expect_success 'create stores correct message' '
1128 >foo &&
1129 git add foo &&
1130 STASH_ID=$(git stash create "create test message") &&
1131 echo "On main: create test message" >expect &&
1132 git show --pretty=%s -s ${STASH_ID} >actual &&
1133 test_cmp expect actual
1136 test_expect_success 'create when branch name has /' '
1137 test_when_finished "git checkout main" &&
1138 git checkout -b some/topic &&
1139 >foo &&
1140 git add foo &&
1141 STASH_ID=$(git stash create "create test message") &&
1142 echo "On some/topic: create test message" >expect &&
1143 git show --pretty=%s -s ${STASH_ID} >actual &&
1144 test_cmp expect actual
1147 test_expect_success 'create with multiple arguments for the message' '
1148 >foo &&
1149 git add foo &&
1150 STASH_ID=$(git stash create test untracked) &&
1151 echo "On main: test untracked" >expect &&
1152 git show --pretty=%s -s ${STASH_ID} >actual &&
1153 test_cmp expect actual
1156 test_expect_success 'create in a detached state' '
1157 test_when_finished "git checkout main" &&
1158 git checkout HEAD~1 &&
1159 >foo &&
1160 git add foo &&
1161 STASH_ID=$(git stash create) &&
1162 HEAD_ID=$(git rev-parse --short HEAD) &&
1163 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1164 git show --pretty=%s -s ${STASH_ID} >actual &&
1165 test_cmp expect actual
1168 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1169 >foo &&
1170 >bar &&
1171 git add foo bar &&
1172 git stash push -- foo &&
1173 test_path_is_file bar &&
1174 test_path_is_missing foo &&
1175 git stash pop &&
1176 test_path_is_file foo &&
1177 test_path_is_file bar
1180 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1181 mkdir sub &&
1182 >foo &&
1183 >bar &&
1184 git add foo bar &&
1186 cd sub &&
1187 git stash push -- ../foo
1188 ) &&
1189 test_path_is_file bar &&
1190 test_path_is_missing foo &&
1191 git stash pop &&
1192 test_path_is_file foo &&
1193 test_path_is_file bar
1196 test_expect_success 'stash with multiple pathspec arguments' '
1197 >foo &&
1198 >bar &&
1199 >extra &&
1200 git add foo bar extra &&
1201 git stash push -- foo bar &&
1202 test_path_is_missing bar &&
1203 test_path_is_missing foo &&
1204 test_path_is_file extra &&
1205 git stash pop &&
1206 test_path_is_file foo &&
1207 test_path_is_file bar &&
1208 test_path_is_file extra
1211 test_expect_success 'stash with file including $IFS character' '
1212 >"foo bar" &&
1213 >foo &&
1214 >bar &&
1215 git add foo* &&
1216 git stash push -- "foo b*" &&
1217 test_path_is_missing "foo bar" &&
1218 test_path_is_file foo &&
1219 test_path_is_file bar &&
1220 git stash pop &&
1221 test_path_is_file "foo bar" &&
1222 test_path_is_file foo &&
1223 test_path_is_file bar
1226 test_expect_success 'stash with pathspec matching multiple paths' '
1227 echo original >file &&
1228 echo original >other-file &&
1229 git commit -m "two" file other-file &&
1230 echo modified >file &&
1231 echo modified >other-file &&
1232 git stash push -- "*file" &&
1233 echo original >expect &&
1234 test_cmp expect file &&
1235 test_cmp expect other-file &&
1236 git stash pop &&
1237 echo modified >expect &&
1238 test_cmp expect file &&
1239 test_cmp expect other-file
1242 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1243 >foo &&
1244 git add foo &&
1245 git commit -m "tmp" &&
1246 git stash push -p foo >actual &&
1247 echo "No local changes to save" >expect &&
1248 git reset --hard HEAD~ &&
1249 test_cmp expect actual
1252 test_expect_success 'push <pathspec>: show no changes when there are none' '
1253 >foo &&
1254 git add foo &&
1255 git commit -m "tmp" &&
1256 git stash push foo >actual &&
1257 echo "No local changes to save" >expect &&
1258 git reset --hard HEAD~ &&
1259 test_cmp expect actual
1262 test_expect_success 'push: <pathspec> not in the repository errors out' '
1263 >untracked &&
1264 test_must_fail git stash push untracked &&
1265 test_path_is_file untracked
1268 test_expect_success 'push: -q is quiet with changes' '
1269 >foo &&
1270 git add foo &&
1271 git stash push -q >output 2>&1 &&
1272 test_must_be_empty output
1275 test_expect_success 'push: -q is quiet with no changes' '
1276 git stash push -q >output 2>&1 &&
1277 test_must_be_empty output
1280 test_expect_success 'push: -q is quiet even if there is no initial commit' '
1281 git init foo_dir &&
1282 test_when_finished rm -rf foo_dir &&
1284 cd foo_dir &&
1285 >bar &&
1286 test_must_fail git stash push -q >output 2>&1 &&
1287 test_must_be_empty output
1291 test_expect_success 'untracked files are left in place when -u is not given' '
1292 >file &&
1293 git add file &&
1294 >untracked &&
1295 git stash push file &&
1296 test_path_is_file untracked
1299 test_expect_success 'stash without verb with pathspec' '
1300 >"foo bar" &&
1301 >foo &&
1302 >bar &&
1303 git add foo* &&
1304 git stash -- "foo b*" &&
1305 test_path_is_missing "foo bar" &&
1306 test_path_is_file foo &&
1307 test_path_is_file bar &&
1308 git stash pop &&
1309 test_path_is_file "foo bar" &&
1310 test_path_is_file foo &&
1311 test_path_is_file bar
1314 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1315 git reset &&
1316 >foo &&
1317 >bar &&
1318 git add foo bar &&
1319 git commit -m "test" &&
1320 echo "foo" >foo &&
1321 echo "bar" >bar &&
1322 git stash -k -- foo &&
1323 test "",bar = $(cat foo),$(cat bar) &&
1324 git stash pop &&
1325 test foo,bar = $(cat foo),$(cat bar)
1328 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1329 git reset &&
1330 >subdir/untracked &&
1331 >subdir/tracked1 &&
1332 >subdir/tracked2 &&
1333 git add subdir/tracked* &&
1334 git stash -- subdir/ &&
1335 test_path_is_missing subdir/tracked1 &&
1336 test_path_is_missing subdir/tracked2 &&
1337 test_path_is_file subdir/untracked &&
1338 git stash pop &&
1339 test_path_is_file subdir/tracked1 &&
1340 test_path_is_file subdir/tracked2 &&
1341 test_path_is_file subdir/untracked
1344 test_expect_success 'stash -- <subdir> works with binary files' '
1345 git reset &&
1346 >subdir/untracked &&
1347 >subdir/tracked &&
1348 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1349 git add subdir/tracked* &&
1350 git stash -- subdir/ &&
1351 test_path_is_missing subdir/tracked &&
1352 test_path_is_missing subdir/tracked-binary &&
1353 test_path_is_file subdir/untracked &&
1354 git stash pop &&
1355 test_path_is_file subdir/tracked &&
1356 test_path_is_file subdir/tracked-binary &&
1357 test_path_is_file subdir/untracked
1360 test_expect_success 'stash with user.name and user.email set works' '
1361 test_config user.name "A U Thor" &&
1362 test_config user.email "a.u@thor" &&
1363 git stash
1366 test_expect_success 'stash works when user.name and user.email are not set' '
1367 git reset &&
1368 >1 &&
1369 git add 1 &&
1370 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1371 git stash &&
1372 git show -s --format="%an <%ae>" refs/stash >actual &&
1373 test_cmp expect actual &&
1374 >2 &&
1375 git add 2 &&
1376 test_config user.useconfigonly true &&
1378 sane_unset GIT_AUTHOR_NAME &&
1379 sane_unset GIT_AUTHOR_EMAIL &&
1380 sane_unset GIT_COMMITTER_NAME &&
1381 sane_unset GIT_COMMITTER_EMAIL &&
1382 test_unconfig user.email &&
1383 test_unconfig user.name &&
1384 test_must_fail git commit -m "should fail" &&
1385 echo "git stash <git@stash>" >expect &&
1386 >2 &&
1387 git stash &&
1388 git show -s --format="%an <%ae>" refs/stash >actual &&
1389 test_cmp expect actual
1393 test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1394 test_commit to-remove to-remove &&
1395 git rm to-remove &&
1396 git stash --keep-index &&
1397 test_path_is_missing to-remove
1400 test_expect_success 'stash apply should succeed with unmodified file' '
1401 echo base >file &&
1402 git add file &&
1403 git commit -m base &&
1405 # now stash a modification
1406 echo modified >file &&
1407 git stash &&
1409 # make the file stat dirty
1410 cp file other &&
1411 mv other file &&
1413 git stash apply
1416 test_expect_success 'stash handles skip-worktree entries nicely' '
1417 test_commit A &&
1418 echo changed >A.t &&
1419 git add A.t &&
1420 git update-index --skip-worktree A.t &&
1421 rm A.t &&
1422 git stash &&
1424 git rev-parse --verify refs/stash:A.t
1428 BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1430 test_expect_success 'stash with core.fsyncmethod=batch' "
1431 test_create_unique_files 2 4 files_base_dir &&
1432 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1434 # The files were untracked, so use the third parent,
1435 # which contains the untracked files
1436 git ls-tree -r stash^3 -- ./files_base_dir/ |
1437 test_parse_ls_tree_oids >stashed_files_oids &&
1439 # We created 2 dirs with 4 files each (8 files total) above
1440 test_line_count = 8 stashed_files_oids &&
1441 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1442 test_cmp stashed_files_oids stashed_files_actual
1446 test_expect_success 'git stash succeeds despite directory/file change' '
1447 test_create_repo directory_file_switch_v1 &&
1449 cd directory_file_switch_v1 &&
1450 test_commit init &&
1452 test_write_lines this file has some words >filler &&
1453 git add filler &&
1454 git commit -m filler &&
1456 git rm filler &&
1457 mkdir filler &&
1458 echo contents >filler/file &&
1459 git stash push
1463 test_expect_success 'git stash can pop file -> directory saved changes' '
1464 test_create_repo directory_file_switch_v2 &&
1466 cd directory_file_switch_v2 &&
1467 test_commit init &&
1469 test_write_lines this file has some words >filler &&
1470 git add filler &&
1471 git commit -m filler &&
1473 git rm filler &&
1474 mkdir filler &&
1475 echo contents >filler/file &&
1476 cp filler/file expect &&
1477 git stash push --include-untracked &&
1478 git stash apply --index &&
1479 test_cmp expect filler/file
1483 test_expect_success 'git stash can pop directory -> file saved changes' '
1484 test_create_repo directory_file_switch_v3 &&
1486 cd directory_file_switch_v3 &&
1487 test_commit init &&
1489 mkdir filler &&
1490 test_write_lines some words >filler/file1 &&
1491 test_write_lines and stuff >filler/file2 &&
1492 git add filler &&
1493 git commit -m filler &&
1495 git rm -rf filler &&
1496 echo contents >filler &&
1497 cp filler expect &&
1498 git stash push --include-untracked &&
1499 git stash apply --index &&
1500 test_cmp expect filler
1504 test_expect_success 'restore untracked files even when we hit conflicts' '
1505 git init restore_untracked_after_conflict &&
1507 cd restore_untracked_after_conflict &&
1509 echo hi >a &&
1510 echo there >b &&
1511 git add . &&
1512 git commit -m first &&
1513 echo hello >a &&
1514 echo something >c &&
1516 git stash push --include-untracked &&
1518 echo conflict >a &&
1519 git add a &&
1520 git commit -m second &&
1522 test_must_fail git stash pop &&
1524 test_path_is_file c
1528 test_expect_success 'stash create reports a locked index' '
1529 test_when_finished "rm -rf repo" &&
1530 git init repo &&
1532 cd repo &&
1533 test_commit A A.file &&
1534 echo change >A.file &&
1535 touch .git/index.lock &&
1537 cat >expect <<-EOF &&
1538 error: could not write index
1540 test_must_fail git stash create 2>err &&
1541 test_cmp expect err
1545 test_expect_success 'stash push reports a locked index' '
1546 test_when_finished "rm -rf repo" &&
1547 git init repo &&
1549 cd repo &&
1550 test_commit A A.file &&
1551 echo change >A.file &&
1552 touch .git/index.lock &&
1554 cat >expect <<-EOF &&
1555 error: could not write index
1557 test_must_fail git stash push 2>err &&
1558 test_cmp expect err
1562 test_expect_success 'stash apply reports a locked index' '
1563 test_when_finished "rm -rf repo" &&
1564 git init repo &&
1566 cd repo &&
1567 test_commit A A.file &&
1568 echo change >A.file &&
1569 git stash push &&
1570 touch .git/index.lock &&
1572 cat >expect <<-EOF &&
1573 error: could not write index
1575 test_must_fail git stash apply 2>err &&
1576 test_cmp expect err
1580 test_done