Merge branch 'master' of git://repo.or.cz/alt-git
[git/mingw.git] / t / t3903-stash.sh
blobe75b052ed85804f31cd06b2ce6222acc72e004f8
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes E Schindelin
6 test_description='Test git stash'
8 . ./test-lib.sh
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
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 @@
32 EOF
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
40 test_expect_success 'applying bogus stash does nothing' '
41 test_must_fail git stash apply stash@{1} &&
42 echo 1 >expect &&
43 test_cmp expect file
46 test_expect_success 'apply does not need clean working directory' '
47 echo 4 >other-file &&
48 git add other-file &&
49 echo 5 >other-file &&
50 git stash apply &&
51 echo 3 >expect &&
52 test_cmp expect file
55 test_expect_success 'apply does not clobber working directory changes' '
56 git reset --hard &&
57 echo 4 >file &&
58 test_must_fail git stash apply &&
59 echo 4 >expect &&
60 test_cmp expect file
63 test_expect_success 'apply stashed changes' '
64 git reset --hard &&
65 echo 5 >other-file &&
66 git add other-file &&
67 test_tick &&
68 git commit -m other-file &&
69 git stash apply &&
70 test 3 = $(cat file) &&
71 test 1 = $(git show :file) &&
72 test 1 = $(git show HEAD:file)
75 test_expect_success 'apply stashed changes (including index)' '
76 git reset --hard HEAD^ &&
77 echo 6 > other-file &&
78 git add other-file &&
79 test_tick &&
80 git commit -m other-file &&
81 git stash apply --index &&
82 test 3 = $(cat file) &&
83 test 2 = $(git show :file) &&
84 test 1 = $(git show HEAD:file)
87 test_expect_success 'unstashing in a subdirectory' '
88 git reset --hard HEAD &&
89 mkdir subdir &&
91 cd subdir &&
92 git stash apply
96 test_expect_success 'drop top stash' '
97 git reset --hard &&
98 git stash list > stashlist1 &&
99 echo 7 > file &&
100 git stash &&
101 git stash drop &&
102 git stash list > stashlist2 &&
103 test_cmp stashlist1 stashlist2 &&
104 git stash apply &&
105 test 3 = $(cat file) &&
106 test 1 = $(git show :file) &&
107 test 1 = $(git show HEAD:file)
110 test_expect_success 'drop middle stash' '
111 git reset --hard &&
112 echo 8 > file &&
113 git stash &&
114 sleep 1 &&
115 echo 9 > file &&
116 git stash &&
117 git stash drop stash@{1} &&
118 test 2 = $(git stash list | wc -l) &&
119 git stash apply &&
120 test 9 = $(cat file) &&
121 test 1 = $(git show :file) &&
122 test 1 = $(git show HEAD:file) &&
123 git reset --hard &&
124 git stash drop &&
125 git stash apply &&
126 test 3 = $(cat file) &&
127 test 1 = $(git show :file) &&
128 test 1 = $(git show HEAD:file)
131 test_expect_success 'stash pop' '
132 git reset --hard &&
133 git stash pop &&
134 test 3 = $(cat file) &&
135 test 1 = $(git show :file) &&
136 test 1 = $(git show HEAD:file) &&
137 test 0 = $(git stash list | wc -l)
140 cat > expect << EOF
141 diff --git a/file2 b/file2
142 new file mode 100644
143 index 0000000..1fe912c
144 --- /dev/null
145 +++ b/file2
146 @@ -0,0 +1 @@
147 +bar2
150 cat > expect1 << EOF
151 diff --git a/file b/file
152 index 257cc56..5716ca5 100644
153 --- a/file
154 +++ b/file
155 @@ -1 +1 @@
156 -foo
157 +bar
160 cat > expect2 << EOF
161 diff --git a/file b/file
162 index 7601807..5716ca5 100644
163 --- a/file
164 +++ b/file
165 @@ -1 +1 @@
166 -baz
167 +bar
168 diff --git a/file2 b/file2
169 new file mode 100644
170 index 0000000..1fe912c
171 --- /dev/null
172 +++ b/file2
173 @@ -0,0 +1 @@
174 +bar2
177 test_expect_success 'stash branch' '
178 echo foo > file &&
179 git commit file -m first &&
180 echo bar > file &&
181 echo bar2 > file2 &&
182 git add file2 &&
183 git stash &&
184 echo baz > file &&
185 git commit file -m second &&
186 git stash branch stashbranch &&
187 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
188 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
189 git diff --cached > output &&
190 test_cmp output expect &&
191 git diff > output &&
192 test_cmp output expect1 &&
193 git add file &&
194 git commit -m alternate\ second &&
195 git diff master..stashbranch > output &&
196 test_cmp output expect2 &&
197 test 0 = $(git stash list | wc -l)
200 test_expect_success 'apply -q is quiet' '
201 echo foo > file &&
202 git stash &&
203 git stash apply -q > output.out 2>&1 &&
204 test_must_be_empty output.out
207 test_expect_success 'save -q is quiet' '
208 git stash save --quiet > output.out 2>&1 &&
209 test_must_be_empty output.out
212 test_expect_success 'pop -q is quiet' '
213 git stash pop -q > output.out 2>&1 &&
214 test_must_be_empty output.out
217 test_expect_success 'pop -q --index works and is quiet' '
218 echo foo > file &&
219 git add file &&
220 git stash save --quiet &&
221 git stash pop -q --index > output.out 2>&1 &&
222 test foo = "$(git show :file)" &&
223 test_must_be_empty output.out
226 test_expect_success 'drop -q is quiet' '
227 git stash &&
228 git stash drop -q > output.out 2>&1 &&
229 test_must_be_empty output.out
232 test_expect_success 'stash -k' '
233 echo bar3 > file &&
234 echo bar4 > file2 &&
235 git add file2 &&
236 git stash -k &&
237 test bar,bar4 = $(cat file),$(cat file2)
240 test_expect_success 'stash --no-keep-index' '
241 echo bar33 > file &&
242 echo bar44 > file2 &&
243 git add file2 &&
244 git stash --no-keep-index &&
245 test bar,bar2 = $(cat file),$(cat file2)
248 test_expect_success 'stash --invalid-option' '
249 echo bar5 > file &&
250 echo bar6 > file2 &&
251 git add file2 &&
252 test_must_fail git stash --invalid-option &&
253 test_must_fail git stash save --invalid-option &&
254 test bar5,bar6 = $(cat file),$(cat file2) &&
255 git stash -- -message-starting-with-dash &&
256 test bar,bar2 = $(cat file),$(cat file2)
259 test_expect_success 'stash an added file' '
260 git reset --hard &&
261 echo new >file3 &&
262 git add file3 &&
263 git stash save "added file" &&
264 ! test -r file3 &&
265 git stash apply &&
266 test new = "$(cat file3)"
269 test_expect_success 'stash rm then recreate' '
270 git reset --hard &&
271 git rm file &&
272 echo bar7 >file &&
273 git stash save "rm then recreate" &&
274 test bar = "$(cat file)" &&
275 git stash apply &&
276 test bar7 = "$(cat file)"
279 test_expect_success 'stash rm and ignore' '
280 git reset --hard &&
281 git rm file &&
282 echo file >.gitignore &&
283 git stash save "rm and ignore" &&
284 test bar = "$(cat file)" &&
285 test file = "$(cat .gitignore)" &&
286 git stash apply &&
287 ! test -r file &&
288 test file = "$(cat .gitignore)"
291 test_expect_success 'stash rm and ignore (stage .gitignore)' '
292 git reset --hard &&
293 git rm file &&
294 echo file >.gitignore &&
295 git add .gitignore &&
296 git stash save "rm and ignore (stage .gitignore)" &&
297 test bar = "$(cat file)" &&
298 ! test -r .gitignore &&
299 git stash apply &&
300 ! test -r file &&
301 test file = "$(cat .gitignore)"
304 test_expect_success SYMLINKS 'stash file to symlink' '
305 git reset --hard &&
306 rm file &&
307 ln -s file2 file &&
308 git stash save "file to symlink" &&
309 test -f file &&
310 test bar = "$(cat file)" &&
311 git stash apply &&
312 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
315 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
316 git reset --hard &&
317 git rm file &&
318 ln -s file2 file &&
319 git stash save "file to symlink (stage rm)" &&
320 test -f file &&
321 test bar = "$(cat file)" &&
322 git stash apply &&
323 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
326 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
327 git reset --hard &&
328 rm file &&
329 ln -s file2 file &&
330 git add file &&
331 git stash save "file to symlink (full stage)" &&
332 test -f file &&
333 test bar = "$(cat file)" &&
334 git stash apply &&
335 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
338 # This test creates a commit with a symlink used for the following tests
340 test_expect_success 'stash symlink to file' '
341 git reset --hard &&
342 test_ln_s_add file filelink &&
343 git commit -m "Add symlink" &&
344 rm filelink &&
345 cp file filelink &&
346 git stash save "symlink to file"
349 test_expect_success SYMLINKS 'this must have re-created the symlink' '
350 test -h filelink &&
351 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
354 test_expect_success 'unstash must re-create the file' '
355 git stash apply &&
356 ! test -h filelink &&
357 test bar = "$(cat file)"
360 test_expect_success 'stash symlink to file (stage rm)' '
361 git reset --hard &&
362 git rm filelink &&
363 cp file filelink &&
364 git stash save "symlink to file (stage rm)"
367 test_expect_success SYMLINKS 'this must have re-created the symlink' '
368 test -h filelink &&
369 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
372 test_expect_success 'unstash must re-create the file' '
373 git stash apply &&
374 ! test -h filelink &&
375 test bar = "$(cat file)"
378 test_expect_success 'stash symlink to file (full stage)' '
379 git reset --hard &&
380 rm filelink &&
381 cp file filelink &&
382 git add filelink &&
383 git stash save "symlink to file (full stage)"
386 test_expect_success SYMLINKS 'this must have re-created the symlink' '
387 test -h filelink &&
388 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
391 test_expect_success 'unstash must re-create the file' '
392 git stash apply &&
393 ! test -h filelink &&
394 test bar = "$(cat file)"
397 test_expect_failure 'stash directory to file' '
398 git reset --hard &&
399 mkdir dir &&
400 echo foo >dir/file &&
401 git add dir/file &&
402 git commit -m "Add file in dir" &&
403 rm -fr dir &&
404 echo bar >dir &&
405 git stash save "directory to file" &&
406 test -d dir &&
407 test foo = "$(cat dir/file)" &&
408 test_must_fail git stash apply &&
409 test bar = "$(cat dir)" &&
410 git reset --soft HEAD^
413 test_expect_failure 'stash file to directory' '
414 git reset --hard &&
415 rm file &&
416 mkdir file &&
417 echo foo >file/file &&
418 git stash save "file to directory" &&
419 test -f file &&
420 test bar = "$(cat file)" &&
421 git stash apply &&
422 test -f file/file &&
423 test foo = "$(cat file/file)"
426 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
427 git stash clear &&
428 test_when_finished "git reset --hard HEAD" &&
429 git reset --hard &&
430 echo foo >> file &&
431 STASH_ID=$(git stash create) &&
432 git reset --hard &&
433 git stash branch stash-branch ${STASH_ID} &&
434 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
435 test $(git ls-files --modified | wc -l) -eq 1
438 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
439 git stash clear &&
440 test_when_finished "git reset --hard HEAD" &&
441 git reset --hard &&
442 echo foo >> file &&
443 git stash &&
444 test_when_finished "git stash drop" &&
445 echo bar >> file &&
446 STASH_ID=$(git stash create) &&
447 git reset --hard &&
448 git stash branch stash-branch ${STASH_ID} &&
449 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
450 test $(git ls-files --modified | wc -l) -eq 1
453 test_expect_success 'stash show format defaults to --stat' '
454 git stash clear &&
455 test_when_finished "git reset --hard HEAD" &&
456 git reset --hard &&
457 echo foo >> file &&
458 git stash &&
459 test_when_finished "git stash drop" &&
460 echo bar >> file &&
461 STASH_ID=$(git stash create) &&
462 git reset --hard &&
463 cat >expected <<-EOF &&
464 file | 1 +
465 1 file changed, 1 insertion(+)
467 git stash show ${STASH_ID} >actual &&
468 test_i18ncmp expected actual
471 test_expect_success 'stash show - stashes on stack, stash-like argument' '
472 git stash clear &&
473 test_when_finished "git reset --hard HEAD" &&
474 git reset --hard &&
475 echo foo >> file &&
476 git stash &&
477 test_when_finished "git stash drop" &&
478 echo bar >> file &&
479 STASH_ID=$(git stash create) &&
480 git reset --hard &&
481 echo "1 0 file" >expected &&
482 git stash show --numstat ${STASH_ID} >actual &&
483 test_cmp expected actual
486 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
487 git stash clear &&
488 test_when_finished "git reset --hard HEAD" &&
489 git reset --hard &&
490 echo foo >> file &&
491 git stash &&
492 test_when_finished "git stash drop" &&
493 echo bar >> file &&
494 STASH_ID=$(git stash create) &&
495 git reset --hard &&
496 cat >expected <<-EOF &&
497 diff --git a/file b/file
498 index 7601807..935fbd3 100644
499 --- a/file
500 +++ b/file
501 @@ -1 +1,2 @@
503 +bar
505 git stash show -p ${STASH_ID} >actual &&
506 test_cmp expected actual
509 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
510 git stash clear &&
511 test_when_finished "git reset --hard HEAD" &&
512 git reset --hard &&
513 echo foo >> file &&
514 STASH_ID=$(git stash create) &&
515 git reset --hard &&
516 echo "1 0 file" >expected &&
517 git stash show --numstat ${STASH_ID} >actual &&
518 test_cmp expected actual
521 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
522 git stash clear &&
523 test_when_finished "git reset --hard HEAD" &&
524 git reset --hard &&
525 echo foo >> file &&
526 STASH_ID=$(git stash create) &&
527 git reset --hard &&
528 cat >expected <<-EOF &&
529 diff --git a/file b/file
530 index 7601807..71b52c4 100644
531 --- a/file
532 +++ b/file
533 @@ -1 +1,2 @@
535 +foo
537 git stash show -p ${STASH_ID} >actual &&
538 test_cmp expected actual
541 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
542 git stash clear &&
543 test_when_finished "git reset --hard HEAD && git stash clear" &&
544 git reset --hard &&
545 echo foo > file &&
546 git stash &&
547 echo bar > file &&
548 git stash &&
549 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
550 git stash pop &&
551 test bar = "$(cat file)" &&
552 git reset --hard HEAD
555 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
556 git stash clear &&
557 test_when_finished "git reset --hard HEAD && git stash clear" &&
558 git reset --hard &&
559 echo foo > file &&
560 git stash &&
561 echo bar > file &&
562 git stash &&
563 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
564 git stash pop &&
565 test bar = "$(cat file)" &&
566 git reset --hard HEAD
569 test_expect_success 'ref with non-existent reflog' '
570 git stash clear &&
571 echo bar5 > file &&
572 echo bar6 > file2 &&
573 git add file2 &&
574 git stash &&
575 test_must_fail git rev-parse --quiet --verify does-not-exist &&
576 test_must_fail git stash drop does-not-exist &&
577 test_must_fail git stash drop does-not-exist@{0} &&
578 test_must_fail git stash pop does-not-exist &&
579 test_must_fail git stash pop does-not-exist@{0} &&
580 test_must_fail git stash apply does-not-exist &&
581 test_must_fail git stash apply does-not-exist@{0} &&
582 test_must_fail git stash show does-not-exist &&
583 test_must_fail git stash show does-not-exist@{0} &&
584 test_must_fail git stash branch tmp does-not-exist &&
585 test_must_fail git stash branch tmp does-not-exist@{0} &&
586 git stash drop
589 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
590 git stash clear &&
591 test_must_fail git stash drop stash@{0} &&
592 echo bar5 > file &&
593 echo bar6 > file2 &&
594 git add file2 &&
595 git stash &&
596 test_must_fail git stash drop stash@{1} &&
597 test_must_fail git stash pop stash@{1} &&
598 test_must_fail git stash apply stash@{1} &&
599 test_must_fail git stash show stash@{1} &&
600 test_must_fail git stash branch tmp stash@{1} &&
601 git stash drop
604 test_expect_success 'stash branch should not drop the stash if the branch exists' '
605 git stash clear &&
606 echo foo >file &&
607 git add file &&
608 git commit -m initial &&
609 echo bar >file &&
610 git stash &&
611 test_must_fail git stash branch master stash@{0} &&
612 git rev-parse stash@{0} --
615 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
616 git stash clear &&
617 echo 1 >subdir/subfile1 &&
618 echo 2 >subdir/subfile2 &&
619 git add subdir/subfile1 &&
620 git commit -m subdir &&
622 cd subdir &&
623 echo x >subfile1 &&
624 echo x >../file &&
625 git status >../expect &&
626 git stash &&
627 sane_unset GIT_MERGE_VERBOSITY &&
628 git stash apply
630 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
631 test_i18ncmp expect actual
634 cat > expect << EOF
635 diff --git a/HEAD b/HEAD
636 new file mode 100644
637 index 0000000..fe0cbee
638 --- /dev/null
639 +++ b/HEAD
640 @@ -0,0 +1 @@
641 +file-not-a-ref
644 test_expect_success 'stash where working directory contains "HEAD" file' '
645 git stash clear &&
646 git reset --hard &&
647 echo file-not-a-ref > HEAD &&
648 git add HEAD &&
649 test_tick &&
650 git stash &&
651 git diff-files --quiet &&
652 git diff-index --cached --quiet HEAD &&
653 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
654 git diff stash^..stash > output &&
655 test_cmp output expect
658 test_expect_success 'store called with invalid commit' '
659 test_must_fail git stash store foo
662 test_expect_success 'store updates stash ref and reflog' '
663 git stash clear &&
664 git reset --hard &&
665 echo quux >bazzy &&
666 git add bazzy &&
667 STASH_ID=$(git stash create) &&
668 git reset --hard &&
669 ! grep quux bazzy &&
670 git stash store -m quuxery $STASH_ID &&
671 test $(cat .git/refs/stash) = $STASH_ID &&
672 grep $STASH_ID .git/logs/refs/stash &&
673 git stash pop &&
674 grep quux bazzy
677 test_expect_success 'handle stash specification with spaces' '
678 git stash clear &&
679 echo pig >file &&
680 git stash &&
681 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
682 test_tick &&
683 echo cow >file &&
684 git stash &&
685 git stash apply "stash@{$stamp}" &&
686 grep pig file
689 test_expect_success 'setup stash with index and worktree changes' '
690 git stash clear &&
691 git reset --hard &&
692 echo index >file &&
693 git add file &&
694 echo working >file &&
695 git stash
698 test_expect_success 'stash list implies --first-parent -m' '
699 cat >expect <<-\EOF &&
700 stash@{0}: WIP on master: b27a2bc subdir
702 diff --git a/file b/file
703 index 257cc56..d26b33d 100644
704 --- a/file
705 +++ b/file
706 @@ -1 +1 @@
707 -foo
708 +working
710 git stash list -p >actual &&
711 test_cmp expect actual
714 test_expect_success 'stash list --cc shows combined diff' '
715 cat >expect <<-\EOF &&
716 stash@{0}: WIP on master: b27a2bc subdir
718 diff --cc file
719 index 257cc56,9015a7a..d26b33d
720 --- a/file
721 +++ b/file
722 @@@ -1,1 -1,1 +1,1 @@@
723 - foo
724 -index
725 ++working
727 git stash list -p --cc >actual &&
728 test_cmp expect actual
731 test_done