Merge branch 'jk/maint-stash-oob' into next
[git/dscho.git] / t / t3903-stash.sh
bloba27d778624c1fe8ca39038f5913eb34a822b23a3
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 --invalid-option' '
240 echo bar5 > file &&
241 echo bar6 > file2 &&
242 git add file2 &&
243 test_must_fail git stash --invalid-option &&
244 test_must_fail git stash save --invalid-option &&
245 test bar5,bar6 = $(cat file),$(cat file2) &&
246 git stash -- -message-starting-with-dash &&
247 test bar,bar2 = $(cat file),$(cat file2)
250 test_expect_success 'stash an added file' '
251 git reset --hard &&
252 echo new >file3 &&
253 git add file3 &&
254 git stash save "added file" &&
255 ! test -r file3 &&
256 git stash apply &&
257 test new = "$(cat file3)"
260 test_expect_success 'stash rm then recreate' '
261 git reset --hard &&
262 git rm file &&
263 echo bar7 >file &&
264 git stash save "rm then recreate" &&
265 test bar = "$(cat file)" &&
266 git stash apply &&
267 test bar7 = "$(cat file)"
270 test_expect_success 'stash rm and ignore' '
271 git reset --hard &&
272 git rm file &&
273 echo file >.gitignore &&
274 git stash save "rm and ignore" &&
275 test bar = "$(cat file)" &&
276 test file = "$(cat .gitignore)" &&
277 git stash apply &&
278 ! test -r file &&
279 test file = "$(cat .gitignore)"
282 test_expect_success 'stash rm and ignore (stage .gitignore)' '
283 git reset --hard &&
284 git rm file &&
285 echo file >.gitignore &&
286 git add .gitignore &&
287 git stash save "rm and ignore (stage .gitignore)" &&
288 test bar = "$(cat file)" &&
289 ! test -r .gitignore &&
290 git stash apply &&
291 ! test -r file &&
292 test file = "$(cat .gitignore)"
295 test_expect_success SYMLINKS 'stash file to symlink' '
296 git reset --hard &&
297 rm file &&
298 ln -s file2 file &&
299 git stash save "file to symlink" &&
300 test -f file &&
301 test bar = "$(cat file)" &&
302 git stash apply &&
303 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
306 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
307 git reset --hard &&
308 git rm file &&
309 ln -s file2 file &&
310 git stash save "file to symlink (stage rm)" &&
311 test -f file &&
312 test bar = "$(cat file)" &&
313 git stash apply &&
314 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
317 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
318 git reset --hard &&
319 rm file &&
320 ln -s file2 file &&
321 git add file &&
322 git stash save "file to symlink (full stage)" &&
323 test -f file &&
324 test bar = "$(cat file)" &&
325 git stash apply &&
326 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
329 # This test creates a commit with a symlink used for the following tests
331 test_expect_success SYMLINKS 'stash symlink to file' '
332 git reset --hard &&
333 ln -s file filelink &&
334 git add filelink &&
335 git commit -m "Add symlink" &&
336 rm filelink &&
337 cp file filelink &&
338 git stash save "symlink to file" &&
339 test -h filelink &&
340 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
341 git stash apply &&
342 ! test -h filelink &&
343 test bar = "$(cat file)"
346 test_expect_success SYMLINKS 'stash symlink to file (stage rm)' '
347 git reset --hard &&
348 git rm filelink &&
349 cp file filelink &&
350 git stash save "symlink to file (stage rm)" &&
351 test -h filelink &&
352 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
353 git stash apply &&
354 ! test -h filelink &&
355 test bar = "$(cat file)"
358 test_expect_success SYMLINKS 'stash symlink to file (full stage)' '
359 git reset --hard &&
360 rm filelink &&
361 cp file filelink &&
362 git add filelink &&
363 git stash save "symlink to file (full stage)" &&
364 test -h filelink &&
365 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
366 git stash apply &&
367 ! test -h filelink &&
368 test bar = "$(cat file)"
371 test_expect_failure 'stash directory to file' '
372 git reset --hard &&
373 mkdir dir &&
374 echo foo >dir/file &&
375 git add dir/file &&
376 git commit -m "Add file in dir" &&
377 rm -fr dir &&
378 echo bar >dir &&
379 git stash save "directory to file" &&
380 test -d dir &&
381 test foo = "$(cat dir/file)" &&
382 test_must_fail git stash apply &&
383 test bar = "$(cat dir)" &&
384 git reset --soft HEAD^
387 test_expect_failure 'stash file to directory' '
388 git reset --hard &&
389 rm file &&
390 mkdir file &&
391 echo foo >file/file &&
392 git stash save "file to directory" &&
393 test -f file &&
394 test bar = "$(cat file)" &&
395 git stash apply &&
396 test -f file/file &&
397 test foo = "$(cat file/file)"
400 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
401 git stash clear &&
402 test_when_finished "git reset --hard HEAD" &&
403 git reset --hard &&
404 echo foo >> file &&
405 STASH_ID=$(git stash create) &&
406 git reset --hard &&
407 git stash branch stash-branch ${STASH_ID} &&
408 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
409 test $(git ls-files --modified | wc -l) -eq 1
412 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
413 git stash clear &&
414 test_when_finished "git reset --hard HEAD" &&
415 git reset --hard &&
416 echo foo >> file &&
417 git stash &&
418 test_when_finished "git stash drop" &&
419 echo bar >> file &&
420 STASH_ID=$(git stash create) &&
421 git reset --hard &&
422 git stash branch stash-branch ${STASH_ID} &&
423 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
424 test $(git ls-files --modified | wc -l) -eq 1
427 test_expect_success 'stash show - stashes on stack, stash-like argument' '
428 git stash clear &&
429 test_when_finished "git reset --hard HEAD" &&
430 git reset --hard &&
431 echo foo >> file &&
432 git stash &&
433 test_when_finished "git stash drop" &&
434 echo bar >> file &&
435 STASH_ID=$(git stash create) &&
436 git reset --hard &&
437 cat >expected <<-EOF &&
438 file | 1 +
439 1 files changed, 1 insertions(+), 0 deletions(-)
441 git stash show ${STASH_ID} >actual &&
442 test_cmp expected actual
445 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
446 git stash clear &&
447 test_when_finished "git reset --hard HEAD" &&
448 git reset --hard &&
449 echo foo >> file &&
450 git stash &&
451 test_when_finished "git stash drop" &&
452 echo bar >> file &&
453 STASH_ID=$(git stash create) &&
454 git reset --hard &&
455 cat >expected <<-EOF &&
456 diff --git a/file b/file
457 index 7601807..935fbd3 100644
458 --- a/file
459 +++ b/file
460 @@ -1 +1,2 @@
462 +bar
464 git stash show -p ${STASH_ID} >actual &&
465 test_cmp expected actual
468 test_expect_success 'stash show - no 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 STASH_ID=$(git stash create) &&
474 git reset --hard &&
475 cat >expected <<-EOF &&
476 file | 1 +
477 1 files changed, 1 insertions(+), 0 deletions(-)
479 git stash show ${STASH_ID} >actual &&
480 test_cmp expected actual
483 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
484 git stash clear &&
485 test_when_finished "git reset --hard HEAD" &&
486 git reset --hard &&
487 echo foo >> file &&
488 STASH_ID=$(git stash create) &&
489 git reset --hard &&
490 cat >expected <<-EOF &&
491 diff --git a/file b/file
492 index 7601807..71b52c4 100644
493 --- a/file
494 +++ b/file
495 @@ -1 +1,2 @@
497 +foo
499 git stash show -p ${STASH_ID} >actual &&
500 test_cmp expected actual
503 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
504 git stash clear &&
505 test_when_finished "git reset --hard HEAD && git stash clear" &&
506 git reset --hard &&
507 echo foo > file &&
508 git stash &&
509 echo bar > file &&
510 git stash &&
511 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
512 git stash pop &&
513 test bar = "$(cat file)" &&
514 git reset --hard HEAD
517 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
518 git stash clear &&
519 test_when_finished "git reset --hard HEAD && git stash clear" &&
520 git reset --hard &&
521 echo foo > file &&
522 git stash &&
523 echo bar > file &&
524 git stash &&
525 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
526 git stash pop &&
527 test bar = "$(cat file)" &&
528 git reset --hard HEAD
531 test_expect_success 'ref with non-existant reflog' '
532 git stash clear &&
533 echo bar5 > file &&
534 echo bar6 > file2 &&
535 git add file2 &&
536 git stash &&
537 ! "git rev-parse --quiet --verify does-not-exist" &&
538 test_must_fail git stash drop does-not-exist &&
539 test_must_fail git stash drop does-not-exist@{0} &&
540 test_must_fail git stash pop does-not-exist &&
541 test_must_fail git stash pop does-not-exist@{0} &&
542 test_must_fail git stash apply does-not-exist &&
543 test_must_fail git stash apply does-not-exist@{0} &&
544 test_must_fail git stash show does-not-exist &&
545 test_must_fail git stash show does-not-exist@{0} &&
546 test_must_fail git stash branch tmp does-not-exist &&
547 test_must_fail git stash branch tmp does-not-exist@{0} &&
548 git stash drop
551 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
552 git stash clear &&
553 test_must_fail git stash drop stash@{0} &&
554 echo bar5 > file &&
555 echo bar6 > file2 &&
556 git add file2 &&
557 git stash &&
558 test_must_fail git stash drop stash@{1} &&
559 test_must_fail git stash pop stash@{1} &&
560 test_must_fail git stash apply stash@{1} &&
561 test_must_fail git stash show stash@{1} &&
562 test_must_fail git stash branch tmp stash@{1} &&
563 git stash drop
566 test_expect_success 'stash branch should not drop the stash if the branch exists' '
567 git stash clear &&
568 echo foo >file &&
569 git add file &&
570 git commit -m initial &&
571 echo bar >file &&
572 git stash &&
573 test_must_fail git stash branch master stash@{0} &&
574 git rev-parse stash@{0} --
577 test_expect_success 'stash apply shows status same as git status (relative to current directory)' '
578 git stash clear &&
579 echo 1 >subdir/subfile1 &&
580 echo 2 >subdir/subfile2 &&
581 git add subdir/subfile1 &&
582 git commit -m subdir &&
584 cd subdir &&
585 echo x >subfile1 &&
586 echo x >../file &&
587 git status >../expect &&
588 git stash &&
589 sane_unset GIT_MERGE_VERBOSITY &&
590 git stash apply
592 sed -e 1,2d >actual && # drop "Saved..." and "HEAD is now..."
593 test_cmp expect actual
596 test_done