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