3 # Copyright (c) 2007 Johannes E Schindelin
6 test_description
='Test git stash'
10 test_expect_success
'stash some dirty working directory' '
14 git commit -m initial &&
20 git diff-files --quiet &&
21 git diff-index --cached --quiet HEAD
25 diff --git a/file b/file
26 index 0cfbf08..00750ed 100644
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} &&
46 test_expect_success
'apply does not need clean working directory' '
55 test_expect_success
'apply does not clobber working directory changes' '
58 test_must_fail git stash apply &&
63 test_expect_success
'apply stashed changes' '
68 git commit -m other-file &&
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 &&
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 &&
96 test_expect_success
'drop top stash' '
98 git stash list > stashlist1 &&
102 git stash list > stashlist2 &&
103 test_cmp stashlist1 stashlist2 &&
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' '
117 git stash drop stash@{1} &&
118 test 2 = $(git stash list | wc -l) &&
120 test 9 = $(cat file) &&
121 test 1 = $(git show :file) &&
122 test 1 = $(git show HEAD:file) &&
126 test 3 = $(cat file) &&
127 test 1 = $(git show :file) &&
128 test 1 = $(git show HEAD:file)
131 test_expect_success
'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)
141 diff --git a/file2 b/file2
143 index 0000000..1fe912c
151 diff --git a/file b/file
152 index 257cc56..5716ca5 100644
161 diff --git a/file b/file
162 index 7601807..5716ca5 100644
168 diff --git a/file2 b/file2
170 index 0000000..1fe912c
177 test_expect_success
'stash branch' '
179 git commit file -m first &&
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 &&
192 test_cmp output expect1 &&
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' '
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' '
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' '
228 git stash drop -q > output.out 2>&1 &&
229 test_must_be_empty output.out
232 test_expect_success
'stash -k' '
237 test bar,bar4 = $(cat file),$(cat file2)
240 test_expect_success
'stash --no-keep-index' '
242 echo bar44 > file2 &&
244 git stash --no-keep-index &&
245 test bar,bar2 = $(cat file),$(cat file2)
248 test_expect_success
'stash --invalid-option' '
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' '
263 git stash save "added file" &&
266 test new = "$(cat file3)"
269 test_expect_success
'stash rm then recreate' '
273 git stash save "rm then recreate" &&
274 test bar = "$(cat file)" &&
276 test bar7 = "$(cat file)"
279 test_expect_success
'stash rm and ignore' '
282 echo file >.gitignore &&
283 git stash save "rm and ignore" &&
284 test bar = "$(cat file)" &&
285 test file = "$(cat .gitignore)" &&
288 test file = "$(cat .gitignore)"
291 test_expect_success
'stash rm and ignore (stage .gitignore)' '
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 &&
301 test file = "$(cat .gitignore)"
304 test_expect_success SYMLINKS
'stash file to symlink' '
308 git stash save "file to symlink" &&
310 test bar = "$(cat file)" &&
312 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
315 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
319 git stash save "file to symlink (stage rm)" &&
321 test bar = "$(cat file)" &&
323 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
326 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
331 git stash save "file to symlink (full stage)" &&
333 test bar = "$(cat file)" &&
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' '
342 test_ln_s_add file filelink &&
343 git commit -m "Add symlink" &&
346 git stash save "symlink to file"
349 test_expect_success SYMLINKS
'this must have re-created the symlink' '
351 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
354 test_expect_success
'unstash must re-create the file' '
356 ! test -h filelink &&
357 test bar = "$(cat file)"
360 test_expect_success
'stash symlink to file (stage rm)' '
364 git stash save "symlink to file (stage rm)"
367 test_expect_success SYMLINKS
'this must have re-created the symlink' '
369 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
372 test_expect_success
'unstash must re-create the file' '
374 ! test -h filelink &&
375 test bar = "$(cat file)"
378 test_expect_success
'stash symlink to file (full stage)' '
383 git stash save "symlink to file (full stage)"
386 test_expect_success SYMLINKS
'this must have re-created the symlink' '
388 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
391 test_expect_success
'unstash must re-create the file' '
393 ! test -h filelink &&
394 test bar = "$(cat file)"
397 test_expect_failure
'stash directory to file' '
400 echo foo >dir/file &&
402 git commit -m "Add file in dir" &&
405 git stash save "directory to file" &&
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' '
417 echo foo >file/file &&
418 git stash save "file to directory" &&
420 test bar = "$(cat file)" &&
423 test foo = "$(cat file/file)"
426 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
428 test_when_finished "git reset --hard HEAD" &&
431 STASH_ID=$(git stash create) &&
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' '
440 test_when_finished "git reset --hard HEAD" &&
444 test_when_finished "git stash drop" &&
446 STASH_ID=$(git stash create) &&
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' '
455 test_when_finished "git reset --hard HEAD" &&
459 test_when_finished "git stash drop" &&
461 STASH_ID=$(git stash create) &&
463 cat >expected <<-EOF &&
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' '
473 test_when_finished "git reset --hard HEAD" &&
477 test_when_finished "git stash drop" &&
479 STASH_ID=$(git stash create) &&
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' '
488 test_when_finished "git reset --hard HEAD" &&
492 test_when_finished "git stash drop" &&
494 STASH_ID=$(git stash create) &&
496 cat >expected <<-EOF &&
497 diff --git a/file b/file
498 index 7601807..935fbd3 100644
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' '
511 test_when_finished "git reset --hard HEAD" &&
514 STASH_ID=$(git stash create) &&
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' '
523 test_when_finished "git reset --hard HEAD" &&
526 STASH_ID=$(git stash create) &&
528 cat >expected <<-EOF &&
529 diff --git a/file b/file
530 index 7601807..71b52c4 100644
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' '
543 test_when_finished "git reset --hard HEAD && git stash clear" &&
549 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
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' '
557 test_when_finished "git reset --hard HEAD && git stash clear" &&
563 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
565 test bar = "$(cat file)" &&
566 git reset --hard HEAD
569 test_expect_success
'ref with non-existent reflog' '
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} &&
589 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
591 test_must_fail git stash drop stash@{0} &&
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} &&
604 test_expect_success
'stash branch should not drop the stash if the branch exists' '
608 git commit -m initial &&
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)' '
617 echo 1 >subdir/subfile1 &&
618 echo 2 >subdir/subfile2 &&
619 git add subdir/subfile1 &&
620 git commit -m subdir &&
625 git status >../expect &&
627 sane_unset GIT_MERGE_VERBOSITY &&
630 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
631 test_i18ncmp expect actual
635 diff --git a/HEAD b/HEAD
637 index 0000000..fe0cbee
644 test_expect_success
'stash where working directory contains "HEAD" file' '
647 echo file-not-a-ref > HEAD &&
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' '
667 STASH_ID=$(git stash create) &&
670 git stash store -m quuxery $STASH_ID &&
671 test $(cat .git/refs/stash) = $STASH_ID &&
672 grep $STASH_ID .git/logs/refs/stash &&
677 test_expect_success
'handle stash specification with spaces' '
681 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
685 git stash apply "stash@{$stamp}" &&
689 test_expect_success
'setup stash with index and worktree changes' '
694 echo working >file &&
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
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
719 index 257cc56,9015a7a..d26b33d
722 @@@ -1,1 -1,1 +1,1 @@@
727 git stash list -p --cc >actual &&
728 test_cmp expect actual