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' '
116 git stash drop stash@{1} &&
117 test 2 = $(git stash list | wc -l) &&
119 test 9 = $(cat file) &&
120 test 1 = $(git show :file) &&
121 test 1 = $(git show HEAD:file) &&
125 test 3 = $(cat file) &&
126 test 1 = $(git show :file) &&
127 test 1 = $(git show HEAD:file)
130 test_expect_success
'stash pop' '
133 test 3 = $(cat file) &&
134 test 1 = $(git show :file) &&
135 test 1 = $(git show HEAD:file) &&
136 test 0 = $(git stash list | wc -l)
140 diff --git a/file2 b/file2
142 index 0000000..1fe912c
150 diff --git a/file b/file
151 index 257cc56..5716ca5 100644
160 diff --git a/file b/file
161 index 7601807..5716ca5 100644
167 diff --git a/file2 b/file2
169 index 0000000..1fe912c
176 test_expect_success
'stash branch' '
178 git commit file -m first &&
184 git commit file -m second &&
185 git stash branch stashbranch &&
186 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
187 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
188 git diff --cached > output &&
189 test_cmp output expect &&
191 test_cmp output expect1 &&
193 git commit -m alternate\ second &&
194 git diff master..stashbranch > output &&
195 test_cmp output expect2 &&
196 test 0 = $(git stash list | wc -l)
199 test_expect_success
'apply -q is quiet' '
202 git stash apply -q > output.out 2>&1 &&
203 test_must_be_empty output.out
206 test_expect_success
'save -q is quiet' '
207 git stash save --quiet > output.out 2>&1 &&
208 test_must_be_empty output.out
211 test_expect_success
'pop -q is quiet' '
212 git stash pop -q > output.out 2>&1 &&
213 test_must_be_empty output.out
216 test_expect_success
'pop -q --index works and is quiet' '
219 git stash save --quiet &&
220 git stash pop -q --index > output.out 2>&1 &&
221 test foo = "$(git show :file)" &&
222 test_must_be_empty output.out
225 test_expect_success
'drop -q is quiet' '
227 git stash drop -q > output.out 2>&1 &&
228 test_must_be_empty output.out
231 test_expect_success
'stash -k' '
236 test bar,bar4 = $(cat file),$(cat file2)
239 test_expect_success
'stash --no-keep-index' '
241 echo bar44 > file2 &&
243 git stash --no-keep-index &&
244 test bar,bar2 = $(cat file),$(cat file2)
247 test_expect_success
'stash --invalid-option' '
251 test_must_fail git stash --invalid-option &&
252 test_must_fail git stash save --invalid-option &&
253 test bar5,bar6 = $(cat file),$(cat file2) &&
254 git stash -- -message-starting-with-dash &&
255 test bar,bar2 = $(cat file),$(cat file2)
258 test_expect_success
'stash an added file' '
262 git stash save "added file" &&
265 test new = "$(cat file3)"
268 test_expect_success
'stash rm then recreate' '
272 git stash save "rm then recreate" &&
273 test bar = "$(cat file)" &&
275 test bar7 = "$(cat file)"
278 test_expect_success
'stash rm and ignore' '
281 echo file >.gitignore &&
282 git stash save "rm and ignore" &&
283 test bar = "$(cat file)" &&
284 test file = "$(cat .gitignore)" &&
287 test file = "$(cat .gitignore)"
290 test_expect_success
'stash rm and ignore (stage .gitignore)' '
293 echo file >.gitignore &&
294 git add .gitignore &&
295 git stash save "rm and ignore (stage .gitignore)" &&
296 test bar = "$(cat file)" &&
297 ! test -r .gitignore &&
300 test file = "$(cat .gitignore)"
303 test_expect_success SYMLINKS
'stash file to symlink' '
307 git stash save "file to symlink" &&
309 test bar = "$(cat file)" &&
311 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
314 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
318 git stash save "file to symlink (stage rm)" &&
320 test bar = "$(cat file)" &&
322 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
325 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
330 git stash save "file to symlink (full stage)" &&
332 test bar = "$(cat file)" &&
334 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
337 # This test creates a commit with a symlink used for the following tests
339 test_expect_success
'stash symlink to file' '
341 test_ln_s_add file filelink &&
342 git commit -m "Add symlink" &&
345 git stash save "symlink to file"
348 test_expect_success SYMLINKS
'this must have re-created the symlink' '
350 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
353 test_expect_success
'unstash must re-create the file' '
355 ! test -h filelink &&
356 test bar = "$(cat file)"
359 test_expect_success
'stash symlink to file (stage rm)' '
363 git stash save "symlink to file (stage rm)"
366 test_expect_success SYMLINKS
'this must have re-created the symlink' '
368 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
371 test_expect_success
'unstash must re-create the file' '
373 ! test -h filelink &&
374 test bar = "$(cat file)"
377 test_expect_success
'stash symlink to file (full stage)' '
382 git stash save "symlink to file (full stage)"
385 test_expect_success SYMLINKS
'this must have re-created the symlink' '
387 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
390 test_expect_success
'unstash must re-create the file' '
392 ! test -h filelink &&
393 test bar = "$(cat file)"
396 test_expect_failure
'stash directory to file' '
399 echo foo >dir/file &&
401 git commit -m "Add file in dir" &&
404 git stash save "directory to file" &&
406 test foo = "$(cat dir/file)" &&
407 test_must_fail git stash apply &&
408 test bar = "$(cat dir)" &&
409 git reset --soft HEAD^
412 test_expect_failure
'stash file to directory' '
416 echo foo >file/file &&
417 git stash save "file to directory" &&
419 test bar = "$(cat file)" &&
422 test foo = "$(cat file/file)"
425 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
427 test_when_finished "git reset --hard HEAD" &&
430 STASH_ID=$(git stash create) &&
432 git stash branch stash-branch ${STASH_ID} &&
433 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
434 test $(git ls-files --modified | wc -l) -eq 1
437 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
439 test_when_finished "git reset --hard HEAD" &&
443 test_when_finished "git stash drop" &&
445 STASH_ID=$(git stash create) &&
447 git stash branch stash-branch ${STASH_ID} &&
448 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
449 test $(git ls-files --modified | wc -l) -eq 1
452 test_expect_success
'stash show format defaults to --stat' '
454 test_when_finished "git reset --hard HEAD" &&
458 test_when_finished "git stash drop" &&
460 STASH_ID=$(git stash create) &&
462 cat >expected <<-EOF &&
464 1 file changed, 1 insertion(+)
466 git stash show ${STASH_ID} >actual &&
467 test_i18ncmp expected actual
470 test_expect_success
'stash show - stashes on stack, stash-like argument' '
472 test_when_finished "git reset --hard HEAD" &&
476 test_when_finished "git stash drop" &&
478 STASH_ID=$(git stash create) &&
480 echo "1 0 file" >expected &&
481 git stash show --numstat ${STASH_ID} >actual &&
482 test_cmp expected actual
485 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
487 test_when_finished "git reset --hard HEAD" &&
491 test_when_finished "git stash drop" &&
493 STASH_ID=$(git stash create) &&
495 cat >expected <<-EOF &&
496 diff --git a/file b/file
497 index 7601807..935fbd3 100644
504 git stash show -p ${STASH_ID} >actual &&
505 test_cmp expected actual
508 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
510 test_when_finished "git reset --hard HEAD" &&
513 STASH_ID=$(git stash create) &&
515 echo "1 0 file" >expected &&
516 git stash show --numstat ${STASH_ID} >actual &&
517 test_cmp expected actual
520 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
522 test_when_finished "git reset --hard HEAD" &&
525 STASH_ID=$(git stash create) &&
527 cat >expected <<-EOF &&
528 diff --git a/file b/file
529 index 7601807..71b52c4 100644
536 git stash show -p ${STASH_ID} >actual &&
537 test_cmp expected actual
540 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
542 test_when_finished "git reset --hard HEAD && git stash clear" &&
548 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
550 test bar = "$(cat file)" &&
551 git reset --hard HEAD
554 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
556 test_when_finished "git reset --hard HEAD && git stash clear" &&
562 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
564 test bar = "$(cat file)" &&
565 git reset --hard HEAD
568 test_expect_success
'ref with non-existent reflog' '
574 test_must_fail git rev-parse --quiet --verify does-not-exist &&
575 test_must_fail git stash drop does-not-exist &&
576 test_must_fail git stash drop does-not-exist@{0} &&
577 test_must_fail git stash pop does-not-exist &&
578 test_must_fail git stash pop does-not-exist@{0} &&
579 test_must_fail git stash apply does-not-exist &&
580 test_must_fail git stash apply does-not-exist@{0} &&
581 test_must_fail git stash show does-not-exist &&
582 test_must_fail git stash show does-not-exist@{0} &&
583 test_must_fail git stash branch tmp does-not-exist &&
584 test_must_fail git stash branch tmp does-not-exist@{0} &&
588 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
590 test_must_fail git stash drop stash@{0} &&
595 test_must_fail git stash drop stash@{1} &&
596 test_must_fail git stash pop stash@{1} &&
597 test_must_fail git stash apply stash@{1} &&
598 test_must_fail git stash show stash@{1} &&
599 test_must_fail git stash branch tmp stash@{1} &&
603 test_expect_success
'stash branch should not drop the stash if the branch exists' '
607 git commit -m initial &&
610 test_must_fail git stash branch master stash@{0} &&
611 git rev-parse stash@{0} --
614 test_expect_success
'stash apply shows status same as git status (relative to current directory)' '
616 echo 1 >subdir/subfile1 &&
617 echo 2 >subdir/subfile2 &&
618 git add subdir/subfile1 &&
619 git commit -m subdir &&
624 git status >../expect &&
626 sane_unset GIT_MERGE_VERBOSITY &&
629 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
630 test_i18ncmp expect actual
634 diff --git a/HEAD b/HEAD
636 index 0000000..fe0cbee
643 test_expect_success
'stash where working directory contains "HEAD" file' '
646 echo file-not-a-ref > HEAD &&
650 git diff-files --quiet &&
651 git diff-index --cached --quiet HEAD &&
652 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
653 git diff stash^..stash > output &&
654 test_cmp output expect
657 test_expect_success
'store called with invalid commit' '
658 test_must_fail git stash store foo
661 test_expect_success
'store updates stash ref and reflog' '
666 STASH_ID=$(git stash create) &&
669 git stash store -m quuxery $STASH_ID &&
670 test $(cat .git/refs/stash) = $STASH_ID &&
671 grep $STASH_ID .git/logs/refs/stash &&
676 test_expect_success
'handle stash specification with spaces' '
680 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
684 git stash apply "stash@{$stamp}" &&
688 test_expect_success
'setup stash with index and worktree changes' '
693 echo working >file &&
697 test_expect_success
'stash list implies --first-parent -m' '
698 cat >expect <<-EOF &&
701 diff --git a/file b/file
702 index 257cc56..d26b33d 100644
709 git stash list --format=%gd -p >actual &&
710 test_cmp expect actual
713 test_expect_success
'stash list --cc shows combined diff' '
714 cat >expect <<-\EOF &&
718 index 257cc56,9015a7a..d26b33d
721 @@@ -1,1 -1,1 +1,1 @@@
726 git stash list --format=%gd -p --cc >actual &&
727 test_cmp expect actual