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' '
127 git stash drop stash@{1} &&
128 test 2 = $(git stash list | wc -l) &&
130 test 9 = $(cat file) &&
131 test 1 = $(git show :file) &&
132 test 1 = $(git show HEAD:file) &&
136 test 3 = $(cat file) &&
137 test 1 = $(git show :file) &&
138 test 1 = $(git show HEAD:file)
141 test_expect_success
'stash pop' '
144 test 3 = $(cat file) &&
145 test 1 = $(git show :file) &&
146 test 1 = $(git show HEAD:file) &&
147 test 0 = $(git stash list | wc -l)
151 diff --git a/file2 b/file2
153 index 0000000..1fe912c
161 diff --git a/file b/file
162 index 257cc56..5716ca5 100644
171 diff --git a/file b/file
172 index 7601807..5716ca5 100644
178 diff --git a/file2 b/file2
180 index 0000000..1fe912c
187 test_expect_success
'stash branch' '
189 git commit file -m first &&
195 git commit file -m second &&
196 git stash branch stashbranch &&
197 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
198 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
199 git diff --cached > output &&
200 test_cmp output expect &&
202 test_cmp output expect1 &&
204 git commit -m alternate\ second &&
205 git diff master..stashbranch > output &&
206 test_cmp output expect2 &&
207 test 0 = $(git stash list | wc -l)
210 test_expect_success
'apply -q is quiet' '
213 git stash apply -q > output.out 2>&1 &&
214 test_must_be_empty output.out
217 test_expect_success
'save -q is quiet' '
218 git stash save --quiet > output.out 2>&1 &&
219 test_must_be_empty output.out
222 test_expect_success
'pop -q is quiet' '
223 git stash pop -q > output.out 2>&1 &&
224 test_must_be_empty output.out
227 test_expect_success
'pop -q --index works and is quiet' '
230 git stash save --quiet &&
231 git stash pop -q --index > output.out 2>&1 &&
232 test foo = "$(git show :file)" &&
233 test_must_be_empty output.out
236 test_expect_success
'drop -q is quiet' '
238 git stash drop -q > output.out 2>&1 &&
239 test_must_be_empty output.out
242 test_expect_success
'stash -k' '
247 test bar,bar4 = $(cat file),$(cat file2)
250 test_expect_success
'stash --no-keep-index' '
252 echo bar44 > file2 &&
254 git stash --no-keep-index &&
255 test bar,bar2 = $(cat file),$(cat file2)
258 test_expect_success
'stash --invalid-option' '
262 test_must_fail git stash --invalid-option &&
263 test_must_fail git stash save --invalid-option &&
264 test bar5,bar6 = $(cat file),$(cat file2) &&
265 git stash -- -message-starting-with-dash &&
266 test bar,bar2 = $(cat file),$(cat file2)
269 test_expect_success
'stash an added file' '
273 git stash save "added file" &&
276 test new = "$(cat file3)"
279 test_expect_success
'stash rm then recreate' '
283 git stash save "rm then recreate" &&
284 test bar = "$(cat file)" &&
286 test bar7 = "$(cat file)"
289 test_expect_success
'stash rm and ignore' '
292 echo file >.gitignore &&
293 git stash save "rm and ignore" &&
294 test bar = "$(cat file)" &&
295 test file = "$(cat .gitignore)" &&
298 test file = "$(cat .gitignore)"
301 test_expect_success
'stash rm and ignore (stage .gitignore)' '
304 echo file >.gitignore &&
305 git add .gitignore &&
306 git stash save "rm and ignore (stage .gitignore)" &&
307 test bar = "$(cat file)" &&
308 ! test -r .gitignore &&
311 test file = "$(cat .gitignore)"
314 test_expect_success SYMLINKS
'stash file to symlink' '
318 git stash save "file to symlink" &&
320 test bar = "$(cat file)" &&
322 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
325 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
329 git stash save "file to symlink (stage rm)" &&
331 test bar = "$(cat file)" &&
333 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
336 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
341 git stash save "file to symlink (full stage)" &&
343 test bar = "$(cat file)" &&
345 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
348 # This test creates a commit with a symlink used for the following tests
350 test_expect_success
'stash symlink to file' '
352 test_ln_s_add file filelink &&
353 git commit -m "Add symlink" &&
356 git stash save "symlink to file"
359 test_expect_success SYMLINKS
'this must have re-created the symlink' '
361 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
364 test_expect_success
'unstash must re-create the file' '
366 ! test -h filelink &&
367 test bar = "$(cat file)"
370 test_expect_success
'stash symlink to file (stage rm)' '
374 git stash save "symlink to file (stage rm)"
377 test_expect_success SYMLINKS
'this must have re-created the symlink' '
379 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
382 test_expect_success
'unstash must re-create the file' '
384 ! test -h filelink &&
385 test bar = "$(cat file)"
388 test_expect_success
'stash symlink to file (full stage)' '
393 git stash save "symlink to file (full stage)"
396 test_expect_success SYMLINKS
'this must have re-created the symlink' '
398 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
401 test_expect_success
'unstash must re-create the file' '
403 ! test -h filelink &&
404 test bar = "$(cat file)"
407 test_expect_failure
'stash directory to file' '
410 echo foo >dir/file &&
412 git commit -m "Add file in dir" &&
415 git stash save "directory to file" &&
417 test foo = "$(cat dir/file)" &&
418 test_must_fail git stash apply &&
419 test bar = "$(cat dir)" &&
420 git reset --soft HEAD^
423 test_expect_failure
'stash file to directory' '
427 echo foo >file/file &&
428 git stash save "file to directory" &&
430 test bar = "$(cat file)" &&
433 test foo = "$(cat file/file)"
436 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
438 test_when_finished "git reset --hard HEAD" &&
441 STASH_ID=$(git stash create) &&
443 git stash branch stash-branch ${STASH_ID} &&
444 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
445 test $(git ls-files --modified | wc -l) -eq 1
448 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
450 test_when_finished "git reset --hard HEAD" &&
454 test_when_finished "git stash drop" &&
456 STASH_ID=$(git stash create) &&
458 git stash branch stash-branch ${STASH_ID} &&
459 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
460 test $(git ls-files --modified | wc -l) -eq 1
463 test_expect_success
'stash show format defaults to --stat' '
465 test_when_finished "git reset --hard HEAD" &&
469 test_when_finished "git stash drop" &&
471 STASH_ID=$(git stash create) &&
473 cat >expected <<-EOF &&
475 1 file changed, 1 insertion(+)
477 git stash show ${STASH_ID} >actual &&
478 test_i18ncmp expected actual
481 test_expect_success
'stash show - stashes on stack, stash-like argument' '
483 test_when_finished "git reset --hard HEAD" &&
487 test_when_finished "git stash drop" &&
489 STASH_ID=$(git stash create) &&
491 echo "1 0 file" >expected &&
492 git stash show --numstat ${STASH_ID} >actual &&
493 test_cmp expected actual
496 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
498 test_when_finished "git reset --hard HEAD" &&
502 test_when_finished "git stash drop" &&
504 STASH_ID=$(git stash create) &&
506 cat >expected <<-EOF &&
507 diff --git a/file b/file
508 index 7601807..935fbd3 100644
515 git stash show -p ${STASH_ID} >actual &&
516 test_cmp expected actual
519 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
521 test_when_finished "git reset --hard HEAD" &&
524 STASH_ID=$(git stash create) &&
526 echo "1 0 file" >expected &&
527 git stash show --numstat ${STASH_ID} >actual &&
528 test_cmp expected actual
531 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
533 test_when_finished "git reset --hard HEAD" &&
536 STASH_ID=$(git stash create) &&
538 cat >expected <<-EOF &&
539 diff --git a/file b/file
540 index 7601807..71b52c4 100644
547 git stash show -p ${STASH_ID} >actual &&
548 test_cmp expected actual
551 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
553 test_when_finished "git reset --hard HEAD && git stash clear" &&
559 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
561 test bar = "$(cat file)" &&
562 git reset --hard HEAD
565 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
567 test_when_finished "git reset --hard HEAD && git stash clear" &&
573 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
575 test bar = "$(cat file)" &&
576 git reset --hard HEAD
579 test_expect_success
'ref with non-existent reflog' '
585 test_must_fail git rev-parse --quiet --verify does-not-exist &&
586 test_must_fail git stash drop does-not-exist &&
587 test_must_fail git stash drop does-not-exist@{0} &&
588 test_must_fail git stash pop does-not-exist &&
589 test_must_fail git stash pop does-not-exist@{0} &&
590 test_must_fail git stash apply does-not-exist &&
591 test_must_fail git stash apply does-not-exist@{0} &&
592 test_must_fail git stash show does-not-exist &&
593 test_must_fail git stash show does-not-exist@{0} &&
594 test_must_fail git stash branch tmp does-not-exist &&
595 test_must_fail git stash branch tmp does-not-exist@{0} &&
599 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
601 test_must_fail git stash drop stash@{0} &&
606 test_must_fail git stash drop stash@{1} &&
607 test_must_fail git stash pop stash@{1} &&
608 test_must_fail git stash apply stash@{1} &&
609 test_must_fail git stash show stash@{1} &&
610 test_must_fail git stash branch tmp stash@{1} &&
614 test_expect_success
'stash branch should not drop the stash if the branch exists' '
618 git commit -m initial &&
621 test_must_fail git stash branch master stash@{0} &&
622 git rev-parse stash@{0} --
625 test_expect_success
'stash apply shows status same as git status (relative to current directory)' '
627 echo 1 >subdir/subfile1 &&
628 echo 2 >subdir/subfile2 &&
629 git add subdir/subfile1 &&
630 git commit -m subdir &&
635 git status >../expect &&
637 sane_unset GIT_MERGE_VERBOSITY &&
640 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
641 test_i18ncmp expect actual
645 diff --git a/HEAD b/HEAD
647 index 0000000..fe0cbee
654 test_expect_success
'stash where working directory contains "HEAD" file' '
657 echo file-not-a-ref > HEAD &&
661 git diff-files --quiet &&
662 git diff-index --cached --quiet HEAD &&
663 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
664 git diff stash^..stash > output &&
665 test_cmp output expect
668 test_expect_success
'store called with invalid commit' '
669 test_must_fail git stash store foo
672 test_expect_success
'store updates stash ref and reflog' '
677 STASH_ID=$(git stash create) &&
680 git stash store -m quuxery $STASH_ID &&
681 test $(cat .git/refs/stash) = $STASH_ID &&
682 grep $STASH_ID .git/logs/refs/stash &&
687 test_expect_success
'handle stash specification with spaces' '
691 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
695 git stash apply "stash@{$stamp}" &&
699 test_expect_success
'setup stash with index and worktree changes' '
704 echo working >file &&
708 test_expect_success
'stash list implies --first-parent -m' '
709 cat >expect <<-EOF &&
712 diff --git a/file b/file
713 index 257cc56..d26b33d 100644
720 git stash list --format=%gd -p >actual &&
721 test_cmp expect actual
724 test_expect_success
'stash list --cc shows combined diff' '
725 cat >expect <<-\EOF &&
729 index 257cc56,9015a7a..d26b33d
732 @@@ -1,1 -1,1 +1,1 @@@
737 git stash list --format=%gd -p --cc >actual &&
738 test_cmp expect actual