]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3903-stash.sh
stash: fix "--staged" with binary files
[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
13 test_expect_success 'usage on cmd and subcommand invalid option' '
14 test_expect_code 129 git stash --invalid-option 2>usage &&
15 grep "or: git stash" usage &&
16
17 test_expect_code 129 git stash push --invalid-option 2>usage &&
18 ! grep "or: git stash" usage
19 '
20
21 test_expect_success 'usage on main command -h emits a summary of subcommands' '
22 test_expect_code 129 git stash -h >usage &&
23 grep -F "usage: git stash list" usage &&
24 grep -F "or: git stash show" usage
25 '
26
27 test_expect_failure 'usage for subcommands should emit subcommand usage' '
28 test_expect_code 129 git stash push -h >usage &&
29 grep -F "usage: git stash [push" usage
30 '
31
32 diff_cmp () {
33 for i in "$1" "$2"
34 do
35 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
36 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
37 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
38 "$i" >"$i.compare" || return 1
39 done &&
40 test_cmp "$1.compare" "$2.compare" &&
41 rm -f "$1.compare" "$2.compare"
42 }
43
44 test_expect_success 'stash some dirty working directory' '
45 echo 1 >file &&
46 git add file &&
47 echo unrelated >other-file &&
48 git add other-file &&
49 test_tick &&
50 git commit -m initial &&
51 echo 2 >file &&
52 git add file &&
53 echo 3 >file &&
54 test_tick &&
55 git stash &&
56 git diff-files --quiet &&
57 git diff-index --cached --quiet HEAD
58 '
59
60 cat >expect <<EOF
61 diff --git a/file b/file
62 index 0cfbf08..00750ed 100644
63 --- a/file
64 +++ b/file
65 @@ -1 +1 @@
66 -2
67 +3
68 EOF
69
70 test_expect_success 'parents of stash' '
71 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
72 git diff stash^2..stash >output &&
73 diff_cmp expect output
74 '
75
76 test_expect_success 'applying bogus stash does nothing' '
77 test_must_fail git stash apply stash@{1} &&
78 echo 1 >expect &&
79 test_cmp expect file
80 '
81
82 test_expect_success 'apply does not need clean working directory' '
83 echo 4 >other-file &&
84 git stash apply &&
85 echo 3 >expect &&
86 test_cmp expect file
87 '
88
89 test_expect_success 'apply does not clobber working directory changes' '
90 git reset --hard &&
91 echo 4 >file &&
92 test_must_fail git stash apply &&
93 echo 4 >expect &&
94 test_cmp expect file
95 '
96
97 test_expect_success 'apply stashed changes' '
98 git reset --hard &&
99 echo 5 >other-file &&
100 git add other-file &&
101 test_tick &&
102 git commit -m other-file &&
103 git stash apply &&
104 test 3 = $(cat file) &&
105 test 1 = $(git show :file) &&
106 test 1 = $(git show HEAD:file)
107 '
108
109 test_expect_success 'apply stashed changes (including index)' '
110 git reset --hard HEAD^ &&
111 echo 6 >other-file &&
112 git add other-file &&
113 test_tick &&
114 git commit -m other-file &&
115 git stash apply --index &&
116 test 3 = $(cat file) &&
117 test 2 = $(git show :file) &&
118 test 1 = $(git show HEAD:file)
119 '
120
121 test_expect_success 'unstashing in a subdirectory' '
122 git reset --hard HEAD &&
123 mkdir subdir &&
124 (
125 cd subdir &&
126 git stash apply
127 )
128 '
129
130 test_expect_success 'stash drop complains of extra options' '
131 test_must_fail git stash drop --foo
132 '
133
134 test_expect_success 'drop top stash' '
135 git reset --hard &&
136 git stash list >expected &&
137 echo 7 >file &&
138 git stash &&
139 git stash drop &&
140 git stash list >actual &&
141 test_cmp expected actual &&
142 git stash apply &&
143 test 3 = $(cat file) &&
144 test 1 = $(git show :file) &&
145 test 1 = $(git show HEAD:file)
146 '
147
148 test_expect_success 'drop middle stash' '
149 git reset --hard &&
150 echo 8 >file &&
151 git stash &&
152 echo 9 >file &&
153 git stash &&
154 git stash drop stash@{1} &&
155 test 2 = $(git stash list | wc -l) &&
156 git stash apply &&
157 test 9 = $(cat file) &&
158 test 1 = $(git show :file) &&
159 test 1 = $(git show HEAD:file) &&
160 git reset --hard &&
161 git stash drop &&
162 git stash apply &&
163 test 3 = $(cat file) &&
164 test 1 = $(git show :file) &&
165 test 1 = $(git show HEAD:file)
166 '
167
168 test_expect_success 'drop middle stash by index' '
169 git reset --hard &&
170 echo 8 >file &&
171 git stash &&
172 echo 9 >file &&
173 git stash &&
174 git stash drop 1 &&
175 test 2 = $(git stash list | wc -l) &&
176 git stash apply &&
177 test 9 = $(cat file) &&
178 test 1 = $(git show :file) &&
179 test 1 = $(git show HEAD:file) &&
180 git reset --hard &&
181 git stash drop &&
182 git stash apply &&
183 test 3 = $(cat file) &&
184 test 1 = $(git show :file) &&
185 test 1 = $(git show HEAD:file)
186 '
187
188 test_expect_success 'stash pop' '
189 git reset --hard &&
190 git stash pop &&
191 test 3 = $(cat file) &&
192 test 1 = $(git show :file) &&
193 test 1 = $(git show HEAD:file) &&
194 test 0 = $(git stash list | wc -l)
195 '
196
197 cat >expect <<EOF
198 diff --git a/file2 b/file2
199 new file mode 100644
200 index 0000000..1fe912c
201 --- /dev/null
202 +++ b/file2
203 @@ -0,0 +1 @@
204 +bar2
205 EOF
206
207 cat >expect1 <<EOF
208 diff --git a/file b/file
209 index 257cc56..5716ca5 100644
210 --- a/file
211 +++ b/file
212 @@ -1 +1 @@
213 -foo
214 +bar
215 EOF
216
217 cat >expect2 <<EOF
218 diff --git a/file b/file
219 index 7601807..5716ca5 100644
220 --- a/file
221 +++ b/file
222 @@ -1 +1 @@
223 -baz
224 +bar
225 diff --git a/file2 b/file2
226 new file mode 100644
227 index 0000000..1fe912c
228 --- /dev/null
229 +++ b/file2
230 @@ -0,0 +1 @@
231 +bar2
232 EOF
233
234 test_expect_success 'stash branch' '
235 echo foo >file &&
236 git commit file -m first &&
237 echo bar >file &&
238 echo bar2 >file2 &&
239 git add file2 &&
240 git stash &&
241 echo baz >file &&
242 git commit file -m second &&
243 git stash branch stashbranch &&
244 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
245 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
246 git diff --cached >output &&
247 diff_cmp expect output &&
248 git diff >output &&
249 diff_cmp expect1 output &&
250 git add file &&
251 git commit -m alternate\ second &&
252 git diff main..stashbranch >output &&
253 diff_cmp output expect2 &&
254 test 0 = $(git stash list | wc -l)
255 '
256
257 test_expect_success 'apply -q is quiet' '
258 echo foo >file &&
259 git stash &&
260 git stash apply -q >output.out 2>&1 &&
261 test_must_be_empty output.out
262 '
263
264 test_expect_success 'save -q is quiet' '
265 git stash save --quiet >output.out 2>&1 &&
266 test_must_be_empty output.out
267 '
268
269 test_expect_success 'pop -q works and is quiet' '
270 git stash pop -q >output.out 2>&1 &&
271 echo bar >expect &&
272 git show :file >actual &&
273 test_cmp expect actual &&
274 test_must_be_empty output.out
275 '
276
277 test_expect_success 'pop -q --index works and is quiet' '
278 echo foo >file &&
279 git add file &&
280 git stash save --quiet &&
281 git stash pop -q --index >output.out 2>&1 &&
282 git diff-files file2 >file2.diff &&
283 test_must_be_empty file2.diff &&
284 test foo = "$(git show :file)" &&
285 test_must_be_empty output.out
286 '
287
288 test_expect_success 'drop -q is quiet' '
289 git stash &&
290 git stash drop -q >output.out 2>&1 &&
291 test_must_be_empty output.out
292 '
293
294 test_expect_success 'stash -k' '
295 echo bar3 >file &&
296 echo bar4 >file2 &&
297 git add file2 &&
298 git stash -k &&
299 test bar,bar4 = $(cat file),$(cat file2)
300 '
301
302 test_expect_success 'stash --no-keep-index' '
303 echo bar33 >file &&
304 echo bar44 >file2 &&
305 git add file2 &&
306 git stash --no-keep-index &&
307 test bar,bar2 = $(cat file),$(cat file2)
308 '
309
310 test_expect_success 'stash --staged' '
311 echo bar3 >file &&
312 echo bar4 >file2 &&
313 git add file2 &&
314 git stash --staged &&
315 test bar3,bar2 = $(cat file),$(cat file2) &&
316 git reset --hard &&
317 git stash pop &&
318 test bar,bar4 = $(cat file),$(cat file2)
319 '
320
321 test_expect_success 'stash --staged with binary file' '
322 printf "\0" >file &&
323 git add file &&
324 git stash --staged &&
325 git stash pop &&
326 printf "\0" >expect &&
327 test_cmp expect file
328 '
329
330 test_expect_success 'dont assume push with non-option args' '
331 test_must_fail git stash -q drop 2>err &&
332 test_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
333 '
334
335 test_expect_success 'stash --invalid-option' '
336 echo bar5 >file &&
337 echo bar6 >file2 &&
338 git add file2 &&
339 test_must_fail git stash --invalid-option &&
340 test_must_fail git stash save --invalid-option &&
341 test bar5,bar6 = $(cat file),$(cat file2)
342 '
343
344 test_expect_success 'stash an added file' '
345 git reset --hard &&
346 echo new >file3 &&
347 git add file3 &&
348 git stash save "added file" &&
349 ! test -r file3 &&
350 git stash apply &&
351 test new = "$(cat file3)"
352 '
353
354 test_expect_success 'stash --intent-to-add file' '
355 git reset --hard &&
356 echo new >file4 &&
357 git add --intent-to-add file4 &&
358 test_when_finished "git rm -f file4" &&
359 test_must_fail git stash
360 '
361
362 test_expect_success 'stash rm then recreate' '
363 git reset --hard &&
364 git rm file &&
365 echo bar7 >file &&
366 git stash save "rm then recreate" &&
367 test bar = "$(cat file)" &&
368 git stash apply &&
369 test bar7 = "$(cat file)"
370 '
371
372 test_expect_success 'stash rm and ignore' '
373 git reset --hard &&
374 git rm file &&
375 echo file >.gitignore &&
376 git stash save "rm and ignore" &&
377 test bar = "$(cat file)" &&
378 test file = "$(cat .gitignore)" &&
379 git stash apply &&
380 ! test -r file &&
381 test file = "$(cat .gitignore)"
382 '
383
384 test_expect_success 'stash rm and ignore (stage .gitignore)' '
385 git reset --hard &&
386 git rm file &&
387 echo file >.gitignore &&
388 git add .gitignore &&
389 git stash save "rm and ignore (stage .gitignore)" &&
390 test bar = "$(cat file)" &&
391 ! test -r .gitignore &&
392 git stash apply &&
393 ! test -r file &&
394 test file = "$(cat .gitignore)"
395 '
396
397 test_expect_success SYMLINKS 'stash file to symlink' '
398 git reset --hard &&
399 rm file &&
400 ln -s file2 file &&
401 git stash save "file to symlink" &&
402 test -f file &&
403 test bar = "$(cat file)" &&
404 git stash apply &&
405 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
406 '
407
408 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
409 git reset --hard &&
410 git rm file &&
411 ln -s file2 file &&
412 git stash save "file to symlink (stage rm)" &&
413 test -f file &&
414 test bar = "$(cat file)" &&
415 git stash apply &&
416 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
417 '
418
419 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
420 git reset --hard &&
421 rm file &&
422 ln -s file2 file &&
423 git add file &&
424 git stash save "file to symlink (full stage)" &&
425 test -f file &&
426 test bar = "$(cat file)" &&
427 git stash apply &&
428 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
429 '
430
431 # This test creates a commit with a symlink used for the following tests
432
433 test_expect_success 'stash symlink to file' '
434 git reset --hard &&
435 test_ln_s_add file filelink &&
436 git commit -m "Add symlink" &&
437 rm filelink &&
438 cp file filelink &&
439 git stash save "symlink to file"
440 '
441
442 test_expect_success SYMLINKS 'this must have re-created the symlink' '
443 test -h filelink &&
444 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
445 '
446
447 test_expect_success 'unstash must re-create the file' '
448 git stash apply &&
449 ! test -h filelink &&
450 test bar = "$(cat file)"
451 '
452
453 test_expect_success 'stash symlink to file (stage rm)' '
454 git reset --hard &&
455 git rm filelink &&
456 cp file filelink &&
457 git stash save "symlink to file (stage rm)"
458 '
459
460 test_expect_success SYMLINKS 'this must have re-created the symlink' '
461 test -h filelink &&
462 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
463 '
464
465 test_expect_success 'unstash must re-create the file' '
466 git stash apply &&
467 ! test -h filelink &&
468 test bar = "$(cat file)"
469 '
470
471 test_expect_success 'stash symlink to file (full stage)' '
472 git reset --hard &&
473 rm filelink &&
474 cp file filelink &&
475 git add filelink &&
476 git stash save "symlink to file (full stage)"
477 '
478
479 test_expect_success SYMLINKS 'this must have re-created the symlink' '
480 test -h filelink &&
481 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
482 '
483
484 test_expect_success 'unstash must re-create the file' '
485 git stash apply &&
486 ! test -h filelink &&
487 test bar = "$(cat file)"
488 '
489
490 test_expect_failure 'stash directory to file' '
491 git reset --hard &&
492 mkdir dir &&
493 echo foo >dir/file &&
494 git add dir/file &&
495 git commit -m "Add file in dir" &&
496 rm -fr dir &&
497 echo bar >dir &&
498 git stash save "directory to file" &&
499 test -d dir &&
500 test foo = "$(cat dir/file)" &&
501 test_must_fail git stash apply &&
502 test bar = "$(cat dir)" &&
503 git reset --soft HEAD^
504 '
505
506 test_expect_failure 'stash file to directory' '
507 git reset --hard &&
508 rm file &&
509 mkdir file &&
510 echo foo >file/file &&
511 git stash save "file to directory" &&
512 test -f file &&
513 test bar = "$(cat file)" &&
514 git stash apply &&
515 test -f file/file &&
516 test foo = "$(cat file/file)"
517 '
518
519 test_expect_success 'giving too many ref arguments does not modify files' '
520 git stash clear &&
521 test_when_finished "git reset --hard HEAD" &&
522 echo foo >file2 &&
523 git stash &&
524 echo bar >file2 &&
525 git stash &&
526 test-tool chmtime =123456789 file2 &&
527 for type in apply pop "branch stash-branch"
528 do
529 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
530 test_i18ngrep "Too many revisions" err &&
531 test 123456789 = $(test-tool chmtime -g file2) || return 1
532 done
533 '
534
535 test_expect_success 'drop: too many arguments errors out (does nothing)' '
536 git stash list >expect &&
537 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
538 test_i18ngrep "Too many revisions" err &&
539 git stash list >actual &&
540 test_cmp expect actual
541 '
542
543 test_expect_success 'show: too many arguments errors out (does nothing)' '
544 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
545 test_i18ngrep "Too many revisions" err &&
546 test_must_be_empty out
547 '
548
549 test_expect_success 'stash create - no changes' '
550 git stash clear &&
551 test_when_finished "git reset --hard HEAD" &&
552 git reset --hard &&
553 git stash create >actual &&
554 test_must_be_empty actual
555 '
556
557 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
558 git stash clear &&
559 test_when_finished "git reset --hard HEAD" &&
560 git reset --hard &&
561 echo foo >>file &&
562 STASH_ID=$(git stash create) &&
563 git reset --hard &&
564 git stash branch stash-branch ${STASH_ID} &&
565 test_when_finished "git reset --hard HEAD && git checkout main &&
566 git branch -D stash-branch" &&
567 test $(git ls-files --modified | wc -l) -eq 1
568 '
569
570 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
571 git stash clear &&
572 test_when_finished "git reset --hard HEAD" &&
573 git reset --hard &&
574 echo foo >>file &&
575 git stash &&
576 test_when_finished "git stash drop" &&
577 echo bar >>file &&
578 STASH_ID=$(git stash create) &&
579 git reset --hard &&
580 git stash branch stash-branch ${STASH_ID} &&
581 test_when_finished "git reset --hard HEAD && git checkout main &&
582 git branch -D stash-branch" &&
583 test $(git ls-files --modified | wc -l) -eq 1
584 '
585
586 test_expect_success 'stash branch complains with no arguments' '
587 test_must_fail git stash branch 2>err &&
588 test_i18ngrep "No branch name specified" err
589 '
590
591 test_expect_success 'stash show format defaults to --stat' '
592 git stash clear &&
593 test_when_finished "git reset --hard HEAD" &&
594 git reset --hard &&
595 echo foo >>file &&
596 git stash &&
597 test_when_finished "git stash drop" &&
598 echo bar >>file &&
599 STASH_ID=$(git stash create) &&
600 git reset --hard &&
601 cat >expected <<-EOF &&
602 file | 1 +
603 1 file changed, 1 insertion(+)
604 EOF
605 git stash show ${STASH_ID} >actual &&
606 test_cmp expected actual
607 '
608
609 test_expect_success 'stash show - stashes on stack, stash-like argument' '
610 git stash clear &&
611 test_when_finished "git reset --hard HEAD" &&
612 git reset --hard &&
613 echo foo >>file &&
614 git stash &&
615 test_when_finished "git stash drop" &&
616 echo bar >>file &&
617 STASH_ID=$(git stash create) &&
618 git reset --hard &&
619 echo "1 0 file" >expected &&
620 git stash show --numstat ${STASH_ID} >actual &&
621 test_cmp expected actual
622 '
623
624 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
625 git stash clear &&
626 test_when_finished "git reset --hard HEAD" &&
627 git reset --hard &&
628 echo foo >>file &&
629 git stash &&
630 test_when_finished "git stash drop" &&
631 echo bar >>file &&
632 STASH_ID=$(git stash create) &&
633 git reset --hard &&
634 cat >expected <<-EOF &&
635 diff --git a/file b/file
636 index 7601807..935fbd3 100644
637 --- a/file
638 +++ b/file
639 @@ -1 +1,2 @@
640 baz
641 +bar
642 EOF
643 git stash show -p ${STASH_ID} >actual &&
644 diff_cmp expected actual
645 '
646
647 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
648 git stash clear &&
649 test_when_finished "git reset --hard HEAD" &&
650 git reset --hard &&
651 echo foo >>file &&
652 STASH_ID=$(git stash create) &&
653 git reset --hard &&
654 echo "1 0 file" >expected &&
655 git stash show --numstat ${STASH_ID} >actual &&
656 test_cmp expected actual
657 '
658
659 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
660 git stash clear &&
661 test_when_finished "git reset --hard HEAD" &&
662 git reset --hard &&
663 echo foo >>file &&
664 STASH_ID=$(git stash create) &&
665 git reset --hard &&
666 cat >expected <<-EOF &&
667 diff --git a/file b/file
668 index 7601807..71b52c4 100644
669 --- a/file
670 +++ b/file
671 @@ -1 +1,2 @@
672 baz
673 +foo
674 EOF
675 git stash show -p ${STASH_ID} >actual &&
676 diff_cmp expected actual
677 '
678
679 test_expect_success 'stash show --patience shows diff' '
680 git reset --hard &&
681 echo foo >>file &&
682 STASH_ID=$(git stash create) &&
683 git reset --hard &&
684 cat >expected <<-EOF &&
685 diff --git a/file b/file
686 index 7601807..71b52c4 100644
687 --- a/file
688 +++ b/file
689 @@ -1 +1,2 @@
690 baz
691 +foo
692 EOF
693 git stash show --patience ${STASH_ID} >actual &&
694 diff_cmp expected actual
695 '
696
697 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
698 git stash clear &&
699 test_when_finished "git reset --hard HEAD && git stash clear" &&
700 git reset --hard &&
701 echo foo >file &&
702 git stash &&
703 echo bar >file &&
704 git stash &&
705 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
706 git stash pop &&
707 test bar = "$(cat file)" &&
708 git reset --hard HEAD
709 '
710
711 test_expect_success 'pop: fail early if specified stash is not a stash ref' '
712 git stash clear &&
713 test_when_finished "git reset --hard HEAD && git stash clear" &&
714 git reset --hard &&
715 echo foo >file &&
716 git stash &&
717 echo bar >file &&
718 git stash &&
719 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
720 git stash pop &&
721 test bar = "$(cat file)" &&
722 git reset --hard HEAD
723 '
724
725 test_expect_success 'ref with non-existent reflog' '
726 git stash clear &&
727 echo bar5 >file &&
728 echo bar6 >file2 &&
729 git add file2 &&
730 git stash &&
731 test_must_fail git rev-parse --quiet --verify does-not-exist &&
732 test_must_fail git stash drop does-not-exist &&
733 test_must_fail git stash drop does-not-exist@{0} &&
734 test_must_fail git stash pop does-not-exist &&
735 test_must_fail git stash pop does-not-exist@{0} &&
736 test_must_fail git stash apply does-not-exist &&
737 test_must_fail git stash apply does-not-exist@{0} &&
738 test_must_fail git stash show does-not-exist &&
739 test_must_fail git stash show does-not-exist@{0} &&
740 test_must_fail git stash branch tmp does-not-exist &&
741 test_must_fail git stash branch tmp does-not-exist@{0} &&
742 git stash drop
743 '
744
745 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
746 git stash clear &&
747 test_must_fail git stash drop stash@{0} &&
748 echo bar5 >file &&
749 echo bar6 >file2 &&
750 git add file2 &&
751 git stash &&
752 test_must_fail git stash drop stash@{1} &&
753 test_must_fail git stash pop stash@{1} &&
754 test_must_fail git stash apply stash@{1} &&
755 test_must_fail git stash show stash@{1} &&
756 test_must_fail git stash branch tmp stash@{1} &&
757 git stash drop
758 '
759
760 test_expect_success 'invalid ref of the form "n", n >= N' '
761 git stash clear &&
762 test_must_fail git stash drop 0 &&
763 echo bar5 >file &&
764 echo bar6 >file2 &&
765 git add file2 &&
766 git stash &&
767 test_must_fail git stash drop 1 &&
768 test_must_fail git stash pop 1 &&
769 test_must_fail git stash apply 1 &&
770 test_must_fail git stash show 1 &&
771 test_must_fail git stash branch tmp 1 &&
772 git stash drop
773 '
774
775 test_expect_success 'valid ref of the form "n", n < N' '
776 git stash clear &&
777 echo bar5 >file &&
778 echo bar6 >file2 &&
779 git add file2 &&
780 git stash &&
781 git stash show 0 &&
782 git stash branch tmp 0 &&
783 git checkout main &&
784 git stash &&
785 git stash apply 0 &&
786 git reset --hard &&
787 git stash pop 0 &&
788 git stash &&
789 git stash drop 0 &&
790 test_must_fail git stash drop
791 '
792
793 test_expect_success 'branch: do not drop the stash if the branch exists' '
794 git stash clear &&
795 echo foo >file &&
796 git add file &&
797 git commit -m initial &&
798 echo bar >file &&
799 git stash &&
800 test_must_fail git stash branch main stash@{0} &&
801 git rev-parse stash@{0} --
802 '
803
804 test_expect_success 'branch: should not drop the stash if the apply fails' '
805 git stash clear &&
806 git reset HEAD~1 --hard &&
807 echo foo >file &&
808 git add file &&
809 git commit -m initial &&
810 echo bar >file &&
811 git stash &&
812 echo baz >file &&
813 test_when_finished "git checkout main" &&
814 test_must_fail git stash branch new_branch stash@{0} &&
815 git rev-parse stash@{0} --
816 '
817
818 test_expect_success 'apply: show same status as git status (relative to ./)' '
819 git stash clear &&
820 echo 1 >subdir/subfile1 &&
821 echo 2 >subdir/subfile2 &&
822 git add subdir/subfile1 &&
823 git commit -m subdir &&
824 (
825 cd subdir &&
826 echo x >subfile1 &&
827 echo x >../file &&
828 git status >../expect &&
829 git stash &&
830 sane_unset GIT_MERGE_VERBOSITY &&
831 git stash apply
832 ) |
833 sed -e 1d >actual && # drop "Saved..."
834 test_cmp expect actual
835 '
836
837 cat >expect <<EOF
838 diff --git a/HEAD b/HEAD
839 new file mode 100644
840 index 0000000..fe0cbee
841 --- /dev/null
842 +++ b/HEAD
843 @@ -0,0 +1 @@
844 +file-not-a-ref
845 EOF
846
847 test_expect_success 'stash where working directory contains "HEAD" file' '
848 git stash clear &&
849 git reset --hard &&
850 echo file-not-a-ref >HEAD &&
851 git add HEAD &&
852 test_tick &&
853 git stash &&
854 git diff-files --quiet &&
855 git diff-index --cached --quiet HEAD &&
856 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
857 git diff stash^..stash >output &&
858 diff_cmp expect output
859 '
860
861 test_expect_success 'store called with invalid commit' '
862 test_must_fail git stash store foo
863 '
864
865 test_expect_success 'store updates stash ref and reflog' '
866 git stash clear &&
867 git reset --hard &&
868 echo quux >bazzy &&
869 git add bazzy &&
870 STASH_ID=$(git stash create) &&
871 git reset --hard &&
872 test_path_is_missing bazzy &&
873 git stash store -m quuxery $STASH_ID &&
874 test $(git rev-parse stash) = $STASH_ID &&
875 git reflog --format=%H stash| grep $STASH_ID &&
876 git stash pop &&
877 grep quux bazzy
878 '
879
880 test_expect_success 'handle stash specification with spaces' '
881 git stash clear &&
882 echo pig >file &&
883 git stash &&
884 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
885 test_tick &&
886 echo cow >file &&
887 git stash &&
888 git stash apply "stash@{$stamp}" &&
889 grep pig file
890 '
891
892 test_expect_success 'setup stash with index and worktree changes' '
893 git stash clear &&
894 git reset --hard &&
895 echo index >file &&
896 git add file &&
897 echo working >file &&
898 git stash
899 '
900
901 test_expect_success 'stash list -p shows simple diff' '
902 cat >expect <<-EOF &&
903 stash@{0}
904
905 diff --git a/file b/file
906 index 257cc56..d26b33d 100644
907 --- a/file
908 +++ b/file
909 @@ -1 +1 @@
910 -foo
911 +working
912 EOF
913 git stash list --format=%gd -p >actual &&
914 diff_cmp expect actual
915 '
916
917 test_expect_success 'stash list --cc shows combined diff' '
918 cat >expect <<-\EOF &&
919 stash@{0}
920
921 diff --cc file
922 index 257cc56,9015a7a..d26b33d
923 --- a/file
924 +++ b/file
925 @@@ -1,1 -1,1 +1,1 @@@
926 - foo
927 -index
928 ++working
929 EOF
930 git stash list --format=%gd -p --cc >actual &&
931 diff_cmp expect actual
932 '
933
934 test_expect_success 'stash is not confused by partial renames' '
935 mv file renamed &&
936 git add renamed &&
937 git stash &&
938 git stash apply &&
939 test_path_is_file renamed &&
940 test_path_is_missing file
941 '
942
943 test_expect_success 'push -m shows right message' '
944 >foo &&
945 git add foo &&
946 git stash push -m "test message" &&
947 echo "stash@{0}: On main: test message" >expect &&
948 git stash list -1 >actual &&
949 test_cmp expect actual
950 '
951
952 test_expect_success 'push -m also works without space' '
953 >foo &&
954 git add foo &&
955 git stash push -m"unspaced test message" &&
956 echo "stash@{0}: On main: unspaced test message" >expect &&
957 git stash list -1 >actual &&
958 test_cmp expect actual
959 '
960
961 test_expect_success 'store -m foo shows right message' '
962 git stash clear &&
963 git reset --hard &&
964 echo quux >bazzy &&
965 git add bazzy &&
966 STASH_ID=$(git stash create) &&
967 git stash store -m "store m" $STASH_ID &&
968 echo "stash@{0}: store m" >expect &&
969 git stash list -1 >actual &&
970 test_cmp expect actual
971 '
972
973 test_expect_success 'store -mfoo shows right message' '
974 git stash clear &&
975 git reset --hard &&
976 echo quux >bazzy &&
977 git add bazzy &&
978 STASH_ID=$(git stash create) &&
979 git stash store -m"store mfoo" $STASH_ID &&
980 echo "stash@{0}: store mfoo" >expect &&
981 git stash list -1 >actual &&
982 test_cmp expect actual
983 '
984
985 test_expect_success 'store --message=foo shows right message' '
986 git stash clear &&
987 git reset --hard &&
988 echo quux >bazzy &&
989 git add bazzy &&
990 STASH_ID=$(git stash create) &&
991 git stash store --message="store message=foo" $STASH_ID &&
992 echo "stash@{0}: store message=foo" >expect &&
993 git stash list -1 >actual &&
994 test_cmp expect actual
995 '
996
997 test_expect_success 'store --message foo shows right message' '
998 git stash clear &&
999 git reset --hard &&
1000 echo quux >bazzy &&
1001 git add bazzy &&
1002 STASH_ID=$(git stash create) &&
1003 git stash store --message "store message foo" $STASH_ID &&
1004 echo "stash@{0}: store message foo" >expect &&
1005 git stash list -1 >actual &&
1006 test_cmp expect actual
1007 '
1008
1009 test_expect_success 'push -mfoo uses right message' '
1010 >foo &&
1011 git add foo &&
1012 git stash push -m"test mfoo" &&
1013 echo "stash@{0}: On main: test mfoo" >expect &&
1014 git stash list -1 >actual &&
1015 test_cmp expect actual
1016 '
1017
1018 test_expect_success 'push --message foo is synonym for -mfoo' '
1019 >foo &&
1020 git add foo &&
1021 git stash push --message "test message foo" &&
1022 echo "stash@{0}: On main: test message foo" >expect &&
1023 git stash list -1 >actual &&
1024 test_cmp expect actual
1025 '
1026
1027 test_expect_success 'push --message=foo is synonym for -mfoo' '
1028 >foo &&
1029 git add foo &&
1030 git stash push --message="test message=foo" &&
1031 echo "stash@{0}: On main: test message=foo" >expect &&
1032 git stash list -1 >actual &&
1033 test_cmp expect actual
1034 '
1035
1036 test_expect_success 'push -m shows right message' '
1037 >foo &&
1038 git add foo &&
1039 git stash push -m "test m foo" &&
1040 echo "stash@{0}: On main: test m foo" >expect &&
1041 git stash list -1 >actual &&
1042 test_cmp expect actual
1043 '
1044
1045 test_expect_success 'create stores correct message' '
1046 >foo &&
1047 git add foo &&
1048 STASH_ID=$(git stash create "create test message") &&
1049 echo "On main: create test message" >expect &&
1050 git show --pretty=%s -s ${STASH_ID} >actual &&
1051 test_cmp expect actual
1052 '
1053
1054 test_expect_success 'create with multiple arguments for the message' '
1055 >foo &&
1056 git add foo &&
1057 STASH_ID=$(git stash create test untracked) &&
1058 echo "On main: test untracked" >expect &&
1059 git show --pretty=%s -s ${STASH_ID} >actual &&
1060 test_cmp expect actual
1061 '
1062
1063 test_expect_success 'create in a detached state' '
1064 test_when_finished "git checkout main" &&
1065 git checkout HEAD~1 &&
1066 >foo &&
1067 git add foo &&
1068 STASH_ID=$(git stash create) &&
1069 HEAD_ID=$(git rev-parse --short HEAD) &&
1070 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1071 git show --pretty=%s -s ${STASH_ID} >actual &&
1072 test_cmp expect actual
1073 '
1074
1075 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1076 >foo &&
1077 >bar &&
1078 git add foo bar &&
1079 git stash push -- foo &&
1080 test_path_is_file bar &&
1081 test_path_is_missing foo &&
1082 git stash pop &&
1083 test_path_is_file foo &&
1084 test_path_is_file bar
1085 '
1086
1087 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1088 mkdir sub &&
1089 >foo &&
1090 >bar &&
1091 git add foo bar &&
1092 (
1093 cd sub &&
1094 git stash push -- ../foo
1095 ) &&
1096 test_path_is_file bar &&
1097 test_path_is_missing foo &&
1098 git stash pop &&
1099 test_path_is_file foo &&
1100 test_path_is_file bar
1101 '
1102
1103 test_expect_success 'stash with multiple pathspec arguments' '
1104 >foo &&
1105 >bar &&
1106 >extra &&
1107 git add foo bar extra &&
1108 git stash push -- foo bar &&
1109 test_path_is_missing bar &&
1110 test_path_is_missing foo &&
1111 test_path_is_file extra &&
1112 git stash pop &&
1113 test_path_is_file foo &&
1114 test_path_is_file bar &&
1115 test_path_is_file extra
1116 '
1117
1118 test_expect_success 'stash with file including $IFS character' '
1119 >"foo bar" &&
1120 >foo &&
1121 >bar &&
1122 git add foo* &&
1123 git stash push -- "foo b*" &&
1124 test_path_is_missing "foo bar" &&
1125 test_path_is_file foo &&
1126 test_path_is_file bar &&
1127 git stash pop &&
1128 test_path_is_file "foo bar" &&
1129 test_path_is_file foo &&
1130 test_path_is_file bar
1131 '
1132
1133 test_expect_success 'stash with pathspec matching multiple paths' '
1134 echo original >file &&
1135 echo original >other-file &&
1136 git commit -m "two" file other-file &&
1137 echo modified >file &&
1138 echo modified >other-file &&
1139 git stash push -- "*file" &&
1140 echo original >expect &&
1141 test_cmp expect file &&
1142 test_cmp expect other-file &&
1143 git stash pop &&
1144 echo modified >expect &&
1145 test_cmp expect file &&
1146 test_cmp expect other-file
1147 '
1148
1149 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1150 >foo &&
1151 git add foo &&
1152 git commit -m "tmp" &&
1153 git stash push -p foo >actual &&
1154 echo "No local changes to save" >expect &&
1155 git reset --hard HEAD~ &&
1156 test_cmp expect actual
1157 '
1158
1159 test_expect_success 'push <pathspec>: show no changes when there are none' '
1160 >foo &&
1161 git add foo &&
1162 git commit -m "tmp" &&
1163 git stash push foo >actual &&
1164 echo "No local changes to save" >expect &&
1165 git reset --hard HEAD~ &&
1166 test_cmp expect actual
1167 '
1168
1169 test_expect_success 'push: <pathspec> not in the repository errors out' '
1170 >untracked &&
1171 test_must_fail git stash push untracked &&
1172 test_path_is_file untracked
1173 '
1174
1175 test_expect_success 'push: -q is quiet with changes' '
1176 >foo &&
1177 git add foo &&
1178 git stash push -q >output 2>&1 &&
1179 test_must_be_empty output
1180 '
1181
1182 test_expect_success 'push: -q is quiet with no changes' '
1183 git stash push -q >output 2>&1 &&
1184 test_must_be_empty output
1185 '
1186
1187 test_expect_success 'push: -q is quiet even if there is no initial commit' '
1188 git init foo_dir &&
1189 test_when_finished rm -rf foo_dir &&
1190 (
1191 cd foo_dir &&
1192 >bar &&
1193 test_must_fail git stash push -q >output 2>&1 &&
1194 test_must_be_empty output
1195 )
1196 '
1197
1198 test_expect_success 'untracked files are left in place when -u is not given' '
1199 >file &&
1200 git add file &&
1201 >untracked &&
1202 git stash push file &&
1203 test_path_is_file untracked
1204 '
1205
1206 test_expect_success 'stash without verb with pathspec' '
1207 >"foo bar" &&
1208 >foo &&
1209 >bar &&
1210 git add foo* &&
1211 git stash -- "foo b*" &&
1212 test_path_is_missing "foo bar" &&
1213 test_path_is_file foo &&
1214 test_path_is_file bar &&
1215 git stash pop &&
1216 test_path_is_file "foo bar" &&
1217 test_path_is_file foo &&
1218 test_path_is_file bar
1219 '
1220
1221 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1222 git reset &&
1223 >foo &&
1224 >bar &&
1225 git add foo bar &&
1226 git commit -m "test" &&
1227 echo "foo" >foo &&
1228 echo "bar" >bar &&
1229 git stash -k -- foo &&
1230 test "",bar = $(cat foo),$(cat bar) &&
1231 git stash pop &&
1232 test foo,bar = $(cat foo),$(cat bar)
1233 '
1234
1235 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1236 git reset &&
1237 >subdir/untracked &&
1238 >subdir/tracked1 &&
1239 >subdir/tracked2 &&
1240 git add subdir/tracked* &&
1241 git stash -- subdir/ &&
1242 test_path_is_missing subdir/tracked1 &&
1243 test_path_is_missing subdir/tracked2 &&
1244 test_path_is_file subdir/untracked &&
1245 git stash pop &&
1246 test_path_is_file subdir/tracked1 &&
1247 test_path_is_file subdir/tracked2 &&
1248 test_path_is_file subdir/untracked
1249 '
1250
1251 test_expect_success 'stash -- <subdir> works with binary files' '
1252 git reset &&
1253 >subdir/untracked &&
1254 >subdir/tracked &&
1255 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1256 git add subdir/tracked* &&
1257 git stash -- subdir/ &&
1258 test_path_is_missing subdir/tracked &&
1259 test_path_is_missing subdir/tracked-binary &&
1260 test_path_is_file subdir/untracked &&
1261 git stash pop &&
1262 test_path_is_file subdir/tracked &&
1263 test_path_is_file subdir/tracked-binary &&
1264 test_path_is_file subdir/untracked
1265 '
1266
1267 test_expect_success 'stash with user.name and user.email set works' '
1268 test_config user.name "A U Thor" &&
1269 test_config user.email "a.u@thor" &&
1270 git stash
1271 '
1272
1273 test_expect_success 'stash works when user.name and user.email are not set' '
1274 git reset &&
1275 >1 &&
1276 git add 1 &&
1277 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1278 git stash &&
1279 git show -s --format="%an <%ae>" refs/stash >actual &&
1280 test_cmp expect actual &&
1281 >2 &&
1282 git add 2 &&
1283 test_config user.useconfigonly true &&
1284 test_config stash.usebuiltin true &&
1285 (
1286 sane_unset GIT_AUTHOR_NAME &&
1287 sane_unset GIT_AUTHOR_EMAIL &&
1288 sane_unset GIT_COMMITTER_NAME &&
1289 sane_unset GIT_COMMITTER_EMAIL &&
1290 test_unconfig user.email &&
1291 test_unconfig user.name &&
1292 test_must_fail git commit -m "should fail" &&
1293 echo "git stash <git@stash>" >expect &&
1294 >2 &&
1295 git stash &&
1296 git show -s --format="%an <%ae>" refs/stash >actual &&
1297 test_cmp expect actual
1298 )
1299 '
1300
1301 test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1302 test_commit to-remove to-remove &&
1303 git rm to-remove &&
1304 git stash --keep-index &&
1305 test_path_is_missing to-remove
1306 '
1307
1308 test_expect_success 'stash apply should succeed with unmodified file' '
1309 echo base >file &&
1310 git add file &&
1311 git commit -m base &&
1312
1313 # now stash a modification
1314 echo modified >file &&
1315 git stash &&
1316
1317 # make the file stat dirty
1318 cp file other &&
1319 mv other file &&
1320
1321 git stash apply
1322 '
1323
1324 test_expect_success 'stash handles skip-worktree entries nicely' '
1325 test_commit A &&
1326 echo changed >A.t &&
1327 git add A.t &&
1328 git update-index --skip-worktree A.t &&
1329 rm A.t &&
1330 git stash &&
1331
1332 git rev-parse --verify refs/stash:A.t
1333 '
1334
1335 test_expect_success 'stash -c stash.useBuiltin=false warning ' '
1336 expected="stash.useBuiltin support has been removed" &&
1337
1338 git -c stash.useBuiltin=false stash 2>err &&
1339 test_i18ngrep "$expected" err &&
1340 env GIT_TEST_STASH_USE_BUILTIN=false git stash 2>err &&
1341 test_i18ngrep "$expected" err &&
1342
1343 git -c stash.useBuiltin=true stash 2>err &&
1344 test_must_be_empty err &&
1345 env GIT_TEST_STASH_USE_BUILTIN=true git stash 2>err &&
1346 test_must_be_empty err
1347 '
1348
1349 test_expect_success 'git stash succeeds despite directory/file change' '
1350 test_create_repo directory_file_switch_v1 &&
1351 (
1352 cd directory_file_switch_v1 &&
1353 test_commit init &&
1354
1355 test_write_lines this file has some words >filler &&
1356 git add filler &&
1357 git commit -m filler &&
1358
1359 git rm filler &&
1360 mkdir filler &&
1361 echo contents >filler/file &&
1362 git stash push
1363 )
1364 '
1365
1366 test_expect_success 'git stash can pop file -> directory saved changes' '
1367 test_create_repo directory_file_switch_v2 &&
1368 (
1369 cd directory_file_switch_v2 &&
1370 test_commit init &&
1371
1372 test_write_lines this file has some words >filler &&
1373 git add filler &&
1374 git commit -m filler &&
1375
1376 git rm filler &&
1377 mkdir filler &&
1378 echo contents >filler/file &&
1379 cp filler/file expect &&
1380 git stash push --include-untracked &&
1381 git stash apply --index &&
1382 test_cmp expect filler/file
1383 )
1384 '
1385
1386 test_expect_success 'git stash can pop directory -> file saved changes' '
1387 test_create_repo directory_file_switch_v3 &&
1388 (
1389 cd directory_file_switch_v3 &&
1390 test_commit init &&
1391
1392 mkdir filler &&
1393 test_write_lines some words >filler/file1 &&
1394 test_write_lines and stuff >filler/file2 &&
1395 git add filler &&
1396 git commit -m filler &&
1397
1398 git rm -rf filler &&
1399 echo contents >filler &&
1400 cp filler expect &&
1401 git stash push --include-untracked &&
1402 git stash apply --index &&
1403 test_cmp expect filler
1404 )
1405 '
1406
1407 test_expect_success 'restore untracked files even when we hit conflicts' '
1408 git init restore_untracked_after_conflict &&
1409 (
1410 cd restore_untracked_after_conflict &&
1411
1412 echo hi >a &&
1413 echo there >b &&
1414 git add . &&
1415 git commit -m first &&
1416 echo hello >a &&
1417 echo something >c &&
1418
1419 git stash push --include-untracked &&
1420
1421 echo conflict >a &&
1422 git add a &&
1423 git commit -m second &&
1424
1425 test_must_fail git stash pop &&
1426
1427 test_path_is_file c
1428 )
1429 '
1430
1431 test_done