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' '
99 git stash drop stash@{1} &&
100 test 2 = $(git stash list | wc -l) &&
102 test 9 = $(cat file) &&
103 test 1 = $(git show :file) &&
104 test 1 = $(git show HEAD:file) &&
108 test 3 = $(cat file) &&
109 test 1 = $(git show :file) &&
110 test 1 = $(git show HEAD:file)
113 test_expect_success
'stash pop' '
116 test 3 = $(cat file) &&
117 test 1 = $(git show :file) &&
118 test 1 = $(git show HEAD:file) &&
119 test 0 = $(git stash list | wc -l)
123 diff --git a/file2 b/file2
125 index 0000000..1fe912c
133 diff --git a/file b/file
134 index 257cc56..5716ca5 100644
143 diff --git a/file b/file
144 index 7601807..5716ca5 100644
150 diff --git a/file2 b/file2
152 index 0000000..1fe912c
159 test_expect_success
'stash branch' '
161 git commit file -m first
167 git commit file -m second &&
168 git stash branch stashbranch &&
169 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
170 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
171 git diff --cached > output &&
172 test_cmp output expect &&
174 test_cmp output expect1 &&
176 git commit -m alternate\ second &&
177 git diff master..stashbranch > output &&
178 test_cmp output expect2 &&
179 test 0 = $(git stash list | wc -l)
182 test_expect_success
'apply -q is quiet' '
185 git stash apply -q > output.out 2>&1 &&
189 test_expect_success
'save -q is quiet' '
190 git stash save --quiet > output.out 2>&1 &&
194 test_expect_success
'pop -q is quiet' '
195 git stash pop -q > output.out 2>&1 &&
199 test_expect_success
'pop -q --index works and is quiet' '
202 git stash save --quiet &&
203 git stash pop -q --index > output.out 2>&1 &&
204 test foo = "$(git show :file)" &&
208 test_expect_success
'drop -q is quiet' '
210 git stash drop -q > output.out 2>&1 &&
214 test_expect_success
'stash -k' '
219 test bar,bar4 = $(cat file),$(cat file2)
222 test_expect_success
'stash --invalid-option' '
226 test_must_fail git stash --invalid-option &&
227 test_must_fail git stash save --invalid-option &&
228 test bar5,bar6 = $(cat file),$(cat file2) &&
229 git stash -- -message-starting-with-dash &&
230 test bar,bar2 = $(cat file),$(cat file2)
233 test_expect_success
'stash an added file' '
237 git stash save "added file" &&
240 test new = "$(cat file3)"
243 test_expect_success
'stash rm then recreate' '
247 git stash save "rm then recreate" &&
248 test bar = "$(cat file)" &&
250 test bar7 = "$(cat file)"
253 test_expect_success
'stash rm and ignore' '
256 echo file >.gitignore &&
257 git stash save "rm and ignore" &&
258 test bar = "$(cat file)" &&
259 test file = "$(cat .gitignore)"
262 test file = "$(cat .gitignore)"
265 test_expect_success
'stash rm and ignore (stage .gitignore)' '
268 echo file >.gitignore &&
269 git add .gitignore &&
270 git stash save "rm and ignore (stage .gitignore)" &&
271 test bar = "$(cat file)" &&
275 test file = "$(cat .gitignore)"
278 test_expect_success SYMLINKS
'stash file to symlink' '
282 git stash save "file to symlink" &&
284 test bar = "$(cat file)" &&
286 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
289 test_expect_success SYMLINKS
'stash file to symlink (stage rm)' '
293 git stash save "file to symlink (stage rm)" &&
295 test bar = "$(cat file)" &&
297 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
300 test_expect_success SYMLINKS
'stash file to symlink (full stage)' '
305 git stash save "file to symlink (full stage)" &&
307 test bar = "$(cat file)" &&
309 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
312 # This test creates a commit with a symlink used for the following tests
314 test_expect_success SYMLINKS
'stash symlink to file' '
316 ln -s file filelink &&
318 git commit -m "Add symlink" &&
321 git stash save "symlink to file" &&
323 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
325 ! test -h filelink &&
326 test bar = "$(cat file)"
329 test_expect_success SYMLINKS
'stash symlink to file (stage rm)' '
333 git stash save "symlink to file (stage rm)" &&
335 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
337 ! test -h filelink &&
338 test bar = "$(cat file)"
341 test_expect_success SYMLINKS
'stash symlink to file (full stage)' '
346 git stash save "symlink to file (full stage)" &&
348 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
350 ! test -h filelink &&
351 test bar = "$(cat file)"
354 test_expect_failure
'stash directory to file' '
357 echo foo >dir/file &&
359 git commit -m "Add file in dir" &&
362 git stash save "directory to file" &&
364 test foo = "$(cat dir/file)" &&
365 test_must_fail git stash apply &&
366 test bar = "$(cat dir)" &&
367 git reset --soft HEAD^
370 test_expect_failure
'stash file to directory' '
374 echo foo >file/file &&
375 git stash save "file to directory" &&
377 test bar = "$(cat file)" &&
380 test foo = "$(cat file/file)"
383 test_expect_success
'stash branch - no stashes on stack, stash-like argument' '
385 test_when_finished "git reset --hard HEAD" &&
388 STASH_ID=$(git stash create) &&
390 git stash branch stash-branch ${STASH_ID} &&
391 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
392 test $(git ls-files --modified | wc -l) -eq 1
395 test_expect_success
'stash branch - stashes on stack, stash-like argument' '
397 test_when_finished "git reset --hard HEAD" &&
401 test_when_finished "git stash drop" &&
403 STASH_ID=$(git stash create) &&
405 git stash branch stash-branch ${STASH_ID} &&
406 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
407 test $(git ls-files --modified | wc -l) -eq 1
410 test_expect_success
'stash show - stashes on stack, stash-like argument' '
412 test_when_finished "git reset --hard HEAD" &&
416 test_when_finished "git stash drop" &&
418 STASH_ID=$(git stash create) &&
420 cat >expected <<-EOF &&
422 1 files changed, 1 insertions(+), 0 deletions(-)
424 git stash show ${STASH_ID} >actual &&
425 test_cmp expected actual
428 test_expect_success
'stash show -p - stashes on stack, stash-like argument' '
430 test_when_finished "git reset --hard HEAD" &&
434 test_when_finished "git stash drop" &&
436 STASH_ID=$(git stash create) &&
438 cat >expected <<-EOF &&
439 diff --git a/file b/file
440 index 7601807..935fbd3 100644
447 git stash show -p ${STASH_ID} >actual &&
448 test_cmp expected actual
451 test_expect_success
'stash show - no stashes on stack, stash-like argument' '
453 test_when_finished "git reset --hard HEAD" &&
456 STASH_ID=$(git stash create) &&
458 cat >expected <<-EOF &&
460 1 files changed, 1 insertions(+), 0 deletions(-)
462 git stash show ${STASH_ID} >actual &&
463 test_cmp expected actual
466 test_expect_success
'stash show -p - no stashes on stack, stash-like argument' '
468 test_when_finished "git reset --hard HEAD" &&
471 STASH_ID=$(git stash create) &&
473 cat >expected <<-EOF &&
474 diff --git a/file b/file
475 index 7601807..71b52c4 100644
482 git stash show -p ${STASH_ID} >actual &&
483 test_cmp expected actual
486 test_expect_success
'stash drop - fail early if specified stash is not a stash reference' '
488 test_when_finished "git reset --hard HEAD && git stash clear" &&
494 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
496 test bar = "$(cat file)" &&
497 git reset --hard HEAD
500 test_expect_success
'stash pop - fail early if specified stash is not a stash reference' '
502 test_when_finished "git reset --hard HEAD && git stash clear" &&
508 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
510 test bar = "$(cat file)" &&
511 git reset --hard HEAD
514 test_expect_success
'ref with non-existant reflog' '
520 ! "git rev-parse --quiet --verify does-not-exist" &&
521 test_must_fail git stash drop does-not-exist &&
522 test_must_fail git stash drop does-not-exist@{0} &&
523 test_must_fail git stash pop does-not-exist &&
524 test_must_fail git stash pop does-not-exist@{0} &&
525 test_must_fail git stash apply does-not-exist &&
526 test_must_fail git stash apply does-not-exist@{0} &&
527 test_must_fail git stash show does-not-exist &&
528 test_must_fail git stash show does-not-exist@{0} &&
529 test_must_fail git stash branch tmp does-not-exist &&
530 test_must_fail git stash branch tmp does-not-exist@{0} &&
534 test_expect_success
'invalid ref of the form stash@{n}, n >= N' '
536 test_must_fail git stash drop stash@{0} &&
541 test_must_fail git drop stash@{1} &&
542 test_must_fail git pop stash@{1} &&
543 test_must_fail git apply stash@{1} &&
544 test_must_fail git show stash@{1} &&
545 test_must_fail git branch tmp stash@{1} &&
549 test_expect_success
'stash branch should not drop the stash if the branch exists' '
553 git commit -m initial &&
556 test_must_fail git stash branch master stash@{0} &&
557 git rev-parse stash@{0} --