rev-parse: fix several options when running in a subdirectory
[git/debian.git] / t / t3903-stash.sh
blob2de3e18ce68a94111c8dfcca858b8515ea8919c1
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 echo 9 > file &&
119 git stash &&
120 git stash drop stash@{1} &&
121 test 2 = $(git stash list | wc -l) &&
122 git stash apply &&
123 test 9 = $(cat file) &&
124 test 1 = $(git show :file) &&
125 test 1 = $(git show HEAD:file) &&
126 git reset --hard &&
127 git stash drop &&
128 git stash apply &&
129 test 3 = $(cat file) &&
130 test 1 = $(git show :file) &&
131 test 1 = $(git show HEAD:file)
134 test_expect_success 'drop middle stash by index' '
135 git reset --hard &&
136 echo 8 >file &&
137 git stash &&
138 echo 9 >file &&
139 git stash &&
140 git stash drop 1 &&
141 test 2 = $(git stash list | wc -l) &&
142 git stash apply &&
143 test 9 = $(cat file) &&
144 test 1 = $(git show :file) &&
145 test 1 = $(git show HEAD:file) &&
146 git reset --hard &&
147 git stash drop &&
148 git stash apply &&
149 test 3 = $(cat file) &&
150 test 1 = $(git show :file) &&
151 test 1 = $(git show HEAD:file)
154 test_expect_success 'stash pop' '
155 git reset --hard &&
156 git stash pop &&
157 test 3 = $(cat file) &&
158 test 1 = $(git show :file) &&
159 test 1 = $(git show HEAD:file) &&
160 test 0 = $(git stash list | wc -l)
163 cat > expect << EOF
164 diff --git a/file2 b/file2
165 new file mode 100644
166 index 0000000..1fe912c
167 --- /dev/null
168 +++ b/file2
169 @@ -0,0 +1 @@
170 +bar2
173 cat > expect1 << EOF
174 diff --git a/file b/file
175 index 257cc56..5716ca5 100644
176 --- a/file
177 +++ b/file
178 @@ -1 +1 @@
179 -foo
180 +bar
183 cat > expect2 << EOF
184 diff --git a/file b/file
185 index 7601807..5716ca5 100644
186 --- a/file
187 +++ b/file
188 @@ -1 +1 @@
189 -baz
190 +bar
191 diff --git a/file2 b/file2
192 new file mode 100644
193 index 0000000..1fe912c
194 --- /dev/null
195 +++ b/file2
196 @@ -0,0 +1 @@
197 +bar2
200 test_expect_success 'stash branch' '
201 echo foo > file &&
202 git commit file -m first &&
203 echo bar > file &&
204 echo bar2 > file2 &&
205 git add file2 &&
206 git stash &&
207 echo baz > file &&
208 git commit file -m second &&
209 git stash branch stashbranch &&
210 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
211 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
212 git diff --cached > output &&
213 test_cmp output expect &&
214 git diff > output &&
215 test_cmp output expect1 &&
216 git add file &&
217 git commit -m alternate\ second &&
218 git diff master..stashbranch > output &&
219 test_cmp output expect2 &&
220 test 0 = $(git stash list | wc -l)
223 test_expect_success 'apply -q is quiet' '
224 echo foo > file &&
225 git stash &&
226 git stash apply -q > output.out 2>&1 &&
227 test_must_be_empty output.out
230 test_expect_success 'save -q is quiet' '
231 git stash save --quiet > output.out 2>&1 &&
232 test_must_be_empty output.out
235 test_expect_success 'pop -q is quiet' '
236 git stash pop -q > output.out 2>&1 &&
237 test_must_be_empty output.out
240 test_expect_success 'pop -q --index works and is quiet' '
241 echo foo > file &&
242 git add file &&
243 git stash save --quiet &&
244 git stash pop -q --index > output.out 2>&1 &&
245 test foo = "$(git show :file)" &&
246 test_must_be_empty output.out
249 test_expect_success 'drop -q is quiet' '
250 git stash &&
251 git stash drop -q > output.out 2>&1 &&
252 test_must_be_empty output.out
255 test_expect_success 'stash -k' '
256 echo bar3 > file &&
257 echo bar4 > file2 &&
258 git add file2 &&
259 git stash -k &&
260 test bar,bar4 = $(cat file),$(cat file2)
263 test_expect_success 'stash --no-keep-index' '
264 echo bar33 > file &&
265 echo bar44 > file2 &&
266 git add file2 &&
267 git stash --no-keep-index &&
268 test bar,bar2 = $(cat file),$(cat file2)
271 test_expect_success 'stash --invalid-option' '
272 echo bar5 > file &&
273 echo bar6 > file2 &&
274 git add file2 &&
275 test_must_fail git stash --invalid-option &&
276 test_must_fail git stash save --invalid-option &&
277 test bar5,bar6 = $(cat file),$(cat file2) &&
278 git stash -- -message-starting-with-dash &&
279 test bar,bar2 = $(cat file),$(cat file2)
282 test_expect_success 'stash an added file' '
283 git reset --hard &&
284 echo new >file3 &&
285 git add file3 &&
286 git stash save "added file" &&
287 ! test -r file3 &&
288 git stash apply &&
289 test new = "$(cat file3)"
292 test_expect_success 'stash rm then recreate' '
293 git reset --hard &&
294 git rm file &&
295 echo bar7 >file &&
296 git stash save "rm then recreate" &&
297 test bar = "$(cat file)" &&
298 git stash apply &&
299 test bar7 = "$(cat file)"
302 test_expect_success 'stash rm and ignore' '
303 git reset --hard &&
304 git rm file &&
305 echo file >.gitignore &&
306 git stash save "rm and ignore" &&
307 test bar = "$(cat file)" &&
308 test file = "$(cat .gitignore)" &&
309 git stash apply &&
310 ! test -r file &&
311 test file = "$(cat .gitignore)"
314 test_expect_success 'stash rm and ignore (stage .gitignore)' '
315 git reset --hard &&
316 git rm file &&
317 echo file >.gitignore &&
318 git add .gitignore &&
319 git stash save "rm and ignore (stage .gitignore)" &&
320 test bar = "$(cat file)" &&
321 ! test -r .gitignore &&
322 git stash apply &&
323 ! test -r file &&
324 test file = "$(cat .gitignore)"
327 test_expect_success SYMLINKS 'stash file to symlink' '
328 git reset --hard &&
329 rm file &&
330 ln -s file2 file &&
331 git stash save "file to symlink" &&
332 test -f file &&
333 test bar = "$(cat file)" &&
334 git stash apply &&
335 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
338 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
339 git reset --hard &&
340 git rm file &&
341 ln -s file2 file &&
342 git stash save "file to symlink (stage rm)" &&
343 test -f file &&
344 test bar = "$(cat file)" &&
345 git stash apply &&
346 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
349 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
350 git reset --hard &&
351 rm file &&
352 ln -s file2 file &&
353 git add file &&
354 git stash save "file to symlink (full stage)" &&
355 test -f file &&
356 test bar = "$(cat file)" &&
357 git stash apply &&
358 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
361 # This test creates a commit with a symlink used for the following tests
363 test_expect_success 'stash symlink to file' '
364 git reset --hard &&
365 test_ln_s_add file filelink &&
366 git commit -m "Add symlink" &&
367 rm filelink &&
368 cp file filelink &&
369 git stash save "symlink to file"
372 test_expect_success SYMLINKS 'this must have re-created the symlink' '
373 test -h filelink &&
374 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
377 test_expect_success 'unstash must re-create the file' '
378 git stash apply &&
379 ! test -h filelink &&
380 test bar = "$(cat file)"
383 test_expect_success 'stash symlink to file (stage rm)' '
384 git reset --hard &&
385 git rm filelink &&
386 cp file filelink &&
387 git stash save "symlink to file (stage rm)"
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_success 'stash symlink to file (full stage)' '
402 git reset --hard &&
403 rm filelink &&
404 cp file filelink &&
405 git add filelink &&
406 git stash save "symlink to file (full stage)"
409 test_expect_success SYMLINKS 'this must have re-created the symlink' '
410 test -h filelink &&
411 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
414 test_expect_success 'unstash must re-create the file' '
415 git stash apply &&
416 ! test -h filelink &&
417 test bar = "$(cat file)"
420 test_expect_failure 'stash directory to file' '
421 git reset --hard &&
422 mkdir dir &&
423 echo foo >dir/file &&
424 git add dir/file &&
425 git commit -m "Add file in dir" &&
426 rm -fr dir &&
427 echo bar >dir &&
428 git stash save "directory to file" &&
429 test -d dir &&
430 test foo = "$(cat dir/file)" &&
431 test_must_fail git stash apply &&
432 test bar = "$(cat dir)" &&
433 git reset --soft HEAD^
436 test_expect_failure 'stash file to directory' '
437 git reset --hard &&
438 rm file &&
439 mkdir file &&
440 echo foo >file/file &&
441 git stash save "file to directory" &&
442 test -f file &&
443 test bar = "$(cat file)" &&
444 git stash apply &&
445 test -f file/file &&
446 test foo = "$(cat file/file)"
449 test_expect_success 'stash branch - no 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 STASH_ID=$(git stash create) &&
455 git reset --hard &&
456 git stash branch stash-branch ${STASH_ID} &&
457 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
458 test $(git ls-files --modified | wc -l) -eq 1
461 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
462 git stash clear &&
463 test_when_finished "git reset --hard HEAD" &&
464 git reset --hard &&
465 echo foo >> file &&
466 git stash &&
467 test_when_finished "git stash drop" &&
468 echo bar >> file &&
469 STASH_ID=$(git stash create) &&
470 git reset --hard &&
471 git stash branch stash-branch ${STASH_ID} &&
472 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
473 test $(git ls-files --modified | wc -l) -eq 1
476 test_expect_success 'stash show format defaults to --stat' '
477 git stash clear &&
478 test_when_finished "git reset --hard HEAD" &&
479 git reset --hard &&
480 echo foo >> file &&
481 git stash &&
482 test_when_finished "git stash drop" &&
483 echo bar >> file &&
484 STASH_ID=$(git stash create) &&
485 git reset --hard &&
486 cat >expected <<-EOF &&
487 file | 1 +
488 1 file changed, 1 insertion(+)
490 git stash show ${STASH_ID} >actual &&
491 test_i18ncmp expected actual
494 test_expect_success 'stash show - stashes on stack, stash-like argument' '
495 git stash clear &&
496 test_when_finished "git reset --hard HEAD" &&
497 git reset --hard &&
498 echo foo >> file &&
499 git stash &&
500 test_when_finished "git stash drop" &&
501 echo bar >> file &&
502 STASH_ID=$(git stash create) &&
503 git reset --hard &&
504 echo "1 0 file" >expected &&
505 git stash show --numstat ${STASH_ID} >actual &&
506 test_cmp expected actual
509 test_expect_success 'stash show -p - 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 git stash &&
515 test_when_finished "git stash drop" &&
516 echo bar >> file &&
517 STASH_ID=$(git stash create) &&
518 git reset --hard &&
519 cat >expected <<-EOF &&
520 diff --git a/file b/file
521 index 7601807..935fbd3 100644
522 --- a/file
523 +++ b/file
524 @@ -1 +1,2 @@
526 +bar
528 git stash show -p ${STASH_ID} >actual &&
529 test_cmp expected actual
532 test_expect_success 'stash show - 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 echo "1 0 file" >expected &&
540 git stash show --numstat ${STASH_ID} >actual &&
541 test_cmp expected actual
544 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
545 git stash clear &&
546 test_when_finished "git reset --hard HEAD" &&
547 git reset --hard &&
548 echo foo >> file &&
549 STASH_ID=$(git stash create) &&
550 git reset --hard &&
551 cat >expected <<-EOF &&
552 diff --git a/file b/file
553 index 7601807..71b52c4 100644
554 --- a/file
555 +++ b/file
556 @@ -1 +1,2 @@
558 +foo
560 git stash show -p ${STASH_ID} >actual &&
561 test_cmp expected actual
564 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
565 git stash clear &&
566 test_when_finished "git reset --hard HEAD && git stash clear" &&
567 git reset --hard &&
568 echo foo > file &&
569 git stash &&
570 echo bar > file &&
571 git stash &&
572 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
573 git stash pop &&
574 test bar = "$(cat file)" &&
575 git reset --hard HEAD
578 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
579 git stash clear &&
580 test_when_finished "git reset --hard HEAD && git stash clear" &&
581 git reset --hard &&
582 echo foo > file &&
583 git stash &&
584 echo bar > file &&
585 git stash &&
586 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
587 git stash pop &&
588 test bar = "$(cat file)" &&
589 git reset --hard HEAD
592 test_expect_success 'ref with non-existent reflog' '
593 git stash clear &&
594 echo bar5 > file &&
595 echo bar6 > file2 &&
596 git add file2 &&
597 git stash &&
598 test_must_fail git rev-parse --quiet --verify does-not-exist &&
599 test_must_fail git stash drop does-not-exist &&
600 test_must_fail git stash drop does-not-exist@{0} &&
601 test_must_fail git stash pop does-not-exist &&
602 test_must_fail git stash pop does-not-exist@{0} &&
603 test_must_fail git stash apply does-not-exist &&
604 test_must_fail git stash apply does-not-exist@{0} &&
605 test_must_fail git stash show does-not-exist &&
606 test_must_fail git stash show does-not-exist@{0} &&
607 test_must_fail git stash branch tmp does-not-exist &&
608 test_must_fail git stash branch tmp does-not-exist@{0} &&
609 git stash drop
612 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
613 git stash clear &&
614 test_must_fail git stash drop stash@{0} &&
615 echo bar5 > file &&
616 echo bar6 > file2 &&
617 git add file2 &&
618 git stash &&
619 test_must_fail git stash drop stash@{1} &&
620 test_must_fail git stash pop stash@{1} &&
621 test_must_fail git stash apply stash@{1} &&
622 test_must_fail git stash show stash@{1} &&
623 test_must_fail git stash branch tmp stash@{1} &&
624 git stash drop
627 test_expect_success 'invalid ref of the form "n", n >= N' '
628 git stash clear &&
629 test_must_fail git stash drop 0 &&
630 echo bar5 >file &&
631 echo bar6 >file2 &&
632 git add file2 &&
633 git stash &&
634 test_must_fail git stash drop 1 &&
635 test_must_fail git stash pop 1 &&
636 test_must_fail git stash apply 1 &&
637 test_must_fail git stash show 1 &&
638 test_must_fail git stash branch tmp 1 &&
639 git stash drop
642 test_expect_success 'stash branch should not drop the stash if the branch exists' '
643 git stash clear &&
644 echo foo >file &&
645 git add file &&
646 git commit -m initial &&
647 echo bar >file &&
648 git stash &&
649 test_must_fail git stash branch master stash@{0} &&
650 git rev-parse stash@{0} --
653 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
654 git stash clear &&
655 echo 1 >subdir/subfile1 &&
656 echo 2 >subdir/subfile2 &&
657 git add subdir/subfile1 &&
658 git commit -m subdir &&
660 cd subdir &&
661 echo x >subfile1 &&
662 echo x >../file &&
663 git status >../expect &&
664 git stash &&
665 sane_unset GIT_MERGE_VERBOSITY &&
666 git stash apply
668 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
669 test_i18ncmp expect actual
672 cat > expect << EOF
673 diff --git a/HEAD b/HEAD
674 new file mode 100644
675 index 0000000..fe0cbee
676 --- /dev/null
677 +++ b/HEAD
678 @@ -0,0 +1 @@
679 +file-not-a-ref
682 test_expect_success 'stash where working directory contains "HEAD" file' '
683 git stash clear &&
684 git reset --hard &&
685 echo file-not-a-ref > HEAD &&
686 git add HEAD &&
687 test_tick &&
688 git stash &&
689 git diff-files --quiet &&
690 git diff-index --cached --quiet HEAD &&
691 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
692 git diff stash^..stash > output &&
693 test_cmp output expect
696 test_expect_success 'store called with invalid commit' '
697 test_must_fail git stash store foo
700 test_expect_success 'store updates stash ref and reflog' '
701 git stash clear &&
702 git reset --hard &&
703 echo quux >bazzy &&
704 git add bazzy &&
705 STASH_ID=$(git stash create) &&
706 git reset --hard &&
707 ! grep quux bazzy &&
708 git stash store -m quuxery $STASH_ID &&
709 test $(cat .git/refs/stash) = $STASH_ID &&
710 git reflog --format=%H stash| grep $STASH_ID &&
711 git stash pop &&
712 grep quux bazzy
715 test_expect_success 'handle stash specification with spaces' '
716 git stash clear &&
717 echo pig >file &&
718 git stash &&
719 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
720 test_tick &&
721 echo cow >file &&
722 git stash &&
723 git stash apply "stash@{$stamp}" &&
724 grep pig file
727 test_expect_success 'setup stash with index and worktree changes' '
728 git stash clear &&
729 git reset --hard &&
730 echo index >file &&
731 git add file &&
732 echo working >file &&
733 git stash
736 test_expect_success 'stash list implies --first-parent -m' '
737 cat >expect <<-EOF &&
738 stash@{0}
740 diff --git a/file b/file
741 index 257cc56..d26b33d 100644
742 --- a/file
743 +++ b/file
744 @@ -1 +1 @@
745 -foo
746 +working
748 git stash list --format=%gd -p >actual &&
749 test_cmp expect actual
752 test_expect_success 'stash list --cc shows combined diff' '
753 cat >expect <<-\EOF &&
754 stash@{0}
756 diff --cc file
757 index 257cc56,9015a7a..d26b33d
758 --- a/file
759 +++ b/file
760 @@@ -1,1 -1,1 +1,1 @@@
761 - foo
762 -index
763 ++working
765 git stash list --format=%gd -p --cc >actual &&
766 test_cmp expect actual
769 test_expect_success 'stash is not confused by partial renames' '
770 mv file renamed &&
771 git add renamed &&
772 git stash &&
773 git stash apply &&
774 test_path_is_file renamed &&
775 test_path_is_missing file
778 test_done