3 # Copyright (c) 2007 Johannes E Schindelin
6 test_description='Test git stash'
10 test_expect_success 'stash some dirty working directory' '
13 echo unrelated >other-file &&
16 git commit -m initial &&
22 git diff-files --quiet &&
23 git diff-index --cached --quiet HEAD
27 diff --git a/file b/file
28 index 0cfbf08..00750ed 100644
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} &&
48 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 'stash drop complains of extra options' '
97 test_must_fail git stash drop --foo
100 test_expect_success 'drop top stash' '
102 git stash list > stashlist1 &&
106 git stash list > stashlist2 &&
107 test_cmp stashlist1 stashlist2 &&
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' '
120 git stash drop stash@{1} &&
121 test 2 = $(git stash list | wc -l) &&
123 test 9 = $(cat file) &&
124 test 1 = $(git show :file) &&
125 test 1 = $(git show HEAD:file) &&
129 test 3 = $(cat file) &&
130 test 1 = $(git show :file) &&
131 test 1 = $(git show HEAD:file)
134 test_expect_success 'stash pop' '
137 test 3 = $(cat file) &&
138 test 1 = $(git show :file) &&
139 test 1 = $(git show HEAD:file) &&
140 test 0 = $(git stash list | wc -l)
144 diff --git a/file2 b/file2
146 index 0000000..1fe912c
154 diff --git a/file b/file
155 index 257cc56..5716ca5 100644
164 diff --git a/file b/file
165 index 7601807..5716ca5 100644
171 diff --git a/file2 b/file2
173 index 0000000..1fe912c
180 test_expect_success 'stash branch' '
182 git commit file -m first &&
188 git commit file -m second &&
189 git stash branch stashbranch &&
190 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
191 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
192 git diff --cached > output &&
193 test_cmp output expect &&
195 test_cmp output expect1 &&
197 git commit -m alternate\ second &&
198 git diff master..stashbranch > output &&
199 test_cmp output expect2 &&
200 test 0 = $(git stash list | wc -l)
203 test_expect_success 'apply -q is quiet' '
206 git stash apply -q > output.out 2>&1 &&
207 test_must_be_empty output.out
210 test_expect_success 'save -q is quiet' '
211 git stash save --quiet > output.out 2>&1 &&
212 test_must_be_empty output.out
215 test_expect_success 'pop -q is quiet' '
216 git stash pop -q > output.out 2>&1 &&
217 test_must_be_empty output.out
220 test_expect_success 'pop -q --index works and is quiet' '
223 git stash save --quiet &&
224 git stash pop -q --index > output.out 2>&1 &&
225 test foo = "$(git show :file)" &&
226 test_must_be_empty output.out
229 test_expect_success 'drop -q is quiet' '
231 git stash drop -q > output.out 2>&1 &&
232 test_must_be_empty output.out
235 test_expect_success 'stash -k' '
240 test bar,bar4 = $(cat file),$(cat file2)
243 test_expect_success 'stash --no-keep-index' '
245 echo bar44 > file2 &&
247 git stash --no-keep-index &&
248 test bar,bar2 = $(cat file),$(cat file2)
251 test_expect_success 'stash --invalid-option' '
255 test_must_fail git stash --invalid-option &&
256 test_must_fail git stash save --invalid-option &&
257 test bar5,bar6 = $(cat file),$(cat file2) &&
258 git stash -- -message-starting-with-dash &&
259 test bar,bar2 = $(cat file),$(cat file2)
262 test_expect_success 'stash an added file' '
266 git stash save "added file" &&
269 test new = "$(cat file3)"
272 test_expect_success 'stash rm then recreate' '
276 git stash save "rm then recreate" &&
277 test bar = "$(cat file)" &&
279 test bar7 = "$(cat file)"
282 test_expect_success 'stash rm and ignore' '
285 echo file >.gitignore &&
286 git stash save "rm and ignore" &&
287 test bar = "$(cat file)" &&
288 test file = "$(cat .gitignore)" &&
291 test file = "$(cat .gitignore)"
294 test_expect_success 'stash rm and ignore (stage .gitignore)' '
297 echo file >.gitignore &&
298 git add .gitignore &&
299 git stash save "rm and ignore (stage .gitignore)" &&
300 test bar = "$(cat file)" &&
301 ! test -r .gitignore &&
304 test file = "$(cat .gitignore)"
307 test_expect_success SYMLINKS 'stash file to symlink' '
311 git stash save "file to symlink" &&
313 test bar = "$(cat file)" &&
315 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
318 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
322 git stash save "file to symlink (stage rm)" &&
324 test bar = "$(cat file)" &&
326 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
329 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
334 git stash save "file to symlink (full stage)" &&
336 test bar = "$(cat file)" &&
338 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
341 # This test creates a commit with a symlink used for the following tests
343 test_expect_success 'stash symlink to file' '
345 test_ln_s_add file filelink &&
346 git commit -m "Add symlink" &&
349 git stash save "symlink to file"
352 test_expect_success SYMLINKS 'this must have re-created the symlink' '
354 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
357 test_expect_success 'unstash must re-create the file' '
359 ! test -h filelink &&
360 test bar = "$(cat file)"
363 test_expect_success 'stash symlink to file (stage rm)' '
367 git stash save "symlink to file (stage rm)"
370 test_expect_success SYMLINKS 'this must have re-created the symlink' '
372 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
375 test_expect_success 'unstash must re-create the file' '
377 ! test -h filelink &&
378 test bar = "$(cat file)"
381 test_expect_success 'stash symlink to file (full stage)' '
386 git stash save "symlink to file (full stage)"
389 test_expect_success SYMLINKS 'this must have re-created the symlink' '
391 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
394 test_expect_success 'unstash must re-create the file' '
396 ! test -h filelink &&
397 test bar = "$(cat file)"
400 test_expect_failure 'stash directory to file' '
403 echo foo >dir/file &&
405 git commit -m "Add file in dir" &&
408 git stash save "directory to file" &&
410 test foo = "$(cat dir/file)" &&
411 test_must_fail git stash apply &&
412 test bar = "$(cat dir)" &&
413 git reset --soft HEAD^
416 test_expect_failure 'stash file to directory' '
420 echo foo >file/file &&
421 git stash save "file to directory" &&
423 test bar = "$(cat file)" &&
426 test foo = "$(cat file/file)"
429 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
431 test_when_finished "git reset --hard HEAD" &&
434 STASH_ID=$(git stash create) &&
436 git stash branch stash-branch ${STASH_ID} &&
437 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
438 test $(git ls-files --modified | wc -l) -eq 1
441 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
443 test_when_finished "git reset --hard HEAD" &&
447 test_when_finished "git stash drop" &&
449 STASH_ID=$(git stash create) &&
451 git stash branch stash-branch ${STASH_ID} &&
452 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
453 test $(git ls-files --modified | wc -l) -eq 1
456 test_expect_success 'stash show format defaults to --stat' '
458 test_when_finished "git reset --hard HEAD" &&
462 test_when_finished "git stash drop" &&
464 STASH_ID=$(git stash create) &&
466 cat >expected <<-EOF &&
468 1 file changed, 1 insertion(+)
470 git stash show ${STASH_ID} >actual &&
471 test_i18ncmp expected actual
474 test_expect_success 'stash show - stashes on stack, stash-like argument' '
476 test_when_finished "git reset --hard HEAD" &&
480 test_when_finished "git stash drop" &&
482 STASH_ID=$(git stash create) &&
484 echo "1 0 file" >expected &&
485 git stash show --numstat ${STASH_ID} >actual &&
486 test_cmp expected actual
489 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
491 test_when_finished "git reset --hard HEAD" &&
495 test_when_finished "git stash drop" &&
497 STASH_ID=$(git stash create) &&
499 cat >expected <<-EOF &&
500 diff --git a/file b/file
501 index 7601807..935fbd3 100644
508 git stash show -p ${STASH_ID} >actual &&
509 test_cmp expected actual
512 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
514 test_when_finished "git reset --hard HEAD" &&
517 STASH_ID=$(git stash create) &&
519 echo "1 0 file" >expected &&
520 git stash show --numstat ${STASH_ID} >actual &&
521 test_cmp expected actual
524 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
526 test_when_finished "git reset --hard HEAD" &&
529 STASH_ID=$(git stash create) &&
531 cat >expected <<-EOF &&
532 diff --git a/file b/file
533 index 7601807..71b52c4 100644
540 git stash show -p ${STASH_ID} >actual &&
541 test_cmp expected actual
544 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
546 test_when_finished "git reset --hard HEAD && git stash clear" &&
552 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
554 test bar = "$(cat file)" &&
555 git reset --hard HEAD
558 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
560 test_when_finished "git reset --hard HEAD && git stash clear" &&
566 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
568 test bar = "$(cat file)" &&
569 git reset --hard HEAD
572 test_expect_success 'ref with non-existent reflog' '
578 test_must_fail git rev-parse --quiet --verify does-not-exist &&
579 test_must_fail git stash drop does-not-exist &&
580 test_must_fail git stash drop does-not-exist@{0} &&
581 test_must_fail git stash pop does-not-exist &&
582 test_must_fail git stash pop does-not-exist@{0} &&
583 test_must_fail git stash apply does-not-exist &&
584 test_must_fail git stash apply does-not-exist@{0} &&
585 test_must_fail git stash show does-not-exist &&
586 test_must_fail git stash show does-not-exist@{0} &&
587 test_must_fail git stash branch tmp does-not-exist &&
588 test_must_fail git stash branch tmp does-not-exist@{0} &&
592 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
594 test_must_fail git stash drop stash@{0} &&
599 test_must_fail git stash drop stash@{1} &&
600 test_must_fail git stash pop stash@{1} &&
601 test_must_fail git stash apply stash@{1} &&
602 test_must_fail git stash show stash@{1} &&
603 test_must_fail git stash branch tmp stash@{1} &&
607 test_expect_success 'stash branch should not drop the stash if the branch exists' '
611 git commit -m initial &&
614 test_must_fail git stash branch master stash@{0} &&
615 git rev-parse stash@{0} --
618 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
620 echo 1 >subdir/subfile1 &&
621 echo 2 >subdir/subfile2 &&
622 git add subdir/subfile1 &&
623 git commit -m subdir &&
628 git status >../expect &&
630 sane_unset GIT_MERGE_VERBOSITY &&
633 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
634 test_i18ncmp expect actual
638 diff --git a/HEAD b/HEAD
640 index 0000000..fe0cbee
647 test_expect_success 'stash where working directory contains "HEAD" file' '
650 echo file-not-a-ref > HEAD &&
654 git diff-files --quiet &&
655 git diff-index --cached --quiet HEAD &&
656 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
657 git diff stash^..stash > output &&
658 test_cmp output expect
661 test_expect_success 'store called with invalid commit' '
662 test_must_fail git stash store foo
665 test_expect_success 'store updates stash ref and reflog' '
670 STASH_ID=$(git stash create) &&
673 git stash store -m quuxery $STASH_ID &&
674 test $(cat .git/refs/stash) = $STASH_ID &&
675 git reflog --format=%H stash| grep $STASH_ID &&
680 test_expect_success 'handle stash specification with spaces' '
684 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
688 git stash apply "stash@{$stamp}" &&
692 test_expect_success 'setup stash with index and worktree changes' '
697 echo working >file &&
701 test_expect_success 'stash list implies --first-parent -m' '
702 cat >expect <<-EOF &&
705 diff --git a/file b/file
706 index 257cc56..d26b33d 100644
713 git stash list --format=%gd -p >actual &&
714 test_cmp expect actual
717 test_expect_success 'stash list --cc shows combined diff' '
718 cat >expect <<-\EOF &&
722 index 257cc56,9015a7a..d26b33d
725 @@@ -1,1 -1,1 +1,1 @@@
730 git stash list --format=%gd -p --cc >actual &&
731 test_cmp expect actual