add DUP_ARRAY
[git.git] / t / t3903-stash.sh
blob376cc8f4ab8429b0488ad23b0f9731c9af237124
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 REFFILES '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 $(test_oid zero) $old_oid
217 $old_oid $new_oid
219 cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
220 test_cmp expect actual &&
222 git -C repo stash drop stash@{1} &&
223 cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
224 cat >expect <<-EOF &&
225 $(test_oid zero) $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_i18ngrep -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_i18ngrep "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_i18ngrep "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_i18ngrep "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_i18ngrep "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 updates stash ref and reflog' '
935 git stash clear &&
936 git reset --hard &&
937 echo quux >bazzy &&
938 git add bazzy &&
939 STASH_ID=$(git stash create) &&
940 git reset --hard &&
941 test_path_is_missing bazzy &&
942 git stash store -m quuxery $STASH_ID &&
943 test $(git rev-parse stash) = $STASH_ID &&
944 git reflog --format=%H stash| grep $STASH_ID &&
945 git stash pop &&
946 grep quux bazzy
949 test_expect_success 'handle stash specification with spaces' '
950 git stash clear &&
951 echo pig >file &&
952 git stash &&
953 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
954 test_tick &&
955 echo cow >file &&
956 git stash &&
957 git stash apply "stash@{$stamp}" &&
958 grep pig file
961 test_expect_success 'setup stash with index and worktree changes' '
962 git stash clear &&
963 git reset --hard &&
964 echo index >file &&
965 git add file &&
966 echo working >file &&
967 git stash
970 test_expect_success 'stash list -p shows simple diff' '
971 cat >expect <<-EOF &&
972 stash@{0}
974 diff --git a/file b/file
975 index 257cc56..d26b33d 100644
976 --- a/file
977 +++ b/file
978 @@ -1 +1 @@
979 -foo
980 +working
982 git stash list --format=%gd -p >actual &&
983 diff_cmp expect actual
986 test_expect_success 'stash list --cc shows combined diff' '
987 cat >expect <<-\EOF &&
988 stash@{0}
990 diff --cc file
991 index 257cc56,9015a7a..d26b33d
992 --- a/file
993 +++ b/file
994 @@@ -1,1 -1,1 +1,1 @@@
995 - foo
996 -index
997 ++working
999 git stash list --format=%gd -p --cc >actual &&
1000 diff_cmp expect actual
1003 test_expect_success 'stash is not confused by partial renames' '
1004 mv file renamed &&
1005 git add renamed &&
1006 git stash &&
1007 git stash apply &&
1008 test_path_is_file renamed &&
1009 test_path_is_missing file
1012 test_expect_success 'push -m shows right message' '
1013 >foo &&
1014 git add foo &&
1015 git stash push -m "test message" &&
1016 echo "stash@{0}: On main: test message" >expect &&
1017 git stash list -1 >actual &&
1018 test_cmp expect actual
1021 test_expect_success 'push -m also works without space' '
1022 >foo &&
1023 git add foo &&
1024 git stash push -m"unspaced test message" &&
1025 echo "stash@{0}: On main: unspaced test message" >expect &&
1026 git stash list -1 >actual &&
1027 test_cmp expect actual
1030 test_expect_success 'store -m foo shows right message' '
1031 git stash clear &&
1032 git reset --hard &&
1033 echo quux >bazzy &&
1034 git add bazzy &&
1035 STASH_ID=$(git stash create) &&
1036 git stash store -m "store m" $STASH_ID &&
1037 echo "stash@{0}: store m" >expect &&
1038 git stash list -1 >actual &&
1039 test_cmp expect actual
1042 test_expect_success 'store -mfoo shows right message' '
1043 git stash clear &&
1044 git reset --hard &&
1045 echo quux >bazzy &&
1046 git add bazzy &&
1047 STASH_ID=$(git stash create) &&
1048 git stash store -m"store mfoo" $STASH_ID &&
1049 echo "stash@{0}: store mfoo" >expect &&
1050 git stash list -1 >actual &&
1051 test_cmp expect actual
1054 test_expect_success 'store --message=foo shows right message' '
1055 git stash clear &&
1056 git reset --hard &&
1057 echo quux >bazzy &&
1058 git add bazzy &&
1059 STASH_ID=$(git stash create) &&
1060 git stash store --message="store message=foo" $STASH_ID &&
1061 echo "stash@{0}: store message=foo" >expect &&
1062 git stash list -1 >actual &&
1063 test_cmp expect actual
1066 test_expect_success 'store --message foo shows right message' '
1067 git stash clear &&
1068 git reset --hard &&
1069 echo quux >bazzy &&
1070 git add bazzy &&
1071 STASH_ID=$(git stash create) &&
1072 git stash store --message "store message foo" $STASH_ID &&
1073 echo "stash@{0}: store message foo" >expect &&
1074 git stash list -1 >actual &&
1075 test_cmp expect actual
1078 test_expect_success 'push -mfoo uses right message' '
1079 >foo &&
1080 git add foo &&
1081 git stash push -m"test mfoo" &&
1082 echo "stash@{0}: On main: test mfoo" >expect &&
1083 git stash list -1 >actual &&
1084 test_cmp expect actual
1087 test_expect_success 'push --message foo is synonym for -mfoo' '
1088 >foo &&
1089 git add foo &&
1090 git stash push --message "test message foo" &&
1091 echo "stash@{0}: On main: test message foo" >expect &&
1092 git stash list -1 >actual &&
1093 test_cmp expect actual
1096 test_expect_success 'push --message=foo is synonym for -mfoo' '
1097 >foo &&
1098 git add foo &&
1099 git stash push --message="test message=foo" &&
1100 echo "stash@{0}: On main: test message=foo" >expect &&
1101 git stash list -1 >actual &&
1102 test_cmp expect actual
1105 test_expect_success 'push -m shows right message' '
1106 >foo &&
1107 git add foo &&
1108 git stash push -m "test m foo" &&
1109 echo "stash@{0}: On main: test m foo" >expect &&
1110 git stash list -1 >actual &&
1111 test_cmp expect actual
1114 test_expect_success 'create stores correct message' '
1115 >foo &&
1116 git add foo &&
1117 STASH_ID=$(git stash create "create test message") &&
1118 echo "On main: create test message" >expect &&
1119 git show --pretty=%s -s ${STASH_ID} >actual &&
1120 test_cmp expect actual
1123 test_expect_success 'create when branch name has /' '
1124 test_when_finished "git checkout main" &&
1125 git checkout -b some/topic &&
1126 >foo &&
1127 git add foo &&
1128 STASH_ID=$(git stash create "create test message") &&
1129 echo "On some/topic: create test message" >expect &&
1130 git show --pretty=%s -s ${STASH_ID} >actual &&
1131 test_cmp expect actual
1134 test_expect_success 'create with multiple arguments for the message' '
1135 >foo &&
1136 git add foo &&
1137 STASH_ID=$(git stash create test untracked) &&
1138 echo "On main: test untracked" >expect &&
1139 git show --pretty=%s -s ${STASH_ID} >actual &&
1140 test_cmp expect actual
1143 test_expect_success 'create in a detached state' '
1144 test_when_finished "git checkout main" &&
1145 git checkout HEAD~1 &&
1146 >foo &&
1147 git add foo &&
1148 STASH_ID=$(git stash create) &&
1149 HEAD_ID=$(git rev-parse --short HEAD) &&
1150 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1151 git show --pretty=%s -s ${STASH_ID} >actual &&
1152 test_cmp expect actual
1155 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1156 >foo &&
1157 >bar &&
1158 git add foo bar &&
1159 git stash push -- foo &&
1160 test_path_is_file bar &&
1161 test_path_is_missing foo &&
1162 git stash pop &&
1163 test_path_is_file foo &&
1164 test_path_is_file bar
1167 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1168 mkdir sub &&
1169 >foo &&
1170 >bar &&
1171 git add foo bar &&
1173 cd sub &&
1174 git stash push -- ../foo
1175 ) &&
1176 test_path_is_file bar &&
1177 test_path_is_missing foo &&
1178 git stash pop &&
1179 test_path_is_file foo &&
1180 test_path_is_file bar
1183 test_expect_success 'stash with multiple pathspec arguments' '
1184 >foo &&
1185 >bar &&
1186 >extra &&
1187 git add foo bar extra &&
1188 git stash push -- foo bar &&
1189 test_path_is_missing bar &&
1190 test_path_is_missing foo &&
1191 test_path_is_file extra &&
1192 git stash pop &&
1193 test_path_is_file foo &&
1194 test_path_is_file bar &&
1195 test_path_is_file extra
1198 test_expect_success 'stash with file including $IFS character' '
1199 >"foo bar" &&
1200 >foo &&
1201 >bar &&
1202 git add foo* &&
1203 git stash push -- "foo b*" &&
1204 test_path_is_missing "foo bar" &&
1205 test_path_is_file foo &&
1206 test_path_is_file bar &&
1207 git stash pop &&
1208 test_path_is_file "foo bar" &&
1209 test_path_is_file foo &&
1210 test_path_is_file bar
1213 test_expect_success 'stash with pathspec matching multiple paths' '
1214 echo original >file &&
1215 echo original >other-file &&
1216 git commit -m "two" file other-file &&
1217 echo modified >file &&
1218 echo modified >other-file &&
1219 git stash push -- "*file" &&
1220 echo original >expect &&
1221 test_cmp expect file &&
1222 test_cmp expect other-file &&
1223 git stash pop &&
1224 echo modified >expect &&
1225 test_cmp expect file &&
1226 test_cmp expect other-file
1229 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1230 >foo &&
1231 git add foo &&
1232 git commit -m "tmp" &&
1233 git stash push -p foo >actual &&
1234 echo "No local changes to save" >expect &&
1235 git reset --hard HEAD~ &&
1236 test_cmp expect actual
1239 test_expect_success 'push <pathspec>: show no changes when there are none' '
1240 >foo &&
1241 git add foo &&
1242 git commit -m "tmp" &&
1243 git stash push foo >actual &&
1244 echo "No local changes to save" >expect &&
1245 git reset --hard HEAD~ &&
1246 test_cmp expect actual
1249 test_expect_success 'push: <pathspec> not in the repository errors out' '
1250 >untracked &&
1251 test_must_fail git stash push untracked &&
1252 test_path_is_file untracked
1255 test_expect_success 'push: -q is quiet with changes' '
1256 >foo &&
1257 git add foo &&
1258 git stash push -q >output 2>&1 &&
1259 test_must_be_empty output
1262 test_expect_success 'push: -q is quiet with no changes' '
1263 git stash push -q >output 2>&1 &&
1264 test_must_be_empty output
1267 test_expect_success 'push: -q is quiet even if there is no initial commit' '
1268 git init foo_dir &&
1269 test_when_finished rm -rf foo_dir &&
1271 cd foo_dir &&
1272 >bar &&
1273 test_must_fail git stash push -q >output 2>&1 &&
1274 test_must_be_empty output
1278 test_expect_success 'untracked files are left in place when -u is not given' '
1279 >file &&
1280 git add file &&
1281 >untracked &&
1282 git stash push file &&
1283 test_path_is_file untracked
1286 test_expect_success 'stash without verb with pathspec' '
1287 >"foo bar" &&
1288 >foo &&
1289 >bar &&
1290 git add foo* &&
1291 git stash -- "foo b*" &&
1292 test_path_is_missing "foo bar" &&
1293 test_path_is_file foo &&
1294 test_path_is_file bar &&
1295 git stash pop &&
1296 test_path_is_file "foo bar" &&
1297 test_path_is_file foo &&
1298 test_path_is_file bar
1301 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1302 git reset &&
1303 >foo &&
1304 >bar &&
1305 git add foo bar &&
1306 git commit -m "test" &&
1307 echo "foo" >foo &&
1308 echo "bar" >bar &&
1309 git stash -k -- foo &&
1310 test "",bar = $(cat foo),$(cat bar) &&
1311 git stash pop &&
1312 test foo,bar = $(cat foo),$(cat bar)
1315 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1316 git reset &&
1317 >subdir/untracked &&
1318 >subdir/tracked1 &&
1319 >subdir/tracked2 &&
1320 git add subdir/tracked* &&
1321 git stash -- subdir/ &&
1322 test_path_is_missing subdir/tracked1 &&
1323 test_path_is_missing subdir/tracked2 &&
1324 test_path_is_file subdir/untracked &&
1325 git stash pop &&
1326 test_path_is_file subdir/tracked1 &&
1327 test_path_is_file subdir/tracked2 &&
1328 test_path_is_file subdir/untracked
1331 test_expect_success 'stash -- <subdir> works with binary files' '
1332 git reset &&
1333 >subdir/untracked &&
1334 >subdir/tracked &&
1335 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1336 git add subdir/tracked* &&
1337 git stash -- subdir/ &&
1338 test_path_is_missing subdir/tracked &&
1339 test_path_is_missing subdir/tracked-binary &&
1340 test_path_is_file subdir/untracked &&
1341 git stash pop &&
1342 test_path_is_file subdir/tracked &&
1343 test_path_is_file subdir/tracked-binary &&
1344 test_path_is_file subdir/untracked
1347 test_expect_success 'stash with user.name and user.email set works' '
1348 test_config user.name "A U Thor" &&
1349 test_config user.email "a.u@thor" &&
1350 git stash
1353 test_expect_success 'stash works when user.name and user.email are not set' '
1354 git reset &&
1355 >1 &&
1356 git add 1 &&
1357 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1358 git stash &&
1359 git show -s --format="%an <%ae>" refs/stash >actual &&
1360 test_cmp expect actual &&
1361 >2 &&
1362 git add 2 &&
1363 test_config user.useconfigonly true &&
1365 sane_unset GIT_AUTHOR_NAME &&
1366 sane_unset GIT_AUTHOR_EMAIL &&
1367 sane_unset GIT_COMMITTER_NAME &&
1368 sane_unset GIT_COMMITTER_EMAIL &&
1369 test_unconfig user.email &&
1370 test_unconfig user.name &&
1371 test_must_fail git commit -m "should fail" &&
1372 echo "git stash <git@stash>" >expect &&
1373 >2 &&
1374 git stash &&
1375 git show -s --format="%an <%ae>" refs/stash >actual &&
1376 test_cmp expect actual
1380 test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1381 test_commit to-remove to-remove &&
1382 git rm to-remove &&
1383 git stash --keep-index &&
1384 test_path_is_missing to-remove
1387 test_expect_success 'stash apply should succeed with unmodified file' '
1388 echo base >file &&
1389 git add file &&
1390 git commit -m base &&
1392 # now stash a modification
1393 echo modified >file &&
1394 git stash &&
1396 # make the file stat dirty
1397 cp file other &&
1398 mv other file &&
1400 git stash apply
1403 test_expect_success 'stash handles skip-worktree entries nicely' '
1404 test_commit A &&
1405 echo changed >A.t &&
1406 git add A.t &&
1407 git update-index --skip-worktree A.t &&
1408 rm A.t &&
1409 git stash &&
1411 git rev-parse --verify refs/stash:A.t
1415 BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1417 test_expect_success 'stash with core.fsyncmethod=batch' "
1418 test_create_unique_files 2 4 files_base_dir &&
1419 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1421 # The files were untracked, so use the third parent,
1422 # which contains the untracked files
1423 git ls-tree -r stash^3 -- ./files_base_dir/ |
1424 test_parse_ls_tree_oids >stashed_files_oids &&
1426 # We created 2 dirs with 4 files each (8 files total) above
1427 test_line_count = 8 stashed_files_oids &&
1428 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1429 test_cmp stashed_files_oids stashed_files_actual
1433 test_expect_success 'git stash succeeds despite directory/file change' '
1434 test_create_repo directory_file_switch_v1 &&
1436 cd directory_file_switch_v1 &&
1437 test_commit init &&
1439 test_write_lines this file has some words >filler &&
1440 git add filler &&
1441 git commit -m filler &&
1443 git rm filler &&
1444 mkdir filler &&
1445 echo contents >filler/file &&
1446 git stash push
1450 test_expect_success 'git stash can pop file -> directory saved changes' '
1451 test_create_repo directory_file_switch_v2 &&
1453 cd directory_file_switch_v2 &&
1454 test_commit init &&
1456 test_write_lines this file has some words >filler &&
1457 git add filler &&
1458 git commit -m filler &&
1460 git rm filler &&
1461 mkdir filler &&
1462 echo contents >filler/file &&
1463 cp filler/file expect &&
1464 git stash push --include-untracked &&
1465 git stash apply --index &&
1466 test_cmp expect filler/file
1470 test_expect_success 'git stash can pop directory -> file saved changes' '
1471 test_create_repo directory_file_switch_v3 &&
1473 cd directory_file_switch_v3 &&
1474 test_commit init &&
1476 mkdir filler &&
1477 test_write_lines some words >filler/file1 &&
1478 test_write_lines and stuff >filler/file2 &&
1479 git add filler &&
1480 git commit -m filler &&
1482 git rm -rf filler &&
1483 echo contents >filler &&
1484 cp filler expect &&
1485 git stash push --include-untracked &&
1486 git stash apply --index &&
1487 test_cmp expect filler
1491 test_expect_success 'restore untracked files even when we hit conflicts' '
1492 git init restore_untracked_after_conflict &&
1494 cd restore_untracked_after_conflict &&
1496 echo hi >a &&
1497 echo there >b &&
1498 git add . &&
1499 git commit -m first &&
1500 echo hello >a &&
1501 echo something >c &&
1503 git stash push --include-untracked &&
1505 echo conflict >a &&
1506 git add a &&
1507 git commit -m second &&
1509 test_must_fail git stash pop &&
1511 test_path_is_file c
1515 test_done