Merge branch 'master' of git://repo.or.cz/alt-git
[git/mingw/j6t.git] / t / t3903-stash.sh
blob6613e0dc4c8a179724cd4a003eb6e605ce9745a3
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 requires a clean index' '
49 test_when_finished "git reset --hard" &&
50 echo changed >other-file &&
51 git add other-file &&
52 test_must_fail git stash apply
55 test_expect_success 'apply does not need clean working directory' '
56 echo 4 >other-file &&
57 git stash apply &&
58 echo 3 >expect &&
59 test_cmp expect file
62 test_expect_success 'apply does not clobber working directory changes' '
63 git reset --hard &&
64 echo 4 >file &&
65 test_must_fail git stash apply &&
66 echo 4 >expect &&
67 test_cmp expect file
70 test_expect_success 'apply stashed changes' '
71 git reset --hard &&
72 echo 5 >other-file &&
73 git add other-file &&
74 test_tick &&
75 git commit -m other-file &&
76 git stash apply &&
77 test 3 = $(cat file) &&
78 test 1 = $(git show :file) &&
79 test 1 = $(git show HEAD:file)
82 test_expect_success 'apply stashed changes (including index)' '
83 git reset --hard HEAD^ &&
84 echo 6 > other-file &&
85 git add other-file &&
86 test_tick &&
87 git commit -m other-file &&
88 git stash apply --index &&
89 test 3 = $(cat file) &&
90 test 2 = $(git show :file) &&
91 test 1 = $(git show HEAD:file)
94 test_expect_success 'unstashing in a subdirectory' '
95 git reset --hard HEAD &&
96 mkdir subdir &&
98 cd subdir &&
99 git stash apply
103 test_expect_success 'stash drop complains of extra options' '
104 test_must_fail git stash drop --foo
107 test_expect_success 'drop top stash' '
108 git reset --hard &&
109 git stash list > stashlist1 &&
110 echo 7 > file &&
111 git stash &&
112 git stash drop &&
113 git stash list > stashlist2 &&
114 test_cmp stashlist1 stashlist2 &&
115 git stash apply &&
116 test 3 = $(cat file) &&
117 test 1 = $(git show :file) &&
118 test 1 = $(git show HEAD:file)
121 test_expect_success 'drop middle stash' '
122 git reset --hard &&
123 echo 8 > file &&
124 git stash &&
125 sleep 1 &&
126 echo 9 > file &&
127 git stash &&
128 git stash drop stash@{1} &&
129 test 2 = $(git stash list | wc -l) &&
130 git stash apply &&
131 test 9 = $(cat file) &&
132 test 1 = $(git show :file) &&
133 test 1 = $(git show HEAD:file) &&
134 git reset --hard &&
135 git stash drop &&
136 git stash apply &&
137 test 3 = $(cat file) &&
138 test 1 = $(git show :file) &&
139 test 1 = $(git show HEAD:file)
142 test_expect_success 'stash pop' '
143 git reset --hard &&
144 git stash pop &&
145 test 3 = $(cat file) &&
146 test 1 = $(git show :file) &&
147 test 1 = $(git show HEAD:file) &&
148 test 0 = $(git stash list | wc -l)
151 cat > expect << EOF
152 diff --git a/file2 b/file2
153 new file mode 100644
154 index 0000000..1fe912c
155 --- /dev/null
156 +++ b/file2
157 @@ -0,0 +1 @@
158 +bar2
161 cat > expect1 << EOF
162 diff --git a/file b/file
163 index 257cc56..5716ca5 100644
164 --- a/file
165 +++ b/file
166 @@ -1 +1 @@
167 -foo
168 +bar
171 cat > expect2 << EOF
172 diff --git a/file b/file
173 index 7601807..5716ca5 100644
174 --- a/file
175 +++ b/file
176 @@ -1 +1 @@
177 -baz
178 +bar
179 diff --git a/file2 b/file2
180 new file mode 100644
181 index 0000000..1fe912c
182 --- /dev/null
183 +++ b/file2
184 @@ -0,0 +1 @@
185 +bar2
188 test_expect_success 'stash branch' '
189 echo foo > file &&
190 git commit file -m first &&
191 echo bar > file &&
192 echo bar2 > file2 &&
193 git add file2 &&
194 git stash &&
195 echo baz > file &&
196 git commit file -m second &&
197 git stash branch stashbranch &&
198 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
199 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
200 git diff --cached > output &&
201 test_cmp output expect &&
202 git diff > output &&
203 test_cmp output expect1 &&
204 git add file &&
205 git commit -m alternate\ second &&
206 git diff master..stashbranch > output &&
207 test_cmp output expect2 &&
208 test 0 = $(git stash list | wc -l)
211 test_expect_success 'apply -q is quiet' '
212 echo foo > file &&
213 git stash &&
214 git stash apply -q > output.out 2>&1 &&
215 test_must_be_empty output.out
218 test_expect_success 'save -q is quiet' '
219 git stash save --quiet > output.out 2>&1 &&
220 test_must_be_empty output.out
223 test_expect_success 'pop -q is quiet' '
224 git stash pop -q > output.out 2>&1 &&
225 test_must_be_empty output.out
228 test_expect_success 'pop -q --index works and is quiet' '
229 echo foo > file &&
230 git add file &&
231 git stash save --quiet &&
232 git stash pop -q --index > output.out 2>&1 &&
233 test foo = "$(git show :file)" &&
234 test_must_be_empty output.out
237 test_expect_success 'drop -q is quiet' '
238 git stash &&
239 git stash drop -q > output.out 2>&1 &&
240 test_must_be_empty output.out
243 test_expect_success 'stash -k' '
244 echo bar3 > file &&
245 echo bar4 > file2 &&
246 git add file2 &&
247 git stash -k &&
248 test bar,bar4 = $(cat file),$(cat file2)
251 test_expect_success 'stash --no-keep-index' '
252 echo bar33 > file &&
253 echo bar44 > file2 &&
254 git add file2 &&
255 git stash --no-keep-index &&
256 test bar,bar2 = $(cat file),$(cat file2)
259 test_expect_success 'stash --invalid-option' '
260 echo bar5 > file &&
261 echo bar6 > file2 &&
262 git add file2 &&
263 test_must_fail git stash --invalid-option &&
264 test_must_fail git stash save --invalid-option &&
265 test bar5,bar6 = $(cat file),$(cat file2) &&
266 git stash -- -message-starting-with-dash &&
267 test bar,bar2 = $(cat file),$(cat file2)
270 test_expect_success 'stash an added file' '
271 git reset --hard &&
272 echo new >file3 &&
273 git add file3 &&
274 git stash save "added file" &&
275 ! test -r file3 &&
276 git stash apply &&
277 test new = "$(cat file3)"
280 test_expect_success 'stash rm then recreate' '
281 git reset --hard &&
282 git rm file &&
283 echo bar7 >file &&
284 git stash save "rm then recreate" &&
285 test bar = "$(cat file)" &&
286 git stash apply &&
287 test bar7 = "$(cat file)"
290 test_expect_success 'stash rm and ignore' '
291 git reset --hard &&
292 git rm file &&
293 echo file >.gitignore &&
294 git stash save "rm and ignore" &&
295 test bar = "$(cat file)" &&
296 test file = "$(cat .gitignore)" &&
297 git stash apply &&
298 ! test -r file &&
299 test file = "$(cat .gitignore)"
302 test_expect_success 'stash rm and ignore (stage .gitignore)' '
303 git reset --hard &&
304 git rm file &&
305 echo file >.gitignore &&
306 git add .gitignore &&
307 git stash save "rm and ignore (stage .gitignore)" &&
308 test bar = "$(cat file)" &&
309 ! test -r .gitignore &&
310 git stash apply &&
311 ! test -r file &&
312 test file = "$(cat .gitignore)"
315 test_expect_success SYMLINKS 'stash file to symlink' '
316 git reset --hard &&
317 rm file &&
318 ln -s file2 file &&
319 git stash save "file to symlink" &&
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 (stage rm)' '
327 git reset --hard &&
328 git rm file &&
329 ln -s file2 file &&
330 git stash save "file to symlink (stage rm)" &&
331 test -f file &&
332 test bar = "$(cat file)" &&
333 git stash apply &&
334 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
337 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
338 git reset --hard &&
339 rm file &&
340 ln -s file2 file &&
341 git add file &&
342 git stash save "file to symlink (full stage)" &&
343 test -f file &&
344 test bar = "$(cat file)" &&
345 git stash apply &&
346 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
349 # This test creates a commit with a symlink used for the following tests
351 test_expect_success 'stash symlink to file' '
352 git reset --hard &&
353 test_ln_s_add file filelink &&
354 git commit -m "Add symlink" &&
355 rm filelink &&
356 cp file filelink &&
357 git stash save "symlink to file"
360 test_expect_success SYMLINKS 'this must have re-created the symlink' '
361 test -h filelink &&
362 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
365 test_expect_success 'unstash must re-create the file' '
366 git stash apply &&
367 ! test -h filelink &&
368 test bar = "$(cat file)"
371 test_expect_success 'stash symlink to file (stage rm)' '
372 git reset --hard &&
373 git rm filelink &&
374 cp file filelink &&
375 git stash save "symlink to file (stage rm)"
378 test_expect_success SYMLINKS 'this must have re-created the symlink' '
379 test -h filelink &&
380 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
383 test_expect_success 'unstash must re-create the file' '
384 git stash apply &&
385 ! test -h filelink &&
386 test bar = "$(cat file)"
389 test_expect_success 'stash symlink to file (full stage)' '
390 git reset --hard &&
391 rm filelink &&
392 cp file filelink &&
393 git add filelink &&
394 git stash save "symlink to file (full stage)"
397 test_expect_success SYMLINKS 'this must have re-created the symlink' '
398 test -h filelink &&
399 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
402 test_expect_success 'unstash must re-create the file' '
403 git stash apply &&
404 ! test -h filelink &&
405 test bar = "$(cat file)"
408 test_expect_failure 'stash directory to file' '
409 git reset --hard &&
410 mkdir dir &&
411 echo foo >dir/file &&
412 git add dir/file &&
413 git commit -m "Add file in dir" &&
414 rm -fr dir &&
415 echo bar >dir &&
416 git stash save "directory to file" &&
417 test -d dir &&
418 test foo = "$(cat dir/file)" &&
419 test_must_fail git stash apply &&
420 test bar = "$(cat dir)" &&
421 git reset --soft HEAD^
424 test_expect_failure 'stash file to directory' '
425 git reset --hard &&
426 rm file &&
427 mkdir file &&
428 echo foo >file/file &&
429 git stash save "file to directory" &&
430 test -f file &&
431 test bar = "$(cat file)" &&
432 git stash apply &&
433 test -f file/file &&
434 test foo = "$(cat file/file)"
437 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
438 git stash clear &&
439 test_when_finished "git reset --hard HEAD" &&
440 git reset --hard &&
441 echo foo >> file &&
442 STASH_ID=$(git stash create) &&
443 git reset --hard &&
444 git stash branch stash-branch ${STASH_ID} &&
445 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
446 test $(git ls-files --modified | wc -l) -eq 1
449 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
450 git stash clear &&
451 test_when_finished "git reset --hard HEAD" &&
452 git reset --hard &&
453 echo foo >> file &&
454 git stash &&
455 test_when_finished "git stash drop" &&
456 echo bar >> file &&
457 STASH_ID=$(git stash create) &&
458 git reset --hard &&
459 git stash branch stash-branch ${STASH_ID} &&
460 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
461 test $(git ls-files --modified | wc -l) -eq 1
464 test_expect_success 'stash show format defaults to --stat' '
465 git stash clear &&
466 test_when_finished "git reset --hard HEAD" &&
467 git reset --hard &&
468 echo foo >> file &&
469 git stash &&
470 test_when_finished "git stash drop" &&
471 echo bar >> file &&
472 STASH_ID=$(git stash create) &&
473 git reset --hard &&
474 cat >expected <<-EOF &&
475 file | 1 +
476 1 file changed, 1 insertion(+)
478 git stash show ${STASH_ID} >actual &&
479 test_i18ncmp expected actual
482 test_expect_success 'stash show - stashes on stack, stash-like argument' '
483 git stash clear &&
484 test_when_finished "git reset --hard HEAD" &&
485 git reset --hard &&
486 echo foo >> file &&
487 git stash &&
488 test_when_finished "git stash drop" &&
489 echo bar >> file &&
490 STASH_ID=$(git stash create) &&
491 git reset --hard &&
492 echo "1 0 file" >expected &&
493 git stash show --numstat ${STASH_ID} >actual &&
494 test_cmp expected actual
497 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
498 git stash clear &&
499 test_when_finished "git reset --hard HEAD" &&
500 git reset --hard &&
501 echo foo >> file &&
502 git stash &&
503 test_when_finished "git stash drop" &&
504 echo bar >> file &&
505 STASH_ID=$(git stash create) &&
506 git reset --hard &&
507 cat >expected <<-EOF &&
508 diff --git a/file b/file
509 index 7601807..935fbd3 100644
510 --- a/file
511 +++ b/file
512 @@ -1 +1,2 @@
514 +bar
516 git stash show -p ${STASH_ID} >actual &&
517 test_cmp expected actual
520 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
521 git stash clear &&
522 test_when_finished "git reset --hard HEAD" &&
523 git reset --hard &&
524 echo foo >> file &&
525 STASH_ID=$(git stash create) &&
526 git reset --hard &&
527 echo "1 0 file" >expected &&
528 git stash show --numstat ${STASH_ID} >actual &&
529 test_cmp expected actual
532 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
533 git stash clear &&
534 test_when_finished "git reset --hard HEAD" &&
535 git reset --hard &&
536 echo foo >> file &&
537 STASH_ID=$(git stash create) &&
538 git reset --hard &&
539 cat >expected <<-EOF &&
540 diff --git a/file b/file
541 index 7601807..71b52c4 100644
542 --- a/file
543 +++ b/file
544 @@ -1 +1,2 @@
546 +foo
548 git stash show -p ${STASH_ID} >actual &&
549 test_cmp expected actual
552 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
553 git stash clear &&
554 test_when_finished "git reset --hard HEAD && git stash clear" &&
555 git reset --hard &&
556 echo foo > file &&
557 git stash &&
558 echo bar > file &&
559 git stash &&
560 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
561 git stash pop &&
562 test bar = "$(cat file)" &&
563 git reset --hard HEAD
566 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
567 git stash clear &&
568 test_when_finished "git reset --hard HEAD && git stash clear" &&
569 git reset --hard &&
570 echo foo > file &&
571 git stash &&
572 echo bar > file &&
573 git stash &&
574 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
575 git stash pop &&
576 test bar = "$(cat file)" &&
577 git reset --hard HEAD
580 test_expect_success 'ref with non-existent reflog' '
581 git stash clear &&
582 echo bar5 > file &&
583 echo bar6 > file2 &&
584 git add file2 &&
585 git stash &&
586 test_must_fail git rev-parse --quiet --verify does-not-exist &&
587 test_must_fail git stash drop does-not-exist &&
588 test_must_fail git stash drop does-not-exist@{0} &&
589 test_must_fail git stash pop does-not-exist &&
590 test_must_fail git stash pop does-not-exist@{0} &&
591 test_must_fail git stash apply does-not-exist &&
592 test_must_fail git stash apply does-not-exist@{0} &&
593 test_must_fail git stash show does-not-exist &&
594 test_must_fail git stash show does-not-exist@{0} &&
595 test_must_fail git stash branch tmp does-not-exist &&
596 test_must_fail git stash branch tmp does-not-exist@{0} &&
597 git stash drop
600 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
601 git stash clear &&
602 test_must_fail git stash drop stash@{0} &&
603 echo bar5 > file &&
604 echo bar6 > file2 &&
605 git add file2 &&
606 git stash &&
607 test_must_fail git stash drop stash@{1} &&
608 test_must_fail git stash pop stash@{1} &&
609 test_must_fail git stash apply stash@{1} &&
610 test_must_fail git stash show stash@{1} &&
611 test_must_fail git stash branch tmp stash@{1} &&
612 git stash drop
615 test_expect_success 'stash branch should not drop the stash if the branch exists' '
616 git stash clear &&
617 echo foo >file &&
618 git add file &&
619 git commit -m initial &&
620 echo bar >file &&
621 git stash &&
622 test_must_fail git stash branch master stash@{0} &&
623 git rev-parse stash@{0} --
626 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
627 git stash clear &&
628 echo 1 >subdir/subfile1 &&
629 echo 2 >subdir/subfile2 &&
630 git add subdir/subfile1 &&
631 git commit -m subdir &&
633 cd subdir &&
634 echo x >subfile1 &&
635 echo x >../file &&
636 git status >../expect &&
637 git stash &&
638 sane_unset GIT_MERGE_VERBOSITY &&
639 git stash apply
641 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
642 test_i18ncmp expect actual
645 cat > expect << EOF
646 diff --git a/HEAD b/HEAD
647 new file mode 100644
648 index 0000000..fe0cbee
649 --- /dev/null
650 +++ b/HEAD
651 @@ -0,0 +1 @@
652 +file-not-a-ref
655 test_expect_success 'stash where working directory contains "HEAD" file' '
656 git stash clear &&
657 git reset --hard &&
658 echo file-not-a-ref > HEAD &&
659 git add HEAD &&
660 test_tick &&
661 git stash &&
662 git diff-files --quiet &&
663 git diff-index --cached --quiet HEAD &&
664 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
665 git diff stash^..stash > output &&
666 test_cmp output expect
669 test_expect_success 'store called with invalid commit' '
670 test_must_fail git stash store foo
673 test_expect_success 'store updates stash ref and reflog' '
674 git stash clear &&
675 git reset --hard &&
676 echo quux >bazzy &&
677 git add bazzy &&
678 STASH_ID=$(git stash create) &&
679 git reset --hard &&
680 ! grep quux bazzy &&
681 git stash store -m quuxery $STASH_ID &&
682 test $(cat .git/refs/stash) = $STASH_ID &&
683 grep $STASH_ID .git/logs/refs/stash &&
684 git stash pop &&
685 grep quux bazzy
688 test_expect_success 'handle stash specification with spaces' '
689 git stash clear &&
690 echo pig >file &&
691 git stash &&
692 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
693 test_tick &&
694 echo cow >file &&
695 git stash &&
696 git stash apply "stash@{$stamp}" &&
697 grep pig file
700 test_expect_success 'setup stash with index and worktree changes' '
701 git stash clear &&
702 git reset --hard &&
703 echo index >file &&
704 git add file &&
705 echo working >file &&
706 git stash
709 test_expect_success 'stash list implies --first-parent -m' '
710 cat >expect <<-EOF &&
711 stash@{0}
713 diff --git a/file b/file
714 index 257cc56..d26b33d 100644
715 --- a/file
716 +++ b/file
717 @@ -1 +1 @@
718 -foo
719 +working
721 git stash list --format=%gd -p >actual &&
722 test_cmp expect actual
725 test_expect_success 'stash list --cc shows combined diff' '
726 cat >expect <<-\EOF &&
727 stash@{0}
729 diff --cc file
730 index 257cc56,9015a7a..d26b33d
731 --- a/file
732 +++ b/file
733 @@@ -1,1 -1,1 +1,1 @@@
734 - foo
735 -index
736 ++working
738 git stash list --format=%gd -p --cc >actual &&
739 test_cmp expect actual
742 test_done