Merge branch 'master' of git://repo.or.cz/alt-git
[git/mingw.git] / t / t3903-stash.sh
blob51fe3a9a51064359070b6b485c200ee01b7c6090
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 sleep 1 &&
115 echo 9 > file &&
116 git stash &&
117 git stash drop stash@{1} &&
118 test 2 = $(git stash list | wc -l) &&
119 git stash apply &&
120 test 9 = $(cat file) &&
121 test 1 = $(git show :file) &&
122 test 1 = $(git show HEAD:file) &&
123 git reset --hard &&
124 git stash drop &&
125 git stash apply &&
126 test 3 = $(cat file) &&
127 test 1 = $(git show :file) &&
128 test 1 = $(git show HEAD:file)
131 test_expect_success 'stash pop' '
132 git reset --hard &&
133 git 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)
140 cat > expect << EOF
141 diff --git a/file2 b/file2
142 new file mode 100644
143 index 0000000..1fe912c
144 --- /dev/null
145 +++ b/file2
146 @@ -0,0 +1 @@
147 +bar2
150 cat > expect1 << EOF
151 diff --git a/file b/file
152 index 257cc56..5716ca5 100644
153 --- a/file
154 +++ b/file
155 @@ -1 +1 @@
156 -foo
157 +bar
160 cat > expect2 << EOF
161 diff --git a/file b/file
162 index 7601807..5716ca5 100644
163 --- a/file
164 +++ b/file
165 @@ -1 +1 @@
166 -baz
167 +bar
168 diff --git a/file2 b/file2
169 new file mode 100644
170 index 0000000..1fe912c
171 --- /dev/null
172 +++ b/file2
173 @@ -0,0 +1 @@
174 +bar2
177 test_expect_success 'stash branch' '
178 echo foo > file &&
179 git commit file -m first &&
180 echo bar > file &&
181 echo bar2 > file2 &&
182 git add file2 &&
183 git stash &&
184 echo baz > file &&
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 &&
191 git diff > output &&
192 test_cmp output expect1 &&
193 git add file &&
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' '
201 echo foo > file &&
202 git stash &&
203 git stash apply -q > output.out 2>&1 &&
204 test ! -s output.out
207 test_expect_success 'save -q is quiet' '
208 git stash save --quiet > output.out 2>&1 &&
209 test ! -s output.out
212 test_expect_success 'pop -q is quiet' '
213 git stash pop -q > output.out 2>&1 &&
214 test ! -s output.out
217 test_expect_success 'pop -q --index works and is quiet' '
218 echo foo > file &&
219 git add file &&
220 git stash save --quiet &&
221 git stash pop -q --index > output.out 2>&1 &&
222 test foo = "$(git show :file)" &&
223 test ! -s output.out
226 test_expect_success 'drop -q is quiet' '
227 git stash &&
228 git stash drop -q > output.out 2>&1 &&
229 test ! -s output.out
232 test_expect_success 'stash -k' '
233 echo bar3 > file &&
234 echo bar4 > file2 &&
235 git add file2 &&
236 git stash -k &&
237 test bar,bar4 = $(cat file),$(cat file2)
240 test_expect_success 'stash --no-keep-index' '
241 echo bar33 > file &&
242 echo bar44 > file2 &&
243 git add file2 &&
244 git stash --no-keep-index &&
245 test bar,bar2 = $(cat file),$(cat file2)
248 test_expect_success 'stash --invalid-option' '
249 echo bar5 > file &&
250 echo bar6 > file2 &&
251 git add file2 &&
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' '
260 git reset --hard &&
261 echo new >file3 &&
262 git add file3 &&
263 git stash save "added file" &&
264 ! test -r file3 &&
265 git stash apply &&
266 test new = "$(cat file3)"
269 test_expect_success 'stash rm then recreate' '
270 git reset --hard &&
271 git rm file &&
272 echo bar7 >file &&
273 git stash save "rm then recreate" &&
274 test bar = "$(cat file)" &&
275 git stash apply &&
276 test bar7 = "$(cat file)"
279 test_expect_success 'stash rm and ignore' '
280 git reset --hard &&
281 git rm file &&
282 echo file >.gitignore &&
283 git stash save "rm and ignore" &&
284 test bar = "$(cat file)" &&
285 test file = "$(cat .gitignore)" &&
286 git stash apply &&
287 ! test -r file &&
288 test file = "$(cat .gitignore)"
291 test_expect_success 'stash rm and ignore (stage .gitignore)' '
292 git reset --hard &&
293 git rm file &&
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 &&
299 git stash apply &&
300 ! test -r file &&
301 test file = "$(cat .gitignore)"
304 test_expect_success SYMLINKS 'stash file to symlink' '
305 git reset --hard &&
306 rm file &&
307 ln -s file2 file &&
308 git stash save "file to symlink" &&
309 test -f file &&
310 test bar = "$(cat file)" &&
311 git stash apply &&
312 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
315 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
316 git reset --hard &&
317 git rm file &&
318 ln -s file2 file &&
319 git stash save "file to symlink (stage rm)" &&
320 test -f file &&
321 test bar = "$(cat file)" &&
322 git stash apply &&
323 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
326 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
327 git reset --hard &&
328 rm file &&
329 ln -s file2 file &&
330 git add file &&
331 git stash save "file to symlink (full stage)" &&
332 test -f file &&
333 test bar = "$(cat file)" &&
334 git stash apply &&
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' '
341 git reset --hard &&
342 ln -s file filelink &&
343 git add filelink &&
344 git commit -m "Add symlink" &&
345 rm filelink &&
346 cp file filelink &&
347 git stash save "symlink to file" &&
348 test -h filelink &&
349 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
350 git stash apply &&
351 ! test -h filelink &&
352 test bar = "$(cat file)"
355 test_expect_success SYMLINKS 'stash symlink to file (stage rm)' '
356 git reset --hard &&
357 git rm filelink &&
358 cp file filelink &&
359 git stash save "symlink to file (stage rm)" &&
360 test -h filelink &&
361 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
362 git stash apply &&
363 ! test -h filelink &&
364 test bar = "$(cat file)"
367 test_expect_success SYMLINKS 'stash symlink to file (full stage)' '
368 git reset --hard &&
369 rm filelink &&
370 cp file filelink &&
371 git add filelink &&
372 git stash save "symlink to file (full stage)" &&
373 test -h filelink &&
374 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
375 git stash apply &&
376 ! test -h filelink &&
377 test bar = "$(cat file)"
380 test_expect_failure 'stash directory to file' '
381 git reset --hard &&
382 mkdir dir &&
383 echo foo >dir/file &&
384 git add dir/file &&
385 git commit -m "Add file in dir" &&
386 rm -fr dir &&
387 echo bar >dir &&
388 git stash save "directory to file" &&
389 test -d dir &&
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' '
397 git reset --hard &&
398 rm file &&
399 mkdir file &&
400 echo foo >file/file &&
401 git stash save "file to directory" &&
402 test -f file &&
403 test bar = "$(cat file)" &&
404 git stash apply &&
405 test -f file/file &&
406 test foo = "$(cat file/file)"
409 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
410 git stash clear &&
411 test_when_finished "git reset --hard HEAD" &&
412 git reset --hard &&
413 echo foo >> file &&
414 STASH_ID=$(git stash create) &&
415 git reset --hard &&
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' '
422 git stash clear &&
423 test_when_finished "git reset --hard HEAD" &&
424 git reset --hard &&
425 echo foo >> file &&
426 git stash &&
427 test_when_finished "git stash drop" &&
428 echo bar >> file &&
429 STASH_ID=$(git stash create) &&
430 git reset --hard &&
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 format defaults to --stat' '
437 git stash clear &&
438 test_when_finished "git reset --hard HEAD" &&
439 git reset --hard &&
440 echo foo >> file &&
441 git stash &&
442 test_when_finished "git stash drop" &&
443 echo bar >> file &&
444 STASH_ID=$(git stash create) &&
445 git reset --hard &&
446 cat >expected <<-EOF &&
447 file | 1 +
448 1 file changed, 1 insertion(+)
450 git stash show ${STASH_ID} >actual &&
451 test_i18ncmp expected actual
454 test_expect_success 'stash show - stashes on stack, stash-like argument' '
455 git stash clear &&
456 test_when_finished "git reset --hard HEAD" &&
457 git reset --hard &&
458 echo foo >> file &&
459 git stash &&
460 test_when_finished "git stash drop" &&
461 echo bar >> file &&
462 STASH_ID=$(git stash create) &&
463 git reset --hard &&
464 echo "1 0 file" >expected &&
465 git stash show --numstat ${STASH_ID} >actual &&
466 test_cmp expected actual
469 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
470 git stash clear &&
471 test_when_finished "git reset --hard HEAD" &&
472 git reset --hard &&
473 echo foo >> file &&
474 git stash &&
475 test_when_finished "git stash drop" &&
476 echo bar >> file &&
477 STASH_ID=$(git stash create) &&
478 git reset --hard &&
479 cat >expected <<-EOF &&
480 diff --git a/file b/file
481 index 7601807..935fbd3 100644
482 --- a/file
483 +++ b/file
484 @@ -1 +1,2 @@
486 +bar
488 git stash show -p ${STASH_ID} >actual &&
489 test_cmp expected actual
492 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
493 git stash clear &&
494 test_when_finished "git reset --hard HEAD" &&
495 git reset --hard &&
496 echo foo >> file &&
497 STASH_ID=$(git stash create) &&
498 git reset --hard &&
499 echo "1 0 file" >expected &&
500 git stash show --numstat ${STASH_ID} >actual &&
501 test_cmp expected actual
504 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
505 git stash clear &&
506 test_when_finished "git reset --hard HEAD" &&
507 git reset --hard &&
508 echo foo >> file &&
509 STASH_ID=$(git stash create) &&
510 git reset --hard &&
511 cat >expected <<-EOF &&
512 diff --git a/file b/file
513 index 7601807..71b52c4 100644
514 --- a/file
515 +++ b/file
516 @@ -1 +1,2 @@
518 +foo
520 git stash show -p ${STASH_ID} >actual &&
521 test_cmp expected actual
524 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
525 git stash clear &&
526 test_when_finished "git reset --hard HEAD && git stash clear" &&
527 git reset --hard &&
528 echo foo > file &&
529 git stash &&
530 echo bar > file &&
531 git stash &&
532 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
533 git stash pop &&
534 test bar = "$(cat file)" &&
535 git reset --hard HEAD
538 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
539 git stash clear &&
540 test_when_finished "git reset --hard HEAD && git stash clear" &&
541 git reset --hard &&
542 echo foo > file &&
543 git stash &&
544 echo bar > file &&
545 git stash &&
546 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
547 git stash pop &&
548 test bar = "$(cat file)" &&
549 git reset --hard HEAD
552 test_expect_success 'ref with non-existent reflog' '
553 git stash clear &&
554 echo bar5 > file &&
555 echo bar6 > file2 &&
556 git add file2 &&
557 git stash &&
558 test_must_fail git rev-parse --quiet --verify does-not-exist &&
559 test_must_fail git stash drop does-not-exist &&
560 test_must_fail git stash drop does-not-exist@{0} &&
561 test_must_fail git stash pop does-not-exist &&
562 test_must_fail git stash pop does-not-exist@{0} &&
563 test_must_fail git stash apply does-not-exist &&
564 test_must_fail git stash apply does-not-exist@{0} &&
565 test_must_fail git stash show does-not-exist &&
566 test_must_fail git stash show does-not-exist@{0} &&
567 test_must_fail git stash branch tmp does-not-exist &&
568 test_must_fail git stash branch tmp does-not-exist@{0} &&
569 git stash drop
572 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
573 git stash clear &&
574 test_must_fail git stash drop stash@{0} &&
575 echo bar5 > file &&
576 echo bar6 > file2 &&
577 git add file2 &&
578 git stash &&
579 test_must_fail git stash drop stash@{1} &&
580 test_must_fail git stash pop stash@{1} &&
581 test_must_fail git stash apply stash@{1} &&
582 test_must_fail git stash show stash@{1} &&
583 test_must_fail git stash branch tmp stash@{1} &&
584 git stash drop
587 test_expect_success 'stash branch should not drop the stash if the branch exists' '
588 git stash clear &&
589 echo foo >file &&
590 git add file &&
591 git commit -m initial &&
592 echo bar >file &&
593 git stash &&
594 test_must_fail git stash branch master stash@{0} &&
595 git rev-parse stash@{0} --
598 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
599 git stash clear &&
600 echo 1 >subdir/subfile1 &&
601 echo 2 >subdir/subfile2 &&
602 git add subdir/subfile1 &&
603 git commit -m subdir &&
605 cd subdir &&
606 echo x >subfile1 &&
607 echo x >../file &&
608 git status >../expect &&
609 git stash &&
610 sane_unset GIT_MERGE_VERBOSITY &&
611 git stash apply
613 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
614 test_i18ncmp expect actual
617 cat > expect << EOF
618 diff --git a/HEAD b/HEAD
619 new file mode 100644
620 index 0000000..fe0cbee
621 --- /dev/null
622 +++ b/HEAD
623 @@ -0,0 +1 @@
624 +file-not-a-ref
627 test_expect_success 'stash where working directory contains "HEAD" file' '
628 git stash clear &&
629 git reset --hard &&
630 echo file-not-a-ref > HEAD &&
631 git add HEAD &&
632 test_tick &&
633 git stash &&
634 git diff-files --quiet &&
635 git diff-index --cached --quiet HEAD &&
636 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
637 git diff stash^..stash > output &&
638 test_cmp output expect
641 test_done