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 does not need clean working directory' '
49 test_expect_success
'apply does not clobber working directory changes' '
52 test_must_fail git stash apply &&
57 test_expect_success
'apply stashed changes' '
62 git commit -m other-file &&
64 test 3 = $(cat file) &&
65 test 1 = $(git show :file) &&
66 test 1 = $(git show HEAD:file)
69 test_expect_success
'apply stashed changes (including index)' '
70 git reset --hard HEAD^ &&
71 echo 6 > other-file &&
74 git commit -m other-file &&
75 git stash apply --index &&
76 test 3 = $(cat file) &&
77 test 2 = $(git show :file) &&
78 test 1 = $(git show HEAD:file)
81 test_expect_success
'unstashing in a subdirectory' '
82 git reset --hard HEAD &&
90 test_expect_success
'drop top stash' '
92 git stash list > stashlist1 &&
96 git stash list > stashlist2 &&
97 test_cmp stashlist1 stashlist2 &&
99 test 3 = $(cat file) &&
100 test 1 = $(git show :file) &&
101 test 1 = $(git show HEAD:file)
104 test_expect_success
'drop middle stash' '
110 git stash drop stash@{1} &&
111 test 2 = $(git stash list | wc -l) &&
113 test 9 = $(cat file) &&
114 test 1 = $(git show :file) &&
115 test 1 = $(git show HEAD:file) &&
119 test 3 = $(cat file) &&
120 test 1 = $(git show :file) &&
121 test 1 = $(git show HEAD:file)
124 test_expect_success
'stash pop' '
127 test 3 = $(cat file) &&
128 test 1 = $(git show :file) &&
129 test 1 = $(git show HEAD:file) &&
130 test 0 = $(git stash list | wc -l)
134 diff --git a/file2 b/file2
136 index 0000000..1fe912c
144 diff --git a/file b/file
145 index 257cc56..5716ca5 100644
154 diff --git a/file b/file
155 index 7601807..5716ca5 100644
161 diff --git a/file2 b/file2
163 index 0000000..1fe912c
170 test_expect_success
'stash branch' '
172 git commit file -m first &&
178 git commit file -m second &&
179 git stash branch stashbranch &&
180 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
181 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
182 git diff --cached > output &&
183 test_cmp output expect &&
185 test_cmp output expect1 &&
187 git commit -m alternate\ second &&
188 git diff master..stashbranch > output &&
189 test_cmp output expect2 &&
190 test 0 = $(git stash list | wc -l)
193 test_expect_success
'apply -q is quiet' '
196 git stash apply -q > output.out 2>&1 &&
200 test_expect_success
'save -q is quiet' '
201 git stash save --quiet > output.out 2>&1 &&
205 test_expect_success
'pop -q is quiet' '
206 git stash pop -q > output.out 2>&1 &&
210 test_expect_success
'pop -q --index works and is quiet' '
213 git stash save --quiet &&
214 git stash pop -q --index > output.out 2>&1 &&
215 test foo = "$(git show :file)" &&
219 test_expect_success
'drop -q is quiet' '
221 git stash drop -q > output.out 2>&1 &&
225 test_expect_success
'stash -k' '
230 test bar,bar4 = $(cat file),$(cat file2)
233 test_expect_success
'stash --invalid-option' '
237 test_must_fail git stash --invalid-option &&
238 test_must_fail git stash save --invalid-option &&
239 test bar5,bar6 = $(cat file),$(cat file2) &&
240 git stash -- -message-starting-with-dash &&
241 test bar,bar2 = $(cat file),$(cat file2)
244 test_expect_success
'stash an added file' '
248 git stash save "added file" &&
251 test new = "$(cat file3)"
254 test_expect_success
'stash rm then recreate' '
258 git stash save "rm then recreate" &&
259 test bar = "$(cat file)" &&
261 test bar7 = "$(cat file)"
264 test_expect_success
'stash rm and ignore' '
267 echo file >.gitignore &&
268 git stash save "rm and ignore" &&
269 test bar = "$(cat file)" &&
270 test file = "$(cat .gitignore)" &&
273 test file = "$(cat .gitignore)"
276 test_expect_success
'stash rm and ignore (stage .gitignore)' '
279 echo file >.gitignore &&
280 git add .gitignore &&
281 git stash save "rm and ignore (stage .gitignore)" &&
282 test bar = "$(cat file)" &&
283 ! test -r .gitignore &&
286 test file = "$(cat .gitignore)"
289 test_expect_success SYMLINKS
'stash file to symlink' '
293 git stash save "file to symlink" &&
295 test bar = "$(cat file)" &&
297 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
300 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
304 git stash save "file to symlink (stage rm)" &&
306 test bar = "$(cat file)" &&
308 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
311 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
316 git stash save "file to symlink (full stage)" &&
318 test bar = "$(cat file)" &&
320 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
323 # This test creates a commit with a symlink used for the following tests
325 test_expect_success SYMLINKS
'stash symlink to file' '
327 ln -s file filelink &&
329 git commit -m "Add symlink" &&
332 git stash save "symlink to file" &&
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 (stage rm)' '
344 git stash save "symlink to file (stage rm)" &&
346 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
348 ! test -h filelink &&
349 test bar = "$(cat file)"
352 test_expect_success SYMLINKS
'stash symlink to file (full stage)' '
357 git stash save "symlink to file (full stage)" &&
359 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
361 ! test -h filelink &&
362 test bar = "$(cat file)"
365 test_expect_failure
'stash directory to file' '
368 echo foo >dir/file &&
370 git commit -m "Add file in dir" &&
373 git stash save "directory to file" &&
375 test foo = "$(cat dir/file)" &&
376 test_must_fail git stash apply &&
377 test bar = "$(cat dir)" &&
378 git reset --soft HEAD^
381 test_expect_failure
'stash file to directory' '
385 echo foo >file/file &&
386 git stash save "file to directory" &&
388 test bar = "$(cat file)" &&
391 test foo = "$(cat file/file)"
394 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
396 test_when_finished "git reset --hard HEAD" &&
399 STASH_ID=$(git stash create) &&
401 git stash branch stash-branch ${STASH_ID} &&
402 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
403 test $(git ls-files --modified | wc -l) -eq 1
406 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
408 test_when_finished "git reset --hard HEAD" &&
412 test_when_finished "git stash drop" &&
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 show - 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 cat >expected <<-EOF &&
433 1 files changed, 1 insertions(+), 0 deletions(-)
435 git stash show ${STASH_ID} >actual &&
436 test_cmp expected actual
439 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
441 test_when_finished "git reset --hard HEAD" &&
445 test_when_finished "git stash drop" &&
447 STASH_ID=$(git stash create) &&
449 cat >expected <<-EOF &&
450 diff --git a/file b/file
451 index 7601807..935fbd3 100644
458 git stash show -p ${STASH_ID} >actual &&
459 test_cmp expected actual
462 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
464 test_when_finished "git reset --hard HEAD" &&
467 STASH_ID=$(git stash create) &&
469 cat >expected <<-EOF &&
471 1 files changed, 1 insertions(+), 0 deletions(-)
473 git stash show ${STASH_ID} >actual &&
474 test_cmp expected actual
477 test_expect_success
'stash show -p - 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 &&
485 diff --git a/file b/file
486 index 7601807..71b52c4 100644
493 git stash show -p ${STASH_ID} >actual &&
494 test_cmp expected actual
497 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
499 test_when_finished "git reset --hard HEAD && git stash clear" &&
505 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
507 test bar = "$(cat file)" &&
508 git reset --hard HEAD
511 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
513 test_when_finished "git reset --hard HEAD && git stash clear" &&
519 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
521 test bar = "$(cat file)" &&
522 git reset --hard HEAD
525 test_expect_success
'ref with non-existant reflog' '
531 ! "git rev-parse --quiet --verify does-not-exist" &&
532 test_must_fail git stash drop does-not-exist &&
533 test_must_fail git stash drop does-not-exist@{0} &&
534 test_must_fail git stash pop does-not-exist &&
535 test_must_fail git stash pop does-not-exist@{0} &&
536 test_must_fail git stash apply does-not-exist &&
537 test_must_fail git stash apply does-not-exist@{0} &&
538 test_must_fail git stash show does-not-exist &&
539 test_must_fail git stash show does-not-exist@{0} &&
540 test_must_fail git stash branch tmp does-not-exist &&
541 test_must_fail git stash branch tmp does-not-exist@{0} &&
545 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
547 test_must_fail git stash drop stash@{0} &&
552 test_must_fail git drop stash@{1} &&
553 test_must_fail git pop stash@{1} &&
554 test_must_fail git apply stash@{1} &&
555 test_must_fail git show stash@{1} &&
556 test_must_fail git branch tmp stash@{1} &&
560 test_expect_success
'stash branch should not drop the stash if the branch exists' '
564 git commit -m initial &&
567 test_must_fail git stash branch master stash@{0} &&
568 git rev-parse stash@{0} --
571 test_expect_success
'stash apply shows status same as git status (relative to current directory)' '
573 echo 1 >subdir/subfile1 &&
574 echo 2 >subdir/subfile2 &&
575 git add subdir/subfile1 &&
576 git commit -m subdir &&
581 git status >../expect &&
583 sane_unset GIT_MERGE_VERBOSITY &&
586 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
587 test_cmp expect actual