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