]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3903-stash.sh
stash: get git_stash_config at the top level
[thirdparty/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
c7848150 10diff_cmp () {
11 for i in "$1" "$2"
12 do
13 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
14 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
15 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
16 "$i" >"$i.compare" || return 1
17 done &&
18 test_cmp "$1.compare" "$2.compare" &&
19 rm -f "$1.compare" "$2.compare"
20}
21
150937c4 22test_expect_success 'stash some dirty working directory' '
6a0b88b2 23 echo 1 >file &&
150937c4 24 git add file &&
88bab59c
JK
25 echo unrelated >other-file &&
26 git add other-file &&
150937c4
JS
27 test_tick &&
28 git commit -m initial &&
6a0b88b2 29 echo 2 >file &&
150937c4 30 git add file &&
6a0b88b2 31 echo 3 >file &&
150937c4
JS
32 test_tick &&
33 git stash &&
34 git diff-files --quiet &&
35 git diff-index --cached --quiet HEAD
36'
37
6a0b88b2 38cat >expect <<EOF
150937c4
JS
39diff --git a/file b/file
40index 0cfbf08..00750ed 100644
41--- a/file
42+++ b/file
43@@ -1 +1 @@
44-2
45+3
46EOF
47
48test_expect_success 'parents of stash' '
49 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
6a0b88b2 50 git diff stash^2..stash >output &&
c7848150 51 diff_cmp expect output
150937c4
JS
52'
53
59d418fe
JK
54test_expect_success 'applying bogus stash does nothing' '
55 test_must_fail git stash apply stash@{1} &&
56 echo 1 >expect &&
57 test_cmp expect file
58'
59
e0e2a9cb
JK
60test_expect_success 'apply does not need clean working directory' '
61 echo 4 >other-file &&
e0e2a9cb
JK
62 git stash apply &&
63 echo 3 >expect &&
64 test_cmp expect file
65'
66
67test_expect_success 'apply does not clobber working directory changes' '
68 git reset --hard &&
69 echo 4 >file &&
70 test_must_fail git stash apply &&
71 echo 4 >expect &&
72 test_cmp expect file
150937c4
JS
73'
74
75test_expect_success 'apply stashed changes' '
e0e2a9cb
JK
76 git reset --hard &&
77 echo 5 >other-file &&
150937c4
JS
78 git add other-file &&
79 test_tick &&
80 git commit -m other-file &&
81 git stash apply &&
82 test 3 = $(cat file) &&
83 test 1 = $(git show :file) &&
84 test 1 = $(git show HEAD:file)
85'
86
87test_expect_success 'apply stashed changes (including index)' '
88 git reset --hard HEAD^ &&
6a0b88b2 89 echo 6 >other-file &&
150937c4
JS
90 git add other-file &&
91 test_tick &&
92 git commit -m other-file &&
93 git stash apply --index &&
94 test 3 = $(cat file) &&
95 test 2 = $(git show :file) &&
96 test 1 = $(git show HEAD:file)
97'
98
ceff079b
JH
99test_expect_success 'unstashing in a subdirectory' '
100 git reset --hard HEAD &&
101 mkdir subdir &&
18a82692
JN
102 (
103 cd subdir &&
104 git stash apply
fd4ec4f2 105 )
b683c080
BC
106'
107
d6cc2df5
JK
108test_expect_success 'stash drop complains of extra options' '
109 test_must_fail git stash drop --foo
110'
111
b683c080
BC
112test_expect_success 'drop top stash' '
113 git reset --hard &&
6a0b88b2
PSU
114 git stash list >expected &&
115 echo 7 >file &&
b683c080
BC
116 git stash &&
117 git stash drop &&
6a0b88b2
PSU
118 git stash list >actual &&
119 test_cmp expected actual &&
b683c080
BC
120 git stash apply &&
121 test 3 = $(cat file) &&
122 test 1 = $(git show :file) &&
123 test 1 = $(git show HEAD:file)
124'
125
126test_expect_success 'drop middle stash' '
127 git reset --hard &&
6a0b88b2 128 echo 8 >file &&
b683c080 129 git stash &&
6a0b88b2 130 echo 9 >file &&
b683c080
BC
131 git stash &&
132 git stash drop stash@{1} &&
133 test 2 = $(git stash list | wc -l) &&
134 git stash apply &&
135 test 9 = $(cat file) &&
136 test 1 = $(git show :file) &&
137 test 1 = $(git show HEAD:file) &&
138 git reset --hard &&
139 git stash drop &&
140 git stash apply &&
141 test 3 = $(cat file) &&
142 test 1 = $(git show :file) &&
143 test 1 = $(git show HEAD:file)
144'
145
a56c8f5a
AW
146test_expect_success 'drop middle stash by index' '
147 git reset --hard &&
148 echo 8 >file &&
149 git stash &&
150 echo 9 >file &&
151 git stash &&
152 git stash drop 1 &&
153 test 2 = $(git stash list | wc -l) &&
154 git stash apply &&
155 test 9 = $(cat file) &&
156 test 1 = $(git show :file) &&
157 test 1 = $(git show HEAD:file) &&
158 git reset --hard &&
159 git stash drop &&
160 git stash apply &&
161 test 3 = $(cat file) &&
162 test 1 = $(git show :file) &&
163 test 1 = $(git show HEAD:file)
164'
165
b683c080
BC
166test_expect_success 'stash pop' '
167 git reset --hard &&
168 git stash pop &&
169 test 3 = $(cat file) &&
170 test 1 = $(git show :file) &&
171 test 1 = $(git show HEAD:file) &&
172 test 0 = $(git stash list | wc -l)
ceff079b
JH
173'
174
6a0b88b2 175cat >expect <<EOF
4a588075
AMS
176diff --git a/file2 b/file2
177new file mode 100644
178index 0000000..1fe912c
179--- /dev/null
180+++ b/file2
181@@ -0,0 +1 @@
182+bar2
183EOF
184
6a0b88b2 185cat >expect1 <<EOF
4a588075
AMS
186diff --git a/file b/file
187index 257cc56..5716ca5 100644
188--- a/file
189+++ b/file
190@@ -1 +1 @@
191-foo
192+bar
193EOF
194
6a0b88b2 195cat >expect2 <<EOF
4a588075
AMS
196diff --git a/file b/file
197index 7601807..5716ca5 100644
198--- a/file
199+++ b/file
200@@ -1 +1 @@
201-baz
202+bar
203diff --git a/file2 b/file2
204new file mode 100644
205index 0000000..1fe912c
206--- /dev/null
207+++ b/file2
208@@ -0,0 +1 @@
209+bar2
210EOF
211
212test_expect_success 'stash branch' '
6a0b88b2 213 echo foo >file &&
a48fcd83 214 git commit file -m first &&
6a0b88b2
PSU
215 echo bar >file &&
216 echo bar2 >file2 &&
4a588075
AMS
217 git add file2 &&
218 git stash &&
6a0b88b2 219 echo baz >file &&
4a588075
AMS
220 git commit file -m second &&
221 git stash branch stashbranch &&
222 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
223 test $(git rev-parse HEAD) = $(git rev-parse master^) &&
6a0b88b2 224 git diff --cached >output &&
c7848150 225 diff_cmp expect output &&
6a0b88b2 226 git diff >output &&
c7848150 227 diff_cmp expect1 output &&
4a588075
AMS
228 git add file &&
229 git commit -m alternate\ second &&
6a0b88b2 230 git diff master..stashbranch >output &&
c7848150 231 diff_cmp output expect2 &&
4a588075
AMS
232 test 0 = $(git stash list | wc -l)
233'
234
fcdd0e92 235test_expect_success 'apply -q is quiet' '
6a0b88b2 236 echo foo >file &&
fcdd0e92 237 git stash &&
6a0b88b2 238 git stash apply -q >output.out 2>&1 &&
ca8d148d 239 test_must_be_empty output.out
fcdd0e92
SB
240'
241
242test_expect_success 'save -q is quiet' '
6a0b88b2 243 git stash save --quiet >output.out 2>&1 &&
ca8d148d 244 test_must_be_empty output.out
fcdd0e92
SB
245'
246
df53c808 247test_expect_success 'pop -q works and is quiet' '
6a0b88b2 248 git stash pop -q >output.out 2>&1 &&
df53c808
TG
249 echo bar >expect &&
250 git show :file >actual &&
251 test_cmp expect actual &&
ca8d148d 252 test_must_be_empty output.out
fcdd0e92
SB
253'
254
460ccd0e 255test_expect_success 'pop -q --index works and is quiet' '
6a0b88b2 256 echo foo >file &&
460ccd0e
TR
257 git add file &&
258 git stash save --quiet &&
6a0b88b2 259 git stash pop -q --index >output.out 2>&1 &&
df53c808
TG
260 git diff-files file2 >file2.diff &&
261 test_must_be_empty file2.diff &&
460ccd0e 262 test foo = "$(git show :file)" &&
ca8d148d 263 test_must_be_empty output.out
460ccd0e
TR
264'
265
fcdd0e92
SB
266test_expect_success 'drop -q is quiet' '
267 git stash &&
6a0b88b2 268 git stash drop -q >output.out 2>&1 &&
ca8d148d 269 test_must_be_empty output.out
fcdd0e92
SB
270'
271
ea41cfc4 272test_expect_success 'stash -k' '
6a0b88b2
PSU
273 echo bar3 >file &&
274 echo bar4 >file2 &&
ea41cfc4
JS
275 git add file2 &&
276 git stash -k &&
277 test bar,bar4 = $(cat file),$(cat file2)
278'
279
21ec98a7 280test_expect_success 'stash --no-keep-index' '
6a0b88b2
PSU
281 echo bar33 >file &&
282 echo bar44 >file2 &&
21ec98a7
DM
283 git add file2 &&
284 git stash --no-keep-index &&
285 test bar,bar2 = $(cat file),$(cat file2)
286'
287
3c2eb80f 288test_expect_success 'stash --invalid-option' '
6a0b88b2
PSU
289 echo bar5 >file &&
290 echo bar6 >file2 &&
3c2eb80f
MM
291 git add file2 &&
292 test_must_fail git stash --invalid-option &&
293 test_must_fail git stash save --invalid-option &&
1ada5020 294 test bar5,bar6 = $(cat file),$(cat file2)
3c2eb80f
MM
295'
296
2ba2fe29
CB
297test_expect_success 'stash an added file' '
298 git reset --hard &&
299 echo new >file3 &&
300 git add file3 &&
301 git stash save "added file" &&
302 ! test -r file3 &&
303 git stash apply &&
304 test new = "$(cat file3)"
305'
306
b6e531e6
MK
307test_expect_success 'stash --intent-to-add file' '
308 git reset --hard &&
309 echo new >file4 &&
310 git add --intent-to-add file4 &&
311 test_when_finished "git rm -f file4" &&
312 test_must_fail git stash
313'
314
2ba2fe29
CB
315test_expect_success 'stash rm then recreate' '
316 git reset --hard &&
317 git rm file &&
318 echo bar7 >file &&
319 git stash save "rm then recreate" &&
320 test bar = "$(cat file)" &&
321 git stash apply &&
322 test bar7 = "$(cat file)"
323'
324
325test_expect_success 'stash rm and ignore' '
326 git reset --hard &&
327 git rm file &&
328 echo file >.gitignore &&
329 git stash save "rm and ignore" &&
330 test bar = "$(cat file)" &&
a48fcd83 331 test file = "$(cat .gitignore)" &&
2ba2fe29
CB
332 git stash apply &&
333 ! test -r file &&
334 test file = "$(cat .gitignore)"
335'
336
337test_expect_success 'stash rm and ignore (stage .gitignore)' '
338 git reset --hard &&
339 git rm file &&
340 echo file >.gitignore &&
341 git add .gitignore &&
342 git stash save "rm and ignore (stage .gitignore)" &&
343 test bar = "$(cat file)" &&
a48fcd83 344 ! test -r .gitignore &&
2ba2fe29
CB
345 git stash apply &&
346 ! test -r file &&
347 test file = "$(cat .gitignore)"
348'
349
350test_expect_success SYMLINKS 'stash file to symlink' '
351 git reset --hard &&
352 rm file &&
353 ln -s file2 file &&
354 git stash save "file to symlink" &&
355 test -f file &&
356 test bar = "$(cat file)" &&
357 git stash apply &&
358 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
359'
360
361test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
362 git reset --hard &&
363 git rm file &&
364 ln -s file2 file &&
365 git stash save "file to symlink (stage rm)" &&
366 test -f file &&
367 test bar = "$(cat file)" &&
368 git stash apply &&
369 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
370'
371
372test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
373 git reset --hard &&
374 rm file &&
375 ln -s file2 file &&
376 git add file &&
377 git stash save "file to symlink (full stage)" &&
378 test -f file &&
379 test bar = "$(cat file)" &&
380 git stash apply &&
381 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
382'
383
384# This test creates a commit with a symlink used for the following tests
385
889c6f0e 386test_expect_success 'stash symlink to file' '
2ba2fe29 387 git reset --hard &&
889c6f0e 388 test_ln_s_add file filelink &&
2ba2fe29
CB
389 git commit -m "Add symlink" &&
390 rm filelink &&
391 cp file filelink &&
889c6f0e
JS
392 git stash save "symlink to file"
393'
394
395test_expect_success SYMLINKS 'this must have re-created the symlink' '
2ba2fe29 396 test -h filelink &&
889c6f0e
JS
397 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
398'
399
400test_expect_success 'unstash must re-create the file' '
2ba2fe29
CB
401 git stash apply &&
402 ! test -h filelink &&
403 test bar = "$(cat file)"
404'
405
889c6f0e 406test_expect_success 'stash symlink to file (stage rm)' '
2ba2fe29
CB
407 git reset --hard &&
408 git rm filelink &&
409 cp file filelink &&
889c6f0e
JS
410 git stash save "symlink to file (stage rm)"
411'
412
413test_expect_success SYMLINKS 'this must have re-created the symlink' '
2ba2fe29 414 test -h filelink &&
889c6f0e
JS
415 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
416'
417
418test_expect_success 'unstash must re-create the file' '
2ba2fe29
CB
419 git stash apply &&
420 ! test -h filelink &&
421 test bar = "$(cat file)"
422'
423
889c6f0e 424test_expect_success 'stash symlink to file (full stage)' '
2ba2fe29
CB
425 git reset --hard &&
426 rm filelink &&
427 cp file filelink &&
428 git add filelink &&
889c6f0e
JS
429 git stash save "symlink to file (full stage)"
430'
431
432test_expect_success SYMLINKS 'this must have re-created the symlink' '
2ba2fe29 433 test -h filelink &&
889c6f0e
JS
434 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
435'
436
437test_expect_success 'unstash must re-create the file' '
2ba2fe29
CB
438 git stash apply &&
439 ! test -h filelink &&
440 test bar = "$(cat file)"
441'
442
443test_expect_failure 'stash directory to file' '
444 git reset --hard &&
445 mkdir dir &&
446 echo foo >dir/file &&
447 git add dir/file &&
448 git commit -m "Add file in dir" &&
449 rm -fr dir &&
450 echo bar >dir &&
451 git stash save "directory to file" &&
452 test -d dir &&
453 test foo = "$(cat dir/file)" &&
454 test_must_fail git stash apply &&
455 test bar = "$(cat dir)" &&
456 git reset --soft HEAD^
457'
458
459test_expect_failure 'stash file to directory' '
460 git reset --hard &&
461 rm file &&
462 mkdir file &&
463 echo foo >file/file &&
464 git stash save "file to directory" &&
465 test -f file &&
466 test bar = "$(cat file)" &&
467 git stash apply &&
468 test -f file/file &&
469 test foo = "$(cat file/file)"
470'
471
93415f58
JT
472test_expect_success 'giving too many ref arguments does not modify files' '
473 git stash clear &&
474 test_when_finished "git reset --hard HEAD" &&
475 echo foo >file2 &&
476 git stash &&
477 echo bar >file2 &&
478 git stash &&
479 test-tool chmtime =123456789 file2 &&
480 for type in apply pop "branch stash-branch"
481 do
482 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
483 test_i18ngrep "Too many revisions" err &&
484 test 123456789 = $(test-tool chmtime -g file2) || return 1
485 done
486'
487
488test_expect_success 'drop: too many arguments errors out (does nothing)' '
489 git stash list >expect &&
490 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
491 test_i18ngrep "Too many revisions" err &&
492 git stash list >actual &&
493 test_cmp expect actual
494'
495
496test_expect_success 'show: too many arguments errors out (does nothing)' '
497 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
498 test_i18ngrep "Too many revisions" err &&
499 test_must_be_empty out
500'
501
c95bc226
JT
502test_expect_success 'stash create - no changes' '
503 git stash clear &&
504 test_when_finished "git reset --hard HEAD" &&
505 git reset --hard &&
506 git stash create >actual &&
507 test_must_be_empty actual
508'
509
daf7a0c0
JS
510test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
511 git stash clear &&
512 test_when_finished "git reset --hard HEAD" &&
513 git reset --hard &&
6a0b88b2 514 echo foo >>file &&
daf7a0c0
JS
515 STASH_ID=$(git stash create) &&
516 git reset --hard &&
517 git stash branch stash-branch ${STASH_ID} &&
6a0b88b2
PSU
518 test_when_finished "git reset --hard HEAD && git checkout master &&
519 git branch -D stash-branch" &&
daf7a0c0
JS
520 test $(git ls-files --modified | wc -l) -eq 1
521'
522
523test_expect_success 'stash branch - stashes on stack, stash-like argument' '
524 git stash clear &&
525 test_when_finished "git reset --hard HEAD" &&
526 git reset --hard &&
6a0b88b2 527 echo foo >>file &&
daf7a0c0
JS
528 git stash &&
529 test_when_finished "git stash drop" &&
6a0b88b2 530 echo bar >>file &&
daf7a0c0
JS
531 STASH_ID=$(git stash create) &&
532 git reset --hard &&
533 git stash branch stash-branch ${STASH_ID} &&
6a0b88b2
PSU
534 test_when_finished "git reset --hard HEAD && git checkout master &&
535 git branch -D stash-branch" &&
daf7a0c0
JS
536 test $(git ls-files --modified | wc -l) -eq 1
537'
538
93415f58
JT
539test_expect_success 'stash branch complains with no arguments' '
540 test_must_fail git stash branch 2>err &&
541 test_i18ngrep "No branch name specified" err
542'
543
11452114 544test_expect_success 'stash show format defaults to --stat' '
daf7a0c0
JS
545 git stash clear &&
546 test_when_finished "git reset --hard HEAD" &&
547 git reset --hard &&
6a0b88b2 548 echo foo >>file &&
daf7a0c0
JS
549 git stash &&
550 test_when_finished "git stash drop" &&
6a0b88b2 551 echo bar >>file &&
daf7a0c0
JS
552 STASH_ID=$(git stash create) &&
553 git reset --hard &&
3fcb8878 554 cat >expected <<-EOF &&
dc801e71 555 file | 1 +
7f814632 556 1 file changed, 1 insertion(+)
3fcb8878
BC
557 EOF
558 git stash show ${STASH_ID} >actual &&
fc5877a6 559 test_i18ncmp expected actual
daf7a0c0 560'
3fcb8878 561
11452114
JN
562test_expect_success 'stash show - stashes on stack, stash-like argument' '
563 git stash clear &&
564 test_when_finished "git reset --hard HEAD" &&
565 git reset --hard &&
6a0b88b2 566 echo foo >>file &&
11452114
JN
567 git stash &&
568 test_when_finished "git stash drop" &&
6a0b88b2 569 echo bar >>file &&
11452114
JN
570 STASH_ID=$(git stash create) &&
571 git reset --hard &&
572 echo "1 0 file" >expected &&
573 git stash show --numstat ${STASH_ID} >actual &&
574 test_cmp expected actual
575'
576
9027fa9e 577test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
3fcb8878
BC
578 git stash clear &&
579 test_when_finished "git reset --hard HEAD" &&
580 git reset --hard &&
6a0b88b2 581 echo foo >>file &&
3fcb8878
BC
582 git stash &&
583 test_when_finished "git stash drop" &&
6a0b88b2 584 echo bar >>file &&
3fcb8878
BC
585 STASH_ID=$(git stash create) &&
586 git reset --hard &&
587 cat >expected <<-EOF &&
588 diff --git a/file b/file
589 index 7601807..935fbd3 100644
590 --- a/file
591 +++ b/file
592 @@ -1 +1,2 @@
593 baz
594 +bar
595 EOF
596 git stash show -p ${STASH_ID} >actual &&
c7848150 597 diff_cmp expected actual
3fcb8878
BC
598'
599
9027fa9e 600test_expect_success 'stash show - no stashes on stack, stash-like argument' '
3fcb8878
BC
601 git stash clear &&
602 test_when_finished "git reset --hard HEAD" &&
603 git reset --hard &&
6a0b88b2 604 echo foo >>file &&
3fcb8878
BC
605 STASH_ID=$(git stash create) &&
606 git reset --hard &&
11452114
JN
607 echo "1 0 file" >expected &&
608 git stash show --numstat ${STASH_ID} >actual &&
609 test_cmp expected actual
3fcb8878
BC
610'
611
9027fa9e 612test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
daf7a0c0
JS
613 git stash clear &&
614 test_when_finished "git reset --hard HEAD" &&
615 git reset --hard &&
6a0b88b2 616 echo foo >>file &&
daf7a0c0
JS
617 STASH_ID=$(git stash create) &&
618 git reset --hard &&
3fcb8878
BC
619 cat >expected <<-EOF &&
620 diff --git a/file b/file
621 index 7601807..71b52c4 100644
622 --- a/file
623 +++ b/file
624 @@ -1 +1,2 @@
625 baz
626 +foo
627 EOF
628 git stash show -p ${STASH_ID} >actual &&
c7848150 629 diff_cmp expected actual
daf7a0c0
JS
630'
631
8e407bc8
TG
632test_expect_success 'stash show --patience shows diff' '
633 git reset --hard &&
634 echo foo >>file &&
635 STASH_ID=$(git stash create) &&
636 git reset --hard &&
637 cat >expected <<-EOF &&
638 diff --git a/file b/file
639 index 7601807..71b52c4 100644
640 --- a/file
641 +++ b/file
642 @@ -1 +1,2 @@
643 baz
644 +foo
645 EOF
646 git stash show --patience ${STASH_ID} >actual &&
c7848150 647 diff_cmp expected actual
8e407bc8
TG
648'
649
ab8ad46e 650test_expect_success 'drop: fail early if specified stash is not a stash ref' '
daf7a0c0
JS
651 git stash clear &&
652 test_when_finished "git reset --hard HEAD && git stash clear" &&
653 git reset --hard &&
6a0b88b2 654 echo foo >file &&
daf7a0c0 655 git stash &&
6a0b88b2 656 echo bar >file &&
daf7a0c0 657 git stash &&
8d66bb05 658 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
daf7a0c0
JS
659 git stash pop &&
660 test bar = "$(cat file)" &&
661 git reset --hard HEAD
662'
663
ab8ad46e 664test_expect_success 'pop: fail early if specified stash is not a stash ref' '
daf7a0c0
JS
665 git stash clear &&
666 test_when_finished "git reset --hard HEAD && git stash clear" &&
667 git reset --hard &&
6a0b88b2 668 echo foo >file &&
daf7a0c0 669 git stash &&
6a0b88b2 670 echo bar >file &&
daf7a0c0 671 git stash &&
8d66bb05 672 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
daf7a0c0
JS
673 git stash pop &&
674 test bar = "$(cat file)" &&
675 git reset --hard HEAD
676'
677
7be8b3ba 678test_expect_success 'ref with non-existent reflog' '
daf7a0c0 679 git stash clear &&
6a0b88b2
PSU
680 echo bar5 >file &&
681 echo bar6 >file2 &&
daf7a0c0
JS
682 git add file2 &&
683 git stash &&
1ae96444 684 test_must_fail git rev-parse --quiet --verify does-not-exist &&
8d66bb05
JS
685 test_must_fail git stash drop does-not-exist &&
686 test_must_fail git stash drop does-not-exist@{0} &&
687 test_must_fail git stash pop does-not-exist &&
688 test_must_fail git stash pop does-not-exist@{0} &&
689 test_must_fail git stash apply does-not-exist &&
690 test_must_fail git stash apply does-not-exist@{0} &&
691 test_must_fail git stash show does-not-exist &&
692 test_must_fail git stash show does-not-exist@{0} &&
693 test_must_fail git stash branch tmp does-not-exist &&
694 test_must_fail git stash branch tmp does-not-exist@{0} &&
daf7a0c0
JS
695 git stash drop
696'
697
698test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
699 git stash clear &&
8d66bb05 700 test_must_fail git stash drop stash@{0} &&
6a0b88b2
PSU
701 echo bar5 >file &&
702 echo bar6 >file2 &&
daf7a0c0
JS
703 git add file2 &&
704 git stash &&
9355fc9c
JS
705 test_must_fail git stash drop stash@{1} &&
706 test_must_fail git stash pop stash@{1} &&
707 test_must_fail git stash apply stash@{1} &&
708 test_must_fail git stash show stash@{1} &&
709 test_must_fail git stash branch tmp stash@{1} &&
daf7a0c0
JS
710 git stash drop
711'
712
a56c8f5a
AW
713test_expect_success 'invalid ref of the form "n", n >= N' '
714 git stash clear &&
715 test_must_fail git stash drop 0 &&
716 echo bar5 >file &&
717 echo bar6 >file2 &&
718 git add file2 &&
719 git stash &&
720 test_must_fail git stash drop 1 &&
721 test_must_fail git stash pop 1 &&
722 test_must_fail git stash apply 1 &&
723 test_must_fail git stash show 1 &&
724 test_must_fail git stash branch tmp 1 &&
725 git stash drop
726'
727
63b50c8f
TG
728test_expect_success 'valid ref of the form "n", n < N' '
729 git stash clear &&
730 echo bar5 >file &&
731 echo bar6 >file2 &&
732 git add file2 &&
733 git stash &&
734 git stash show 0 &&
735 git stash branch tmp 0 &&
736 git checkout master &&
737 git stash &&
738 git stash apply 0 &&
739 git reset --hard &&
740 git stash pop 0 &&
741 git stash &&
742 git stash drop 0 &&
743 test_must_fail git stash drop
744'
745
ab8ad46e 746test_expect_success 'branch: do not drop the stash if the branch exists' '
835d6a1f
TC
747 git stash clear &&
748 echo foo >file &&
749 git add file &&
750 git commit -m initial &&
751 echo bar >file &&
752 git stash &&
753 test_must_fail git stash branch master stash@{0} &&
754 git rev-parse stash@{0} --
755'
756
ab8ad46e 757test_expect_success 'branch: should not drop the stash if the apply fails' '
b04e6915
JT
758 git stash clear &&
759 git reset HEAD~1 --hard &&
760 echo foo >file &&
761 git add file &&
762 git commit -m initial &&
763 echo bar >file &&
764 git stash &&
765 echo baz >file &&
766 test_when_finished "git checkout master" &&
767 test_must_fail git stash branch new_branch stash@{0} &&
768 git rev-parse stash@{0} --
769'
770
ab8ad46e 771test_expect_success 'apply: show same status as git status (relative to ./)' '
6eaf92f3
PK
772 git stash clear &&
773 echo 1 >subdir/subfile1 &&
774 echo 2 >subdir/subfile2 &&
775 git add subdir/subfile1 &&
776 git commit -m subdir &&
777 (
778 cd subdir &&
779 echo x >subfile1 &&
780 echo x >../file &&
781 git status >../expect &&
782 git stash &&
783 sane_unset GIT_MERGE_VERBOSITY &&
784 git stash apply
785 ) |
1790f4fe 786 sed -e 1d >actual && # drop "Saved..."
28785688 787 test_i18ncmp expect actual
6eaf92f3
PK
788'
789
6a0b88b2 790cat >expect <<EOF
44df2e29
JM
791diff --git a/HEAD b/HEAD
792new file mode 100644
793index 0000000..fe0cbee
794--- /dev/null
795+++ b/HEAD
796@@ -0,0 +1 @@
797+file-not-a-ref
798EOF
799
800test_expect_success 'stash where working directory contains "HEAD" file' '
801 git stash clear &&
802 git reset --hard &&
6a0b88b2 803 echo file-not-a-ref >HEAD &&
44df2e29
JM
804 git add HEAD &&
805 test_tick &&
806 git stash &&
807 git diff-files --quiet &&
808 git diff-index --cached --quiet HEAD &&
809 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
6a0b88b2 810 git diff stash^..stash >output &&
c7848150 811 diff_cmp expect output
44df2e29
JM
812'
813
bd514cad
RR
814test_expect_success 'store called with invalid commit' '
815 test_must_fail git stash store foo
816'
817
818test_expect_success 'store updates stash ref and reflog' '
819 git stash clear &&
820 git reset --hard &&
821 echo quux >bazzy &&
822 git add bazzy &&
823 STASH_ID=$(git stash create) &&
824 git reset --hard &&
79b04f9b 825 test_path_is_missing bazzy &&
bd514cad 826 git stash store -m quuxery $STASH_ID &&
cbc5cf7c 827 test $(git rev-parse stash) = $STASH_ID &&
d0ab0584 828 git reflog --format=%H stash| grep $STASH_ID &&
bd514cad
RR
829 git stash pop &&
830 grep quux bazzy
831'
832
2a07e437
ØW
833test_expect_success 'handle stash specification with spaces' '
834 git stash clear &&
835 echo pig >file &&
836 git stash &&
837 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
838 test_tick &&
839 echo cow >file &&
840 git stash &&
841 git stash apply "stash@{$stamp}" &&
842 grep pig file
843'
844
288c67ca
JK
845test_expect_success 'setup stash with index and worktree changes' '
846 git stash clear &&
847 git reset --hard &&
848 echo index >file &&
849 git add file &&
850 echo working >file &&
851 git stash
852'
853
854test_expect_success 'stash list implies --first-parent -m' '
f2f3fc95
JK
855 cat >expect <<-EOF &&
856 stash@{0}
288c67ca
JK
857
858 diff --git a/file b/file
859 index 257cc56..d26b33d 100644
860 --- a/file
861 +++ b/file
862 @@ -1 +1 @@
863 -foo
864 +working
865 EOF
f2f3fc95 866 git stash list --format=%gd -p >actual &&
c7848150 867 diff_cmp expect actual
288c67ca
JK
868'
869
870test_expect_success 'stash list --cc shows combined diff' '
871 cat >expect <<-\EOF &&
f2f3fc95 872 stash@{0}
288c67ca
JK
873
874 diff --cc file
875 index 257cc56,9015a7a..d26b33d
876 --- a/file
877 +++ b/file
878 @@@ -1,1 -1,1 +1,1 @@@
879 - foo
880 -index
881 ++working
882 EOF
f2f3fc95 883 git stash list --format=%gd -p --cc >actual &&
c7848150 884 diff_cmp expect actual
288c67ca
JK
885'
886
9d4e28ea
JK
887test_expect_success 'stash is not confused by partial renames' '
888 mv file renamed &&
889 git add renamed &&
890 git stash &&
891 git stash apply &&
892 test_path_is_file renamed &&
893 test_path_is_missing file
894'
895
f5727e26
TG
896test_expect_success 'push -m shows right message' '
897 >foo &&
898 git add foo &&
899 git stash push -m "test message" &&
900 echo "stash@{0}: On master: test message" >expect &&
901 git stash list -1 >actual &&
902 test_cmp expect actual
903'
904
5675473f
PH
905test_expect_success 'push -m also works without space' '
906 >foo &&
907 git add foo &&
908 git stash push -m"unspaced test message" &&
909 echo "stash@{0}: On master: unspaced test message" >expect &&
910 git stash list -1 >actual &&
911 test_cmp expect actual
912'
913
914test_expect_success 'store -m foo shows right message' '
915 git stash clear &&
916 git reset --hard &&
917 echo quux >bazzy &&
918 git add bazzy &&
919 STASH_ID=$(git stash create) &&
920 git stash store -m "store m" $STASH_ID &&
921 echo "stash@{0}: store m" >expect &&
922 git stash list -1 >actual &&
923 test_cmp expect actual
924'
925
926test_expect_success 'store -mfoo shows right message' '
927 git stash clear &&
928 git reset --hard &&
929 echo quux >bazzy &&
930 git add bazzy &&
931 STASH_ID=$(git stash create) &&
932 git stash store -m"store mfoo" $STASH_ID &&
933 echo "stash@{0}: store mfoo" >expect &&
934 git stash list -1 >actual &&
935 test_cmp expect actual
936'
937
938test_expect_success 'store --message=foo shows right message' '
939 git stash clear &&
940 git reset --hard &&
941 echo quux >bazzy &&
942 git add bazzy &&
943 STASH_ID=$(git stash create) &&
944 git stash store --message="store message=foo" $STASH_ID &&
945 echo "stash@{0}: store message=foo" >expect &&
946 git stash list -1 >actual &&
947 test_cmp expect actual
948'
949
950test_expect_success 'store --message foo shows right message' '
951 git stash clear &&
952 git reset --hard &&
953 echo quux >bazzy &&
954 git add bazzy &&
955 STASH_ID=$(git stash create) &&
956 git stash store --message "store message foo" $STASH_ID &&
957 echo "stash@{0}: store message foo" >expect &&
958 git stash list -1 >actual &&
959 test_cmp expect actual
960'
961
962test_expect_success 'push -mfoo uses right message' '
963 >foo &&
964 git add foo &&
965 git stash push -m"test mfoo" &&
966 echo "stash@{0}: On master: test mfoo" >expect &&
967 git stash list -1 >actual &&
968 test_cmp expect actual
969'
970
971test_expect_success 'push --message foo is synonym for -mfoo' '
972 >foo &&
973 git add foo &&
974 git stash push --message "test message foo" &&
975 echo "stash@{0}: On master: test message foo" >expect &&
976 git stash list -1 >actual &&
977 test_cmp expect actual
978'
979
980test_expect_success 'push --message=foo is synonym for -mfoo' '
981 >foo &&
982 git add foo &&
983 git stash push --message="test message=foo" &&
984 echo "stash@{0}: On master: test message=foo" >expect &&
985 git stash list -1 >actual &&
986 test_cmp expect actual
987'
988
989test_expect_success 'push -m shows right message' '
990 >foo &&
991 git add foo &&
992 git stash push -m "test m foo" &&
993 echo "stash@{0}: On master: test m foo" >expect &&
994 git stash list -1 >actual &&
995 test_cmp expect actual
996'
997
6f5ccd4d
TG
998test_expect_success 'create stores correct message' '
999 >foo &&
1000 git add foo &&
1001 STASH_ID=$(git stash create "create test message") &&
1002 echo "On master: create test message" >expect &&
1003 git show --pretty=%s -s ${STASH_ID} >actual &&
1004 test_cmp expect actual
1005'
1006
1007test_expect_success 'create with multiple arguments for the message' '
1008 >foo &&
1009 git add foo &&
1010 STASH_ID=$(git stash create test untracked) &&
1011 echo "On master: test untracked" >expect &&
1012 git show --pretty=%s -s ${STASH_ID} >actual &&
1013 test_cmp expect actual
1014'
1015
4e9bf3dd
JT
1016test_expect_success 'create in a detached state' '
1017 test_when_finished "git checkout master" &&
1018 git checkout HEAD~1 &&
1019 >foo &&
1020 git add foo &&
1021 STASH_ID=$(git stash create) &&
1022 HEAD_ID=$(git rev-parse --short HEAD) &&
1023 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1024 git show --pretty=%s -s ${STASH_ID} >actual &&
1025 test_cmp expect actual
1026'
1027
df6bba09
TG
1028test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1029 >foo &&
1030 >bar &&
1031 git add foo bar &&
1032 git stash push -- foo &&
1033 test_path_is_file bar &&
1034 test_path_is_missing foo &&
1035 git stash pop &&
1036 test_path_is_file foo &&
1037 test_path_is_file bar
1038'
1039
22fc703e
PS
1040test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1041 mkdir sub &&
1042 >foo &&
1043 >bar &&
1044 git add foo bar &&
1045 (
1046 cd sub &&
1047 git stash push -- ../foo
1048 ) &&
1049 test_path_is_file bar &&
1050 test_path_is_missing foo &&
1051 git stash pop &&
1052 test_path_is_file foo &&
1053 test_path_is_file bar
1054'
1055
df6bba09
TG
1056test_expect_success 'stash with multiple pathspec arguments' '
1057 >foo &&
1058 >bar &&
1059 >extra &&
1060 git add foo bar extra &&
1061 git stash push -- foo bar &&
1062 test_path_is_missing bar &&
1063 test_path_is_missing foo &&
1064 test_path_is_file extra &&
1065 git stash pop &&
1066 test_path_is_file foo &&
1067 test_path_is_file bar &&
1068 test_path_is_file extra
1069'
1070
1071test_expect_success 'stash with file including $IFS character' '
1072 >"foo bar" &&
1073 >foo &&
1074 >bar &&
1075 git add foo* &&
1076 git stash push -- "foo b*" &&
1077 test_path_is_missing "foo bar" &&
1078 test_path_is_file foo &&
1079 test_path_is_file bar &&
1080 git stash pop &&
1081 test_path_is_file "foo bar" &&
1082 test_path_is_file foo &&
1083 test_path_is_file bar
1084'
1085
1086test_expect_success 'stash with pathspec matching multiple paths' '
1087 echo original >file &&
1088 echo original >other-file &&
1089 git commit -m "two" file other-file &&
1090 echo modified >file &&
1091 echo modified >other-file &&
1092 git stash push -- "*file" &&
1093 echo original >expect &&
1094 test_cmp expect file &&
1095 test_cmp expect other-file &&
1096 git stash pop &&
1097 echo modified >expect &&
1098 test_cmp expect file &&
1099 test_cmp expect other-file
1100'
1101
1102test_expect_success 'stash push -p with pathspec shows no changes only once' '
1103 >foo &&
1104 git add foo &&
1105 git commit -m "tmp" &&
1106 git stash push -p foo >actual &&
1107 echo "No local changes to save" >expect &&
1108 git reset --hard HEAD~ &&
0d75bfe6 1109 test_i18ncmp expect actual
df6bba09
TG
1110'
1111
ab8ad46e 1112test_expect_success 'push <pathspec>: show no changes when there are none' '
df6bba09
TG
1113 >foo &&
1114 git add foo &&
1115 git commit -m "tmp" &&
1116 git stash push foo >actual &&
1117 echo "No local changes to save" >expect &&
1118 git reset --hard HEAD~ &&
0d75bfe6 1119 test_i18ncmp expect actual
df6bba09
TG
1120'
1121
ab8ad46e 1122test_expect_success 'push: <pathspec> not in the repository errors out' '
df6bba09
TG
1123 >untracked &&
1124 test_must_fail git stash push untracked &&
1125 test_path_is_file untracked
1126'
1127
1ac528c0
PSU
1128test_expect_success 'push: -q is quiet with changes' '
1129 >foo &&
1130 git add foo &&
1131 git stash push -q >output 2>&1 &&
1132 test_must_be_empty output
1133'
1134
1135test_expect_success 'push: -q is quiet with no changes' '
1136 git stash push -q >output 2>&1 &&
1137 test_must_be_empty output
1138'
1139
1140test_expect_success 'push: -q is quiet even if there is no initial commit' '
1141 git init foo_dir &&
1142 test_when_finished rm -rf foo_dir &&
1143 (
1144 cd foo_dir &&
1145 >bar &&
1146 test_must_fail git stash push -q >output 2>&1 &&
1147 test_must_be_empty output
1148 )
1149'
1150
df6bba09
TG
1151test_expect_success 'untracked files are left in place when -u is not given' '
1152 >file &&
1153 git add file &&
1154 >untracked &&
1155 git stash push file &&
1156 test_path_is_file untracked
1157'
1158
9e140909
TG
1159test_expect_success 'stash without verb with pathspec' '
1160 >"foo bar" &&
1161 >foo &&
1162 >bar &&
1163 git add foo* &&
1164 git stash -- "foo b*" &&
1165 test_path_is_missing "foo bar" &&
1166 test_path_is_file foo &&
1167 test_path_is_file bar &&
1168 git stash pop &&
1169 test_path_is_file "foo bar" &&
1170 test_path_is_file foo &&
1171 test_path_is_file bar
1172'
1173
e0e7f99e
TG
1174test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1175 git reset &&
1176 >foo &&
1177 >bar &&
1178 git add foo bar &&
1179 git commit -m "test" &&
1180 echo "foo" >foo &&
1181 echo "bar" >bar &&
1182 git stash -k -- foo &&
1183 test "",bar = $(cat foo),$(cat bar) &&
1184 git stash pop &&
1185 test foo,bar = $(cat foo),$(cat bar)
1186'
1187
bba067d2
TG
1188test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1189 git reset &&
1190 >subdir/untracked &&
1191 >subdir/tracked1 &&
1192 >subdir/tracked2 &&
1193 git add subdir/tracked* &&
1194 git stash -- subdir/ &&
1195 test_path_is_missing subdir/tracked1 &&
1196 test_path_is_missing subdir/tracked2 &&
1197 test_path_is_file subdir/untracked &&
1198 git stash pop &&
1199 test_path_is_file subdir/tracked1 &&
1200 test_path_is_file subdir/tracked2 &&
1201 test_path_is_file subdir/untracked
1202'
1203
1204test_expect_success 'stash -- <subdir> works with binary files' '
1205 git reset &&
1206 >subdir/untracked &&
1207 >subdir/tracked &&
1208 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1209 git add subdir/tracked* &&
1210 git stash -- subdir/ &&
1211 test_path_is_missing subdir/tracked &&
1212 test_path_is_missing subdir/tracked-binary &&
1213 test_path_is_file subdir/untracked &&
1214 git stash pop &&
1215 test_path_is_file subdir/tracked &&
1216 test_path_is_file subdir/tracked-binary &&
1217 test_path_is_file subdir/untracked
1218'
1219
0640897d
TG
1220test_expect_success 'stash with user.name and user.email set works' '
1221 test_config user.name "A U Thor" &&
1222 test_config user.email "a.u@thor" &&
1223 git stash
1224'
1225
3bc2111f
SD
1226test_expect_success 'stash works when user.name and user.email are not set' '
1227 git reset &&
1228 >1 &&
1229 git add 1 &&
1230 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1231 git stash &&
1232 git show -s --format="%an <%ae>" refs/stash >actual &&
1233 test_cmp expect actual &&
1234 >2 &&
1235 git add 2 &&
1236 test_config user.useconfigonly true &&
1237 test_config stash.usebuiltin true &&
1238 (
1239 sane_unset GIT_AUTHOR_NAME &&
1240 sane_unset GIT_AUTHOR_EMAIL &&
1241 sane_unset GIT_COMMITTER_NAME &&
1242 sane_unset GIT_COMMITTER_EMAIL &&
1243 test_unconfig user.email &&
1244 test_unconfig user.name &&
1245 test_must_fail git commit -m "should fail" &&
1246 echo "git stash <git@stash>" >expect &&
1247 >2 &&
1248 git stash &&
1249 git show -s --format="%an <%ae>" refs/stash >actual &&
1250 test_cmp expect actual
1251 )
1252'
1253
b932f6a5
TG
1254test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1255 test_commit to-remove to-remove &&
1256 git rm to-remove &&
1257 git stash --keep-index &&
1258 test_path_is_missing to-remove
1259'
1260
34933d0e
TG
1261test_expect_success 'stash apply should succeed with unmodified file' '
1262 echo base >file &&
1263 git add file &&
1264 git commit -m base &&
1265
1266 # now stash a modification
1267 echo modified >file &&
1268 git stash &&
1269
1270 # make the file stat dirty
1271 cp file other &&
1272 mv other file &&
1273
1274 git stash apply
1275'
1276
4a58c3d7
JS
1277test_expect_success 'stash handles skip-worktree entries nicely' '
1278 test_commit A &&
1279 echo changed >A.t &&
1280 git add A.t &&
1281 git update-index --skip-worktree A.t &&
1282 rm A.t &&
1283 git stash &&
1284
1285 git rev-parse --verify refs/stash:A.t
1286'
1287
150937c4 1288test_done