Imported Upstream version 2.25.4
[platform/upstream/git.git] / t / t3903-stash.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes E Schindelin
4 #
5
6 test_description='Test git stash'
7
8 . ./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 'stash --invalid-option' '
289         echo bar5 >file &&
290         echo bar6 >file2 &&
291         git add file2 &&
292         test_must_fail git stash --invalid-option &&
293         test_must_fail git stash save --invalid-option &&
294         test bar5,bar6 = $(cat file),$(cat file2)
295 '
296
297 test_expect_success 'stash an added file' '
298         git reset --hard &&
299         echo new >file3 &&
300         git add file3 &&
301         git stash save "added file" &&
302         ! test -r file3 &&
303         git stash apply &&
304         test new = "$(cat file3)"
305 '
306
307 test_expect_success 'stash --intent-to-add file' '
308         git reset --hard &&
309         echo new >file4 &&
310         git add --intent-to-add file4 &&
311         test_when_finished "git rm -f file4" &&
312         test_must_fail git stash
313 '
314
315 test_expect_success 'stash rm then recreate' '
316         git reset --hard &&
317         git rm file &&
318         echo bar7 >file &&
319         git stash save "rm then recreate" &&
320         test bar = "$(cat file)" &&
321         git stash apply &&
322         test bar7 = "$(cat file)"
323 '
324
325 test_expect_success 'stash rm and ignore' '
326         git reset --hard &&
327         git rm file &&
328         echo file >.gitignore &&
329         git stash save "rm and ignore" &&
330         test bar = "$(cat file)" &&
331         test file = "$(cat .gitignore)" &&
332         git stash apply &&
333         ! test -r file &&
334         test file = "$(cat .gitignore)"
335 '
336
337 test_expect_success 'stash rm and ignore (stage .gitignore)' '
338         git reset --hard &&
339         git rm file &&
340         echo file >.gitignore &&
341         git add .gitignore &&
342         git stash save "rm and ignore (stage .gitignore)" &&
343         test bar = "$(cat file)" &&
344         ! test -r .gitignore &&
345         git stash apply &&
346         ! test -r file &&
347         test file = "$(cat .gitignore)"
348 '
349
350 test_expect_success SYMLINKS 'stash file to symlink' '
351         git reset --hard &&
352         rm file &&
353         ln -s file2 file &&
354         git stash save "file to symlink" &&
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 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
362         git reset --hard &&
363         git rm file &&
364         ln -s file2 file &&
365         git stash save "file to symlink (stage rm)" &&
366         test -f file &&
367         test bar = "$(cat file)" &&
368         git stash apply &&
369         case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
370 '
371
372 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
373         git reset --hard &&
374         rm file &&
375         ln -s file2 file &&
376         git add file &&
377         git stash save "file to symlink (full stage)" &&
378         test -f file &&
379         test bar = "$(cat file)" &&
380         git stash apply &&
381         case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
382 '
383
384 # This test creates a commit with a symlink used for the following tests
385
386 test_expect_success 'stash symlink to file' '
387         git reset --hard &&
388         test_ln_s_add file filelink &&
389         git commit -m "Add symlink" &&
390         rm filelink &&
391         cp file filelink &&
392         git stash save "symlink to file"
393 '
394
395 test_expect_success SYMLINKS 'this must have re-created the symlink' '
396         test -h filelink &&
397         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
398 '
399
400 test_expect_success 'unstash must re-create the file' '
401         git stash apply &&
402         ! test -h filelink &&
403         test bar = "$(cat file)"
404 '
405
406 test_expect_success 'stash symlink to file (stage rm)' '
407         git reset --hard &&
408         git rm filelink &&
409         cp file filelink &&
410         git stash save "symlink to file (stage rm)"
411 '
412
413 test_expect_success SYMLINKS 'this must have re-created the symlink' '
414         test -h filelink &&
415         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
416 '
417
418 test_expect_success 'unstash must re-create the file' '
419         git stash apply &&
420         ! test -h filelink &&
421         test bar = "$(cat file)"
422 '
423
424 test_expect_success 'stash symlink to file (full stage)' '
425         git reset --hard &&
426         rm filelink &&
427         cp file filelink &&
428         git add filelink &&
429         git stash save "symlink to file (full stage)"
430 '
431
432 test_expect_success SYMLINKS 'this must have re-created the symlink' '
433         test -h filelink &&
434         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
435 '
436
437 test_expect_success 'unstash must re-create the file' '
438         git stash apply &&
439         ! test -h filelink &&
440         test bar = "$(cat file)"
441 '
442
443 test_expect_failure 'stash directory to file' '
444         git reset --hard &&
445         mkdir dir &&
446         echo foo >dir/file &&
447         git add dir/file &&
448         git commit -m "Add file in dir" &&
449         rm -fr dir &&
450         echo bar >dir &&
451         git stash save "directory to file" &&
452         test -d dir &&
453         test foo = "$(cat dir/file)" &&
454         test_must_fail git stash apply &&
455         test bar = "$(cat dir)" &&
456         git reset --soft HEAD^
457 '
458
459 test_expect_failure 'stash file to directory' '
460         git reset --hard &&
461         rm file &&
462         mkdir file &&
463         echo foo >file/file &&
464         git stash save "file to directory" &&
465         test -f file &&
466         test bar = "$(cat file)" &&
467         git stash apply &&
468         test -f file/file &&
469         test foo = "$(cat file/file)"
470 '
471
472 test_expect_success 'giving too many ref arguments does not modify files' '
473         git stash clear &&
474         test_when_finished "git reset --hard HEAD" &&
475         echo foo >file2 &&
476         git stash &&
477         echo bar >file2 &&
478         git stash &&
479         test-tool chmtime =123456789 file2 &&
480         for type in apply pop "branch stash-branch"
481         do
482                 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
483                 test_i18ngrep "Too many revisions" err &&
484                 test 123456789 = $(test-tool chmtime -g file2) || return 1
485         done
486 '
487
488 test_expect_success 'drop: too many arguments errors out (does nothing)' '
489         git stash list >expect &&
490         test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
491         test_i18ngrep "Too many revisions" err &&
492         git stash list >actual &&
493         test_cmp expect actual
494 '
495
496 test_expect_success 'show: too many arguments errors out (does nothing)' '
497         test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
498         test_i18ngrep "Too many revisions" err &&
499         test_must_be_empty out
500 '
501
502 test_expect_success 'stash create - no changes' '
503         git stash clear &&
504         test_when_finished "git reset --hard HEAD" &&
505         git reset --hard &&
506         git stash create >actual &&
507         test_must_be_empty actual
508 '
509
510 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
511         git stash clear &&
512         test_when_finished "git reset --hard HEAD" &&
513         git reset --hard &&
514         echo foo >>file &&
515         STASH_ID=$(git stash create) &&
516         git reset --hard &&
517         git stash branch stash-branch ${STASH_ID} &&
518         test_when_finished "git reset --hard HEAD && git checkout master &&
519         git branch -D stash-branch" &&
520         test $(git ls-files --modified | wc -l) -eq 1
521 '
522
523 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
524         git stash clear &&
525         test_when_finished "git reset --hard HEAD" &&
526         git reset --hard &&
527         echo foo >>file &&
528         git stash &&
529         test_when_finished "git stash drop" &&
530         echo bar >>file &&
531         STASH_ID=$(git stash create) &&
532         git reset --hard &&
533         git stash branch stash-branch ${STASH_ID} &&
534         test_when_finished "git reset --hard HEAD && git checkout master &&
535         git branch -D stash-branch" &&
536         test $(git ls-files --modified | wc -l) -eq 1
537 '
538
539 test_expect_success 'stash branch complains with no arguments' '
540         test_must_fail git stash branch 2>err &&
541         test_i18ngrep "No branch name specified" err
542 '
543
544 test_expect_success 'stash show format defaults to --stat' '
545         git stash clear &&
546         test_when_finished "git reset --hard HEAD" &&
547         git reset --hard &&
548         echo foo >>file &&
549         git stash &&
550         test_when_finished "git stash drop" &&
551         echo bar >>file &&
552         STASH_ID=$(git stash create) &&
553         git reset --hard &&
554         cat >expected <<-EOF &&
555          file | 1 +
556          1 file changed, 1 insertion(+)
557         EOF
558         git stash show ${STASH_ID} >actual &&
559         test_i18ncmp expected actual
560 '
561
562 test_expect_success 'stash show - stashes on stack, stash-like argument' '
563         git stash clear &&
564         test_when_finished "git reset --hard HEAD" &&
565         git reset --hard &&
566         echo foo >>file &&
567         git stash &&
568         test_when_finished "git stash drop" &&
569         echo bar >>file &&
570         STASH_ID=$(git stash create) &&
571         git reset --hard &&
572         echo "1 0       file" >expected &&
573         git stash show --numstat ${STASH_ID} >actual &&
574         test_cmp expected actual
575 '
576
577 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
578         git stash clear &&
579         test_when_finished "git reset --hard HEAD" &&
580         git reset --hard &&
581         echo foo >>file &&
582         git stash &&
583         test_when_finished "git stash drop" &&
584         echo bar >>file &&
585         STASH_ID=$(git stash create) &&
586         git reset --hard &&
587         cat >expected <<-EOF &&
588         diff --git a/file b/file
589         index 7601807..935fbd3 100644
590         --- a/file
591         +++ b/file
592         @@ -1 +1,2 @@
593          baz
594         +bar
595         EOF
596         git stash show -p ${STASH_ID} >actual &&
597         diff_cmp expected actual
598 '
599
600 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
601         git stash clear &&
602         test_when_finished "git reset --hard HEAD" &&
603         git reset --hard &&
604         echo foo >>file &&
605         STASH_ID=$(git stash create) &&
606         git reset --hard &&
607         echo "1 0       file" >expected &&
608         git stash show --numstat ${STASH_ID} >actual &&
609         test_cmp expected actual
610 '
611
612 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
613         git stash clear &&
614         test_when_finished "git reset --hard HEAD" &&
615         git reset --hard &&
616         echo foo >>file &&
617         STASH_ID=$(git stash create) &&
618         git reset --hard &&
619         cat >expected <<-EOF &&
620         diff --git a/file b/file
621         index 7601807..71b52c4 100644
622         --- a/file
623         +++ b/file
624         @@ -1 +1,2 @@
625          baz
626         +foo
627         EOF
628         git stash show -p ${STASH_ID} >actual &&
629         diff_cmp expected actual
630 '
631
632 test_expect_success 'stash show --patience shows diff' '
633         git reset --hard &&
634         echo foo >>file &&
635         STASH_ID=$(git stash create) &&
636         git reset --hard &&
637         cat >expected <<-EOF &&
638         diff --git a/file b/file
639         index 7601807..71b52c4 100644
640         --- a/file
641         +++ b/file
642         @@ -1 +1,2 @@
643          baz
644         +foo
645         EOF
646         git stash show --patience ${STASH_ID} >actual &&
647         diff_cmp expected actual
648 '
649
650 test_expect_success 'drop: fail early if specified stash is not a stash ref' '
651         git stash clear &&
652         test_when_finished "git reset --hard HEAD && git stash clear" &&
653         git reset --hard &&
654         echo foo >file &&
655         git stash &&
656         echo bar >file &&
657         git stash &&
658         test_must_fail git stash drop $(git rev-parse stash@{0}) &&
659         git stash pop &&
660         test bar = "$(cat file)" &&
661         git reset --hard HEAD
662 '
663
664 test_expect_success 'pop: fail early if specified stash is not a stash ref' '
665         git stash clear &&
666         test_when_finished "git reset --hard HEAD && git stash clear" &&
667         git reset --hard &&
668         echo foo >file &&
669         git stash &&
670         echo bar >file &&
671         git stash &&
672         test_must_fail git stash pop $(git rev-parse stash@{0}) &&
673         git stash pop &&
674         test bar = "$(cat file)" &&
675         git reset --hard HEAD
676 '
677
678 test_expect_success 'ref with non-existent reflog' '
679         git stash clear &&
680         echo bar5 >file &&
681         echo bar6 >file2 &&
682         git add file2 &&
683         git stash &&
684         test_must_fail git rev-parse --quiet --verify does-not-exist &&
685         test_must_fail git stash drop does-not-exist &&
686         test_must_fail git stash drop does-not-exist@{0} &&
687         test_must_fail git stash pop does-not-exist &&
688         test_must_fail git stash pop does-not-exist@{0} &&
689         test_must_fail git stash apply does-not-exist &&
690         test_must_fail git stash apply does-not-exist@{0} &&
691         test_must_fail git stash show does-not-exist &&
692         test_must_fail git stash show does-not-exist@{0} &&
693         test_must_fail git stash branch tmp does-not-exist &&
694         test_must_fail git stash branch tmp does-not-exist@{0} &&
695         git stash drop
696 '
697
698 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
699         git stash clear &&
700         test_must_fail git stash drop stash@{0} &&
701         echo bar5 >file &&
702         echo bar6 >file2 &&
703         git add file2 &&
704         git stash &&
705         test_must_fail git stash drop stash@{1} &&
706         test_must_fail git stash pop stash@{1} &&
707         test_must_fail git stash apply stash@{1} &&
708         test_must_fail git stash show stash@{1} &&
709         test_must_fail git stash branch tmp stash@{1} &&
710         git stash drop
711 '
712
713 test_expect_success 'invalid ref of the form "n", n >= N' '
714         git stash clear &&
715         test_must_fail git stash drop 0 &&
716         echo bar5 >file &&
717         echo bar6 >file2 &&
718         git add file2 &&
719         git stash &&
720         test_must_fail git stash drop 1 &&
721         test_must_fail git stash pop 1 &&
722         test_must_fail git stash apply 1 &&
723         test_must_fail git stash show 1 &&
724         test_must_fail git stash branch tmp 1 &&
725         git stash drop
726 '
727
728 test_expect_success 'valid ref of the form "n", n < N' '
729         git stash clear &&
730         echo bar5 >file &&
731         echo bar6 >file2 &&
732         git add file2 &&
733         git stash &&
734         git stash show 0 &&
735         git stash branch tmp 0 &&
736         git checkout master &&
737         git stash &&
738         git stash apply 0 &&
739         git reset --hard &&
740         git stash pop 0 &&
741         git stash &&
742         git stash drop 0 &&
743         test_must_fail git stash drop
744 '
745
746 test_expect_success 'branch: do not drop the stash if the branch exists' '
747         git stash clear &&
748         echo foo >file &&
749         git add file &&
750         git commit -m initial &&
751         echo bar >file &&
752         git stash &&
753         test_must_fail git stash branch master stash@{0} &&
754         git rev-parse stash@{0} --
755 '
756
757 test_expect_success 'branch: should not drop the stash if the apply fails' '
758         git stash clear &&
759         git reset HEAD~1 --hard &&
760         echo foo >file &&
761         git add file &&
762         git commit -m initial &&
763         echo bar >file &&
764         git stash &&
765         echo baz >file &&
766         test_when_finished "git checkout master" &&
767         test_must_fail git stash branch new_branch stash@{0} &&
768         git rev-parse stash@{0} --
769 '
770
771 test_expect_success 'apply: show same status as git status (relative to ./)' '
772         git stash clear &&
773         echo 1 >subdir/subfile1 &&
774         echo 2 >subdir/subfile2 &&
775         git add subdir/subfile1 &&
776         git commit -m subdir &&
777         (
778                 cd subdir &&
779                 echo x >subfile1 &&
780                 echo x >../file &&
781                 git status >../expect &&
782                 git stash &&
783                 sane_unset GIT_MERGE_VERBOSITY &&
784                 git stash apply
785         ) |
786         sed -e 1d >actual && # drop "Saved..."
787         test_i18ncmp expect actual
788 '
789
790 cat >expect <<EOF
791 diff --git a/HEAD b/HEAD
792 new file mode 100644
793 index 0000000..fe0cbee
794 --- /dev/null
795 +++ b/HEAD
796 @@ -0,0 +1 @@
797 +file-not-a-ref
798 EOF
799
800 test_expect_success 'stash where working directory contains "HEAD" file' '
801         git stash clear &&
802         git reset --hard &&
803         echo file-not-a-ref >HEAD &&
804         git add HEAD &&
805         test_tick &&
806         git stash &&
807         git diff-files --quiet &&
808         git diff-index --cached --quiet HEAD &&
809         test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
810         git diff stash^..stash >output &&
811         diff_cmp expect output
812 '
813
814 test_expect_success 'store called with invalid commit' '
815         test_must_fail git stash store foo
816 '
817
818 test_expect_success 'store updates stash ref and reflog' '
819         git stash clear &&
820         git reset --hard &&
821         echo quux >bazzy &&
822         git add bazzy &&
823         STASH_ID=$(git stash create) &&
824         git reset --hard &&
825         test_path_is_missing bazzy &&
826         git stash store -m quuxery $STASH_ID &&
827         test $(git rev-parse stash) = $STASH_ID &&
828         git reflog --format=%H stash| grep $STASH_ID &&
829         git stash pop &&
830         grep quux bazzy
831 '
832
833 test_expect_success 'handle stash specification with spaces' '
834         git stash clear &&
835         echo pig >file &&
836         git stash &&
837         stamp=$(git log -g --format="%cd" -1 refs/stash) &&
838         test_tick &&
839         echo cow >file &&
840         git stash &&
841         git stash apply "stash@{$stamp}" &&
842         grep pig file
843 '
844
845 test_expect_success 'setup stash with index and worktree changes' '
846         git stash clear &&
847         git reset --hard &&
848         echo index >file &&
849         git add file &&
850         echo working >file &&
851         git stash
852 '
853
854 test_expect_success 'stash list implies --first-parent -m' '
855         cat >expect <<-EOF &&
856         stash@{0}
857
858         diff --git a/file b/file
859         index 257cc56..d26b33d 100644
860         --- a/file
861         +++ b/file
862         @@ -1 +1 @@
863         -foo
864         +working
865         EOF
866         git stash list --format=%gd -p >actual &&
867         diff_cmp expect actual
868 '
869
870 test_expect_success 'stash list --cc shows combined diff' '
871         cat >expect <<-\EOF &&
872         stash@{0}
873
874         diff --cc file
875         index 257cc56,9015a7a..d26b33d
876         --- a/file
877         +++ b/file
878         @@@ -1,1 -1,1 +1,1 @@@
879         - foo
880          -index
881         ++working
882         EOF
883         git stash list --format=%gd -p --cc >actual &&
884         diff_cmp expect actual
885 '
886
887 test_expect_success 'stash is not confused by partial renames' '
888         mv file renamed &&
889         git add renamed &&
890         git stash &&
891         git stash apply &&
892         test_path_is_file renamed &&
893         test_path_is_missing file
894 '
895
896 test_expect_success 'push -m shows right message' '
897         >foo &&
898         git add foo &&
899         git stash push -m "test message" &&
900         echo "stash@{0}: On master: test message" >expect &&
901         git stash list -1 >actual &&
902         test_cmp expect actual
903 '
904
905 test_expect_success 'push -m also works without space' '
906         >foo &&
907         git add foo &&
908         git stash push -m"unspaced test message" &&
909         echo "stash@{0}: On master: unspaced test message" >expect &&
910         git stash list -1 >actual &&
911         test_cmp expect actual
912 '
913
914 test_expect_success 'store -m foo shows right message' '
915         git stash clear &&
916         git reset --hard &&
917         echo quux >bazzy &&
918         git add bazzy &&
919         STASH_ID=$(git stash create) &&
920         git stash store -m "store m" $STASH_ID &&
921         echo "stash@{0}: store m" >expect &&
922         git stash list -1 >actual &&
923         test_cmp expect actual
924 '
925
926 test_expect_success 'store -mfoo shows right message' '
927         git stash clear &&
928         git reset --hard &&
929         echo quux >bazzy &&
930         git add bazzy &&
931         STASH_ID=$(git stash create) &&
932         git stash store -m"store mfoo" $STASH_ID &&
933         echo "stash@{0}: store mfoo" >expect &&
934         git stash list -1 >actual &&
935         test_cmp expect actual
936 '
937
938 test_expect_success 'store --message=foo shows right message' '
939         git stash clear &&
940         git reset --hard &&
941         echo quux >bazzy &&
942         git add bazzy &&
943         STASH_ID=$(git stash create) &&
944         git stash store --message="store message=foo" $STASH_ID &&
945         echo "stash@{0}: store message=foo" >expect &&
946         git stash list -1 >actual &&
947         test_cmp expect actual
948 '
949
950 test_expect_success 'store --message foo shows right message' '
951         git stash clear &&
952         git reset --hard &&
953         echo quux >bazzy &&
954         git add bazzy &&
955         STASH_ID=$(git stash create) &&
956         git stash store --message "store message foo" $STASH_ID &&
957         echo "stash@{0}: store message foo" >expect &&
958         git stash list -1 >actual &&
959         test_cmp expect actual
960 '
961
962 test_expect_success 'push -mfoo uses right message' '
963         >foo &&
964         git add foo &&
965         git stash push -m"test mfoo" &&
966         echo "stash@{0}: On master: test mfoo" >expect &&
967         git stash list -1 >actual &&
968         test_cmp expect actual
969 '
970
971 test_expect_success 'push --message foo is synonym for -mfoo' '
972         >foo &&
973         git add foo &&
974         git stash push --message "test message foo" &&
975         echo "stash@{0}: On master: test message foo" >expect &&
976         git stash list -1 >actual &&
977         test_cmp expect actual
978 '
979
980 test_expect_success 'push --message=foo is synonym for -mfoo' '
981         >foo &&
982         git add foo &&
983         git stash push --message="test message=foo" &&
984         echo "stash@{0}: On master: test message=foo" >expect &&
985         git stash list -1 >actual &&
986         test_cmp expect actual
987 '
988
989 test_expect_success 'push -m shows right message' '
990         >foo &&
991         git add foo &&
992         git stash push -m "test m foo" &&
993         echo "stash@{0}: On master: test m foo" >expect &&
994         git stash list -1 >actual &&
995         test_cmp expect actual
996 '
997
998 test_expect_success 'create stores correct message' '
999         >foo &&
1000         git add foo &&
1001         STASH_ID=$(git stash create "create test message") &&
1002         echo "On master: create test message" >expect &&
1003         git show --pretty=%s -s ${STASH_ID} >actual &&
1004         test_cmp expect actual
1005 '
1006
1007 test_expect_success 'create with multiple arguments for the message' '
1008         >foo &&
1009         git add foo &&
1010         STASH_ID=$(git stash create test untracked) &&
1011         echo "On master: test untracked" >expect &&
1012         git show --pretty=%s -s ${STASH_ID} >actual &&
1013         test_cmp expect actual
1014 '
1015
1016 test_expect_success 'create in a detached state' '
1017         test_when_finished "git checkout master" &&
1018         git checkout HEAD~1 &&
1019         >foo &&
1020         git add foo &&
1021         STASH_ID=$(git stash create) &&
1022         HEAD_ID=$(git rev-parse --short HEAD) &&
1023         echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1024         git show --pretty=%s -s ${STASH_ID} >actual &&
1025         test_cmp expect actual
1026 '
1027
1028 test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1029         >foo &&
1030         >bar &&
1031         git add foo bar &&
1032         git stash push -- foo &&
1033         test_path_is_file bar &&
1034         test_path_is_missing foo &&
1035         git stash pop &&
1036         test_path_is_file foo &&
1037         test_path_is_file bar
1038 '
1039
1040 test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1041         mkdir sub &&
1042         >foo &&
1043         >bar &&
1044         git add foo bar &&
1045         (
1046                 cd sub &&
1047                 git stash push -- ../foo
1048         ) &&
1049         test_path_is_file bar &&
1050         test_path_is_missing foo &&
1051         git stash pop &&
1052         test_path_is_file foo &&
1053         test_path_is_file bar
1054 '
1055
1056 test_expect_success 'stash with multiple pathspec arguments' '
1057         >foo &&
1058         >bar &&
1059         >extra &&
1060         git add foo bar extra &&
1061         git stash push -- foo bar &&
1062         test_path_is_missing bar &&
1063         test_path_is_missing foo &&
1064         test_path_is_file extra &&
1065         git stash pop &&
1066         test_path_is_file foo &&
1067         test_path_is_file bar &&
1068         test_path_is_file extra
1069 '
1070
1071 test_expect_success 'stash with file including $IFS character' '
1072         >"foo bar" &&
1073         >foo &&
1074         >bar &&
1075         git add foo* &&
1076         git stash push -- "foo b*" &&
1077         test_path_is_missing "foo bar" &&
1078         test_path_is_file foo &&
1079         test_path_is_file bar &&
1080         git stash pop &&
1081         test_path_is_file "foo bar" &&
1082         test_path_is_file foo &&
1083         test_path_is_file bar
1084 '
1085
1086 test_expect_success 'stash with pathspec matching multiple paths' '
1087        echo original >file &&
1088        echo original >other-file &&
1089        git commit -m "two" file other-file &&
1090        echo modified >file &&
1091        echo modified >other-file &&
1092        git stash push -- "*file" &&
1093        echo original >expect &&
1094        test_cmp expect file &&
1095        test_cmp expect other-file &&
1096        git stash pop &&
1097        echo modified >expect &&
1098        test_cmp expect file &&
1099        test_cmp expect other-file
1100 '
1101
1102 test_expect_success 'stash push -p with pathspec shows no changes only once' '
1103         >foo &&
1104         git add foo &&
1105         git commit -m "tmp" &&
1106         git stash push -p foo >actual &&
1107         echo "No local changes to save" >expect &&
1108         git reset --hard HEAD~ &&
1109         test_i18ncmp expect actual
1110 '
1111
1112 test_expect_success 'push <pathspec>: show no changes when there are none' '
1113         >foo &&
1114         git add foo &&
1115         git commit -m "tmp" &&
1116         git stash push foo >actual &&
1117         echo "No local changes to save" >expect &&
1118         git reset --hard HEAD~ &&
1119         test_i18ncmp expect actual
1120 '
1121
1122 test_expect_success 'push: <pathspec> not in the repository errors out' '
1123         >untracked &&
1124         test_must_fail git stash push untracked &&
1125         test_path_is_file untracked
1126 '
1127
1128 test_expect_success 'push: -q is quiet with changes' '
1129         >foo &&
1130         git add foo &&
1131         git stash push -q >output 2>&1 &&
1132         test_must_be_empty output
1133 '
1134
1135 test_expect_success 'push: -q is quiet with no changes' '
1136         git stash push -q >output 2>&1 &&
1137         test_must_be_empty output
1138 '
1139
1140 test_expect_success 'push: -q is quiet even if there is no initial commit' '
1141         git init foo_dir &&
1142         test_when_finished rm -rf foo_dir &&
1143         (
1144                 cd foo_dir &&
1145                 >bar &&
1146                 test_must_fail git stash push -q >output 2>&1 &&
1147                 test_must_be_empty output
1148         )
1149 '
1150
1151 test_expect_success 'untracked files are left in place when -u is not given' '
1152         >file &&
1153         git add file &&
1154         >untracked &&
1155         git stash push file &&
1156         test_path_is_file untracked
1157 '
1158
1159 test_expect_success 'stash without verb with pathspec' '
1160         >"foo bar" &&
1161         >foo &&
1162         >bar &&
1163         git add foo* &&
1164         git stash -- "foo b*" &&
1165         test_path_is_missing "foo bar" &&
1166         test_path_is_file foo &&
1167         test_path_is_file bar &&
1168         git stash pop &&
1169         test_path_is_file "foo bar" &&
1170         test_path_is_file foo &&
1171         test_path_is_file bar
1172 '
1173
1174 test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1175         git reset &&
1176         >foo &&
1177         >bar &&
1178         git add foo bar &&
1179         git commit -m "test" &&
1180         echo "foo" >foo &&
1181         echo "bar" >bar &&
1182         git stash -k -- foo &&
1183         test "",bar = $(cat foo),$(cat bar) &&
1184         git stash pop &&
1185         test foo,bar = $(cat foo),$(cat bar)
1186 '
1187
1188 test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1189         git reset &&
1190         >subdir/untracked &&
1191         >subdir/tracked1 &&
1192         >subdir/tracked2 &&
1193         git add subdir/tracked* &&
1194         git stash -- subdir/ &&
1195         test_path_is_missing subdir/tracked1 &&
1196         test_path_is_missing subdir/tracked2 &&
1197         test_path_is_file subdir/untracked &&
1198         git stash pop &&
1199         test_path_is_file subdir/tracked1 &&
1200         test_path_is_file subdir/tracked2 &&
1201         test_path_is_file subdir/untracked
1202 '
1203
1204 test_expect_success 'stash -- <subdir> works with binary files' '
1205         git reset &&
1206         >subdir/untracked &&
1207         >subdir/tracked &&
1208         cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1209         git add subdir/tracked* &&
1210         git stash -- subdir/ &&
1211         test_path_is_missing subdir/tracked &&
1212         test_path_is_missing subdir/tracked-binary &&
1213         test_path_is_file subdir/untracked &&
1214         git stash pop &&
1215         test_path_is_file subdir/tracked &&
1216         test_path_is_file subdir/tracked-binary &&
1217         test_path_is_file subdir/untracked
1218 '
1219
1220 test_expect_success 'stash with user.name and user.email set works' '
1221         test_config user.name "A U Thor" &&
1222         test_config user.email "a.u@thor" &&
1223         git stash
1224 '
1225
1226 test_expect_success 'stash works when user.name and user.email are not set' '
1227         git reset &&
1228         >1 &&
1229         git add 1 &&
1230         echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1231         git stash &&
1232         git show -s --format="%an <%ae>" refs/stash >actual &&
1233         test_cmp expect actual &&
1234         >2 &&
1235         git add 2 &&
1236         test_config user.useconfigonly true &&
1237         test_config stash.usebuiltin true &&
1238         (
1239                 sane_unset GIT_AUTHOR_NAME &&
1240                 sane_unset GIT_AUTHOR_EMAIL &&
1241                 sane_unset GIT_COMMITTER_NAME &&
1242                 sane_unset GIT_COMMITTER_EMAIL &&
1243                 test_unconfig user.email &&
1244                 test_unconfig user.name &&
1245                 test_must_fail git commit -m "should fail" &&
1246                 echo "git stash <git@stash>" >expect &&
1247                 >2 &&
1248                 git stash &&
1249                 git show -s --format="%an <%ae>" refs/stash >actual &&
1250                 test_cmp expect actual
1251         )
1252 '
1253
1254 test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1255         test_commit to-remove to-remove &&
1256         git rm to-remove &&
1257         git stash --keep-index &&
1258         test_path_is_missing to-remove
1259 '
1260
1261 test_expect_success 'stash apply should succeed with unmodified file' '
1262         echo base >file &&
1263         git add file &&
1264         git commit -m base &&
1265
1266         # now stash a modification
1267         echo modified >file &&
1268         git stash &&
1269
1270         # make the file stat dirty
1271         cp file other &&
1272         mv other file &&
1273
1274         git stash apply
1275 '
1276
1277 test_expect_success 'stash handles skip-worktree entries nicely' '
1278         test_commit A &&
1279         echo changed >A.t &&
1280         git add A.t &&
1281         git update-index --skip-worktree A.t &&
1282         rm A.t &&
1283         git stash &&
1284
1285         git rev-parse --verify refs/stash:A.t
1286 '
1287
1288 test_done