rebase: use correct base for --keep-base when a branch is given
[alt-git.git] / t / t3903-stash.sh
blob4abbc8fccae78c69358970644320a3a114793083
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
13 test_expect_success 'usage on cmd and subcommand invalid option' '
14 test_expect_code 129 git stash --invalid-option 2>usage &&
15 grep "or: git stash" usage &&
17 test_expect_code 129 git stash push --invalid-option 2>usage &&
18 ! grep "or: git stash" usage
21 test_expect_success 'usage on main command -h emits a summary of subcommands' '
22 test_expect_code 129 git stash -h >usage &&
23 grep -F "usage: git stash list" usage &&
24 grep -F "or: git stash show" usage
27 test_expect_failure 'usage for subcommands should emit subcommand usage' '
28 test_expect_code 129 git stash push -h >usage &&
29 grep -F "usage: git stash [push" usage
32 diff_cmp () {
33 for i in "$1" "$2"
35 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
36 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
37 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
38 "$i" >"$i.compare" || return 1
39 done &&
40 test_cmp "$1.compare" "$2.compare" &&
41 rm -f "$1.compare" "$2.compare"
44 setup_stash() {
45 echo 1 >file &&
46 git add file &&
47 echo unrelated >other-file &&
48 git add other-file &&
49 test_tick &&
50 git commit -m initial &&
51 echo 2 >file &&
52 git add file &&
53 echo 3 >file &&
54 test_tick &&
55 git stash &&
56 git diff-files --quiet &&
57 git diff-index --cached --quiet HEAD
60 test_expect_success 'stash some dirty working directory' '
61 setup_stash
64 cat >expect <<EOF
65 diff --git a/file b/file
66 index 0cfbf08..00750ed 100644
67 --- a/file
68 +++ b/file
69 @@ -1 +1 @@
72 EOF
74 test_expect_success 'parents of stash' '
75 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
76 git diff stash^2..stash >output &&
77 diff_cmp expect output
80 test_expect_success 'applying bogus stash does nothing' '
81 test_must_fail git stash apply stash@{1} &&
82 echo 1 >expect &&
83 test_cmp expect file
86 test_expect_success 'apply does not need clean working directory' '
87 echo 4 >other-file &&
88 git stash apply &&
89 echo 3 >expect &&
90 test_cmp expect file
93 test_expect_success 'apply does not clobber working directory changes' '
94 git reset --hard &&
95 echo 4 >file &&
96 test_must_fail git stash apply &&
97 echo 4 >expect &&
98 test_cmp expect file
101 test_expect_success 'apply stashed changes' '
102 git reset --hard &&
103 echo 5 >other-file &&
104 git add other-file &&
105 test_tick &&
106 git commit -m other-file &&
107 git stash apply &&
108 test 3 = $(cat file) &&
109 test 1 = $(git show :file) &&
110 test 1 = $(git show HEAD:file)
113 test_expect_success 'apply stashed changes (including index)' '
114 git reset --hard HEAD^ &&
115 echo 6 >other-file &&
116 git add other-file &&
117 test_tick &&
118 git commit -m other-file &&
119 git stash apply --index &&
120 test 3 = $(cat file) &&
121 test 2 = $(git show :file) &&
122 test 1 = $(git show HEAD:file)
125 test_expect_success 'unstashing in a subdirectory' '
126 git reset --hard HEAD &&
127 mkdir subdir &&
129 cd subdir &&
130 git stash apply
134 test_expect_success 'stash drop complains of extra options' '
135 test_must_fail git stash drop --foo
138 test_expect_success 'drop top stash' '
139 git reset --hard &&
140 git stash list >expected &&
141 echo 7 >file &&
142 git stash &&
143 git stash drop &&
144 git stash list >actual &&
145 test_cmp expected actual &&
146 git stash apply &&
147 test 3 = $(cat file) &&
148 test 1 = $(git show :file) &&
149 test 1 = $(git show HEAD:file)
152 test_expect_success 'drop middle stash' '
153 git reset --hard &&
154 echo 8 >file &&
155 git stash &&
156 echo 9 >file &&
157 git stash &&
158 git stash drop stash@{1} &&
159 test 2 = $(git stash list | wc -l) &&
160 git stash apply &&
161 test 9 = $(cat file) &&
162 test 1 = $(git show :file) &&
163 test 1 = $(git show HEAD:file) &&
164 git reset --hard &&
165 git stash drop &&
166 git stash apply &&
167 test 3 = $(cat file) &&
168 test 1 = $(git show :file) &&
169 test 1 = $(git show HEAD:file)
172 test_expect_success 'drop middle stash by index' '
173 git reset --hard &&
174 echo 8 >file &&
175 git stash &&
176 echo 9 >file &&
177 git stash &&
178 git stash drop 1 &&
179 test 2 = $(git stash list | wc -l) &&
180 git stash apply &&
181 test 9 = $(cat file) &&
182 test 1 = $(git show :file) &&
183 test 1 = $(git show HEAD:file) &&
184 git reset --hard &&
185 git stash drop &&
186 git stash apply &&
187 test 3 = $(cat file) &&
188 test 1 = $(git show :file) &&
189 test 1 = $(git show HEAD:file)
192 test_expect_success 'drop stash reflog updates refs/stash' '
193 git reset --hard &&
194 git rev-parse refs/stash >expect &&
195 echo 9 >file &&
196 git stash &&
197 git stash drop stash@{0} &&
198 git rev-parse refs/stash >actual &&
199 test_cmp expect actual
202 test_expect_success REFFILES 'drop stash reflog updates refs/stash with rewrite' '
203 git init repo &&
205 cd repo &&
206 setup_stash
207 ) &&
208 echo 9 >repo/file &&
210 old_oid="$(git -C repo rev-parse stash@{0})" &&
211 git -C repo stash &&
212 new_oid="$(git -C repo rev-parse stash@{0})" &&
214 cat >expect <<-EOF &&
215 $(test_oid zero) $old_oid
216 $old_oid $new_oid
218 cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
219 test_cmp expect actual &&
221 git -C repo stash drop stash@{1} &&
222 cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
223 cat >expect <<-EOF &&
224 $(test_oid zero) $new_oid
226 test_cmp expect actual
229 test_expect_success 'stash pop' '
230 git reset --hard &&
231 git stash pop &&
232 test 3 = $(cat file) &&
233 test 1 = $(git show :file) &&
234 test 1 = $(git show HEAD:file) &&
235 test 0 = $(git stash list | wc -l)
238 cat >expect <<EOF
239 diff --git a/file2 b/file2
240 new file mode 100644
241 index 0000000..1fe912c
242 --- /dev/null
243 +++ b/file2
244 @@ -0,0 +1 @@
245 +bar2
248 cat >expect1 <<EOF
249 diff --git a/file b/file
250 index 257cc56..5716ca5 100644
251 --- a/file
252 +++ b/file
253 @@ -1 +1 @@
254 -foo
255 +bar
258 cat >expect2 <<EOF
259 diff --git a/file b/file
260 index 7601807..5716ca5 100644
261 --- a/file
262 +++ b/file
263 @@ -1 +1 @@
264 -baz
265 +bar
266 diff --git a/file2 b/file2
267 new file mode 100644
268 index 0000000..1fe912c
269 --- /dev/null
270 +++ b/file2
271 @@ -0,0 +1 @@
272 +bar2
275 test_expect_success 'stash branch' '
276 echo foo >file &&
277 git commit file -m first &&
278 echo bar >file &&
279 echo bar2 >file2 &&
280 git add file2 &&
281 git stash &&
282 echo baz >file &&
283 git commit file -m second &&
284 git stash branch stashbranch &&
285 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
286 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
287 git diff --cached >output &&
288 diff_cmp expect output &&
289 git diff >output &&
290 diff_cmp expect1 output &&
291 git add file &&
292 git commit -m alternate\ second &&
293 git diff main..stashbranch >output &&
294 diff_cmp output expect2 &&
295 test 0 = $(git stash list | wc -l)
298 test_expect_success 'apply -q is quiet' '
299 echo foo >file &&
300 git stash &&
301 git stash apply -q >output.out 2>&1 &&
302 test_must_be_empty output.out
305 test_expect_success 'apply --index -q is quiet' '
306 # Added file, deleted file, modified file all staged for commit
307 echo foo >new-file &&
308 echo test >file &&
309 git add new-file file &&
310 git rm other-file &&
312 git stash &&
313 git stash apply --index -q >output.out 2>&1 &&
314 test_must_be_empty output.out
317 test_expect_success 'save -q is quiet' '
318 git stash save --quiet >output.out 2>&1 &&
319 test_must_be_empty output.out
322 test_expect_success 'pop -q works and is quiet' '
323 git stash pop -q >output.out 2>&1 &&
324 echo bar >expect &&
325 git show :file >actual &&
326 test_cmp expect actual &&
327 test_must_be_empty output.out
330 test_expect_success 'pop -q --index works and is quiet' '
331 echo foo >file &&
332 git add file &&
333 git stash save --quiet &&
334 git stash pop -q --index >output.out 2>&1 &&
335 git diff-files file2 >file2.diff &&
336 test_must_be_empty file2.diff &&
337 test foo = "$(git show :file)" &&
338 test_must_be_empty output.out
341 test_expect_success 'drop -q is quiet' '
342 git stash &&
343 git stash drop -q >output.out 2>&1 &&
344 test_must_be_empty output.out
347 test_expect_success 'stash push -q --staged refreshes the index' '
348 git reset --hard &&
349 echo test >file &&
350 git add file &&
351 git stash push -q --staged &&
352 git diff-files >output.out &&
353 test_must_be_empty output.out
356 test_expect_success 'stash apply -q --index refreshes the index' '
357 echo test >other-file &&
358 git add other-file &&
359 echo another-change >other-file &&
360 git diff-files >expect &&
361 git stash &&
363 git stash apply -q --index &&
364 git diff-files >actual &&
365 test_cmp expect actual
368 test_expect_success 'stash -k' '
369 echo bar3 >file &&
370 echo bar4 >file2 &&
371 git add file2 &&
372 git stash -k &&
373 test bar,bar4 = $(cat file),$(cat file2)
376 test_expect_success 'stash --no-keep-index' '
377 echo bar33 >file &&
378 echo bar44 >file2 &&
379 git add file2 &&
380 git stash --no-keep-index &&
381 test bar,bar2 = $(cat file),$(cat file2)
384 test_expect_success 'stash --staged' '
385 echo bar3 >file &&
386 echo bar4 >file2 &&
387 git add file2 &&
388 git stash --staged &&
389 test bar3,bar2 = $(cat file),$(cat file2) &&
390 git reset --hard &&
391 git stash pop &&
392 test bar,bar4 = $(cat file),$(cat file2)
395 test_expect_success 'dont assume push with non-option args' '
396 test_must_fail git stash -q drop 2>err &&
397 test_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
400 test_expect_success 'stash --invalid-option' '
401 echo bar5 >file &&
402 echo bar6 >file2 &&
403 git add file2 &&
404 test_must_fail git stash --invalid-option &&
405 test_must_fail git stash save --invalid-option &&
406 test bar5,bar6 = $(cat file),$(cat file2)
409 test_expect_success 'stash an added file' '
410 git reset --hard &&
411 echo new >file3 &&
412 git add file3 &&
413 git stash save "added file" &&
414 ! test -r file3 &&
415 git stash apply &&
416 test new = "$(cat file3)"
419 test_expect_success 'stash --intent-to-add file' '
420 git reset --hard &&
421 echo new >file4 &&
422 git add --intent-to-add file4 &&
423 test_when_finished "git rm -f file4" &&
424 test_must_fail git stash
427 test_expect_success 'stash rm then recreate' '
428 git reset --hard &&
429 git rm file &&
430 echo bar7 >file &&
431 git stash save "rm then recreate" &&
432 test bar = "$(cat file)" &&
433 git stash apply &&
434 test bar7 = "$(cat file)"
437 test_expect_success 'stash rm and ignore' '
438 git reset --hard &&
439 git rm file &&
440 echo file >.gitignore &&
441 git stash save "rm and ignore" &&
442 test bar = "$(cat file)" &&
443 test file = "$(cat .gitignore)" &&
444 git stash apply &&
445 ! test -r file &&
446 test file = "$(cat .gitignore)"
449 test_expect_success 'stash rm and ignore (stage .gitignore)' '
450 git reset --hard &&
451 git rm file &&
452 echo file >.gitignore &&
453 git add .gitignore &&
454 git stash save "rm and ignore (stage .gitignore)" &&
455 test bar = "$(cat file)" &&
456 ! test -r .gitignore &&
457 git stash apply &&
458 ! test -r file &&
459 test file = "$(cat .gitignore)"
462 test_expect_success SYMLINKS 'stash file to symlink' '
463 git reset --hard &&
464 rm file &&
465 ln -s file2 file &&
466 git stash save "file to symlink" &&
467 test_path_is_file_not_symlink file &&
468 test bar = "$(cat file)" &&
469 git stash apply &&
470 test_path_is_symlink file &&
471 test "$(test_readlink file)" = file2
474 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
475 git reset --hard &&
476 git rm file &&
477 ln -s file2 file &&
478 git stash save "file to symlink (stage rm)" &&
479 test_path_is_file_not_symlink file &&
480 test bar = "$(cat file)" &&
481 git stash apply &&
482 test_path_is_symlink file &&
483 test "$(test_readlink file)" = file2
486 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
487 git reset --hard &&
488 rm file &&
489 ln -s file2 file &&
490 git add file &&
491 git stash save "file to symlink (full stage)" &&
492 test_path_is_file_not_symlink file &&
493 test bar = "$(cat file)" &&
494 git stash apply &&
495 test_path_is_symlink file &&
496 test "$(test_readlink file)" = file2
499 # This test creates a commit with a symlink used for the following tests
501 test_expect_success 'stash symlink to file' '
502 git reset --hard &&
503 test_ln_s_add file filelink &&
504 git commit -m "Add symlink" &&
505 rm filelink &&
506 cp file filelink &&
507 git stash save "symlink to file"
510 test_expect_success SYMLINKS 'this must have re-created the symlink' '
511 test -h filelink &&
512 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
515 test_expect_success 'unstash must re-create the file' '
516 git stash apply &&
517 ! test -h filelink &&
518 test bar = "$(cat file)"
521 test_expect_success 'stash symlink to file (stage rm)' '
522 git reset --hard &&
523 git rm filelink &&
524 cp file filelink &&
525 git stash save "symlink to file (stage rm)"
528 test_expect_success SYMLINKS 'this must have re-created the symlink' '
529 test -h filelink &&
530 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
533 test_expect_success 'unstash must re-create the file' '
534 git stash apply &&
535 ! test -h filelink &&
536 test bar = "$(cat file)"
539 test_expect_success 'stash symlink to file (full stage)' '
540 git reset --hard &&
541 rm filelink &&
542 cp file filelink &&
543 git add filelink &&
544 git stash save "symlink to file (full stage)"
547 test_expect_success SYMLINKS 'this must have re-created the symlink' '
548 test -h filelink &&
549 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
552 test_expect_success 'unstash must re-create the file' '
553 git stash apply &&
554 ! test -h filelink &&
555 test bar = "$(cat file)"
558 test_expect_failure 'stash directory to file' '
559 git reset --hard &&
560 mkdir dir &&
561 echo foo >dir/file &&
562 git add dir/file &&
563 git commit -m "Add file in dir" &&
564 rm -fr dir &&
565 echo bar >dir &&
566 git stash save "directory to file" &&
567 test_path_is_dir dir &&
568 test foo = "$(cat dir/file)" &&
569 test_must_fail git stash apply &&
570 test bar = "$(cat dir)" &&
571 git reset --soft HEAD^
574 test_expect_failure 'stash file to directory' '
575 git reset --hard &&
576 rm file &&
577 mkdir file &&
578 echo foo >file/file &&
579 git stash save "file to directory" &&
580 test_path_is_file file &&
581 test bar = "$(cat file)" &&
582 git stash apply &&
583 test_path_is_file file/file &&
584 test foo = "$(cat file/file)"
587 test_expect_success 'giving too many ref arguments does not modify files' '
588 git stash clear &&
589 test_when_finished "git reset --hard HEAD" &&
590 echo foo >file2 &&
591 git stash &&
592 echo bar >file2 &&
593 git stash &&
594 test-tool chmtime =123456789 file2 &&
595 for type in apply pop "branch stash-branch"
597 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
598 test_i18ngrep "Too many revisions" err &&
599 test 123456789 = $(test-tool chmtime -g file2) || return 1
600 done
603 test_expect_success 'drop: too many arguments errors out (does nothing)' '
604 git stash list >expect &&
605 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
606 test_i18ngrep "Too many revisions" err &&
607 git stash list >actual &&
608 test_cmp expect actual
611 test_expect_success 'show: too many arguments errors out (does nothing)' '
612 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
613 test_i18ngrep "Too many revisions" err &&
614 test_must_be_empty out
617 test_expect_success 'stash create - no changes' '
618 git stash clear &&
619 test_when_finished "git reset --hard HEAD" &&
620 git reset --hard &&
621 git stash create >actual &&
622 test_must_be_empty actual
625 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
626 git stash clear &&
627 test_when_finished "git reset --hard HEAD" &&
628 git reset --hard &&
629 echo foo >>file &&
630 STASH_ID=$(git stash create) &&
631 git reset --hard &&
632 git stash branch stash-branch ${STASH_ID} &&
633 test_when_finished "git reset --hard HEAD && git checkout main &&
634 git branch -D stash-branch" &&
635 test $(git ls-files --modified | wc -l) -eq 1
638 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
639 git stash clear &&
640 test_when_finished "git reset --hard HEAD" &&
641 git reset --hard &&
642 echo foo >>file &&
643 git stash &&
644 test_when_finished "git stash drop" &&
645 echo bar >>file &&
646 STASH_ID=$(git stash create) &&
647 git reset --hard &&
648 git stash branch stash-branch ${STASH_ID} &&
649 test_when_finished "git reset --hard HEAD && git checkout main &&
650 git branch -D stash-branch" &&
651 test $(git ls-files --modified | wc -l) -eq 1
654 test_expect_success 'stash branch complains with no arguments' '
655 test_must_fail git stash branch 2>err &&
656 test_i18ngrep "No branch name specified" err
659 test_expect_success 'stash show format defaults to --stat' '
660 git stash clear &&
661 test_when_finished "git reset --hard HEAD" &&
662 git reset --hard &&
663 echo foo >>file &&
664 git stash &&
665 test_when_finished "git stash drop" &&
666 echo bar >>file &&
667 STASH_ID=$(git stash create) &&
668 git reset --hard &&
669 cat >expected <<-EOF &&
670 file | 1 +
671 1 file changed, 1 insertion(+)
673 git stash show ${STASH_ID} >actual &&
674 test_cmp expected actual
677 test_expect_success 'stash show - stashes on stack, stash-like argument' '
678 git stash clear &&
679 test_when_finished "git reset --hard HEAD" &&
680 git reset --hard &&
681 echo foo >>file &&
682 git stash &&
683 test_when_finished "git stash drop" &&
684 echo bar >>file &&
685 STASH_ID=$(git stash create) &&
686 git reset --hard &&
687 echo "1 0 file" >expected &&
688 git stash show --numstat ${STASH_ID} >actual &&
689 test_cmp expected actual
692 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
693 git stash clear &&
694 test_when_finished "git reset --hard HEAD" &&
695 git reset --hard &&
696 echo foo >>file &&
697 git stash &&
698 test_when_finished "git stash drop" &&
699 echo bar >>file &&
700 STASH_ID=$(git stash create) &&
701 git reset --hard &&
702 cat >expected <<-EOF &&
703 diff --git a/file b/file
704 index 7601807..935fbd3 100644
705 --- a/file
706 +++ b/file
707 @@ -1 +1,2 @@
709 +bar
711 git stash show -p ${STASH_ID} >actual &&
712 diff_cmp expected actual
715 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
716 git stash clear &&
717 test_when_finished "git reset --hard HEAD" &&
718 git reset --hard &&
719 echo foo >>file &&
720 STASH_ID=$(git stash create) &&
721 git reset --hard &&
722 echo "1 0 file" >expected &&
723 git stash show --numstat ${STASH_ID} >actual &&
724 test_cmp expected actual
727 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
728 git stash clear &&
729 test_when_finished "git reset --hard HEAD" &&
730 git reset --hard &&
731 echo foo >>file &&
732 STASH_ID=$(git stash create) &&
733 git reset --hard &&
734 cat >expected <<-EOF &&
735 diff --git a/file b/file
736 index 7601807..71b52c4 100644
737 --- a/file
738 +++ b/file
739 @@ -1 +1,2 @@
741 +foo
743 git stash show -p ${STASH_ID} >actual &&
744 diff_cmp expected actual
747 test_expect_success 'stash show --patience shows diff' '
748 git reset --hard &&
749 echo foo >>file &&
750 STASH_ID=$(git stash create) &&
751 git reset --hard &&
752 cat >expected <<-EOF &&
753 diff --git a/file b/file
754 index 7601807..71b52c4 100644
755 --- a/file
756 +++ b/file
757 @@ -1 +1,2 @@
759 +foo
761 git stash show --patience ${STASH_ID} >actual &&
762 diff_cmp expected actual
765 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
766 git stash clear &&
767 test_when_finished "git reset --hard HEAD && git stash clear" &&
768 git reset --hard &&
769 echo foo >file &&
770 git stash &&
771 echo bar >file &&
772 git stash &&
773 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
774 git stash pop &&
775 test bar = "$(cat file)" &&
776 git reset --hard HEAD
779 test_expect_success 'pop: fail early if specified stash is not a stash ref' '
780 git stash clear &&
781 test_when_finished "git reset --hard HEAD && git stash clear" &&
782 git reset --hard &&
783 echo foo >file &&
784 git stash &&
785 echo bar >file &&
786 git stash &&
787 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
788 git stash pop &&
789 test bar = "$(cat file)" &&
790 git reset --hard HEAD
793 test_expect_success 'ref with non-existent reflog' '
794 git stash clear &&
795 echo bar5 >file &&
796 echo bar6 >file2 &&
797 git add file2 &&
798 git stash &&
799 test_must_fail git rev-parse --quiet --verify does-not-exist &&
800 test_must_fail git stash drop does-not-exist &&
801 test_must_fail git stash drop does-not-exist@{0} &&
802 test_must_fail git stash pop does-not-exist &&
803 test_must_fail git stash pop does-not-exist@{0} &&
804 test_must_fail git stash apply does-not-exist &&
805 test_must_fail git stash apply does-not-exist@{0} &&
806 test_must_fail git stash show does-not-exist &&
807 test_must_fail git stash show does-not-exist@{0} &&
808 test_must_fail git stash branch tmp does-not-exist &&
809 test_must_fail git stash branch tmp does-not-exist@{0} &&
810 git stash drop
813 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
814 git stash clear &&
815 test_must_fail git stash drop stash@{0} &&
816 echo bar5 >file &&
817 echo bar6 >file2 &&
818 git add file2 &&
819 git stash &&
820 test_must_fail git stash drop stash@{1} &&
821 test_must_fail git stash pop stash@{1} &&
822 test_must_fail git stash apply stash@{1} &&
823 test_must_fail git stash show stash@{1} &&
824 test_must_fail git stash branch tmp stash@{1} &&
825 git stash drop
828 test_expect_success 'invalid ref of the form "n", n >= N' '
829 git stash clear &&
830 test_must_fail git stash drop 0 &&
831 echo bar5 >file &&
832 echo bar6 >file2 &&
833 git add file2 &&
834 git stash &&
835 test_must_fail git stash drop 1 &&
836 test_must_fail git stash pop 1 &&
837 test_must_fail git stash apply 1 &&
838 test_must_fail git stash show 1 &&
839 test_must_fail git stash branch tmp 1 &&
840 git stash drop
843 test_expect_success 'valid ref of the form "n", n < N' '
844 git stash clear &&
845 echo bar5 >file &&
846 echo bar6 >file2 &&
847 git add file2 &&
848 git stash &&
849 git stash show 0 &&
850 git stash branch tmp 0 &&
851 git checkout main &&
852 git stash &&
853 git stash apply 0 &&
854 git reset --hard &&
855 git stash pop 0 &&
856 git stash &&
857 git stash drop 0 &&
858 test_must_fail git stash drop
861 test_expect_success 'branch: do not drop the stash if the branch exists' '
862 git stash clear &&
863 echo foo >file &&
864 git add file &&
865 git commit -m initial &&
866 echo bar >file &&
867 git stash &&
868 test_must_fail git stash branch main stash@{0} &&
869 git rev-parse stash@{0} --
872 test_expect_success 'branch: should not drop the stash if the apply fails' '
873 git stash clear &&
874 git reset HEAD~1 --hard &&
875 echo foo >file &&
876 git add file &&
877 git commit -m initial &&
878 echo bar >file &&
879 git stash &&
880 echo baz >file &&
881 test_when_finished "git checkout main" &&
882 test_must_fail git stash branch new_branch stash@{0} &&
883 git rev-parse stash@{0} --
886 test_expect_success 'apply: show same status as git status (relative to ./)' '
887 git stash clear &&
888 echo 1 >subdir/subfile1 &&
889 echo 2 >subdir/subfile2 &&
890 git add subdir/subfile1 &&
891 git commit -m subdir &&
893 cd subdir &&
894 echo x >subfile1 &&
895 echo x >../file &&
896 git status >../expect &&
897 git stash &&
898 sane_unset GIT_MERGE_VERBOSITY &&
899 git stash apply
901 sed -e 1d >actual && # drop "Saved..."
902 test_cmp expect actual
905 cat >expect <<EOF
906 diff --git a/HEAD b/HEAD
907 new file mode 100644
908 index 0000000..fe0cbee
909 --- /dev/null
910 +++ b/HEAD
911 @@ -0,0 +1 @@
912 +file-not-a-ref
915 test_expect_success 'stash where working directory contains "HEAD" file' '
916 git stash clear &&
917 git reset --hard &&
918 echo file-not-a-ref >HEAD &&
919 git add HEAD &&
920 test_tick &&
921 git stash &&
922 git diff-files --quiet &&
923 git diff-index --cached --quiet HEAD &&
924 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
925 git diff stash^..stash >output &&
926 diff_cmp expect output
929 test_expect_success 'store called with invalid commit' '
930 test_must_fail git stash store foo
933 test_expect_success 'store updates stash ref and reflog' '
934 git stash clear &&
935 git reset --hard &&
936 echo quux >bazzy &&
937 git add bazzy &&
938 STASH_ID=$(git stash create) &&
939 git reset --hard &&
940 test_path_is_missing bazzy &&
941 git stash store -m quuxery $STASH_ID &&
942 test $(git rev-parse stash) = $STASH_ID &&
943 git reflog --format=%H stash| grep $STASH_ID &&
944 git stash pop &&
945 grep quux bazzy
948 test_expect_success 'handle stash specification with spaces' '
949 git stash clear &&
950 echo pig >file &&
951 git stash &&
952 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
953 test_tick &&
954 echo cow >file &&
955 git stash &&
956 git stash apply "stash@{$stamp}" &&
957 grep pig file
960 test_expect_success 'setup stash with index and worktree changes' '
961 git stash clear &&
962 git reset --hard &&
963 echo index >file &&
964 git add file &&
965 echo working >file &&
966 git stash
969 test_expect_success 'stash list -p shows simple diff' '
970 cat >expect <<-EOF &&
971 stash@{0}
973 diff --git a/file b/file
974 index 257cc56..d26b33d 100644
975 --- a/file
976 +++ b/file
977 @@ -1 +1 @@
978 -foo
979 +working
981 git stash list --format=%gd -p >actual &&
982 diff_cmp expect actual
985 test_expect_success 'stash list --cc shows combined diff' '
986 cat >expect <<-\EOF &&
987 stash@{0}
989 diff --cc file
990 index 257cc56,9015a7a..d26b33d
991 --- a/file
992 +++ b/file
993 @@@ -1,1 -1,1 +1,1 @@@
994 - foo
995 -index
996 ++working
998 git stash list --format=%gd -p --cc >actual &&
999 diff_cmp expect actual
1002 test_expect_success 'stash is not confused by partial renames' '
1003 mv file renamed &&
1004 git add renamed &&
1005 git stash &&
1006 git stash apply &&
1007 test_path_is_file renamed &&
1008 test_path_is_missing file
1011 test_expect_success 'push -m shows right message' '
1012 >foo &&
1013 git add foo &&
1014 git stash push -m "test message" &&
1015 echo "stash@{0}: On main: test message" >expect &&
1016 git stash list -1 >actual &&
1017 test_cmp expect actual
1020 test_expect_success 'push -m also works without space' '
1021 >foo &&
1022 git add foo &&
1023 git stash push -m"unspaced test message" &&
1024 echo "stash@{0}: On main: unspaced test message" >expect &&
1025 git stash list -1 >actual &&
1026 test_cmp expect actual
1029 test_expect_success 'store -m foo shows right message' '
1030 git stash clear &&
1031 git reset --hard &&
1032 echo quux >bazzy &&
1033 git add bazzy &&
1034 STASH_ID=$(git stash create) &&
1035 git stash store -m "store m" $STASH_ID &&
1036 echo "stash@{0}: store m" >expect &&
1037 git stash list -1 >actual &&
1038 test_cmp expect actual
1041 test_expect_success 'store -mfoo shows right message' '
1042 git stash clear &&
1043 git reset --hard &&
1044 echo quux >bazzy &&
1045 git add bazzy &&
1046 STASH_ID=$(git stash create) &&
1047 git stash store -m"store mfoo" $STASH_ID &&
1048 echo "stash@{0}: store mfoo" >expect &&
1049 git stash list -1 >actual &&
1050 test_cmp expect actual
1053 test_expect_success 'store --message=foo shows right message' '
1054 git stash clear &&
1055 git reset --hard &&
1056 echo quux >bazzy &&
1057 git add bazzy &&
1058 STASH_ID=$(git stash create) &&
1059 git stash store --message="store message=foo" $STASH_ID &&
1060 echo "stash@{0}: store message=foo" >expect &&
1061 git stash list -1 >actual &&
1062 test_cmp expect actual
1065 test_expect_success 'store --message foo shows right message' '
1066 git stash clear &&
1067 git reset --hard &&
1068 echo quux >bazzy &&
1069 git add bazzy &&
1070 STASH_ID=$(git stash create) &&
1071 git stash store --message "store message foo" $STASH_ID &&
1072 echo "stash@{0}: store message foo" >expect &&
1073 git stash list -1 >actual &&
1074 test_cmp expect actual
1077 test_expect_success 'push -mfoo uses right message' '
1078 >foo &&
1079 git add foo &&
1080 git stash push -m"test mfoo" &&
1081 echo "stash@{0}: On main: test mfoo" >expect &&
1082 git stash list -1 >actual &&
1083 test_cmp expect actual
1086 test_expect_success 'push --message foo is synonym for -mfoo' '
1087 >foo &&
1088 git add foo &&
1089 git stash push --message "test message foo" &&
1090 echo "stash@{0}: On main: test message foo" >expect &&
1091 git stash list -1 >actual &&
1092 test_cmp expect actual
1095 test_expect_success 'push --message=foo is synonym for -mfoo' '
1096 >foo &&
1097 git add foo &&
1098 git stash push --message="test message=foo" &&
1099 echo "stash@{0}: On main: test message=foo" >expect &&
1100 git stash list -1 >actual &&
1101 test_cmp expect actual
1104 test_expect_success 'push -m shows right message' '
1105 >foo &&
1106 git add foo &&
1107 git stash push -m "test m foo" &&
1108 echo "stash@{0}: On main: test m foo" >expect &&
1109 git stash list -1 >actual &&
1110 test_cmp expect actual
1113 test_expect_success 'create stores correct message' '
1114 >foo &&
1115 git add foo &&
1116 STASH_ID=$(git stash create "create test message") &&
1117 echo "On main: create test message" >expect &&
1118 git show --pretty=%s -s ${STASH_ID} >actual &&
1119 test_cmp expect actual
1122 test_expect_success 'create when branch name has /' '
1123 test_when_finished "git checkout main" &&
1124 git checkout -b some/topic &&
1125 >foo &&
1126 git add foo &&
1127 STASH_ID=$(git stash create "create test message") &&
1128 echo "On some/topic: create test message" >expect &&
1129 git show --pretty=%s -s ${STASH_ID} >actual &&
1130 test_cmp expect actual
1133 test_expect_success 'create with multiple arguments for the message' '
1134 >foo &&
1135 git add foo &&
1136 STASH_ID=$(git stash create test untracked) &&
1137 echo "On main: test untracked" >expect &&
1138 git show --pretty=%s -s ${STASH_ID} >actual &&
1139 test_cmp expect actual
1142 test_expect_success 'create in a detached state' '
1143 test_when_finished "git checkout main" &&
1144 git checkout HEAD~1 &&
1145 >foo &&
1146 git add foo &&
1147 STASH_ID=$(git stash create) &&
1148 HEAD_ID=$(git rev-parse --short HEAD) &&
1149 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1150 git show --pretty=%s -s ${STASH_ID} >actual &&
1151 test_cmp expect actual
1154 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1155 >foo &&
1156 >bar &&
1157 git add foo bar &&
1158 git stash push -- foo &&
1159 test_path_is_file bar &&
1160 test_path_is_missing foo &&
1161 git stash pop &&
1162 test_path_is_file foo &&
1163 test_path_is_file bar
1166 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1167 mkdir sub &&
1168 >foo &&
1169 >bar &&
1170 git add foo bar &&
1172 cd sub &&
1173 git stash push -- ../foo
1174 ) &&
1175 test_path_is_file bar &&
1176 test_path_is_missing foo &&
1177 git stash pop &&
1178 test_path_is_file foo &&
1179 test_path_is_file bar
1182 test_expect_success 'stash with multiple pathspec arguments' '
1183 >foo &&
1184 >bar &&
1185 >extra &&
1186 git add foo bar extra &&
1187 git stash push -- foo bar &&
1188 test_path_is_missing bar &&
1189 test_path_is_missing foo &&
1190 test_path_is_file extra &&
1191 git stash pop &&
1192 test_path_is_file foo &&
1193 test_path_is_file bar &&
1194 test_path_is_file extra
1197 test_expect_success 'stash with file including $IFS character' '
1198 >"foo bar" &&
1199 >foo &&
1200 >bar &&
1201 git add foo* &&
1202 git stash push -- "foo b*" &&
1203 test_path_is_missing "foo bar" &&
1204 test_path_is_file foo &&
1205 test_path_is_file bar &&
1206 git stash pop &&
1207 test_path_is_file "foo bar" &&
1208 test_path_is_file foo &&
1209 test_path_is_file bar
1212 test_expect_success 'stash with pathspec matching multiple paths' '
1213 echo original >file &&
1214 echo original >other-file &&
1215 git commit -m "two" file other-file &&
1216 echo modified >file &&
1217 echo modified >other-file &&
1218 git stash push -- "*file" &&
1219 echo original >expect &&
1220 test_cmp expect file &&
1221 test_cmp expect other-file &&
1222 git stash pop &&
1223 echo modified >expect &&
1224 test_cmp expect file &&
1225 test_cmp expect other-file
1228 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1229 >foo &&
1230 git add foo &&
1231 git commit -m "tmp" &&
1232 git stash push -p foo >actual &&
1233 echo "No local changes to save" >expect &&
1234 git reset --hard HEAD~ &&
1235 test_cmp expect actual
1238 test_expect_success 'push <pathspec>: show no changes when there are none' '
1239 >foo &&
1240 git add foo &&
1241 git commit -m "tmp" &&
1242 git stash push foo >actual &&
1243 echo "No local changes to save" >expect &&
1244 git reset --hard HEAD~ &&
1245 test_cmp expect actual
1248 test_expect_success 'push: <pathspec> not in the repository errors out' '
1249 >untracked &&
1250 test_must_fail git stash push untracked &&
1251 test_path_is_file untracked
1254 test_expect_success 'push: -q is quiet with changes' '
1255 >foo &&
1256 git add foo &&
1257 git stash push -q >output 2>&1 &&
1258 test_must_be_empty output
1261 test_expect_success 'push: -q is quiet with no changes' '
1262 git stash push -q >output 2>&1 &&
1263 test_must_be_empty output
1266 test_expect_success 'push: -q is quiet even if there is no initial commit' '
1267 git init foo_dir &&
1268 test_when_finished rm -rf foo_dir &&
1270 cd foo_dir &&
1271 >bar &&
1272 test_must_fail git stash push -q >output 2>&1 &&
1273 test_must_be_empty output
1277 test_expect_success 'untracked files are left in place when -u is not given' '
1278 >file &&
1279 git add file &&
1280 >untracked &&
1281 git stash push file &&
1282 test_path_is_file untracked
1285 test_expect_success 'stash without verb with pathspec' '
1286 >"foo bar" &&
1287 >foo &&
1288 >bar &&
1289 git add foo* &&
1290 git stash -- "foo b*" &&
1291 test_path_is_missing "foo bar" &&
1292 test_path_is_file foo &&
1293 test_path_is_file bar &&
1294 git stash pop &&
1295 test_path_is_file "foo bar" &&
1296 test_path_is_file foo &&
1297 test_path_is_file bar
1300 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1301 git reset &&
1302 >foo &&
1303 >bar &&
1304 git add foo bar &&
1305 git commit -m "test" &&
1306 echo "foo" >foo &&
1307 echo "bar" >bar &&
1308 git stash -k -- foo &&
1309 test "",bar = $(cat foo),$(cat bar) &&
1310 git stash pop &&
1311 test foo,bar = $(cat foo),$(cat bar)
1314 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1315 git reset &&
1316 >subdir/untracked &&
1317 >subdir/tracked1 &&
1318 >subdir/tracked2 &&
1319 git add subdir/tracked* &&
1320 git stash -- subdir/ &&
1321 test_path_is_missing subdir/tracked1 &&
1322 test_path_is_missing subdir/tracked2 &&
1323 test_path_is_file subdir/untracked &&
1324 git stash pop &&
1325 test_path_is_file subdir/tracked1 &&
1326 test_path_is_file subdir/tracked2 &&
1327 test_path_is_file subdir/untracked
1330 test_expect_success 'stash -- <subdir> works with binary files' '
1331 git reset &&
1332 >subdir/untracked &&
1333 >subdir/tracked &&
1334 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1335 git add subdir/tracked* &&
1336 git stash -- subdir/ &&
1337 test_path_is_missing subdir/tracked &&
1338 test_path_is_missing subdir/tracked-binary &&
1339 test_path_is_file subdir/untracked &&
1340 git stash pop &&
1341 test_path_is_file subdir/tracked &&
1342 test_path_is_file subdir/tracked-binary &&
1343 test_path_is_file subdir/untracked
1346 test_expect_success 'stash with user.name and user.email set works' '
1347 test_config user.name "A U Thor" &&
1348 test_config user.email "a.u@thor" &&
1349 git stash
1352 test_expect_success 'stash works when user.name and user.email are not set' '
1353 git reset &&
1354 >1 &&
1355 git add 1 &&
1356 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1357 git stash &&
1358 git show -s --format="%an <%ae>" refs/stash >actual &&
1359 test_cmp expect actual &&
1360 >2 &&
1361 git add 2 &&
1362 test_config user.useconfigonly true &&
1364 sane_unset GIT_AUTHOR_NAME &&
1365 sane_unset GIT_AUTHOR_EMAIL &&
1366 sane_unset GIT_COMMITTER_NAME &&
1367 sane_unset GIT_COMMITTER_EMAIL &&
1368 test_unconfig user.email &&
1369 test_unconfig user.name &&
1370 test_must_fail git commit -m "should fail" &&
1371 echo "git stash <git@stash>" >expect &&
1372 >2 &&
1373 git stash &&
1374 git show -s --format="%an <%ae>" refs/stash >actual &&
1375 test_cmp expect actual
1379 test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1380 test_commit to-remove to-remove &&
1381 git rm to-remove &&
1382 git stash --keep-index &&
1383 test_path_is_missing to-remove
1386 test_expect_success 'stash apply should succeed with unmodified file' '
1387 echo base >file &&
1388 git add file &&
1389 git commit -m base &&
1391 # now stash a modification
1392 echo modified >file &&
1393 git stash &&
1395 # make the file stat dirty
1396 cp file other &&
1397 mv other file &&
1399 git stash apply
1402 test_expect_success 'stash handles skip-worktree entries nicely' '
1403 test_commit A &&
1404 echo changed >A.t &&
1405 git add A.t &&
1406 git update-index --skip-worktree A.t &&
1407 rm A.t &&
1408 git stash &&
1410 git rev-parse --verify refs/stash:A.t
1413 test_expect_success 'git stash succeeds despite directory/file change' '
1414 test_create_repo directory_file_switch_v1 &&
1416 cd directory_file_switch_v1 &&
1417 test_commit init &&
1419 test_write_lines this file has some words >filler &&
1420 git add filler &&
1421 git commit -m filler &&
1423 git rm filler &&
1424 mkdir filler &&
1425 echo contents >filler/file &&
1426 git stash push
1430 test_expect_success 'git stash can pop file -> directory saved changes' '
1431 test_create_repo directory_file_switch_v2 &&
1433 cd directory_file_switch_v2 &&
1434 test_commit init &&
1436 test_write_lines this file has some words >filler &&
1437 git add filler &&
1438 git commit -m filler &&
1440 git rm filler &&
1441 mkdir filler &&
1442 echo contents >filler/file &&
1443 cp filler/file expect &&
1444 git stash push --include-untracked &&
1445 git stash apply --index &&
1446 test_cmp expect filler/file
1450 test_expect_success 'git stash can pop directory -> file saved changes' '
1451 test_create_repo directory_file_switch_v3 &&
1453 cd directory_file_switch_v3 &&
1454 test_commit init &&
1456 mkdir filler &&
1457 test_write_lines some words >filler/file1 &&
1458 test_write_lines and stuff >filler/file2 &&
1459 git add filler &&
1460 git commit -m filler &&
1462 git rm -rf filler &&
1463 echo contents >filler &&
1464 cp filler expect &&
1465 git stash push --include-untracked &&
1466 git stash apply --index &&
1467 test_cmp expect filler
1471 test_expect_success 'restore untracked files even when we hit conflicts' '
1472 git init restore_untracked_after_conflict &&
1474 cd restore_untracked_after_conflict &&
1476 echo hi >a &&
1477 echo there >b &&
1478 git add . &&
1479 git commit -m first &&
1480 echo hello >a &&
1481 echo something >c &&
1483 git stash push --include-untracked &&
1485 echo conflict >a &&
1486 git add a &&
1487 git commit -m second &&
1489 test_must_fail git stash pop &&
1491 test_path_is_file c
1495 test_done