Merge branch 'ab/do-not-limit-stash-help-to-push'
[git/debian.git] / t / t3903-stash.sh
blobb17c52d8807a767cdac0bb35b645d9b076d2a9b2
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 test_expect_success 'stash some dirty working directory' '
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 cat >expect <<EOF
61 diff --git a/file b/file
62 index 0cfbf08..00750ed 100644
63 --- a/file
64 +++ b/file
65 @@ -1 +1 @@
68 EOF
70 test_expect_success 'parents of stash' '
71 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
72 git diff stash^2..stash >output &&
73 diff_cmp expect output
76 test_expect_success 'applying bogus stash does nothing' '
77 test_must_fail git stash apply stash@{1} &&
78 echo 1 >expect &&
79 test_cmp expect file
82 test_expect_success 'apply does not need clean working directory' '
83 echo 4 >other-file &&
84 git stash apply &&
85 echo 3 >expect &&
86 test_cmp expect file
89 test_expect_success 'apply does not clobber working directory changes' '
90 git reset --hard &&
91 echo 4 >file &&
92 test_must_fail git stash apply &&
93 echo 4 >expect &&
94 test_cmp expect file
97 test_expect_success 'apply stashed changes' '
98 git reset --hard &&
99 echo 5 >other-file &&
100 git add other-file &&
101 test_tick &&
102 git commit -m other-file &&
103 git stash apply &&
104 test 3 = $(cat file) &&
105 test 1 = $(git show :file) &&
106 test 1 = $(git show HEAD:file)
109 test_expect_success 'apply stashed changes (including index)' '
110 git reset --hard HEAD^ &&
111 echo 6 >other-file &&
112 git add other-file &&
113 test_tick &&
114 git commit -m other-file &&
115 git stash apply --index &&
116 test 3 = $(cat file) &&
117 test 2 = $(git show :file) &&
118 test 1 = $(git show HEAD:file)
121 test_expect_success 'unstashing in a subdirectory' '
122 git reset --hard HEAD &&
123 mkdir subdir &&
125 cd subdir &&
126 git stash apply
130 test_expect_success 'stash drop complains of extra options' '
131 test_must_fail git stash drop --foo
134 test_expect_success 'drop top stash' '
135 git reset --hard &&
136 git stash list >expected &&
137 echo 7 >file &&
138 git stash &&
139 git stash drop &&
140 git stash list >actual &&
141 test_cmp expected actual &&
142 git stash apply &&
143 test 3 = $(cat file) &&
144 test 1 = $(git show :file) &&
145 test 1 = $(git show HEAD:file)
148 test_expect_success 'drop middle stash' '
149 git reset --hard &&
150 echo 8 >file &&
151 git stash &&
152 echo 9 >file &&
153 git stash &&
154 git stash drop stash@{1} &&
155 test 2 = $(git stash list | wc -l) &&
156 git stash apply &&
157 test 9 = $(cat file) &&
158 test 1 = $(git show :file) &&
159 test 1 = $(git show HEAD:file) &&
160 git reset --hard &&
161 git stash drop &&
162 git stash apply &&
163 test 3 = $(cat file) &&
164 test 1 = $(git show :file) &&
165 test 1 = $(git show HEAD:file)
168 test_expect_success 'drop middle stash by index' '
169 git reset --hard &&
170 echo 8 >file &&
171 git stash &&
172 echo 9 >file &&
173 git stash &&
174 git stash drop 1 &&
175 test 2 = $(git stash list | wc -l) &&
176 git stash apply &&
177 test 9 = $(cat file) &&
178 test 1 = $(git show :file) &&
179 test 1 = $(git show HEAD:file) &&
180 git reset --hard &&
181 git stash drop &&
182 git stash apply &&
183 test 3 = $(cat file) &&
184 test 1 = $(git show :file) &&
185 test 1 = $(git show HEAD:file)
188 test_expect_success 'stash pop' '
189 git reset --hard &&
190 git stash pop &&
191 test 3 = $(cat file) &&
192 test 1 = $(git show :file) &&
193 test 1 = $(git show HEAD:file) &&
194 test 0 = $(git stash list | wc -l)
197 cat >expect <<EOF
198 diff --git a/file2 b/file2
199 new file mode 100644
200 index 0000000..1fe912c
201 --- /dev/null
202 +++ b/file2
203 @@ -0,0 +1 @@
204 +bar2
207 cat >expect1 <<EOF
208 diff --git a/file b/file
209 index 257cc56..5716ca5 100644
210 --- a/file
211 +++ b/file
212 @@ -1 +1 @@
213 -foo
214 +bar
217 cat >expect2 <<EOF
218 diff --git a/file b/file
219 index 7601807..5716ca5 100644
220 --- a/file
221 +++ b/file
222 @@ -1 +1 @@
223 -baz
224 +bar
225 diff --git a/file2 b/file2
226 new file mode 100644
227 index 0000000..1fe912c
228 --- /dev/null
229 +++ b/file2
230 @@ -0,0 +1 @@
231 +bar2
234 test_expect_success 'stash branch' '
235 echo foo >file &&
236 git commit file -m first &&
237 echo bar >file &&
238 echo bar2 >file2 &&
239 git add file2 &&
240 git stash &&
241 echo baz >file &&
242 git commit file -m second &&
243 git stash branch stashbranch &&
244 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
245 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
246 git diff --cached >output &&
247 diff_cmp expect output &&
248 git diff >output &&
249 diff_cmp expect1 output &&
250 git add file &&
251 git commit -m alternate\ second &&
252 git diff main..stashbranch >output &&
253 diff_cmp output expect2 &&
254 test 0 = $(git stash list | wc -l)
257 test_expect_success 'apply -q is quiet' '
258 echo foo >file &&
259 git stash &&
260 git stash apply -q >output.out 2>&1 &&
261 test_must_be_empty output.out
264 test_expect_success 'save -q is quiet' '
265 git stash save --quiet >output.out 2>&1 &&
266 test_must_be_empty output.out
269 test_expect_success 'pop -q works and is quiet' '
270 git stash pop -q >output.out 2>&1 &&
271 echo bar >expect &&
272 git show :file >actual &&
273 test_cmp expect actual &&
274 test_must_be_empty output.out
277 test_expect_success 'pop -q --index works and is quiet' '
278 echo foo >file &&
279 git add file &&
280 git stash save --quiet &&
281 git stash pop -q --index >output.out 2>&1 &&
282 git diff-files file2 >file2.diff &&
283 test_must_be_empty file2.diff &&
284 test foo = "$(git show :file)" &&
285 test_must_be_empty output.out
288 test_expect_success 'drop -q is quiet' '
289 git stash &&
290 git stash drop -q >output.out 2>&1 &&
291 test_must_be_empty output.out
294 test_expect_success 'stash -k' '
295 echo bar3 >file &&
296 echo bar4 >file2 &&
297 git add file2 &&
298 git stash -k &&
299 test bar,bar4 = $(cat file),$(cat file2)
302 test_expect_success 'stash --no-keep-index' '
303 echo bar33 >file &&
304 echo bar44 >file2 &&
305 git add file2 &&
306 git stash --no-keep-index &&
307 test bar,bar2 = $(cat file),$(cat file2)
310 test_expect_success 'stash --staged' '
311 echo bar3 >file &&
312 echo bar4 >file2 &&
313 git add file2 &&
314 git stash --staged &&
315 test bar3,bar2 = $(cat file),$(cat file2) &&
316 git reset --hard &&
317 git stash pop &&
318 test bar,bar4 = $(cat file),$(cat file2)
321 test_expect_success 'dont assume push with non-option args' '
322 test_must_fail git stash -q drop 2>err &&
323 test_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
326 test_expect_success 'stash --invalid-option' '
327 echo bar5 >file &&
328 echo bar6 >file2 &&
329 git add file2 &&
330 test_must_fail git stash --invalid-option &&
331 test_must_fail git stash save --invalid-option &&
332 test bar5,bar6 = $(cat file),$(cat file2)
335 test_expect_success 'stash an added file' '
336 git reset --hard &&
337 echo new >file3 &&
338 git add file3 &&
339 git stash save "added file" &&
340 ! test -r file3 &&
341 git stash apply &&
342 test new = "$(cat file3)"
345 test_expect_success 'stash --intent-to-add file' '
346 git reset --hard &&
347 echo new >file4 &&
348 git add --intent-to-add file4 &&
349 test_when_finished "git rm -f file4" &&
350 test_must_fail git stash
353 test_expect_success 'stash rm then recreate' '
354 git reset --hard &&
355 git rm file &&
356 echo bar7 >file &&
357 git stash save "rm then recreate" &&
358 test bar = "$(cat file)" &&
359 git stash apply &&
360 test bar7 = "$(cat file)"
363 test_expect_success 'stash rm and ignore' '
364 git reset --hard &&
365 git rm file &&
366 echo file >.gitignore &&
367 git stash save "rm and ignore" &&
368 test bar = "$(cat file)" &&
369 test file = "$(cat .gitignore)" &&
370 git stash apply &&
371 ! test -r file &&
372 test file = "$(cat .gitignore)"
375 test_expect_success 'stash rm and ignore (stage .gitignore)' '
376 git reset --hard &&
377 git rm file &&
378 echo file >.gitignore &&
379 git add .gitignore &&
380 git stash save "rm and ignore (stage .gitignore)" &&
381 test bar = "$(cat file)" &&
382 ! test -r .gitignore &&
383 git stash apply &&
384 ! test -r file &&
385 test file = "$(cat .gitignore)"
388 test_expect_success SYMLINKS 'stash file to symlink' '
389 git reset --hard &&
390 rm file &&
391 ln -s file2 file &&
392 git stash save "file to symlink" &&
393 test -f file &&
394 test bar = "$(cat file)" &&
395 git stash apply &&
396 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
399 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
400 git reset --hard &&
401 git rm file &&
402 ln -s file2 file &&
403 git stash save "file to symlink (stage rm)" &&
404 test -f file &&
405 test bar = "$(cat file)" &&
406 git stash apply &&
407 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
410 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
411 git reset --hard &&
412 rm file &&
413 ln -s file2 file &&
414 git add file &&
415 git stash save "file to symlink (full stage)" &&
416 test -f file &&
417 test bar = "$(cat file)" &&
418 git stash apply &&
419 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
422 # This test creates a commit with a symlink used for the following tests
424 test_expect_success 'stash symlink to file' '
425 git reset --hard &&
426 test_ln_s_add file filelink &&
427 git commit -m "Add symlink" &&
428 rm filelink &&
429 cp file filelink &&
430 git stash save "symlink to file"
433 test_expect_success SYMLINKS 'this must have re-created the symlink' '
434 test -h filelink &&
435 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
438 test_expect_success 'unstash must re-create the file' '
439 git stash apply &&
440 ! test -h filelink &&
441 test bar = "$(cat file)"
444 test_expect_success 'stash symlink to file (stage rm)' '
445 git reset --hard &&
446 git rm filelink &&
447 cp file filelink &&
448 git stash save "symlink to file (stage rm)"
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_success 'stash symlink to file (full stage)' '
463 git reset --hard &&
464 rm filelink &&
465 cp file filelink &&
466 git add filelink &&
467 git stash save "symlink to file (full stage)"
470 test_expect_success SYMLINKS 'this must have re-created the symlink' '
471 test -h filelink &&
472 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
475 test_expect_success 'unstash must re-create the file' '
476 git stash apply &&
477 ! test -h filelink &&
478 test bar = "$(cat file)"
481 test_expect_failure 'stash directory to file' '
482 git reset --hard &&
483 mkdir dir &&
484 echo foo >dir/file &&
485 git add dir/file &&
486 git commit -m "Add file in dir" &&
487 rm -fr dir &&
488 echo bar >dir &&
489 git stash save "directory to file" &&
490 test -d dir &&
491 test foo = "$(cat dir/file)" &&
492 test_must_fail git stash apply &&
493 test bar = "$(cat dir)" &&
494 git reset --soft HEAD^
497 test_expect_failure 'stash file to directory' '
498 git reset --hard &&
499 rm file &&
500 mkdir file &&
501 echo foo >file/file &&
502 git stash save "file to directory" &&
503 test -f file &&
504 test bar = "$(cat file)" &&
505 git stash apply &&
506 test -f file/file &&
507 test foo = "$(cat file/file)"
510 test_expect_success 'giving too many ref arguments does not modify files' '
511 git stash clear &&
512 test_when_finished "git reset --hard HEAD" &&
513 echo foo >file2 &&
514 git stash &&
515 echo bar >file2 &&
516 git stash &&
517 test-tool chmtime =123456789 file2 &&
518 for type in apply pop "branch stash-branch"
520 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
521 test_i18ngrep "Too many revisions" err &&
522 test 123456789 = $(test-tool chmtime -g file2) || return 1
523 done
526 test_expect_success 'drop: too many arguments errors out (does nothing)' '
527 git stash list >expect &&
528 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
529 test_i18ngrep "Too many revisions" err &&
530 git stash list >actual &&
531 test_cmp expect actual
534 test_expect_success 'show: too many arguments errors out (does nothing)' '
535 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
536 test_i18ngrep "Too many revisions" err &&
537 test_must_be_empty out
540 test_expect_success 'stash create - no changes' '
541 git stash clear &&
542 test_when_finished "git reset --hard HEAD" &&
543 git reset --hard &&
544 git stash create >actual &&
545 test_must_be_empty actual
548 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
549 git stash clear &&
550 test_when_finished "git reset --hard HEAD" &&
551 git reset --hard &&
552 echo foo >>file &&
553 STASH_ID=$(git stash create) &&
554 git reset --hard &&
555 git stash branch stash-branch ${STASH_ID} &&
556 test_when_finished "git reset --hard HEAD && git checkout main &&
557 git branch -D stash-branch" &&
558 test $(git ls-files --modified | wc -l) -eq 1
561 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
562 git stash clear &&
563 test_when_finished "git reset --hard HEAD" &&
564 git reset --hard &&
565 echo foo >>file &&
566 git stash &&
567 test_when_finished "git stash drop" &&
568 echo bar >>file &&
569 STASH_ID=$(git stash create) &&
570 git reset --hard &&
571 git stash branch stash-branch ${STASH_ID} &&
572 test_when_finished "git reset --hard HEAD && git checkout main &&
573 git branch -D stash-branch" &&
574 test $(git ls-files --modified | wc -l) -eq 1
577 test_expect_success 'stash branch complains with no arguments' '
578 test_must_fail git stash branch 2>err &&
579 test_i18ngrep "No branch name specified" err
582 test_expect_success 'stash show format defaults to --stat' '
583 git stash clear &&
584 test_when_finished "git reset --hard HEAD" &&
585 git reset --hard &&
586 echo foo >>file &&
587 git stash &&
588 test_when_finished "git stash drop" &&
589 echo bar >>file &&
590 STASH_ID=$(git stash create) &&
591 git reset --hard &&
592 cat >expected <<-EOF &&
593 file | 1 +
594 1 file changed, 1 insertion(+)
596 git stash show ${STASH_ID} >actual &&
597 test_cmp expected actual
600 test_expect_success 'stash show - stashes on stack, stash-like argument' '
601 git stash clear &&
602 test_when_finished "git reset --hard HEAD" &&
603 git reset --hard &&
604 echo foo >>file &&
605 git stash &&
606 test_when_finished "git stash drop" &&
607 echo bar >>file &&
608 STASH_ID=$(git stash create) &&
609 git reset --hard &&
610 echo "1 0 file" >expected &&
611 git stash show --numstat ${STASH_ID} >actual &&
612 test_cmp expected actual
615 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
616 git stash clear &&
617 test_when_finished "git reset --hard HEAD" &&
618 git reset --hard &&
619 echo foo >>file &&
620 git stash &&
621 test_when_finished "git stash drop" &&
622 echo bar >>file &&
623 STASH_ID=$(git stash create) &&
624 git reset --hard &&
625 cat >expected <<-EOF &&
626 diff --git a/file b/file
627 index 7601807..935fbd3 100644
628 --- a/file
629 +++ b/file
630 @@ -1 +1,2 @@
632 +bar
634 git stash show -p ${STASH_ID} >actual &&
635 diff_cmp expected actual
638 test_expect_success 'stash show - no 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 STASH_ID=$(git stash create) &&
644 git reset --hard &&
645 echo "1 0 file" >expected &&
646 git stash show --numstat ${STASH_ID} >actual &&
647 test_cmp expected actual
650 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
651 git stash clear &&
652 test_when_finished "git reset --hard HEAD" &&
653 git reset --hard &&
654 echo foo >>file &&
655 STASH_ID=$(git stash create) &&
656 git reset --hard &&
657 cat >expected <<-EOF &&
658 diff --git a/file b/file
659 index 7601807..71b52c4 100644
660 --- a/file
661 +++ b/file
662 @@ -1 +1,2 @@
664 +foo
666 git stash show -p ${STASH_ID} >actual &&
667 diff_cmp expected actual
670 test_expect_success 'stash show --patience shows diff' '
671 git reset --hard &&
672 echo foo >>file &&
673 STASH_ID=$(git stash create) &&
674 git reset --hard &&
675 cat >expected <<-EOF &&
676 diff --git a/file b/file
677 index 7601807..71b52c4 100644
678 --- a/file
679 +++ b/file
680 @@ -1 +1,2 @@
682 +foo
684 git stash show --patience ${STASH_ID} >actual &&
685 diff_cmp expected actual
688 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
689 git stash clear &&
690 test_when_finished "git reset --hard HEAD && git stash clear" &&
691 git reset --hard &&
692 echo foo >file &&
693 git stash &&
694 echo bar >file &&
695 git stash &&
696 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
697 git stash pop &&
698 test bar = "$(cat file)" &&
699 git reset --hard HEAD
702 test_expect_success 'pop: fail early if specified stash is not a stash ref' '
703 git stash clear &&
704 test_when_finished "git reset --hard HEAD && git stash clear" &&
705 git reset --hard &&
706 echo foo >file &&
707 git stash &&
708 echo bar >file &&
709 git stash &&
710 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
711 git stash pop &&
712 test bar = "$(cat file)" &&
713 git reset --hard HEAD
716 test_expect_success 'ref with non-existent reflog' '
717 git stash clear &&
718 echo bar5 >file &&
719 echo bar6 >file2 &&
720 git add file2 &&
721 git stash &&
722 test_must_fail git rev-parse --quiet --verify does-not-exist &&
723 test_must_fail git stash drop does-not-exist &&
724 test_must_fail git stash drop does-not-exist@{0} &&
725 test_must_fail git stash pop does-not-exist &&
726 test_must_fail git stash pop does-not-exist@{0} &&
727 test_must_fail git stash apply does-not-exist &&
728 test_must_fail git stash apply does-not-exist@{0} &&
729 test_must_fail git stash show does-not-exist &&
730 test_must_fail git stash show does-not-exist@{0} &&
731 test_must_fail git stash branch tmp does-not-exist &&
732 test_must_fail git stash branch tmp does-not-exist@{0} &&
733 git stash drop
736 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
737 git stash clear &&
738 test_must_fail git stash drop stash@{0} &&
739 echo bar5 >file &&
740 echo bar6 >file2 &&
741 git add file2 &&
742 git stash &&
743 test_must_fail git stash drop stash@{1} &&
744 test_must_fail git stash pop stash@{1} &&
745 test_must_fail git stash apply stash@{1} &&
746 test_must_fail git stash show stash@{1} &&
747 test_must_fail git stash branch tmp stash@{1} &&
748 git stash drop
751 test_expect_success 'invalid ref of the form "n", n >= N' '
752 git stash clear &&
753 test_must_fail git stash drop 0 &&
754 echo bar5 >file &&
755 echo bar6 >file2 &&
756 git add file2 &&
757 git stash &&
758 test_must_fail git stash drop 1 &&
759 test_must_fail git stash pop 1 &&
760 test_must_fail git stash apply 1 &&
761 test_must_fail git stash show 1 &&
762 test_must_fail git stash branch tmp 1 &&
763 git stash drop
766 test_expect_success 'valid ref of the form "n", n < N' '
767 git stash clear &&
768 echo bar5 >file &&
769 echo bar6 >file2 &&
770 git add file2 &&
771 git stash &&
772 git stash show 0 &&
773 git stash branch tmp 0 &&
774 git checkout main &&
775 git stash &&
776 git stash apply 0 &&
777 git reset --hard &&
778 git stash pop 0 &&
779 git stash &&
780 git stash drop 0 &&
781 test_must_fail git stash drop
784 test_expect_success 'branch: do not drop the stash if the branch exists' '
785 git stash clear &&
786 echo foo >file &&
787 git add file &&
788 git commit -m initial &&
789 echo bar >file &&
790 git stash &&
791 test_must_fail git stash branch main stash@{0} &&
792 git rev-parse stash@{0} --
795 test_expect_success 'branch: should not drop the stash if the apply fails' '
796 git stash clear &&
797 git reset HEAD~1 --hard &&
798 echo foo >file &&
799 git add file &&
800 git commit -m initial &&
801 echo bar >file &&
802 git stash &&
803 echo baz >file &&
804 test_when_finished "git checkout main" &&
805 test_must_fail git stash branch new_branch stash@{0} &&
806 git rev-parse stash@{0} --
809 test_expect_success 'apply: show same status as git status (relative to ./)' '
810 git stash clear &&
811 echo 1 >subdir/subfile1 &&
812 echo 2 >subdir/subfile2 &&
813 git add subdir/subfile1 &&
814 git commit -m subdir &&
816 cd subdir &&
817 echo x >subfile1 &&
818 echo x >../file &&
819 git status >../expect &&
820 git stash &&
821 sane_unset GIT_MERGE_VERBOSITY &&
822 git stash apply
824 sed -e 1d >actual && # drop "Saved..."
825 test_cmp expect actual
828 cat >expect <<EOF
829 diff --git a/HEAD b/HEAD
830 new file mode 100644
831 index 0000000..fe0cbee
832 --- /dev/null
833 +++ b/HEAD
834 @@ -0,0 +1 @@
835 +file-not-a-ref
838 test_expect_success 'stash where working directory contains "HEAD" file' '
839 git stash clear &&
840 git reset --hard &&
841 echo file-not-a-ref >HEAD &&
842 git add HEAD &&
843 test_tick &&
844 git stash &&
845 git diff-files --quiet &&
846 git diff-index --cached --quiet HEAD &&
847 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
848 git diff stash^..stash >output &&
849 diff_cmp expect output
852 test_expect_success 'store called with invalid commit' '
853 test_must_fail git stash store foo
856 test_expect_success 'store updates stash ref and reflog' '
857 git stash clear &&
858 git reset --hard &&
859 echo quux >bazzy &&
860 git add bazzy &&
861 STASH_ID=$(git stash create) &&
862 git reset --hard &&
863 test_path_is_missing bazzy &&
864 git stash store -m quuxery $STASH_ID &&
865 test $(git rev-parse stash) = $STASH_ID &&
866 git reflog --format=%H stash| grep $STASH_ID &&
867 git stash pop &&
868 grep quux bazzy
871 test_expect_success 'handle stash specification with spaces' '
872 git stash clear &&
873 echo pig >file &&
874 git stash &&
875 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
876 test_tick &&
877 echo cow >file &&
878 git stash &&
879 git stash apply "stash@{$stamp}" &&
880 grep pig file
883 test_expect_success 'setup stash with index and worktree changes' '
884 git stash clear &&
885 git reset --hard &&
886 echo index >file &&
887 git add file &&
888 echo working >file &&
889 git stash
892 test_expect_success 'stash list -p shows simple diff' '
893 cat >expect <<-EOF &&
894 stash@{0}
896 diff --git a/file b/file
897 index 257cc56..d26b33d 100644
898 --- a/file
899 +++ b/file
900 @@ -1 +1 @@
901 -foo
902 +working
904 git stash list --format=%gd -p >actual &&
905 diff_cmp expect actual
908 test_expect_success 'stash list --cc shows combined diff' '
909 cat >expect <<-\EOF &&
910 stash@{0}
912 diff --cc file
913 index 257cc56,9015a7a..d26b33d
914 --- a/file
915 +++ b/file
916 @@@ -1,1 -1,1 +1,1 @@@
917 - foo
918 -index
919 ++working
921 git stash list --format=%gd -p --cc >actual &&
922 diff_cmp expect actual
925 test_expect_success 'stash is not confused by partial renames' '
926 mv file renamed &&
927 git add renamed &&
928 git stash &&
929 git stash apply &&
930 test_path_is_file renamed &&
931 test_path_is_missing file
934 test_expect_success 'push -m shows right message' '
935 >foo &&
936 git add foo &&
937 git stash push -m "test message" &&
938 echo "stash@{0}: On main: test message" >expect &&
939 git stash list -1 >actual &&
940 test_cmp expect actual
943 test_expect_success 'push -m also works without space' '
944 >foo &&
945 git add foo &&
946 git stash push -m"unspaced test message" &&
947 echo "stash@{0}: On main: unspaced test message" >expect &&
948 git stash list -1 >actual &&
949 test_cmp expect actual
952 test_expect_success 'store -m foo shows right message' '
953 git stash clear &&
954 git reset --hard &&
955 echo quux >bazzy &&
956 git add bazzy &&
957 STASH_ID=$(git stash create) &&
958 git stash store -m "store m" $STASH_ID &&
959 echo "stash@{0}: store m" >expect &&
960 git stash list -1 >actual &&
961 test_cmp expect actual
964 test_expect_success 'store -mfoo shows right message' '
965 git stash clear &&
966 git reset --hard &&
967 echo quux >bazzy &&
968 git add bazzy &&
969 STASH_ID=$(git stash create) &&
970 git stash store -m"store mfoo" $STASH_ID &&
971 echo "stash@{0}: store mfoo" >expect &&
972 git stash list -1 >actual &&
973 test_cmp expect actual
976 test_expect_success 'store --message=foo shows right message' '
977 git stash clear &&
978 git reset --hard &&
979 echo quux >bazzy &&
980 git add bazzy &&
981 STASH_ID=$(git stash create) &&
982 git stash store --message="store message=foo" $STASH_ID &&
983 echo "stash@{0}: store message=foo" >expect &&
984 git stash list -1 >actual &&
985 test_cmp expect actual
988 test_expect_success 'store --message foo shows right message' '
989 git stash clear &&
990 git reset --hard &&
991 echo quux >bazzy &&
992 git add bazzy &&
993 STASH_ID=$(git stash create) &&
994 git stash store --message "store message foo" $STASH_ID &&
995 echo "stash@{0}: store message foo" >expect &&
996 git stash list -1 >actual &&
997 test_cmp expect actual
1000 test_expect_success 'push -mfoo uses right message' '
1001 >foo &&
1002 git add foo &&
1003 git stash push -m"test mfoo" &&
1004 echo "stash@{0}: On main: test mfoo" >expect &&
1005 git stash list -1 >actual &&
1006 test_cmp expect actual
1009 test_expect_success 'push --message foo is synonym for -mfoo' '
1010 >foo &&
1011 git add foo &&
1012 git stash push --message "test message foo" &&
1013 echo "stash@{0}: On main: test message foo" >expect &&
1014 git stash list -1 >actual &&
1015 test_cmp expect actual
1018 test_expect_success 'push --message=foo is synonym for -mfoo' '
1019 >foo &&
1020 git add foo &&
1021 git stash push --message="test message=foo" &&
1022 echo "stash@{0}: On main: test message=foo" >expect &&
1023 git stash list -1 >actual &&
1024 test_cmp expect actual
1027 test_expect_success 'push -m shows right message' '
1028 >foo &&
1029 git add foo &&
1030 git stash push -m "test m foo" &&
1031 echo "stash@{0}: On main: test m foo" >expect &&
1032 git stash list -1 >actual &&
1033 test_cmp expect actual
1036 test_expect_success 'create stores correct message' '
1037 >foo &&
1038 git add foo &&
1039 STASH_ID=$(git stash create "create test message") &&
1040 echo "On main: create test message" >expect &&
1041 git show --pretty=%s -s ${STASH_ID} >actual &&
1042 test_cmp expect actual
1045 test_expect_success 'create with multiple arguments for the message' '
1046 >foo &&
1047 git add foo &&
1048 STASH_ID=$(git stash create test untracked) &&
1049 echo "On main: test untracked" >expect &&
1050 git show --pretty=%s -s ${STASH_ID} >actual &&
1051 test_cmp expect actual
1054 test_expect_success 'create in a detached state' '
1055 test_when_finished "git checkout main" &&
1056 git checkout HEAD~1 &&
1057 >foo &&
1058 git add foo &&
1059 STASH_ID=$(git stash create) &&
1060 HEAD_ID=$(git rev-parse --short HEAD) &&
1061 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1062 git show --pretty=%s -s ${STASH_ID} >actual &&
1063 test_cmp expect actual
1066 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1067 >foo &&
1068 >bar &&
1069 git add foo bar &&
1070 git stash push -- foo &&
1071 test_path_is_file bar &&
1072 test_path_is_missing foo &&
1073 git stash pop &&
1074 test_path_is_file foo &&
1075 test_path_is_file bar
1078 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1079 mkdir sub &&
1080 >foo &&
1081 >bar &&
1082 git add foo bar &&
1084 cd sub &&
1085 git stash push -- ../foo
1086 ) &&
1087 test_path_is_file bar &&
1088 test_path_is_missing foo &&
1089 git stash pop &&
1090 test_path_is_file foo &&
1091 test_path_is_file bar
1094 test_expect_success 'stash with multiple pathspec arguments' '
1095 >foo &&
1096 >bar &&
1097 >extra &&
1098 git add foo bar extra &&
1099 git stash push -- foo bar &&
1100 test_path_is_missing bar &&
1101 test_path_is_missing foo &&
1102 test_path_is_file extra &&
1103 git stash pop &&
1104 test_path_is_file foo &&
1105 test_path_is_file bar &&
1106 test_path_is_file extra
1109 test_expect_success 'stash with file including $IFS character' '
1110 >"foo bar" &&
1111 >foo &&
1112 >bar &&
1113 git add foo* &&
1114 git stash push -- "foo b*" &&
1115 test_path_is_missing "foo bar" &&
1116 test_path_is_file foo &&
1117 test_path_is_file bar &&
1118 git stash pop &&
1119 test_path_is_file "foo bar" &&
1120 test_path_is_file foo &&
1121 test_path_is_file bar
1124 test_expect_success 'stash with pathspec matching multiple paths' '
1125 echo original >file &&
1126 echo original >other-file &&
1127 git commit -m "two" file other-file &&
1128 echo modified >file &&
1129 echo modified >other-file &&
1130 git stash push -- "*file" &&
1131 echo original >expect &&
1132 test_cmp expect file &&
1133 test_cmp expect other-file &&
1134 git stash pop &&
1135 echo modified >expect &&
1136 test_cmp expect file &&
1137 test_cmp expect other-file
1140 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1141 >foo &&
1142 git add foo &&
1143 git commit -m "tmp" &&
1144 git stash push -p foo >actual &&
1145 echo "No local changes to save" >expect &&
1146 git reset --hard HEAD~ &&
1147 test_cmp expect actual
1150 test_expect_success 'push <pathspec>: show no changes when there are none' '
1151 >foo &&
1152 git add foo &&
1153 git commit -m "tmp" &&
1154 git stash push foo >actual &&
1155 echo "No local changes to save" >expect &&
1156 git reset --hard HEAD~ &&
1157 test_cmp expect actual
1160 test_expect_success 'push: <pathspec> not in the repository errors out' '
1161 >untracked &&
1162 test_must_fail git stash push untracked &&
1163 test_path_is_file untracked
1166 test_expect_success 'push: -q is quiet with changes' '
1167 >foo &&
1168 git add foo &&
1169 git stash push -q >output 2>&1 &&
1170 test_must_be_empty output
1173 test_expect_success 'push: -q is quiet with no changes' '
1174 git stash push -q >output 2>&1 &&
1175 test_must_be_empty output
1178 test_expect_success 'push: -q is quiet even if there is no initial commit' '
1179 git init foo_dir &&
1180 test_when_finished rm -rf foo_dir &&
1182 cd foo_dir &&
1183 >bar &&
1184 test_must_fail git stash push -q >output 2>&1 &&
1185 test_must_be_empty output
1189 test_expect_success 'untracked files are left in place when -u is not given' '
1190 >file &&
1191 git add file &&
1192 >untracked &&
1193 git stash push file &&
1194 test_path_is_file untracked
1197 test_expect_success 'stash without verb with pathspec' '
1198 >"foo bar" &&
1199 >foo &&
1200 >bar &&
1201 git add foo* &&
1202 git stash -- "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 -k -- <pathspec> leaves unstaged files intact' '
1213 git reset &&
1214 >foo &&
1215 >bar &&
1216 git add foo bar &&
1217 git commit -m "test" &&
1218 echo "foo" >foo &&
1219 echo "bar" >bar &&
1220 git stash -k -- foo &&
1221 test "",bar = $(cat foo),$(cat bar) &&
1222 git stash pop &&
1223 test foo,bar = $(cat foo),$(cat bar)
1226 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1227 git reset &&
1228 >subdir/untracked &&
1229 >subdir/tracked1 &&
1230 >subdir/tracked2 &&
1231 git add subdir/tracked* &&
1232 git stash -- subdir/ &&
1233 test_path_is_missing subdir/tracked1 &&
1234 test_path_is_missing subdir/tracked2 &&
1235 test_path_is_file subdir/untracked &&
1236 git stash pop &&
1237 test_path_is_file subdir/tracked1 &&
1238 test_path_is_file subdir/tracked2 &&
1239 test_path_is_file subdir/untracked
1242 test_expect_success 'stash -- <subdir> works with binary files' '
1243 git reset &&
1244 >subdir/untracked &&
1245 >subdir/tracked &&
1246 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1247 git add subdir/tracked* &&
1248 git stash -- subdir/ &&
1249 test_path_is_missing subdir/tracked &&
1250 test_path_is_missing subdir/tracked-binary &&
1251 test_path_is_file subdir/untracked &&
1252 git stash pop &&
1253 test_path_is_file subdir/tracked &&
1254 test_path_is_file subdir/tracked-binary &&
1255 test_path_is_file subdir/untracked
1258 test_expect_success 'stash with user.name and user.email set works' '
1259 test_config user.name "A U Thor" &&
1260 test_config user.email "a.u@thor" &&
1261 git stash
1264 test_expect_success 'stash works when user.name and user.email are not set' '
1265 git reset &&
1266 >1 &&
1267 git add 1 &&
1268 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1269 git stash &&
1270 git show -s --format="%an <%ae>" refs/stash >actual &&
1271 test_cmp expect actual &&
1272 >2 &&
1273 git add 2 &&
1274 test_config user.useconfigonly true &&
1275 test_config stash.usebuiltin true &&
1277 sane_unset GIT_AUTHOR_NAME &&
1278 sane_unset GIT_AUTHOR_EMAIL &&
1279 sane_unset GIT_COMMITTER_NAME &&
1280 sane_unset GIT_COMMITTER_EMAIL &&
1281 test_unconfig user.email &&
1282 test_unconfig user.name &&
1283 test_must_fail git commit -m "should fail" &&
1284 echo "git stash <git@stash>" >expect &&
1285 >2 &&
1286 git stash &&
1287 git show -s --format="%an <%ae>" refs/stash >actual &&
1288 test_cmp expect actual
1292 test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1293 test_commit to-remove to-remove &&
1294 git rm to-remove &&
1295 git stash --keep-index &&
1296 test_path_is_missing to-remove
1299 test_expect_success 'stash apply should succeed with unmodified file' '
1300 echo base >file &&
1301 git add file &&
1302 git commit -m base &&
1304 # now stash a modification
1305 echo modified >file &&
1306 git stash &&
1308 # make the file stat dirty
1309 cp file other &&
1310 mv other file &&
1312 git stash apply
1315 test_expect_success 'stash handles skip-worktree entries nicely' '
1316 test_commit A &&
1317 echo changed >A.t &&
1318 git add A.t &&
1319 git update-index --skip-worktree A.t &&
1320 rm A.t &&
1321 git stash &&
1323 git rev-parse --verify refs/stash:A.t
1326 test_expect_success 'stash -c stash.useBuiltin=false warning ' '
1327 expected="stash.useBuiltin support has been removed" &&
1329 git -c stash.useBuiltin=false stash 2>err &&
1330 test_i18ngrep "$expected" err &&
1331 env GIT_TEST_STASH_USE_BUILTIN=false git stash 2>err &&
1332 test_i18ngrep "$expected" err &&
1334 git -c stash.useBuiltin=true stash 2>err &&
1335 test_must_be_empty err &&
1336 env GIT_TEST_STASH_USE_BUILTIN=true git stash 2>err &&
1337 test_must_be_empty err
1340 test_expect_success 'git stash succeeds despite directory/file change' '
1341 test_create_repo directory_file_switch_v1 &&
1343 cd directory_file_switch_v1 &&
1344 test_commit init &&
1346 test_write_lines this file has some words >filler &&
1347 git add filler &&
1348 git commit -m filler &&
1350 git rm filler &&
1351 mkdir filler &&
1352 echo contents >filler/file &&
1353 git stash push
1357 test_expect_success 'git stash can pop file -> directory saved changes' '
1358 test_create_repo directory_file_switch_v2 &&
1360 cd directory_file_switch_v2 &&
1361 test_commit init &&
1363 test_write_lines this file has some words >filler &&
1364 git add filler &&
1365 git commit -m filler &&
1367 git rm filler &&
1368 mkdir filler &&
1369 echo contents >filler/file &&
1370 cp filler/file expect &&
1371 git stash push --include-untracked &&
1372 git stash apply --index &&
1373 test_cmp expect filler/file
1377 test_expect_success 'git stash can pop directory -> file saved changes' '
1378 test_create_repo directory_file_switch_v3 &&
1380 cd directory_file_switch_v3 &&
1381 test_commit init &&
1383 mkdir filler &&
1384 test_write_lines some words >filler/file1 &&
1385 test_write_lines and stuff >filler/file2 &&
1386 git add filler &&
1387 git commit -m filler &&
1389 git rm -rf filler &&
1390 echo contents >filler &&
1391 cp filler expect &&
1392 git stash push --include-untracked &&
1393 git stash apply --index &&
1394 test_cmp expect filler
1398 test_done