1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Johannes E Schindelin
4 #
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
22 '
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 @@
30 -2
31 +3
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
38 '
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
44 '
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
53 '
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
61 '
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)
73 '
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)
85 '
87 test_expect_success 'unstashing in a subdirectory' '
88 git reset --hard HEAD &&
89 mkdir subdir &&
90 (
91 cd subdir &&
92 git stash apply
93 )
94 '
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)
108 '
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)
128 '
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)
137 '
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
147 EOF
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
157 EOF
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
174 EOF
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)
197 '
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
204 '
206 test_expect_success 'save -q is quiet' '
207 git stash save --quiet > output.out 2>&1 &&
208 test ! -s output.out
209 '
211 test_expect_success 'pop -q is quiet' '
212 git stash pop -q > output.out 2>&1 &&
213 test ! -s output.out
214 '
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
223 '
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
229 '
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)
237 '
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)
245 '
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)
256 '
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)"
266 '
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)"
276 '
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)"
288 '
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)"
301 '
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
312 '
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
323 '
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
335 '
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)"
352 '
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)"
364 '
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)"
377 '
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^
393 '
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)"
406 '
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
418 '
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
433 '
435 test_expect_success 'stash show - stashes on stack, stash-like argument' '
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(+)
448 EOF
449 git stash show ${STASH_ID} >actual &&
450 test_cmp expected actual
451 '
453 test_expect_success 'stash show -p - 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 cat >expected <<-EOF &&
464 diff --git a/file b/file
465 index 7601807..935fbd3 100644
466 --- a/file
467 +++ b/file
468 @@ -1 +1,2 @@
469 baz
470 +bar
471 EOF
472 git stash show -p ${STASH_ID} >actual &&
473 test_cmp expected actual
474 '
476 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
477 git stash clear &&
478 test_when_finished "git reset --hard HEAD" &&
479 git reset --hard &&
480 echo foo >> file &&
481 STASH_ID=$(git stash create) &&
482 git reset --hard &&
483 cat >expected <<-EOF &&
484 file | 1 +
485 1 file changed, 1 insertion(+)
486 EOF
487 git stash show ${STASH_ID} >actual &&
488 test_cmp expected actual
489 '
491 test_expect_success 'stash show -p - 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 cat >expected <<-EOF &&
499 diff --git a/file b/file
500 index 7601807..71b52c4 100644
501 --- a/file
502 +++ b/file
503 @@ -1 +1,2 @@
504 baz
505 +foo
506 EOF
507 git stash show -p ${STASH_ID} >actual &&
508 test_cmp expected actual
509 '
511 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
512 git stash clear &&
513 test_when_finished "git reset --hard HEAD && git stash clear" &&
514 git reset --hard &&
515 echo foo > file &&
516 git stash &&
517 echo bar > file &&
518 git stash &&
519 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
520 git stash pop &&
521 test bar = "$(cat file)" &&
522 git reset --hard HEAD
523 '
525 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
526 git stash clear &&
527 test_when_finished "git reset --hard HEAD && git stash clear" &&
528 git reset --hard &&
529 echo foo > file &&
530 git stash &&
531 echo bar > file &&
532 git stash &&
533 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
534 git stash pop &&
535 test bar = "$(cat file)" &&
536 git reset --hard HEAD
537 '
539 test_expect_success 'ref with non-existent reflog' '
540 git stash clear &&
541 echo bar5 > file &&
542 echo bar6 > file2 &&
543 git add file2 &&
544 git stash &&
545 test_must_fail git rev-parse --quiet --verify does-not-exist &&
546 test_must_fail git stash drop does-not-exist &&
547 test_must_fail git stash drop does-not-exist@{0} &&
548 test_must_fail git stash pop does-not-exist &&
549 test_must_fail git stash pop does-not-exist@{0} &&
550 test_must_fail git stash apply does-not-exist &&
551 test_must_fail git stash apply does-not-exist@{0} &&
552 test_must_fail git stash show does-not-exist &&
553 test_must_fail git stash show does-not-exist@{0} &&
554 test_must_fail git stash branch tmp does-not-exist &&
555 test_must_fail git stash branch tmp does-not-exist@{0} &&
556 git stash drop
557 '
559 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
560 git stash clear &&
561 test_must_fail git stash drop stash@{0} &&
562 echo bar5 > file &&
563 echo bar6 > file2 &&
564 git add file2 &&
565 git stash &&
566 test_must_fail git stash drop stash@{1} &&
567 test_must_fail git stash pop stash@{1} &&
568 test_must_fail git stash apply stash@{1} &&
569 test_must_fail git stash show stash@{1} &&
570 test_must_fail git stash branch tmp stash@{1} &&
571 git stash drop
572 '
574 test_expect_success 'stash branch should not drop the stash if the branch exists' '
575 git stash clear &&
576 echo foo >file &&
577 git add file &&
578 git commit -m initial &&
579 echo bar >file &&
580 git stash &&
581 test_must_fail git stash branch master stash@{0} &&
582 git rev-parse stash@{0} --
583 '
585 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
586 git stash clear &&
587 echo 1 >subdir/subfile1 &&
588 echo 2 >subdir/subfile2 &&
589 git add subdir/subfile1 &&
590 git commit -m subdir &&
591 (
592 cd subdir &&
593 echo x >subfile1 &&
594 echo x >../file &&
595 git status >../expect &&
596 git stash &&
597 sane_unset GIT_MERGE_VERBOSITY &&
598 git stash apply
599 ) |
600 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
601 test_cmp expect actual
602 '
604 cat > expect << EOF
605 diff --git a/HEAD b/HEAD
606 new file mode 100644
607 index 0000000..fe0cbee
608 --- /dev/null
609 +++ b/HEAD
610 @@ -0,0 +1 @@
611 +file-not-a-ref
612 EOF
614 test_expect_success 'stash where working directory contains "HEAD" file' '
615 git stash clear &&
616 git reset --hard &&
617 echo file-not-a-ref > HEAD &&
618 git add HEAD &&
619 test_tick &&
620 git stash &&
621 git diff-files --quiet &&
622 git diff-index --cached --quiet HEAD &&
623 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
624 git diff stash^..stash > output &&
625 test_cmp output expect
626 '
628 test_done