interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t3903-stash.sh

1912 lines · 49180 bytes Raw Blame History
1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes E Schindelin
4#
5
6test_description='Test git stash'
7
8GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11. ./test-lib.sh
12. "$TEST_DIRECTORY"/lib-unique-files.sh
13
14test_expect_success 'setup' '
15 test_oid_cache <<-EOF
16 export_base sha1:73c9bab443d1f88ac61aa533d2eeaaa15451239c
17 export_base sha256:f210fa6346e3e2ce047bdb570426b17075980c1ac01fec8fc4b75bd3ab4bcfe4
18 EOF
19'
20
21test_expect_success 'usage on cmd and subcommand invalid option' '
22 test_expect_code 129 git stash --invalid-option 2>usage &&
23 test_grep "or: git stash" usage &&
24
25 test_expect_code 129 git stash push --invalid-option 2>usage &&
26 test_grep ! "or: git stash" usage
27'
28
29test_expect_success 'usage on main command -h emits a summary of subcommands' '
30 git stash -h >usage &&
31 test_grep -F "usage: git stash list" usage &&
32 test_grep -F "or: git stash show" usage
33'
34
35test_expect_success 'usage for subcommands should emit subcommand usage' '
36 git stash push -h >usage &&
37 test_grep -F "usage: git stash [push" usage
38'
39
40diff_cmp () {
41 for i in "$1" "$2"
42 do
43 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
44 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
45 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
46 "$i" >"$i.compare" || return 1
47 done &&
48 test_cmp "$1.compare" "$2.compare" &&
49 rm -f "$1.compare" "$2.compare"
50}
51
52setup_stash() {
53 echo 1 >file &&
54 git add file &&
55 echo unrelated >other-file &&
56 git add other-file &&
57 test_tick &&
58 git commit -m initial &&
59 git tag initial &&
60 echo 2 >file &&
61 git add file &&
62 echo 3 >file &&
63 test_tick &&
64 git stash &&
65 git diff-files --quiet &&
66 git diff-index --cached --quiet HEAD
67}
68
69test_expect_success 'stash some dirty working directory' '
70 setup_stash
71'
72
73cat >expect <<EOF
74diff --git a/file b/file
75index 0cfbf08..00750ed 100644
76--- a/file
77+++ b/file
78@@ -1 +1 @@
79-2
80+3
81EOF
82
83test_expect_success 'parents of stash' '
84 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
85 git diff stash^2..stash >output &&
86 diff_cmp expect output
87'
88
89test_expect_success 'applying bogus stash does nothing' '
90 test_must_fail git stash apply stash@{1} &&
91 echo 1 >expect &&
92 test_cmp expect file
93'
94
95test_expect_success 'apply does not need clean working directory' '
96 echo 4 >other-file &&
97 git stash apply &&
98 echo 3 >expect &&
99 test_cmp expect file
100'
101
102test_expect_success 'apply does not clobber working directory changes' '
103 git reset --hard &&
104 echo 4 >file &&
105 test_must_fail git stash apply &&
106 echo 4 >expect &&
107 test_cmp expect file
108'
109
110test_expect_success 'apply stashed changes' '
111 git reset --hard &&
112 echo 5 >other-file &&
113 git add other-file &&
114 test_tick &&
115 git commit -m other-file &&
116 git stash apply &&
117 test 3 = $(cat file) &&
118 test 1 = $(git show :file) &&
119 test 1 = $(git show HEAD:file)
120'
121
122test_expect_success 'apply stashed changes (including index)' '
123 git reset --hard HEAD^ &&
124 echo 6 >other-file &&
125 git add other-file &&
126 test_tick &&
127 git commit -m other-file &&
128 git stash apply --index &&
129 test 3 = $(cat file) &&
130 test 2 = $(git show :file) &&
131 test 1 = $(git show HEAD:file)
132'
133
134test_expect_success 'unstashing in a subdirectory' '
135 git reset --hard HEAD &&
136 mkdir subdir &&
137 (
138 cd subdir &&
139 git stash apply
140 )
141'
142
143test_expect_success 'stash drop complains of extra options' '
144 test_must_fail git stash drop --foo
145'
146
147test_expect_success 'drop top stash' '
148 git reset --hard &&
149 git stash list >expected &&
150 echo 7 >file &&
151 git stash &&
152 git stash drop &&
153 git stash list >actual &&
154 test_cmp expected actual &&
155 git stash apply &&
156 test 3 = $(cat file) &&
157 test 1 = $(git show :file) &&
158 test 1 = $(git show HEAD:file)
159'
160
161test_expect_success 'drop middle stash' '
162 git reset --hard &&
163 echo 8 >file &&
164 git stash &&
165 echo 9 >file &&
166 git stash &&
167 git stash drop stash@{1} &&
168 test 2 = $(git stash list | wc -l) &&
169 git stash apply &&
170 test 9 = $(cat file) &&
171 test 1 = $(git show :file) &&
172 test 1 = $(git show HEAD:file) &&
173 git reset --hard &&
174 git stash drop &&
175 git stash apply &&
176 test 3 = $(cat file) &&
177 test 1 = $(git show :file) &&
178 test 1 = $(git show HEAD:file)
179'
180
181test_expect_success 'drop middle stash by index' '
182 git reset --hard &&
183 echo 8 >file &&
184 git stash &&
185 echo 9 >file &&
186 git stash &&
187 git stash drop 1 &&
188 test 2 = $(git stash list | wc -l) &&
189 git stash apply &&
190 test 9 = $(cat file) &&
191 test 1 = $(git show :file) &&
192 test 1 = $(git show HEAD:file) &&
193 git reset --hard &&
194 git stash drop &&
195 git stash apply &&
196 test 3 = $(cat file) &&
197 test 1 = $(git show :file) &&
198 test 1 = $(git show HEAD:file)
199'
200
201test_expect_success 'drop stash reflog updates refs/stash' '
202 git reset --hard &&
203 git rev-parse refs/stash >expect &&
204 echo 9 >file &&
205 git stash &&
206 git stash drop stash@{0} &&
207 git rev-parse refs/stash >actual &&
208 test_cmp expect actual
209'
210
211test_expect_success 'drop stash reflog updates refs/stash with rewrite' '
212 git init repo &&
213 (
214 cd repo &&
215 setup_stash
216 ) &&
217 echo 9 >repo/file &&
218
219 old_oid="$(git -C repo rev-parse stash@{0})" &&
220 git -C repo stash &&
221 new_oid="$(git -C repo rev-parse stash@{0})" &&
222
223 cat >expect <<-EOF &&
224 $new_oid
225 $old_oid
226 EOF
227 git -C repo reflog show refs/stash --format=%H >actual &&
228 test_cmp expect actual &&
229
230 git -C repo stash drop stash@{1} &&
231 git -C repo reflog show refs/stash --format=%H >actual &&
232 cat >expect <<-EOF &&
233 $new_oid
234 EOF
235 test_cmp expect actual
236'
237
238test_expect_success 'stash pop' '
239 git reset --hard &&
240 git stash pop &&
241 test 3 = $(cat file) &&
242 test 1 = $(git show :file) &&
243 test 1 = $(git show HEAD:file) &&
244 test 0 = $(git stash list | wc -l)
245'
246
247cat >expect <<EOF
248diff --git a/file2 b/file2
249new file mode 100644
250index 0000000..1fe912c
251--- /dev/null
252+++ b/file2
253@@ -0,0 +1 @@
254+bar2
255EOF
256
257cat >expect1 <<EOF
258diff --git a/file b/file
259index 257cc56..5716ca5 100644
260--- a/file
261+++ b/file
262@@ -1 +1 @@
263-foo
264+bar
265EOF
266
267cat >expect2 <<EOF
268diff --git a/file b/file
269index 7601807..5716ca5 100644
270--- a/file
271+++ b/file
272@@ -1 +1 @@
273-baz
274+bar
275diff --git a/file2 b/file2
276new file mode 100644
277index 0000000..1fe912c
278--- /dev/null
279+++ b/file2
280@@ -0,0 +1 @@
281+bar2
282EOF
283
284test_expect_success 'stash branch' '
285 echo foo >file &&
286 git commit file -m first &&
287 echo bar >file &&
288 echo bar2 >file2 &&
289 git add file2 &&
290 git stash &&
291 echo baz >file &&
292 git commit file -m second &&
293 git stash branch stashbranch &&
294 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
295 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
296 git diff --cached >output &&
297 diff_cmp expect output &&
298 git diff >output &&
299 diff_cmp expect1 output &&
300 git add file &&
301 git commit -m alternate\ second &&
302 git diff main..stashbranch >output &&
303 diff_cmp output expect2 &&
304 test 0 = $(git stash list | wc -l)
305'
306
307test_expect_success 'apply -q is quiet' '
308 echo foo >file &&
309 git stash &&
310 git stash apply -q >output.out 2>&1 &&
311 test_must_be_empty output.out
312'
313
314test_expect_success 'apply --index -q is quiet' '
315 # Added file, deleted file, modified file all staged for commit
316 echo foo >new-file &&
317 echo test >file &&
318 git add new-file file &&
319 git rm other-file &&
320
321 git stash &&
322 git stash apply --index -q >output.out 2>&1 &&
323 test_must_be_empty output.out
324'
325
326test_expect_success 'save -q is quiet' '
327 git stash save --quiet >output.out 2>&1 &&
328 test_must_be_empty output.out
329'
330
331test_expect_success 'pop -q works and is quiet' '
332 git stash pop -q >output.out 2>&1 &&
333 echo bar >expect &&
334 git show :file >actual &&
335 test_cmp expect actual &&
336 test_must_be_empty output.out
337'
338
339test_expect_success 'pop -q --index works and is quiet' '
340 echo foo >file &&
341 git add file &&
342 git stash save --quiet &&
343 git stash pop -q --index >output.out 2>&1 &&
344 git diff-files file2 >file2.diff &&
345 test_must_be_empty file2.diff &&
346 test foo = "$(git show :file)" &&
347 test_must_be_empty output.out
348'
349
350test_expect_success 'drop -q is quiet' '
351 git stash &&
352 git stash drop -q >output.out 2>&1 &&
353 test_must_be_empty output.out
354'
355
356test_expect_success 'stash push -q --staged refreshes the index' '
357 git reset --hard &&
358 echo test >file &&
359 git add file &&
360 git stash push -q --staged &&
361 git diff-files >output.out &&
362 test_must_be_empty output.out
363'
364
365test_expect_success 'stash apply -q --index refreshes the index' '
366 echo test >other-file &&
367 git add other-file &&
368 echo another-change >other-file &&
369 git diff-files >expect &&
370 git stash &&
371
372 git stash apply -q --index &&
373 git diff-files >actual &&
374 test_cmp expect actual
375'
376
377test_expect_success 'stash apply --index leaves everything untouched on failure' '
378 git reset --hard &&
379 echo test >other-file &&
380 git add other-file &&
381 git stash &&
382 echo unrelated >file &&
383 echo unrelated >another-file &&
384 git add another-file &&
385 echo conflict >other-file &&
386 git add other-file &&
387 git diff-files -p >expect &&
388 git diff-index --cached HEAD >expect-index &&
389
390 test_must_fail git stash apply --index 2>err &&
391 test_grep "conflicts in index. Try without --index" err &&
392 git diff-files -p >actual &&
393 test_cmp expect actual &&
394 git diff-index --cached HEAD >actual-index &&
395 test_cmp expect-index actual-index
396'
397
398# the later "stash -k" test is not expecting us to muck with file so much, so
399# reset when finished
400test_expect_success 'stash apply --index merges the correct trees' '
401 head=$(git rev-parse HEAD) &&
402 test_when_finished "git reset --hard $head" &&
403 test_write_lines A B C >file &&
404 git commit -m setup file &&
405 test_write_lines A B staged >file &&
406 git add file &&
407 test_write_lines A B unstaged >file &&
408 git stash &&
409 test_write_lines committed B C >file &&
410 git commit -m to-be-merged file &&
411 git stash pop --index &&
412 git show :file >actual &&
413 test_write_lines committed B staged >expect &&
414 test_cmp expect actual &&
415 test_write_lines committed B unstaged >expect &&
416 test_cmp expect file
417'
418
419test_expect_success 'stash -k' '
420 echo bar3 >file &&
421 echo bar4 >file2 &&
422 git add file2 &&
423 git stash -k &&
424 test bar,bar4 = $(cat file),$(cat file2)
425'
426
427test_expect_success 'stash --no-keep-index' '
428 echo bar33 >file &&
429 echo bar44 >file2 &&
430 git add file2 &&
431 git stash --no-keep-index &&
432 test bar,bar2 = $(cat file),$(cat file2)
433'
434
435test_expect_success 'stash --staged' '
436 echo bar3 >file &&
437 echo bar4 >file2 &&
438 git add file2 &&
439 git stash --staged &&
440 test bar3,bar2 = $(cat file),$(cat file2) &&
441 git reset --hard &&
442 git stash pop &&
443 test bar,bar4 = $(cat file),$(cat file2)
444'
445
446test_expect_success 'stash --staged with binary file' '
447 printf "\0" >file &&
448 git add file &&
449 git stash --staged &&
450 git stash pop &&
451 printf "\0" >expect &&
452 test_cmp expect file
453'
454
455test_expect_success 'dont assume push with non-option args' '
456 test_must_fail git stash -q drop 2>err &&
457 test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
458'
459
460test_expect_success 'stash --invalid-option' '
461 echo bar5 >file &&
462 echo bar6 >file2 &&
463 git add file2 &&
464 test_must_fail git stash --invalid-option &&
465 test_must_fail git stash save --invalid-option &&
466 test bar5,bar6 = $(cat file),$(cat file2)
467'
468
469test_expect_success 'stash an added file' '
470 git reset --hard &&
471 echo new >file3 &&
472 git add file3 &&
473 git stash save "added file" &&
474 ! test -r file3 &&
475 git stash apply &&
476 test new = "$(cat file3)"
477'
478
479test_expect_success 'stash --intent-to-add file' '
480 git reset --hard &&
481 echo new >file4 &&
482 git add --intent-to-add file4 &&
483 test_when_finished "git rm -f file4" &&
484 test_must_fail git stash
485'
486
487test_expect_success 'stash rm then recreate' '
488 git reset --hard &&
489 git rm file &&
490 echo bar7 >file &&
491 git stash save "rm then recreate" &&
492 test bar = "$(cat file)" &&
493 git stash apply &&
494 test bar7 = "$(cat file)"
495'
496
497test_expect_success 'stash rm and ignore' '
498 git reset --hard &&
499 git rm file &&
500 echo file >.gitignore &&
501 git stash save "rm and ignore" &&
502 test bar = "$(cat file)" &&
503 test file = "$(cat .gitignore)" &&
504 git stash apply &&
505 ! test -r file &&
506 test file = "$(cat .gitignore)"
507'
508
509test_expect_success 'stash rm and ignore (stage .gitignore)' '
510 git reset --hard &&
511 git rm file &&
512 echo file >.gitignore &&
513 git add .gitignore &&
514 git stash save "rm and ignore (stage .gitignore)" &&
515 test bar = "$(cat file)" &&
516 ! test -r .gitignore &&
517 git stash apply &&
518 ! test -r file &&
519 test file = "$(cat .gitignore)"
520'
521
522test_expect_success SYMLINKS 'stash file to symlink' '
523 git reset --hard &&
524 rm file &&
525 ln -s file2 file &&
526 git stash save "file to symlink" &&
527 test_path_is_file_not_symlink file &&
528 test bar = "$(cat file)" &&
529 git stash apply &&
530 test_path_is_symlink file &&
531 test "$(test_readlink file)" = file2
532'
533
534test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
535 git reset --hard &&
536 git rm file &&
537 ln -s file2 file &&
538 git stash save "file to symlink (stage rm)" &&
539 test_path_is_file_not_symlink file &&
540 test bar = "$(cat file)" &&
541 git stash apply &&
542 test_path_is_symlink file &&
543 test "$(test_readlink file)" = file2
544'
545
546test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
547 git reset --hard &&
548 rm file &&
549 ln -s file2 file &&
550 git add file &&
551 git stash save "file to symlink (full stage)" &&
552 test_path_is_file_not_symlink file &&
553 test bar = "$(cat file)" &&
554 git stash apply &&
555 test_path_is_symlink file &&
556 test "$(test_readlink file)" = file2
557'
558
559# This test creates a commit with a symlink used for the following tests
560
561test_expect_success 'stash symlink to file' '
562 git reset --hard &&
563 test_ln_s_add file filelink &&
564 git commit -m "Add symlink" &&
565 rm filelink &&
566 cp file filelink &&
567 git stash save "symlink to file"
568'
569
570test_expect_success SYMLINKS 'this must have re-created the symlink' '
571 test -h filelink &&
572 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
573'
574
575test_expect_success 'unstash must re-create the file' '
576 git stash apply &&
577 ! test -h filelink &&
578 test bar = "$(cat file)"
579'
580
581test_expect_success 'stash symlink to file (stage rm)' '
582 git reset --hard &&
583 git rm filelink &&
584 cp file filelink &&
585 git stash save "symlink to file (stage rm)"
586'
587
588test_expect_success SYMLINKS 'this must have re-created the symlink' '
589 test -h filelink &&
590 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
591'
592
593test_expect_success 'unstash must re-create the file' '
594 git stash apply &&
595 ! test -h filelink &&
596 test bar = "$(cat file)"
597'
598
599test_expect_success 'stash symlink to file (full stage)' '
600 git reset --hard &&
601 rm filelink &&
602 cp file filelink &&
603 git add filelink &&
604 git stash save "symlink to file (full stage)"
605'
606
607test_expect_success SYMLINKS 'this must have re-created the symlink' '
608 test -h filelink &&
609 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
610'
611
612test_expect_success 'unstash must re-create the file' '
613 git stash apply &&
614 ! test -h filelink &&
615 test bar = "$(cat file)"
616'
617
618test_expect_failure 'stash directory to file' '
619 git reset --hard &&
620 mkdir dir &&
621 echo foo >dir/file &&
622 git add dir/file &&
623 git commit -m "Add file in dir" &&
624 rm -fr dir &&
625 echo bar >dir &&
626 git stash save "directory to file" &&
627 test_path_is_dir dir &&
628 test foo = "$(cat dir/file)" &&
629 test_must_fail git stash apply &&
630 test bar = "$(cat dir)" &&
631 git reset --soft HEAD^
632'
633
634test_expect_failure 'stash file to directory' '
635 git reset --hard &&
636 rm file &&
637 mkdir file &&
638 echo foo >file/file &&
639 git stash save "file to directory" &&
640 test_path_is_file file &&
641 test bar = "$(cat file)" &&
642 git stash apply &&
643 test_path_is_file file/file &&
644 test foo = "$(cat file/file)"
645'
646
647test_expect_success 'giving too many ref arguments does not modify files' '
648 git stash clear &&
649 test_when_finished "git reset --hard HEAD" &&
650 echo foo >file2 &&
651 git stash &&
652 echo bar >file2 &&
653 git stash &&
654 test-tool chmtime =123456789 file2 &&
655 for type in apply pop "branch stash-branch"
656 do
657 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
658 test_grep "Too many revisions" err &&
659 test 123456789 = $(test-tool chmtime -g file2) || return 1
660 done
661'
662
663test_expect_success 'drop: too many arguments errors out (does nothing)' '
664 git stash list >expect &&
665 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
666 test_grep "Too many revisions" err &&
667 git stash list >actual &&
668 test_cmp expect actual
669'
670
671test_expect_success 'show: too many arguments errors out (does nothing)' '
672 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
673 test_grep "Too many revisions" err &&
674 test_must_be_empty out
675'
676
677test_expect_success 'stash create - no changes' '
678 git stash clear &&
679 test_when_finished "git reset --hard HEAD" &&
680 git reset --hard &&
681 git stash create >actual &&
682 test_must_be_empty actual
683'
684
685test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
686 git stash clear &&
687 test_when_finished "git reset --hard HEAD" &&
688 git reset --hard &&
689 echo foo >>file &&
690 STASH_ID=$(git stash create) &&
691 git reset --hard &&
692 git stash branch stash-branch ${STASH_ID} &&
693 test_when_finished "git reset --hard HEAD && git checkout main &&
694 git branch -D stash-branch" &&
695 test $(git ls-files --modified | wc -l) -eq 1
696'
697
698test_expect_success 'stash branch - stashes on stack, stash-like argument' '
699 git stash clear &&
700 test_when_finished "git reset --hard HEAD" &&
701 git reset --hard &&
702 echo foo >>file &&
703 git stash &&
704 test_when_finished "git stash drop" &&
705 echo bar >>file &&
706 STASH_ID=$(git stash create) &&
707 git reset --hard &&
708 git stash branch stash-branch ${STASH_ID} &&
709 test_when_finished "git reset --hard HEAD && git checkout main &&
710 git branch -D stash-branch" &&
711 test $(git ls-files --modified | wc -l) -eq 1
712'
713
714test_expect_success 'stash branch complains with no arguments' '
715 test_must_fail git stash branch 2>err &&
716 test_grep "No branch name specified" err
717'
718
719test_expect_success 'stash show format defaults to --stat' '
720 git stash clear &&
721 test_when_finished "git reset --hard HEAD" &&
722 git reset --hard &&
723 echo foo >>file &&
724 git stash &&
725 test_when_finished "git stash drop" &&
726 echo bar >>file &&
727 STASH_ID=$(git stash create) &&
728 git reset --hard &&
729 cat >expected <<-EOF &&
730 file | 1 +
731 1 file changed, 1 insertion(+)
732 EOF
733 git stash show ${STASH_ID} >actual &&
734 test_cmp expected actual
735'
736
737test_expect_success 'stash show - stashes on stack, stash-like argument' '
738 git stash clear &&
739 test_when_finished "git reset --hard HEAD" &&
740 git reset --hard &&
741 echo foo >>file &&
742 git stash &&
743 test_when_finished "git stash drop" &&
744 echo bar >>file &&
745 STASH_ID=$(git stash create) &&
746 git reset --hard &&
747 echo "1 0 file" >expected &&
748 git stash show --numstat ${STASH_ID} >actual &&
749 test_cmp expected actual
750'
751
752test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
753 git stash clear &&
754 test_when_finished "git reset --hard HEAD" &&
755 git reset --hard &&
756 echo foo >>file &&
757 git stash &&
758 test_when_finished "git stash drop" &&
759 echo bar >>file &&
760 STASH_ID=$(git stash create) &&
761 git reset --hard &&
762 cat >expected <<-EOF &&
763 diff --git a/file b/file
764 index 7601807..935fbd3 100644
765 --- a/file
766 +++ b/file
767 @@ -1 +1,2 @@
768 baz
769 +bar
770 EOF
771 git stash show -p ${STASH_ID} >actual &&
772 diff_cmp expected actual
773'
774
775test_expect_success 'stash show - no stashes on stack, stash-like argument' '
776 git stash clear &&
777 test_when_finished "git reset --hard HEAD" &&
778 git reset --hard &&
779 echo foo >>file &&
780 STASH_ID=$(git stash create) &&
781 git reset --hard &&
782 echo "1 0 file" >expected &&
783 git stash show --numstat ${STASH_ID} >actual &&
784 test_cmp expected actual
785'
786
787test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
788 git stash clear &&
789 test_when_finished "git reset --hard HEAD" &&
790 git reset --hard &&
791 echo foo >>file &&
792 STASH_ID=$(git stash create) &&
793 git reset --hard &&
794 cat >expected <<-EOF &&
795 diff --git a/file b/file
796 index 7601807..71b52c4 100644
797 --- a/file
798 +++ b/file
799 @@ -1 +1,2 @@
800 baz
801 +foo
802 EOF
803 git stash show -p ${STASH_ID} >actual &&
804 diff_cmp expected actual
805'
806
807test_expect_success 'stash show --patience shows diff' '
808 git reset --hard &&
809 echo foo >>file &&
810 STASH_ID=$(git stash create) &&
811 git reset --hard &&
812 cat >expected <<-EOF &&
813 diff --git a/file b/file
814 index 7601807..71b52c4 100644
815 --- a/file
816 +++ b/file
817 @@ -1 +1,2 @@
818 baz
819 +foo
820 EOF
821 git stash show --patience ${STASH_ID} >actual &&
822 diff_cmp expected actual
823'
824
825test_expect_success 'stash show supports prefixes' '
826 git reset --hard &&
827 echo foo >>file &&
828 git stash &&
829 cat >expected <<-\EOF &&
830 diff --git foo/file bar/file
831 index 7601807..71b52c4 100644
832 --- foo/file
833 +++ bar/file
834 @@ -1 +1,2 @@
835 baz
836 +foo
837 EOF
838 git stash show --src-prefix=foo/ --dst-prefix=bar/ >actual &&
839 diff_cmp expected actual
840'
841
842test_expect_success 'drop: fail early if specified stash is not a stash ref' '
843 git stash clear &&
844 test_when_finished "git reset --hard HEAD && git stash clear" &&
845 git reset --hard &&
846 echo foo >file &&
847 git stash &&
848 echo bar >file &&
849 git stash &&
850 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
851 git stash pop &&
852 test bar = "$(cat file)" &&
853 git reset --hard HEAD
854'
855
856test_expect_success 'pop: fail early if specified stash is not a stash ref' '
857 git stash clear &&
858 test_when_finished "git reset --hard HEAD && git stash clear" &&
859 git reset --hard &&
860 echo foo >file &&
861 git stash &&
862 echo bar >file &&
863 git stash &&
864 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
865 git stash pop &&
866 test bar = "$(cat file)" &&
867 git reset --hard HEAD
868'
869
870test_expect_success 'ref with non-existent reflog' '
871 git stash clear &&
872 echo bar5 >file &&
873 echo bar6 >file2 &&
874 git add file2 &&
875 git stash &&
876 test_must_fail git rev-parse --quiet --verify does-not-exist &&
877 test_must_fail git stash drop does-not-exist &&
878 test_must_fail git stash drop does-not-exist@{0} &&
879 test_must_fail git stash pop does-not-exist &&
880 test_must_fail git stash pop does-not-exist@{0} &&
881 test_must_fail git stash apply does-not-exist &&
882 test_must_fail git stash apply does-not-exist@{0} &&
883 test_must_fail git stash show does-not-exist &&
884 test_must_fail git stash show does-not-exist@{0} &&
885 test_must_fail git stash branch tmp does-not-exist &&
886 test_must_fail git stash branch tmp does-not-exist@{0} &&
887 git stash drop
888'
889
890test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
891 git stash clear &&
892 test_must_fail git stash drop stash@{0} &&
893 echo bar5 >file &&
894 echo bar6 >file2 &&
895 git add file2 &&
896 git stash &&
897 test_must_fail git stash drop stash@{1} &&
898 test_must_fail git stash pop stash@{1} &&
899 test_must_fail git stash apply stash@{1} &&
900 test_must_fail git stash show stash@{1} &&
901 test_must_fail git stash branch tmp stash@{1} &&
902 git stash drop
903'
904
905test_expect_success 'invalid ref of the form "n", n >= N' '
906 git stash clear &&
907 test_must_fail git stash drop 0 &&
908 echo bar5 >file &&
909 echo bar6 >file2 &&
910 git add file2 &&
911 git stash &&
912 test_must_fail git stash drop 1 &&
913 test_must_fail git stash pop 1 &&
914 test_must_fail git stash apply 1 &&
915 test_must_fail git stash show 1 &&
916 test_must_fail git stash branch tmp 1 &&
917 git stash drop
918'
919
920test_expect_success 'valid ref of the form "n", n < N' '
921 git stash clear &&
922 echo bar5 >file &&
923 echo bar6 >file2 &&
924 git add file2 &&
925 git stash &&
926 git stash show 0 &&
927 git stash branch tmp 0 &&
928 git checkout main &&
929 git stash &&
930 git stash apply 0 &&
931 git reset --hard &&
932 git stash pop 0 &&
933 git stash &&
934 git stash drop 0 &&
935 test_must_fail git stash drop
936'
937
938test_expect_success 'branch: do not drop the stash if the branch exists' '
939 git stash clear &&
940 echo foo >file &&
941 git add file &&
942 git commit -m initial &&
943 echo bar >file &&
944 git stash &&
945 test_must_fail git stash branch main stash@{0} &&
946 git rev-parse stash@{0} --
947'
948
949test_expect_success 'branch: should not drop the stash if the apply fails' '
950 git stash clear &&
951 git reset HEAD~1 --hard &&
952 echo foo >file &&
953 git add file &&
954 git commit -m initial &&
955 echo bar >file &&
956 git stash &&
957 echo baz >file &&
958 test_when_finished "git checkout main" &&
959 test_must_fail git stash branch new_branch stash@{0} &&
960 git rev-parse stash@{0} --
961'
962
963test_expect_success 'apply: show same status as git status (relative to ./)' '
964 git stash clear &&
965 mkdir -p subdir &&
966 echo 1 >subdir/subfile1 &&
967 echo 2 >subdir/subfile2 &&
968 git add subdir/subfile1 &&
969 git commit -m subdir &&
970 (
971 cd subdir &&
972 echo x >subfile1 &&
973 echo x >../file &&
974 git status >../expect &&
975 git stash &&
976 sane_unset GIT_MERGE_VERBOSITY &&
977 git stash apply
978 ) |
979 sed -e 1d >actual && # drop "Saved..."
980 test_cmp expect actual
981'
982
983cat >expect <<EOF
984diff --git a/HEAD b/HEAD
985new file mode 100644
986index 0000000..fe0cbee
987--- /dev/null
988+++ b/HEAD
989@@ -0,0 +1 @@
990+file-not-a-ref
991EOF
992
993test_expect_success 'stash where working directory contains "HEAD" file' '
994 git stash clear &&
995 git reset --hard &&
996 echo file-not-a-ref >HEAD &&
997 git add HEAD &&
998 test_tick &&
999 git stash &&
1000 git diff-files --quiet &&
1001 git diff-index --cached --quiet HEAD &&
1002 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
1003 git diff stash^..stash >output &&
1004 diff_cmp expect output
1005'
1006
1007test_expect_success 'store called with invalid commit' '
1008 test_must_fail git stash store foo
1009'
1010
1011test_expect_success 'store called with non-stash commit' '
1012 test_must_fail git stash store HEAD
1013'
1014
1015test_expect_success 'store updates stash ref and reflog' '
1016 git stash clear &&
1017 git reset --hard &&
1018 echo quux >bazzy &&
1019 git add bazzy &&
1020 STASH_ID=$(git stash create) &&
1021 git reset --hard &&
1022 test_path_is_missing bazzy &&
1023 git stash store -m quuxery $STASH_ID &&
1024 test $(git rev-parse stash) = $STASH_ID &&
1025 git reflog --format=%H stash| grep $STASH_ID &&
1026 git stash pop &&
1027 test_grep quux bazzy
1028'
1029
1030test_expect_success 'handle stash specification with spaces' '
1031 git stash clear &&
1032 echo pig >file &&
1033 git stash &&
1034 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
1035 test_tick &&
1036 echo cow >file &&
1037 git stash &&
1038 git stash apply "stash@{$stamp}" &&
1039 test_grep pig file
1040'
1041
1042test_expect_success 'setup stash with index and worktree changes' '
1043 git stash clear &&
1044 git reset --hard &&
1045 echo index >file &&
1046 git add file &&
1047 echo working >file &&
1048 git stash
1049'
1050
1051test_expect_success 'stash list -p shows simple diff' '
1052 cat >expect <<-EOF &&
1053 stash@{0}
1054
1055 diff --git a/file b/file
1056 index 257cc56..d26b33d 100644
1057 --- a/file
1058 +++ b/file
1059 @@ -1 +1 @@
1060 -foo
1061 +working
1062 EOF
1063 git stash list --format=%gd -p >actual &&
1064 diff_cmp expect actual
1065'
1066
1067test_expect_success 'stash list --cc shows combined diff' '
1068 cat >expect <<-\EOF &&
1069 stash@{0}
1070
1071 diff --cc file
1072 index 257cc56,9015a7a..d26b33d
1073 --- a/file
1074 +++ b/file
1075 @@@ -1,1 -1,1 +1,1 @@@
1076 - foo
1077 -index
1078 ++working
1079 EOF
1080 git stash list --format=%gd -p --cc >actual &&
1081 diff_cmp expect actual
1082'
1083
1084test_expect_success 'stash is not confused by partial renames' '
1085 mv file renamed &&
1086 git add renamed &&
1087 git stash &&
1088 git stash apply &&
1089 test_path_is_file renamed &&
1090 test_path_is_missing file
1091'
1092
1093test_expect_success 'push -m shows right message' '
1094 >foo &&
1095 git add foo &&
1096 git stash push -m "test message" &&
1097 echo "stash@{0}: On main: test message" >expect &&
1098 git stash list -1 >actual &&
1099 test_cmp expect actual
1100'
1101
1102test_expect_success 'push -m also works without space' '
1103 >foo &&
1104 git add foo &&
1105 git stash push -m"unspaced test message" &&
1106 echo "stash@{0}: On main: unspaced test message" >expect &&
1107 git stash list -1 >actual &&
1108 test_cmp expect actual
1109'
1110
1111test_expect_success 'store -m foo shows right message' '
1112 git stash clear &&
1113 git reset --hard &&
1114 echo quux >bazzy &&
1115 git add bazzy &&
1116 STASH_ID=$(git stash create) &&
1117 git stash store -m "store m" $STASH_ID &&
1118 echo "stash@{0}: store m" >expect &&
1119 git stash list -1 >actual &&
1120 test_cmp expect actual
1121'
1122
1123test_expect_success 'store -mfoo shows right message' '
1124 git stash clear &&
1125 git reset --hard &&
1126 echo quux >bazzy &&
1127 git add bazzy &&
1128 STASH_ID=$(git stash create) &&
1129 git stash store -m"store mfoo" $STASH_ID &&
1130 echo "stash@{0}: store mfoo" >expect &&
1131 git stash list -1 >actual &&
1132 test_cmp expect actual
1133'
1134
1135test_expect_success 'store --message=foo shows right message' '
1136 git stash clear &&
1137 git reset --hard &&
1138 echo quux >bazzy &&
1139 git add bazzy &&
1140 STASH_ID=$(git stash create) &&
1141 git stash store --message="store message=foo" $STASH_ID &&
1142 echo "stash@{0}: store message=foo" >expect &&
1143 git stash list -1 >actual &&
1144 test_cmp expect actual
1145'
1146
1147test_expect_success 'store --message foo shows right message' '
1148 git stash clear &&
1149 git reset --hard &&
1150 echo quux >bazzy &&
1151 git add bazzy &&
1152 STASH_ID=$(git stash create) &&
1153 git stash store --message "store message foo" $STASH_ID &&
1154 echo "stash@{0}: store message foo" >expect &&
1155 git stash list -1 >actual &&
1156 test_cmp expect actual
1157'
1158
1159test_expect_success 'push -mfoo uses right message' '
1160 >foo &&
1161 git add foo &&
1162 git stash push -m"test mfoo" &&
1163 echo "stash@{0}: On main: test mfoo" >expect &&
1164 git stash list -1 >actual &&
1165 test_cmp expect actual
1166'
1167
1168test_expect_success 'push --message foo is synonym for -mfoo' '
1169 >foo &&
1170 git add foo &&
1171 git stash push --message "test message foo" &&
1172 echo "stash@{0}: On main: test message foo" >expect &&
1173 git stash list -1 >actual &&
1174 test_cmp expect actual
1175'
1176
1177test_expect_success 'push --message=foo is synonym for -mfoo' '
1178 >foo &&
1179 git add foo &&
1180 git stash push --message="test message=foo" &&
1181 echo "stash@{0}: On main: test message=foo" >expect &&
1182 git stash list -1 >actual &&
1183 test_cmp expect actual
1184'
1185
1186test_expect_success 'push -m shows right message' '
1187 >foo &&
1188 git add foo &&
1189 git stash push -m "test m foo" &&
1190 echo "stash@{0}: On main: test m foo" >expect &&
1191 git stash list -1 >actual &&
1192 test_cmp expect actual
1193'
1194
1195test_expect_success 'create stores correct message' '
1196 >foo &&
1197 git add foo &&
1198 STASH_ID=$(git stash create "create test message") &&
1199 echo "On main: create test message" >expect &&
1200 git show --pretty=%s -s ${STASH_ID} >actual &&
1201 test_cmp expect actual
1202'
1203
1204test_expect_success 'create when branch name has /' '
1205 test_when_finished "git checkout main" &&
1206 git checkout -b some/topic &&
1207 >foo &&
1208 git add foo &&
1209 STASH_ID=$(git stash create "create test message") &&
1210 echo "On some/topic: create test message" >expect &&
1211 git show --pretty=%s -s ${STASH_ID} >actual &&
1212 test_cmp expect actual
1213'
1214
1215test_expect_success 'create with multiple arguments for the message' '
1216 >foo &&
1217 git add foo &&
1218 STASH_ID=$(git stash create test untracked) &&
1219 echo "On main: test untracked" >expect &&
1220 git show --pretty=%s -s ${STASH_ID} >actual &&
1221 test_cmp expect actual
1222'
1223
1224test_expect_success 'create in a detached state' '
1225 test_when_finished "git checkout main" &&
1226 git checkout HEAD~1 &&
1227 >foo &&
1228 git add foo &&
1229 STASH_ID=$(git stash create) &&
1230 HEAD_ID=$(git rev-parse --short HEAD) &&
1231 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1232 git show --pretty=%s -s ${STASH_ID} >actual &&
1233 test_cmp expect actual
1234'
1235
1236test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1237 >foo &&
1238 >bar &&
1239 git add foo bar &&
1240 git stash push -- foo &&
1241 test_path_is_file bar &&
1242 test_path_is_missing foo &&
1243 git stash pop &&
1244 test_path_is_file foo &&
1245 test_path_is_file bar
1246'
1247
1248test_expect_success 'stash --patch <pathspec> stash and restores the file' '
1249 test_write_lines b c >file &&
1250 git commit -m "add a few lines" file &&
1251 test_write_lines a b c d >file &&
1252 test_write_lines b c d >expect-file &&
1253 echo changed-other-file >other-file &&
1254 test_write_lines s y n | git stash -m "stash bar" --patch file &&
1255 test_cmp expect-file file &&
1256 echo changed-other-file >expect &&
1257 test_cmp expect other-file &&
1258 git checkout HEAD -- file &&
1259 git stash pop &&
1260 test_cmp expect other-file &&
1261 test_write_lines a b c >expect &&
1262 test_cmp expect file
1263'
1264
1265test_expect_success 'stash <pathspec> -p is rejected' '
1266 test_must_fail git stash file -p 2>err &&
1267 test_grep "subcommand wasn${SQ}t specified; ${SQ}push${SQ} can${SQ}t be assumed due to unexpected token ${SQ}file${SQ}" err
1268'
1269
1270test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1271 mkdir sub &&
1272 >foo &&
1273 >bar &&
1274 git add foo bar &&
1275 (
1276 cd sub &&
1277 git stash push -- ../foo
1278 ) &&
1279 test_path_is_file bar &&
1280 test_path_is_missing foo &&
1281 git stash pop &&
1282 test_path_is_file foo &&
1283 test_path_is_file bar
1284'
1285
1286test_expect_success 'stash with multiple pathspec arguments' '
1287 >foo &&
1288 >bar &&
1289 >extra &&
1290 git add foo bar extra &&
1291 git stash push -- foo bar &&
1292 test_path_is_missing bar &&
1293 test_path_is_missing foo &&
1294 test_path_is_file extra &&
1295 git stash pop &&
1296 test_path_is_file foo &&
1297 test_path_is_file bar &&
1298 test_path_is_file extra
1299'
1300
1301test_expect_success 'stash with file including $IFS character' '
1302 >"foo bar" &&
1303 >foo &&
1304 >bar &&
1305 git add foo* &&
1306 git stash push -- "foo b*" &&
1307 test_path_is_missing "foo bar" &&
1308 test_path_is_file foo &&
1309 test_path_is_file bar &&
1310 git stash pop &&
1311 test_path_is_file "foo bar" &&
1312 test_path_is_file foo &&
1313 test_path_is_file bar
1314'
1315
1316test_expect_success 'stash with pathspec matching multiple paths' '
1317 echo original >file &&
1318 echo original >other-file &&
1319 git commit -m "two" file other-file &&
1320 echo modified >file &&
1321 echo modified >other-file &&
1322 git stash push -- "*file" &&
1323 echo original >expect &&
1324 test_cmp expect file &&
1325 test_cmp expect other-file &&
1326 git stash pop &&
1327 echo modified >expect &&
1328 test_cmp expect file &&
1329 test_cmp expect other-file
1330'
1331
1332test_expect_success 'stash push -p with pathspec shows no changes only once' '
1333 >foo &&
1334 git add foo &&
1335 git commit -m "tmp" &&
1336 git stash push -p foo >actual &&
1337 echo "No local changes to save" >expect &&
1338 git reset --hard HEAD~ &&
1339 test_cmp expect actual
1340'
1341
1342test_expect_success 'push <pathspec>: show no changes when there are none' '
1343 >foo &&
1344 git add foo &&
1345 git commit -m "tmp" &&
1346 git stash push foo >actual &&
1347 echo "No local changes to save" >expect &&
1348 git reset --hard HEAD~ &&
1349 test_cmp expect actual
1350'
1351
1352test_expect_success 'push: <pathspec> not in the repository errors out' '
1353 >untracked &&
1354 test_must_fail git stash push untracked &&
1355 test_path_is_file untracked
1356'
1357
1358test_expect_success 'push: -q is quiet with changes' '
1359 >foo &&
1360 git add foo &&
1361 git stash push -q >output 2>&1 &&
1362 test_must_be_empty output
1363'
1364
1365test_expect_success 'push: -q is quiet with no changes' '
1366 git stash push -q >output 2>&1 &&
1367 test_must_be_empty output
1368'
1369
1370test_expect_success 'push: -q is quiet even if there is no initial commit' '
1371 git init foo_dir &&
1372 test_when_finished rm -rf foo_dir &&
1373 (
1374 cd foo_dir &&
1375 >bar &&
1376 test_must_fail git stash push -q >output 2>&1 &&
1377 test_must_be_empty output
1378 )
1379'
1380
1381test_expect_success 'untracked files are left in place when -u is not given' '
1382 >file &&
1383 git add file &&
1384 >untracked &&
1385 git stash push file &&
1386 test_path_is_file untracked
1387'
1388
1389test_expect_success 'stash without verb with pathspec' '
1390 >"foo bar" &&
1391 >foo &&
1392 >bar &&
1393 git add foo* &&
1394 git stash -- "foo b*" &&
1395 test_path_is_missing "foo bar" &&
1396 test_path_is_file foo &&
1397 test_path_is_file bar &&
1398 git stash pop &&
1399 test_path_is_file "foo bar" &&
1400 test_path_is_file foo &&
1401 test_path_is_file bar
1402'
1403
1404test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1405 git reset &&
1406 >foo &&
1407 >bar &&
1408 git add foo bar &&
1409 git commit -m "test" &&
1410 echo "foo" >foo &&
1411 echo "bar" >bar &&
1412 git stash -k -- foo &&
1413 test "",bar = $(cat foo),$(cat bar) &&
1414 git stash pop &&
1415 test foo,bar = $(cat foo),$(cat bar)
1416'
1417
1418test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1419 git reset &&
1420 mkdir -p subdir &&
1421 >subdir/untracked &&
1422 >subdir/tracked1 &&
1423 >subdir/tracked2 &&
1424 git add subdir/tracked* &&
1425 git stash -- subdir/ &&
1426 test_path_is_missing subdir/tracked1 &&
1427 test_path_is_missing subdir/tracked2 &&
1428 test_path_is_file subdir/untracked &&
1429 git stash pop &&
1430 test_path_is_file subdir/tracked1 &&
1431 test_path_is_file subdir/tracked2 &&
1432 test_path_is_file subdir/untracked
1433'
1434
1435test_expect_success 'stash -- <subdir> works with binary files' '
1436 git reset &&
1437 mkdir -p subdir &&
1438 >subdir/untracked &&
1439 >subdir/tracked &&
1440 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1441 git add subdir/tracked* &&
1442 git stash -- subdir/ &&
1443 test_path_is_missing subdir/tracked &&
1444 test_path_is_missing subdir/tracked-binary &&
1445 test_path_is_file subdir/untracked &&
1446 git stash pop &&
1447 test_path_is_file subdir/tracked &&
1448 test_path_is_file subdir/tracked-binary &&
1449 test_path_is_file subdir/untracked
1450'
1451
1452test_expect_success 'stash with user.name and user.email set works' '
1453 test_config user.name "A U Thor" &&
1454 test_config user.email "a.u@thor" &&
1455 git stash
1456'
1457
1458test_expect_success 'stash works when user.name and user.email are not set' '
1459 git reset &&
1460 >1 &&
1461 git add 1 &&
1462 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1463 git stash &&
1464 git show -s --format="%an <%ae>" refs/stash >actual &&
1465 test_cmp expect actual &&
1466 >2 &&
1467 git add 2 &&
1468 test_config user.useconfigonly true &&
1469 (
1470 sane_unset GIT_AUTHOR_NAME &&
1471 sane_unset GIT_AUTHOR_EMAIL &&
1472 sane_unset GIT_COMMITTER_NAME &&
1473 sane_unset GIT_COMMITTER_EMAIL &&
1474 test_unconfig user.email &&
1475 test_unconfig user.name &&
1476 test_must_fail git commit -m "should fail" &&
1477 echo "git stash <git@stash>" >expect &&
1478 >2 &&
1479 git stash &&
1480 git show -s --format="%an <%ae>" refs/stash >actual &&
1481 test_cmp expect actual
1482 )
1483'
1484
1485test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1486 test_commit to-remove to-remove &&
1487 git rm to-remove &&
1488 git stash --keep-index &&
1489 test_path_is_missing to-remove
1490'
1491
1492test_expect_success 'stash --keep-index --include-untracked with empty tree' '
1493 test_when_finished "rm -rf empty" &&
1494 git init empty &&
1495 (
1496 cd empty &&
1497 git commit --allow-empty --message "empty" &&
1498 echo content >file &&
1499 git stash push --keep-index --include-untracked &&
1500 test_path_is_missing file &&
1501 git stash pop &&
1502 echo content >expect &&
1503 test_cmp expect file
1504 )
1505'
1506
1507test_expect_success 'stash export and import round-trip stashes' '
1508 git reset &&
1509 >untracked &&
1510 >tracked1 &&
1511 >tracked2 &&
1512 git add tracked* &&
1513 git stash -- &&
1514 >subdir/untracked &&
1515 >subdir/tracked1 &&
1516 >subdir/tracked2 &&
1517 git add subdir/tracked* &&
1518 git stash --include-untracked -- subdir/ &&
1519 git tag t-stash0 stash@{0} &&
1520 git tag t-stash1 stash@{1} &&
1521 simple=$(git stash export --print) &&
1522 git stash clear &&
1523 git stash import "$simple" &&
1524 test_cmp_rev stash@{0} t-stash0 &&
1525 test_cmp_rev stash@{1} t-stash1 &&
1526 git stash export --to-ref refs/heads/foo &&
1527 test_cmp_rev "$(test_oid empty_tree)" foo: &&
1528 test_cmp_rev "$(test_oid empty_tree)" foo^: &&
1529 test_cmp_rev t-stash0 foo^2 &&
1530 test_cmp_rev t-stash1 foo^^2 &&
1531 git log --first-parent --format="%s" refs/heads/foo >log &&
1532 grep "^git stash: " log >log2 &&
1533 test_line_count = 13 log2 &&
1534 git stash clear &&
1535 git stash import foo &&
1536 test_cmp_rev stash@{0} t-stash0 &&
1537 test_cmp_rev stash@{1} t-stash1
1538'
1539
1540test_expect_success 'stash import appends commits' '
1541 git log --format=oneline -g refs/stash >out &&
1542 cat out out >out2 &&
1543 git stash import refs/heads/foo &&
1544 git log --format=oneline -g refs/stash >actual &&
1545 test_line_count = $(wc -l <out2) actual
1546'
1547
1548test_expect_success 'stash export can accept specified stashes' '
1549 git stash clear &&
1550 git stash import foo &&
1551 git stash export --to-ref refs/heads/bar stash@{1} stash@{0} &&
1552 git stash clear &&
1553 git stash import refs/heads/bar &&
1554 test_cmp_rev stash@{1} t-stash0 &&
1555 test_cmp_rev stash@{0} t-stash1 &&
1556 git log --format=oneline -g refs/stash >actual &&
1557 test_line_count = 2 actual
1558'
1559
1560test_expect_success 'stash export rejects invalid arguments' '
1561 test_must_fail git stash export --print --to-ref refs/heads/invalid 2>err &&
1562 test_grep "exactly one of --print and --to-ref is required" err &&
1563 test_must_fail git stash export 2>err2 &&
1564 test_grep "exactly one of --print and --to-ref is required" err2
1565'
1566
1567test_expect_success 'stash can import and export zero stashes' '
1568 git stash clear &&
1569 git stash export --to-ref refs/heads/baz &&
1570 test_cmp_rev "$(test_oid empty_tree)" baz: &&
1571 test_cmp_rev "$(test_oid export_base)" baz &&
1572 test_must_fail git rev-parse baz^1 &&
1573 git stash import baz &&
1574 test_must_fail git rev-parse refs/stash
1575'
1576
1577test_expect_success 'stash rejects invalid attempts to import commits' '
1578 git stash import foo &&
1579 test_must_fail git stash import HEAD 2>output &&
1580 oid=$(git rev-parse HEAD) &&
1581 test_grep "$oid is not a valid exported stash commit" output &&
1582 test_cmp_rev stash@{0} t-stash0 &&
1583
1584 git checkout --orphan orphan &&
1585 git commit-tree $(test_oid empty_tree) -p "$oid" -p "$oid^" -m "" >fake-commit &&
1586 git update-ref refs/heads/orphan "$(cat fake-commit)" &&
1587 oid=$(git rev-parse HEAD) &&
1588 test_must_fail git stash import orphan 2>output &&
1589 test_grep "found stash commit $oid without expected prefix" output &&
1590 test_cmp_rev stash@{0} t-stash0 &&
1591
1592 git checkout --orphan orphan2 &&
1593 git commit-tree $(test_oid empty_tree) -m "" >fake-commit &&
1594 git update-ref refs/heads/orphan2 "$(cat fake-commit)" &&
1595 oid=$(git rev-parse HEAD) &&
1596 test_must_fail git stash import orphan2 2>output &&
1597 test_grep "found root commit $oid with invalid data" output &&
1598 test_cmp_rev stash@{0} t-stash0
1599'
1600
1601test_expect_success 'stash apply should succeed with unmodified file' '
1602 echo base >file &&
1603 git add file &&
1604 git commit -m base &&
1605
1606 # now stash a modification
1607 echo modified >file &&
1608 git stash &&
1609
1610 # make the file stat dirty
1611 cp file other &&
1612 mv other file &&
1613
1614 git stash apply
1615'
1616
1617test_expect_success 'stash handles skip-worktree entries nicely' '
1618 test_commit A &&
1619 echo changed >A.t &&
1620 git add A.t &&
1621 git update-index --skip-worktree A.t &&
1622 rm A.t &&
1623 git stash &&
1624
1625 git rev-parse --verify refs/stash:A.t
1626'
1627
1628
1629BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1630
1631test_expect_success 'stash with core.fsyncmethod=batch' "
1632 test_create_unique_files 2 4 files_base_dir &&
1633 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1634
1635 # The files were untracked, so use the third parent,
1636 # which contains the untracked files
1637 git ls-tree -r stash^3 -- ./files_base_dir/ |
1638 test_parse_ls_tree_oids >stashed_files_oids &&
1639
1640 # We created 2 dirs with 4 files each (8 files total) above
1641 test_line_count = 8 stashed_files_oids &&
1642 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1643 test_cmp stashed_files_oids stashed_files_actual
1644"
1645
1646
1647test_expect_success 'git stash succeeds despite directory/file change' '
1648 test_create_repo directory_file_switch_v1 &&
1649 (
1650 cd directory_file_switch_v1 &&
1651 test_commit init &&
1652
1653 test_write_lines this file has some words >filler &&
1654 git add filler &&
1655 git commit -m filler &&
1656
1657 git rm filler &&
1658 mkdir filler &&
1659 echo contents >filler/file &&
1660 git stash push
1661 )
1662'
1663
1664test_expect_success 'git stash can pop file -> directory saved changes' '
1665 test_create_repo directory_file_switch_v2 &&
1666 (
1667 cd directory_file_switch_v2 &&
1668 test_commit init &&
1669
1670 test_write_lines this file has some words >filler &&
1671 git add filler &&
1672 git commit -m filler &&
1673
1674 git rm filler &&
1675 mkdir filler &&
1676 echo contents >filler/file &&
1677 cp filler/file expect &&
1678 git stash push --include-untracked &&
1679 git stash apply --index &&
1680 test_cmp expect filler/file
1681 )
1682'
1683
1684test_expect_success 'git stash can pop directory -> file saved changes' '
1685 test_create_repo directory_file_switch_v3 &&
1686 (
1687 cd directory_file_switch_v3 &&
1688 test_commit init &&
1689
1690 mkdir filler &&
1691 test_write_lines some words >filler/file1 &&
1692 test_write_lines and stuff >filler/file2 &&
1693 git add filler &&
1694 git commit -m filler &&
1695
1696 git rm -rf filler &&
1697 echo contents >filler &&
1698 cp filler expect &&
1699 git stash push --include-untracked &&
1700 git stash apply --index &&
1701 test_cmp expect filler
1702 )
1703'
1704
1705test_expect_success 'restore untracked files even when we hit conflicts' '
1706 git init restore_untracked_after_conflict &&
1707 (
1708 cd restore_untracked_after_conflict &&
1709
1710 echo hi >a &&
1711 echo there >b &&
1712 git add . &&
1713 git commit -m first &&
1714 echo hello >a &&
1715 echo something >c &&
1716
1717 git stash push --include-untracked &&
1718
1719 echo conflict >a &&
1720 git add a &&
1721 git commit -m second &&
1722
1723 test_must_fail git stash pop &&
1724
1725 test_path_is_file c
1726 )
1727'
1728
1729test_expect_success 'stash create reports a locked index' '
1730 test_when_finished "rm -rf repo" &&
1731 git init repo &&
1732 (
1733 cd repo &&
1734 test_commit A A.file &&
1735 echo change >A.file &&
1736 touch .git/index.lock &&
1737
1738 test_must_fail git stash create 2>err &&
1739 test_grep "error: could not write index" err &&
1740 test_grep "error: Unable to create '.*index.lock'" err
1741 )
1742'
1743
1744test_expect_success 'stash push reports a locked index' '
1745 test_when_finished "rm -rf repo" &&
1746 git init repo &&
1747 (
1748 cd repo &&
1749 test_commit A A.file &&
1750 echo change >A.file &&
1751 touch .git/index.lock &&
1752
1753 test_must_fail git stash push 2>err &&
1754 test_grep "error: could not write index" err &&
1755 test_grep "error: Unable to create '.*index.lock'" err
1756 )
1757'
1758
1759test_expect_success 'stash apply reports a locked index' '
1760 test_when_finished "rm -rf repo" &&
1761 git init repo &&
1762 (
1763 cd repo &&
1764 test_commit A A.file &&
1765 echo change >A.file &&
1766 git stash push &&
1767 touch .git/index.lock &&
1768
1769 test_must_fail git stash apply 2>err &&
1770 test_grep "error: could not write index" err &&
1771 test_grep "error: Unable to create '.*index.lock'" err
1772 )
1773'
1774
1775test_expect_success 'submodules does not affect the branch recorded in stash message' '
1776 git init sub_project &&
1777 (
1778 cd sub_project &&
1779 echo "Initial content in sub_project" >sub_file.txt &&
1780 git add sub_file.txt &&
1781 git commit -m "Initial commit in sub_project"
1782 ) &&
1783
1784 git init main_project &&
1785 (
1786 cd main_project &&
1787 echo "Initial content in main_project" >main_file.txt &&
1788 git add main_file.txt &&
1789 git commit -m "Initial commit in main_project" &&
1790
1791 git -c protocol.file.allow=always submodule add ../sub_project sub &&
1792 git commit -m "Added submodule sub_project" &&
1793
1794 git checkout -b feature_main &&
1795 git -C sub checkout -b feature_sub &&
1796
1797 git checkout -b work_branch &&
1798 echo "Important work to be stashed" >work_item.txt &&
1799 git add work_item.txt &&
1800 git stash push -m "custom stash for work_branch" &&
1801
1802 git stash list >../actual_stash_list.txt &&
1803 test_grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt
1804 )
1805'
1806
1807test_expect_success SANITIZE_LEAK 'stash show handles -- without leaking' '
1808 git stash show --
1809'
1810
1811test_expect_success 'controlled error return on unrecognized option' '
1812 test_expect_code 129 git stash show -p --invalid 2>usage &&
1813 test_grep -e "^usage: git stash show" usage
1814'
1815
1816test_expect_success 'stash.index=true implies --index' '
1817 # setup for a few related tests
1818 test_commit file base &&
1819 echo index >file &&
1820 git add file &&
1821 echo working >file &&
1822 git stash &&
1823
1824 test_when_finished "git reset --hard" &&
1825 git -c stash.index=true stash apply &&
1826 echo index >expect &&
1827 git show :0:file >actual &&
1828 test_cmp expect actual &&
1829 echo working >expect &&
1830 test_cmp expect file
1831'
1832
1833test_expect_success 'stash.index=true overridden by --no-index' '
1834 test_when_finished "git reset --hard" &&
1835 git -c stash.index=true stash apply --no-index &&
1836 echo base >expect &&
1837 git show :0:file >actual &&
1838 test_cmp expect actual &&
1839 echo working >expect &&
1840 test_cmp expect file
1841'
1842
1843test_expect_success 'stash.index=false overridden by --index' '
1844 test_when_finished "git reset --hard" &&
1845 git -c stash.index=false stash apply --index &&
1846 echo index >expect &&
1847 git show :0:file >actual &&
1848 test_cmp expect actual &&
1849 echo working >expect &&
1850 test_cmp expect file
1851'
1852
1853test_expect_success 'apply exits 1 on conflicts' '
1854 git reset --hard initial &&
1855 test_commit label-base conflict-file base-content &&
1856 echo stashed >conflict-file &&
1857 git stash push -m "stashed" &&
1858 test_commit label-upstream conflict-file upstream-content &&
1859 test_expect_code 1 git -c merge.conflictStyle=diff3 stash apply --label-ours=UP --label-theirs=STASH &&
1860 test_grep "^<<<<<<< UP" conflict-file &&
1861 test_grep "^||||||| Stash base" conflict-file &&
1862 test_grep "^>>>>>>> STASH" conflict-file
1863'
1864
1865test_expect_success 'apply with empty conflict labels' '
1866 git reset --hard initial &&
1867 test_commit empty-label-base conflict-file base-content &&
1868 echo stashed >conflict-file &&
1869 git stash push -m "stashed" &&
1870 test_commit empty-label-upstream conflict-file upstream-content &&
1871 test_expect_code 1 git stash apply --label-ours= --label-theirs= &&
1872 test_grep "^<<<<<<<$" conflict-file &&
1873 test_grep "^>>>>>>>$" conflict-file
1874'
1875
1876test_expect_success 'pop exits 1 on conflicts and keeps the stash entry' '
1877 git reset --hard initial &&
1878 echo stashed >file &&
1879 git stash push -m pop-stashed &&
1880 test_commit pop-upstream file upstream-content &&
1881 test_expect_code 1 git stash pop &&
1882 git stash list >list &&
1883 test_grep pop-stashed list
1884'
1885
1886test_expect_success 'stash branch exits with a non-1 status on errors' '
1887 git reset --hard initial &&
1888 echo stashed >file &&
1889 git stash push -m branch-stashed &&
1890 test_expect_code 128 git stash branch conflicting-branch refs/heads/does-not-exist &&
1891 git stash list >list &&
1892 test_grep branch-stashed list
1893'
1894
1895test_expect_success 'stash show --include-untracked includes untracked files' '
1896 git reset --hard &&
1897
1898 echo tracked >tracked &&
1899 git add tracked &&
1900 git commit -m "base" &&
1901
1902 echo change >>tracked &&
1903 echo untracked >untracked &&
1904
1905 git stash push --include-untracked &&
1906 test_path_is_missing untracked &&
1907
1908 git stash show --include-untracked >actual &&
1909 test_grep "untracked" actual
1910'
1911
1912test_done