Merge branch 'master' of git://repo.or.cz/alt-git
[git/mingw.git] / t / t3903-stash.sh
blob430072f960bd16b1e48bcce7d9cec791097ead5b
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 'apply needs clean working directory' '
41 echo 4 > other-file &&
42 git add other-file &&
43 echo 5 > other-file &&
44 test_must_fail git stash apply
47 test_expect_success 'apply stashed changes' '
48 git add other-file &&
49 test_tick &&
50 git commit -m other-file &&
51 git stash apply &&
52 test 3 = $(cat file) &&
53 test 1 = $(git show :file) &&
54 test 1 = $(git show HEAD:file)
57 test_expect_success 'apply stashed changes (including index)' '
58 git reset --hard HEAD^ &&
59 echo 6 > other-file &&
60 git add other-file &&
61 test_tick &&
62 git commit -m other-file &&
63 git stash apply --index &&
64 test 3 = $(cat file) &&
65 test 2 = $(git show :file) &&
66 test 1 = $(git show HEAD:file)
69 test_expect_success 'unstashing in a subdirectory' '
70 git reset --hard HEAD &&
71 mkdir subdir &&
73 cd subdir &&
74 git stash apply
78 test_expect_success 'drop top stash' '
79 git reset --hard &&
80 git stash list > stashlist1 &&
81 echo 7 > file &&
82 git stash &&
83 git stash drop &&
84 git stash list > stashlist2 &&
85 test_cmp stashlist1 stashlist2 &&
86 git stash apply &&
87 test 3 = $(cat file) &&
88 test 1 = $(git show :file) &&
89 test 1 = $(git show HEAD:file)
92 test_expect_success 'drop middle stash' '
93 git reset --hard &&
94 echo 8 > file &&
95 git stash &&
96 sleep 1 &&
97 echo 9 > file &&
98 git stash &&
99 git stash drop stash@{1} &&
100 test 2 = $(git stash list | wc -l) &&
101 git stash apply &&
102 test 9 = $(cat file) &&
103 test 1 = $(git show :file) &&
104 test 1 = $(git show HEAD:file) &&
105 git reset --hard &&
106 git stash drop &&
107 git stash apply &&
108 test 3 = $(cat file) &&
109 test 1 = $(git show :file) &&
110 test 1 = $(git show HEAD:file)
113 test_expect_success 'stash pop' '
114 git reset --hard &&
115 git stash pop &&
116 test 3 = $(cat file) &&
117 test 1 = $(git show :file) &&
118 test 1 = $(git show HEAD:file) &&
119 test 0 = $(git stash list | wc -l)
122 cat > expect << EOF
123 diff --git a/file2 b/file2
124 new file mode 100644
125 index 0000000..1fe912c
126 --- /dev/null
127 +++ b/file2
128 @@ -0,0 +1 @@
129 +bar2
132 cat > expect1 << EOF
133 diff --git a/file b/file
134 index 257cc56..5716ca5 100644
135 --- a/file
136 +++ b/file
137 @@ -1 +1 @@
138 -foo
139 +bar
142 cat > expect2 << EOF
143 diff --git a/file b/file
144 index 7601807..5716ca5 100644
145 --- a/file
146 +++ b/file
147 @@ -1 +1 @@
148 -baz
149 +bar
150 diff --git a/file2 b/file2
151 new file mode 100644
152 index 0000000..1fe912c
153 --- /dev/null
154 +++ b/file2
155 @@ -0,0 +1 @@
156 +bar2
159 test_expect_success 'stash branch' '
160 echo foo > file &&
161 git commit file -m first
162 echo bar > file &&
163 echo bar2 > file2 &&
164 git add file2 &&
165 git stash &&
166 echo baz > file &&
167 git commit file -m second &&
168 git stash branch stashbranch &&
169 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
170 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
171 git diff --cached > output &&
172 test_cmp output expect &&
173 git diff > output &&
174 test_cmp output expect1 &&
175 git add file &&
176 git commit -m alternate\ second &&
177 git diff master..stashbranch > output &&
178 test_cmp output expect2 &&
179 test 0 = $(git stash list | wc -l)
182 test_expect_success 'apply -q is quiet' '
183 echo foo > file &&
184 git stash &&
185 git stash apply -q > output.out 2>&1 &&
186 test ! -s output.out
189 test_expect_success 'save -q is quiet' '
190 git stash save --quiet > output.out 2>&1 &&
191 test ! -s output.out
194 test_expect_success 'pop -q is quiet' '
195 git stash pop -q > output.out 2>&1 &&
196 test ! -s output.out
199 test_expect_success 'pop -q --index works and is quiet' '
200 echo foo > file &&
201 git add file &&
202 git stash save --quiet &&
203 git stash pop -q --index > output.out 2>&1 &&
204 test foo = "$(git show :file)" &&
205 test ! -s output.out
208 test_expect_success 'drop -q is quiet' '
209 git stash &&
210 git stash drop -q > output.out 2>&1 &&
211 test ! -s output.out
214 test_expect_success 'stash -k' '
215 echo bar3 > file &&
216 echo bar4 > file2 &&
217 git add file2 &&
218 git stash -k &&
219 test bar,bar4 = $(cat file),$(cat file2)
222 test_expect_success 'stash --invalid-option' '
223 echo bar5 > file &&
224 echo bar6 > file2 &&
225 git add file2 &&
226 test_must_fail git stash --invalid-option &&
227 test_must_fail git stash save --invalid-option &&
228 test bar5,bar6 = $(cat file),$(cat file2) &&
229 git stash -- -message-starting-with-dash &&
230 test bar,bar2 = $(cat file),$(cat file2)
233 test_expect_success 'stash an added file' '
234 git reset --hard &&
235 echo new >file3 &&
236 git add file3 &&
237 git stash save "added file" &&
238 ! test -r file3 &&
239 git stash apply &&
240 test new = "$(cat file3)"
243 test_expect_success 'stash rm then recreate' '
244 git reset --hard &&
245 git rm file &&
246 echo bar7 >file &&
247 git stash save "rm then recreate" &&
248 test bar = "$(cat file)" &&
249 git stash apply &&
250 test bar7 = "$(cat file)"
253 test_expect_success 'stash rm and ignore' '
254 git reset --hard &&
255 git rm file &&
256 echo file >.gitignore &&
257 git stash save "rm and ignore" &&
258 test bar = "$(cat file)" &&
259 test file = "$(cat .gitignore)"
260 git stash apply &&
261 ! test -r file &&
262 test file = "$(cat .gitignore)"
265 test_expect_success 'stash rm and ignore (stage .gitignore)' '
266 git reset --hard &&
267 git rm file &&
268 echo file >.gitignore &&
269 git add .gitignore &&
270 git stash save "rm and ignore (stage .gitignore)" &&
271 test bar = "$(cat file)" &&
272 ! test -r .gitignore
273 git stash apply &&
274 ! test -r file &&
275 test file = "$(cat .gitignore)"
278 test_expect_success SYMLINKS 'stash file to symlink' '
279 git reset --hard &&
280 rm file &&
281 ln -s file2 file &&
282 git stash save "file to symlink" &&
283 test -f file &&
284 test bar = "$(cat file)" &&
285 git stash apply &&
286 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
289 test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
290 git reset --hard &&
291 git rm file &&
292 ln -s file2 file &&
293 git stash save "file to symlink (stage rm)" &&
294 test -f file &&
295 test bar = "$(cat file)" &&
296 git stash apply &&
297 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
300 test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
301 git reset --hard &&
302 rm file &&
303 ln -s file2 file &&
304 git add file &&
305 git stash save "file to symlink (full stage)" &&
306 test -f file &&
307 test bar = "$(cat file)" &&
308 git stash apply &&
309 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
312 # This test creates a commit with a symlink used for the following tests
314 test_expect_success SYMLINKS 'stash symlink to file' '
315 git reset --hard &&
316 ln -s file filelink &&
317 git add filelink &&
318 git commit -m "Add symlink" &&
319 rm filelink &&
320 cp file filelink &&
321 git stash save "symlink to file" &&
322 test -h filelink &&
323 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
324 git stash apply &&
325 ! test -h filelink &&
326 test bar = "$(cat file)"
329 test_expect_success SYMLINKS 'stash symlink to file (stage rm)' '
330 git reset --hard &&
331 git rm filelink &&
332 cp file filelink &&
333 git stash save "symlink to file (stage rm)" &&
334 test -h filelink &&
335 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
336 git stash apply &&
337 ! test -h filelink &&
338 test bar = "$(cat file)"
341 test_expect_success SYMLINKS 'stash symlink to file (full stage)' '
342 git reset --hard &&
343 rm filelink &&
344 cp file filelink &&
345 git add filelink &&
346 git stash save "symlink to file (full stage)" &&
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_failure 'stash directory to file' '
355 git reset --hard &&
356 mkdir dir &&
357 echo foo >dir/file &&
358 git add dir/file &&
359 git commit -m "Add file in dir" &&
360 rm -fr dir &&
361 echo bar >dir &&
362 git stash save "directory to file" &&
363 test -d dir &&
364 test foo = "$(cat dir/file)" &&
365 test_must_fail git stash apply &&
366 test bar = "$(cat dir)" &&
367 git reset --soft HEAD^
370 test_expect_failure 'stash file to directory' '
371 git reset --hard &&
372 rm file &&
373 mkdir file &&
374 echo foo >file/file &&
375 git stash save "file to directory" &&
376 test -f file &&
377 test bar = "$(cat file)" &&
378 git stash apply &&
379 test -f file/file &&
380 test foo = "$(cat file/file)"
383 test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
384 git stash clear &&
385 test_when_finished "git reset --hard HEAD" &&
386 git reset --hard &&
387 echo foo >> file &&
388 STASH_ID=$(git stash create) &&
389 git reset --hard &&
390 git stash branch stash-branch ${STASH_ID} &&
391 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
392 test $(git ls-files --modified | wc -l) -eq 1
395 test_expect_success 'stash branch - stashes on stack, stash-like argument' '
396 git stash clear &&
397 test_when_finished "git reset --hard HEAD" &&
398 git reset --hard &&
399 echo foo >> file &&
400 git stash &&
401 test_when_finished "git stash drop" &&
402 echo bar >> file &&
403 STASH_ID=$(git stash create) &&
404 git reset --hard &&
405 git stash branch stash-branch ${STASH_ID} &&
406 test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
407 test $(git ls-files --modified | wc -l) -eq 1
410 test_expect_success 'stash show - stashes on stack, stash-like argument' '
411 git stash clear &&
412 test_when_finished "git reset --hard HEAD" &&
413 git reset --hard &&
414 echo foo >> file &&
415 git stash &&
416 test_when_finished "git stash drop" &&
417 echo bar >> file &&
418 STASH_ID=$(git stash create) &&
419 git reset --hard &&
420 cat >expected <<-EOF &&
421 file | 1 +
422 1 files changed, 1 insertions(+), 0 deletions(-)
424 git stash show ${STASH_ID} >actual &&
425 test_cmp expected actual
428 test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
429 git stash clear &&
430 test_when_finished "git reset --hard HEAD" &&
431 git reset --hard &&
432 echo foo >> file &&
433 git stash &&
434 test_when_finished "git stash drop" &&
435 echo bar >> file &&
436 STASH_ID=$(git stash create) &&
437 git reset --hard &&
438 cat >expected <<-EOF &&
439 diff --git a/file b/file
440 index 7601807..935fbd3 100644
441 --- a/file
442 +++ b/file
443 @@ -1 +1,2 @@
445 +bar
447 git stash show -p ${STASH_ID} >actual &&
448 test_cmp expected actual
451 test_expect_success 'stash show - no stashes on stack, stash-like argument' '
452 git stash clear &&
453 test_when_finished "git reset --hard HEAD" &&
454 git reset --hard &&
455 echo foo >> file &&
456 STASH_ID=$(git stash create) &&
457 git reset --hard &&
458 cat >expected <<-EOF &&
459 file | 1 +
460 1 files changed, 1 insertions(+), 0 deletions(-)
462 git stash show ${STASH_ID} >actual &&
463 test_cmp expected actual
466 test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
467 git stash clear &&
468 test_when_finished "git reset --hard HEAD" &&
469 git reset --hard &&
470 echo foo >> file &&
471 STASH_ID=$(git stash create) &&
472 git reset --hard &&
473 cat >expected <<-EOF &&
474 diff --git a/file b/file
475 index 7601807..71b52c4 100644
476 --- a/file
477 +++ b/file
478 @@ -1 +1,2 @@
480 +foo
482 git stash show -p ${STASH_ID} >actual &&
483 test_cmp expected actual
486 test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
487 git stash clear &&
488 test_when_finished "git reset --hard HEAD && git stash clear" &&
489 git reset --hard &&
490 echo foo > file &&
491 git stash &&
492 echo bar > file &&
493 git stash &&
494 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
495 git stash pop &&
496 test bar = "$(cat file)" &&
497 git reset --hard HEAD
500 test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
501 git stash clear &&
502 test_when_finished "git reset --hard HEAD && git stash clear" &&
503 git reset --hard &&
504 echo foo > file &&
505 git stash &&
506 echo bar > file &&
507 git stash &&
508 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
509 git stash pop &&
510 test bar = "$(cat file)" &&
511 git reset --hard HEAD
514 test_expect_success 'ref with non-existant reflog' '
515 git stash clear &&
516 echo bar5 > file &&
517 echo bar6 > file2 &&
518 git add file2 &&
519 git stash &&
520 ! "git rev-parse --quiet --verify does-not-exist" &&
521 test_must_fail git stash drop does-not-exist &&
522 test_must_fail git stash drop does-not-exist@{0} &&
523 test_must_fail git stash pop does-not-exist &&
524 test_must_fail git stash pop does-not-exist@{0} &&
525 test_must_fail git stash apply does-not-exist &&
526 test_must_fail git stash apply does-not-exist@{0} &&
527 test_must_fail git stash show does-not-exist &&
528 test_must_fail git stash show does-not-exist@{0} &&
529 test_must_fail git stash branch tmp does-not-exist &&
530 test_must_fail git stash branch tmp does-not-exist@{0} &&
531 git stash drop
534 test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
535 git stash clear &&
536 test_must_fail git stash drop stash@{0} &&
537 echo bar5 > file &&
538 echo bar6 > file2 &&
539 git add file2 &&
540 git stash &&
541 test_must_fail git drop stash@{1} &&
542 test_must_fail git pop stash@{1} &&
543 test_must_fail git apply stash@{1} &&
544 test_must_fail git show stash@{1} &&
545 test_must_fail git branch tmp stash@{1} &&
546 git stash drop
549 test_expect_success 'stash branch should not drop the stash if the branch exists' '
550 git stash clear &&
551 echo foo >file &&
552 git add file &&
553 git commit -m initial &&
554 echo bar >file &&
555 git stash &&
556 test_must_fail git stash branch master stash@{0} &&
557 git rev-parse stash@{0} --
560 test_done