1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes E Schindelin
4#
5
6test_description='Test git stash'
7
8. ./test-lib.sh
9
10test_expect_success 'stash some dirty working directory' '
11 echo 1 > file &&
12 git add file &&
13 echo unrelated >other-file &&
14 git add other-file &&
15 test_tick &&
16 git commit -m initial &&
17 echo 2 > file &&
18 git add file &&
19 echo 3 > file &&
20 test_tick &&
21 git stash &&
22 git diff-files --quiet &&
23 git diff-index --cached --quiet HEAD
24'
25
26cat > expect << EOF
27diff --git a/file b/file
28index 0cfbf08..00750ed 100644
29--- a/file
30+++ b/file
31@@ -1 +1 @@
32-2
33+3
34EOF
35
36test_expect_success 'parents of stash' '
37 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
38 git diff stash^2..stash > output &&
39 test_cmp output expect
40'
41
42test_expect_success 'applying bogus stash does nothing' '
43 test_must_fail git stash apply stash@{1} &&
44 echo 1 >expect &&
45 test_cmp expect file
46'
47
48test_expect_success 'apply does not need clean working directory' '
49 echo 4 >other-file &&
50 git stash apply &&
51 echo 3 >expect &&
52 test_cmp expect file
53'
54
55test_expect_success 'apply does not clobber working directory changes' '
56 git reset --hard &&
57 echo 4 >file &&
58 test_must_fail git stash apply &&
59 echo 4 >expect &&
60 test_cmp expect file
61'
62
63test_expect_success 'apply stashed changes' '
64 git reset --hard &&
65 echo 5 >other-file &&
66 git add other-file &&
67 test_tick &&
68 git commit -m other-file &&
69 git stash apply &&
70 test 3 = $(cat file) &&
71 test 1 = $(git show :file) &&
72 test 1 = $(git show HEAD:file)
73'
74
75test_expect_success 'apply stashed changes (including index)' '
76 git reset --hard HEAD^ &&
77 echo 6 > other-file &&
78 git add other-file &&
79 test_tick &&
80 git commit -m other-file &&
81 git stash apply --index &&
82 test 3 = $(cat file) &&
83 test 2 = $(git show :file) &&
84 test 1 = $(git show HEAD:file)
85'
86
87test_expect_success 'unstashing in a subdirectory' '
88 git reset --hard HEAD &&
89 mkdir subdir &&
90 (
91 cd subdir &&
92 git stash apply
93 )
94'
95
96test_expect_success 'stash drop complains of extra options' '
97 test_must_fail git stash drop --foo
98'
99
100test_expect_success 'drop top stash' '
101 git reset --hard &&
102 git stash list > stashlist1 &&
103 echo 7 > file &&
104 git stash &&
105 git stash drop &&
106 git stash list > stashlist2 &&
107 test_cmp stashlist1 stashlist2 &&
108 git stash apply &&
109 test 3 = $(cat file) &&
110 test 1 = $(git show :file) &&
111 test 1 = $(git show HEAD:file)
112'
113
114test_expect_success 'drop middle stash' '
115 git reset --hard &&
116 echo 8 > file &&
117 git stash &&
118 echo 9 > file &&
119 git stash &&
120 git stash drop stash@{1} &&
121 test 2 = $(git stash list | wc -l) &&
122 git stash apply &&
123 test 9 = $(cat file) &&
124 test 1 = $(git show :file) &&
125 test 1 = $(git show HEAD:file) &&
126 git reset --hard &&
127 git stash drop &&
128 git stash apply &&
129 test 3 = $(cat file) &&
130 test 1 = $(git show :file) &&
131 test 1 = $(git show HEAD:file)
132'
133
134test_expect_success 'drop middle stash by index' '
135 git reset --hard &&
136 echo 8 >file &&
137 git stash &&
138 echo 9 >file &&
139 git stash &&
140 git stash drop 1 &&
141 test 2 = $(git stash list | wc -l) &&
142 git stash apply &&
143 test 9 = $(cat file) &&
144 test 1 = $(git show :file) &&
145 test 1 = $(git show HEAD:file) &&
146 git reset --hard &&
147 git stash drop &&
148 git stash apply &&
149 test 3 = $(cat file) &&
150 test 1 = $(git show :file) &&
151 test 1 = $(git show HEAD:file)
152'
153
154test_expect_success 'stash pop' '
155 git reset --hard &&
156 git stash pop &&
157 test 3 = $(cat file) &&
158 test 1 = $(git show :file) &&
159 test 1 = $(git show HEAD:file) &&
160 test 0 = $(git stash list | wc -l)
161'
162
163cat > expect << EOF
164diff --git a/file2 b/file2
165new file mode 100644
166index 0000000..1fe912c
167--- /dev/null
168+++ b/file2
169@@ -0,0 +1 @@
170+bar2
171EOF
172
173cat > expect1 << EOF
174diff --git a/file b/file
175index 257cc56..5716ca5 100644
176--- a/file
177+++ b/file
178@@ -1 +1 @@
179-foo
180+bar
181EOF
182
183cat > expect2 << EOF
184diff --git a/file b/file
185index 7601807..5716ca5 100644
186--- a/file
187+++ b/file
188@@ -1 +1 @@
189-baz
190+bar
191diff --git a/file2 b/file2
192new file mode 100644
193index 0000000..1fe912c
194--- /dev/null
195+++ b/file2
196@@ -0,0 +1 @@
197+bar2
198EOF
199
200test_expect_success 'stash branch' '
201 echo foo > file &&
202 git commit file -m first &&
203 echo bar > file &&
204 echo bar2 > file2 &&
205 git add file2 &&
206 git stash &&
207 echo baz > file &&
208 git commit file -m second &&
209 git stash branch stashbranch &&
210 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
211 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
212 git diff --cached > output &&
213 test_cmp output expect &&
214 git diff > output &&
215 test_cmp output expect1 &&
216 git add file &&
217 git commit -m alternate\ second &&
218 git diff master..stashbranch > output &&
219 test_cmp output expect2 &&
220 test 0 = $(git stash list | wc -l)
221'
222
223test_expect_success 'apply -q is quiet' '
224 echo foo > file &&
225 git stash &&
226 git stash apply -q > output.out 2>&1 &&
227 test_must_be_empty output.out
228'
229
230test_expect_success 'save -q is quiet' '
231 git stash save --quiet > output.out 2>&1 &&
232 test_must_be_empty output.out
233'
234
235test_expect_success 'pop -q is quiet' '
236 git stash pop -q > output.out 2>&1 &&
237 test_must_be_empty output.out
238'
239
240test_expect_success 'pop -q --index works and is quiet' '
241 echo foo > file &&
242 git add file &&
243 git stash save --quiet &&
244 git stash pop -q --index > output.out 2>&1 &&
245 test foo = "$(git show :file)" &&
246 test_must_be_empty output.out
247'
248
249test_expect_success 'drop -q is quiet' '
250 git stash &&
251 git stash drop -q > output.out 2>&1 &&
252 test_must_be_empty output.out
253'
254
255test_expect_success 'stash -k' '
256 echo bar3 > file &&
257 echo bar4 > file2 &&
258 git add file2 &&
259 git stash -k &&
260 test bar,bar4 = $(cat file),$(cat file2)
261'
262
263test_expect_success 'stash --no-keep-index' '
264 echo bar33 > file &&
265 echo bar44 > file2 &&
266 git add file2 &&
267 git stash --no-keep-index &&
268 test bar,bar2 = $(cat file),$(cat file2)
269'
270
271test_expect_success 'stash --invalid-option' '
272 echo bar5 > file &&
273 echo bar6 > file2 &&
274 git add file2 &&
275 test_must_fail git stash --invalid-option &&
276 test_must_fail git stash save --invalid-option &&
277 test bar5,bar6 = $(cat file),$(cat file2) &&
278 git stash -- -message-starting-with-dash &&
279 test bar,bar2 = $(cat file),$(cat file2)
280'
281
282test_expect_success 'stash an added file' '
283 git reset --hard &&
284 echo new >file3 &&
285 git add file3 &&
286 git stash save "added file" &&
287 ! test -r file3 &&
288 git stash apply &&
289 test new = "$(cat file3)"
290'
291
292test_expect_success 'stash rm then recreate' '
293 git reset --hard &&
294 git rm file &&
295 echo bar7 >file &&
296 git stash save "rm then recreate" &&
297 test bar = "$(cat file)" &&
298 git stash apply &&
299 test bar7 = "$(cat file)"
300'
301
302test_expect_success 'stash rm and ignore' '
303 git reset --hard &&
304 git rm file &&
305 echo file >.gitignore &&
306 git stash save "rm and ignore" &&
307 test bar = "$(cat file)" &&
308 test file = "$(cat .gitignore)" &&
309 git stash apply &&
310 ! test -r file &&
311 test file = "$(cat .gitignore)"
312'
313
314test_expect_success 'stash rm and ignore (stage .gitignore)' '
315 git reset --hard &&
316 git rm file &&
317 echo file >.gitignore &&
318 git add .gitignore &&
319 git stash save "rm and ignore (stage .gitignore)" &&
320 test bar = "$(cat file)" &&
321 ! test -r .gitignore &&
322 git stash apply &&
323 ! test -r file &&
324 test file = "$(cat .gitignore)"
325'
326
327test_expect_success SYMLINKS 'stash file to symlink' '
328 git reset --hard &&
329 rm file &&
330 ln -s file2 file &&
331 git stash save "file to symlink" &&
332 test -f file &&
333 test bar = "$(cat file)" &&
334 git stash apply &&
335 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
336'
337
338test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
339 git reset --hard &&
340 git rm file &&
341 ln -s file2 file &&
342 git stash save "file to symlink (stage rm)" &&
343 test -f file &&
344 test bar = "$(cat file)" &&
345 git stash apply &&
346 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
347'
348
349test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
350 git reset --hard &&
351 rm file &&
352 ln -s file2 file &&
353 git add file &&
354 git stash save "file to symlink (full stage)" &&
355 test -f file &&
356 test bar = "$(cat file)" &&
357 git stash apply &&
358 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
359'
360
361# This test creates a commit with a symlink used for the following tests
362
363test_expect_success 'stash symlink to file' '
364 git reset --hard &&
365 test_ln_s_add file filelink &&
366 git commit -m "Add symlink" &&
367 rm filelink &&
368 cp file filelink &&
369 git stash save "symlink to file"
370'
371
372test_expect_success SYMLINKS 'this must have re-created the symlink' '
373 test -h filelink &&
374 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
375'
376
377test_expect_success 'unstash must re-create the file' '
378 git stash apply &&
379 ! test -h filelink &&
380 test bar = "$(cat file)"
381'
382
383test_expect_success 'stash symlink to file (stage rm)' '
384 git reset --hard &&
385 git rm filelink &&
386 cp file filelink &&
387 git stash save "symlink to file (stage rm)"
388'
389
390test_expect_success SYMLINKS 'this must have re-created the symlink' '
391 test -h filelink &&
392 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
393'
394
395test_expect_success 'unstash must re-create the file' '
396 git stash apply &&
397 ! test -h filelink &&
398 test bar = "$(cat file)"
399'
400
401test_expect_success 'stash symlink to file (full stage)' '
402 git reset --hard &&
403 rm filelink &&
404 cp file filelink &&
405 git add filelink &&
406 git stash save "symlink to file (full stage)"
407'
408
409test_expect_success SYMLINKS 'this must have re-created the symlink' '
410 test -h filelink &&
411 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
412'
413
414test_expect_success 'unstash must re-create the file' '
415 git stash apply &&
416 ! test -h filelink &&
417 test bar = "$(cat file)"
418'
419
420test_expect_failure 'stash directory to file' '
421 git reset --hard &&
422 mkdir dir &&
423 echo foo >dir/file &&
424 git add dir/file &&
425 git commit -m "Add file in dir" &&
426 rm -fr dir &&
427 echo bar >dir &&
428 git stash save "directory to file" &&
429 test -d dir &&
430 test foo = "$(cat dir/file)" &&
431 test_must_fail git stash apply &&
432 test bar = "$(cat dir)" &&
433 git reset --soft HEAD^
434'
435
436test_expect_failure 'stash file to directory' '
437 git reset --hard &&
438 rm file &&
439 mkdir file &&
440 echo foo >file/file &&
441 git stash save "file to directory" &&
442 test -f file &&
443 test bar = "$(cat file)" &&
444 git stash apply &&
445 test -f file/file &&
446 test foo = "$(cat file/file)"
447'
448
449test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
450 git stash clear &&
451 test_when_finished "git reset --hard HEAD" &&
452 git reset --hard &&
453 echo foo >> file &&
454 STASH_ID=$(git stash create) &&
455 git reset --hard &&
456 git stash branch stash-branch ${STASH_ID} &&
457 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
458 test $(git ls-files --modified | wc -l) -eq 1
459'
460
461test_expect_success 'stash branch - stashes on stack, stash-like argument' '
462 git stash clear &&
463 test_when_finished "git reset --hard HEAD" &&
464 git reset --hard &&
465 echo foo >> file &&
466 git stash &&
467 test_when_finished "git stash drop" &&
468 echo bar >> file &&
469 STASH_ID=$(git stash create) &&
470 git reset --hard &&
471 git stash branch stash-branch ${STASH_ID} &&
472 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
473 test $(git ls-files --modified | wc -l) -eq 1
474'
475
476test_expect_success 'stash show format defaults to --stat' '
477 git stash clear &&
478 test_when_finished "git reset --hard HEAD" &&
479 git reset --hard &&
480 echo foo >> file &&
481 git stash &&
482 test_when_finished "git stash drop" &&
483 echo bar >> file &&
484 STASH_ID=$(git stash create) &&
485 git reset --hard &&
486 cat >expected <<-EOF &&
487 file | 1 +
488 1 file changed, 1 insertion(+)
489 EOF
490 git stash show ${STASH_ID} >actual &&
491 test_i18ncmp expected actual
492'
493
494test_expect_success 'stash show - stashes on stack, stash-like argument' '
495 git stash clear &&
496 test_when_finished "git reset --hard HEAD" &&
497 git reset --hard &&
498 echo foo >> file &&
499 git stash &&
500 test_when_finished "git stash drop" &&
501 echo bar >> file &&
502 STASH_ID=$(git stash create) &&
503 git reset --hard &&
504 echo "1 0 file" >expected &&
505 git stash show --numstat ${STASH_ID} >actual &&
506 test_cmp expected actual
507'
508
509test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
510 git stash clear &&
511 test_when_finished "git reset --hard HEAD" &&
512 git reset --hard &&
513 echo foo >> file &&
514 git stash &&
515 test_when_finished "git stash drop" &&
516 echo bar >> file &&
517 STASH_ID=$(git stash create) &&
518 git reset --hard &&
519 cat >expected <<-EOF &&
520 diff --git a/file b/file
521 index 7601807..935fbd3 100644
522 --- a/file
523 +++ b/file
524 @@ -1 +1,2 @@
525 baz
526 +bar
527 EOF
528 git stash show -p ${STASH_ID} >actual &&
529 test_cmp expected actual
530'
531
532test_expect_success 'stash show - no stashes on stack, stash-like argument' '
533 git stash clear &&
534 test_when_finished "git reset --hard HEAD" &&
535 git reset --hard &&
536 echo foo >> file &&
537 STASH_ID=$(git stash create) &&
538 git reset --hard &&
539 echo "1 0 file" >expected &&
540 git stash show --numstat ${STASH_ID} >actual &&
541 test_cmp expected actual
542'
543
544test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
545 git stash clear &&
546 test_when_finished "git reset --hard HEAD" &&
547 git reset --hard &&
548 echo foo >> file &&
549 STASH_ID=$(git stash create) &&
550 git reset --hard &&
551 cat >expected <<-EOF &&
552 diff --git a/file b/file
553 index 7601807..71b52c4 100644
554 --- a/file
555 +++ b/file
556 @@ -1 +1,2 @@
557 baz
558 +foo
559 EOF
560 git stash show -p ${STASH_ID} >actual &&
561 test_cmp expected actual
562'
563
564test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
565 git stash clear &&
566 test_when_finished "git reset --hard HEAD && git stash clear" &&
567 git reset --hard &&
568 echo foo > file &&
569 git stash &&
570 echo bar > file &&
571 git stash &&
572 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
573 git stash pop &&
574 test bar = "$(cat file)" &&
575 git reset --hard HEAD
576'
577
578test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
579 git stash clear &&
580 test_when_finished "git reset --hard HEAD && git stash clear" &&
581 git reset --hard &&
582 echo foo > file &&
583 git stash &&
584 echo bar > file &&
585 git stash &&
586 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
587 git stash pop &&
588 test bar = "$(cat file)" &&
589 git reset --hard HEAD
590'
591
592test_expect_success 'ref with non-existent reflog' '
593 git stash clear &&
594 echo bar5 > file &&
595 echo bar6 > file2 &&
596 git add file2 &&
597 git stash &&
598 test_must_fail git rev-parse --quiet --verify does-not-exist &&
599 test_must_fail git stash drop does-not-exist &&
600 test_must_fail git stash drop does-not-exist@{0} &&
601 test_must_fail git stash pop does-not-exist &&
602 test_must_fail git stash pop does-not-exist@{0} &&
603 test_must_fail git stash apply does-not-exist &&
604 test_must_fail git stash apply does-not-exist@{0} &&
605 test_must_fail git stash show does-not-exist &&
606 test_must_fail git stash show does-not-exist@{0} &&
607 test_must_fail git stash branch tmp does-not-exist &&
608 test_must_fail git stash branch tmp does-not-exist@{0} &&
609 git stash drop
610'
611
612test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
613 git stash clear &&
614 test_must_fail git stash drop stash@{0} &&
615 echo bar5 > file &&
616 echo bar6 > file2 &&
617 git add file2 &&
618 git stash &&
619 test_must_fail git stash drop stash@{1} &&
620 test_must_fail git stash pop stash@{1} &&
621 test_must_fail git stash apply stash@{1} &&
622 test_must_fail git stash show stash@{1} &&
623 test_must_fail git stash branch tmp stash@{1} &&
624 git stash drop
625'
626
627test_expect_success 'invalid ref of the form "n", n >= N' '
628 git stash clear &&
629 test_must_fail git stash drop 0 &&
630 echo bar5 >file &&
631 echo bar6 >file2 &&
632 git add file2 &&
633 git stash &&
634 test_must_fail git stash drop 1 &&
635 test_must_fail git stash pop 1 &&
636 test_must_fail git stash apply 1 &&
637 test_must_fail git stash show 1 &&
638 test_must_fail git stash branch tmp 1 &&
639 git stash drop
640'
641
642test_expect_success 'stash branch should not drop the stash if the branch exists' '
643 git stash clear &&
644 echo foo >file &&
645 git add file &&
646 git commit -m initial &&
647 echo bar >file &&
648 git stash &&
649 test_must_fail git stash branch master stash@{0} &&
650 git rev-parse stash@{0} --
651'
652
653test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
654 git stash clear &&
655 echo 1 >subdir/subfile1 &&
656 echo 2 >subdir/subfile2 &&
657 git add subdir/subfile1 &&
658 git commit -m subdir &&
659 (
660 cd subdir &&
661 echo x >subfile1 &&
662 echo x >../file &&
663 git status >../expect &&
664 git stash &&
665 sane_unset GIT_MERGE_VERBOSITY &&
666 git stash apply
667 ) |
668 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
669 test_i18ncmp expect actual
670'
671
672cat > expect << EOF
673diff --git a/HEAD b/HEAD
674new file mode 100644
675index 0000000..fe0cbee
676--- /dev/null
677+++ b/HEAD
678@@ -0,0 +1 @@
679+file-not-a-ref
680EOF
681
682test_expect_success 'stash where working directory contains "HEAD" file' '
683 git stash clear &&
684 git reset --hard &&
685 echo file-not-a-ref > HEAD &&
686 git add HEAD &&
687 test_tick &&
688 git stash &&
689 git diff-files --quiet &&
690 git diff-index --cached --quiet HEAD &&
691 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
692 git diff stash^..stash > output &&
693 test_cmp output expect
694'
695
696test_expect_success 'store called with invalid commit' '
697 test_must_fail git stash store foo
698'
699
700test_expect_success 'store updates stash ref and reflog' '
701 git stash clear &&
702 git reset --hard &&
703 echo quux >bazzy &&
704 git add bazzy &&
705 STASH_ID=$(git stash create) &&
706 git reset --hard &&
707 ! grep quux bazzy &&
708 git stash store -m quuxery $STASH_ID &&
709 test $(cat .git/refs/stash) = $STASH_ID &&
710 git reflog --format=%H stash| grep $STASH_ID &&
711 git stash pop &&
712 grep quux bazzy
713'
714
715test_expect_success 'handle stash specification with spaces' '
716 git stash clear &&
717 echo pig >file &&
718 git stash &&
719 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
720 test_tick &&
721 echo cow >file &&
722 git stash &&
723 git stash apply "stash@{$stamp}" &&
724 grep pig file
725'
726
727test_expect_success 'setup stash with index and worktree changes' '
728 git stash clear &&
729 git reset --hard &&
730 echo index >file &&
731 git add file &&
732 echo working >file &&
733 git stash
734'
735
736test_expect_success 'stash list implies --first-parent -m' '
737 cat >expect <<-EOF &&
738 stash@{0}
739
740 diff --git a/file b/file
741 index 257cc56..d26b33d 100644
742 --- a/file
743 +++ b/file
744 @@ -1 +1 @@
745 -foo
746 +working
747 EOF
748 git stash list --format=%gd -p >actual &&
749 test_cmp expect actual
750'
751
752test_expect_success 'stash list --cc shows combined diff' '
753 cat >expect <<-\EOF &&
754 stash@{0}
755
756 diff --cc file
757 index 257cc56,9015a7a..d26b33d
758 --- a/file
759 +++ b/file
760 @@@ -1,1 -1,1 +1,1 @@@
761 - foo
762 -index
763 ++working
764 EOF
765 git stash list --format=%gd -p --cc >actual &&
766 test_cmp expect actual
767'
768
769test_expect_success 'stash is not confused by partial renames' '
770 mv file renamed &&
771 git add renamed &&
772 git stash &&
773 git stash apply &&
774 test_path_is_file renamed &&
775 test_path_is_missing file
776'
777
778test_done