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 requires a clean index' '
49 test_when_finished "git reset --hard" &&
50 echo changed >other-file &&
52 test_must_fail git stash apply
55 test_expect_success
'apply does not need clean working directory' '
62 test_expect_success
'apply does not clobber working directory changes' '
65 test_must_fail git stash apply &&
70 test_expect_success
'apply stashed changes' '
75 git commit -m other-file &&
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 &&
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 &&
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' '
109 git stash list > stashlist1 &&
113 git stash list > stashlist2 &&
114 test_cmp stashlist1 stashlist2 &&
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' '
128 git stash drop stash@{1} &&
129 test 2 = $(git stash list | wc -l) &&
131 test 9 = $(cat file) &&
132 test 1 = $(git show :file) &&
133 test 1 = $(git show HEAD:file) &&
137 test 3 = $(cat file) &&
138 test 1 = $(git show :file) &&
139 test 1 = $(git show HEAD:file)
142 test_expect_success
'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)
152 diff --git a/file2 b/file2
154 index 0000000..1fe912c
162 diff --git a/file b/file
163 index 257cc56..5716ca5 100644
172 diff --git a/file b/file
173 index 7601807..5716ca5 100644
179 diff --git a/file2 b/file2
181 index 0000000..1fe912c
188 test_expect_success
'stash branch' '
190 git commit file -m first &&
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 &&
203 test_cmp output expect1 &&
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' '
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' '
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' '
239 git stash drop -q > output.out 2>&1 &&
240 test_must_be_empty output.out
243 test_expect_success
'stash -k' '
248 test bar,bar4 = $(cat file),$(cat file2)
251 test_expect_success
'stash --no-keep-index' '
253 echo bar44 > file2 &&
255 git stash --no-keep-index &&
256 test bar,bar2 = $(cat file),$(cat file2)
259 test_expect_success
'stash --invalid-option' '
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' '
274 git stash save "added file" &&
277 test new = "$(cat file3)"
280 test_expect_success
'stash rm then recreate' '
284 git stash save "rm then recreate" &&
285 test bar = "$(cat file)" &&
287 test bar7 = "$(cat file)"
290 test_expect_success
'stash rm and ignore' '
293 echo file >.gitignore &&
294 git stash save "rm and ignore" &&
295 test bar = "$(cat file)" &&
296 test file = "$(cat .gitignore)" &&
299 test file = "$(cat .gitignore)"
302 test_expect_success
'stash rm and ignore (stage .gitignore)' '
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 &&
312 test file = "$(cat .gitignore)"
315 test_expect_success SYMLINKS
'stash file to symlink' '
319 git stash save "file to symlink" &&
321 test bar = "$(cat file)" &&
323 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
326 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
330 git stash save "file to symlink (stage rm)" &&
332 test bar = "$(cat file)" &&
334 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
337 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
342 git stash save "file to symlink (full stage)" &&
344 test bar = "$(cat file)" &&
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' '
353 test_ln_s_add file filelink &&
354 git commit -m "Add symlink" &&
357 git stash save "symlink to file"
360 test_expect_success SYMLINKS
'this must have re-created the symlink' '
362 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
365 test_expect_success
'unstash must re-create the file' '
367 ! test -h filelink &&
368 test bar = "$(cat file)"
371 test_expect_success
'stash symlink to file (stage rm)' '
375 git stash save "symlink to file (stage rm)"
378 test_expect_success SYMLINKS
'this must have re-created the symlink' '
380 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
383 test_expect_success
'unstash must re-create the file' '
385 ! test -h filelink &&
386 test bar = "$(cat file)"
389 test_expect_success
'stash symlink to file (full stage)' '
394 git stash save "symlink to file (full stage)"
397 test_expect_success SYMLINKS
'this must have re-created the symlink' '
399 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
402 test_expect_success
'unstash must re-create the file' '
404 ! test -h filelink &&
405 test bar = "$(cat file)"
408 test_expect_failure
'stash directory to file' '
411 echo foo >dir/file &&
413 git commit -m "Add file in dir" &&
416 git stash save "directory to file" &&
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' '
428 echo foo >file/file &&
429 git stash save "file to directory" &&
431 test bar = "$(cat file)" &&
434 test foo = "$(cat file/file)"
437 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
439 test_when_finished "git reset --hard HEAD" &&
442 STASH_ID=$(git stash create) &&
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' '
451 test_when_finished "git reset --hard HEAD" &&
455 test_when_finished "git stash drop" &&
457 STASH_ID=$(git stash create) &&
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' '
466 test_when_finished "git reset --hard HEAD" &&
470 test_when_finished "git stash drop" &&
472 STASH_ID=$(git stash create) &&
474 cat >expected <<-EOF &&
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' '
484 test_when_finished "git reset --hard HEAD" &&
488 test_when_finished "git stash drop" &&
490 STASH_ID=$(git stash create) &&
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' '
499 test_when_finished "git reset --hard HEAD" &&
503 test_when_finished "git stash drop" &&
505 STASH_ID=$(git stash create) &&
507 cat >expected <<-EOF &&
508 diff --git a/file b/file
509 index 7601807..935fbd3 100644
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' '
522 test_when_finished "git reset --hard HEAD" &&
525 STASH_ID=$(git stash create) &&
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' '
534 test_when_finished "git reset --hard HEAD" &&
537 STASH_ID=$(git stash create) &&
539 cat >expected <<-EOF &&
540 diff --git a/file b/file
541 index 7601807..71b52c4 100644
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' '
554 test_when_finished "git reset --hard HEAD && git stash clear" &&
560 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
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' '
568 test_when_finished "git reset --hard HEAD && git stash clear" &&
574 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
576 test bar = "$(cat file)" &&
577 git reset --hard HEAD
580 test_expect_success
'ref with non-existent reflog' '
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} &&
600 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
602 test_must_fail git stash drop stash@{0} &&
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} &&
615 test_expect_success
'stash branch should not drop the stash if the branch exists' '
619 git commit -m initial &&
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)' '
628 echo 1 >subdir/subfile1 &&
629 echo 2 >subdir/subfile2 &&
630 git add subdir/subfile1 &&
631 git commit -m subdir &&
636 git status >../expect &&
638 sane_unset GIT_MERGE_VERBOSITY &&
641 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
642 test_i18ncmp expect actual
646 diff --git a/HEAD b/HEAD
648 index 0000000..fe0cbee
655 test_expect_success
'stash where working directory contains "HEAD" file' '
658 echo file-not-a-ref > HEAD &&
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' '
678 STASH_ID=$(git stash create) &&
681 git stash store -m quuxery $STASH_ID &&
682 test $(cat .git/refs/stash) = $STASH_ID &&
683 grep $STASH_ID .git/logs/refs/stash &&
688 test_expect_success
'handle stash specification with spaces' '
692 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
696 git stash apply "stash@{$stamp}" &&
700 test_expect_success
'setup stash with index and worktree changes' '
705 echo working >file &&
709 test_expect_success
'stash list implies --first-parent -m' '
710 cat >expect <<-EOF &&
713 diff --git a/file b/file
714 index 257cc56..d26b33d 100644
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 &&
730 index 257cc56,9015a7a..d26b33d
733 @@@ -1,1 -1,1 +1,1 @@@
738 git stash list --format=%gd -p --cc >actual &&
739 test_cmp expect actual