Merge branch 'master' of git://repo.or.cz/alt-git
[git/mingw.git] / t / t3903-stash.sh
blob38a5f01f1c395d395734c2ea24526d63dd9a3d2e
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 - stashes on stack, stash-like argument' '
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 files changed, 1 insertions(+), 0 deletions(-)
450 git stash show ${STASH_ID} >actual &&
451 test_cmp expected actual
454 test_expect_success 'stash show -p - 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 cat >expected <<-EOF &&
465 diff --git a/file b/file
466 index 7601807..935fbd3 100644
467 --- a/file
468 +++ b/file
469 @@ -1 +1,2 @@
471 +bar
473 git stash show -p ${STASH_ID} >actual &&
474 test_cmp expected actual
477 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
478 git stash clear &&
479 test_when_finished "git reset --hard HEAD" &&
480 git reset --hard &&
481 echo foo >> file &&
482 STASH_ID=$(git stash create) &&
483 git reset --hard &&
484 cat >expected <<-EOF &&
485 file | 1 +
486 1 files changed, 1 insertions(+), 0 deletions(-)
488 git stash show ${STASH_ID} >actual &&
489 test_cmp expected actual
492 test_expect_success 'stash show -p - 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 cat >expected <<-EOF &&
500 diff --git a/file b/file
501 index 7601807..71b52c4 100644
502 --- a/file
503 +++ b/file
504 @@ -1 +1,2 @@
506 +foo
508 git stash show -p ${STASH_ID} >actual &&
509 test_cmp expected actual
512 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
513 git stash clear &&
514 test_when_finished "git reset --hard HEAD && git stash clear" &&
515 git reset --hard &&
516 echo foo > file &&
517 git stash &&
518 echo bar > file &&
519 git stash &&
520 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
521 git stash pop &&
522 test bar = "$(cat file)" &&
523 git reset --hard HEAD
526 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
527 git stash clear &&
528 test_when_finished "git reset --hard HEAD && git stash clear" &&
529 git reset --hard &&
530 echo foo > file &&
531 git stash &&
532 echo bar > file &&
533 git stash &&
534 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
535 git stash pop &&
536 test bar = "$(cat file)" &&
537 git reset --hard HEAD
540 test_expect_success 'ref with non-existent reflog' '
541 git stash clear &&
542 echo bar5 > file &&
543 echo bar6 > file2 &&
544 git add file2 &&
545 git stash &&
546 ! "git rev-parse --quiet --verify does-not-exist" &&
547 test_must_fail git stash drop does-not-exist &&
548 test_must_fail git stash drop does-not-exist@{0} &&
549 test_must_fail git stash pop does-not-exist &&
550 test_must_fail git stash pop does-not-exist@{0} &&
551 test_must_fail git stash apply does-not-exist &&
552 test_must_fail git stash apply does-not-exist@{0} &&
553 test_must_fail git stash show does-not-exist &&
554 test_must_fail git stash show does-not-exist@{0} &&
555 test_must_fail git stash branch tmp does-not-exist &&
556 test_must_fail git stash branch tmp does-not-exist@{0} &&
557 git stash drop
560 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
561 git stash clear &&
562 test_must_fail git stash drop stash@{0} &&
563 echo bar5 > file &&
564 echo bar6 > file2 &&
565 git add file2 &&
566 git stash &&
567 test_must_fail git stash drop stash@{1} &&
568 test_must_fail git stash pop stash@{1} &&
569 test_must_fail git stash apply stash@{1} &&
570 test_must_fail git stash show stash@{1} &&
571 test_must_fail git stash branch tmp stash@{1} &&
572 git stash drop
575 test_expect_success 'stash branch should not drop the stash if the branch exists' '
576 git stash clear &&
577 echo foo >file &&
578 git add file &&
579 git commit -m initial &&
580 echo bar >file &&
581 git stash &&
582 test_must_fail git stash branch master stash@{0} &&
583 git rev-parse stash@{0} --
586 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
587 git stash clear &&
588 echo 1 >subdir/subfile1 &&
589 echo 2 >subdir/subfile2 &&
590 git add subdir/subfile1 &&
591 git commit -m subdir &&
593 cd subdir &&
594 echo x >subfile1 &&
595 echo x >../file &&
596 git status >../expect &&
597 git stash &&
598 sane_unset GIT_MERGE_VERBOSITY &&
599 git stash apply
601 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
602 test_cmp expect actual
605 test_done