Sync with 'maint'
[alt-git.git] / t / t3903-stash.sh
blobc87592ee2f39fe3568fe98955d0df24bd2180f68
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_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
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
34 diff_cmp () {
35 for i in "$1" "$2"
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
41 done &&
42 test_cmp "$1.compare" "$2.compare" &&
43 rm -f "$1.compare" "$2.compare"
46 setup_stash() {
47 echo 1 >file &&
48 git add file &&
49 echo unrelated >other-file &&
50 git add other-file &&
51 test_tick &&
52 git commit -m initial &&
53 echo 2 >file &&
54 git add file &&
55 echo 3 >file &&
56 test_tick &&
57 git stash &&
58 git diff-files --quiet &&
59 git diff-index --cached --quiet HEAD
62 test_expect_success 'stash some dirty working directory' '
63 setup_stash
66 cat >expect <<EOF
67 diff --git a/file b/file
68 index 0cfbf08..00750ed 100644
69 --- a/file
70 +++ b/file
71 @@ -1 +1 @@
74 EOF
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} &&
84 echo 1 >expect &&
85 test_cmp expect file
88 test_expect_success 'apply does not need clean working directory' '
89 echo 4 >other-file &&
90 git stash apply &&
91 echo 3 >expect &&
92 test_cmp expect file
95 test_expect_success 'apply does not clobber working directory changes' '
96 git reset --hard &&
97 echo 4 >file &&
98 test_must_fail git stash apply &&
99 echo 4 >expect &&
100 test_cmp expect file
103 test_expect_success 'apply stashed changes' '
104 git reset --hard &&
105 echo 5 >other-file &&
106 git add other-file &&
107 test_tick &&
108 git commit -m other-file &&
109 git stash apply &&
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 &&
119 test_tick &&
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 &&
129 mkdir subdir &&
131 cd subdir &&
132 git stash apply
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' '
141 git reset --hard &&
142 git stash list >expected &&
143 echo 7 >file &&
144 git stash &&
145 git stash drop &&
146 git stash list >actual &&
147 test_cmp expected actual &&
148 git stash apply &&
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' '
155 git reset --hard &&
156 echo 8 >file &&
157 git stash &&
158 echo 9 >file &&
159 git stash &&
160 git stash drop stash@{1} &&
161 test 2 = $(git stash list | wc -l) &&
162 git stash apply &&
163 test 9 = $(cat file) &&
164 test 1 = $(git show :file) &&
165 test 1 = $(git show HEAD:file) &&
166 git reset --hard &&
167 git stash drop &&
168 git stash apply &&
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' '
175 git reset --hard &&
176 echo 8 >file &&
177 git stash &&
178 echo 9 >file &&
179 git stash &&
180 git stash drop 1 &&
181 test 2 = $(git stash list | wc -l) &&
182 git stash apply &&
183 test 9 = $(cat file) &&
184 test 1 = $(git show :file) &&
185 test 1 = $(git show HEAD:file) &&
186 git reset --hard &&
187 git stash drop &&
188 git stash apply &&
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' '
195 git reset --hard &&
196 git rev-parse refs/stash >expect &&
197 echo 9 >file &&
198 git stash &&
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' '
205 git init repo &&
207 cd repo &&
208 setup_stash
209 ) &&
210 echo 9 >repo/file &&
212 old_oid="$(git -C repo rev-parse stash@{0})" &&
213 git -C repo stash &&
214 new_oid="$(git -C repo rev-parse stash@{0})" &&
216 cat >expect <<-EOF &&
217 $new_oid
218 $old_oid
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 &&
226 $new_oid
228 test_cmp expect actual
231 test_expect_success 'stash pop' '
232 git reset --hard &&
233 git 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)
240 cat >expect <<EOF
241 diff --git a/file2 b/file2
242 new file mode 100644
243 index 0000000..1fe912c
244 --- /dev/null
245 +++ b/file2
246 @@ -0,0 +1 @@
247 +bar2
250 cat >expect1 <<EOF
251 diff --git a/file b/file
252 index 257cc56..5716ca5 100644
253 --- a/file
254 +++ b/file
255 @@ -1 +1 @@
256 -foo
257 +bar
260 cat >expect2 <<EOF
261 diff --git a/file b/file
262 index 7601807..5716ca5 100644
263 --- a/file
264 +++ b/file
265 @@ -1 +1 @@
266 -baz
267 +bar
268 diff --git a/file2 b/file2
269 new file mode 100644
270 index 0000000..1fe912c
271 --- /dev/null
272 +++ b/file2
273 @@ -0,0 +1 @@
274 +bar2
277 test_expect_success 'stash branch' '
278 echo foo >file &&
279 git commit file -m first &&
280 echo bar >file &&
281 echo bar2 >file2 &&
282 git add file2 &&
283 git stash &&
284 echo baz >file &&
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 &&
291 git diff >output &&
292 diff_cmp expect1 output &&
293 git add file &&
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' '
301 echo foo >file &&
302 git stash &&
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 &&
310 echo test >file &&
311 git add new-file file &&
312 git rm other-file &&
314 git stash &&
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 &&
326 echo bar >expect &&
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' '
333 echo foo >file &&
334 git add file &&
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' '
344 git stash &&
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' '
350 git reset --hard &&
351 echo test >file &&
352 git add file &&
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 &&
363 git stash &&
365 git stash apply -q --index &&
366 git diff-files >actual &&
367 test_cmp expect actual
370 test_expect_success 'stash -k' '
371 echo bar3 >file &&
372 echo bar4 >file2 &&
373 git add file2 &&
374 git stash -k &&
375 test bar,bar4 = $(cat file),$(cat file2)
378 test_expect_success 'stash --no-keep-index' '
379 echo bar33 >file &&
380 echo bar44 >file2 &&
381 git add file2 &&
382 git stash --no-keep-index &&
383 test bar,bar2 = $(cat file),$(cat file2)
386 test_expect_success 'stash --staged' '
387 echo bar3 >file &&
388 echo bar4 >file2 &&
389 git add file2 &&
390 git stash --staged &&
391 test bar3,bar2 = $(cat file),$(cat file2) &&
392 git reset --hard &&
393 git stash pop &&
394 test bar,bar4 = $(cat file),$(cat file2)
397 test_expect_success 'stash --staged with binary file' '
398 printf "\0" >file &&
399 git add file &&
400 git stash --staged &&
401 git stash pop &&
402 printf "\0" >expect &&
403 test_cmp expect file
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' '
412 echo bar5 >file &&
413 echo bar6 >file2 &&
414 git add file2 &&
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' '
421 git reset --hard &&
422 echo new >file3 &&
423 git add file3 &&
424 git stash save "added file" &&
425 ! test -r file3 &&
426 git stash apply &&
427 test new = "$(cat file3)"
430 test_expect_success 'stash --intent-to-add file' '
431 git reset --hard &&
432 echo new >file4 &&
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' '
439 git reset --hard &&
440 git rm file &&
441 echo bar7 >file &&
442 git stash save "rm then recreate" &&
443 test bar = "$(cat file)" &&
444 git stash apply &&
445 test bar7 = "$(cat file)"
448 test_expect_success 'stash rm and ignore' '
449 git reset --hard &&
450 git rm file &&
451 echo file >.gitignore &&
452 git stash save "rm and ignore" &&
453 test bar = "$(cat file)" &&
454 test file = "$(cat .gitignore)" &&
455 git stash apply &&
456 ! test -r file &&
457 test file = "$(cat .gitignore)"
460 test_expect_success 'stash rm and ignore (stage .gitignore)' '
461 git reset --hard &&
462 git rm file &&
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 &&
468 git stash apply &&
469 ! test -r file &&
470 test file = "$(cat .gitignore)"
473 test_expect_success SYMLINKS 'stash file to symlink' '
474 git reset --hard &&
475 rm file &&
476 ln -s file2 file &&
477 git stash save "file to symlink" &&
478 test_path_is_file_not_symlink file &&
479 test bar = "$(cat file)" &&
480 git stash apply &&
481 test_path_is_symlink file &&
482 test "$(test_readlink file)" = file2
485 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
486 git reset --hard &&
487 git rm file &&
488 ln -s file2 file &&
489 git stash save "file to symlink (stage rm)" &&
490 test_path_is_file_not_symlink file &&
491 test bar = "$(cat file)" &&
492 git stash apply &&
493 test_path_is_symlink file &&
494 test "$(test_readlink file)" = file2
497 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
498 git reset --hard &&
499 rm file &&
500 ln -s file2 file &&
501 git add file &&
502 git stash save "file to symlink (full stage)" &&
503 test_path_is_file_not_symlink file &&
504 test bar = "$(cat file)" &&
505 git stash apply &&
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' '
513 git reset --hard &&
514 test_ln_s_add file filelink &&
515 git commit -m "Add symlink" &&
516 rm filelink &&
517 cp file filelink &&
518 git stash save "symlink to file"
521 test_expect_success SYMLINKS 'this must have re-created the symlink' '
522 test -h filelink &&
523 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
526 test_expect_success 'unstash must re-create the file' '
527 git stash apply &&
528 ! test -h filelink &&
529 test bar = "$(cat file)"
532 test_expect_success 'stash symlink to file (stage rm)' '
533 git reset --hard &&
534 git rm filelink &&
535 cp file filelink &&
536 git stash save "symlink to file (stage rm)"
539 test_expect_success SYMLINKS 'this must have re-created the symlink' '
540 test -h filelink &&
541 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
544 test_expect_success 'unstash must re-create the file' '
545 git stash apply &&
546 ! test -h filelink &&
547 test bar = "$(cat file)"
550 test_expect_success 'stash symlink to file (full stage)' '
551 git reset --hard &&
552 rm filelink &&
553 cp file filelink &&
554 git add filelink &&
555 git stash save "symlink to file (full stage)"
558 test_expect_success SYMLINKS 'this must have re-created the symlink' '
559 test -h filelink &&
560 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
563 test_expect_success 'unstash must re-create the file' '
564 git stash apply &&
565 ! test -h filelink &&
566 test bar = "$(cat file)"
569 test_expect_failure 'stash directory to file' '
570 git reset --hard &&
571 mkdir dir &&
572 echo foo >dir/file &&
573 git add dir/file &&
574 git commit -m "Add file in dir" &&
575 rm -fr dir &&
576 echo bar >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' '
586 git reset --hard &&
587 rm file &&
588 mkdir file &&
589 echo foo >file/file &&
590 git stash save "file to directory" &&
591 test_path_is_file file &&
592 test bar = "$(cat file)" &&
593 git stash apply &&
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' '
599 git stash clear &&
600 test_when_finished "git reset --hard HEAD" &&
601 echo foo >file2 &&
602 git stash &&
603 echo bar >file2 &&
604 git stash &&
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
611 done
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' '
629 git stash clear &&
630 test_when_finished "git reset --hard HEAD" &&
631 git reset --hard &&
632 git stash create >actual &&
633 test_must_be_empty actual
636 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
637 git stash clear &&
638 test_when_finished "git reset --hard HEAD" &&
639 git reset --hard &&
640 echo foo >>file &&
641 STASH_ID=$(git stash create) &&
642 git reset --hard &&
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' '
650 git stash clear &&
651 test_when_finished "git reset --hard HEAD" &&
652 git reset --hard &&
653 echo foo >>file &&
654 git stash &&
655 test_when_finished "git stash drop" &&
656 echo bar >>file &&
657 STASH_ID=$(git stash create) &&
658 git reset --hard &&
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' '
671 git stash clear &&
672 test_when_finished "git reset --hard HEAD" &&
673 git reset --hard &&
674 echo foo >>file &&
675 git stash &&
676 test_when_finished "git stash drop" &&
677 echo bar >>file &&
678 STASH_ID=$(git stash create) &&
679 git reset --hard &&
680 cat >expected <<-EOF &&
681 file | 1 +
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' '
689 git stash clear &&
690 test_when_finished "git reset --hard HEAD" &&
691 git reset --hard &&
692 echo foo >>file &&
693 git stash &&
694 test_when_finished "git stash drop" &&
695 echo bar >>file &&
696 STASH_ID=$(git stash create) &&
697 git reset --hard &&
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' '
704 git stash clear &&
705 test_when_finished "git reset --hard HEAD" &&
706 git reset --hard &&
707 echo foo >>file &&
708 git stash &&
709 test_when_finished "git stash drop" &&
710 echo bar >>file &&
711 STASH_ID=$(git stash create) &&
712 git reset --hard &&
713 cat >expected <<-EOF &&
714 diff --git a/file b/file
715 index 7601807..935fbd3 100644
716 --- a/file
717 +++ b/file
718 @@ -1 +1,2 @@
720 +bar
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' '
727 git stash clear &&
728 test_when_finished "git reset --hard HEAD" &&
729 git reset --hard &&
730 echo foo >>file &&
731 STASH_ID=$(git stash create) &&
732 git reset --hard &&
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' '
739 git stash clear &&
740 test_when_finished "git reset --hard HEAD" &&
741 git reset --hard &&
742 echo foo >>file &&
743 STASH_ID=$(git stash create) &&
744 git reset --hard &&
745 cat >expected <<-EOF &&
746 diff --git a/file b/file
747 index 7601807..71b52c4 100644
748 --- a/file
749 +++ b/file
750 @@ -1 +1,2 @@
752 +foo
754 git stash show -p ${STASH_ID} >actual &&
755 diff_cmp expected actual
758 test_expect_success 'stash show --patience shows diff' '
759 git reset --hard &&
760 echo foo >>file &&
761 STASH_ID=$(git stash create) &&
762 git reset --hard &&
763 cat >expected <<-EOF &&
764 diff --git a/file b/file
765 index 7601807..71b52c4 100644
766 --- a/file
767 +++ b/file
768 @@ -1 +1,2 @@
770 +foo
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' '
777 git stash clear &&
778 test_when_finished "git reset --hard HEAD && git stash clear" &&
779 git reset --hard &&
780 echo foo >file &&
781 git stash &&
782 echo bar >file &&
783 git stash &&
784 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
785 git stash pop &&
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' '
791 git stash clear &&
792 test_when_finished "git reset --hard HEAD && git stash clear" &&
793 git reset --hard &&
794 echo foo >file &&
795 git stash &&
796 echo bar >file &&
797 git stash &&
798 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
799 git stash pop &&
800 test bar = "$(cat file)" &&
801 git reset --hard HEAD
804 test_expect_success 'ref with non-existent reflog' '
805 git stash clear &&
806 echo bar5 >file &&
807 echo bar6 >file2 &&
808 git add file2 &&
809 git stash &&
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} &&
821 git stash drop
824 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
825 git stash clear &&
826 test_must_fail git stash drop stash@{0} &&
827 echo bar5 >file &&
828 echo bar6 >file2 &&
829 git add file2 &&
830 git stash &&
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} &&
836 git stash drop
839 test_expect_success 'invalid ref of the form "n", n >= N' '
840 git stash clear &&
841 test_must_fail git stash drop 0 &&
842 echo bar5 >file &&
843 echo bar6 >file2 &&
844 git add file2 &&
845 git stash &&
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 &&
851 git stash drop
854 test_expect_success 'valid ref of the form "n", n < N' '
855 git stash clear &&
856 echo bar5 >file &&
857 echo bar6 >file2 &&
858 git add file2 &&
859 git stash &&
860 git stash show 0 &&
861 git stash branch tmp 0 &&
862 git checkout main &&
863 git stash &&
864 git stash apply 0 &&
865 git reset --hard &&
866 git stash pop 0 &&
867 git stash &&
868 git stash drop 0 &&
869 test_must_fail git stash drop
872 test_expect_success 'branch: do not drop the stash if the branch exists' '
873 git stash clear &&
874 echo foo >file &&
875 git add file &&
876 git commit -m initial &&
877 echo bar >file &&
878 git stash &&
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' '
884 git stash clear &&
885 git reset HEAD~1 --hard &&
886 echo foo >file &&
887 git add file &&
888 git commit -m initial &&
889 echo bar >file &&
890 git stash &&
891 echo baz >file &&
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 ./)' '
898 git stash clear &&
899 echo 1 >subdir/subfile1 &&
900 echo 2 >subdir/subfile2 &&
901 git add subdir/subfile1 &&
902 git commit -m subdir &&
904 cd subdir &&
905 echo x >subfile1 &&
906 echo x >../file &&
907 git status >../expect &&
908 git stash &&
909 sane_unset GIT_MERGE_VERBOSITY &&
910 git stash apply
912 sed -e 1d >actual && # drop "Saved..."
913 test_cmp expect actual
916 cat >expect <<EOF
917 diff --git a/HEAD b/HEAD
918 new file mode 100644
919 index 0000000..fe0cbee
920 --- /dev/null
921 +++ b/HEAD
922 @@ -0,0 +1 @@
923 +file-not-a-ref
926 test_expect_success 'stash where working directory contains "HEAD" file' '
927 git stash clear &&
928 git reset --hard &&
929 echo file-not-a-ref >HEAD &&
930 git add HEAD &&
931 test_tick &&
932 git stash &&
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' '
949 git stash clear &&
950 git reset --hard &&
951 echo quux >bazzy &&
952 git add bazzy &&
953 STASH_ID=$(git stash create) &&
954 git reset --hard &&
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 &&
959 git stash pop &&
960 grep quux bazzy
963 test_expect_success 'handle stash specification with spaces' '
964 git stash clear &&
965 echo pig >file &&
966 git stash &&
967 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
968 test_tick &&
969 echo cow >file &&
970 git stash &&
971 git stash apply "stash@{$stamp}" &&
972 grep pig file
975 test_expect_success 'setup stash with index and worktree changes' '
976 git stash clear &&
977 git reset --hard &&
978 echo index >file &&
979 git add file &&
980 echo working >file &&
981 git stash
984 test_expect_success 'stash list -p shows simple diff' '
985 cat >expect <<-EOF &&
986 stash@{0}
988 diff --git a/file b/file
989 index 257cc56..d26b33d 100644
990 --- a/file
991 +++ b/file
992 @@ -1 +1 @@
993 -foo
994 +working
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 &&
1002 stash@{0}
1004 diff --cc file
1005 index 257cc56,9015a7a..d26b33d
1006 --- a/file
1007 +++ b/file
1008 @@@ -1,1 -1,1 +1,1 @@@
1009 - foo
1010 -index
1011 ++working
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' '
1018 mv file renamed &&
1019 git add renamed &&
1020 git stash &&
1021 git stash apply &&
1022 test_path_is_file renamed &&
1023 test_path_is_missing file
1026 test_expect_success 'push -m shows right message' '
1027 >foo &&
1028 git add foo &&
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' '
1036 >foo &&
1037 git add foo &&
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' '
1045 git stash clear &&
1046 git reset --hard &&
1047 echo quux >bazzy &&
1048 git add bazzy &&
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' '
1057 git stash clear &&
1058 git reset --hard &&
1059 echo quux >bazzy &&
1060 git add bazzy &&
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' '
1069 git stash clear &&
1070 git reset --hard &&
1071 echo quux >bazzy &&
1072 git add bazzy &&
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' '
1081 git stash clear &&
1082 git reset --hard &&
1083 echo quux >bazzy &&
1084 git add bazzy &&
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' '
1093 >foo &&
1094 git add foo &&
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' '
1102 >foo &&
1103 git add foo &&
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' '
1111 >foo &&
1112 git add foo &&
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' '
1120 >foo &&
1121 git add foo &&
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' '
1129 >foo &&
1130 git add foo &&
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 &&
1140 >foo &&
1141 git add foo &&
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' '
1149 >foo &&
1150 git add foo &&
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 &&
1160 >foo &&
1161 git add foo &&
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' '
1170 >foo &&
1171 >bar &&
1172 git add foo bar &&
1173 git stash push -- foo &&
1174 test_path_is_file bar &&
1175 test_path_is_missing foo &&
1176 git stash pop &&
1177 test_path_is_file foo &&
1178 test_path_is_file bar
1181 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1182 mkdir sub &&
1183 >foo &&
1184 >bar &&
1185 git add foo bar &&
1187 cd sub &&
1188 git stash push -- ../foo
1189 ) &&
1190 test_path_is_file bar &&
1191 test_path_is_missing foo &&
1192 git stash pop &&
1193 test_path_is_file foo &&
1194 test_path_is_file bar
1197 test_expect_success 'stash with multiple pathspec arguments' '
1198 >foo &&
1199 >bar &&
1200 >extra &&
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 &&
1206 git stash pop &&
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' '
1213 >"foo bar" &&
1214 >foo &&
1215 >bar &&
1216 git add foo* &&
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 &&
1221 git stash pop &&
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 &&
1237 git stash pop &&
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' '
1244 >foo &&
1245 git add foo &&
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' '
1254 >foo &&
1255 git add foo &&
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' '
1264 >untracked &&
1265 test_must_fail git stash push untracked &&
1266 test_path_is_file untracked
1269 test_expect_success 'push: -q is quiet with changes' '
1270 >foo &&
1271 git add foo &&
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' '
1282 git init foo_dir &&
1283 test_when_finished rm -rf foo_dir &&
1285 cd foo_dir &&
1286 >bar &&
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' '
1293 >file &&
1294 git add file &&
1295 >untracked &&
1296 git stash push file &&
1297 test_path_is_file untracked
1300 test_expect_success 'stash without verb with pathspec' '
1301 >"foo bar" &&
1302 >foo &&
1303 >bar &&
1304 git add foo* &&
1305 git stash -- "foo b*" &&
1306 test_path_is_missing "foo bar" &&
1307 test_path_is_file foo &&
1308 test_path_is_file bar &&
1309 git stash pop &&
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' '
1316 git reset &&
1317 >foo &&
1318 >bar &&
1319 git add foo bar &&
1320 git commit -m "test" &&
1321 echo "foo" >foo &&
1322 echo "bar" >bar &&
1323 git stash -k -- foo &&
1324 test "",bar = $(cat foo),$(cat bar) &&
1325 git stash pop &&
1326 test foo,bar = $(cat foo),$(cat bar)
1329 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1330 git reset &&
1331 >subdir/untracked &&
1332 >subdir/tracked1 &&
1333 >subdir/tracked2 &&
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 &&
1339 git stash pop &&
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' '
1346 git reset &&
1347 >subdir/untracked &&
1348 >subdir/tracked &&
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 &&
1355 git stash pop &&
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" &&
1364 git stash
1367 test_expect_success 'stash works when user.name and user.email are not set' '
1368 git reset &&
1369 >1 &&
1370 git add 1 &&
1371 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1372 git stash &&
1373 git show -s --format="%an <%ae>" refs/stash >actual &&
1374 test_cmp expect actual &&
1375 >2 &&
1376 git add 2 &&
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 &&
1387 >2 &&
1388 git stash &&
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 &&
1396 git rm 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" &&
1403 git init empty &&
1405 cd 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 &&
1410 git stash pop &&
1411 echo content >expect &&
1412 test_cmp expect file
1416 test_expect_success 'stash apply should succeed with unmodified file' '
1417 echo base >file &&
1418 git add file &&
1419 git commit -m base &&
1421 # now stash a modification
1422 echo modified >file &&
1423 git stash &&
1425 # make the file stat dirty
1426 cp file other &&
1427 mv other file &&
1429 git stash apply
1432 test_expect_success 'stash handles skip-worktree entries nicely' '
1433 test_commit A &&
1434 echo changed >A.t &&
1435 git add A.t &&
1436 git update-index --skip-worktree A.t &&
1437 rm A.t &&
1438 git stash &&
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 &&
1466 test_commit init &&
1468 test_write_lines this file has some words >filler &&
1469 git add filler &&
1470 git commit -m filler &&
1472 git rm filler &&
1473 mkdir filler &&
1474 echo contents >filler/file &&
1475 git stash push
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 &&
1483 test_commit init &&
1485 test_write_lines this file has some words >filler &&
1486 git add filler &&
1487 git commit -m filler &&
1489 git rm filler &&
1490 mkdir 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 &&
1503 test_commit init &&
1505 mkdir filler &&
1506 test_write_lines some words >filler/file1 &&
1507 test_write_lines and stuff >filler/file2 &&
1508 git add filler &&
1509 git commit -m filler &&
1511 git rm -rf filler &&
1512 echo contents >filler &&
1513 cp filler expect &&
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 &&
1525 echo hi >a &&
1526 echo there >b &&
1527 git add . &&
1528 git commit -m first &&
1529 echo hello >a &&
1530 echo something >c &&
1532 git stash push --include-untracked &&
1534 echo conflict >a &&
1535 git add a &&
1536 git commit -m second &&
1538 test_must_fail git stash pop &&
1540 test_path_is_file c
1544 test_expect_success 'stash create reports a locked index' '
1545 test_when_finished "rm -rf repo" &&
1546 git init repo &&
1548 cd 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 &&
1557 test_cmp expect err
1561 test_expect_success 'stash push reports a locked index' '
1562 test_when_finished "rm -rf repo" &&
1563 git init repo &&
1565 cd 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 &&
1574 test_cmp expect err
1578 test_expect_success 'stash apply reports a locked index' '
1579 test_when_finished "rm -rf repo" &&
1580 git init repo &&
1582 cd repo &&
1583 test_commit A A.file &&
1584 echo change >A.file &&
1585 git stash push &&
1586 touch .git/index.lock &&
1588 cat >expect <<-EOF &&
1589 error: could not write index
1591 test_must_fail git stash apply 2>err &&
1592 test_cmp expect err
1596 test_done