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' '
117 git stash drop stash@{1} &&
118 test 2 = $(git stash list | wc -l) &&
120 test 9 = $(cat file) &&
121 test 1 = $(git show :file) &&
122 test 1 = $(git show HEAD:file) &&
126 test 3 = $(cat file) &&
127 test 1 = $(git show :file) &&
128 test 1 = $(git show HEAD:file)
131 test_expect_success
'stash pop' '
134 test 3 = $(cat file) &&
135 test 1 = $(git show :file) &&
136 test 1 = $(git show HEAD:file) &&
137 test 0 = $(git stash list | wc -l)
141 diff --git a/file2 b/file2
143 index 0000000..1fe912c
151 diff --git a/file b/file
152 index 257cc56..5716ca5 100644
161 diff --git a/file b/file
162 index 7601807..5716ca5 100644
168 diff --git a/file2 b/file2
170 index 0000000..1fe912c
177 test_expect_success
'stash branch' '
179 git commit file -m first &&
185 git commit file -m second &&
186 git stash branch stashbranch &&
187 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
188 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
189 git diff --cached > output &&
190 test_cmp output expect &&
192 test_cmp output expect1 &&
194 git commit -m alternate\ second &&
195 git diff master..stashbranch > output &&
196 test_cmp output expect2 &&
197 test 0 = $(git stash list | wc -l)
200 test_expect_success
'apply -q is quiet' '
203 git stash apply -q > output.out 2>&1 &&
207 test_expect_success
'save -q is quiet' '
208 git stash save --quiet > output.out 2>&1 &&
212 test_expect_success
'pop -q is quiet' '
213 git stash pop -q > output.out 2>&1 &&
217 test_expect_success
'pop -q --index works and is quiet' '
220 git stash save --quiet &&
221 git stash pop -q --index > output.out 2>&1 &&
222 test foo = "$(git show :file)" &&
226 test_expect_success
'drop -q is quiet' '
228 git stash drop -q > output.out 2>&1 &&
232 test_expect_success
'stash -k' '
237 test bar,bar4 = $(cat file),$(cat file2)
240 test_expect_success
'stash --no-keep-index' '
242 echo bar44 > file2 &&
244 git stash --no-keep-index &&
245 test bar,bar2 = $(cat file),$(cat file2)
248 test_expect_success
'stash --invalid-option' '
252 test_must_fail git stash --invalid-option &&
253 test_must_fail git stash save --invalid-option &&
254 test bar5,bar6 = $(cat file),$(cat file2) &&
255 git stash -- -message-starting-with-dash &&
256 test bar,bar2 = $(cat file),$(cat file2)
259 test_expect_success
'stash an added file' '
263 git stash save "added file" &&
266 test new = "$(cat file3)"
269 test_expect_success
'stash rm then recreate' '
273 git stash save "rm then recreate" &&
274 test bar = "$(cat file)" &&
276 test bar7 = "$(cat file)"
279 test_expect_success
'stash rm and ignore' '
282 echo file >.gitignore &&
283 git stash save "rm and ignore" &&
284 test bar = "$(cat file)" &&
285 test file = "$(cat .gitignore)" &&
288 test file = "$(cat .gitignore)"
291 test_expect_success
'stash rm and ignore (stage .gitignore)' '
294 echo file >.gitignore &&
295 git add .gitignore &&
296 git stash save "rm and ignore (stage .gitignore)" &&
297 test bar = "$(cat file)" &&
298 ! test -r .gitignore &&
301 test file = "$(cat .gitignore)"
304 test_expect_success SYMLINKS
'stash file to symlink' '
308 git stash save "file to symlink" &&
310 test bar = "$(cat file)" &&
312 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
315 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
319 git stash save "file to symlink (stage rm)" &&
321 test bar = "$(cat file)" &&
323 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
326 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
331 git stash save "file to symlink (full stage)" &&
333 test bar = "$(cat file)" &&
335 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
338 # This test creates a commit with a symlink used for the following tests
340 test_expect_success SYMLINKS
'stash symlink to file' '
342 ln -s file filelink &&
344 git commit -m "Add symlink" &&
347 git stash save "symlink to file" &&
349 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
351 ! test -h filelink &&
352 test bar = "$(cat file)"
355 test_expect_success SYMLINKS
'stash symlink to file (stage rm)' '
359 git stash save "symlink to file (stage rm)" &&
361 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
363 ! test -h filelink &&
364 test bar = "$(cat file)"
367 test_expect_success SYMLINKS
'stash symlink to file (full stage)' '
372 git stash save "symlink to file (full stage)" &&
374 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
376 ! test -h filelink &&
377 test bar = "$(cat file)"
380 test_expect_failure
'stash directory to file' '
383 echo foo >dir/file &&
385 git commit -m "Add file in dir" &&
388 git stash save "directory to file" &&
390 test foo = "$(cat dir/file)" &&
391 test_must_fail git stash apply &&
392 test bar = "$(cat dir)" &&
393 git reset --soft HEAD^
396 test_expect_failure
'stash file to directory' '
400 echo foo >file/file &&
401 git stash save "file to directory" &&
403 test bar = "$(cat file)" &&
406 test foo = "$(cat file/file)"
409 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
411 test_when_finished "git reset --hard HEAD" &&
414 STASH_ID=$(git stash create) &&
416 git stash branch stash-branch ${STASH_ID} &&
417 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
418 test $(git ls-files --modified | wc -l) -eq 1
421 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
423 test_when_finished "git reset --hard HEAD" &&
427 test_when_finished "git stash drop" &&
429 STASH_ID=$(git stash create) &&
431 git stash branch stash-branch ${STASH_ID} &&
432 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
433 test $(git ls-files --modified | wc -l) -eq 1
436 test_expect_success
'stash show - stashes on stack, stash-like argument' '
438 test_when_finished "git reset --hard HEAD" &&
442 test_when_finished "git stash drop" &&
444 STASH_ID=$(git stash create) &&
446 cat >expected <<-EOF &&
448 1 files changed, 1 insertions(+), 0 deletions(-)
450 git stash show ${STASH_ID} >actual &&
451 test_cmp expected actual
454 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
456 test_when_finished "git reset --hard HEAD" &&
460 test_when_finished "git stash drop" &&
462 STASH_ID=$(git stash create) &&
464 cat >expected <<-EOF &&
465 diff --git a/file b/file
466 index 7601807..935fbd3 100644
473 git stash show -p ${STASH_ID} >actual &&
474 test_cmp expected actual
477 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
479 test_when_finished "git reset --hard HEAD" &&
482 STASH_ID=$(git stash create) &&
484 cat >expected <<-EOF &&
486 1 files changed, 1 insertions(+), 0 deletions(-)
488 git stash show ${STASH_ID} >actual &&
489 test_cmp expected actual
492 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
494 test_when_finished "git reset --hard HEAD" &&
497 STASH_ID=$(git stash create) &&
499 cat >expected <<-EOF &&
500 diff --git a/file b/file
501 index 7601807..71b52c4 100644
508 git stash show -p ${STASH_ID} >actual &&
509 test_cmp expected actual
512 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
514 test_when_finished "git reset --hard HEAD && git stash clear" &&
520 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
522 test bar = "$(cat file)" &&
523 git reset --hard HEAD
526 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
528 test_when_finished "git reset --hard HEAD && git stash clear" &&
534 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
536 test bar = "$(cat file)" &&
537 git reset --hard HEAD
540 test_expect_success
'ref with non-existent reflog' '
546 ! "git rev-parse --quiet --verify does-not-exist" &&
547 test_must_fail git stash drop does-not-exist &&
548 test_must_fail git stash drop does-not-exist@{0} &&
549 test_must_fail git stash pop does-not-exist &&
550 test_must_fail git stash pop does-not-exist@{0} &&
551 test_must_fail git stash apply does-not-exist &&
552 test_must_fail git stash apply does-not-exist@{0} &&
553 test_must_fail git stash show does-not-exist &&
554 test_must_fail git stash show does-not-exist@{0} &&
555 test_must_fail git stash branch tmp does-not-exist &&
556 test_must_fail git stash branch tmp does-not-exist@{0} &&
560 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
562 test_must_fail git stash drop stash@{0} &&
567 test_must_fail git stash drop stash@{1} &&
568 test_must_fail git stash pop stash@{1} &&
569 test_must_fail git stash apply stash@{1} &&
570 test_must_fail git stash show stash@{1} &&
571 test_must_fail git stash branch tmp stash@{1} &&
575 test_expect_success
'stash branch should not drop the stash if the branch exists' '
579 git commit -m initial &&
582 test_must_fail git stash branch master stash@{0} &&
583 git rev-parse stash@{0} --
586 test_expect_success
'stash apply shows status same as git status (relative to current directory)' '
588 echo 1 >subdir/subfile1 &&
589 echo 2 >subdir/subfile2 &&
590 git add subdir/subfile1 &&
591 git commit -m subdir &&
596 git status >../expect &&
598 sane_unset GIT_MERGE_VERBOSITY &&
601 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
602 test_cmp expect actual