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