Merge branch 'mg/reflog-with-options' 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         test_tick &&
14         git commit -m initial &&
15         echo 2 > file &&
16         git add file &&
17         echo 3 > file &&
18         test_tick &&
19         git stash &&
20         git diff-files --quiet &&
21         git diff-index --cached --quiet HEAD
22 '
23
24 cat > expect << EOF
25 diff --git a/file b/file
26 index 0cfbf08..00750ed 100644
27 --- a/file
28 +++ b/file
29 @@ -1 +1 @@
30 -2
31 +3
32 EOF
33
34 test_expect_success 'parents of stash' '
35         test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
36         git diff stash^2..stash > output &&
37         test_cmp output expect
38 '
39
40 test_expect_success 'apply does not need clean working directory' '
41         echo 4 >other-file &&
42         git add other-file &&
43         echo 5 >other-file &&
44         git stash apply &&
45         echo 3 >expect &&
46         test_cmp expect file
47 '
48
49 test_expect_success 'apply does not clobber working directory changes' '
50         git reset --hard &&
51         echo 4 >file &&
52         test_must_fail git stash apply &&
53         echo 4 >expect &&
54         test_cmp expect file
55 '
56
57 test_expect_success 'apply stashed changes' '
58         git reset --hard &&
59         echo 5 >other-file &&
60         git add other-file &&
61         test_tick &&
62         git commit -m other-file &&
63         git stash apply &&
64         test 3 = $(cat file) &&
65         test 1 = $(git show :file) &&
66         test 1 = $(git show HEAD:file)
67 '
68
69 test_expect_success 'apply stashed changes (including index)' '
70         git reset --hard HEAD^ &&
71         echo 6 > other-file &&
72         git add other-file &&
73         test_tick &&
74         git commit -m other-file &&
75         git stash apply --index &&
76         test 3 = $(cat file) &&
77         test 2 = $(git show :file) &&
78         test 1 = $(git show HEAD:file)
79 '
80
81 test_expect_success 'unstashing in a subdirectory' '
82         git reset --hard HEAD &&
83         mkdir subdir &&
84         (
85                 cd subdir &&
86                 git stash apply
87         )
88 '
89
90 test_expect_success 'drop top stash' '
91         git reset --hard &&
92         git stash list > stashlist1 &&
93         echo 7 > file &&
94         git stash &&
95         git stash drop &&
96         git stash list > stashlist2 &&
97         test_cmp stashlist1 stashlist2 &&
98         git stash apply &&
99         test 3 = $(cat file) &&
100         test 1 = $(git show :file) &&
101         test 1 = $(git show HEAD:file)
102 '
103
104 test_expect_success 'drop middle stash' '
105         git reset --hard &&
106         echo 8 > file &&
107         git stash &&
108         echo 9 > file &&
109         git stash &&
110         git stash drop stash@{1} &&
111         test 2 = $(git stash list | wc -l) &&
112         git stash apply &&
113         test 9 = $(cat file) &&
114         test 1 = $(git show :file) &&
115         test 1 = $(git show HEAD:file) &&
116         git reset --hard &&
117         git stash drop &&
118         git stash apply &&
119         test 3 = $(cat file) &&
120         test 1 = $(git show :file) &&
121         test 1 = $(git show HEAD:file)
122 '
123
124 test_expect_success 'stash pop' '
125         git reset --hard &&
126         git stash pop &&
127         test 3 = $(cat file) &&
128         test 1 = $(git show :file) &&
129         test 1 = $(git show HEAD:file) &&
130         test 0 = $(git stash list | wc -l)
131 '
132
133 cat > expect << EOF
134 diff --git a/file2 b/file2
135 new file mode 100644
136 index 0000000..1fe912c
137 --- /dev/null
138 +++ b/file2
139 @@ -0,0 +1 @@
140 +bar2
141 EOF
142
143 cat > expect1 << EOF
144 diff --git a/file b/file
145 index 257cc56..5716ca5 100644
146 --- a/file
147 +++ b/file
148 @@ -1 +1 @@
149 -foo
150 +bar
151 EOF
152
153 cat > expect2 << EOF
154 diff --git a/file b/file
155 index 7601807..5716ca5 100644
156 --- a/file
157 +++ b/file
158 @@ -1 +1 @@
159 -baz
160 +bar
161 diff --git a/file2 b/file2
162 new file mode 100644
163 index 0000000..1fe912c
164 --- /dev/null
165 +++ b/file2
166 @@ -0,0 +1 @@
167 +bar2
168 EOF
169
170 test_expect_success 'stash branch' '
171         echo foo > file &&
172         git commit file -m first &&
173         echo bar > file &&
174         echo bar2 > file2 &&
175         git add file2 &&
176         git stash &&
177         echo baz > file &&
178         git commit file -m second &&
179         git stash branch stashbranch &&
180         test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
181         test $(git rev-parse HEAD) = $(git rev-parse master^) &&
182         git diff --cached > output &&
183         test_cmp output expect &&
184         git diff > output &&
185         test_cmp output expect1 &&
186         git add file &&
187         git commit -m alternate\ second &&
188         git diff master..stashbranch > output &&
189         test_cmp output expect2 &&
190         test 0 = $(git stash list | wc -l)
191 '
192
193 test_expect_success 'apply -q is quiet' '
194         echo foo > file &&
195         git stash &&
196         git stash apply -q > output.out 2>&1 &&
197         test ! -s output.out
198 '
199
200 test_expect_success 'save -q is quiet' '
201         git stash save --quiet > output.out 2>&1 &&
202         test ! -s output.out
203 '
204
205 test_expect_success 'pop -q is quiet' '
206         git stash pop -q > output.out 2>&1 &&
207         test ! -s output.out
208 '
209
210 test_expect_success 'pop -q --index works and is quiet' '
211         echo foo > file &&
212         git add file &&
213         git stash save --quiet &&
214         git stash pop -q --index > output.out 2>&1 &&
215         test foo = "$(git show :file)" &&
216         test ! -s output.out
217 '
218
219 test_expect_success 'drop -q is quiet' '
220         git stash &&
221         git stash drop -q > output.out 2>&1 &&
222         test ! -s output.out
223 '
224
225 test_expect_success 'stash -k' '
226         echo bar3 > file &&
227         echo bar4 > file2 &&
228         git add file2 &&
229         git stash -k &&
230         test bar,bar4 = $(cat file),$(cat file2)
231 '
232
233 test_expect_success 'stash --invalid-option' '
234         echo bar5 > file &&
235         echo bar6 > file2 &&
236         git add file2 &&
237         test_must_fail git stash --invalid-option &&
238         test_must_fail git stash save --invalid-option &&
239         test bar5,bar6 = $(cat file),$(cat file2) &&
240         git stash -- -message-starting-with-dash &&
241         test bar,bar2 = $(cat file),$(cat file2)
242 '
243
244 test_expect_success 'stash an added file' '
245         git reset --hard &&
246         echo new >file3 &&
247         git add file3 &&
248         git stash save "added file" &&
249         ! test -r file3 &&
250         git stash apply &&
251         test new = "$(cat file3)"
252 '
253
254 test_expect_success 'stash rm then recreate' '
255         git reset --hard &&
256         git rm file &&
257         echo bar7 >file &&
258         git stash save "rm then recreate" &&
259         test bar = "$(cat file)" &&
260         git stash apply &&
261         test bar7 = "$(cat file)"
262 '
263
264 test_expect_success 'stash rm and ignore' '
265         git reset --hard &&
266         git rm file &&
267         echo file >.gitignore &&
268         git stash save "rm and ignore" &&
269         test bar = "$(cat file)" &&
270         test file = "$(cat .gitignore)" &&
271         git stash apply &&
272         ! test -r file &&
273         test file = "$(cat .gitignore)"
274 '
275
276 test_expect_success 'stash rm and ignore (stage .gitignore)' '
277         git reset --hard &&
278         git rm file &&
279         echo file >.gitignore &&
280         git add .gitignore &&
281         git stash save "rm and ignore (stage .gitignore)" &&
282         test bar = "$(cat file)" &&
283         ! test -r .gitignore &&
284         git stash apply &&
285         ! test -r file &&
286         test file = "$(cat .gitignore)"
287 '
288
289 test_expect_success SYMLINKS 'stash file to symlink' '
290         git reset --hard &&
291         rm file &&
292         ln -s file2 file &&
293         git stash save "file to symlink" &&
294         test -f file &&
295         test bar = "$(cat file)" &&
296         git stash apply &&
297         case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
298 '
299
300 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
301         git reset --hard &&
302         git rm file &&
303         ln -s file2 file &&
304         git stash save "file to symlink (stage rm)" &&
305         test -f file &&
306         test bar = "$(cat file)" &&
307         git stash apply &&
308         case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
309 '
310
311 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
312         git reset --hard &&
313         rm file &&
314         ln -s file2 file &&
315         git add file &&
316         git stash save "file to symlink (full stage)" &&
317         test -f file &&
318         test bar = "$(cat file)" &&
319         git stash apply &&
320         case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
321 '
322
323 # This test creates a commit with a symlink used for the following tests
324
325 test_expect_success SYMLINKS 'stash symlink to file' '
326         git reset --hard &&
327         ln -s file filelink &&
328         git add filelink &&
329         git commit -m "Add symlink" &&
330         rm filelink &&
331         cp file filelink &&
332         git stash save "symlink to file" &&
333         test -h filelink &&
334         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
335         git stash apply &&
336         ! test -h filelink &&
337         test bar = "$(cat file)"
338 '
339
340 test_expect_success SYMLINKS 'stash symlink to file (stage rm)' '
341         git reset --hard &&
342         git rm filelink &&
343         cp file filelink &&
344         git stash save "symlink to file (stage rm)" &&
345         test -h filelink &&
346         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
347         git stash apply &&
348         ! test -h filelink &&
349         test bar = "$(cat file)"
350 '
351
352 test_expect_success SYMLINKS 'stash symlink to file (full stage)' '
353         git reset --hard &&
354         rm filelink &&
355         cp file filelink &&
356         git add filelink &&
357         git stash save "symlink to file (full stage)" &&
358         test -h filelink &&
359         case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
360         git stash apply &&
361         ! test -h filelink &&
362         test bar = "$(cat file)"
363 '
364
365 test_expect_failure 'stash directory to file' '
366         git reset --hard &&
367         mkdir dir &&
368         echo foo >dir/file &&
369         git add dir/file &&
370         git commit -m "Add file in dir" &&
371         rm -fr dir &&
372         echo bar >dir &&
373         git stash save "directory to file" &&
374         test -d dir &&
375         test foo = "$(cat dir/file)" &&
376         test_must_fail git stash apply &&
377         test bar = "$(cat dir)" &&
378         git reset --soft HEAD^
379 '
380
381 test_expect_failure 'stash file to directory' '
382         git reset --hard &&
383         rm file &&
384         mkdir file &&
385         echo foo >file/file &&
386         git stash save "file to directory" &&
387         test -f file &&
388         test bar = "$(cat file)" &&
389         git stash apply &&
390         test -f file/file &&
391         test foo = "$(cat file/file)"
392 '
393
394 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
395         git stash clear &&
396         test_when_finished "git reset --hard HEAD" &&
397         git reset --hard &&
398         echo foo >> file &&
399         STASH_ID=$(git stash create) &&
400         git reset --hard &&
401         git stash branch stash-branch ${STASH_ID} &&
402         test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
403         test $(git ls-files --modified | wc -l) -eq 1
404 '
405
406 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
407         git stash clear &&
408         test_when_finished "git reset --hard HEAD" &&
409         git reset --hard &&
410         echo foo >> file &&
411         git stash &&
412         test_when_finished "git stash drop" &&
413         echo bar >> file &&
414         STASH_ID=$(git stash create) &&
415         git reset --hard &&
416         git stash branch stash-branch ${STASH_ID} &&
417         test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
418         test $(git ls-files --modified | wc -l) -eq 1
419 '
420
421 test_expect_success 'stash show - stashes on stack, stash-like argument' '
422         git stash clear &&
423         test_when_finished "git reset --hard HEAD" &&
424         git reset --hard &&
425         echo foo >> file &&
426         git stash &&
427         test_when_finished "git stash drop" &&
428         echo bar >> file &&
429         STASH_ID=$(git stash create) &&
430         git reset --hard &&
431         cat >expected <<-EOF &&
432          file |    1 +
433          1 files changed, 1 insertions(+), 0 deletions(-)
434         EOF
435         git stash show ${STASH_ID} >actual &&
436         test_cmp expected actual
437 '
438
439 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
440         git stash clear &&
441         test_when_finished "git reset --hard HEAD" &&
442         git reset --hard &&
443         echo foo >> file &&
444         git stash &&
445         test_when_finished "git stash drop" &&
446         echo bar >> file &&
447         STASH_ID=$(git stash create) &&
448         git reset --hard &&
449         cat >expected <<-EOF &&
450         diff --git a/file b/file
451         index 7601807..935fbd3 100644
452         --- a/file
453         +++ b/file
454         @@ -1 +1,2 @@
455          baz
456         +bar
457         EOF
458         git stash show -p ${STASH_ID} >actual &&
459         test_cmp expected actual
460 '
461
462 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
463         git stash clear &&
464         test_when_finished "git reset --hard HEAD" &&
465         git reset --hard &&
466         echo foo >> file &&
467         STASH_ID=$(git stash create) &&
468         git reset --hard &&
469         cat >expected <<-EOF &&
470          file |    1 +
471          1 files changed, 1 insertions(+), 0 deletions(-)
472         EOF
473         git stash show ${STASH_ID} >actual &&
474         test_cmp expected actual
475 '
476
477 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
478         git stash clear &&
479         test_when_finished "git reset --hard HEAD" &&
480         git reset --hard &&
481         echo foo >> file &&
482         STASH_ID=$(git stash create) &&
483         git reset --hard &&
484         cat >expected <<-EOF &&
485         diff --git a/file b/file
486         index 7601807..71b52c4 100644
487         --- a/file
488         +++ b/file
489         @@ -1 +1,2 @@
490          baz
491         +foo
492         EOF
493         git stash show -p ${STASH_ID} >actual &&
494         test_cmp expected actual
495 '
496
497 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
498         git stash clear &&
499         test_when_finished "git reset --hard HEAD && git stash clear" &&
500         git reset --hard &&
501         echo foo > file &&
502         git stash &&
503         echo bar > file &&
504         git stash &&
505         test_must_fail git stash drop $(git rev-parse stash@{0}) &&
506         git stash pop &&
507         test bar = "$(cat file)" &&
508         git reset --hard HEAD
509 '
510
511 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
512         git stash clear &&
513         test_when_finished "git reset --hard HEAD && git stash clear" &&
514         git reset --hard &&
515         echo foo > file &&
516         git stash &&
517         echo bar > file &&
518         git stash &&
519         test_must_fail git stash pop $(git rev-parse stash@{0}) &&
520         git stash pop &&
521         test bar = "$(cat file)" &&
522         git reset --hard HEAD
523 '
524
525 test_expect_success 'ref with non-existant reflog' '
526         git stash clear &&
527         echo bar5 > file &&
528         echo bar6 > file2 &&
529         git add file2 &&
530         git stash &&
531         ! "git rev-parse --quiet --verify does-not-exist" &&
532         test_must_fail git stash drop does-not-exist &&
533         test_must_fail git stash drop does-not-exist@{0} &&
534         test_must_fail git stash pop does-not-exist &&
535         test_must_fail git stash pop does-not-exist@{0} &&
536         test_must_fail git stash apply does-not-exist &&
537         test_must_fail git stash apply does-not-exist@{0} &&
538         test_must_fail git stash show does-not-exist &&
539         test_must_fail git stash show does-not-exist@{0} &&
540         test_must_fail git stash branch tmp does-not-exist &&
541         test_must_fail git stash branch tmp does-not-exist@{0} &&
542         git stash drop
543 '
544
545 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
546         git stash clear &&
547         test_must_fail git stash drop stash@{0} &&
548         echo bar5 > file &&
549         echo bar6 > file2 &&
550         git add file2 &&
551         git stash &&
552         test_must_fail git drop stash@{1} &&
553         test_must_fail git pop stash@{1} &&
554         test_must_fail git apply stash@{1} &&
555         test_must_fail git show stash@{1} &&
556         test_must_fail git branch tmp stash@{1} &&
557         git stash drop
558 '
559
560 test_expect_success 'stash branch should not drop the stash if the branch exists' '
561         git stash clear &&
562         echo foo >file &&
563         git add file &&
564         git commit -m initial &&
565         echo bar >file &&
566         git stash &&
567         test_must_fail git stash branch master stash@{0} &&
568         git rev-parse stash@{0} --
569 '
570
571 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
572         git stash clear &&
573         echo 1 >subdir/subfile1 &&
574         echo 2 >subdir/subfile2 &&
575         git add subdir/subfile1 &&
576         git commit -m subdir &&
577         (
578                 cd subdir &&
579                 echo x >subfile1 &&
580                 echo x >../file &&
581                 git status >../expect &&
582                 git stash &&
583                 sane_unset GIT_MERGE_VERBOSITY &&
584                 git stash apply
585         ) |
586         sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
587         test_cmp expect actual
588 '
589
590 test_done