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 needs clean working directory' '
47 echo 4 > other-file &&
49 echo 5 > other-file &&
50 test_must_fail git stash apply
53 test_expect_success
'apply stashed changes' '
56 git commit -m other-file &&
58 test 3 = $(cat file) &&
59 test 1 = $(git show :file) &&
60 test 1 = $(git show HEAD:file)
63 test_expect_success
'apply stashed changes (including index)' '
64 git reset --hard HEAD^ &&
65 echo 6 > other-file &&
68 git commit -m other-file &&
69 git stash apply --index &&
70 test 3 = $(cat file) &&
71 test 2 = $(git show :file) &&
72 test 1 = $(git show HEAD:file)
75 test_expect_success
'unstashing in a subdirectory' '
76 git reset --hard HEAD &&
84 test_expect_success
'drop top stash' '
86 git stash list > stashlist1 &&
90 git stash list > stashlist2 &&
91 test_cmp stashlist1 stashlist2 &&
93 test 3 = $(cat file) &&
94 test 1 = $(git show :file) &&
95 test 1 = $(git show HEAD:file)
98 test_expect_success
'drop middle stash' '
104 git stash drop stash@{1} &&
105 test 2 = $(git stash list | wc -l) &&
107 test 9 = $(cat file) &&
108 test 1 = $(git show :file) &&
109 test 1 = $(git show HEAD:file) &&
113 test 3 = $(cat file) &&
114 test 1 = $(git show :file) &&
115 test 1 = $(git show HEAD:file)
118 test_expect_success
'stash pop' '
121 test 3 = $(cat file) &&
122 test 1 = $(git show :file) &&
123 test 1 = $(git show HEAD:file) &&
124 test 0 = $(git stash list | wc -l)
128 diff --git a/file2 b/file2
130 index 0000000..1fe912c
138 diff --git a/file b/file
139 index 257cc56..5716ca5 100644
148 diff --git a/file b/file
149 index 7601807..5716ca5 100644
155 diff --git a/file2 b/file2
157 index 0000000..1fe912c
164 test_expect_success
'stash branch' '
166 git commit file -m first &&
172 git commit file -m second &&
173 git stash branch stashbranch &&
174 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
175 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
176 git diff --cached > output &&
177 test_cmp output expect &&
179 test_cmp output expect1 &&
181 git commit -m alternate\ second &&
182 git diff master..stashbranch > output &&
183 test_cmp output expect2 &&
184 test 0 = $(git stash list | wc -l)
187 test_expect_success
'apply -q is quiet' '
190 git stash apply -q > output.out 2>&1 &&
194 test_expect_success
'save -q is quiet' '
195 git stash save --quiet > output.out 2>&1 &&
199 test_expect_success
'pop -q is quiet' '
200 git stash pop -q > output.out 2>&1 &&
204 test_expect_success
'pop -q --index works and is quiet' '
207 git stash save --quiet &&
208 git stash pop -q --index > output.out 2>&1 &&
209 test foo = "$(git show :file)" &&
213 test_expect_success
'drop -q is quiet' '
215 git stash drop -q > output.out 2>&1 &&
219 test_expect_success
'stash -k' '
224 test bar,bar4 = $(cat file),$(cat file2)
227 test_expect_success
'stash --invalid-option' '
231 test_must_fail git stash --invalid-option &&
232 test_must_fail git stash save --invalid-option &&
233 test bar5,bar6 = $(cat file),$(cat file2) &&
234 git stash -- -message-starting-with-dash &&
235 test bar,bar2 = $(cat file),$(cat file2)
238 test_expect_success
'stash an added file' '
242 git stash save "added file" &&
245 test new = "$(cat file3)"
248 test_expect_success
'stash rm then recreate' '
252 git stash save "rm then recreate" &&
253 test bar = "$(cat file)" &&
255 test bar7 = "$(cat file)"
258 test_expect_success
'stash rm and ignore' '
261 echo file >.gitignore &&
262 git stash save "rm and ignore" &&
263 test bar = "$(cat file)" &&
264 test file = "$(cat .gitignore)" &&
267 test file = "$(cat .gitignore)"
270 test_expect_success
'stash rm and ignore (stage .gitignore)' '
273 echo file >.gitignore &&
274 git add .gitignore &&
275 git stash save "rm and ignore (stage .gitignore)" &&
276 test bar = "$(cat file)" &&
277 ! test -r .gitignore &&
280 test file = "$(cat .gitignore)"
283 test_expect_success SYMLINKS
'stash file to symlink' '
287 git stash save "file to symlink" &&
289 test bar = "$(cat file)" &&
291 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
294 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
298 git stash save "file to symlink (stage rm)" &&
300 test bar = "$(cat file)" &&
302 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
305 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
310 git stash save "file to symlink (full stage)" &&
312 test bar = "$(cat file)" &&
314 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
317 # This test creates a commit with a symlink used for the following tests
319 test_expect_success SYMLINKS
'stash symlink to file' '
321 ln -s file filelink &&
323 git commit -m "Add symlink" &&
326 git stash save "symlink to file" &&
328 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
330 ! test -h filelink &&
331 test bar = "$(cat file)"
334 test_expect_success SYMLINKS
'stash symlink to file (stage rm)' '
338 git stash save "symlink to file (stage rm)" &&
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 (full stage)' '
351 git stash save "symlink to file (full stage)" &&
353 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
355 ! test -h filelink &&
356 test bar = "$(cat file)"
359 test_expect_failure
'stash directory to file' '
362 echo foo >dir/file &&
364 git commit -m "Add file in dir" &&
367 git stash save "directory to file" &&
369 test foo = "$(cat dir/file)" &&
370 test_must_fail git stash apply &&
371 test bar = "$(cat dir)" &&
372 git reset --soft HEAD^
375 test_expect_failure
'stash file to directory' '
379 echo foo >file/file &&
380 git stash save "file to directory" &&
382 test bar = "$(cat file)" &&
385 test foo = "$(cat file/file)"
388 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
390 test_when_finished "git reset --hard HEAD" &&
393 STASH_ID=$(git stash create) &&
395 git stash branch stash-branch ${STASH_ID} &&
396 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
397 test $(git ls-files --modified | wc -l) -eq 1
400 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
402 test_when_finished "git reset --hard HEAD" &&
406 test_when_finished "git stash drop" &&
408 STASH_ID=$(git stash create) &&
410 git stash branch stash-branch ${STASH_ID} &&
411 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
412 test $(git ls-files --modified | wc -l) -eq 1
415 test_expect_success
'stash show - stashes on stack, stash-like argument' '
417 test_when_finished "git reset --hard HEAD" &&
421 test_when_finished "git stash drop" &&
423 STASH_ID=$(git stash create) &&
425 cat >expected <<-EOF &&
427 1 files changed, 1 insertions(+), 0 deletions(-)
429 git stash show ${STASH_ID} >actual &&
430 test_cmp expected actual
433 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
435 test_when_finished "git reset --hard HEAD" &&
439 test_when_finished "git stash drop" &&
441 STASH_ID=$(git stash create) &&
443 cat >expected <<-EOF &&
444 diff --git a/file b/file
445 index 7601807..935fbd3 100644
452 git stash show -p ${STASH_ID} >actual &&
453 test_cmp expected actual
456 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
458 test_when_finished "git reset --hard HEAD" &&
461 STASH_ID=$(git stash create) &&
463 cat >expected <<-EOF &&
465 1 files changed, 1 insertions(+), 0 deletions(-)
467 git stash show ${STASH_ID} >actual &&
468 test_cmp expected actual
471 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
473 test_when_finished "git reset --hard HEAD" &&
476 STASH_ID=$(git stash create) &&
478 cat >expected <<-EOF &&
479 diff --git a/file b/file
480 index 7601807..71b52c4 100644
487 git stash show -p ${STASH_ID} >actual &&
488 test_cmp expected actual
491 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
493 test_when_finished "git reset --hard HEAD && git stash clear" &&
499 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
501 test bar = "$(cat file)" &&
502 git reset --hard HEAD
505 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
507 test_when_finished "git reset --hard HEAD && git stash clear" &&
513 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
515 test bar = "$(cat file)" &&
516 git reset --hard HEAD
519 test_expect_success
'ref with non-existant reflog' '
525 ! "git rev-parse --quiet --verify does-not-exist" &&
526 test_must_fail git stash drop does-not-exist &&
527 test_must_fail git stash drop does-not-exist@{0} &&
528 test_must_fail git stash pop does-not-exist &&
529 test_must_fail git stash pop does-not-exist@{0} &&
530 test_must_fail git stash apply does-not-exist &&
531 test_must_fail git stash apply does-not-exist@{0} &&
532 test_must_fail git stash show does-not-exist &&
533 test_must_fail git stash show does-not-exist@{0} &&
534 test_must_fail git stash branch tmp does-not-exist &&
535 test_must_fail git stash branch tmp does-not-exist@{0} &&
539 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
541 test_must_fail git stash drop stash@{0} &&
546 test_must_fail git drop stash@{1} &&
547 test_must_fail git pop stash@{1} &&
548 test_must_fail git apply stash@{1} &&
549 test_must_fail git show stash@{1} &&
550 test_must_fail git branch tmp stash@{1} &&
554 test_expect_success
'stash branch should not drop the stash if the branch exists' '
558 git commit -m initial &&
561 test_must_fail git stash branch master stash@{0} &&
562 git rev-parse stash@{0} --