Fix typo: existant->existent
[git/git.git] / t / t3903-stash.sh
CommitLineData
150937c4
JS
1#!/bin/sh
2#
3# Copyright (c) 2007 Johannes E Schindelin
4#
5
0cb0e143 6test_description='Test git stash'
150937c4
JS
7
8. ./test-lib.sh
9
10test_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'
23
24cat > expect << EOF
25diff --git a/file b/file
26index 0cfbf08..00750ed 100644
27--- a/file
28+++ b/file
29@@ -1 +1 @@
30-2
31+3
32EOF
33
34test_expect_success 'parents of stash' '
35 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
36 git diff stash^2..stash > output &&
82ebb0b6 37 test_cmp output expect
150937c4
JS
38'
39
59d418fe
JK
40test_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'
45
e0e2a9cb
JK
46test_expect_success 'apply does not need clean working directory' '
47 echo 4 >other-file &&
150937c4 48 git add other-file &&
e0e2a9cb
JK
49 echo 5 >other-file &&
50 git stash apply &&
51 echo 3 >expect &&
52 test_cmp expect file
53'
54
55test_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
150937c4
JS
61'
62
63test_expect_success 'apply stashed changes' '
e0e2a9cb
JK
64 git reset --hard &&
65 echo 5 >other-file &&
150937c4
JS
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'
74
75test_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'
86
ceff079b
JH
87test_expect_success 'unstashing in a subdirectory' '
88 git reset --hard HEAD &&
89 mkdir subdir &&
18a82692
JN
90 (
91 cd subdir &&
92 git stash apply
fd4ec4f2 93 )
b683c080
BC
94'
95
96test_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 &&
4fdf71be 103 test_cmp stashlist1 stashlist2 &&
b683c080
BC
104 git stash apply &&
105 test 3 = $(cat file) &&
106 test 1 = $(git show :file) &&
107 test 1 = $(git show HEAD:file)
108'
109
110test_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'
129
130test_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)
ceff079b
JH
137'
138
4a588075
AMS
139cat > expect << EOF
140diff --git a/file2 b/file2
141new file mode 100644
142index 0000000..1fe912c
143--- /dev/null
144+++ b/file2
145@@ -0,0 +1 @@
146+bar2
147EOF
148
149cat > expect1 << EOF
150diff --git a/file b/file
151index 257cc56..5716ca5 100644
152--- a/file
153+++ b/file
154@@ -1 +1 @@
155-foo
156+bar
157EOF
158
159cat > expect2 << EOF
160diff --git a/file b/file
161index 7601807..5716ca5 100644
162--- a/file
163+++ b/file
164@@ -1 +1 @@
165-baz
166+bar
167diff --git a/file2 b/file2
168new file mode 100644
169index 0000000..1fe912c
170--- /dev/null
171+++ b/file2
172@@ -0,0 +1 @@
173+bar2
174EOF
175
176test_expect_success 'stash branch' '
177 echo foo > file &&
a48fcd83 178 git commit file -m first &&
4a588075
AMS
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'
198
fcdd0e92
SB
199test_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'
205
206test_expect_success 'save -q is quiet' '
207 git stash save --quiet > output.out 2>&1 &&
208 test ! -s output.out
209'
210
211test_expect_success 'pop -q is quiet' '
212 git stash pop -q > output.out 2>&1 &&
213 test ! -s output.out
214'
215
460ccd0e
TR
216test_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'
224
fcdd0e92
SB
225test_expect_success 'drop -q is quiet' '
226 git stash &&
227 git stash drop -q > output.out 2>&1 &&
228 test ! -s output.out
229'
230
ea41cfc4
JS
231test_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'
238
21ec98a7
DM
239test_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'
246
3c2eb80f
MM
247test_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'
257
2ba2fe29
CB
258test_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'
267
268test_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'
277
278test_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)" &&
a48fcd83 284 test file = "$(cat .gitignore)" &&
2ba2fe29
CB
285 git stash apply &&
286 ! test -r file &&
287 test file = "$(cat .gitignore)"
288'
289
290test_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)" &&
a48fcd83 297 ! test -r .gitignore &&
2ba2fe29
CB
298 git stash apply &&
299 ! test -r file &&
300 test file = "$(cat .gitignore)"
301'
302
303test_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'
313
314test_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'
324
325test_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'
336
337# This test creates a commit with a symlink used for the following tests
338
339test_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'
353
354test_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'
365
366test_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'
378
379test_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'
394
395test_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'
407
daf7a0c0
JS
408test_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'
419
420test_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'
434
9027fa9e 435test_expect_success 'stash show - stashes on stack, stash-like argument' '
daf7a0c0
JS
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 &&
3fcb8878
BC
445 cat >expected <<-EOF &&
446 file | 1 +
447 1 files changed, 1 insertions(+), 0 deletions(-)
448 EOF
449 git stash show ${STASH_ID} >actual &&
450 test_cmp expected actual
daf7a0c0 451'
3fcb8878 452
9027fa9e 453test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
3fcb8878
BC
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'
475
9027fa9e 476test_expect_success 'stash show - no stashes on stack, stash-like argument' '
3fcb8878
BC
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 files changed, 1 insertions(+), 0 deletions(-)
486 EOF
487 git stash show ${STASH_ID} >actual &&
488 test_cmp expected actual
489'
490
9027fa9e 491test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
daf7a0c0
JS
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 &&
3fcb8878
BC
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
daf7a0c0
JS
509'
510
511test_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 &&
8d66bb05 519 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
daf7a0c0
JS
520 git stash pop &&
521 test bar = "$(cat file)" &&
522 git reset --hard HEAD
523'
524
525test_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 &&
8d66bb05 533 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
daf7a0c0
JS
534 git stash pop &&
535 test bar = "$(cat file)" &&
536 git reset --hard HEAD
537'
538
7be8b3ba 539test_expect_success 'ref with non-existent reflog' '
daf7a0c0
JS
540 git stash clear &&
541 echo bar5 > file &&
542 echo bar6 > file2 &&
543 git add file2 &&
544 git stash &&
545 ! "git rev-parse --quiet --verify does-not-exist" &&
8d66bb05
JS
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} &&
daf7a0c0
JS
556 git stash drop
557'
558
559test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
560 git stash clear &&
8d66bb05 561 test_must_fail git stash drop stash@{0} &&
daf7a0c0
JS
562 echo bar5 > file &&
563 echo bar6 > file2 &&
564 git add file2 &&
565 git stash &&
9355fc9c
JS
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} &&
daf7a0c0
JS
571 git stash drop
572'
573
57693d03 574test_expect_success 'stash branch should not drop the stash if the branch exists' '
835d6a1f
TC
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'
584
6eaf92f3
PK
585test_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'
603
150937c4 604test_done