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 &&
206 test_expect_success
'save -q is quiet' '
207 git stash save --quiet > output.out 2>&1 &&
211 test_expect_success
'pop -q is quiet' '
212 git stash pop -q > output.out 2>&1 &&
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)" &&
225 test_expect_success
'drop -q is quiet' '
227 git stash drop -q > output.out 2>&1 &&
231 test_expect_success
'stash -k' '
236 test bar,bar4 = $(cat file),$(cat file2)
239 test_expect_success
'stash --invalid-option' '
243 test_must_fail git stash --invalid-option &&
244 test_must_fail git stash save --invalid-option &&
245 test bar5,bar6 = $(cat file),$(cat file2) &&
246 git stash -- -message-starting-with-dash &&
247 test bar,bar2 = $(cat file),$(cat file2)
250 test_expect_success
'stash an added file' '
254 git stash save "added file" &&
257 test new = "$(cat file3)"
260 test_expect_success
'stash rm then recreate' '
264 git stash save "rm then recreate" &&
265 test bar = "$(cat file)" &&
267 test bar7 = "$(cat file)"
270 test_expect_success
'stash rm and ignore' '
273 echo file >.gitignore &&
274 git stash save "rm and ignore" &&
275 test bar = "$(cat file)" &&
276 test file = "$(cat .gitignore)" &&
279 test file = "$(cat .gitignore)"
282 test_expect_success
'stash rm and ignore (stage .gitignore)' '
285 echo file >.gitignore &&
286 git add .gitignore &&
287 git stash save "rm and ignore (stage .gitignore)" &&
288 test bar = "$(cat file)" &&
289 ! test -r .gitignore &&
292 test file = "$(cat .gitignore)"
295 test_expect_success SYMLINKS
'stash file to symlink' '
299 git stash save "file to symlink" &&
301 test bar = "$(cat file)" &&
303 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
306 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
310 git stash save "file to symlink (stage rm)" &&
312 test bar = "$(cat file)" &&
314 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
317 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
322 git stash save "file to symlink (full stage)" &&
324 test bar = "$(cat file)" &&
326 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
329 # This test creates a commit with a symlink used for the following tests
331 test_expect_success SYMLINKS
'stash symlink to file' '
333 ln -s file filelink &&
335 git commit -m "Add symlink" &&
338 git stash save "symlink to file" &&
340 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
342 ! test -h filelink &&
343 test bar = "$(cat file)"
346 test_expect_success SYMLINKS
'stash symlink to file (stage rm)' '
350 git stash save "symlink to file (stage rm)" &&
352 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
354 ! test -h filelink &&
355 test bar = "$(cat file)"
358 test_expect_success SYMLINKS
'stash symlink to file (full stage)' '
363 git stash save "symlink to file (full stage)" &&
365 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
367 ! test -h filelink &&
368 test bar = "$(cat file)"
371 test_expect_failure
'stash directory to file' '
374 echo foo >dir/file &&
376 git commit -m "Add file in dir" &&
379 git stash save "directory to file" &&
381 test foo = "$(cat dir/file)" &&
382 test_must_fail git stash apply &&
383 test bar = "$(cat dir)" &&
384 git reset --soft HEAD^
387 test_expect_failure
'stash file to directory' '
391 echo foo >file/file &&
392 git stash save "file to directory" &&
394 test bar = "$(cat file)" &&
397 test foo = "$(cat file/file)"
400 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
402 test_when_finished "git reset --hard HEAD" &&
405 STASH_ID=$(git stash create) &&
407 git stash branch stash-branch ${STASH_ID} &&
408 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
409 test $(git ls-files --modified | wc -l) -eq 1
412 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
414 test_when_finished "git reset --hard HEAD" &&
418 test_when_finished "git stash drop" &&
420 STASH_ID=$(git stash create) &&
422 git stash branch stash-branch ${STASH_ID} &&
423 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
424 test $(git ls-files --modified | wc -l) -eq 1
427 test_expect_success
'stash show - 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 &&
439 1 files changed, 1 insertions(+), 0 deletions(-)
441 git stash show ${STASH_ID} >actual &&
442 test_cmp expected actual
445 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
447 test_when_finished "git reset --hard HEAD" &&
451 test_when_finished "git stash drop" &&
453 STASH_ID=$(git stash create) &&
455 cat >expected <<-EOF &&
456 diff --git a/file b/file
457 index 7601807..935fbd3 100644
464 git stash show -p ${STASH_ID} >actual &&
465 test_cmp expected actual
468 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
470 test_when_finished "git reset --hard HEAD" &&
473 STASH_ID=$(git stash create) &&
475 cat >expected <<-EOF &&
477 1 files changed, 1 insertions(+), 0 deletions(-)
479 git stash show ${STASH_ID} >actual &&
480 test_cmp expected actual
483 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
485 test_when_finished "git reset --hard HEAD" &&
488 STASH_ID=$(git stash create) &&
490 cat >expected <<-EOF &&
491 diff --git a/file b/file
492 index 7601807..71b52c4 100644
499 git stash show -p ${STASH_ID} >actual &&
500 test_cmp expected actual
503 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
505 test_when_finished "git reset --hard HEAD && git stash clear" &&
511 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
513 test bar = "$(cat file)" &&
514 git reset --hard HEAD
517 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
519 test_when_finished "git reset --hard HEAD && git stash clear" &&
525 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
527 test bar = "$(cat file)" &&
528 git reset --hard HEAD
531 test_expect_success
'ref with non-existant reflog' '
537 ! "git rev-parse --quiet --verify does-not-exist" &&
538 test_must_fail git stash drop does-not-exist &&
539 test_must_fail git stash drop does-not-exist@{0} &&
540 test_must_fail git stash pop does-not-exist &&
541 test_must_fail git stash pop does-not-exist@{0} &&
542 test_must_fail git stash apply does-not-exist &&
543 test_must_fail git stash apply does-not-exist@{0} &&
544 test_must_fail git stash show does-not-exist &&
545 test_must_fail git stash show does-not-exist@{0} &&
546 test_must_fail git stash branch tmp does-not-exist &&
547 test_must_fail git stash branch tmp does-not-exist@{0} &&
551 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
553 test_must_fail git stash drop stash@{0} &&
558 test_must_fail git stash drop stash@{1} &&
559 test_must_fail git stash pop stash@{1} &&
560 test_must_fail git stash apply stash@{1} &&
561 test_must_fail git stash show stash@{1} &&
562 test_must_fail git stash branch tmp stash@{1} &&
566 test_expect_success
'stash branch should not drop the stash if the branch exists' '
570 git commit -m initial &&
573 test_must_fail git stash branch master stash@{0} &&
574 git rev-parse stash@{0} --
577 test_expect_success
'stash apply shows status same as git status (relative to current directory)' '
579 echo 1 >subdir/subfile1 &&
580 echo 2 >subdir/subfile2 &&
581 git add subdir/subfile1 &&
582 git commit -m subdir &&
587 git status >../expect &&
589 sane_unset GIT_MERGE_VERBOSITY &&
592 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
593 test_cmp expect actual