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 'apply needs clean working directory' '
41 echo 4 > other-file &&
43 echo 5 > other-file &&
44 test_must_fail git stash apply
47 test_expect_success 'apply stashed changes' '
50 git commit -m other-file &&
52 test 3 = $(cat file) &&
53 test 1 = $(git show :file) &&
54 test 1 = $(git show HEAD:file)
57 test_expect_success 'apply stashed changes (including index)' '
58 git reset --hard HEAD^ &&
59 echo 6 > other-file &&
62 git commit -m other-file &&
63 git stash apply --index &&
64 test 3 = $(cat file) &&
65 test 2 = $(git show :file) &&
66 test 1 = $(git show HEAD:file)
69 test_expect_success 'unstashing in a subdirectory' '
70 git reset --hard HEAD &&
78 test_expect_success 'drop top stash' '
80 git stash list > stashlist1 &&
84 git stash list > stashlist2 &&
85 test_cmp stashlist1 stashlist2 &&
87 test 3 = $(cat file) &&
88 test 1 = $(git show :file) &&
89 test 1 = $(git show HEAD:file)
92 test_expect_success 'drop middle stash' '
98 git stash drop stash@{1} &&
99 test 2 = $(git stash list | wc -l) &&
101 test 9 = $(cat file) &&
102 test 1 = $(git show :file) &&
103 test 1 = $(git show HEAD:file) &&
107 test 3 = $(cat file) &&
108 test 1 = $(git show :file) &&
109 test 1 = $(git show HEAD:file)
112 test_expect_success 'stash pop' '
115 test 3 = $(cat file) &&
116 test 1 = $(git show :file) &&
117 test 1 = $(git show HEAD:file) &&
118 test 0 = $(git stash list | wc -l)
122 diff --git a/file2 b/file2
124 index 0000000..1fe912c
132 diff --git a/file b/file
133 index 257cc56..5716ca5 100644
142 diff --git a/file b/file
143 index 7601807..5716ca5 100644
149 diff --git a/file2 b/file2
151 index 0000000..1fe912c
158 test_expect_success 'stash branch' '
160 git commit file -m first &&
166 git commit file -m second &&
167 git stash branch stashbranch &&
168 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
169 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
170 git diff --cached > output &&
171 test_cmp output expect &&
173 test_cmp output expect1 &&
175 git commit -m alternate\ second &&
176 git diff master..stashbranch > output &&
177 test_cmp output expect2 &&
178 test 0 = $(git stash list | wc -l)
181 test_expect_success 'apply -q is quiet' '
184 git stash apply -q > output.out 2>&1 &&
188 test_expect_success 'save -q is quiet' '
189 git stash save --quiet > output.out 2>&1 &&
193 test_expect_success 'pop -q is quiet' '
194 git stash pop -q > output.out 2>&1 &&
198 test_expect_success 'pop -q --index works and is quiet' '
201 git stash save --quiet &&
202 git stash pop -q --index > output.out 2>&1 &&
203 test foo = "$(git show :file)" &&
207 test_expect_success 'drop -q is quiet' '
209 git stash drop -q > output.out 2>&1 &&
213 test_expect_success 'stash -k' '
218 test bar,bar4 = $(cat file),$(cat file2)
221 test_expect_success 'stash --invalid-option' '
225 test_must_fail git stash --invalid-option &&
226 test_must_fail git stash save --invalid-option &&
227 test bar5,bar6 = $(cat file),$(cat file2) &&
228 git stash -- -message-starting-with-dash &&
229 test bar,bar2 = $(cat file),$(cat file2)
232 test_expect_success 'stash an added file' '
236 git stash save "added file" &&
239 test new = "$(cat file3)"
242 test_expect_success 'stash rm then recreate' '
246 git stash save "rm then recreate" &&
247 test bar = "$(cat file)" &&
249 test bar7 = "$(cat file)"
252 test_expect_success 'stash rm and ignore' '
255 echo file >.gitignore &&
256 git stash save "rm and ignore" &&
257 test bar = "$(cat file)" &&
258 test file = "$(cat .gitignore)" &&
261 test file = "$(cat .gitignore)"
264 test_expect_success 'stash rm and ignore (stage .gitignore)' '
267 echo file >.gitignore &&
268 git add .gitignore &&
269 git stash save "rm and ignore (stage .gitignore)" &&
270 test bar = "$(cat file)" &&
271 ! test -r .gitignore &&
274 test file = "$(cat .gitignore)"
277 test_expect_success SYMLINKS 'stash file to symlink' '
281 git stash save "file to symlink" &&
283 test bar = "$(cat file)" &&
285 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
288 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
292 git stash save "file to symlink (stage rm)" &&
294 test bar = "$(cat file)" &&
296 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
299 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
304 git stash save "file to symlink (full stage)" &&
306 test bar = "$(cat file)" &&
308 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
311 # This test creates a commit with a symlink used for the following tests
313 test_expect_success SYMLINKS 'stash symlink to file' '
315 ln -s file filelink &&
317 git commit -m "Add symlink" &&
320 git stash save "symlink to file" &&
322 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
324 ! test -h filelink &&
325 test bar = "$(cat file)"
328 test_expect_success SYMLINKS 'stash symlink to file (stage rm)' '
332 git stash save "symlink to file (stage rm)" &&
334 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
336 ! test -h filelink &&
337 test bar = "$(cat file)"
340 test_expect_success SYMLINKS 'stash symlink to file (full stage)' '
345 git stash save "symlink to file (full stage)" &&
347 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
349 ! test -h filelink &&
350 test bar = "$(cat file)"
353 test_expect_failure 'stash directory to file' '
356 echo foo >dir/file &&
358 git commit -m "Add file in dir" &&
361 git stash save "directory to file" &&
363 test foo = "$(cat dir/file)" &&
364 test_must_fail git stash apply &&
365 test bar = "$(cat dir)" &&
366 git reset --soft HEAD^
369 test_expect_failure 'stash file to directory' '
373 echo foo >file/file &&
374 git stash save "file to directory" &&
376 test bar = "$(cat file)" &&
379 test foo = "$(cat file/file)"
382 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
384 test_when_finished "git reset --hard HEAD" &&
387 STASH_ID=$(git stash create) &&
389 git stash branch stash-branch ${STASH_ID} &&
390 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
391 test $(git ls-files --modified | wc -l) -eq 1
394 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
396 test_when_finished "git reset --hard HEAD" &&
400 test_when_finished "git stash drop" &&
402 STASH_ID=$(git stash create) &&
404 git stash branch stash-branch ${STASH_ID} &&
405 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
406 test $(git ls-files --modified | wc -l) -eq 1
409 test_expect_success 'stash show - stashes on stack, stash-like argument' '
411 test_when_finished "git reset --hard HEAD" &&
415 test_when_finished "git stash drop" &&
417 STASH_ID=$(git stash create) &&
419 cat >expected <<-EOF &&
421 1 files changed, 1 insertions(+), 0 deletions(-)
423 git stash show ${STASH_ID} >actual &&
424 test_cmp expected actual
427 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
429 test_when_finished "git reset --hard HEAD" &&
433 test_when_finished "git stash drop" &&
435 STASH_ID=$(git stash create) &&
437 cat >expected <<-EOF &&
438 diff --git a/file b/file
439 index 7601807..935fbd3 100644
446 git stash show -p ${STASH_ID} >actual &&
447 test_cmp expected actual
450 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
452 test_when_finished "git reset --hard HEAD" &&
455 STASH_ID=$(git stash create) &&
457 cat >expected <<-EOF &&
459 1 files changed, 1 insertions(+), 0 deletions(-)
461 git stash show ${STASH_ID} >actual &&
462 test_cmp expected actual
465 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
467 test_when_finished "git reset --hard HEAD" &&
470 STASH_ID=$(git stash create) &&
472 cat >expected <<-EOF &&
473 diff --git a/file b/file
474 index 7601807..71b52c4 100644
481 git stash show -p ${STASH_ID} >actual &&
482 test_cmp expected actual
485 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
487 test_when_finished "git reset --hard HEAD && git stash clear" &&
493 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
495 test bar = "$(cat file)" &&
496 git reset --hard HEAD
499 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
501 test_when_finished "git reset --hard HEAD && git stash clear" &&
507 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
509 test bar = "$(cat file)" &&
510 git reset --hard HEAD
513 test_expect_success 'ref with non-existant reflog' '
519 ! "git rev-parse --quiet --verify does-not-exist" &&
520 test_must_fail git stash drop does-not-exist &&
521 test_must_fail git stash drop does-not-exist@{0} &&
522 test_must_fail git stash pop does-not-exist &&
523 test_must_fail git stash pop does-not-exist@{0} &&
524 test_must_fail git stash apply does-not-exist &&
525 test_must_fail git stash apply does-not-exist@{0} &&
526 test_must_fail git stash show does-not-exist &&
527 test_must_fail git stash show does-not-exist@{0} &&
528 test_must_fail git stash branch tmp does-not-exist &&
529 test_must_fail git stash branch tmp does-not-exist@{0} &&
533 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
535 test_must_fail git stash drop stash@{0} &&
540 test_must_fail git drop stash@{1} &&
541 test_must_fail git pop stash@{1} &&
542 test_must_fail git apply stash@{1} &&
543 test_must_fail git show stash@{1} &&
544 test_must_fail git branch tmp stash@{1} &&
548 test_expect_success 'stash branch should not drop the stash if the branch exists' '
552 git commit -m initial &&
555 test_must_fail git stash branch master stash@{0} &&
556 git rev-parse stash@{0} --
559 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
561 echo 1 >subdir/subfile1 &&
562 echo 2 >subdir/subfile2 &&
563 git add subdir/subfile1 &&
564 git commit -m subdir &&
569 git status >../expect &&
571 sane_unset GIT_MERGE_VERBOSITY &&
574 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
575 test_cmp expect actual