Merge branch 'tg/stash-with-pathspec-fix' into maint
[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 . ./test-lib.sh
9
10 test_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
26 cat > expect << EOF
27 diff --git a/file b/file
28 index 0cfbf08..00750ed 100644
29 --- a/file
30 +++ b/file
31 @@ -1 +1 @@
32 -2
33 +3
34 EOF
35
36 test_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
42 test_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
48 test_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
55 test_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
63 test_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
75 test_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
87 test_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
96 test_expect_success 'stash drop complains of extra options' '
97         test_must_fail git stash drop --foo
98 '
99
100 test_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
114 test_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
134 test_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
154 test_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
163 cat > expect << EOF
164 diff --git a/file2 b/file2
165 new file mode 100644
166 index 0000000..1fe912c
167 --- /dev/null
168 +++ b/file2
169 @@ -0,0 +1 @@
170 +bar2
171 EOF
172
173 cat > expect1 << EOF
174 diff --git a/file b/file
175 index 257cc56..5716ca5 100644
176 --- a/file
177 +++ b/file
178 @@ -1 +1 @@
179 -foo
180 +bar
181 EOF
182
183 cat > expect2 << EOF
184 diff --git a/file b/file
185 index 7601807..5716ca5 100644
186 --- a/file
187 +++ b/file
188 @@ -1 +1 @@
189 -baz
190 +bar
191 diff --git a/file2 b/file2
192 new file mode 100644
193 index 0000000..1fe912c
194 --- /dev/null
195 +++ b/file2
196 @@ -0,0 +1 @@
197 +bar2
198 EOF
199
200 test_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
223 test_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
230 test_expect_success 'save -q is quiet' '
231         git stash save --quiet > output.out 2>&1 &&
232         test_must_be_empty output.out
233 '
234
235 test_expect_success 'pop -q is quiet' '
236         git stash pop -q > output.out 2>&1 &&
237         test_must_be_empty output.out
238 '
239
240 test_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
249 test_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
255 test_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
263 test_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
271 test_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 '
279
280 test_expect_success 'stash an added file' '
281         git reset --hard &&
282         echo new >file3 &&
283         git add file3 &&
284         git stash save "added file" &&
285         ! test -r file3 &&
286         git stash apply &&
287         test new = "$(cat file3)"
288 '
289
290 test_expect_success 'stash rm then recreate' '
291         git reset --hard &&
292         git rm file &&
293         echo bar7 >file &&
294         git stash save "rm then recreate" &&
295         test bar = "$(cat file)" &&
296         git stash apply &&
297         test bar7 = "$(cat file)"
298 '
299
300 test_expect_success 'stash rm and ignore' '
301         git reset --hard &&
302         git rm file &&
303         echo file >.gitignore &&
304         git stash save "rm and ignore" &&
305         test bar = "$(cat file)" &&
306         test file = "$(cat .gitignore)" &&
307         git stash apply &&
308         ! test -r file &&
309         test file = "$(cat .gitignore)"
310 '
311
312 test_expect_success 'stash rm and ignore (stage .gitignore)' '
313         git reset --hard &&
314         git rm file &&
315         echo file >.gitignore &&
316         git add .gitignore &&
317         git stash save "rm and ignore (stage .gitignore)" &&
318         test bar = "$(cat file)" &&
319         ! test -r .gitignore &&
320         git stash apply &&
321         ! test -r file &&
322         test file = "$(cat .gitignore)"
323 '
324
325 test_expect_success SYMLINKS 'stash file to symlink' '
326         git reset --hard &&
327         rm file &&
328         ln -s file2 file &&
329         git stash save "file to symlink" &&
330         test -f file &&
331         test bar = "$(cat file)" &&
332         git stash apply &&
333         case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
334 '
335
336 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
337         git reset --hard &&
338         git rm file &&
339         ln -s file2 file &&
340         git stash save "file to symlink (stage rm)" &&
341         test -f file &&
342         test bar = "$(cat file)" &&
343         git stash apply &&
344         case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
345 '
346
347 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
348         git reset --hard &&
349         rm file &&
350         ln -s file2 file &&
351         git add file &&
352         git stash save "file to symlink (full stage)" &&
353         test -f file &&
354         test bar = "$(cat file)" &&
355         git stash apply &&
356         case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
357 '
358
359 # This test creates a commit with a symlink used for the following tests
360
361 test_expect_success 'stash symlink to file' '
362         git reset --hard &&
363         test_ln_s_add file filelink &&
364         git commit -m "Add symlink" &&
365         rm filelink &&
366         cp file filelink &&
367         git stash save "symlink to file"
368 '
369
370 test_expect_success SYMLINKS 'this must have re-created the symlink' '
371         test -h filelink &&
372         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
373 '
374
375 test_expect_success 'unstash must re-create the file' '
376         git stash apply &&
377         ! test -h filelink &&
378         test bar = "$(cat file)"
379 '
380
381 test_expect_success 'stash symlink to file (stage rm)' '
382         git reset --hard &&
383         git rm filelink &&
384         cp file filelink &&
385         git stash save "symlink to file (stage rm)"
386 '
387
388 test_expect_success SYMLINKS 'this must have re-created the symlink' '
389         test -h filelink &&
390         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
391 '
392
393 test_expect_success 'unstash must re-create the file' '
394         git stash apply &&
395         ! test -h filelink &&
396         test bar = "$(cat file)"
397 '
398
399 test_expect_success 'stash symlink to file (full stage)' '
400         git reset --hard &&
401         rm filelink &&
402         cp file filelink &&
403         git add filelink &&
404         git stash save "symlink to file (full stage)"
405 '
406
407 test_expect_success SYMLINKS 'this must have re-created the symlink' '
408         test -h filelink &&
409         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
410 '
411
412 test_expect_success 'unstash must re-create the file' '
413         git stash apply &&
414         ! test -h filelink &&
415         test bar = "$(cat file)"
416 '
417
418 test_expect_failure 'stash directory to file' '
419         git reset --hard &&
420         mkdir dir &&
421         echo foo >dir/file &&
422         git add dir/file &&
423         git commit -m "Add file in dir" &&
424         rm -fr dir &&
425         echo bar >dir &&
426         git stash save "directory to file" &&
427         test -d dir &&
428         test foo = "$(cat dir/file)" &&
429         test_must_fail git stash apply &&
430         test bar = "$(cat dir)" &&
431         git reset --soft HEAD^
432 '
433
434 test_expect_failure 'stash file to directory' '
435         git reset --hard &&
436         rm file &&
437         mkdir file &&
438         echo foo >file/file &&
439         git stash save "file to directory" &&
440         test -f file &&
441         test bar = "$(cat file)" &&
442         git stash apply &&
443         test -f file/file &&
444         test foo = "$(cat file/file)"
445 '
446
447 test_expect_success 'stash create - no changes' '
448         git stash clear &&
449         test_when_finished "git reset --hard HEAD" &&
450         git reset --hard &&
451         git stash create >actual &&
452         test_must_be_empty actual
453 '
454
455 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
456         git stash clear &&
457         test_when_finished "git reset --hard HEAD" &&
458         git reset --hard &&
459         echo foo >> file &&
460         STASH_ID=$(git stash create) &&
461         git reset --hard &&
462         git stash branch stash-branch ${STASH_ID} &&
463         test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
464         test $(git ls-files --modified | wc -l) -eq 1
465 '
466
467 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
468         git stash clear &&
469         test_when_finished "git reset --hard HEAD" &&
470         git reset --hard &&
471         echo foo >> file &&
472         git stash &&
473         test_when_finished "git stash drop" &&
474         echo bar >> file &&
475         STASH_ID=$(git stash create) &&
476         git reset --hard &&
477         git stash branch stash-branch ${STASH_ID} &&
478         test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
479         test $(git ls-files --modified | wc -l) -eq 1
480 '
481
482 test_expect_success 'stash show format defaults to --stat' '
483         git stash clear &&
484         test_when_finished "git reset --hard HEAD" &&
485         git reset --hard &&
486         echo foo >> file &&
487         git stash &&
488         test_when_finished "git stash drop" &&
489         echo bar >> file &&
490         STASH_ID=$(git stash create) &&
491         git reset --hard &&
492         cat >expected <<-EOF &&
493          file | 1 +
494          1 file changed, 1 insertion(+)
495         EOF
496         git stash show ${STASH_ID} >actual &&
497         test_i18ncmp expected actual
498 '
499
500 test_expect_success 'stash show - stashes on stack, stash-like argument' '
501         git stash clear &&
502         test_when_finished "git reset --hard HEAD" &&
503         git reset --hard &&
504         echo foo >> file &&
505         git stash &&
506         test_when_finished "git stash drop" &&
507         echo bar >> file &&
508         STASH_ID=$(git stash create) &&
509         git reset --hard &&
510         echo "1 0       file" >expected &&
511         git stash show --numstat ${STASH_ID} >actual &&
512         test_cmp expected actual
513 '
514
515 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
516         git stash clear &&
517         test_when_finished "git reset --hard HEAD" &&
518         git reset --hard &&
519         echo foo >> file &&
520         git stash &&
521         test_when_finished "git stash drop" &&
522         echo bar >> file &&
523         STASH_ID=$(git stash create) &&
524         git reset --hard &&
525         cat >expected <<-EOF &&
526         diff --git a/file b/file
527         index 7601807..935fbd3 100644
528         --- a/file
529         +++ b/file
530         @@ -1 +1,2 @@
531          baz
532         +bar
533         EOF
534         git stash show -p ${STASH_ID} >actual &&
535         test_cmp expected actual
536 '
537
538 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
539         git stash clear &&
540         test_when_finished "git reset --hard HEAD" &&
541         git reset --hard &&
542         echo foo >> file &&
543         STASH_ID=$(git stash create) &&
544         git reset --hard &&
545         echo "1 0       file" >expected &&
546         git stash show --numstat ${STASH_ID} >actual &&
547         test_cmp expected actual
548 '
549
550 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
551         git stash clear &&
552         test_when_finished "git reset --hard HEAD" &&
553         git reset --hard &&
554         echo foo >> file &&
555         STASH_ID=$(git stash create) &&
556         git reset --hard &&
557         cat >expected <<-EOF &&
558         diff --git a/file b/file
559         index 7601807..71b52c4 100644
560         --- a/file
561         +++ b/file
562         @@ -1 +1,2 @@
563          baz
564         +foo
565         EOF
566         git stash show -p ${STASH_ID} >actual &&
567         test_cmp expected actual
568 '
569
570 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
571         git stash clear &&
572         test_when_finished "git reset --hard HEAD && git stash clear" &&
573         git reset --hard &&
574         echo foo > file &&
575         git stash &&
576         echo bar > file &&
577         git stash &&
578         test_must_fail git stash drop $(git rev-parse stash@{0}) &&
579         git stash pop &&
580         test bar = "$(cat file)" &&
581         git reset --hard HEAD
582 '
583
584 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
585         git stash clear &&
586         test_when_finished "git reset --hard HEAD && git stash clear" &&
587         git reset --hard &&
588         echo foo > file &&
589         git stash &&
590         echo bar > file &&
591         git stash &&
592         test_must_fail git stash pop $(git rev-parse stash@{0}) &&
593         git stash pop &&
594         test bar = "$(cat file)" &&
595         git reset --hard HEAD
596 '
597
598 test_expect_success 'ref with non-existent reflog' '
599         git stash clear &&
600         echo bar5 > file &&
601         echo bar6 > file2 &&
602         git add file2 &&
603         git stash &&
604         test_must_fail git rev-parse --quiet --verify does-not-exist &&
605         test_must_fail git stash drop does-not-exist &&
606         test_must_fail git stash drop does-not-exist@{0} &&
607         test_must_fail git stash pop does-not-exist &&
608         test_must_fail git stash pop does-not-exist@{0} &&
609         test_must_fail git stash apply does-not-exist &&
610         test_must_fail git stash apply does-not-exist@{0} &&
611         test_must_fail git stash show does-not-exist &&
612         test_must_fail git stash show does-not-exist@{0} &&
613         test_must_fail git stash branch tmp does-not-exist &&
614         test_must_fail git stash branch tmp does-not-exist@{0} &&
615         git stash drop
616 '
617
618 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
619         git stash clear &&
620         test_must_fail git stash drop stash@{0} &&
621         echo bar5 > file &&
622         echo bar6 > file2 &&
623         git add file2 &&
624         git stash &&
625         test_must_fail git stash drop stash@{1} &&
626         test_must_fail git stash pop stash@{1} &&
627         test_must_fail git stash apply stash@{1} &&
628         test_must_fail git stash show stash@{1} &&
629         test_must_fail git stash branch tmp stash@{1} &&
630         git stash drop
631 '
632
633 test_expect_success 'invalid ref of the form "n", n >= N' '
634         git stash clear &&
635         test_must_fail git stash drop 0 &&
636         echo bar5 >file &&
637         echo bar6 >file2 &&
638         git add file2 &&
639         git stash &&
640         test_must_fail git stash drop 1 &&
641         test_must_fail git stash pop 1 &&
642         test_must_fail git stash apply 1 &&
643         test_must_fail git stash show 1 &&
644         test_must_fail git stash branch tmp 1 &&
645         git stash drop
646 '
647
648 test_expect_success 'stash branch should not drop the stash if the branch exists' '
649         git stash clear &&
650         echo foo >file &&
651         git add file &&
652         git commit -m initial &&
653         echo bar >file &&
654         git stash &&
655         test_must_fail git stash branch master stash@{0} &&
656         git rev-parse stash@{0} --
657 '
658
659 test_expect_success 'stash branch should not drop the stash if the apply fails' '
660         git stash clear &&
661         git reset HEAD~1 --hard &&
662         echo foo >file &&
663         git add file &&
664         git commit -m initial &&
665         echo bar >file &&
666         git stash &&
667         echo baz >file &&
668         test_when_finished "git checkout master" &&
669         test_must_fail git stash branch new_branch stash@{0} &&
670         git rev-parse stash@{0} --
671 '
672
673 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
674         git stash clear &&
675         echo 1 >subdir/subfile1 &&
676         echo 2 >subdir/subfile2 &&
677         git add subdir/subfile1 &&
678         git commit -m subdir &&
679         (
680                 cd subdir &&
681                 echo x >subfile1 &&
682                 echo x >../file &&
683                 git status >../expect &&
684                 git stash &&
685                 sane_unset GIT_MERGE_VERBOSITY &&
686                 git stash apply
687         ) |
688         sed -e 1d >actual && # drop "Saved..."
689         test_i18ncmp expect actual
690 '
691
692 cat > expect << EOF
693 diff --git a/HEAD b/HEAD
694 new file mode 100644
695 index 0000000..fe0cbee
696 --- /dev/null
697 +++ b/HEAD
698 @@ -0,0 +1 @@
699 +file-not-a-ref
700 EOF
701
702 test_expect_success 'stash where working directory contains "HEAD" file' '
703         git stash clear &&
704         git reset --hard &&
705         echo file-not-a-ref > HEAD &&
706         git add HEAD &&
707         test_tick &&
708         git stash &&
709         git diff-files --quiet &&
710         git diff-index --cached --quiet HEAD &&
711         test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
712         git diff stash^..stash > output &&
713         test_cmp output expect
714 '
715
716 test_expect_success 'store called with invalid commit' '
717         test_must_fail git stash store foo
718 '
719
720 test_expect_success 'store updates stash ref and reflog' '
721         git stash clear &&
722         git reset --hard &&
723         echo quux >bazzy &&
724         git add bazzy &&
725         STASH_ID=$(git stash create) &&
726         git reset --hard &&
727         ! grep quux bazzy &&
728         git stash store -m quuxery $STASH_ID &&
729         test $(cat .git/refs/stash) = $STASH_ID &&
730         git reflog --format=%H stash| grep $STASH_ID &&
731         git stash pop &&
732         grep quux bazzy
733 '
734
735 test_expect_success 'handle stash specification with spaces' '
736         git stash clear &&
737         echo pig >file &&
738         git stash &&
739         stamp=$(git log -g --format="%cd" -1 refs/stash) &&
740         test_tick &&
741         echo cow >file &&
742         git stash &&
743         git stash apply "stash@{$stamp}" &&
744         grep pig file
745 '
746
747 test_expect_success 'setup stash with index and worktree changes' '
748         git stash clear &&
749         git reset --hard &&
750         echo index >file &&
751         git add file &&
752         echo working >file &&
753         git stash
754 '
755
756 test_expect_success 'stash list implies --first-parent -m' '
757         cat >expect <<-EOF &&
758         stash@{0}
759
760         diff --git a/file b/file
761         index 257cc56..d26b33d 100644
762         --- a/file
763         +++ b/file
764         @@ -1 +1 @@
765         -foo
766         +working
767         EOF
768         git stash list --format=%gd -p >actual &&
769         test_cmp expect actual
770 '
771
772 test_expect_success 'stash list --cc shows combined diff' '
773         cat >expect <<-\EOF &&
774         stash@{0}
775
776         diff --cc file
777         index 257cc56,9015a7a..d26b33d
778         --- a/file
779         +++ b/file
780         @@@ -1,1 -1,1 +1,1 @@@
781         - foo
782          -index
783         ++working
784         EOF
785         git stash list --format=%gd -p --cc >actual &&
786         test_cmp expect actual
787 '
788
789 test_expect_success 'stash is not confused by partial renames' '
790         mv file renamed &&
791         git add renamed &&
792         git stash &&
793         git stash apply &&
794         test_path_is_file renamed &&
795         test_path_is_missing file
796 '
797
798 test_expect_success 'push -m shows right message' '
799         >foo &&
800         git add foo &&
801         git stash push -m "test message" &&
802         echo "stash@{0}: On master: test message" >expect &&
803         git stash list -1 >actual &&
804         test_cmp expect actual
805 '
806
807 test_expect_success 'push -m also works without space' '
808         >foo &&
809         git add foo &&
810         git stash push -m"unspaced test message" &&
811         echo "stash@{0}: On master: unspaced test message" >expect &&
812         git stash list -1 >actual &&
813         test_cmp expect actual
814 '
815
816 test_expect_success 'store -m foo shows right message' '
817         git stash clear &&
818         git reset --hard &&
819         echo quux >bazzy &&
820         git add bazzy &&
821         STASH_ID=$(git stash create) &&
822         git stash store -m "store m" $STASH_ID &&
823         echo "stash@{0}: store m" >expect &&
824         git stash list -1 >actual &&
825         test_cmp expect actual
826 '
827
828 test_expect_success 'store -mfoo shows right message' '
829         git stash clear &&
830         git reset --hard &&
831         echo quux >bazzy &&
832         git add bazzy &&
833         STASH_ID=$(git stash create) &&
834         git stash store -m"store mfoo" $STASH_ID &&
835         echo "stash@{0}: store mfoo" >expect &&
836         git stash list -1 >actual &&
837         test_cmp expect actual
838 '
839
840 test_expect_success 'store --message=foo shows right message' '
841         git stash clear &&
842         git reset --hard &&
843         echo quux >bazzy &&
844         git add bazzy &&
845         STASH_ID=$(git stash create) &&
846         git stash store --message="store message=foo" $STASH_ID &&
847         echo "stash@{0}: store message=foo" >expect &&
848         git stash list -1 >actual &&
849         test_cmp expect actual
850 '
851
852 test_expect_success 'store --message foo shows right message' '
853         git stash clear &&
854         git reset --hard &&
855         echo quux >bazzy &&
856         git add bazzy &&
857         STASH_ID=$(git stash create) &&
858         git stash store --message "store message foo" $STASH_ID &&
859         echo "stash@{0}: store message foo" >expect &&
860         git stash list -1 >actual &&
861         test_cmp expect actual
862 '
863
864 test_expect_success 'push -mfoo uses right message' '
865         >foo &&
866         git add foo &&
867         git stash push -m"test mfoo" &&
868         echo "stash@{0}: On master: test mfoo" >expect &&
869         git stash list -1 >actual &&
870         test_cmp expect actual
871 '
872
873 test_expect_success 'push --message foo is synonym for -mfoo' '
874         >foo &&
875         git add foo &&
876         git stash push --message "test message foo" &&
877         echo "stash@{0}: On master: test message foo" >expect &&
878         git stash list -1 >actual &&
879         test_cmp expect actual
880 '
881
882 test_expect_success 'push --message=foo is synonym for -mfoo' '
883         >foo &&
884         git add foo &&
885         git stash push --message="test message=foo" &&
886         echo "stash@{0}: On master: test message=foo" >expect &&
887         git stash list -1 >actual &&
888         test_cmp expect actual
889 '
890
891 test_expect_success 'push -m shows right message' '
892         >foo &&
893         git add foo &&
894         git stash push -m "test m foo" &&
895         echo "stash@{0}: On master: test m foo" >expect &&
896         git stash list -1 >actual &&
897         test_cmp expect actual
898 '
899
900 test_expect_success 'create stores correct message' '
901         >foo &&
902         git add foo &&
903         STASH_ID=$(git stash create "create test message") &&
904         echo "On master: create test message" >expect &&
905         git show --pretty=%s -s ${STASH_ID} >actual &&
906         test_cmp expect actual
907 '
908
909 test_expect_success 'create with multiple arguments for the message' '
910         >foo &&
911         git add foo &&
912         STASH_ID=$(git stash create test untracked) &&
913         echo "On master: test untracked" >expect &&
914         git show --pretty=%s -s ${STASH_ID} >actual &&
915         test_cmp expect actual
916 '
917
918 test_expect_success 'create in a detached state' '
919         test_when_finished "git checkout master" &&
920         git checkout HEAD~1 &&
921         >foo &&
922         git add foo &&
923         STASH_ID=$(git stash create) &&
924         HEAD_ID=$(git rev-parse --short HEAD) &&
925         echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
926         git show --pretty=%s -s ${STASH_ID} >actual &&
927         test_cmp expect actual
928 '
929
930 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
931         >foo &&
932         >bar &&
933         git add foo bar &&
934         git stash push -- foo &&
935         test_path_is_file bar &&
936         test_path_is_missing foo &&
937         git stash pop &&
938         test_path_is_file foo &&
939         test_path_is_file bar
940 '
941
942 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
943         mkdir sub &&
944         >foo &&
945         >bar &&
946         git add foo bar &&
947         (
948                 cd sub &&
949                 git stash push -- ../foo
950         ) &&
951         test_path_is_file bar &&
952         test_path_is_missing foo &&
953         git stash pop &&
954         test_path_is_file foo &&
955         test_path_is_file bar
956 '
957
958 test_expect_success 'stash with multiple pathspec arguments' '
959         >foo &&
960         >bar &&
961         >extra &&
962         git add foo bar extra &&
963         git stash push -- foo bar &&
964         test_path_is_missing bar &&
965         test_path_is_missing foo &&
966         test_path_is_file extra &&
967         git stash pop &&
968         test_path_is_file foo &&
969         test_path_is_file bar &&
970         test_path_is_file extra
971 '
972
973 test_expect_success 'stash with file including $IFS character' '
974         >"foo bar" &&
975         >foo &&
976         >bar &&
977         git add foo* &&
978         git stash push -- "foo b*" &&
979         test_path_is_missing "foo bar" &&
980         test_path_is_file foo &&
981         test_path_is_file bar &&
982         git stash pop &&
983         test_path_is_file "foo bar" &&
984         test_path_is_file foo &&
985         test_path_is_file bar
986 '
987
988 test_expect_success 'stash with pathspec matching multiple paths' '
989        echo original >file &&
990        echo original >other-file &&
991        git commit -m "two" file other-file &&
992        echo modified >file &&
993        echo modified >other-file &&
994        git stash push -- "*file" &&
995        echo original >expect &&
996        test_cmp expect file &&
997        test_cmp expect other-file &&
998        git stash pop &&
999        echo modified >expect &&
1000        test_cmp expect file &&
1001        test_cmp expect other-file
1002 '
1003
1004 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1005         >foo &&
1006         git add foo &&
1007         git commit -m "tmp" &&
1008         git stash push -p foo >actual &&
1009         echo "No local changes to save" >expect &&
1010         git reset --hard HEAD~ &&
1011         test_i18ncmp expect actual
1012 '
1013
1014 test_expect_success 'stash push with pathspec shows no changes when there are none' '
1015         >foo &&
1016         git add foo &&
1017         git commit -m "tmp" &&
1018         git stash push foo >actual &&
1019         echo "No local changes to save" >expect &&
1020         git reset --hard HEAD~ &&
1021         test_i18ncmp expect actual
1022 '
1023
1024 test_expect_success 'stash push with pathspec not in the repository errors out' '
1025         >untracked &&
1026         test_must_fail git stash push untracked &&
1027         test_path_is_file untracked
1028 '
1029
1030 test_expect_success 'untracked files are left in place when -u is not given' '
1031         >file &&
1032         git add file &&
1033         >untracked &&
1034         git stash push file &&
1035         test_path_is_file untracked
1036 '
1037
1038 test_expect_success 'stash without verb with pathspec' '
1039         >"foo bar" &&
1040         >foo &&
1041         >bar &&
1042         git add foo* &&
1043         git stash -- "foo b*" &&
1044         test_path_is_missing "foo bar" &&
1045         test_path_is_file foo &&
1046         test_path_is_file bar &&
1047         git stash pop &&
1048         test_path_is_file "foo bar" &&
1049         test_path_is_file foo &&
1050         test_path_is_file bar
1051 '
1052
1053 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1054         git reset &&
1055         >foo &&
1056         >bar &&
1057         git add foo bar &&
1058         git commit -m "test" &&
1059         echo "foo" >foo &&
1060         echo "bar" >bar &&
1061         git stash -k -- foo &&
1062         test "",bar = $(cat foo),$(cat bar) &&
1063         git stash pop &&
1064         test foo,bar = $(cat foo),$(cat bar)
1065 '
1066
1067 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1068         git reset &&
1069         >subdir/untracked &&
1070         >subdir/tracked1 &&
1071         >subdir/tracked2 &&
1072         git add subdir/tracked* &&
1073         git stash -- subdir/ &&
1074         test_path_is_missing subdir/tracked1 &&
1075         test_path_is_missing subdir/tracked2 &&
1076         test_path_is_file subdir/untracked &&
1077         git stash pop &&
1078         test_path_is_file subdir/tracked1 &&
1079         test_path_is_file subdir/tracked2 &&
1080         test_path_is_file subdir/untracked
1081 '
1082
1083 test_expect_success 'stash -- <subdir> works with binary files' '
1084         git reset &&
1085         >subdir/untracked &&
1086         >subdir/tracked &&
1087         cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1088         git add subdir/tracked* &&
1089         git stash -- subdir/ &&
1090         test_path_is_missing subdir/tracked &&
1091         test_path_is_missing subdir/tracked-binary &&
1092         test_path_is_file subdir/untracked &&
1093         git stash pop &&
1094         test_path_is_file subdir/tracked &&
1095         test_path_is_file subdir/tracked-binary &&
1096         test_path_is_file subdir/untracked
1097 '
1098
1099 test_done