Skip tests that fail due to incomplete implementations, missing tools...
[git/mingw/j6t.git] / t / t3903-stash.sh
blob65c2d496d4fe484cabf58313af987c1415e5518a
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 echo unrelated >other-file &&
14 git add other-file &&
15 test_tick &&
16 git commit -m initial &&
17 echo 2 > file &&
18 git add file &&
19 echo 3 > file &&
20 test_tick &&
21 git stash &&
22 git diff-files --quiet &&
23 git diff-index --cached --quiet HEAD
26 cat > expect << EOF
27 diff --git a/file b/file
28 index 0cfbf08..00750ed 100644
29 --- a/file
30 +++ b/file
31 @@ -1 +1 @@
34 EOF
36 test_expect_success 'parents of stash' '
37 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
38 git diff stash^2..stash > output &&
39 test_cmp output expect
42 test_expect_success 'applying bogus stash does nothing' '
43 test_must_fail git stash apply stash@{1} &&
44 echo 1 >expect &&
45 test_cmp expect file
48 test_expect_success 'apply does not need clean working directory' '
49 echo 4 >other-file &&
50 git stash apply &&
51 echo 3 >expect &&
52 test_cmp expect file
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 'stash drop complains of extra options' '
97 test_must_fail git stash drop --foo
100 test_expect_success 'drop top stash' '
101 git reset --hard &&
102 git stash list > stashlist1 &&
103 echo 7 > file &&
104 git stash &&
105 git stash drop &&
106 git stash list > stashlist2 &&
107 test_cmp stashlist1 stashlist2 &&
108 git stash apply &&
109 test 3 = $(cat file) &&
110 test 1 = $(git show :file) &&
111 test 1 = $(git show HEAD:file)
114 test_expect_success 'drop middle stash' '
115 git reset --hard &&
116 echo 8 > file &&
117 git stash &&
118 sleep 1 &&
119 echo 9 > file &&
120 git stash &&
121 git stash drop stash@{1} &&
122 test 2 = $(git stash list | wc -l) &&
123 git stash apply &&
124 test 9 = $(cat file) &&
125 test 1 = $(git show :file) &&
126 test 1 = $(git show HEAD:file) &&
127 git reset --hard &&
128 git stash drop &&
129 git stash apply &&
130 test 3 = $(cat file) &&
131 test 1 = $(git show :file) &&
132 test 1 = $(git show HEAD:file)
135 test_expect_success 'stash pop' '
136 git reset --hard &&
137 git stash pop &&
138 test 3 = $(cat file) &&
139 test 1 = $(git show :file) &&
140 test 1 = $(git show HEAD:file) &&
141 test 0 = $(git stash list | wc -l)
144 cat > expect << EOF
145 diff --git a/file2 b/file2
146 new file mode 100644
147 index 0000000..1fe912c
148 --- /dev/null
149 +++ b/file2
150 @@ -0,0 +1 @@
151 +bar2
154 cat > expect1 << EOF
155 diff --git a/file b/file
156 index 257cc56..5716ca5 100644
157 --- a/file
158 +++ b/file
159 @@ -1 +1 @@
160 -foo
161 +bar
164 cat > expect2 << EOF
165 diff --git a/file b/file
166 index 7601807..5716ca5 100644
167 --- a/file
168 +++ b/file
169 @@ -1 +1 @@
170 -baz
171 +bar
172 diff --git a/file2 b/file2
173 new file mode 100644
174 index 0000000..1fe912c
175 --- /dev/null
176 +++ b/file2
177 @@ -0,0 +1 @@
178 +bar2
181 test_expect_success 'stash branch' '
182 echo foo > file &&
183 git commit file -m first &&
184 echo bar > file &&
185 echo bar2 > file2 &&
186 git add file2 &&
187 git stash &&
188 echo baz > file &&
189 git commit file -m second &&
190 git stash branch stashbranch &&
191 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
192 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
193 git diff --cached > output &&
194 test_cmp output expect &&
195 git diff > output &&
196 test_cmp output expect1 &&
197 git add file &&
198 git commit -m alternate\ second &&
199 git diff master..stashbranch > output &&
200 test_cmp output expect2 &&
201 test 0 = $(git stash list | wc -l)
204 test_expect_success 'apply -q is quiet' '
205 echo foo > file &&
206 git stash &&
207 git stash apply -q > output.out 2>&1 &&
208 test_must_be_empty output.out
211 test_expect_success 'save -q is quiet' '
212 git stash save --quiet > output.out 2>&1 &&
213 test_must_be_empty output.out
216 test_expect_success 'pop -q is quiet' '
217 git stash pop -q > output.out 2>&1 &&
218 test_must_be_empty output.out
221 test_expect_success 'pop -q --index works and is quiet' '
222 echo foo > file &&
223 git add file &&
224 git stash save --quiet &&
225 git stash pop -q --index > output.out 2>&1 &&
226 test foo = "$(git show :file)" &&
227 test_must_be_empty output.out
230 test_expect_success 'drop -q is quiet' '
231 git stash &&
232 git stash drop -q > output.out 2>&1 &&
233 test_must_be_empty output.out
236 test_expect_success 'stash -k' '
237 echo bar3 > file &&
238 echo bar4 > file2 &&
239 git add file2 &&
240 git stash -k &&
241 test bar,bar4 = $(cat file),$(cat file2)
244 test_expect_success 'stash --no-keep-index' '
245 echo bar33 > file &&
246 echo bar44 > file2 &&
247 git add file2 &&
248 git stash --no-keep-index &&
249 test bar,bar2 = $(cat file),$(cat file2)
252 test_expect_success 'stash --invalid-option' '
253 echo bar5 > file &&
254 echo bar6 > file2 &&
255 git add file2 &&
256 test_must_fail git stash --invalid-option &&
257 test_must_fail git stash save --invalid-option &&
258 test bar5,bar6 = $(cat file),$(cat file2) &&
259 git stash -- -message-starting-with-dash &&
260 test bar,bar2 = $(cat file),$(cat file2)
263 test_expect_success 'stash an added file' '
264 git reset --hard &&
265 echo new >file3 &&
266 git add file3 &&
267 git stash save "added file" &&
268 ! test -r file3 &&
269 git stash apply &&
270 test new = "$(cat file3)"
273 test_expect_success 'stash rm then recreate' '
274 git reset --hard &&
275 git rm file &&
276 echo bar7 >file &&
277 git stash save "rm then recreate" &&
278 test bar = "$(cat file)" &&
279 git stash apply &&
280 test bar7 = "$(cat file)"
283 test_expect_success 'stash rm and ignore' '
284 git reset --hard &&
285 git rm file &&
286 echo file >.gitignore &&
287 git stash save "rm and ignore" &&
288 test bar = "$(cat file)" &&
289 test file = "$(cat .gitignore)" &&
290 git stash apply &&
291 ! test -r file &&
292 test file = "$(cat .gitignore)"
295 test_expect_success 'stash rm and ignore (stage .gitignore)' '
296 git reset --hard &&
297 git rm file &&
298 echo file >.gitignore &&
299 git add .gitignore &&
300 git stash save "rm and ignore (stage .gitignore)" &&
301 test bar = "$(cat file)" &&
302 ! test -r .gitignore &&
303 git stash apply &&
304 ! test -r file &&
305 test file = "$(cat .gitignore)"
308 test_expect_success SYMLINKS 'stash file to symlink' '
309 git reset --hard &&
310 rm file &&
311 ln -s file2 file &&
312 git stash save "file to symlink" &&
313 test -f file &&
314 test bar = "$(cat file)" &&
315 git stash apply &&
316 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
319 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
320 git reset --hard &&
321 git rm file &&
322 ln -s file2 file &&
323 git stash save "file to symlink (stage rm)" &&
324 test -f file &&
325 test bar = "$(cat file)" &&
326 git stash apply &&
327 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
330 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
331 git reset --hard &&
332 rm file &&
333 ln -s file2 file &&
334 git add file &&
335 git stash save "file to symlink (full stage)" &&
336 test -f file &&
337 test bar = "$(cat file)" &&
338 git stash apply &&
339 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
342 # This test creates a commit with a symlink used for the following tests
344 test_expect_success 'stash symlink to file' '
345 git reset --hard &&
346 test_ln_s_add file filelink &&
347 git commit -m "Add symlink" &&
348 rm filelink &&
349 cp file filelink &&
350 git stash save "symlink to file"
353 test_expect_success SYMLINKS 'this must have re-created the symlink' '
354 test -h filelink &&
355 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
358 test_expect_success 'unstash must re-create the file' '
359 git stash apply &&
360 ! test -h filelink &&
361 test bar = "$(cat file)"
364 test_expect_success 'stash symlink to file (stage rm)' '
365 git reset --hard &&
366 git rm filelink &&
367 cp file filelink &&
368 git stash save "symlink to file (stage rm)"
371 test_expect_success SYMLINKS 'this must have re-created the symlink' '
372 test -h filelink &&
373 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
376 test_expect_success 'unstash must re-create the file' '
377 git stash apply &&
378 ! test -h filelink &&
379 test bar = "$(cat file)"
382 test_expect_success 'stash symlink to file (full stage)' '
383 git reset --hard &&
384 rm filelink &&
385 cp file filelink &&
386 git add filelink &&
387 git stash save "symlink to file (full stage)"
390 test_expect_success SYMLINKS 'this must have re-created the symlink' '
391 test -h filelink &&
392 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
395 test_expect_success 'unstash must re-create the file' '
396 git stash apply &&
397 ! test -h filelink &&
398 test bar = "$(cat file)"
401 test_expect_failure 'stash directory to file' '
402 git reset --hard &&
403 mkdir dir &&
404 echo foo >dir/file &&
405 git add dir/file &&
406 git commit -m "Add file in dir" &&
407 rm -fr dir &&
408 echo bar >dir &&
409 git stash save "directory to file" &&
410 test -d dir &&
411 test foo = "$(cat dir/file)" &&
412 test_must_fail git stash apply &&
413 test bar = "$(cat dir)" &&
414 git reset --soft HEAD^
417 test_expect_failure 'stash file to directory' '
418 git reset --hard &&
419 rm file &&
420 mkdir file &&
421 echo foo >file/file &&
422 git stash save "file to directory" &&
423 test -f file &&
424 test bar = "$(cat file)" &&
425 git stash apply &&
426 test -f file/file &&
427 test foo = "$(cat file/file)"
430 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
431 git stash clear &&
432 test_when_finished "git reset --hard HEAD" &&
433 git reset --hard &&
434 echo foo >> file &&
435 STASH_ID=$(git stash create) &&
436 git reset --hard &&
437 git stash branch stash-branch ${STASH_ID} &&
438 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
439 test $(git ls-files --modified | wc -l) -eq 1
442 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
443 git stash clear &&
444 test_when_finished "git reset --hard HEAD" &&
445 git reset --hard &&
446 echo foo >> file &&
447 git stash &&
448 test_when_finished "git stash drop" &&
449 echo bar >> file &&
450 STASH_ID=$(git stash create) &&
451 git reset --hard &&
452 git stash branch stash-branch ${STASH_ID} &&
453 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
454 test $(git ls-files --modified | wc -l) -eq 1
457 test_expect_success 'stash show format defaults to --stat' '
458 git stash clear &&
459 test_when_finished "git reset --hard HEAD" &&
460 git reset --hard &&
461 echo foo >> file &&
462 git stash &&
463 test_when_finished "git stash drop" &&
464 echo bar >> file &&
465 STASH_ID=$(git stash create) &&
466 git reset --hard &&
467 cat >expected <<-EOF &&
468 file | 1 +
469 1 file changed, 1 insertion(+)
471 git stash show ${STASH_ID} >actual &&
472 test_i18ncmp expected actual
475 test_expect_success 'stash show - stashes on stack, stash-like argument' '
476 git stash clear &&
477 test_when_finished "git reset --hard HEAD" &&
478 git reset --hard &&
479 echo foo >> file &&
480 git stash &&
481 test_when_finished "git stash drop" &&
482 echo bar >> file &&
483 STASH_ID=$(git stash create) &&
484 git reset --hard &&
485 echo "1 0 file" >expected &&
486 git stash show --numstat ${STASH_ID} >actual &&
487 test_cmp expected actual
490 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
491 git stash clear &&
492 test_when_finished "git reset --hard HEAD" &&
493 git reset --hard &&
494 echo foo >> file &&
495 git stash &&
496 test_when_finished "git stash drop" &&
497 echo bar >> file &&
498 STASH_ID=$(git stash create) &&
499 git reset --hard &&
500 cat >expected <<-EOF &&
501 diff --git a/file b/file
502 index 7601807..935fbd3 100644
503 --- a/file
504 +++ b/file
505 @@ -1 +1,2 @@
507 +bar
509 git stash show -p ${STASH_ID} >actual &&
510 test_cmp expected actual
513 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
514 git stash clear &&
515 test_when_finished "git reset --hard HEAD" &&
516 git reset --hard &&
517 echo foo >> file &&
518 STASH_ID=$(git stash create) &&
519 git reset --hard &&
520 echo "1 0 file" >expected &&
521 git stash show --numstat ${STASH_ID} >actual &&
522 test_cmp expected actual
525 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
526 git stash clear &&
527 test_when_finished "git reset --hard HEAD" &&
528 git reset --hard &&
529 echo foo >> file &&
530 STASH_ID=$(git stash create) &&
531 git reset --hard &&
532 cat >expected <<-EOF &&
533 diff --git a/file b/file
534 index 7601807..71b52c4 100644
535 --- a/file
536 +++ b/file
537 @@ -1 +1,2 @@
539 +foo
541 git stash show -p ${STASH_ID} >actual &&
542 test_cmp expected actual
545 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
546 git stash clear &&
547 test_when_finished "git reset --hard HEAD && git stash clear" &&
548 git reset --hard &&
549 echo foo > file &&
550 git stash &&
551 echo bar > file &&
552 git stash &&
553 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
554 git stash pop &&
555 test bar = "$(cat file)" &&
556 git reset --hard HEAD
559 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
560 git stash clear &&
561 test_when_finished "git reset --hard HEAD && git stash clear" &&
562 git reset --hard &&
563 echo foo > file &&
564 git stash &&
565 echo bar > file &&
566 git stash &&
567 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
568 git stash pop &&
569 test bar = "$(cat file)" &&
570 git reset --hard HEAD
573 test_expect_success 'ref with non-existent reflog' '
574 git stash clear &&
575 echo bar5 > file &&
576 echo bar6 > file2 &&
577 git add file2 &&
578 git stash &&
579 test_must_fail git rev-parse --quiet --verify does-not-exist &&
580 test_must_fail git stash drop does-not-exist &&
581 test_must_fail git stash drop does-not-exist@{0} &&
582 test_must_fail git stash pop does-not-exist &&
583 test_must_fail git stash pop does-not-exist@{0} &&
584 test_must_fail git stash apply does-not-exist &&
585 test_must_fail git stash apply does-not-exist@{0} &&
586 test_must_fail git stash show does-not-exist &&
587 test_must_fail git stash show does-not-exist@{0} &&
588 test_must_fail git stash branch tmp does-not-exist &&
589 test_must_fail git stash branch tmp does-not-exist@{0} &&
590 git stash drop
593 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
594 git stash clear &&
595 test_must_fail git stash drop stash@{0} &&
596 echo bar5 > file &&
597 echo bar6 > file2 &&
598 git add file2 &&
599 git stash &&
600 test_must_fail git stash drop stash@{1} &&
601 test_must_fail git stash pop stash@{1} &&
602 test_must_fail git stash apply stash@{1} &&
603 test_must_fail git stash show stash@{1} &&
604 test_must_fail git stash branch tmp stash@{1} &&
605 git stash drop
608 test_expect_success 'stash branch should not drop the stash if the branch exists' '
609 git stash clear &&
610 echo foo >file &&
611 git add file &&
612 git commit -m initial &&
613 echo bar >file &&
614 git stash &&
615 test_must_fail git stash branch master stash@{0} &&
616 git rev-parse stash@{0} --
619 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
620 git stash clear &&
621 echo 1 >subdir/subfile1 &&
622 echo 2 >subdir/subfile2 &&
623 git add subdir/subfile1 &&
624 git commit -m subdir &&
626 cd subdir &&
627 echo x >subfile1 &&
628 echo x >../file &&
629 git status >../expect &&
630 git stash &&
631 sane_unset GIT_MERGE_VERBOSITY &&
632 git stash apply
634 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
635 test_i18ncmp expect actual
638 cat > expect << EOF
639 diff --git a/HEAD b/HEAD
640 new file mode 100644
641 index 0000000..fe0cbee
642 --- /dev/null
643 +++ b/HEAD
644 @@ -0,0 +1 @@
645 +file-not-a-ref
648 test_expect_success 'stash where working directory contains "HEAD" file' '
649 git stash clear &&
650 git reset --hard &&
651 echo file-not-a-ref > HEAD &&
652 git add HEAD &&
653 test_tick &&
654 git stash &&
655 git diff-files --quiet &&
656 git diff-index --cached --quiet HEAD &&
657 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
658 git diff stash^..stash > output &&
659 test_cmp output expect
662 test_expect_success 'store called with invalid commit' '
663 test_must_fail git stash store foo
666 test_expect_success 'store updates stash ref and reflog' '
667 git stash clear &&
668 git reset --hard &&
669 echo quux >bazzy &&
670 git add bazzy &&
671 STASH_ID=$(git stash create) &&
672 git reset --hard &&
673 ! grep quux bazzy &&
674 git stash store -m quuxery $STASH_ID &&
675 test $(cat .git/refs/stash) = $STASH_ID &&
676 git reflog --format=%H stash| grep $STASH_ID &&
677 git stash pop &&
678 grep quux bazzy
681 test_expect_success 'handle stash specification with spaces' '
682 git stash clear &&
683 echo pig >file &&
684 git stash &&
685 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
686 test_tick &&
687 echo cow >file &&
688 git stash &&
689 git stash apply "stash@{$stamp}" &&
690 grep pig file
693 test_expect_success 'setup stash with index and worktree changes' '
694 git stash clear &&
695 git reset --hard &&
696 echo index >file &&
697 git add file &&
698 echo working >file &&
699 git stash
702 test_expect_success 'stash list implies --first-parent -m' '
703 cat >expect <<-EOF &&
704 stash@{0}
706 diff --git a/file b/file
707 index 257cc56..d26b33d 100644
708 --- a/file
709 +++ b/file
710 @@ -1 +1 @@
711 -foo
712 +working
714 git stash list --format=%gd -p >actual &&
715 test_cmp expect actual
718 test_expect_success 'stash list --cc shows combined diff' '
719 cat >expect <<-\EOF &&
720 stash@{0}
722 diff --cc file
723 index 257cc56,9015a7a..d26b33d
724 --- a/file
725 +++ b/file
726 @@@ -1,1 -1,1 +1,1 @@@
727 - foo
728 -index
729 ++working
731 git stash list --format=%gd -p --cc >actual &&
732 test_cmp expect actual
735 test_done