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