]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3903-stash.sh
The twenty-first batch
[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 11. ./test-lib.sh
386e7a9d 12. "$TEST_DIRECTORY"/lib-unique-files.sh
150937c4 13
bc303718 14test_expect_success 'setup' '
15 test_oid_cache <<-EOF
16 export_base sha1:73c9bab443d1f88ac61aa533d2eeaaa15451239c
17 export_base sha256:f210fa6346e3e2ce047bdb570426b17075980c1ac01fec8fc4b75bd3ab4bcfe4
18 EOF
19'
20
ca7990ce
ÆAB
21test_expect_success 'usage on cmd and subcommand invalid option' '
22 test_expect_code 129 git stash --invalid-option 2>usage &&
23 grep "or: git stash" usage &&
24
25 test_expect_code 129 git stash push --invalid-option 2>usage &&
26 ! grep "or: git stash" usage
27'
28
29test_expect_success 'usage on main command -h emits a summary of subcommands' '
30 test_expect_code 129 git stash -h >usage &&
31 grep -F "usage: git stash list" usage &&
32 grep -F "or: git stash show" usage
33'
34
2b057d97 35test_expect_success 'usage for subcommands should emit subcommand usage' '
ca7990ce
ÆAB
36 test_expect_code 129 git stash push -h >usage &&
37 grep -F "usage: git stash [push" usage
38'
39
c7848150 40diff_cmp () {
41 for i in "$1" "$2"
42 do
43 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
44 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
45 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
46 "$i" >"$i.compare" || return 1
47 done &&
48 test_cmp "$1.compare" "$2.compare" &&
49 rm -f "$1.compare" "$2.compare"
50}
51
76bccbcf 52setup_stash() {
6a0b88b2 53 echo 1 >file &&
150937c4 54 git add file &&
88bab59c
JK
55 echo unrelated >other-file &&
56 git add other-file &&
150937c4
JS
57 test_tick &&
58 git commit -m initial &&
6a0b88b2 59 echo 2 >file &&
150937c4 60 git add file &&
6a0b88b2 61 echo 3 >file &&
150937c4
JS
62 test_tick &&
63 git stash &&
64 git diff-files --quiet &&
65 git diff-index --cached --quiet HEAD
76bccbcf
JC
66}
67
68test_expect_success 'stash some dirty working directory' '
69 setup_stash
150937c4
JS
70'
71
6a0b88b2 72cat >expect <<EOF
150937c4
JS
73diff --git a/file b/file
74index 0cfbf08..00750ed 100644
75--- a/file
76+++ b/file
77@@ -1 +1 @@
78-2
79+3
80EOF
81
82test_expect_success 'parents of stash' '
83 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
6a0b88b2 84 git diff stash^2..stash >output &&
c7848150 85 diff_cmp expect output
150937c4
JS
86'
87
59d418fe
JK
88test_expect_success 'applying bogus stash does nothing' '
89 test_must_fail git stash apply stash@{1} &&
90 echo 1 >expect &&
91 test_cmp expect file
92'
93
e0e2a9cb
JK
94test_expect_success 'apply does not need clean working directory' '
95 echo 4 >other-file &&
e0e2a9cb
JK
96 git stash apply &&
97 echo 3 >expect &&
98 test_cmp expect file
99'
100
101test_expect_success 'apply does not clobber working directory changes' '
102 git reset --hard &&
103 echo 4 >file &&
104 test_must_fail git stash apply &&
105 echo 4 >expect &&
106 test_cmp expect file
150937c4
JS
107'
108
109test_expect_success 'apply stashed changes' '
e0e2a9cb
JK
110 git reset --hard &&
111 echo 5 >other-file &&
150937c4
JS
112 git add other-file &&
113 test_tick &&
114 git commit -m other-file &&
115 git stash apply &&
116 test 3 = $(cat file) &&
117 test 1 = $(git show :file) &&
118 test 1 = $(git show HEAD:file)
119'
120
121test_expect_success 'apply stashed changes (including index)' '
122 git reset --hard HEAD^ &&
6a0b88b2 123 echo 6 >other-file &&
150937c4
JS
124 git add other-file &&
125 test_tick &&
126 git commit -m other-file &&
127 git stash apply --index &&
128 test 3 = $(cat file) &&
129 test 2 = $(git show :file) &&
130 test 1 = $(git show HEAD:file)
131'
132
ceff079b
JH
133test_expect_success 'unstashing in a subdirectory' '
134 git reset --hard HEAD &&
135 mkdir subdir &&
18a82692
JN
136 (
137 cd subdir &&
138 git stash apply
fd4ec4f2 139 )
b683c080
BC
140'
141
d6cc2df5
JK
142test_expect_success 'stash drop complains of extra options' '
143 test_must_fail git stash drop --foo
144'
145
b683c080
BC
146test_expect_success 'drop top stash' '
147 git reset --hard &&
6a0b88b2
PSU
148 git stash list >expected &&
149 echo 7 >file &&
b683c080
BC
150 git stash &&
151 git stash drop &&
6a0b88b2
PSU
152 git stash list >actual &&
153 test_cmp expected actual &&
b683c080
BC
154 git stash apply &&
155 test 3 = $(cat file) &&
156 test 1 = $(git show :file) &&
157 test 1 = $(git show HEAD:file)
158'
159
160test_expect_success 'drop middle stash' '
161 git reset --hard &&
6a0b88b2 162 echo 8 >file &&
b683c080 163 git stash &&
6a0b88b2 164 echo 9 >file &&
b683c080
BC
165 git stash &&
166 git stash drop stash@{1} &&
167 test 2 = $(git stash list | wc -l) &&
168 git stash apply &&
169 test 9 = $(cat file) &&
170 test 1 = $(git show :file) &&
171 test 1 = $(git show HEAD:file) &&
172 git reset --hard &&
173 git stash drop &&
174 git stash apply &&
175 test 3 = $(cat file) &&
176 test 1 = $(git show :file) &&
177 test 1 = $(git show HEAD:file)
178'
179
a56c8f5a
AW
180test_expect_success 'drop middle stash by index' '
181 git reset --hard &&
182 echo 8 >file &&
183 git stash &&
184 echo 9 >file &&
185 git stash &&
186 git stash drop 1 &&
187 test 2 = $(git stash list | wc -l) &&
188 git stash apply &&
189 test 9 = $(cat file) &&
190 test 1 = $(git show :file) &&
191 test 1 = $(git show HEAD:file) &&
192 git reset --hard &&
193 git stash drop &&
194 git stash apply &&
195 test 3 = $(cat file) &&
196 test 1 = $(git show :file) &&
197 test 1 = $(git show HEAD:file)
198'
199
76bccbcf
JC
200test_expect_success 'drop stash reflog updates refs/stash' '
201 git reset --hard &&
202 git rev-parse refs/stash >expect &&
203 echo 9 >file &&
204 git stash &&
205 git stash drop stash@{0} &&
206 git rev-parse refs/stash >actual &&
207 test_cmp expect actual
208'
209
99a294bc 210test_expect_success 'drop stash reflog updates refs/stash with rewrite' '
76bccbcf
JC
211 git init repo &&
212 (
213 cd repo &&
214 setup_stash
215 ) &&
216 echo 9 >repo/file &&
217
218 old_oid="$(git -C repo rev-parse stash@{0})" &&
219 git -C repo stash &&
220 new_oid="$(git -C repo rev-parse stash@{0})" &&
221
222 cat >expect <<-EOF &&
99a294bc
JC
223 $new_oid
224 $old_oid
76bccbcf 225 EOF
99a294bc 226 git -C repo reflog show refs/stash --format=%H >actual &&
76bccbcf
JC
227 test_cmp expect actual &&
228
229 git -C repo stash drop stash@{1} &&
99a294bc 230 git -C repo reflog show refs/stash --format=%H >actual &&
76bccbcf 231 cat >expect <<-EOF &&
99a294bc 232 $new_oid
76bccbcf
JC
233 EOF
234 test_cmp expect actual
235'
236
b683c080
BC
237test_expect_success 'stash pop' '
238 git reset --hard &&
239 git stash pop &&
240 test 3 = $(cat file) &&
241 test 1 = $(git show :file) &&
242 test 1 = $(git show HEAD:file) &&
243 test 0 = $(git stash list | wc -l)
ceff079b
JH
244'
245
6a0b88b2 246cat >expect <<EOF
4a588075
AMS
247diff --git a/file2 b/file2
248new file mode 100644
249index 0000000..1fe912c
250--- /dev/null
251+++ b/file2
252@@ -0,0 +1 @@
253+bar2
254EOF
255
6a0b88b2 256cat >expect1 <<EOF
4a588075
AMS
257diff --git a/file b/file
258index 257cc56..5716ca5 100644
259--- a/file
260+++ b/file
261@@ -1 +1 @@
262-foo
263+bar
264EOF
265
6a0b88b2 266cat >expect2 <<EOF
4a588075
AMS
267diff --git a/file b/file
268index 7601807..5716ca5 100644
269--- a/file
270+++ b/file
271@@ -1 +1 @@
272-baz
273+bar
274diff --git a/file2 b/file2
275new file mode 100644
276index 0000000..1fe912c
277--- /dev/null
278+++ b/file2
279@@ -0,0 +1 @@
280+bar2
281EOF
282
283test_expect_success 'stash branch' '
6a0b88b2 284 echo foo >file &&
a48fcd83 285 git commit file -m first &&
6a0b88b2
PSU
286 echo bar >file &&
287 echo bar2 >file2 &&
4a588075
AMS
288 git add file2 &&
289 git stash &&
6a0b88b2 290 echo baz >file &&
4a588075
AMS
291 git commit file -m second &&
292 git stash branch stashbranch &&
293 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
cbc75a12 294 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
6a0b88b2 295 git diff --cached >output &&
c7848150 296 diff_cmp expect output &&
6a0b88b2 297 git diff >output &&
c7848150 298 diff_cmp expect1 output &&
4a588075
AMS
299 git add file &&
300 git commit -m alternate\ second &&
cbc75a12 301 git diff main..stashbranch >output &&
c7848150 302 diff_cmp output expect2 &&
4a588075
AMS
303 test 0 = $(git stash list | wc -l)
304'
305
fcdd0e92 306test_expect_success 'apply -q is quiet' '
6a0b88b2 307 echo foo >file &&
fcdd0e92 308 git stash &&
6a0b88b2 309 git stash apply -q >output.out 2>&1 &&
ca8d148d 310 test_must_be_empty output.out
fcdd0e92
SB
311'
312
4b8b0f6f
VD
313test_expect_success 'apply --index -q is quiet' '
314 # Added file, deleted file, modified file all staged for commit
315 echo foo >new-file &&
316 echo test >file &&
317 git add new-file file &&
318 git rm other-file &&
319
320 git stash &&
321 git stash apply --index -q >output.out 2>&1 &&
322 test_must_be_empty output.out
323'
324
fcdd0e92 325test_expect_success 'save -q is quiet' '
6a0b88b2 326 git stash save --quiet >output.out 2>&1 &&
ca8d148d 327 test_must_be_empty output.out
fcdd0e92
SB
328'
329
df53c808 330test_expect_success 'pop -q works and is quiet' '
6a0b88b2 331 git stash pop -q >output.out 2>&1 &&
df53c808
TG
332 echo bar >expect &&
333 git show :file >actual &&
334 test_cmp expect actual &&
ca8d148d 335 test_must_be_empty output.out
fcdd0e92
SB
336'
337
460ccd0e 338test_expect_success 'pop -q --index works and is quiet' '
6a0b88b2 339 echo foo >file &&
460ccd0e
TR
340 git add file &&
341 git stash save --quiet &&
6a0b88b2 342 git stash pop -q --index >output.out 2>&1 &&
df53c808
TG
343 git diff-files file2 >file2.diff &&
344 test_must_be_empty file2.diff &&
460ccd0e 345 test foo = "$(git show :file)" &&
ca8d148d 346 test_must_be_empty output.out
460ccd0e
TR
347'
348
fcdd0e92
SB
349test_expect_success 'drop -q is quiet' '
350 git stash &&
6a0b88b2 351 git stash drop -q >output.out 2>&1 &&
ca8d148d 352 test_must_be_empty output.out
fcdd0e92
SB
353'
354
4b8b0f6f
VD
355test_expect_success 'stash push -q --staged refreshes the index' '
356 git reset --hard &&
357 echo test >file &&
358 git add file &&
359 git stash push -q --staged &&
360 git diff-files >output.out &&
361 test_must_be_empty output.out
362'
363
364test_expect_success 'stash apply -q --index refreshes the index' '
365 echo test >other-file &&
366 git add other-file &&
367 echo another-change >other-file &&
368 git diff-files >expect &&
369 git stash &&
370
371 git stash apply -q --index &&
372 git diff-files >actual &&
373 test_cmp expect actual
374'
375
ea41cfc4 376test_expect_success 'stash -k' '
6a0b88b2
PSU
377 echo bar3 >file &&
378 echo bar4 >file2 &&
ea41cfc4
JS
379 git add file2 &&
380 git stash -k &&
381 test bar,bar4 = $(cat file),$(cat file2)
382'
383
21ec98a7 384test_expect_success 'stash --no-keep-index' '
6a0b88b2
PSU
385 echo bar33 >file &&
386 echo bar44 >file2 &&
21ec98a7
DM
387 git add file2 &&
388 git stash --no-keep-index &&
389 test bar,bar2 = $(cat file),$(cat file2)
390'
391
41a28eb6
SO
392test_expect_success 'stash --staged' '
393 echo bar3 >file &&
394 echo bar4 >file2 &&
395 git add file2 &&
396 git stash --staged &&
397 test bar3,bar2 = $(cat file),$(cat file2) &&
398 git reset --hard &&
399 git stash pop &&
400 test bar,bar4 = $(cat file),$(cat file2)
401'
402
5fb76864
AJ
403test_expect_success 'stash --staged with binary file' '
404 printf "\0" >file &&
405 git add file &&
406 git stash --staged &&
407 git stash pop &&
408 printf "\0" >expect &&
409 test_cmp expect file
410'
411
8c3713ce
AM
412test_expect_success 'dont assume push with non-option args' '
413 test_must_fail git stash -q drop 2>err &&
6789275d 414 test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
8c3713ce
AM
415'
416
3c2eb80f 417test_expect_success 'stash --invalid-option' '
6a0b88b2
PSU
418 echo bar5 >file &&
419 echo bar6 >file2 &&
3c2eb80f
MM
420 git add file2 &&
421 test_must_fail git stash --invalid-option &&
422 test_must_fail git stash save --invalid-option &&
1ada5020 423 test bar5,bar6 = $(cat file),$(cat file2)
3c2eb80f
MM
424'
425
2ba2fe29
CB
426test_expect_success 'stash an added file' '
427 git reset --hard &&
428 echo new >file3 &&
429 git add file3 &&
430 git stash save "added file" &&
431 ! test -r file3 &&
432 git stash apply &&
433 test new = "$(cat file3)"
434'
435
b6e531e6
MK
436test_expect_success 'stash --intent-to-add file' '
437 git reset --hard &&
438 echo new >file4 &&
439 git add --intent-to-add file4 &&
440 test_when_finished "git rm -f file4" &&
441 test_must_fail git stash
442'
443
2ba2fe29
CB
444test_expect_success 'stash rm then recreate' '
445 git reset --hard &&
446 git rm file &&
447 echo bar7 >file &&
448 git stash save "rm then recreate" &&
449 test bar = "$(cat file)" &&
450 git stash apply &&
451 test bar7 = "$(cat file)"
452'
453
454test_expect_success 'stash rm and ignore' '
455 git reset --hard &&
456 git rm file &&
457 echo file >.gitignore &&
458 git stash save "rm and ignore" &&
459 test bar = "$(cat file)" &&
a48fcd83 460 test file = "$(cat .gitignore)" &&
2ba2fe29
CB
461 git stash apply &&
462 ! test -r file &&
463 test file = "$(cat .gitignore)"
464'
465
466test_expect_success 'stash rm and ignore (stage .gitignore)' '
467 git reset --hard &&
468 git rm file &&
469 echo file >.gitignore &&
470 git add .gitignore &&
471 git stash save "rm and ignore (stage .gitignore)" &&
472 test bar = "$(cat file)" &&
a48fcd83 473 ! test -r .gitignore &&
2ba2fe29
CB
474 git stash apply &&
475 ! test -r file &&
476 test file = "$(cat .gitignore)"
477'
478
479test_expect_success SYMLINKS 'stash file to symlink' '
480 git reset --hard &&
481 rm file &&
482 ln -s file2 file &&
483 git stash save "file to symlink" &&
456296b5 484 test_path_is_file_not_symlink file &&
2ba2fe29
CB
485 test bar = "$(cat file)" &&
486 git stash apply &&
cc143f12
CG
487 test_path_is_symlink file &&
488 test "$(test_readlink file)" = file2
2ba2fe29
CB
489'
490
491test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
492 git reset --hard &&
493 git rm file &&
494 ln -s file2 file &&
495 git stash save "file to symlink (stage rm)" &&
456296b5 496 test_path_is_file_not_symlink file &&
2ba2fe29
CB
497 test bar = "$(cat file)" &&
498 git stash apply &&
cc143f12
CG
499 test_path_is_symlink file &&
500 test "$(test_readlink file)" = file2
2ba2fe29
CB
501'
502
503test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
504 git reset --hard &&
505 rm file &&
506 ln -s file2 file &&
507 git add file &&
508 git stash save "file to symlink (full stage)" &&
456296b5 509 test_path_is_file_not_symlink file &&
2ba2fe29
CB
510 test bar = "$(cat file)" &&
511 git stash apply &&
cc143f12
CG
512 test_path_is_symlink file &&
513 test "$(test_readlink file)" = file2
2ba2fe29
CB
514'
515
516# This test creates a commit with a symlink used for the following tests
517
889c6f0e 518test_expect_success 'stash symlink to file' '
2ba2fe29 519 git reset --hard &&
889c6f0e 520 test_ln_s_add file filelink &&
2ba2fe29
CB
521 git commit -m "Add symlink" &&
522 rm filelink &&
523 cp file filelink &&
889c6f0e
JS
524 git stash save "symlink to file"
525'
526
527test_expect_success SYMLINKS 'this must have re-created the symlink' '
2ba2fe29 528 test -h filelink &&
889c6f0e
JS
529 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
530'
531
532test_expect_success 'unstash must re-create the file' '
2ba2fe29
CB
533 git stash apply &&
534 ! test -h filelink &&
535 test bar = "$(cat file)"
536'
537
889c6f0e 538test_expect_success 'stash symlink to file (stage rm)' '
2ba2fe29
CB
539 git reset --hard &&
540 git rm filelink &&
541 cp file filelink &&
889c6f0e
JS
542 git stash save "symlink to file (stage rm)"
543'
544
545test_expect_success SYMLINKS 'this must have re-created the symlink' '
2ba2fe29 546 test -h filelink &&
889c6f0e
JS
547 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
548'
549
550test_expect_success 'unstash must re-create the file' '
2ba2fe29
CB
551 git stash apply &&
552 ! test -h filelink &&
553 test bar = "$(cat file)"
554'
555
889c6f0e 556test_expect_success 'stash symlink to file (full stage)' '
2ba2fe29
CB
557 git reset --hard &&
558 rm filelink &&
559 cp file filelink &&
560 git add filelink &&
889c6f0e
JS
561 git stash save "symlink to file (full stage)"
562'
563
564test_expect_success SYMLINKS 'this must have re-created the symlink' '
2ba2fe29 565 test -h filelink &&
889c6f0e
JS
566 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
567'
568
569test_expect_success 'unstash must re-create the file' '
2ba2fe29
CB
570 git stash apply &&
571 ! test -h filelink &&
572 test bar = "$(cat file)"
573'
574
575test_expect_failure 'stash directory to file' '
576 git reset --hard &&
577 mkdir dir &&
578 echo foo >dir/file &&
579 git add dir/file &&
580 git commit -m "Add file in dir" &&
581 rm -fr dir &&
582 echo bar >dir &&
583 git stash save "directory to file" &&
f01f9482 584 test_path_is_dir dir &&
2ba2fe29
CB
585 test foo = "$(cat dir/file)" &&
586 test_must_fail git stash apply &&
587 test bar = "$(cat dir)" &&
588 git reset --soft HEAD^
589'
590
591test_expect_failure 'stash file to directory' '
592 git reset --hard &&
593 rm file &&
594 mkdir file &&
595 echo foo >file/file &&
596 git stash save "file to directory" &&
f01f9482 597 test_path_is_file file &&
2ba2fe29
CB
598 test bar = "$(cat file)" &&
599 git stash apply &&
f01f9482 600 test_path_is_file file/file &&
2ba2fe29
CB
601 test foo = "$(cat file/file)"
602'
603
93415f58
JT
604test_expect_success 'giving too many ref arguments does not modify files' '
605 git stash clear &&
606 test_when_finished "git reset --hard HEAD" &&
607 echo foo >file2 &&
608 git stash &&
609 echo bar >file2 &&
610 git stash &&
611 test-tool chmtime =123456789 file2 &&
612 for type in apply pop "branch stash-branch"
613 do
614 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
6789275d 615 test_grep "Too many revisions" err &&
93415f58
JT
616 test 123456789 = $(test-tool chmtime -g file2) || return 1
617 done
618'
619
620test_expect_success 'drop: too many arguments errors out (does nothing)' '
621 git stash list >expect &&
622 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
6789275d 623 test_grep "Too many revisions" err &&
93415f58
JT
624 git stash list >actual &&
625 test_cmp expect actual
626'
627
628test_expect_success 'show: too many arguments errors out (does nothing)' '
629 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
6789275d 630 test_grep "Too many revisions" err &&
93415f58
JT
631 test_must_be_empty out
632'
633
c95bc226
JT
634test_expect_success 'stash create - no changes' '
635 git stash clear &&
636 test_when_finished "git reset --hard HEAD" &&
637 git reset --hard &&
638 git stash create >actual &&
639 test_must_be_empty actual
640'
641
daf7a0c0
JS
642test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
643 git stash clear &&
644 test_when_finished "git reset --hard HEAD" &&
645 git reset --hard &&
6a0b88b2 646 echo foo >>file &&
daf7a0c0
JS
647 STASH_ID=$(git stash create) &&
648 git reset --hard &&
649 git stash branch stash-branch ${STASH_ID} &&
cbc75a12 650 test_when_finished "git reset --hard HEAD && git checkout main &&
6a0b88b2 651 git branch -D stash-branch" &&
daf7a0c0
JS
652 test $(git ls-files --modified | wc -l) -eq 1
653'
654
655test_expect_success 'stash branch - stashes on stack, stash-like argument' '
656 git stash clear &&
657 test_when_finished "git reset --hard HEAD" &&
658 git reset --hard &&
6a0b88b2 659 echo foo >>file &&
daf7a0c0
JS
660 git stash &&
661 test_when_finished "git stash drop" &&
6a0b88b2 662 echo bar >>file &&
daf7a0c0
JS
663 STASH_ID=$(git stash create) &&
664 git reset --hard &&
665 git stash branch stash-branch ${STASH_ID} &&
cbc75a12 666 test_when_finished "git reset --hard HEAD && git checkout main &&
6a0b88b2 667 git branch -D stash-branch" &&
daf7a0c0
JS
668 test $(git ls-files --modified | wc -l) -eq 1
669'
670
93415f58
JT
671test_expect_success 'stash branch complains with no arguments' '
672 test_must_fail git stash branch 2>err &&
6789275d 673 test_grep "No branch name specified" err
93415f58
JT
674'
675
11452114 676test_expect_success 'stash show format defaults to --stat' '
daf7a0c0
JS
677 git stash clear &&
678 test_when_finished "git reset --hard HEAD" &&
679 git reset --hard &&
6a0b88b2 680 echo foo >>file &&
daf7a0c0
JS
681 git stash &&
682 test_when_finished "git stash drop" &&
6a0b88b2 683 echo bar >>file &&
daf7a0c0
JS
684 STASH_ID=$(git stash create) &&
685 git reset --hard &&
3fcb8878 686 cat >expected <<-EOF &&
dc801e71 687 file | 1 +
7f814632 688 1 file changed, 1 insertion(+)
3fcb8878
BC
689 EOF
690 git stash show ${STASH_ID} >actual &&
1108cea7 691 test_cmp expected actual
daf7a0c0 692'
3fcb8878 693
11452114
JN
694test_expect_success 'stash show - stashes on stack, stash-like argument' '
695 git stash clear &&
696 test_when_finished "git reset --hard HEAD" &&
697 git reset --hard &&
6a0b88b2 698 echo foo >>file &&
11452114
JN
699 git stash &&
700 test_when_finished "git stash drop" &&
6a0b88b2 701 echo bar >>file &&
11452114
JN
702 STASH_ID=$(git stash create) &&
703 git reset --hard &&
704 echo "1 0 file" >expected &&
705 git stash show --numstat ${STASH_ID} >actual &&
706 test_cmp expected actual
707'
708
9027fa9e 709test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
3fcb8878
BC
710 git stash clear &&
711 test_when_finished "git reset --hard HEAD" &&
712 git reset --hard &&
6a0b88b2 713 echo foo >>file &&
3fcb8878
BC
714 git stash &&
715 test_when_finished "git stash drop" &&
6a0b88b2 716 echo bar >>file &&
3fcb8878
BC
717 STASH_ID=$(git stash create) &&
718 git reset --hard &&
719 cat >expected <<-EOF &&
720 diff --git a/file b/file
721 index 7601807..935fbd3 100644
722 --- a/file
723 +++ b/file
724 @@ -1 +1,2 @@
725 baz
726 +bar
727 EOF
728 git stash show -p ${STASH_ID} >actual &&
c7848150 729 diff_cmp expected actual
3fcb8878
BC
730'
731
9027fa9e 732test_expect_success 'stash show - no stashes on stack, stash-like argument' '
3fcb8878
BC
733 git stash clear &&
734 test_when_finished "git reset --hard HEAD" &&
735 git reset --hard &&
6a0b88b2 736 echo foo >>file &&
3fcb8878
BC
737 STASH_ID=$(git stash create) &&
738 git reset --hard &&
11452114
JN
739 echo "1 0 file" >expected &&
740 git stash show --numstat ${STASH_ID} >actual &&
741 test_cmp expected actual
3fcb8878
BC
742'
743
9027fa9e 744test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
daf7a0c0
JS
745 git stash clear &&
746 test_when_finished "git reset --hard HEAD" &&
747 git reset --hard &&
6a0b88b2 748 echo foo >>file &&
daf7a0c0
JS
749 STASH_ID=$(git stash create) &&
750 git reset --hard &&
3fcb8878
BC
751 cat >expected <<-EOF &&
752 diff --git a/file b/file
753 index 7601807..71b52c4 100644
754 --- a/file
755 +++ b/file
756 @@ -1 +1,2 @@
757 baz
758 +foo
759 EOF
760 git stash show -p ${STASH_ID} >actual &&
c7848150 761 diff_cmp expected actual
daf7a0c0
JS
762'
763
8e407bc8
TG
764test_expect_success 'stash show --patience shows diff' '
765 git reset --hard &&
766 echo foo >>file &&
767 STASH_ID=$(git stash create) &&
768 git reset --hard &&
769 cat >expected <<-EOF &&
770 diff --git a/file b/file
771 index 7601807..71b52c4 100644
772 --- a/file
773 +++ b/file
774 @@ -1 +1,2 @@
775 baz
776 +foo
777 EOF
778 git stash show --patience ${STASH_ID} >actual &&
c7848150 779 diff_cmp expected actual
8e407bc8
TG
780'
781
ab8ad46e 782test_expect_success 'drop: fail early if specified stash is not a stash ref' '
daf7a0c0
JS
783 git stash clear &&
784 test_when_finished "git reset --hard HEAD && git stash clear" &&
785 git reset --hard &&
6a0b88b2 786 echo foo >file &&
daf7a0c0 787 git stash &&
6a0b88b2 788 echo bar >file &&
daf7a0c0 789 git stash &&
8d66bb05 790 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
daf7a0c0
JS
791 git stash pop &&
792 test bar = "$(cat file)" &&
793 git reset --hard HEAD
794'
795
ab8ad46e 796test_expect_success 'pop: fail early if specified stash is not a stash ref' '
daf7a0c0
JS
797 git stash clear &&
798 test_when_finished "git reset --hard HEAD && git stash clear" &&
799 git reset --hard &&
6a0b88b2 800 echo foo >file &&
daf7a0c0 801 git stash &&
6a0b88b2 802 echo bar >file &&
daf7a0c0 803 git stash &&
8d66bb05 804 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
daf7a0c0
JS
805 git stash pop &&
806 test bar = "$(cat file)" &&
807 git reset --hard HEAD
808'
809
7be8b3ba 810test_expect_success 'ref with non-existent reflog' '
daf7a0c0 811 git stash clear &&
6a0b88b2
PSU
812 echo bar5 >file &&
813 echo bar6 >file2 &&
daf7a0c0
JS
814 git add file2 &&
815 git stash &&
1ae96444 816 test_must_fail git rev-parse --quiet --verify does-not-exist &&
8d66bb05
JS
817 test_must_fail git stash drop does-not-exist &&
818 test_must_fail git stash drop does-not-exist@{0} &&
819 test_must_fail git stash pop does-not-exist &&
820 test_must_fail git stash pop does-not-exist@{0} &&
821 test_must_fail git stash apply does-not-exist &&
822 test_must_fail git stash apply does-not-exist@{0} &&
823 test_must_fail git stash show does-not-exist &&
824 test_must_fail git stash show does-not-exist@{0} &&
825 test_must_fail git stash branch tmp does-not-exist &&
826 test_must_fail git stash branch tmp does-not-exist@{0} &&
daf7a0c0
JS
827 git stash drop
828'
829
830test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
831 git stash clear &&
8d66bb05 832 test_must_fail git stash drop stash@{0} &&
6a0b88b2
PSU
833 echo bar5 >file &&
834 echo bar6 >file2 &&
daf7a0c0
JS
835 git add file2 &&
836 git stash &&
9355fc9c
JS
837 test_must_fail git stash drop stash@{1} &&
838 test_must_fail git stash pop stash@{1} &&
839 test_must_fail git stash apply stash@{1} &&
840 test_must_fail git stash show stash@{1} &&
841 test_must_fail git stash branch tmp stash@{1} &&
daf7a0c0
JS
842 git stash drop
843'
844
a56c8f5a
AW
845test_expect_success 'invalid ref of the form "n", n >= N' '
846 git stash clear &&
847 test_must_fail git stash drop 0 &&
848 echo bar5 >file &&
849 echo bar6 >file2 &&
850 git add file2 &&
851 git stash &&
852 test_must_fail git stash drop 1 &&
853 test_must_fail git stash pop 1 &&
854 test_must_fail git stash apply 1 &&
855 test_must_fail git stash show 1 &&
856 test_must_fail git stash branch tmp 1 &&
857 git stash drop
858'
859
63b50c8f
TG
860test_expect_success 'valid ref of the form "n", n < N' '
861 git stash clear &&
862 echo bar5 >file &&
863 echo bar6 >file2 &&
864 git add file2 &&
865 git stash &&
866 git stash show 0 &&
867 git stash branch tmp 0 &&
cbc75a12 868 git checkout main &&
63b50c8f
TG
869 git stash &&
870 git stash apply 0 &&
871 git reset --hard &&
872 git stash pop 0 &&
873 git stash &&
874 git stash drop 0 &&
875 test_must_fail git stash drop
876'
877
ab8ad46e 878test_expect_success 'branch: do not drop the stash if the branch exists' '
835d6a1f
TC
879 git stash clear &&
880 echo foo >file &&
881 git add file &&
882 git commit -m initial &&
883 echo bar >file &&
884 git stash &&
cbc75a12 885 test_must_fail git stash branch main stash@{0} &&
835d6a1f
TC
886 git rev-parse stash@{0} --
887'
888
ab8ad46e 889test_expect_success 'branch: should not drop the stash if the apply fails' '
b04e6915
JT
890 git stash clear &&
891 git reset HEAD~1 --hard &&
892 echo foo >file &&
893 git add file &&
894 git commit -m initial &&
895 echo bar >file &&
896 git stash &&
897 echo baz >file &&
cbc75a12 898 test_when_finished "git checkout main" &&
b04e6915
JT
899 test_must_fail git stash branch new_branch stash@{0} &&
900 git rev-parse stash@{0} --
901'
902
ab8ad46e 903test_expect_success 'apply: show same status as git status (relative to ./)' '
6eaf92f3 904 git stash clear &&
80bc042b 905 mkdir -p subdir &&
6eaf92f3
PK
906 echo 1 >subdir/subfile1 &&
907 echo 2 >subdir/subfile2 &&
908 git add subdir/subfile1 &&
909 git commit -m subdir &&
910 (
911 cd subdir &&
912 echo x >subfile1 &&
913 echo x >../file &&
914 git status >../expect &&
915 git stash &&
916 sane_unset GIT_MERGE_VERBOSITY &&
917 git stash apply
918 ) |
1790f4fe 919 sed -e 1d >actual && # drop "Saved..."
1108cea7 920 test_cmp expect actual
6eaf92f3
PK
921'
922
6a0b88b2 923cat >expect <<EOF
44df2e29
JM
924diff --git a/HEAD b/HEAD
925new file mode 100644
926index 0000000..fe0cbee
927--- /dev/null
928+++ b/HEAD
929@@ -0,0 +1 @@
930+file-not-a-ref
931EOF
932
933test_expect_success 'stash where working directory contains "HEAD" file' '
934 git stash clear &&
935 git reset --hard &&
6a0b88b2 936 echo file-not-a-ref >HEAD &&
44df2e29
JM
937 git add HEAD &&
938 test_tick &&
939 git stash &&
940 git diff-files --quiet &&
941 git diff-index --cached --quiet HEAD &&
942 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
6a0b88b2 943 git diff stash^..stash >output &&
c7848150 944 diff_cmp expect output
44df2e29
JM
945'
946
bd514cad
RR
947test_expect_success 'store called with invalid commit' '
948 test_must_fail git stash store foo
949'
950
d9b66345
JH
951test_expect_success 'store called with non-stash commit' '
952 test_must_fail git stash store HEAD
953'
954
bd514cad
RR
955test_expect_success 'store updates stash ref and reflog' '
956 git stash clear &&
957 git reset --hard &&
958 echo quux >bazzy &&
959 git add bazzy &&
960 STASH_ID=$(git stash create) &&
961 git reset --hard &&
79b04f9b 962 test_path_is_missing bazzy &&
bd514cad 963 git stash store -m quuxery $STASH_ID &&
cbc5cf7c 964 test $(git rev-parse stash) = $STASH_ID &&
d0ab0584 965 git reflog --format=%H stash| grep $STASH_ID &&
bd514cad
RR
966 git stash pop &&
967 grep quux bazzy
968'
969
2a07e437
ØW
970test_expect_success 'handle stash specification with spaces' '
971 git stash clear &&
972 echo pig >file &&
973 git stash &&
974 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
975 test_tick &&
976 echo cow >file &&
977 git stash &&
978 git stash apply "stash@{$stamp}" &&
979 grep pig file
980'
981
288c67ca
JK
982test_expect_success 'setup stash with index and worktree changes' '
983 git stash clear &&
984 git reset --hard &&
985 echo index >file &&
986 git add file &&
987 echo working >file &&
988 git stash
989'
990
1e20a407 991test_expect_success 'stash list -p shows simple diff' '
f2f3fc95
JK
992 cat >expect <<-EOF &&
993 stash@{0}
288c67ca
JK
994
995 diff --git a/file b/file
996 index 257cc56..d26b33d 100644
997 --- a/file
998 +++ b/file
999 @@ -1 +1 @@
1000 -foo
1001 +working
1002 EOF
f2f3fc95 1003 git stash list --format=%gd -p >actual &&
c7848150 1004 diff_cmp expect actual
288c67ca
JK
1005'
1006
1007test_expect_success 'stash list --cc shows combined diff' '
1008 cat >expect <<-\EOF &&
f2f3fc95 1009 stash@{0}
288c67ca
JK
1010
1011 diff --cc file
1012 index 257cc56,9015a7a..d26b33d
1013 --- a/file
1014 +++ b/file
1015 @@@ -1,1 -1,1 +1,1 @@@
1016 - foo
1017 -index
1018 ++working
1019 EOF
f2f3fc95 1020 git stash list --format=%gd -p --cc >actual &&
c7848150 1021 diff_cmp expect actual
288c67ca
JK
1022'
1023
9d4e28ea
JK
1024test_expect_success 'stash is not confused by partial renames' '
1025 mv file renamed &&
1026 git add renamed &&
1027 git stash &&
1028 git stash apply &&
1029 test_path_is_file renamed &&
1030 test_path_is_missing file
1031'
1032
f5727e26
TG
1033test_expect_success 'push -m shows right message' '
1034 >foo &&
1035 git add foo &&
1036 git stash push -m "test message" &&
cbc75a12 1037 echo "stash@{0}: On main: test message" >expect &&
f5727e26
TG
1038 git stash list -1 >actual &&
1039 test_cmp expect actual
1040'
1041
5675473f
PH
1042test_expect_success 'push -m also works without space' '
1043 >foo &&
1044 git add foo &&
1045 git stash push -m"unspaced test message" &&
cbc75a12 1046 echo "stash@{0}: On main: unspaced test message" >expect &&
5675473f
PH
1047 git stash list -1 >actual &&
1048 test_cmp expect actual
1049'
1050
1051test_expect_success 'store -m foo shows right message' '
1052 git stash clear &&
1053 git reset --hard &&
1054 echo quux >bazzy &&
1055 git add bazzy &&
1056 STASH_ID=$(git stash create) &&
1057 git stash store -m "store m" $STASH_ID &&
1058 echo "stash@{0}: store m" >expect &&
1059 git stash list -1 >actual &&
1060 test_cmp expect actual
1061'
1062
1063test_expect_success 'store -mfoo shows right message' '
1064 git stash clear &&
1065 git reset --hard &&
1066 echo quux >bazzy &&
1067 git add bazzy &&
1068 STASH_ID=$(git stash create) &&
1069 git stash store -m"store mfoo" $STASH_ID &&
1070 echo "stash@{0}: store mfoo" >expect &&
1071 git stash list -1 >actual &&
1072 test_cmp expect actual
1073'
1074
1075test_expect_success 'store --message=foo shows right message' '
1076 git stash clear &&
1077 git reset --hard &&
1078 echo quux >bazzy &&
1079 git add bazzy &&
1080 STASH_ID=$(git stash create) &&
1081 git stash store --message="store message=foo" $STASH_ID &&
1082 echo "stash@{0}: store message=foo" >expect &&
1083 git stash list -1 >actual &&
1084 test_cmp expect actual
1085'
1086
1087test_expect_success 'store --message foo shows right message' '
1088 git stash clear &&
1089 git reset --hard &&
1090 echo quux >bazzy &&
1091 git add bazzy &&
1092 STASH_ID=$(git stash create) &&
1093 git stash store --message "store message foo" $STASH_ID &&
1094 echo "stash@{0}: store message foo" >expect &&
1095 git stash list -1 >actual &&
1096 test_cmp expect actual
1097'
1098
1099test_expect_success 'push -mfoo uses right message' '
1100 >foo &&
1101 git add foo &&
1102 git stash push -m"test mfoo" &&
cbc75a12 1103 echo "stash@{0}: On main: test mfoo" >expect &&
5675473f
PH
1104 git stash list -1 >actual &&
1105 test_cmp expect actual
1106'
1107
1108test_expect_success 'push --message foo is synonym for -mfoo' '
1109 >foo &&
1110 git add foo &&
1111 git stash push --message "test message foo" &&
cbc75a12 1112 echo "stash@{0}: On main: test message foo" >expect &&
5675473f
PH
1113 git stash list -1 >actual &&
1114 test_cmp expect actual
1115'
1116
1117test_expect_success 'push --message=foo is synonym for -mfoo' '
1118 >foo &&
1119 git add foo &&
1120 git stash push --message="test message=foo" &&
cbc75a12 1121 echo "stash@{0}: On main: test message=foo" >expect &&
5675473f
PH
1122 git stash list -1 >actual &&
1123 test_cmp expect actual
1124'
1125
1126test_expect_success 'push -m shows right message' '
1127 >foo &&
1128 git add foo &&
1129 git stash push -m "test m foo" &&
cbc75a12 1130 echo "stash@{0}: On main: test m foo" >expect &&
5675473f
PH
1131 git stash list -1 >actual &&
1132 test_cmp expect actual
1133'
1134
6f5ccd4d
TG
1135test_expect_success 'create stores correct message' '
1136 >foo &&
1137 git add foo &&
1138 STASH_ID=$(git stash create "create test message") &&
cbc75a12 1139 echo "On main: create test message" >expect &&
6f5ccd4d
TG
1140 git show --pretty=%s -s ${STASH_ID} >actual &&
1141 test_cmp expect actual
1142'
1143
ceaf037f
GC
1144test_expect_success 'create when branch name has /' '
1145 test_when_finished "git checkout main" &&
1146 git checkout -b some/topic &&
1147 >foo &&
1148 git add foo &&
1149 STASH_ID=$(git stash create "create test message") &&
1150 echo "On some/topic: create test message" >expect &&
1151 git show --pretty=%s -s ${STASH_ID} >actual &&
1152 test_cmp expect actual
1153'
1154
6f5ccd4d
TG
1155test_expect_success 'create with multiple arguments for the message' '
1156 >foo &&
1157 git add foo &&
1158 STASH_ID=$(git stash create test untracked) &&
cbc75a12 1159 echo "On main: test untracked" >expect &&
6f5ccd4d
TG
1160 git show --pretty=%s -s ${STASH_ID} >actual &&
1161 test_cmp expect actual
1162'
1163
4e9bf3dd 1164test_expect_success 'create in a detached state' '
cbc75a12 1165 test_when_finished "git checkout main" &&
4e9bf3dd
JT
1166 git checkout HEAD~1 &&
1167 >foo &&
1168 git add foo &&
1169 STASH_ID=$(git stash create) &&
1170 HEAD_ID=$(git rev-parse --short HEAD) &&
1171 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1172 git show --pretty=%s -s ${STASH_ID} >actual &&
1173 test_cmp expect actual
1174'
1175
df6bba09
TG
1176test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1177 >foo &&
1178 >bar &&
1179 git add foo bar &&
1180 git stash push -- foo &&
1181 test_path_is_file bar &&
1182 test_path_is_missing foo &&
1183 git stash pop &&
1184 test_path_is_file foo &&
1185 test_path_is_file bar
1186'
1187
468817ba 1188test_expect_success 'stash --patch <pathspec> stash and restores the file' '
e6659b77
PW
1189 test_write_lines b c >file &&
1190 git commit -m "add a few lines" file &&
1191 test_write_lines a b c d >file &&
1192 test_write_lines b c d >expect-file &&
1193 echo changed-other-file >other-file &&
468817ba 1194 test_write_lines s y n | git stash -m "stash bar" --patch file &&
e6659b77
PW
1195 test_cmp expect-file file &&
1196 echo changed-other-file >expect &&
1197 test_cmp expect other-file &&
1198 git checkout HEAD -- file &&
1199 git stash pop &&
1200 test_cmp expect other-file &&
1201 test_write_lines a b c >expect &&
1202 test_cmp expect file
1203'
1204
1205test_expect_success 'stash <pathspec> -p is rejected' '
1206 test_must_fail git stash file -p 2>err &&
1207 test_grep "subcommand wasn${SQ}t specified; ${SQ}push${SQ} can${SQ}t be assumed due to unexpected token ${SQ}file${SQ}" err
1208'
1209
22fc703e
PS
1210test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1211 mkdir sub &&
1212 >foo &&
1213 >bar &&
1214 git add foo bar &&
1215 (
1216 cd sub &&
1217 git stash push -- ../foo
1218 ) &&
1219 test_path_is_file bar &&
1220 test_path_is_missing foo &&
1221 git stash pop &&
1222 test_path_is_file foo &&
1223 test_path_is_file bar
1224'
1225
df6bba09
TG
1226test_expect_success 'stash with multiple pathspec arguments' '
1227 >foo &&
1228 >bar &&
1229 >extra &&
1230 git add foo bar extra &&
1231 git stash push -- foo bar &&
1232 test_path_is_missing bar &&
1233 test_path_is_missing foo &&
1234 test_path_is_file extra &&
1235 git stash pop &&
1236 test_path_is_file foo &&
1237 test_path_is_file bar &&
1238 test_path_is_file extra
1239'
1240
1241test_expect_success 'stash with file including $IFS character' '
1242 >"foo bar" &&
1243 >foo &&
1244 >bar &&
1245 git add foo* &&
1246 git stash push -- "foo b*" &&
1247 test_path_is_missing "foo bar" &&
1248 test_path_is_file foo &&
1249 test_path_is_file bar &&
1250 git stash pop &&
1251 test_path_is_file "foo bar" &&
1252 test_path_is_file foo &&
1253 test_path_is_file bar
1254'
1255
1256test_expect_success 'stash with pathspec matching multiple paths' '
a8fcc0ac
JC
1257 echo original >file &&
1258 echo original >other-file &&
1259 git commit -m "two" file other-file &&
1260 echo modified >file &&
1261 echo modified >other-file &&
1262 git stash push -- "*file" &&
1263 echo original >expect &&
1264 test_cmp expect file &&
1265 test_cmp expect other-file &&
1266 git stash pop &&
1267 echo modified >expect &&
1268 test_cmp expect file &&
1269 test_cmp expect other-file
df6bba09
TG
1270'
1271
1272test_expect_success 'stash push -p with pathspec shows no changes only once' '
1273 >foo &&
1274 git add foo &&
1275 git commit -m "tmp" &&
1276 git stash push -p foo >actual &&
1277 echo "No local changes to save" >expect &&
1278 git reset --hard HEAD~ &&
1108cea7 1279 test_cmp expect actual
df6bba09
TG
1280'
1281
ab8ad46e 1282test_expect_success 'push <pathspec>: show no changes when there are none' '
df6bba09
TG
1283 >foo &&
1284 git add foo &&
1285 git commit -m "tmp" &&
1286 git stash push foo >actual &&
1287 echo "No local changes to save" >expect &&
1288 git reset --hard HEAD~ &&
1108cea7 1289 test_cmp expect actual
df6bba09
TG
1290'
1291
ab8ad46e 1292test_expect_success 'push: <pathspec> not in the repository errors out' '
df6bba09
TG
1293 >untracked &&
1294 test_must_fail git stash push untracked &&
1295 test_path_is_file untracked
1296'
1297
1ac528c0
PSU
1298test_expect_success 'push: -q is quiet with changes' '
1299 >foo &&
1300 git add foo &&
1301 git stash push -q >output 2>&1 &&
1302 test_must_be_empty output
1303'
1304
1305test_expect_success 'push: -q is quiet with no changes' '
1306 git stash push -q >output 2>&1 &&
1307 test_must_be_empty output
1308'
1309
1310test_expect_success 'push: -q is quiet even if there is no initial commit' '
1311 git init foo_dir &&
1312 test_when_finished rm -rf foo_dir &&
1313 (
1314 cd foo_dir &&
1315 >bar &&
1316 test_must_fail git stash push -q >output 2>&1 &&
1317 test_must_be_empty output
1318 )
1319'
1320
df6bba09
TG
1321test_expect_success 'untracked files are left in place when -u is not given' '
1322 >file &&
1323 git add file &&
1324 >untracked &&
1325 git stash push file &&
1326 test_path_is_file untracked
1327'
1328
9e140909
TG
1329test_expect_success 'stash without verb with pathspec' '
1330 >"foo bar" &&
1331 >foo &&
1332 >bar &&
1333 git add foo* &&
1334 git stash -- "foo b*" &&
1335 test_path_is_missing "foo bar" &&
1336 test_path_is_file foo &&
1337 test_path_is_file bar &&
1338 git stash pop &&
1339 test_path_is_file "foo bar" &&
1340 test_path_is_file foo &&
1341 test_path_is_file bar
1342'
1343
e0e7f99e
TG
1344test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1345 git reset &&
1346 >foo &&
1347 >bar &&
1348 git add foo bar &&
1349 git commit -m "test" &&
1350 echo "foo" >foo &&
1351 echo "bar" >bar &&
1352 git stash -k -- foo &&
1353 test "",bar = $(cat foo),$(cat bar) &&
1354 git stash pop &&
1355 test foo,bar = $(cat foo),$(cat bar)
1356'
1357
bba067d2
TG
1358test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1359 git reset &&
80bc042b 1360 mkdir -p subdir &&
bba067d2
TG
1361 >subdir/untracked &&
1362 >subdir/tracked1 &&
1363 >subdir/tracked2 &&
1364 git add subdir/tracked* &&
1365 git stash -- subdir/ &&
1366 test_path_is_missing subdir/tracked1 &&
1367 test_path_is_missing subdir/tracked2 &&
1368 test_path_is_file subdir/untracked &&
1369 git stash pop &&
1370 test_path_is_file subdir/tracked1 &&
1371 test_path_is_file subdir/tracked2 &&
1372 test_path_is_file subdir/untracked
1373'
1374
1375test_expect_success 'stash -- <subdir> works with binary files' '
1376 git reset &&
80bc042b 1377 mkdir -p subdir &&
bba067d2
TG
1378 >subdir/untracked &&
1379 >subdir/tracked &&
1380 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1381 git add subdir/tracked* &&
1382 git stash -- subdir/ &&
1383 test_path_is_missing subdir/tracked &&
1384 test_path_is_missing subdir/tracked-binary &&
1385 test_path_is_file subdir/untracked &&
1386 git stash pop &&
1387 test_path_is_file subdir/tracked &&
1388 test_path_is_file subdir/tracked-binary &&
1389 test_path_is_file subdir/untracked
1390'
1391
0640897d
TG
1392test_expect_success 'stash with user.name and user.email set works' '
1393 test_config user.name "A U Thor" &&
1394 test_config user.email "a.u@thor" &&
1395 git stash
1396'
1397
3bc2111f
SD
1398test_expect_success 'stash works when user.name and user.email are not set' '
1399 git reset &&
1400 >1 &&
1401 git add 1 &&
1402 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1403 git stash &&
1404 git show -s --format="%an <%ae>" refs/stash >actual &&
1405 test_cmp expect actual &&
1406 >2 &&
1407 git add 2 &&
1408 test_config user.useconfigonly true &&
3bc2111f
SD
1409 (
1410 sane_unset GIT_AUTHOR_NAME &&
1411 sane_unset GIT_AUTHOR_EMAIL &&
1412 sane_unset GIT_COMMITTER_NAME &&
1413 sane_unset GIT_COMMITTER_EMAIL &&
1414 test_unconfig user.email &&
1415 test_unconfig user.name &&
1416 test_must_fail git commit -m "should fail" &&
1417 echo "git stash <git@stash>" >expect &&
1418 >2 &&
1419 git stash &&
1420 git show -s --format="%an <%ae>" refs/stash >actual &&
1421 test_cmp expect actual
1422 )
1423'
1424
b932f6a5
TG
1425test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1426 test_commit to-remove to-remove &&
1427 git rm to-remove &&
1428 git stash --keep-index &&
1429 test_path_is_missing to-remove
1430'
1431
e3209bd4
PS
1432test_expect_success 'stash --keep-index --include-untracked with empty tree' '
1433 test_when_finished "rm -rf empty" &&
1434 git init empty &&
1435 (
1436 cd empty &&
1437 git commit --allow-empty --message "empty" &&
1438 echo content >file &&
1439 git stash push --keep-index --include-untracked &&
1440 test_path_is_missing file &&
1441 git stash pop &&
1442 echo content >expect &&
1443 test_cmp expect file
1444 )
1445'
1446
bc303718 1447test_expect_success 'stash export and import round-trip stashes' '
1448 git reset &&
1449 >untracked &&
1450 >tracked1 &&
1451 >tracked2 &&
1452 git add tracked* &&
1453 git stash -- &&
1454 >subdir/untracked &&
1455 >subdir/tracked1 &&
1456 >subdir/tracked2 &&
1457 git add subdir/tracked* &&
1458 git stash --include-untracked -- subdir/ &&
1459 git tag t-stash0 stash@{0} &&
1460 git tag t-stash1 stash@{1} &&
1461 simple=$(git stash export --print) &&
1462 git stash clear &&
1463 git stash import "$simple" &&
1464 test_cmp_rev stash@{0} t-stash0 &&
1465 test_cmp_rev stash@{1} t-stash1 &&
1466 git stash export --to-ref refs/heads/foo &&
1467 test_cmp_rev "$(test_oid empty_tree)" foo: &&
1468 test_cmp_rev "$(test_oid empty_tree)" foo^: &&
1469 test_cmp_rev t-stash0 foo^2 &&
1470 test_cmp_rev t-stash1 foo^^2 &&
1471 git log --first-parent --format="%s" refs/heads/foo >log &&
1472 grep "^git stash: " log >log2 &&
1473 test_line_count = 13 log2 &&
1474 git stash clear &&
1475 git stash import foo &&
1476 test_cmp_rev stash@{0} t-stash0 &&
1477 test_cmp_rev stash@{1} t-stash1
1478'
1479
1480test_expect_success 'stash import appends commits' '
1481 git log --format=oneline -g refs/stash >out &&
1482 cat out out >out2 &&
1483 git stash import refs/heads/foo &&
1484 git log --format=oneline -g refs/stash >actual &&
1485 test_line_count = $(wc -l <out2) actual
1486'
1487
1488test_expect_success 'stash export can accept specified stashes' '
1489 git stash clear &&
1490 git stash import foo &&
1491 git stash export --to-ref refs/heads/bar stash@{1} stash@{0} &&
1492 git stash clear &&
1493 git stash import refs/heads/bar &&
1494 test_cmp_rev stash@{1} t-stash0 &&
1495 test_cmp_rev stash@{0} t-stash1 &&
1496 git log --format=oneline -g refs/stash >actual &&
1497 test_line_count = 2 actual
1498'
1499
1500test_expect_success 'stash export rejects invalid arguments' '
1501 test_must_fail git stash export --print --to-ref refs/heads/invalid 2>err &&
1502 grep "exactly one of --print and --to-ref is required" err &&
1503 test_must_fail git stash export 2>err2 &&
1504 grep "exactly one of --print and --to-ref is required" err2
1505'
1506
1507test_expect_success 'stash can import and export zero stashes' '
1508 git stash clear &&
1509 git stash export --to-ref refs/heads/baz &&
1510 test_cmp_rev "$(test_oid empty_tree)" baz: &&
1511 test_cmp_rev "$(test_oid export_base)" baz &&
1512 test_must_fail git rev-parse baz^1 &&
1513 git stash import baz &&
1514 test_must_fail git rev-parse refs/stash
1515'
1516
1517test_expect_success 'stash rejects invalid attempts to import commits' '
1518 git stash import foo &&
1519 test_must_fail git stash import HEAD 2>output &&
1520 oid=$(git rev-parse HEAD) &&
1521 grep "$oid is not a valid exported stash commit" output &&
1522 test_cmp_rev stash@{0} t-stash0 &&
1523
1524 git checkout --orphan orphan &&
1525 git commit-tree $(test_oid empty_tree) -p "$oid" -p "$oid^" -m "" >fake-commit &&
1526 git update-ref refs/heads/orphan "$(cat fake-commit)" &&
1527 oid=$(git rev-parse HEAD) &&
1528 test_must_fail git stash import orphan 2>output &&
1529 grep "found stash commit $oid without expected prefix" output &&
1530 test_cmp_rev stash@{0} t-stash0 &&
1531
1532 git checkout --orphan orphan2 &&
1533 git commit-tree $(test_oid empty_tree) -m "" >fake-commit &&
1534 git update-ref refs/heads/orphan2 "$(cat fake-commit)" &&
1535 oid=$(git rev-parse HEAD) &&
1536 test_must_fail git stash import orphan2 2>output &&
1537 grep "found root commit $oid with invalid data" output &&
1538 test_cmp_rev stash@{0} t-stash0
1539'
1540
34933d0e
TG
1541test_expect_success 'stash apply should succeed with unmodified file' '
1542 echo base >file &&
1543 git add file &&
1544 git commit -m base &&
1545
1546 # now stash a modification
1547 echo modified >file &&
1548 git stash &&
1549
1550 # make the file stat dirty
1551 cp file other &&
1552 mv other file &&
1553
1554 git stash apply
1555'
1556
4a58c3d7
JS
1557test_expect_success 'stash handles skip-worktree entries nicely' '
1558 test_commit A &&
1559 echo changed >A.t &&
1560 git add A.t &&
1561 git update-index --skip-worktree A.t &&
1562 rm A.t &&
1563 git stash &&
1564
1565 git rev-parse --verify refs/stash:A.t
1566'
1567
d42bab44
NS
1568
1569BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1570
1571test_expect_success 'stash with core.fsyncmethod=batch' "
1572 test_create_unique_files 2 4 files_base_dir &&
1573 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1574
1575 # The files were untracked, so use the third parent,
1576 # which contains the untracked files
1577 git ls-tree -r stash^3 -- ./files_base_dir/ |
1578 test_parse_ls_tree_oids >stashed_files_oids &&
1579
1580 # We created 2 dirs with 4 files each (8 files total) above
1581 test_line_count = 8 stashed_files_oids &&
1582 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1583 test_cmp stashed_files_oids stashed_files_actual
1584"
1585
1586
3d40e372 1587test_expect_success 'git stash succeeds despite directory/file change' '
4dbf7f30
EN
1588 test_create_repo directory_file_switch_v1 &&
1589 (
1590 cd directory_file_switch_v1 &&
1591 test_commit init &&
1592
1593 test_write_lines this file has some words >filler &&
1594 git add filler &&
1595 git commit -m filler &&
1596
1597 git rm filler &&
1598 mkdir filler &&
1599 echo contents >filler/file &&
1600 git stash push
1601 )
1602'
1603
bee8691f 1604test_expect_success 'git stash can pop file -> directory saved changes' '
4dbf7f30
EN
1605 test_create_repo directory_file_switch_v2 &&
1606 (
1607 cd directory_file_switch_v2 &&
1608 test_commit init &&
1609
1610 test_write_lines this file has some words >filler &&
1611 git add filler &&
1612 git commit -m filler &&
1613
1614 git rm filler &&
1615 mkdir filler &&
1616 echo contents >filler/file &&
1617 cp filler/file expect &&
1618 git stash push --include-untracked &&
1619 git stash apply --index &&
1620 test_cmp expect filler/file
1621 )
1622'
1623
bee8691f 1624test_expect_success 'git stash can pop directory -> file saved changes' '
4dbf7f30
EN
1625 test_create_repo directory_file_switch_v3 &&
1626 (
1627 cd directory_file_switch_v3 &&
1628 test_commit init &&
1629
1630 mkdir filler &&
1631 test_write_lines some words >filler/file1 &&
1632 test_write_lines and stuff >filler/file2 &&
1633 git add filler &&
1634 git commit -m filler &&
1635
1636 git rm -rf filler &&
1637 echo contents >filler &&
1638 cp filler expect &&
1639 git stash push --include-untracked &&
1640 git stash apply --index &&
1641 test_cmp expect filler
1642 )
1643'
1644
71cade5a
EN
1645test_expect_success 'restore untracked files even when we hit conflicts' '
1646 git init restore_untracked_after_conflict &&
1647 (
1648 cd restore_untracked_after_conflict &&
1649
1650 echo hi >a &&
1651 echo there >b &&
1652 git add . &&
1653 git commit -m first &&
1654 echo hello >a &&
1655 echo something >c &&
1656
1657 git stash push --include-untracked &&
1658
1659 echo conflict >a &&
1660 git add a &&
1661 git commit -m second &&
1662
1663 test_must_fail git stash pop &&
1664
1665 test_path_is_file c
1666 )
1667'
1668
d2058cb2
PS
1669test_expect_success 'stash create reports a locked index' '
1670 test_when_finished "rm -rf repo" &&
1671 git init repo &&
1672 (
1673 cd repo &&
1674 test_commit A A.file &&
1675 echo change >A.file &&
1676 touch .git/index.lock &&
1677
d2058cb2 1678 test_must_fail git stash create 2>err &&
9455397a
HY
1679 test_grep "error: could not write index" err &&
1680 test_grep "error: Unable to create '.*index.lock'" err
d2058cb2
PS
1681 )
1682'
1683
1684test_expect_success 'stash push reports a locked index' '
1685 test_when_finished "rm -rf repo" &&
1686 git init repo &&
1687 (
1688 cd repo &&
1689 test_commit A A.file &&
1690 echo change >A.file &&
1691 touch .git/index.lock &&
1692
d2058cb2 1693 test_must_fail git stash push 2>err &&
9455397a
HY
1694 test_grep "error: could not write index" err &&
1695 test_grep "error: Unable to create '.*index.lock'" err
d2058cb2
PS
1696 )
1697'
1698
1699test_expect_success 'stash apply reports a locked index' '
1700 test_when_finished "rm -rf repo" &&
1701 git init repo &&
1702 (
1703 cd repo &&
1704 test_commit A A.file &&
1705 echo change >A.file &&
1706 git stash push &&
1707 touch .git/index.lock &&
1708
d2058cb2 1709 test_must_fail git stash apply 2>err &&
9455397a
HY
1710 test_grep "error: could not write index" err &&
1711 test_grep "error: Unable to create '.*index.lock'" err
d2058cb2
PS
1712 )
1713'
1714
ffb36c64
J
1715test_expect_success 'submodules does not affect the branch recorded in stash message' '
1716 git init sub_project &&
1717 (
1718 cd sub_project &&
1719 echo "Initial content in sub_project" >sub_file.txt &&
1720 git add sub_file.txt &&
1721 git commit -m "Initial commit in sub_project"
1722 ) &&
1723
1724 git init main_project &&
1725 (
1726 cd main_project &&
1727 echo "Initial content in main_project" >main_file.txt &&
1728 git add main_file.txt &&
1729 git commit -m "Initial commit in main_project" &&
1730
1731 git -c protocol.file.allow=always submodule add ../sub_project sub &&
1732 git commit -m "Added submodule sub_project" &&
1733
1734 git checkout -b feature_main &&
1735 git -C sub checkout -b feature_sub &&
1736
1737 git checkout -b work_branch &&
1738 echo "Important work to be stashed" >work_item.txt &&
1739 git add work_item.txt &&
1740 git stash push -m "custom stash for work_branch" &&
1741
1742 git stash list >../actual_stash_list.txt &&
1743 grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt
1744 )
1745'
1746
3ea35c64
JK
1747test_expect_success SANITIZE_LEAK 'stash show handles -- without leaking' '
1748 git stash show --
1749'
1750
cd439487
JK
1751test_expect_success 'controlled error return on unrecognized option' '
1752 test_expect_code 129 git stash show -p --invalid 2>usage &&
1753 grep -e "^usage: git stash show" usage
1754'
1755
9842c0c7
BK
1756test_expect_success 'stash.index=true implies --index' '
1757 # setup for a few related tests
1758 test_commit file base &&
1759 echo index >file &&
1760 git add file &&
1761 echo working >file &&
1762 git stash &&
1763
1764 test_when_finished "git reset --hard" &&
1765 git -c stash.index=true stash apply &&
1766 echo index >expect &&
1767 git show :0:file >actual &&
1768 test_cmp expect actual &&
1769 echo working >expect &&
1770 test_cmp expect file
1771'
1772
1773test_expect_success 'stash.index=true overridden by --no-index' '
1774 test_when_finished "git reset --hard" &&
1775 git -c stash.index=true stash apply --no-index &&
1776 echo base >expect &&
1777 git show :0:file >actual &&
1778 test_cmp expect actual &&
1779 echo working >expect &&
1780 test_cmp expect file
1781'
1782
1783test_expect_success 'stash.index=false overridden by --index' '
1784 test_when_finished "git reset --hard" &&
1785 git -c stash.index=false stash apply --index &&
1786 echo index >expect &&
1787 git show :0:file >actual &&
1788 test_cmp expect actual &&
1789 echo working >expect &&
1790 test_cmp expect file
1791'
1792
150937c4 1793test_done