add-patch: do not show UI messages on stderr
[git/gitster.git] / t / t3903-stash.sh
blob00db82fb2455b8397de6ae0e5ea8e4fd96ed3d6c
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 'dont assume push with non-option args' '
397 test_must_fail git stash -q drop 2>err &&
398 test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
401 test_expect_success 'stash --invalid-option' '
402 echo bar5 >file &&
403 echo bar6 >file2 &&
404 git add file2 &&
405 test_must_fail git stash --invalid-option &&
406 test_must_fail git stash save --invalid-option &&
407 test bar5,bar6 = $(cat file),$(cat file2)
410 test_expect_success 'stash an added file' '
411 git reset --hard &&
412 echo new >file3 &&
413 git add file3 &&
414 git stash save "added file" &&
415 ! test -r file3 &&
416 git stash apply &&
417 test new = "$(cat file3)"
420 test_expect_success 'stash --intent-to-add file' '
421 git reset --hard &&
422 echo new >file4 &&
423 git add --intent-to-add file4 &&
424 test_when_finished "git rm -f file4" &&
425 test_must_fail git stash
428 test_expect_success 'stash rm then recreate' '
429 git reset --hard &&
430 git rm file &&
431 echo bar7 >file &&
432 git stash save "rm then recreate" &&
433 test bar = "$(cat file)" &&
434 git stash apply &&
435 test bar7 = "$(cat file)"
438 test_expect_success 'stash rm and ignore' '
439 git reset --hard &&
440 git rm file &&
441 echo file >.gitignore &&
442 git stash save "rm and ignore" &&
443 test bar = "$(cat file)" &&
444 test file = "$(cat .gitignore)" &&
445 git stash apply &&
446 ! test -r file &&
447 test file = "$(cat .gitignore)"
450 test_expect_success 'stash rm and ignore (stage .gitignore)' '
451 git reset --hard &&
452 git rm file &&
453 echo file >.gitignore &&
454 git add .gitignore &&
455 git stash save "rm and ignore (stage .gitignore)" &&
456 test bar = "$(cat file)" &&
457 ! test -r .gitignore &&
458 git stash apply &&
459 ! test -r file &&
460 test file = "$(cat .gitignore)"
463 test_expect_success SYMLINKS 'stash file to symlink' '
464 git reset --hard &&
465 rm file &&
466 ln -s file2 file &&
467 git stash save "file to symlink" &&
468 test_path_is_file_not_symlink file &&
469 test bar = "$(cat file)" &&
470 git stash apply &&
471 test_path_is_symlink file &&
472 test "$(test_readlink file)" = file2
475 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
476 git reset --hard &&
477 git rm file &&
478 ln -s file2 file &&
479 git stash save "file to symlink (stage rm)" &&
480 test_path_is_file_not_symlink file &&
481 test bar = "$(cat file)" &&
482 git stash apply &&
483 test_path_is_symlink file &&
484 test "$(test_readlink file)" = file2
487 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
488 git reset --hard &&
489 rm file &&
490 ln -s file2 file &&
491 git add file &&
492 git stash save "file to symlink (full stage)" &&
493 test_path_is_file_not_symlink file &&
494 test bar = "$(cat file)" &&
495 git stash apply &&
496 test_path_is_symlink file &&
497 test "$(test_readlink file)" = file2
500 # This test creates a commit with a symlink used for the following tests
502 test_expect_success 'stash symlink to file' '
503 git reset --hard &&
504 test_ln_s_add file filelink &&
505 git commit -m "Add symlink" &&
506 rm filelink &&
507 cp file filelink &&
508 git stash save "symlink to file"
511 test_expect_success SYMLINKS 'this must have re-created the symlink' '
512 test -h filelink &&
513 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
516 test_expect_success 'unstash must re-create the file' '
517 git stash apply &&
518 ! test -h filelink &&
519 test bar = "$(cat file)"
522 test_expect_success 'stash symlink to file (stage rm)' '
523 git reset --hard &&
524 git rm filelink &&
525 cp file filelink &&
526 git stash save "symlink to file (stage rm)"
529 test_expect_success SYMLINKS 'this must have re-created the symlink' '
530 test -h filelink &&
531 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
534 test_expect_success 'unstash must re-create the file' '
535 git stash apply &&
536 ! test -h filelink &&
537 test bar = "$(cat file)"
540 test_expect_success 'stash symlink to file (full stage)' '
541 git reset --hard &&
542 rm filelink &&
543 cp file filelink &&
544 git add filelink &&
545 git stash save "symlink to file (full stage)"
548 test_expect_success SYMLINKS 'this must have re-created the symlink' '
549 test -h filelink &&
550 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
553 test_expect_success 'unstash must re-create the file' '
554 git stash apply &&
555 ! test -h filelink &&
556 test bar = "$(cat file)"
559 test_expect_failure 'stash directory to file' '
560 git reset --hard &&
561 mkdir dir &&
562 echo foo >dir/file &&
563 git add dir/file &&
564 git commit -m "Add file in dir" &&
565 rm -fr dir &&
566 echo bar >dir &&
567 git stash save "directory to file" &&
568 test_path_is_dir dir &&
569 test foo = "$(cat dir/file)" &&
570 test_must_fail git stash apply &&
571 test bar = "$(cat dir)" &&
572 git reset --soft HEAD^
575 test_expect_failure 'stash file to directory' '
576 git reset --hard &&
577 rm file &&
578 mkdir file &&
579 echo foo >file/file &&
580 git stash save "file to directory" &&
581 test_path_is_file file &&
582 test bar = "$(cat file)" &&
583 git stash apply &&
584 test_path_is_file file/file &&
585 test foo = "$(cat file/file)"
588 test_expect_success 'giving too many ref arguments does not modify files' '
589 git stash clear &&
590 test_when_finished "git reset --hard HEAD" &&
591 echo foo >file2 &&
592 git stash &&
593 echo bar >file2 &&
594 git stash &&
595 test-tool chmtime =123456789 file2 &&
596 for type in apply pop "branch stash-branch"
598 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
599 test_grep "Too many revisions" err &&
600 test 123456789 = $(test-tool chmtime -g file2) || return 1
601 done
604 test_expect_success 'drop: too many arguments errors out (does nothing)' '
605 git stash list >expect &&
606 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
607 test_grep "Too many revisions" err &&
608 git stash list >actual &&
609 test_cmp expect actual
612 test_expect_success 'show: too many arguments errors out (does nothing)' '
613 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
614 test_grep "Too many revisions" err &&
615 test_must_be_empty out
618 test_expect_success 'stash create - no changes' '
619 git stash clear &&
620 test_when_finished "git reset --hard HEAD" &&
621 git reset --hard &&
622 git stash create >actual &&
623 test_must_be_empty actual
626 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
627 git stash clear &&
628 test_when_finished "git reset --hard HEAD" &&
629 git reset --hard &&
630 echo foo >>file &&
631 STASH_ID=$(git stash create) &&
632 git reset --hard &&
633 git stash branch stash-branch ${STASH_ID} &&
634 test_when_finished "git reset --hard HEAD && git checkout main &&
635 git branch -D stash-branch" &&
636 test $(git ls-files --modified | wc -l) -eq 1
639 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
640 git stash clear &&
641 test_when_finished "git reset --hard HEAD" &&
642 git reset --hard &&
643 echo foo >>file &&
644 git stash &&
645 test_when_finished "git stash drop" &&
646 echo bar >>file &&
647 STASH_ID=$(git stash create) &&
648 git reset --hard &&
649 git stash branch stash-branch ${STASH_ID} &&
650 test_when_finished "git reset --hard HEAD && git checkout main &&
651 git branch -D stash-branch" &&
652 test $(git ls-files --modified | wc -l) -eq 1
655 test_expect_success 'stash branch complains with no arguments' '
656 test_must_fail git stash branch 2>err &&
657 test_grep "No branch name specified" err
660 test_expect_success 'stash show format defaults to --stat' '
661 git stash clear &&
662 test_when_finished "git reset --hard HEAD" &&
663 git reset --hard &&
664 echo foo >>file &&
665 git stash &&
666 test_when_finished "git stash drop" &&
667 echo bar >>file &&
668 STASH_ID=$(git stash create) &&
669 git reset --hard &&
670 cat >expected <<-EOF &&
671 file | 1 +
672 1 file changed, 1 insertion(+)
674 git stash show ${STASH_ID} >actual &&
675 test_cmp expected actual
678 test_expect_success 'stash show - stashes on stack, stash-like argument' '
679 git stash clear &&
680 test_when_finished "git reset --hard HEAD" &&
681 git reset --hard &&
682 echo foo >>file &&
683 git stash &&
684 test_when_finished "git stash drop" &&
685 echo bar >>file &&
686 STASH_ID=$(git stash create) &&
687 git reset --hard &&
688 echo "1 0 file" >expected &&
689 git stash show --numstat ${STASH_ID} >actual &&
690 test_cmp expected actual
693 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
694 git stash clear &&
695 test_when_finished "git reset --hard HEAD" &&
696 git reset --hard &&
697 echo foo >>file &&
698 git stash &&
699 test_when_finished "git stash drop" &&
700 echo bar >>file &&
701 STASH_ID=$(git stash create) &&
702 git reset --hard &&
703 cat >expected <<-EOF &&
704 diff --git a/file b/file
705 index 7601807..935fbd3 100644
706 --- a/file
707 +++ b/file
708 @@ -1 +1,2 @@
710 +bar
712 git stash show -p ${STASH_ID} >actual &&
713 diff_cmp expected actual
716 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
717 git stash clear &&
718 test_when_finished "git reset --hard HEAD" &&
719 git reset --hard &&
720 echo foo >>file &&
721 STASH_ID=$(git stash create) &&
722 git reset --hard &&
723 echo "1 0 file" >expected &&
724 git stash show --numstat ${STASH_ID} >actual &&
725 test_cmp expected actual
728 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
729 git stash clear &&
730 test_when_finished "git reset --hard HEAD" &&
731 git reset --hard &&
732 echo foo >>file &&
733 STASH_ID=$(git stash create) &&
734 git reset --hard &&
735 cat >expected <<-EOF &&
736 diff --git a/file b/file
737 index 7601807..71b52c4 100644
738 --- a/file
739 +++ b/file
740 @@ -1 +1,2 @@
742 +foo
744 git stash show -p ${STASH_ID} >actual &&
745 diff_cmp expected actual
748 test_expect_success 'stash show --patience shows diff' '
749 git reset --hard &&
750 echo foo >>file &&
751 STASH_ID=$(git stash create) &&
752 git reset --hard &&
753 cat >expected <<-EOF &&
754 diff --git a/file b/file
755 index 7601807..71b52c4 100644
756 --- a/file
757 +++ b/file
758 @@ -1 +1,2 @@
760 +foo
762 git stash show --patience ${STASH_ID} >actual &&
763 diff_cmp expected actual
766 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
767 git stash clear &&
768 test_when_finished "git reset --hard HEAD && git stash clear" &&
769 git reset --hard &&
770 echo foo >file &&
771 git stash &&
772 echo bar >file &&
773 git stash &&
774 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
775 git stash pop &&
776 test bar = "$(cat file)" &&
777 git reset --hard HEAD
780 test_expect_success 'pop: fail early if specified stash is not a stash ref' '
781 git stash clear &&
782 test_when_finished "git reset --hard HEAD && git stash clear" &&
783 git reset --hard &&
784 echo foo >file &&
785 git stash &&
786 echo bar >file &&
787 git stash &&
788 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
789 git stash pop &&
790 test bar = "$(cat file)" &&
791 git reset --hard HEAD
794 test_expect_success 'ref with non-existent reflog' '
795 git stash clear &&
796 echo bar5 >file &&
797 echo bar6 >file2 &&
798 git add file2 &&
799 git stash &&
800 test_must_fail git rev-parse --quiet --verify does-not-exist &&
801 test_must_fail git stash drop does-not-exist &&
802 test_must_fail git stash drop does-not-exist@{0} &&
803 test_must_fail git stash pop does-not-exist &&
804 test_must_fail git stash pop does-not-exist@{0} &&
805 test_must_fail git stash apply does-not-exist &&
806 test_must_fail git stash apply does-not-exist@{0} &&
807 test_must_fail git stash show does-not-exist &&
808 test_must_fail git stash show does-not-exist@{0} &&
809 test_must_fail git stash branch tmp does-not-exist &&
810 test_must_fail git stash branch tmp does-not-exist@{0} &&
811 git stash drop
814 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
815 git stash clear &&
816 test_must_fail git stash drop stash@{0} &&
817 echo bar5 >file &&
818 echo bar6 >file2 &&
819 git add file2 &&
820 git stash &&
821 test_must_fail git stash drop stash@{1} &&
822 test_must_fail git stash pop stash@{1} &&
823 test_must_fail git stash apply stash@{1} &&
824 test_must_fail git stash show stash@{1} &&
825 test_must_fail git stash branch tmp stash@{1} &&
826 git stash drop
829 test_expect_success 'invalid ref of the form "n", n >= N' '
830 git stash clear &&
831 test_must_fail git stash drop 0 &&
832 echo bar5 >file &&
833 echo bar6 >file2 &&
834 git add file2 &&
835 git stash &&
836 test_must_fail git stash drop 1 &&
837 test_must_fail git stash pop 1 &&
838 test_must_fail git stash apply 1 &&
839 test_must_fail git stash show 1 &&
840 test_must_fail git stash branch tmp 1 &&
841 git stash drop
844 test_expect_success 'valid ref of the form "n", n < N' '
845 git stash clear &&
846 echo bar5 >file &&
847 echo bar6 >file2 &&
848 git add file2 &&
849 git stash &&
850 git stash show 0 &&
851 git stash branch tmp 0 &&
852 git checkout main &&
853 git stash &&
854 git stash apply 0 &&
855 git reset --hard &&
856 git stash pop 0 &&
857 git stash &&
858 git stash drop 0 &&
859 test_must_fail git stash drop
862 test_expect_success 'branch: do not drop the stash if the branch exists' '
863 git stash clear &&
864 echo foo >file &&
865 git add file &&
866 git commit -m initial &&
867 echo bar >file &&
868 git stash &&
869 test_must_fail git stash branch main stash@{0} &&
870 git rev-parse stash@{0} --
873 test_expect_success 'branch: should not drop the stash if the apply fails' '
874 git stash clear &&
875 git reset HEAD~1 --hard &&
876 echo foo >file &&
877 git add file &&
878 git commit -m initial &&
879 echo bar >file &&
880 git stash &&
881 echo baz >file &&
882 test_when_finished "git checkout main" &&
883 test_must_fail git stash branch new_branch stash@{0} &&
884 git rev-parse stash@{0} --
887 test_expect_success 'apply: show same status as git status (relative to ./)' '
888 git stash clear &&
889 echo 1 >subdir/subfile1 &&
890 echo 2 >subdir/subfile2 &&
891 git add subdir/subfile1 &&
892 git commit -m subdir &&
894 cd subdir &&
895 echo x >subfile1 &&
896 echo x >../file &&
897 git status >../expect &&
898 git stash &&
899 sane_unset GIT_MERGE_VERBOSITY &&
900 git stash apply
902 sed -e 1d >actual && # drop "Saved..."
903 test_cmp expect actual
906 cat >expect <<EOF
907 diff --git a/HEAD b/HEAD
908 new file mode 100644
909 index 0000000..fe0cbee
910 --- /dev/null
911 +++ b/HEAD
912 @@ -0,0 +1 @@
913 +file-not-a-ref
916 test_expect_success 'stash where working directory contains "HEAD" file' '
917 git stash clear &&
918 git reset --hard &&
919 echo file-not-a-ref >HEAD &&
920 git add HEAD &&
921 test_tick &&
922 git stash &&
923 git diff-files --quiet &&
924 git diff-index --cached --quiet HEAD &&
925 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
926 git diff stash^..stash >output &&
927 diff_cmp expect output
930 test_expect_success 'store called with invalid commit' '
931 test_must_fail git stash store foo
934 test_expect_success 'store called with non-stash commit' '
935 test_must_fail git stash store HEAD
938 test_expect_success 'store updates stash ref and reflog' '
939 git stash clear &&
940 git reset --hard &&
941 echo quux >bazzy &&
942 git add bazzy &&
943 STASH_ID=$(git stash create) &&
944 git reset --hard &&
945 test_path_is_missing bazzy &&
946 git stash store -m quuxery $STASH_ID &&
947 test $(git rev-parse stash) = $STASH_ID &&
948 git reflog --format=%H stash| grep $STASH_ID &&
949 git stash pop &&
950 grep quux bazzy
953 test_expect_success 'handle stash specification with spaces' '
954 git stash clear &&
955 echo pig >file &&
956 git stash &&
957 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
958 test_tick &&
959 echo cow >file &&
960 git stash &&
961 git stash apply "stash@{$stamp}" &&
962 grep pig file
965 test_expect_success 'setup stash with index and worktree changes' '
966 git stash clear &&
967 git reset --hard &&
968 echo index >file &&
969 git add file &&
970 echo working >file &&
971 git stash
974 test_expect_success 'stash list -p shows simple diff' '
975 cat >expect <<-EOF &&
976 stash@{0}
978 diff --git a/file b/file
979 index 257cc56..d26b33d 100644
980 --- a/file
981 +++ b/file
982 @@ -1 +1 @@
983 -foo
984 +working
986 git stash list --format=%gd -p >actual &&
987 diff_cmp expect actual
990 test_expect_success 'stash list --cc shows combined diff' '
991 cat >expect <<-\EOF &&
992 stash@{0}
994 diff --cc file
995 index 257cc56,9015a7a..d26b33d
996 --- a/file
997 +++ b/file
998 @@@ -1,1 -1,1 +1,1 @@@
999 - foo
1000 -index
1001 ++working
1003 git stash list --format=%gd -p --cc >actual &&
1004 diff_cmp expect actual
1007 test_expect_success 'stash is not confused by partial renames' '
1008 mv file renamed &&
1009 git add renamed &&
1010 git stash &&
1011 git stash apply &&
1012 test_path_is_file renamed &&
1013 test_path_is_missing file
1016 test_expect_success 'push -m shows right message' '
1017 >foo &&
1018 git add foo &&
1019 git stash push -m "test message" &&
1020 echo "stash@{0}: On main: test message" >expect &&
1021 git stash list -1 >actual &&
1022 test_cmp expect actual
1025 test_expect_success 'push -m also works without space' '
1026 >foo &&
1027 git add foo &&
1028 git stash push -m"unspaced test message" &&
1029 echo "stash@{0}: On main: unspaced test message" >expect &&
1030 git stash list -1 >actual &&
1031 test_cmp expect actual
1034 test_expect_success 'store -m foo shows right message' '
1035 git stash clear &&
1036 git reset --hard &&
1037 echo quux >bazzy &&
1038 git add bazzy &&
1039 STASH_ID=$(git stash create) &&
1040 git stash store -m "store m" $STASH_ID &&
1041 echo "stash@{0}: store m" >expect &&
1042 git stash list -1 >actual &&
1043 test_cmp expect actual
1046 test_expect_success 'store -mfoo shows right message' '
1047 git stash clear &&
1048 git reset --hard &&
1049 echo quux >bazzy &&
1050 git add bazzy &&
1051 STASH_ID=$(git stash create) &&
1052 git stash store -m"store mfoo" $STASH_ID &&
1053 echo "stash@{0}: store mfoo" >expect &&
1054 git stash list -1 >actual &&
1055 test_cmp expect actual
1058 test_expect_success 'store --message=foo shows right message' '
1059 git stash clear &&
1060 git reset --hard &&
1061 echo quux >bazzy &&
1062 git add bazzy &&
1063 STASH_ID=$(git stash create) &&
1064 git stash store --message="store message=foo" $STASH_ID &&
1065 echo "stash@{0}: store message=foo" >expect &&
1066 git stash list -1 >actual &&
1067 test_cmp expect actual
1070 test_expect_success 'store --message foo shows right message' '
1071 git stash clear &&
1072 git reset --hard &&
1073 echo quux >bazzy &&
1074 git add bazzy &&
1075 STASH_ID=$(git stash create) &&
1076 git stash store --message "store message foo" $STASH_ID &&
1077 echo "stash@{0}: store message foo" >expect &&
1078 git stash list -1 >actual &&
1079 test_cmp expect actual
1082 test_expect_success 'push -mfoo uses right message' '
1083 >foo &&
1084 git add foo &&
1085 git stash push -m"test mfoo" &&
1086 echo "stash@{0}: On main: test mfoo" >expect &&
1087 git stash list -1 >actual &&
1088 test_cmp expect actual
1091 test_expect_success 'push --message foo is synonym for -mfoo' '
1092 >foo &&
1093 git add foo &&
1094 git stash push --message "test message foo" &&
1095 echo "stash@{0}: On main: test message foo" >expect &&
1096 git stash list -1 >actual &&
1097 test_cmp expect actual
1100 test_expect_success 'push --message=foo is synonym for -mfoo' '
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 -m shows right message' '
1110 >foo &&
1111 git add foo &&
1112 git stash push -m "test m foo" &&
1113 echo "stash@{0}: On main: test m foo" >expect &&
1114 git stash list -1 >actual &&
1115 test_cmp expect actual
1118 test_expect_success 'create stores correct message' '
1119 >foo &&
1120 git add foo &&
1121 STASH_ID=$(git stash create "create test message") &&
1122 echo "On main: create test message" >expect &&
1123 git show --pretty=%s -s ${STASH_ID} >actual &&
1124 test_cmp expect actual
1127 test_expect_success 'create when branch name has /' '
1128 test_when_finished "git checkout main" &&
1129 git checkout -b some/topic &&
1130 >foo &&
1131 git add foo &&
1132 STASH_ID=$(git stash create "create test message") &&
1133 echo "On some/topic: create test message" >expect &&
1134 git show --pretty=%s -s ${STASH_ID} >actual &&
1135 test_cmp expect actual
1138 test_expect_success 'create with multiple arguments for the message' '
1139 >foo &&
1140 git add foo &&
1141 STASH_ID=$(git stash create test untracked) &&
1142 echo "On main: test untracked" >expect &&
1143 git show --pretty=%s -s ${STASH_ID} >actual &&
1144 test_cmp expect actual
1147 test_expect_success 'create in a detached state' '
1148 test_when_finished "git checkout main" &&
1149 git checkout HEAD~1 &&
1150 >foo &&
1151 git add foo &&
1152 STASH_ID=$(git stash create) &&
1153 HEAD_ID=$(git rev-parse --short HEAD) &&
1154 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1155 git show --pretty=%s -s ${STASH_ID} >actual &&
1156 test_cmp expect actual
1159 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1160 >foo &&
1161 >bar &&
1162 git add foo bar &&
1163 git stash push -- foo &&
1164 test_path_is_file bar &&
1165 test_path_is_missing foo &&
1166 git stash pop &&
1167 test_path_is_file foo &&
1168 test_path_is_file bar
1171 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1172 mkdir sub &&
1173 >foo &&
1174 >bar &&
1175 git add foo bar &&
1177 cd sub &&
1178 git stash push -- ../foo
1179 ) &&
1180 test_path_is_file bar &&
1181 test_path_is_missing foo &&
1182 git stash pop &&
1183 test_path_is_file foo &&
1184 test_path_is_file bar
1187 test_expect_success 'stash with multiple pathspec arguments' '
1188 >foo &&
1189 >bar &&
1190 >extra &&
1191 git add foo bar extra &&
1192 git stash push -- foo bar &&
1193 test_path_is_missing bar &&
1194 test_path_is_missing foo &&
1195 test_path_is_file extra &&
1196 git stash pop &&
1197 test_path_is_file foo &&
1198 test_path_is_file bar &&
1199 test_path_is_file extra
1202 test_expect_success 'stash with file including $IFS character' '
1203 >"foo bar" &&
1204 >foo &&
1205 >bar &&
1206 git add foo* &&
1207 git stash push -- "foo b*" &&
1208 test_path_is_missing "foo bar" &&
1209 test_path_is_file foo &&
1210 test_path_is_file bar &&
1211 git stash pop &&
1212 test_path_is_file "foo bar" &&
1213 test_path_is_file foo &&
1214 test_path_is_file bar
1217 test_expect_success 'stash with pathspec matching multiple paths' '
1218 echo original >file &&
1219 echo original >other-file &&
1220 git commit -m "two" file other-file &&
1221 echo modified >file &&
1222 echo modified >other-file &&
1223 git stash push -- "*file" &&
1224 echo original >expect &&
1225 test_cmp expect file &&
1226 test_cmp expect other-file &&
1227 git stash pop &&
1228 echo modified >expect &&
1229 test_cmp expect file &&
1230 test_cmp expect other-file
1233 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1234 >foo &&
1235 git add foo &&
1236 git commit -m "tmp" &&
1237 git stash push -p foo >actual &&
1238 echo "No local changes to save" >expect &&
1239 git reset --hard HEAD~ &&
1240 test_cmp expect actual
1243 test_expect_success 'push <pathspec>: show no changes when there are none' '
1244 >foo &&
1245 git add foo &&
1246 git commit -m "tmp" &&
1247 git stash push foo >actual &&
1248 echo "No local changes to save" >expect &&
1249 git reset --hard HEAD~ &&
1250 test_cmp expect actual
1253 test_expect_success 'push: <pathspec> not in the repository errors out' '
1254 >untracked &&
1255 test_must_fail git stash push untracked &&
1256 test_path_is_file untracked
1259 test_expect_success 'push: -q is quiet with changes' '
1260 >foo &&
1261 git add foo &&
1262 git stash push -q >output 2>&1 &&
1263 test_must_be_empty output
1266 test_expect_success 'push: -q is quiet with no changes' '
1267 git stash push -q >output 2>&1 &&
1268 test_must_be_empty output
1271 test_expect_success 'push: -q is quiet even if there is no initial commit' '
1272 git init foo_dir &&
1273 test_when_finished rm -rf foo_dir &&
1275 cd foo_dir &&
1276 >bar &&
1277 test_must_fail git stash push -q >output 2>&1 &&
1278 test_must_be_empty output
1282 test_expect_success 'untracked files are left in place when -u is not given' '
1283 >file &&
1284 git add file &&
1285 >untracked &&
1286 git stash push file &&
1287 test_path_is_file untracked
1290 test_expect_success 'stash without verb with pathspec' '
1291 >"foo bar" &&
1292 >foo &&
1293 >bar &&
1294 git add foo* &&
1295 git stash -- "foo b*" &&
1296 test_path_is_missing "foo bar" &&
1297 test_path_is_file foo &&
1298 test_path_is_file bar &&
1299 git stash pop &&
1300 test_path_is_file "foo bar" &&
1301 test_path_is_file foo &&
1302 test_path_is_file bar
1305 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1306 git reset &&
1307 >foo &&
1308 >bar &&
1309 git add foo bar &&
1310 git commit -m "test" &&
1311 echo "foo" >foo &&
1312 echo "bar" >bar &&
1313 git stash -k -- foo &&
1314 test "",bar = $(cat foo),$(cat bar) &&
1315 git stash pop &&
1316 test foo,bar = $(cat foo),$(cat bar)
1319 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1320 git reset &&
1321 >subdir/untracked &&
1322 >subdir/tracked1 &&
1323 >subdir/tracked2 &&
1324 git add subdir/tracked* &&
1325 git stash -- subdir/ &&
1326 test_path_is_missing subdir/tracked1 &&
1327 test_path_is_missing subdir/tracked2 &&
1328 test_path_is_file subdir/untracked &&
1329 git stash pop &&
1330 test_path_is_file subdir/tracked1 &&
1331 test_path_is_file subdir/tracked2 &&
1332 test_path_is_file subdir/untracked
1335 test_expect_success 'stash -- <subdir> works with binary files' '
1336 git reset &&
1337 >subdir/untracked &&
1338 >subdir/tracked &&
1339 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1340 git add subdir/tracked* &&
1341 git stash -- subdir/ &&
1342 test_path_is_missing subdir/tracked &&
1343 test_path_is_missing subdir/tracked-binary &&
1344 test_path_is_file subdir/untracked &&
1345 git stash pop &&
1346 test_path_is_file subdir/tracked &&
1347 test_path_is_file subdir/tracked-binary &&
1348 test_path_is_file subdir/untracked
1351 test_expect_success 'stash with user.name and user.email set works' '
1352 test_config user.name "A U Thor" &&
1353 test_config user.email "a.u@thor" &&
1354 git stash
1357 test_expect_success 'stash works when user.name and user.email are not set' '
1358 git reset &&
1359 >1 &&
1360 git add 1 &&
1361 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1362 git stash &&
1363 git show -s --format="%an <%ae>" refs/stash >actual &&
1364 test_cmp expect actual &&
1365 >2 &&
1366 git add 2 &&
1367 test_config user.useconfigonly true &&
1369 sane_unset GIT_AUTHOR_NAME &&
1370 sane_unset GIT_AUTHOR_EMAIL &&
1371 sane_unset GIT_COMMITTER_NAME &&
1372 sane_unset GIT_COMMITTER_EMAIL &&
1373 test_unconfig user.email &&
1374 test_unconfig user.name &&
1375 test_must_fail git commit -m "should fail" &&
1376 echo "git stash <git@stash>" >expect &&
1377 >2 &&
1378 git stash &&
1379 git show -s --format="%an <%ae>" refs/stash >actual &&
1380 test_cmp expect actual
1384 test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1385 test_commit to-remove to-remove &&
1386 git rm to-remove &&
1387 git stash --keep-index &&
1388 test_path_is_missing to-remove
1391 test_expect_success 'stash apply should succeed with unmodified file' '
1392 echo base >file &&
1393 git add file &&
1394 git commit -m base &&
1396 # now stash a modification
1397 echo modified >file &&
1398 git stash &&
1400 # make the file stat dirty
1401 cp file other &&
1402 mv other file &&
1404 git stash apply
1407 test_expect_success 'stash handles skip-worktree entries nicely' '
1408 test_commit A &&
1409 echo changed >A.t &&
1410 git add A.t &&
1411 git update-index --skip-worktree A.t &&
1412 rm A.t &&
1413 git stash &&
1415 git rev-parse --verify refs/stash:A.t
1419 BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1421 test_expect_success 'stash with core.fsyncmethod=batch' "
1422 test_create_unique_files 2 4 files_base_dir &&
1423 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1425 # The files were untracked, so use the third parent,
1426 # which contains the untracked files
1427 git ls-tree -r stash^3 -- ./files_base_dir/ |
1428 test_parse_ls_tree_oids >stashed_files_oids &&
1430 # We created 2 dirs with 4 files each (8 files total) above
1431 test_line_count = 8 stashed_files_oids &&
1432 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1433 test_cmp stashed_files_oids stashed_files_actual
1437 test_expect_success 'git stash succeeds despite directory/file change' '
1438 test_create_repo directory_file_switch_v1 &&
1440 cd directory_file_switch_v1 &&
1441 test_commit init &&
1443 test_write_lines this file has some words >filler &&
1444 git add filler &&
1445 git commit -m filler &&
1447 git rm filler &&
1448 mkdir filler &&
1449 echo contents >filler/file &&
1450 git stash push
1454 test_expect_success 'git stash can pop file -> directory saved changes' '
1455 test_create_repo directory_file_switch_v2 &&
1457 cd directory_file_switch_v2 &&
1458 test_commit init &&
1460 test_write_lines this file has some words >filler &&
1461 git add filler &&
1462 git commit -m filler &&
1464 git rm filler &&
1465 mkdir filler &&
1466 echo contents >filler/file &&
1467 cp filler/file expect &&
1468 git stash push --include-untracked &&
1469 git stash apply --index &&
1470 test_cmp expect filler/file
1474 test_expect_success 'git stash can pop directory -> file saved changes' '
1475 test_create_repo directory_file_switch_v3 &&
1477 cd directory_file_switch_v3 &&
1478 test_commit init &&
1480 mkdir filler &&
1481 test_write_lines some words >filler/file1 &&
1482 test_write_lines and stuff >filler/file2 &&
1483 git add filler &&
1484 git commit -m filler &&
1486 git rm -rf filler &&
1487 echo contents >filler &&
1488 cp filler expect &&
1489 git stash push --include-untracked &&
1490 git stash apply --index &&
1491 test_cmp expect filler
1495 test_expect_success 'restore untracked files even when we hit conflicts' '
1496 git init restore_untracked_after_conflict &&
1498 cd restore_untracked_after_conflict &&
1500 echo hi >a &&
1501 echo there >b &&
1502 git add . &&
1503 git commit -m first &&
1504 echo hello >a &&
1505 echo something >c &&
1507 git stash push --include-untracked &&
1509 echo conflict >a &&
1510 git add a &&
1511 git commit -m second &&
1513 test_must_fail git stash pop &&
1515 test_path_is_file c
1519 test_expect_success 'stash create reports a locked index' '
1520 test_when_finished "rm -rf repo" &&
1521 git init repo &&
1523 cd repo &&
1524 test_commit A A.file &&
1525 echo change >A.file &&
1526 touch .git/index.lock &&
1528 cat >expect <<-EOF &&
1529 error: could not write index
1531 test_must_fail git stash create 2>err &&
1532 test_cmp expect err
1536 test_expect_success 'stash push reports a locked index' '
1537 test_when_finished "rm -rf repo" &&
1538 git init repo &&
1540 cd repo &&
1541 test_commit A A.file &&
1542 echo change >A.file &&
1543 touch .git/index.lock &&
1545 cat >expect <<-EOF &&
1546 error: could not write index
1548 test_must_fail git stash push 2>err &&
1549 test_cmp expect err
1553 test_expect_success 'stash apply reports a locked index' '
1554 test_when_finished "rm -rf repo" &&
1555 git init repo &&
1557 cd repo &&
1558 test_commit A A.file &&
1559 echo change >A.file &&
1560 git stash push &&
1561 touch .git/index.lock &&
1563 cat >expect <<-EOF &&
1564 error: could not write index
1566 test_must_fail git stash apply 2>err &&
1567 test_cmp expect err
1571 test_done