Merge branch 'ab/mark-leak-free-tests-even-more'
[git/debian.git] / t / t3903-stash.sh
blob2c66cfbc3b7fbadaa3f8c7a34a38cba2a6eac37f
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 diff_cmp () {
14 for i in "$1" "$2"
16 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
17 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
18 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
19 "$i" >"$i.compare" || return 1
20 done &&
21 test_cmp "$1.compare" "$2.compare" &&
22 rm -f "$1.compare" "$2.compare"
25 test_expect_success 'stash some dirty working directory' '
26 echo 1 >file &&
27 git add file &&
28 echo unrelated >other-file &&
29 git add other-file &&
30 test_tick &&
31 git commit -m initial &&
32 echo 2 >file &&
33 git add file &&
34 echo 3 >file &&
35 test_tick &&
36 git stash &&
37 git diff-files --quiet &&
38 git diff-index --cached --quiet HEAD
41 cat >expect <<EOF
42 diff --git a/file b/file
43 index 0cfbf08..00750ed 100644
44 --- a/file
45 +++ b/file
46 @@ -1 +1 @@
49 EOF
51 test_expect_success 'parents of stash' '
52 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
53 git diff stash^2..stash >output &&
54 diff_cmp expect output
57 test_expect_success 'applying bogus stash does nothing' '
58 test_must_fail git stash apply stash@{1} &&
59 echo 1 >expect &&
60 test_cmp expect file
63 test_expect_success 'apply does not need clean working directory' '
64 echo 4 >other-file &&
65 git stash apply &&
66 echo 3 >expect &&
67 test_cmp expect file
70 test_expect_success 'apply does not clobber working directory changes' '
71 git reset --hard &&
72 echo 4 >file &&
73 test_must_fail git stash apply &&
74 echo 4 >expect &&
75 test_cmp expect file
78 test_expect_success 'apply stashed changes' '
79 git reset --hard &&
80 echo 5 >other-file &&
81 git add other-file &&
82 test_tick &&
83 git commit -m other-file &&
84 git stash apply &&
85 test 3 = $(cat file) &&
86 test 1 = $(git show :file) &&
87 test 1 = $(git show HEAD:file)
90 test_expect_success 'apply stashed changes (including index)' '
91 git reset --hard HEAD^ &&
92 echo 6 >other-file &&
93 git add other-file &&
94 test_tick &&
95 git commit -m other-file &&
96 git stash apply --index &&
97 test 3 = $(cat file) &&
98 test 2 = $(git show :file) &&
99 test 1 = $(git show HEAD:file)
102 test_expect_success 'unstashing in a subdirectory' '
103 git reset --hard HEAD &&
104 mkdir subdir &&
106 cd subdir &&
107 git stash apply
111 test_expect_success 'stash drop complains of extra options' '
112 test_must_fail git stash drop --foo
115 test_expect_success 'drop top stash' '
116 git reset --hard &&
117 git stash list >expected &&
118 echo 7 >file &&
119 git stash &&
120 git stash drop &&
121 git stash list >actual &&
122 test_cmp expected actual &&
123 git stash apply &&
124 test 3 = $(cat file) &&
125 test 1 = $(git show :file) &&
126 test 1 = $(git show HEAD:file)
129 test_expect_success 'drop middle stash' '
130 git reset --hard &&
131 echo 8 >file &&
132 git stash &&
133 echo 9 >file &&
134 git stash &&
135 git stash drop stash@{1} &&
136 test 2 = $(git stash list | wc -l) &&
137 git stash apply &&
138 test 9 = $(cat file) &&
139 test 1 = $(git show :file) &&
140 test 1 = $(git show HEAD:file) &&
141 git reset --hard &&
142 git stash drop &&
143 git stash apply &&
144 test 3 = $(cat file) &&
145 test 1 = $(git show :file) &&
146 test 1 = $(git show HEAD:file)
149 test_expect_success 'drop middle stash by index' '
150 git reset --hard &&
151 echo 8 >file &&
152 git stash &&
153 echo 9 >file &&
154 git stash &&
155 git stash drop 1 &&
156 test 2 = $(git stash list | wc -l) &&
157 git stash apply &&
158 test 9 = $(cat file) &&
159 test 1 = $(git show :file) &&
160 test 1 = $(git show HEAD:file) &&
161 git reset --hard &&
162 git stash drop &&
163 git stash apply &&
164 test 3 = $(cat file) &&
165 test 1 = $(git show :file) &&
166 test 1 = $(git show HEAD:file)
169 test_expect_success 'stash pop' '
170 git reset --hard &&
171 git stash pop &&
172 test 3 = $(cat file) &&
173 test 1 = $(git show :file) &&
174 test 1 = $(git show HEAD:file) &&
175 test 0 = $(git stash list | wc -l)
178 cat >expect <<EOF
179 diff --git a/file2 b/file2
180 new file mode 100644
181 index 0000000..1fe912c
182 --- /dev/null
183 +++ b/file2
184 @@ -0,0 +1 @@
185 +bar2
188 cat >expect1 <<EOF
189 diff --git a/file b/file
190 index 257cc56..5716ca5 100644
191 --- a/file
192 +++ b/file
193 @@ -1 +1 @@
194 -foo
195 +bar
198 cat >expect2 <<EOF
199 diff --git a/file b/file
200 index 7601807..5716ca5 100644
201 --- a/file
202 +++ b/file
203 @@ -1 +1 @@
204 -baz
205 +bar
206 diff --git a/file2 b/file2
207 new file mode 100644
208 index 0000000..1fe912c
209 --- /dev/null
210 +++ b/file2
211 @@ -0,0 +1 @@
212 +bar2
215 test_expect_success 'stash branch' '
216 echo foo >file &&
217 git commit file -m first &&
218 echo bar >file &&
219 echo bar2 >file2 &&
220 git add file2 &&
221 git stash &&
222 echo baz >file &&
223 git commit file -m second &&
224 git stash branch stashbranch &&
225 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
226 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
227 git diff --cached >output &&
228 diff_cmp expect output &&
229 git diff >output &&
230 diff_cmp expect1 output &&
231 git add file &&
232 git commit -m alternate\ second &&
233 git diff main..stashbranch >output &&
234 diff_cmp output expect2 &&
235 test 0 = $(git stash list | wc -l)
238 test_expect_success 'apply -q is quiet' '
239 echo foo >file &&
240 git stash &&
241 git stash apply -q >output.out 2>&1 &&
242 test_must_be_empty output.out
245 test_expect_success 'save -q is quiet' '
246 git stash save --quiet >output.out 2>&1 &&
247 test_must_be_empty output.out
250 test_expect_success 'pop -q works and is quiet' '
251 git stash pop -q >output.out 2>&1 &&
252 echo bar >expect &&
253 git show :file >actual &&
254 test_cmp expect actual &&
255 test_must_be_empty output.out
258 test_expect_success 'pop -q --index works and is quiet' '
259 echo foo >file &&
260 git add file &&
261 git stash save --quiet &&
262 git stash pop -q --index >output.out 2>&1 &&
263 git diff-files file2 >file2.diff &&
264 test_must_be_empty file2.diff &&
265 test foo = "$(git show :file)" &&
266 test_must_be_empty output.out
269 test_expect_success 'drop -q is quiet' '
270 git stash &&
271 git stash drop -q >output.out 2>&1 &&
272 test_must_be_empty output.out
275 test_expect_success 'stash -k' '
276 echo bar3 >file &&
277 echo bar4 >file2 &&
278 git add file2 &&
279 git stash -k &&
280 test bar,bar4 = $(cat file),$(cat file2)
283 test_expect_success 'stash --no-keep-index' '
284 echo bar33 >file &&
285 echo bar44 >file2 &&
286 git add file2 &&
287 git stash --no-keep-index &&
288 test bar,bar2 = $(cat file),$(cat file2)
291 test_expect_success 'stash --staged' '
292 echo bar3 >file &&
293 echo bar4 >file2 &&
294 git add file2 &&
295 git stash --staged &&
296 test bar3,bar2 = $(cat file),$(cat file2) &&
297 git reset --hard &&
298 git stash pop &&
299 test bar,bar4 = $(cat file),$(cat file2)
302 test_expect_success 'dont assume push with non-option args' '
303 test_must_fail git stash -q drop 2>err &&
304 test_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
307 test_expect_success 'stash --invalid-option' '
308 echo bar5 >file &&
309 echo bar6 >file2 &&
310 git add file2 &&
311 test_must_fail git stash --invalid-option &&
312 test_must_fail git stash save --invalid-option &&
313 test bar5,bar6 = $(cat file),$(cat file2)
316 test_expect_success 'stash an added file' '
317 git reset --hard &&
318 echo new >file3 &&
319 git add file3 &&
320 git stash save "added file" &&
321 ! test -r file3 &&
322 git stash apply &&
323 test new = "$(cat file3)"
326 test_expect_success 'stash --intent-to-add file' '
327 git reset --hard &&
328 echo new >file4 &&
329 git add --intent-to-add file4 &&
330 test_when_finished "git rm -f file4" &&
331 test_must_fail git stash
334 test_expect_success 'stash rm then recreate' '
335 git reset --hard &&
336 git rm file &&
337 echo bar7 >file &&
338 git stash save "rm then recreate" &&
339 test bar = "$(cat file)" &&
340 git stash apply &&
341 test bar7 = "$(cat file)"
344 test_expect_success 'stash rm and ignore' '
345 git reset --hard &&
346 git rm file &&
347 echo file >.gitignore &&
348 git stash save "rm and ignore" &&
349 test bar = "$(cat file)" &&
350 test file = "$(cat .gitignore)" &&
351 git stash apply &&
352 ! test -r file &&
353 test file = "$(cat .gitignore)"
356 test_expect_success 'stash rm and ignore (stage .gitignore)' '
357 git reset --hard &&
358 git rm file &&
359 echo file >.gitignore &&
360 git add .gitignore &&
361 git stash save "rm and ignore (stage .gitignore)" &&
362 test bar = "$(cat file)" &&
363 ! test -r .gitignore &&
364 git stash apply &&
365 ! test -r file &&
366 test file = "$(cat .gitignore)"
369 test_expect_success SYMLINKS 'stash file to symlink' '
370 git reset --hard &&
371 rm file &&
372 ln -s file2 file &&
373 git stash save "file to symlink" &&
374 test -f file &&
375 test bar = "$(cat file)" &&
376 git stash apply &&
377 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
380 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
381 git reset --hard &&
382 git rm file &&
383 ln -s file2 file &&
384 git stash save "file to symlink (stage rm)" &&
385 test -f file &&
386 test bar = "$(cat file)" &&
387 git stash apply &&
388 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
391 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
392 git reset --hard &&
393 rm file &&
394 ln -s file2 file &&
395 git add file &&
396 git stash save "file to symlink (full stage)" &&
397 test -f file &&
398 test bar = "$(cat file)" &&
399 git stash apply &&
400 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
403 # This test creates a commit with a symlink used for the following tests
405 test_expect_success 'stash symlink to file' '
406 git reset --hard &&
407 test_ln_s_add file filelink &&
408 git commit -m "Add symlink" &&
409 rm filelink &&
410 cp file filelink &&
411 git stash save "symlink to file"
414 test_expect_success SYMLINKS 'this must have re-created the symlink' '
415 test -h filelink &&
416 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
419 test_expect_success 'unstash must re-create the file' '
420 git stash apply &&
421 ! test -h filelink &&
422 test bar = "$(cat file)"
425 test_expect_success 'stash symlink to file (stage rm)' '
426 git reset --hard &&
427 git rm filelink &&
428 cp file filelink &&
429 git stash save "symlink to file (stage rm)"
432 test_expect_success SYMLINKS 'this must have re-created the symlink' '
433 test -h filelink &&
434 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
437 test_expect_success 'unstash must re-create the file' '
438 git stash apply &&
439 ! test -h filelink &&
440 test bar = "$(cat file)"
443 test_expect_success 'stash symlink to file (full stage)' '
444 git reset --hard &&
445 rm filelink &&
446 cp file filelink &&
447 git add filelink &&
448 git stash save "symlink to file (full stage)"
451 test_expect_success SYMLINKS 'this must have re-created the symlink' '
452 test -h filelink &&
453 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
456 test_expect_success 'unstash must re-create the file' '
457 git stash apply &&
458 ! test -h filelink &&
459 test bar = "$(cat file)"
462 test_expect_failure 'stash directory to file' '
463 git reset --hard &&
464 mkdir dir &&
465 echo foo >dir/file &&
466 git add dir/file &&
467 git commit -m "Add file in dir" &&
468 rm -fr dir &&
469 echo bar >dir &&
470 git stash save "directory to file" &&
471 test -d dir &&
472 test foo = "$(cat dir/file)" &&
473 test_must_fail git stash apply &&
474 test bar = "$(cat dir)" &&
475 git reset --soft HEAD^
478 test_expect_failure 'stash file to directory' '
479 git reset --hard &&
480 rm file &&
481 mkdir file &&
482 echo foo >file/file &&
483 git stash save "file to directory" &&
484 test -f file &&
485 test bar = "$(cat file)" &&
486 git stash apply &&
487 test -f file/file &&
488 test foo = "$(cat file/file)"
491 test_expect_success 'giving too many ref arguments does not modify files' '
492 git stash clear &&
493 test_when_finished "git reset --hard HEAD" &&
494 echo foo >file2 &&
495 git stash &&
496 echo bar >file2 &&
497 git stash &&
498 test-tool chmtime =123456789 file2 &&
499 for type in apply pop "branch stash-branch"
501 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
502 test_i18ngrep "Too many revisions" err &&
503 test 123456789 = $(test-tool chmtime -g file2) || return 1
504 done
507 test_expect_success 'drop: too many arguments errors out (does nothing)' '
508 git stash list >expect &&
509 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
510 test_i18ngrep "Too many revisions" err &&
511 git stash list >actual &&
512 test_cmp expect actual
515 test_expect_success 'show: too many arguments errors out (does nothing)' '
516 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
517 test_i18ngrep "Too many revisions" err &&
518 test_must_be_empty out
521 test_expect_success 'stash create - no changes' '
522 git stash clear &&
523 test_when_finished "git reset --hard HEAD" &&
524 git reset --hard &&
525 git stash create >actual &&
526 test_must_be_empty actual
529 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
530 git stash clear &&
531 test_when_finished "git reset --hard HEAD" &&
532 git reset --hard &&
533 echo foo >>file &&
534 STASH_ID=$(git stash create) &&
535 git reset --hard &&
536 git stash branch stash-branch ${STASH_ID} &&
537 test_when_finished "git reset --hard HEAD && git checkout main &&
538 git branch -D stash-branch" &&
539 test $(git ls-files --modified | wc -l) -eq 1
542 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
543 git stash clear &&
544 test_when_finished "git reset --hard HEAD" &&
545 git reset --hard &&
546 echo foo >>file &&
547 git stash &&
548 test_when_finished "git stash drop" &&
549 echo bar >>file &&
550 STASH_ID=$(git stash create) &&
551 git reset --hard &&
552 git stash branch stash-branch ${STASH_ID} &&
553 test_when_finished "git reset --hard HEAD && git checkout main &&
554 git branch -D stash-branch" &&
555 test $(git ls-files --modified | wc -l) -eq 1
558 test_expect_success 'stash branch complains with no arguments' '
559 test_must_fail git stash branch 2>err &&
560 test_i18ngrep "No branch name specified" err
563 test_expect_success 'stash show format defaults to --stat' '
564 git stash clear &&
565 test_when_finished "git reset --hard HEAD" &&
566 git reset --hard &&
567 echo foo >>file &&
568 git stash &&
569 test_when_finished "git stash drop" &&
570 echo bar >>file &&
571 STASH_ID=$(git stash create) &&
572 git reset --hard &&
573 cat >expected <<-EOF &&
574 file | 1 +
575 1 file changed, 1 insertion(+)
577 git stash show ${STASH_ID} >actual &&
578 test_cmp expected actual
581 test_expect_success 'stash show - stashes on stack, stash-like argument' '
582 git stash clear &&
583 test_when_finished "git reset --hard HEAD" &&
584 git reset --hard &&
585 echo foo >>file &&
586 git stash &&
587 test_when_finished "git stash drop" &&
588 echo bar >>file &&
589 STASH_ID=$(git stash create) &&
590 git reset --hard &&
591 echo "1 0 file" >expected &&
592 git stash show --numstat ${STASH_ID} >actual &&
593 test_cmp expected actual
596 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
597 git stash clear &&
598 test_when_finished "git reset --hard HEAD" &&
599 git reset --hard &&
600 echo foo >>file &&
601 git stash &&
602 test_when_finished "git stash drop" &&
603 echo bar >>file &&
604 STASH_ID=$(git stash create) &&
605 git reset --hard &&
606 cat >expected <<-EOF &&
607 diff --git a/file b/file
608 index 7601807..935fbd3 100644
609 --- a/file
610 +++ b/file
611 @@ -1 +1,2 @@
613 +bar
615 git stash show -p ${STASH_ID} >actual &&
616 diff_cmp expected actual
619 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
620 git stash clear &&
621 test_when_finished "git reset --hard HEAD" &&
622 git reset --hard &&
623 echo foo >>file &&
624 STASH_ID=$(git stash create) &&
625 git reset --hard &&
626 echo "1 0 file" >expected &&
627 git stash show --numstat ${STASH_ID} >actual &&
628 test_cmp expected actual
631 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
632 git stash clear &&
633 test_when_finished "git reset --hard HEAD" &&
634 git reset --hard &&
635 echo foo >>file &&
636 STASH_ID=$(git stash create) &&
637 git reset --hard &&
638 cat >expected <<-EOF &&
639 diff --git a/file b/file
640 index 7601807..71b52c4 100644
641 --- a/file
642 +++ b/file
643 @@ -1 +1,2 @@
645 +foo
647 git stash show -p ${STASH_ID} >actual &&
648 diff_cmp expected actual
651 test_expect_success 'stash show --patience shows diff' '
652 git reset --hard &&
653 echo foo >>file &&
654 STASH_ID=$(git stash create) &&
655 git reset --hard &&
656 cat >expected <<-EOF &&
657 diff --git a/file b/file
658 index 7601807..71b52c4 100644
659 --- a/file
660 +++ b/file
661 @@ -1 +1,2 @@
663 +foo
665 git stash show --patience ${STASH_ID} >actual &&
666 diff_cmp expected actual
669 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
670 git stash clear &&
671 test_when_finished "git reset --hard HEAD && git stash clear" &&
672 git reset --hard &&
673 echo foo >file &&
674 git stash &&
675 echo bar >file &&
676 git stash &&
677 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
678 git stash pop &&
679 test bar = "$(cat file)" &&
680 git reset --hard HEAD
683 test_expect_success 'pop: fail early if specified stash is not a stash ref' '
684 git stash clear &&
685 test_when_finished "git reset --hard HEAD && git stash clear" &&
686 git reset --hard &&
687 echo foo >file &&
688 git stash &&
689 echo bar >file &&
690 git stash &&
691 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
692 git stash pop &&
693 test bar = "$(cat file)" &&
694 git reset --hard HEAD
697 test_expect_success 'ref with non-existent reflog' '
698 git stash clear &&
699 echo bar5 >file &&
700 echo bar6 >file2 &&
701 git add file2 &&
702 git stash &&
703 test_must_fail git rev-parse --quiet --verify does-not-exist &&
704 test_must_fail git stash drop does-not-exist &&
705 test_must_fail git stash drop does-not-exist@{0} &&
706 test_must_fail git stash pop does-not-exist &&
707 test_must_fail git stash pop does-not-exist@{0} &&
708 test_must_fail git stash apply does-not-exist &&
709 test_must_fail git stash apply does-not-exist@{0} &&
710 test_must_fail git stash show does-not-exist &&
711 test_must_fail git stash show does-not-exist@{0} &&
712 test_must_fail git stash branch tmp does-not-exist &&
713 test_must_fail git stash branch tmp does-not-exist@{0} &&
714 git stash drop
717 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
718 git stash clear &&
719 test_must_fail git stash drop stash@{0} &&
720 echo bar5 >file &&
721 echo bar6 >file2 &&
722 git add file2 &&
723 git stash &&
724 test_must_fail git stash drop stash@{1} &&
725 test_must_fail git stash pop stash@{1} &&
726 test_must_fail git stash apply stash@{1} &&
727 test_must_fail git stash show stash@{1} &&
728 test_must_fail git stash branch tmp stash@{1} &&
729 git stash drop
732 test_expect_success 'invalid ref of the form "n", n >= N' '
733 git stash clear &&
734 test_must_fail git stash drop 0 &&
735 echo bar5 >file &&
736 echo bar6 >file2 &&
737 git add file2 &&
738 git stash &&
739 test_must_fail git stash drop 1 &&
740 test_must_fail git stash pop 1 &&
741 test_must_fail git stash apply 1 &&
742 test_must_fail git stash show 1 &&
743 test_must_fail git stash branch tmp 1 &&
744 git stash drop
747 test_expect_success 'valid ref of the form "n", n < N' '
748 git stash clear &&
749 echo bar5 >file &&
750 echo bar6 >file2 &&
751 git add file2 &&
752 git stash &&
753 git stash show 0 &&
754 git stash branch tmp 0 &&
755 git checkout main &&
756 git stash &&
757 git stash apply 0 &&
758 git reset --hard &&
759 git stash pop 0 &&
760 git stash &&
761 git stash drop 0 &&
762 test_must_fail git stash drop
765 test_expect_success 'branch: do not drop the stash if the branch exists' '
766 git stash clear &&
767 echo foo >file &&
768 git add file &&
769 git commit -m initial &&
770 echo bar >file &&
771 git stash &&
772 test_must_fail git stash branch main stash@{0} &&
773 git rev-parse stash@{0} --
776 test_expect_success 'branch: should not drop the stash if the apply fails' '
777 git stash clear &&
778 git reset HEAD~1 --hard &&
779 echo foo >file &&
780 git add file &&
781 git commit -m initial &&
782 echo bar >file &&
783 git stash &&
784 echo baz >file &&
785 test_when_finished "git checkout main" &&
786 test_must_fail git stash branch new_branch stash@{0} &&
787 git rev-parse stash@{0} --
790 test_expect_success 'apply: show same status as git status (relative to ./)' '
791 git stash clear &&
792 echo 1 >subdir/subfile1 &&
793 echo 2 >subdir/subfile2 &&
794 git add subdir/subfile1 &&
795 git commit -m subdir &&
797 cd subdir &&
798 echo x >subfile1 &&
799 echo x >../file &&
800 git status >../expect &&
801 git stash &&
802 sane_unset GIT_MERGE_VERBOSITY &&
803 git stash apply
805 sed -e 1d >actual && # drop "Saved..."
806 test_cmp expect actual
809 cat >expect <<EOF
810 diff --git a/HEAD b/HEAD
811 new file mode 100644
812 index 0000000..fe0cbee
813 --- /dev/null
814 +++ b/HEAD
815 @@ -0,0 +1 @@
816 +file-not-a-ref
819 test_expect_success 'stash where working directory contains "HEAD" file' '
820 git stash clear &&
821 git reset --hard &&
822 echo file-not-a-ref >HEAD &&
823 git add HEAD &&
824 test_tick &&
825 git stash &&
826 git diff-files --quiet &&
827 git diff-index --cached --quiet HEAD &&
828 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
829 git diff stash^..stash >output &&
830 diff_cmp expect output
833 test_expect_success 'store called with invalid commit' '
834 test_must_fail git stash store foo
837 test_expect_success 'store updates stash ref and reflog' '
838 git stash clear &&
839 git reset --hard &&
840 echo quux >bazzy &&
841 git add bazzy &&
842 STASH_ID=$(git stash create) &&
843 git reset --hard &&
844 test_path_is_missing bazzy &&
845 git stash store -m quuxery $STASH_ID &&
846 test $(git rev-parse stash) = $STASH_ID &&
847 git reflog --format=%H stash| grep $STASH_ID &&
848 git stash pop &&
849 grep quux bazzy
852 test_expect_success 'handle stash specification with spaces' '
853 git stash clear &&
854 echo pig >file &&
855 git stash &&
856 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
857 test_tick &&
858 echo cow >file &&
859 git stash &&
860 git stash apply "stash@{$stamp}" &&
861 grep pig file
864 test_expect_success 'setup stash with index and worktree changes' '
865 git stash clear &&
866 git reset --hard &&
867 echo index >file &&
868 git add file &&
869 echo working >file &&
870 git stash
873 test_expect_success 'stash list -p shows simple diff' '
874 cat >expect <<-EOF &&
875 stash@{0}
877 diff --git a/file b/file
878 index 257cc56..d26b33d 100644
879 --- a/file
880 +++ b/file
881 @@ -1 +1 @@
882 -foo
883 +working
885 git stash list --format=%gd -p >actual &&
886 diff_cmp expect actual
889 test_expect_success 'stash list --cc shows combined diff' '
890 cat >expect <<-\EOF &&
891 stash@{0}
893 diff --cc file
894 index 257cc56,9015a7a..d26b33d
895 --- a/file
896 +++ b/file
897 @@@ -1,1 -1,1 +1,1 @@@
898 - foo
899 -index
900 ++working
902 git stash list --format=%gd -p --cc >actual &&
903 diff_cmp expect actual
906 test_expect_success 'stash is not confused by partial renames' '
907 mv file renamed &&
908 git add renamed &&
909 git stash &&
910 git stash apply &&
911 test_path_is_file renamed &&
912 test_path_is_missing file
915 test_expect_success 'push -m shows right message' '
916 >foo &&
917 git add foo &&
918 git stash push -m "test message" &&
919 echo "stash@{0}: On main: test message" >expect &&
920 git stash list -1 >actual &&
921 test_cmp expect actual
924 test_expect_success 'push -m also works without space' '
925 >foo &&
926 git add foo &&
927 git stash push -m"unspaced test message" &&
928 echo "stash@{0}: On main: unspaced test message" >expect &&
929 git stash list -1 >actual &&
930 test_cmp expect actual
933 test_expect_success 'store -m foo shows right message' '
934 git stash clear &&
935 git reset --hard &&
936 echo quux >bazzy &&
937 git add bazzy &&
938 STASH_ID=$(git stash create) &&
939 git stash store -m "store m" $STASH_ID &&
940 echo "stash@{0}: store m" >expect &&
941 git stash list -1 >actual &&
942 test_cmp expect actual
945 test_expect_success 'store -mfoo shows right message' '
946 git stash clear &&
947 git reset --hard &&
948 echo quux >bazzy &&
949 git add bazzy &&
950 STASH_ID=$(git stash create) &&
951 git stash store -m"store mfoo" $STASH_ID &&
952 echo "stash@{0}: store mfoo" >expect &&
953 git stash list -1 >actual &&
954 test_cmp expect actual
957 test_expect_success 'store --message=foo shows right message' '
958 git stash clear &&
959 git reset --hard &&
960 echo quux >bazzy &&
961 git add bazzy &&
962 STASH_ID=$(git stash create) &&
963 git stash store --message="store message=foo" $STASH_ID &&
964 echo "stash@{0}: store message=foo" >expect &&
965 git stash list -1 >actual &&
966 test_cmp expect actual
969 test_expect_success 'store --message foo shows right message' '
970 git stash clear &&
971 git reset --hard &&
972 echo quux >bazzy &&
973 git add bazzy &&
974 STASH_ID=$(git stash create) &&
975 git stash store --message "store message foo" $STASH_ID &&
976 echo "stash@{0}: store message foo" >expect &&
977 git stash list -1 >actual &&
978 test_cmp expect actual
981 test_expect_success 'push -mfoo uses right message' '
982 >foo &&
983 git add foo &&
984 git stash push -m"test mfoo" &&
985 echo "stash@{0}: On main: test mfoo" >expect &&
986 git stash list -1 >actual &&
987 test_cmp expect actual
990 test_expect_success 'push --message foo is synonym for -mfoo' '
991 >foo &&
992 git add foo &&
993 git stash push --message "test message foo" &&
994 echo "stash@{0}: On main: test message foo" >expect &&
995 git stash list -1 >actual &&
996 test_cmp expect actual
999 test_expect_success 'push --message=foo is synonym for -mfoo' '
1000 >foo &&
1001 git add foo &&
1002 git stash push --message="test message=foo" &&
1003 echo "stash@{0}: On main: test message=foo" >expect &&
1004 git stash list -1 >actual &&
1005 test_cmp expect actual
1008 test_expect_success 'push -m shows right message' '
1009 >foo &&
1010 git add foo &&
1011 git stash push -m "test m foo" &&
1012 echo "stash@{0}: On main: test m foo" >expect &&
1013 git stash list -1 >actual &&
1014 test_cmp expect actual
1017 test_expect_success 'create stores correct message' '
1018 >foo &&
1019 git add foo &&
1020 STASH_ID=$(git stash create "create test message") &&
1021 echo "On main: create test message" >expect &&
1022 git show --pretty=%s -s ${STASH_ID} >actual &&
1023 test_cmp expect actual
1026 test_expect_success 'create with multiple arguments for the message' '
1027 >foo &&
1028 git add foo &&
1029 STASH_ID=$(git stash create test untracked) &&
1030 echo "On main: test untracked" >expect &&
1031 git show --pretty=%s -s ${STASH_ID} >actual &&
1032 test_cmp expect actual
1035 test_expect_success 'create in a detached state' '
1036 test_when_finished "git checkout main" &&
1037 git checkout HEAD~1 &&
1038 >foo &&
1039 git add foo &&
1040 STASH_ID=$(git stash create) &&
1041 HEAD_ID=$(git rev-parse --short HEAD) &&
1042 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1043 git show --pretty=%s -s ${STASH_ID} >actual &&
1044 test_cmp expect actual
1047 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1048 >foo &&
1049 >bar &&
1050 git add foo bar &&
1051 git stash push -- foo &&
1052 test_path_is_file bar &&
1053 test_path_is_missing foo &&
1054 git stash pop &&
1055 test_path_is_file foo &&
1056 test_path_is_file bar
1059 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1060 mkdir sub &&
1061 >foo &&
1062 >bar &&
1063 git add foo bar &&
1065 cd sub &&
1066 git stash push -- ../foo
1067 ) &&
1068 test_path_is_file bar &&
1069 test_path_is_missing foo &&
1070 git stash pop &&
1071 test_path_is_file foo &&
1072 test_path_is_file bar
1075 test_expect_success 'stash with multiple pathspec arguments' '
1076 >foo &&
1077 >bar &&
1078 >extra &&
1079 git add foo bar extra &&
1080 git stash push -- foo bar &&
1081 test_path_is_missing bar &&
1082 test_path_is_missing foo &&
1083 test_path_is_file extra &&
1084 git stash pop &&
1085 test_path_is_file foo &&
1086 test_path_is_file bar &&
1087 test_path_is_file extra
1090 test_expect_success 'stash with file including $IFS character' '
1091 >"foo bar" &&
1092 >foo &&
1093 >bar &&
1094 git add foo* &&
1095 git stash push -- "foo b*" &&
1096 test_path_is_missing "foo bar" &&
1097 test_path_is_file foo &&
1098 test_path_is_file bar &&
1099 git stash pop &&
1100 test_path_is_file "foo bar" &&
1101 test_path_is_file foo &&
1102 test_path_is_file bar
1105 test_expect_success 'stash with pathspec matching multiple paths' '
1106 echo original >file &&
1107 echo original >other-file &&
1108 git commit -m "two" file other-file &&
1109 echo modified >file &&
1110 echo modified >other-file &&
1111 git stash push -- "*file" &&
1112 echo original >expect &&
1113 test_cmp expect file &&
1114 test_cmp expect other-file &&
1115 git stash pop &&
1116 echo modified >expect &&
1117 test_cmp expect file &&
1118 test_cmp expect other-file
1121 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1122 >foo &&
1123 git add foo &&
1124 git commit -m "tmp" &&
1125 git stash push -p foo >actual &&
1126 echo "No local changes to save" >expect &&
1127 git reset --hard HEAD~ &&
1128 test_cmp expect actual
1131 test_expect_success 'push <pathspec>: show no changes when there are none' '
1132 >foo &&
1133 git add foo &&
1134 git commit -m "tmp" &&
1135 git stash push foo >actual &&
1136 echo "No local changes to save" >expect &&
1137 git reset --hard HEAD~ &&
1138 test_cmp expect actual
1141 test_expect_success 'push: <pathspec> not in the repository errors out' '
1142 >untracked &&
1143 test_must_fail git stash push untracked &&
1144 test_path_is_file untracked
1147 test_expect_success 'push: -q is quiet with changes' '
1148 >foo &&
1149 git add foo &&
1150 git stash push -q >output 2>&1 &&
1151 test_must_be_empty output
1154 test_expect_success 'push: -q is quiet with no changes' '
1155 git stash push -q >output 2>&1 &&
1156 test_must_be_empty output
1159 test_expect_success 'push: -q is quiet even if there is no initial commit' '
1160 git init foo_dir &&
1161 test_when_finished rm -rf foo_dir &&
1163 cd foo_dir &&
1164 >bar &&
1165 test_must_fail git stash push -q >output 2>&1 &&
1166 test_must_be_empty output
1170 test_expect_success 'untracked files are left in place when -u is not given' '
1171 >file &&
1172 git add file &&
1173 >untracked &&
1174 git stash push file &&
1175 test_path_is_file untracked
1178 test_expect_success 'stash without verb with pathspec' '
1179 >"foo bar" &&
1180 >foo &&
1181 >bar &&
1182 git add foo* &&
1183 git stash -- "foo b*" &&
1184 test_path_is_missing "foo bar" &&
1185 test_path_is_file foo &&
1186 test_path_is_file bar &&
1187 git stash pop &&
1188 test_path_is_file "foo bar" &&
1189 test_path_is_file foo &&
1190 test_path_is_file bar
1193 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1194 git reset &&
1195 >foo &&
1196 >bar &&
1197 git add foo bar &&
1198 git commit -m "test" &&
1199 echo "foo" >foo &&
1200 echo "bar" >bar &&
1201 git stash -k -- foo &&
1202 test "",bar = $(cat foo),$(cat bar) &&
1203 git stash pop &&
1204 test foo,bar = $(cat foo),$(cat bar)
1207 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1208 git reset &&
1209 >subdir/untracked &&
1210 >subdir/tracked1 &&
1211 >subdir/tracked2 &&
1212 git add subdir/tracked* &&
1213 git stash -- subdir/ &&
1214 test_path_is_missing subdir/tracked1 &&
1215 test_path_is_missing subdir/tracked2 &&
1216 test_path_is_file subdir/untracked &&
1217 git stash pop &&
1218 test_path_is_file subdir/tracked1 &&
1219 test_path_is_file subdir/tracked2 &&
1220 test_path_is_file subdir/untracked
1223 test_expect_success 'stash -- <subdir> works with binary files' '
1224 git reset &&
1225 >subdir/untracked &&
1226 >subdir/tracked &&
1227 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1228 git add subdir/tracked* &&
1229 git stash -- subdir/ &&
1230 test_path_is_missing subdir/tracked &&
1231 test_path_is_missing subdir/tracked-binary &&
1232 test_path_is_file subdir/untracked &&
1233 git stash pop &&
1234 test_path_is_file subdir/tracked &&
1235 test_path_is_file subdir/tracked-binary &&
1236 test_path_is_file subdir/untracked
1239 test_expect_success 'stash with user.name and user.email set works' '
1240 test_config user.name "A U Thor" &&
1241 test_config user.email "a.u@thor" &&
1242 git stash
1245 test_expect_success 'stash works when user.name and user.email are not set' '
1246 git reset &&
1247 >1 &&
1248 git add 1 &&
1249 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1250 git stash &&
1251 git show -s --format="%an <%ae>" refs/stash >actual &&
1252 test_cmp expect actual &&
1253 >2 &&
1254 git add 2 &&
1255 test_config user.useconfigonly true &&
1256 test_config stash.usebuiltin true &&
1258 sane_unset GIT_AUTHOR_NAME &&
1259 sane_unset GIT_AUTHOR_EMAIL &&
1260 sane_unset GIT_COMMITTER_NAME &&
1261 sane_unset GIT_COMMITTER_EMAIL &&
1262 test_unconfig user.email &&
1263 test_unconfig user.name &&
1264 test_must_fail git commit -m "should fail" &&
1265 echo "git stash <git@stash>" >expect &&
1266 >2 &&
1267 git stash &&
1268 git show -s --format="%an <%ae>" refs/stash >actual &&
1269 test_cmp expect actual
1273 test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1274 test_commit to-remove to-remove &&
1275 git rm to-remove &&
1276 git stash --keep-index &&
1277 test_path_is_missing to-remove
1280 test_expect_success 'stash apply should succeed with unmodified file' '
1281 echo base >file &&
1282 git add file &&
1283 git commit -m base &&
1285 # now stash a modification
1286 echo modified >file &&
1287 git stash &&
1289 # make the file stat dirty
1290 cp file other &&
1291 mv other file &&
1293 git stash apply
1296 test_expect_success 'stash handles skip-worktree entries nicely' '
1297 test_commit A &&
1298 echo changed >A.t &&
1299 git add A.t &&
1300 git update-index --skip-worktree A.t &&
1301 rm A.t &&
1302 git stash &&
1304 git rev-parse --verify refs/stash:A.t
1307 test_expect_success 'stash -c stash.useBuiltin=false warning ' '
1308 expected="stash.useBuiltin support has been removed" &&
1310 git -c stash.useBuiltin=false stash 2>err &&
1311 test_i18ngrep "$expected" err &&
1312 env GIT_TEST_STASH_USE_BUILTIN=false git stash 2>err &&
1313 test_i18ngrep "$expected" err &&
1315 git -c stash.useBuiltin=true stash 2>err &&
1316 test_must_be_empty err &&
1317 env GIT_TEST_STASH_USE_BUILTIN=true git stash 2>err &&
1318 test_must_be_empty err
1321 test_expect_success 'git stash succeeds despite directory/file change' '
1322 test_create_repo directory_file_switch_v1 &&
1324 cd directory_file_switch_v1 &&
1325 test_commit init &&
1327 test_write_lines this file has some words >filler &&
1328 git add filler &&
1329 git commit -m filler &&
1331 git rm filler &&
1332 mkdir filler &&
1333 echo contents >filler/file &&
1334 git stash push
1338 test_expect_success 'git stash can pop file -> directory saved changes' '
1339 test_create_repo directory_file_switch_v2 &&
1341 cd directory_file_switch_v2 &&
1342 test_commit init &&
1344 test_write_lines this file has some words >filler &&
1345 git add filler &&
1346 git commit -m filler &&
1348 git rm filler &&
1349 mkdir filler &&
1350 echo contents >filler/file &&
1351 cp filler/file expect &&
1352 git stash push --include-untracked &&
1353 git stash apply --index &&
1354 test_cmp expect filler/file
1358 test_expect_success 'git stash can pop directory -> file saved changes' '
1359 test_create_repo directory_file_switch_v3 &&
1361 cd directory_file_switch_v3 &&
1362 test_commit init &&
1364 mkdir filler &&
1365 test_write_lines some words >filler/file1 &&
1366 test_write_lines and stuff >filler/file2 &&
1367 git add filler &&
1368 git commit -m filler &&
1370 git rm -rf filler &&
1371 echo contents >filler &&
1372 cp filler expect &&
1373 git stash push --include-untracked &&
1374 git stash apply --index &&
1375 test_cmp expect filler
1379 test_done