archive: handle commits with an empty tree
[git/jnareb-git.git] / t / t3903-stash.sh
blob5dfbda7491aceaa64215ac94bdd416f9b93f55d3
1 #!/bin/sh
3 # Copyright (c) 2007 Johannes E Schindelin
6 test_description='Test git stash'
8 . ./test-lib.sh
10 test_expect_success 'stash some dirty working directory' '
11 echo 1 > file &&
12 git add file &&
13 test_tick &&
14 git commit -m initial &&
15 echo 2 > file &&
16 git add file &&
17 echo 3 > file &&
18 test_tick &&
19 git stash &&
20 git diff-files --quiet &&
21 git diff-index --cached --quiet HEAD
24 cat > expect << EOF
25 diff --git a/file b/file
26 index 0cfbf08..00750ed 100644
27 --- a/file
28 +++ b/file
29 @@ -1 +1 @@
32 EOF
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} &&
42 echo 1 >expect &&
43 test_cmp expect file
46 test_expect_success 'apply does not need clean working directory' '
47 echo 4 >other-file &&
48 git add other-file &&
49 echo 5 >other-file &&
50 git stash apply &&
51 echo 3 >expect &&
52 test_cmp expect file
55 test_expect_success 'apply does not clobber working directory changes' '
56 git reset --hard &&
57 echo 4 >file &&
58 test_must_fail git stash apply &&
59 echo 4 >expect &&
60 test_cmp expect file
63 test_expect_success 'apply stashed changes' '
64 git reset --hard &&
65 echo 5 >other-file &&
66 git add other-file &&
67 test_tick &&
68 git commit -m other-file &&
69 git stash apply &&
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 &&
78 git add other-file &&
79 test_tick &&
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 &&
89 mkdir subdir &&
91 cd subdir &&
92 git stash apply
96 test_expect_success 'drop top stash' '
97 git reset --hard &&
98 git stash list > stashlist1 &&
99 echo 7 > file &&
100 git stash &&
101 git stash drop &&
102 git stash list > stashlist2 &&
103 test_cmp stashlist1 stashlist2 &&
104 git stash apply &&
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' '
111 git reset --hard &&
112 echo 8 > file &&
113 git stash &&
114 echo 9 > file &&
115 git stash &&
116 git stash drop stash@{1} &&
117 test 2 = $(git stash list | wc -l) &&
118 git stash apply &&
119 test 9 = $(cat file) &&
120 test 1 = $(git show :file) &&
121 test 1 = $(git show HEAD:file) &&
122 git reset --hard &&
123 git stash drop &&
124 git stash apply &&
125 test 3 = $(cat file) &&
126 test 1 = $(git show :file) &&
127 test 1 = $(git show HEAD:file)
130 test_expect_success 'stash pop' '
131 git reset --hard &&
132 git 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)
139 cat > expect << EOF
140 diff --git a/file2 b/file2
141 new file mode 100644
142 index 0000000..1fe912c
143 --- /dev/null
144 +++ b/file2
145 @@ -0,0 +1 @@
146 +bar2
149 cat > expect1 << EOF
150 diff --git a/file b/file
151 index 257cc56..5716ca5 100644
152 --- a/file
153 +++ b/file
154 @@ -1 +1 @@
155 -foo
156 +bar
159 cat > expect2 << EOF
160 diff --git a/file b/file
161 index 7601807..5716ca5 100644
162 --- a/file
163 +++ b/file
164 @@ -1 +1 @@
165 -baz
166 +bar
167 diff --git a/file2 b/file2
168 new file mode 100644
169 index 0000000..1fe912c
170 --- /dev/null
171 +++ b/file2
172 @@ -0,0 +1 @@
173 +bar2
176 test_expect_success 'stash branch' '
177 echo foo > file &&
178 git commit file -m first &&
179 echo bar > file &&
180 echo bar2 > file2 &&
181 git add file2 &&
182 git stash &&
183 echo baz > file &&
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 &&
190 git diff > output &&
191 test_cmp output expect1 &&
192 git add file &&
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' '
200 echo foo > file &&
201 git stash &&
202 git stash apply -q > output.out 2>&1 &&
203 test ! -s output.out
206 test_expect_success 'save -q is quiet' '
207 git stash save --quiet > output.out 2>&1 &&
208 test ! -s output.out
211 test_expect_success 'pop -q is quiet' '
212 git stash pop -q > output.out 2>&1 &&
213 test ! -s output.out
216 test_expect_success 'pop -q --index works and is quiet' '
217 echo foo > file &&
218 git add file &&
219 git stash save --quiet &&
220 git stash pop -q --index > output.out 2>&1 &&
221 test foo = "$(git show :file)" &&
222 test ! -s output.out
225 test_expect_success 'drop -q is quiet' '
226 git stash &&
227 git stash drop -q > output.out 2>&1 &&
228 test ! -s output.out
231 test_expect_success 'stash -k' '
232 echo bar3 > file &&
233 echo bar4 > file2 &&
234 git add file2 &&
235 git stash -k &&
236 test bar,bar4 = $(cat file),$(cat file2)
239 test_expect_success 'stash --no-keep-index' '
240 echo bar33 > file &&
241 echo bar44 > file2 &&
242 git add file2 &&
243 git stash --no-keep-index &&
244 test bar,bar2 = $(cat file),$(cat file2)
247 test_expect_success 'stash --invalid-option' '
248 echo bar5 > file &&
249 echo bar6 > file2 &&
250 git add file2 &&
251 test_must_fail git stash --invalid-option &&
252 test_must_fail git stash save --invalid-option &&
253 test bar5,bar6 = $(cat file),$(cat file2) &&
254 git stash -- -message-starting-with-dash &&
255 test bar,bar2 = $(cat file),$(cat file2)
258 test_expect_success 'stash an added file' '
259 git reset --hard &&
260 echo new >file3 &&
261 git add file3 &&
262 git stash save "added file" &&
263 ! test -r file3 &&
264 git stash apply &&
265 test new = "$(cat file3)"
268 test_expect_success 'stash rm then recreate' '
269 git reset --hard &&
270 git rm file &&
271 echo bar7 >file &&
272 git stash save "rm then recreate" &&
273 test bar = "$(cat file)" &&
274 git stash apply &&
275 test bar7 = "$(cat file)"
278 test_expect_success 'stash rm and ignore' '
279 git reset --hard &&
280 git rm file &&
281 echo file >.gitignore &&
282 git stash save "rm and ignore" &&
283 test bar = "$(cat file)" &&
284 test file = "$(cat .gitignore)" &&
285 git stash apply &&
286 ! test -r file &&
287 test file = "$(cat .gitignore)"
290 test_expect_success 'stash rm and ignore (stage .gitignore)' '
291 git reset --hard &&
292 git rm file &&
293 echo file >.gitignore &&
294 git add .gitignore &&
295 git stash save "rm and ignore (stage .gitignore)" &&
296 test bar = "$(cat file)" &&
297 ! test -r .gitignore &&
298 git stash apply &&
299 ! test -r file &&
300 test file = "$(cat .gitignore)"
303 test_expect_success SYMLINKS 'stash file to symlink' '
304 git reset --hard &&
305 rm file &&
306 ln -s file2 file &&
307 git stash save "file to symlink" &&
308 test -f file &&
309 test bar = "$(cat file)" &&
310 git stash apply &&
311 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
314 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
315 git reset --hard &&
316 git rm file &&
317 ln -s file2 file &&
318 git stash save "file to symlink (stage rm)" &&
319 test -f file &&
320 test bar = "$(cat file)" &&
321 git stash apply &&
322 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
325 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
326 git reset --hard &&
327 rm file &&
328 ln -s file2 file &&
329 git add file &&
330 git stash save "file to symlink (full stage)" &&
331 test -f file &&
332 test bar = "$(cat file)" &&
333 git stash apply &&
334 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
337 # This test creates a commit with a symlink used for the following tests
339 test_expect_success SYMLINKS 'stash symlink to file' '
340 git reset --hard &&
341 ln -s file filelink &&
342 git add filelink &&
343 git commit -m "Add symlink" &&
344 rm filelink &&
345 cp file filelink &&
346 git stash save "symlink to file" &&
347 test -h filelink &&
348 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
349 git stash apply &&
350 ! test -h filelink &&
351 test bar = "$(cat file)"
354 test_expect_success SYMLINKS 'stash symlink to file (stage rm)' '
355 git reset --hard &&
356 git rm filelink &&
357 cp file filelink &&
358 git stash save "symlink to file (stage rm)" &&
359 test -h filelink &&
360 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
361 git stash apply &&
362 ! test -h filelink &&
363 test bar = "$(cat file)"
366 test_expect_success SYMLINKS 'stash symlink to file (full stage)' '
367 git reset --hard &&
368 rm filelink &&
369 cp file filelink &&
370 git add filelink &&
371 git stash save "symlink to file (full stage)" &&
372 test -h filelink &&
373 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
374 git stash apply &&
375 ! test -h filelink &&
376 test bar = "$(cat file)"
379 test_expect_failure 'stash directory to file' '
380 git reset --hard &&
381 mkdir dir &&
382 echo foo >dir/file &&
383 git add dir/file &&
384 git commit -m "Add file in dir" &&
385 rm -fr dir &&
386 echo bar >dir &&
387 git stash save "directory to file" &&
388 test -d dir &&
389 test foo = "$(cat dir/file)" &&
390 test_must_fail git stash apply &&
391 test bar = "$(cat dir)" &&
392 git reset --soft HEAD^
395 test_expect_failure 'stash file to directory' '
396 git reset --hard &&
397 rm file &&
398 mkdir file &&
399 echo foo >file/file &&
400 git stash save "file to directory" &&
401 test -f file &&
402 test bar = "$(cat file)" &&
403 git stash apply &&
404 test -f file/file &&
405 test foo = "$(cat file/file)"
408 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
409 git stash clear &&
410 test_when_finished "git reset --hard HEAD" &&
411 git reset --hard &&
412 echo foo >> file &&
413 STASH_ID=$(git stash create) &&
414 git reset --hard &&
415 git stash branch stash-branch ${STASH_ID} &&
416 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
417 test $(git ls-files --modified | wc -l) -eq 1
420 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
421 git stash clear &&
422 test_when_finished "git reset --hard HEAD" &&
423 git reset --hard &&
424 echo foo >> file &&
425 git stash &&
426 test_when_finished "git stash drop" &&
427 echo bar >> file &&
428 STASH_ID=$(git stash create) &&
429 git reset --hard &&
430 git stash branch stash-branch ${STASH_ID} &&
431 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
432 test $(git ls-files --modified | wc -l) -eq 1
435 test_expect_success 'stash show format defaults to --stat' '
436 git stash clear &&
437 test_when_finished "git reset --hard HEAD" &&
438 git reset --hard &&
439 echo foo >> file &&
440 git stash &&
441 test_when_finished "git stash drop" &&
442 echo bar >> file &&
443 STASH_ID=$(git stash create) &&
444 git reset --hard &&
445 cat >expected <<-EOF &&
446 file | 1 +
447 1 file changed, 1 insertion(+)
449 git stash show ${STASH_ID} >actual &&
450 test_i18ncmp expected actual
453 test_expect_success 'stash show - stashes on stack, stash-like argument' '
454 git stash clear &&
455 test_when_finished "git reset --hard HEAD" &&
456 git reset --hard &&
457 echo foo >> file &&
458 git stash &&
459 test_when_finished "git stash drop" &&
460 echo bar >> file &&
461 STASH_ID=$(git stash create) &&
462 git reset --hard &&
463 echo "1 0 file" >expected &&
464 git stash show --numstat ${STASH_ID} >actual &&
465 test_cmp expected actual
468 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
469 git stash clear &&
470 test_when_finished "git reset --hard HEAD" &&
471 git reset --hard &&
472 echo foo >> file &&
473 git stash &&
474 test_when_finished "git stash drop" &&
475 echo bar >> file &&
476 STASH_ID=$(git stash create) &&
477 git reset --hard &&
478 cat >expected <<-EOF &&
479 diff --git a/file b/file
480 index 7601807..935fbd3 100644
481 --- a/file
482 +++ b/file
483 @@ -1 +1,2 @@
485 +bar
487 git stash show -p ${STASH_ID} >actual &&
488 test_cmp expected actual
491 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
492 git stash clear &&
493 test_when_finished "git reset --hard HEAD" &&
494 git reset --hard &&
495 echo foo >> file &&
496 STASH_ID=$(git stash create) &&
497 git reset --hard &&
498 echo "1 0 file" >expected &&
499 git stash show --numstat ${STASH_ID} >actual &&
500 test_cmp expected actual
503 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
504 git stash clear &&
505 test_when_finished "git reset --hard HEAD" &&
506 git reset --hard &&
507 echo foo >> file &&
508 STASH_ID=$(git stash create) &&
509 git reset --hard &&
510 cat >expected <<-EOF &&
511 diff --git a/file b/file
512 index 7601807..71b52c4 100644
513 --- a/file
514 +++ b/file
515 @@ -1 +1,2 @@
517 +foo
519 git stash show -p ${STASH_ID} >actual &&
520 test_cmp expected actual
523 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
524 git stash clear &&
525 test_when_finished "git reset --hard HEAD && git stash clear" &&
526 git reset --hard &&
527 echo foo > file &&
528 git stash &&
529 echo bar > file &&
530 git stash &&
531 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
532 git stash pop &&
533 test bar = "$(cat file)" &&
534 git reset --hard HEAD
537 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
538 git stash clear &&
539 test_when_finished "git reset --hard HEAD && git stash clear" &&
540 git reset --hard &&
541 echo foo > file &&
542 git stash &&
543 echo bar > file &&
544 git stash &&
545 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
546 git stash pop &&
547 test bar = "$(cat file)" &&
548 git reset --hard HEAD
551 test_expect_success 'ref with non-existent reflog' '
552 git stash clear &&
553 echo bar5 > file &&
554 echo bar6 > file2 &&
555 git add file2 &&
556 git stash &&
557 test_must_fail git rev-parse --quiet --verify does-not-exist &&
558 test_must_fail git stash drop does-not-exist &&
559 test_must_fail git stash drop does-not-exist@{0} &&
560 test_must_fail git stash pop does-not-exist &&
561 test_must_fail git stash pop does-not-exist@{0} &&
562 test_must_fail git stash apply does-not-exist &&
563 test_must_fail git stash apply does-not-exist@{0} &&
564 test_must_fail git stash show does-not-exist &&
565 test_must_fail git stash show does-not-exist@{0} &&
566 test_must_fail git stash branch tmp does-not-exist &&
567 test_must_fail git stash branch tmp does-not-exist@{0} &&
568 git stash drop
571 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
572 git stash clear &&
573 test_must_fail git stash drop stash@{0} &&
574 echo bar5 > file &&
575 echo bar6 > file2 &&
576 git add file2 &&
577 git stash &&
578 test_must_fail git stash drop stash@{1} &&
579 test_must_fail git stash pop stash@{1} &&
580 test_must_fail git stash apply stash@{1} &&
581 test_must_fail git stash show stash@{1} &&
582 test_must_fail git stash branch tmp stash@{1} &&
583 git stash drop
586 test_expect_success 'stash branch should not drop the stash if the branch exists' '
587 git stash clear &&
588 echo foo >file &&
589 git add file &&
590 git commit -m initial &&
591 echo bar >file &&
592 git stash &&
593 test_must_fail git stash branch master stash@{0} &&
594 git rev-parse stash@{0} --
597 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
598 git stash clear &&
599 echo 1 >subdir/subfile1 &&
600 echo 2 >subdir/subfile2 &&
601 git add subdir/subfile1 &&
602 git commit -m subdir &&
604 cd subdir &&
605 echo x >subfile1 &&
606 echo x >../file &&
607 git status >../expect &&
608 git stash &&
609 sane_unset GIT_MERGE_VERBOSITY &&
610 git stash apply
612 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
613 test_i18ncmp expect actual
616 cat > expect << EOF
617 diff --git a/HEAD b/HEAD
618 new file mode 100644
619 index 0000000..fe0cbee
620 --- /dev/null
621 +++ b/HEAD
622 @@ -0,0 +1 @@
623 +file-not-a-ref
626 test_expect_success 'stash where working directory contains "HEAD" file' '
627 git stash clear &&
628 git reset --hard &&
629 echo file-not-a-ref > HEAD &&
630 git add HEAD &&
631 test_tick &&
632 git stash &&
633 git diff-files --quiet &&
634 git diff-index --cached --quiet HEAD &&
635 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
636 git diff stash^..stash > output &&
637 test_cmp output expect
640 test_done