]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3903-stash.sh
The eighth 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
PK
904 git stash clear &&
905 echo 1 >subdir/subfile1 &&
906 echo 2 >subdir/subfile2 &&
907 git add subdir/subfile1 &&
908 git commit -m subdir &&
909 (
910 cd subdir &&
911 echo x >subfile1 &&
912 echo x >../file &&
913 git status >../expect &&
914 git stash &&
915 sane_unset GIT_MERGE_VERBOSITY &&
916 git stash apply
917 ) |
1790f4fe 918 sed -e 1d >actual && # drop "Saved..."
1108cea7 919 test_cmp expect actual
6eaf92f3
PK
920'
921
6a0b88b2 922cat >expect <<EOF
44df2e29
JM
923diff --git a/HEAD b/HEAD
924new file mode 100644
925index 0000000..fe0cbee
926--- /dev/null
927+++ b/HEAD
928@@ -0,0 +1 @@
929+file-not-a-ref
930EOF
931
932test_expect_success 'stash where working directory contains "HEAD" file' '
933 git stash clear &&
934 git reset --hard &&
6a0b88b2 935 echo file-not-a-ref >HEAD &&
44df2e29
JM
936 git add HEAD &&
937 test_tick &&
938 git stash &&
939 git diff-files --quiet &&
940 git diff-index --cached --quiet HEAD &&
941 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
6a0b88b2 942 git diff stash^..stash >output &&
c7848150 943 diff_cmp expect output
44df2e29
JM
944'
945
bd514cad
RR
946test_expect_success 'store called with invalid commit' '
947 test_must_fail git stash store foo
948'
949
d9b66345
JH
950test_expect_success 'store called with non-stash commit' '
951 test_must_fail git stash store HEAD
952'
953
bd514cad
RR
954test_expect_success 'store updates stash ref and reflog' '
955 git stash clear &&
956 git reset --hard &&
957 echo quux >bazzy &&
958 git add bazzy &&
959 STASH_ID=$(git stash create) &&
960 git reset --hard &&
79b04f9b 961 test_path_is_missing bazzy &&
bd514cad 962 git stash store -m quuxery $STASH_ID &&
cbc5cf7c 963 test $(git rev-parse stash) = $STASH_ID &&
d0ab0584 964 git reflog --format=%H stash| grep $STASH_ID &&
bd514cad
RR
965 git stash pop &&
966 grep quux bazzy
967'
968
2a07e437
ØW
969test_expect_success 'handle stash specification with spaces' '
970 git stash clear &&
971 echo pig >file &&
972 git stash &&
973 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
974 test_tick &&
975 echo cow >file &&
976 git stash &&
977 git stash apply "stash@{$stamp}" &&
978 grep pig file
979'
980
288c67ca
JK
981test_expect_success 'setup stash with index and worktree changes' '
982 git stash clear &&
983 git reset --hard &&
984 echo index >file &&
985 git add file &&
986 echo working >file &&
987 git stash
988'
989
1e20a407 990test_expect_success 'stash list -p shows simple diff' '
f2f3fc95
JK
991 cat >expect <<-EOF &&
992 stash@{0}
288c67ca
JK
993
994 diff --git a/file b/file
995 index 257cc56..d26b33d 100644
996 --- a/file
997 +++ b/file
998 @@ -1 +1 @@
999 -foo
1000 +working
1001 EOF
f2f3fc95 1002 git stash list --format=%gd -p >actual &&
c7848150 1003 diff_cmp expect actual
288c67ca
JK
1004'
1005
1006test_expect_success 'stash list --cc shows combined diff' '
1007 cat >expect <<-\EOF &&
f2f3fc95 1008 stash@{0}
288c67ca
JK
1009
1010 diff --cc file
1011 index 257cc56,9015a7a..d26b33d
1012 --- a/file
1013 +++ b/file
1014 @@@ -1,1 -1,1 +1,1 @@@
1015 - foo
1016 -index
1017 ++working
1018 EOF
f2f3fc95 1019 git stash list --format=%gd -p --cc >actual &&
c7848150 1020 diff_cmp expect actual
288c67ca
JK
1021'
1022
9d4e28ea
JK
1023test_expect_success 'stash is not confused by partial renames' '
1024 mv file renamed &&
1025 git add renamed &&
1026 git stash &&
1027 git stash apply &&
1028 test_path_is_file renamed &&
1029 test_path_is_missing file
1030'
1031
f5727e26
TG
1032test_expect_success 'push -m shows right message' '
1033 >foo &&
1034 git add foo &&
1035 git stash push -m "test message" &&
cbc75a12 1036 echo "stash@{0}: On main: test message" >expect &&
f5727e26
TG
1037 git stash list -1 >actual &&
1038 test_cmp expect actual
1039'
1040
5675473f
PH
1041test_expect_success 'push -m also works without space' '
1042 >foo &&
1043 git add foo &&
1044 git stash push -m"unspaced test message" &&
cbc75a12 1045 echo "stash@{0}: On main: unspaced test message" >expect &&
5675473f
PH
1046 git stash list -1 >actual &&
1047 test_cmp expect actual
1048'
1049
1050test_expect_success 'store -m foo shows right message' '
1051 git stash clear &&
1052 git reset --hard &&
1053 echo quux >bazzy &&
1054 git add bazzy &&
1055 STASH_ID=$(git stash create) &&
1056 git stash store -m "store m" $STASH_ID &&
1057 echo "stash@{0}: store m" >expect &&
1058 git stash list -1 >actual &&
1059 test_cmp expect actual
1060'
1061
1062test_expect_success 'store -mfoo shows right message' '
1063 git stash clear &&
1064 git reset --hard &&
1065 echo quux >bazzy &&
1066 git add bazzy &&
1067 STASH_ID=$(git stash create) &&
1068 git stash store -m"store mfoo" $STASH_ID &&
1069 echo "stash@{0}: store mfoo" >expect &&
1070 git stash list -1 >actual &&
1071 test_cmp expect actual
1072'
1073
1074test_expect_success 'store --message=foo shows right message' '
1075 git stash clear &&
1076 git reset --hard &&
1077 echo quux >bazzy &&
1078 git add bazzy &&
1079 STASH_ID=$(git stash create) &&
1080 git stash store --message="store message=foo" $STASH_ID &&
1081 echo "stash@{0}: store message=foo" >expect &&
1082 git stash list -1 >actual &&
1083 test_cmp expect actual
1084'
1085
1086test_expect_success 'store --message foo shows right message' '
1087 git stash clear &&
1088 git reset --hard &&
1089 echo quux >bazzy &&
1090 git add bazzy &&
1091 STASH_ID=$(git stash create) &&
1092 git stash store --message "store message foo" $STASH_ID &&
1093 echo "stash@{0}: store message foo" >expect &&
1094 git stash list -1 >actual &&
1095 test_cmp expect actual
1096'
1097
1098test_expect_success 'push -mfoo uses right message' '
1099 >foo &&
1100 git add foo &&
1101 git stash push -m"test mfoo" &&
cbc75a12 1102 echo "stash@{0}: On main: test mfoo" >expect &&
5675473f
PH
1103 git stash list -1 >actual &&
1104 test_cmp expect actual
1105'
1106
1107test_expect_success 'push --message foo is synonym for -mfoo' '
1108 >foo &&
1109 git add foo &&
1110 git stash push --message "test message foo" &&
cbc75a12 1111 echo "stash@{0}: On main: test message foo" >expect &&
5675473f
PH
1112 git stash list -1 >actual &&
1113 test_cmp expect actual
1114'
1115
1116test_expect_success 'push --message=foo is synonym for -mfoo' '
1117 >foo &&
1118 git add foo &&
1119 git stash push --message="test message=foo" &&
cbc75a12 1120 echo "stash@{0}: On main: test message=foo" >expect &&
5675473f
PH
1121 git stash list -1 >actual &&
1122 test_cmp expect actual
1123'
1124
1125test_expect_success 'push -m shows right message' '
1126 >foo &&
1127 git add foo &&
1128 git stash push -m "test m foo" &&
cbc75a12 1129 echo "stash@{0}: On main: test m foo" >expect &&
5675473f
PH
1130 git stash list -1 >actual &&
1131 test_cmp expect actual
1132'
1133
6f5ccd4d
TG
1134test_expect_success 'create stores correct message' '
1135 >foo &&
1136 git add foo &&
1137 STASH_ID=$(git stash create "create test message") &&
cbc75a12 1138 echo "On main: create test message" >expect &&
6f5ccd4d
TG
1139 git show --pretty=%s -s ${STASH_ID} >actual &&
1140 test_cmp expect actual
1141'
1142
ceaf037f
GC
1143test_expect_success 'create when branch name has /' '
1144 test_when_finished "git checkout main" &&
1145 git checkout -b some/topic &&
1146 >foo &&
1147 git add foo &&
1148 STASH_ID=$(git stash create "create test message") &&
1149 echo "On some/topic: create test message" >expect &&
1150 git show --pretty=%s -s ${STASH_ID} >actual &&
1151 test_cmp expect actual
1152'
1153
6f5ccd4d
TG
1154test_expect_success 'create with multiple arguments for the message' '
1155 >foo &&
1156 git add foo &&
1157 STASH_ID=$(git stash create test untracked) &&
cbc75a12 1158 echo "On main: test untracked" >expect &&
6f5ccd4d
TG
1159 git show --pretty=%s -s ${STASH_ID} >actual &&
1160 test_cmp expect actual
1161'
1162
4e9bf3dd 1163test_expect_success 'create in a detached state' '
cbc75a12 1164 test_when_finished "git checkout main" &&
4e9bf3dd
JT
1165 git checkout HEAD~1 &&
1166 >foo &&
1167 git add foo &&
1168 STASH_ID=$(git stash create) &&
1169 HEAD_ID=$(git rev-parse --short HEAD) &&
1170 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1171 git show --pretty=%s -s ${STASH_ID} >actual &&
1172 test_cmp expect actual
1173'
1174
df6bba09
TG
1175test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1176 >foo &&
1177 >bar &&
1178 git add foo bar &&
1179 git stash push -- foo &&
1180 test_path_is_file bar &&
1181 test_path_is_missing foo &&
1182 git stash pop &&
1183 test_path_is_file foo &&
1184 test_path_is_file bar
1185'
1186
468817ba 1187test_expect_success 'stash --patch <pathspec> stash and restores the file' '
e6659b77
PW
1188 test_write_lines b c >file &&
1189 git commit -m "add a few lines" file &&
1190 test_write_lines a b c d >file &&
1191 test_write_lines b c d >expect-file &&
1192 echo changed-other-file >other-file &&
468817ba 1193 test_write_lines s y n | git stash -m "stash bar" --patch file &&
e6659b77
PW
1194 test_cmp expect-file file &&
1195 echo changed-other-file >expect &&
1196 test_cmp expect other-file &&
1197 git checkout HEAD -- file &&
1198 git stash pop &&
1199 test_cmp expect other-file &&
1200 test_write_lines a b c >expect &&
1201 test_cmp expect file
1202'
1203
1204test_expect_success 'stash <pathspec> -p is rejected' '
1205 test_must_fail git stash file -p 2>err &&
1206 test_grep "subcommand wasn${SQ}t specified; ${SQ}push${SQ} can${SQ}t be assumed due to unexpected token ${SQ}file${SQ}" err
1207'
1208
22fc703e
PS
1209test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1210 mkdir sub &&
1211 >foo &&
1212 >bar &&
1213 git add foo bar &&
1214 (
1215 cd sub &&
1216 git stash push -- ../foo
1217 ) &&
1218 test_path_is_file bar &&
1219 test_path_is_missing foo &&
1220 git stash pop &&
1221 test_path_is_file foo &&
1222 test_path_is_file bar
1223'
1224
df6bba09
TG
1225test_expect_success 'stash with multiple pathspec arguments' '
1226 >foo &&
1227 >bar &&
1228 >extra &&
1229 git add foo bar extra &&
1230 git stash push -- foo bar &&
1231 test_path_is_missing bar &&
1232 test_path_is_missing foo &&
1233 test_path_is_file extra &&
1234 git stash pop &&
1235 test_path_is_file foo &&
1236 test_path_is_file bar &&
1237 test_path_is_file extra
1238'
1239
1240test_expect_success 'stash with file including $IFS character' '
1241 >"foo bar" &&
1242 >foo &&
1243 >bar &&
1244 git add foo* &&
1245 git stash push -- "foo b*" &&
1246 test_path_is_missing "foo bar" &&
1247 test_path_is_file foo &&
1248 test_path_is_file bar &&
1249 git stash pop &&
1250 test_path_is_file "foo bar" &&
1251 test_path_is_file foo &&
1252 test_path_is_file bar
1253'
1254
1255test_expect_success 'stash with pathspec matching multiple paths' '
a8fcc0ac
JC
1256 echo original >file &&
1257 echo original >other-file &&
1258 git commit -m "two" file other-file &&
1259 echo modified >file &&
1260 echo modified >other-file &&
1261 git stash push -- "*file" &&
1262 echo original >expect &&
1263 test_cmp expect file &&
1264 test_cmp expect other-file &&
1265 git stash pop &&
1266 echo modified >expect &&
1267 test_cmp expect file &&
1268 test_cmp expect other-file
df6bba09
TG
1269'
1270
1271test_expect_success 'stash push -p with pathspec shows no changes only once' '
1272 >foo &&
1273 git add foo &&
1274 git commit -m "tmp" &&
1275 git stash push -p foo >actual &&
1276 echo "No local changes to save" >expect &&
1277 git reset --hard HEAD~ &&
1108cea7 1278 test_cmp expect actual
df6bba09
TG
1279'
1280
ab8ad46e 1281test_expect_success 'push <pathspec>: show no changes when there are none' '
df6bba09
TG
1282 >foo &&
1283 git add foo &&
1284 git commit -m "tmp" &&
1285 git stash push foo >actual &&
1286 echo "No local changes to save" >expect &&
1287 git reset --hard HEAD~ &&
1108cea7 1288 test_cmp expect actual
df6bba09
TG
1289'
1290
ab8ad46e 1291test_expect_success 'push: <pathspec> not in the repository errors out' '
df6bba09
TG
1292 >untracked &&
1293 test_must_fail git stash push untracked &&
1294 test_path_is_file untracked
1295'
1296
1ac528c0
PSU
1297test_expect_success 'push: -q is quiet with changes' '
1298 >foo &&
1299 git add foo &&
1300 git stash push -q >output 2>&1 &&
1301 test_must_be_empty output
1302'
1303
1304test_expect_success 'push: -q is quiet with no changes' '
1305 git stash push -q >output 2>&1 &&
1306 test_must_be_empty output
1307'
1308
1309test_expect_success 'push: -q is quiet even if there is no initial commit' '
1310 git init foo_dir &&
1311 test_when_finished rm -rf foo_dir &&
1312 (
1313 cd foo_dir &&
1314 >bar &&
1315 test_must_fail git stash push -q >output 2>&1 &&
1316 test_must_be_empty output
1317 )
1318'
1319
df6bba09
TG
1320test_expect_success 'untracked files are left in place when -u is not given' '
1321 >file &&
1322 git add file &&
1323 >untracked &&
1324 git stash push file &&
1325 test_path_is_file untracked
1326'
1327
9e140909
TG
1328test_expect_success 'stash without verb with pathspec' '
1329 >"foo bar" &&
1330 >foo &&
1331 >bar &&
1332 git add foo* &&
1333 git stash -- "foo b*" &&
1334 test_path_is_missing "foo bar" &&
1335 test_path_is_file foo &&
1336 test_path_is_file bar &&
1337 git stash pop &&
1338 test_path_is_file "foo bar" &&
1339 test_path_is_file foo &&
1340 test_path_is_file bar
1341'
1342
e0e7f99e
TG
1343test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1344 git reset &&
1345 >foo &&
1346 >bar &&
1347 git add foo bar &&
1348 git commit -m "test" &&
1349 echo "foo" >foo &&
1350 echo "bar" >bar &&
1351 git stash -k -- foo &&
1352 test "",bar = $(cat foo),$(cat bar) &&
1353 git stash pop &&
1354 test foo,bar = $(cat foo),$(cat bar)
1355'
1356
bba067d2
TG
1357test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1358 git reset &&
1359 >subdir/untracked &&
1360 >subdir/tracked1 &&
1361 >subdir/tracked2 &&
1362 git add subdir/tracked* &&
1363 git stash -- subdir/ &&
1364 test_path_is_missing subdir/tracked1 &&
1365 test_path_is_missing subdir/tracked2 &&
1366 test_path_is_file subdir/untracked &&
1367 git stash pop &&
1368 test_path_is_file subdir/tracked1 &&
1369 test_path_is_file subdir/tracked2 &&
1370 test_path_is_file subdir/untracked
1371'
1372
1373test_expect_success 'stash -- <subdir> works with binary files' '
1374 git reset &&
1375 >subdir/untracked &&
1376 >subdir/tracked &&
1377 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1378 git add subdir/tracked* &&
1379 git stash -- subdir/ &&
1380 test_path_is_missing subdir/tracked &&
1381 test_path_is_missing subdir/tracked-binary &&
1382 test_path_is_file subdir/untracked &&
1383 git stash pop &&
1384 test_path_is_file subdir/tracked &&
1385 test_path_is_file subdir/tracked-binary &&
1386 test_path_is_file subdir/untracked
1387'
1388
0640897d
TG
1389test_expect_success 'stash with user.name and user.email set works' '
1390 test_config user.name "A U Thor" &&
1391 test_config user.email "a.u@thor" &&
1392 git stash
1393'
1394
3bc2111f
SD
1395test_expect_success 'stash works when user.name and user.email are not set' '
1396 git reset &&
1397 >1 &&
1398 git add 1 &&
1399 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1400 git stash &&
1401 git show -s --format="%an <%ae>" refs/stash >actual &&
1402 test_cmp expect actual &&
1403 >2 &&
1404 git add 2 &&
1405 test_config user.useconfigonly true &&
3bc2111f
SD
1406 (
1407 sane_unset GIT_AUTHOR_NAME &&
1408 sane_unset GIT_AUTHOR_EMAIL &&
1409 sane_unset GIT_COMMITTER_NAME &&
1410 sane_unset GIT_COMMITTER_EMAIL &&
1411 test_unconfig user.email &&
1412 test_unconfig user.name &&
1413 test_must_fail git commit -m "should fail" &&
1414 echo "git stash <git@stash>" >expect &&
1415 >2 &&
1416 git stash &&
1417 git show -s --format="%an <%ae>" refs/stash >actual &&
1418 test_cmp expect actual
1419 )
1420'
1421
b932f6a5
TG
1422test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1423 test_commit to-remove to-remove &&
1424 git rm to-remove &&
1425 git stash --keep-index &&
1426 test_path_is_missing to-remove
1427'
1428
e3209bd4
PS
1429test_expect_success 'stash --keep-index --include-untracked with empty tree' '
1430 test_when_finished "rm -rf empty" &&
1431 git init empty &&
1432 (
1433 cd empty &&
1434 git commit --allow-empty --message "empty" &&
1435 echo content >file &&
1436 git stash push --keep-index --include-untracked &&
1437 test_path_is_missing file &&
1438 git stash pop &&
1439 echo content >expect &&
1440 test_cmp expect file
1441 )
1442'
1443
bc303718 1444test_expect_success 'stash export and import round-trip stashes' '
1445 git reset &&
1446 >untracked &&
1447 >tracked1 &&
1448 >tracked2 &&
1449 git add tracked* &&
1450 git stash -- &&
1451 >subdir/untracked &&
1452 >subdir/tracked1 &&
1453 >subdir/tracked2 &&
1454 git add subdir/tracked* &&
1455 git stash --include-untracked -- subdir/ &&
1456 git tag t-stash0 stash@{0} &&
1457 git tag t-stash1 stash@{1} &&
1458 simple=$(git stash export --print) &&
1459 git stash clear &&
1460 git stash import "$simple" &&
1461 test_cmp_rev stash@{0} t-stash0 &&
1462 test_cmp_rev stash@{1} t-stash1 &&
1463 git stash export --to-ref refs/heads/foo &&
1464 test_cmp_rev "$(test_oid empty_tree)" foo: &&
1465 test_cmp_rev "$(test_oid empty_tree)" foo^: &&
1466 test_cmp_rev t-stash0 foo^2 &&
1467 test_cmp_rev t-stash1 foo^^2 &&
1468 git log --first-parent --format="%s" refs/heads/foo >log &&
1469 grep "^git stash: " log >log2 &&
1470 test_line_count = 13 log2 &&
1471 git stash clear &&
1472 git stash import foo &&
1473 test_cmp_rev stash@{0} t-stash0 &&
1474 test_cmp_rev stash@{1} t-stash1
1475'
1476
1477test_expect_success 'stash import appends commits' '
1478 git log --format=oneline -g refs/stash >out &&
1479 cat out out >out2 &&
1480 git stash import refs/heads/foo &&
1481 git log --format=oneline -g refs/stash >actual &&
1482 test_line_count = $(wc -l <out2) actual
1483'
1484
1485test_expect_success 'stash export can accept specified stashes' '
1486 git stash clear &&
1487 git stash import foo &&
1488 git stash export --to-ref refs/heads/bar stash@{1} stash@{0} &&
1489 git stash clear &&
1490 git stash import refs/heads/bar &&
1491 test_cmp_rev stash@{1} t-stash0 &&
1492 test_cmp_rev stash@{0} t-stash1 &&
1493 git log --format=oneline -g refs/stash >actual &&
1494 test_line_count = 2 actual
1495'
1496
1497test_expect_success 'stash export rejects invalid arguments' '
1498 test_must_fail git stash export --print --to-ref refs/heads/invalid 2>err &&
1499 grep "exactly one of --print and --to-ref is required" err &&
1500 test_must_fail git stash export 2>err2 &&
1501 grep "exactly one of --print and --to-ref is required" err2
1502'
1503
1504test_expect_success 'stash can import and export zero stashes' '
1505 git stash clear &&
1506 git stash export --to-ref refs/heads/baz &&
1507 test_cmp_rev "$(test_oid empty_tree)" baz: &&
1508 test_cmp_rev "$(test_oid export_base)" baz &&
1509 test_must_fail git rev-parse baz^1 &&
1510 git stash import baz &&
1511 test_must_fail git rev-parse refs/stash
1512'
1513
1514test_expect_success 'stash rejects invalid attempts to import commits' '
1515 git stash import foo &&
1516 test_must_fail git stash import HEAD 2>output &&
1517 oid=$(git rev-parse HEAD) &&
1518 grep "$oid is not a valid exported stash commit" output &&
1519 test_cmp_rev stash@{0} t-stash0 &&
1520
1521 git checkout --orphan orphan &&
1522 git commit-tree $(test_oid empty_tree) -p "$oid" -p "$oid^" -m "" >fake-commit &&
1523 git update-ref refs/heads/orphan "$(cat fake-commit)" &&
1524 oid=$(git rev-parse HEAD) &&
1525 test_must_fail git stash import orphan 2>output &&
1526 grep "found stash commit $oid without expected prefix" output &&
1527 test_cmp_rev stash@{0} t-stash0 &&
1528
1529 git checkout --orphan orphan2 &&
1530 git commit-tree $(test_oid empty_tree) -m "" >fake-commit &&
1531 git update-ref refs/heads/orphan2 "$(cat fake-commit)" &&
1532 oid=$(git rev-parse HEAD) &&
1533 test_must_fail git stash import orphan2 2>output &&
1534 grep "found root commit $oid with invalid data" output &&
1535 test_cmp_rev stash@{0} t-stash0
1536'
1537
34933d0e
TG
1538test_expect_success 'stash apply should succeed with unmodified file' '
1539 echo base >file &&
1540 git add file &&
1541 git commit -m base &&
1542
1543 # now stash a modification
1544 echo modified >file &&
1545 git stash &&
1546
1547 # make the file stat dirty
1548 cp file other &&
1549 mv other file &&
1550
1551 git stash apply
1552'
1553
4a58c3d7
JS
1554test_expect_success 'stash handles skip-worktree entries nicely' '
1555 test_commit A &&
1556 echo changed >A.t &&
1557 git add A.t &&
1558 git update-index --skip-worktree A.t &&
1559 rm A.t &&
1560 git stash &&
1561
1562 git rev-parse --verify refs/stash:A.t
1563'
1564
d42bab44
NS
1565
1566BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1567
1568test_expect_success 'stash with core.fsyncmethod=batch' "
1569 test_create_unique_files 2 4 files_base_dir &&
1570 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1571
1572 # The files were untracked, so use the third parent,
1573 # which contains the untracked files
1574 git ls-tree -r stash^3 -- ./files_base_dir/ |
1575 test_parse_ls_tree_oids >stashed_files_oids &&
1576
1577 # We created 2 dirs with 4 files each (8 files total) above
1578 test_line_count = 8 stashed_files_oids &&
1579 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1580 test_cmp stashed_files_oids stashed_files_actual
1581"
1582
1583
3d40e372 1584test_expect_success 'git stash succeeds despite directory/file change' '
4dbf7f30
EN
1585 test_create_repo directory_file_switch_v1 &&
1586 (
1587 cd directory_file_switch_v1 &&
1588 test_commit init &&
1589
1590 test_write_lines this file has some words >filler &&
1591 git add filler &&
1592 git commit -m filler &&
1593
1594 git rm filler &&
1595 mkdir filler &&
1596 echo contents >filler/file &&
1597 git stash push
1598 )
1599'
1600
bee8691f 1601test_expect_success 'git stash can pop file -> directory saved changes' '
4dbf7f30
EN
1602 test_create_repo directory_file_switch_v2 &&
1603 (
1604 cd directory_file_switch_v2 &&
1605 test_commit init &&
1606
1607 test_write_lines this file has some words >filler &&
1608 git add filler &&
1609 git commit -m filler &&
1610
1611 git rm filler &&
1612 mkdir filler &&
1613 echo contents >filler/file &&
1614 cp filler/file expect &&
1615 git stash push --include-untracked &&
1616 git stash apply --index &&
1617 test_cmp expect filler/file
1618 )
1619'
1620
bee8691f 1621test_expect_success 'git stash can pop directory -> file saved changes' '
4dbf7f30
EN
1622 test_create_repo directory_file_switch_v3 &&
1623 (
1624 cd directory_file_switch_v3 &&
1625 test_commit init &&
1626
1627 mkdir filler &&
1628 test_write_lines some words >filler/file1 &&
1629 test_write_lines and stuff >filler/file2 &&
1630 git add filler &&
1631 git commit -m filler &&
1632
1633 git rm -rf filler &&
1634 echo contents >filler &&
1635 cp filler expect &&
1636 git stash push --include-untracked &&
1637 git stash apply --index &&
1638 test_cmp expect filler
1639 )
1640'
1641
71cade5a
EN
1642test_expect_success 'restore untracked files even when we hit conflicts' '
1643 git init restore_untracked_after_conflict &&
1644 (
1645 cd restore_untracked_after_conflict &&
1646
1647 echo hi >a &&
1648 echo there >b &&
1649 git add . &&
1650 git commit -m first &&
1651 echo hello >a &&
1652 echo something >c &&
1653
1654 git stash push --include-untracked &&
1655
1656 echo conflict >a &&
1657 git add a &&
1658 git commit -m second &&
1659
1660 test_must_fail git stash pop &&
1661
1662 test_path_is_file c
1663 )
1664'
1665
d2058cb2
PS
1666test_expect_success 'stash create reports a locked index' '
1667 test_when_finished "rm -rf repo" &&
1668 git init repo &&
1669 (
1670 cd repo &&
1671 test_commit A A.file &&
1672 echo change >A.file &&
1673 touch .git/index.lock &&
1674
1675 cat >expect <<-EOF &&
1676 error: could not write index
1677 EOF
1678 test_must_fail git stash create 2>err &&
1679 test_cmp expect err
1680 )
1681'
1682
1683test_expect_success 'stash push reports a locked index' '
1684 test_when_finished "rm -rf repo" &&
1685 git init repo &&
1686 (
1687 cd repo &&
1688 test_commit A A.file &&
1689 echo change >A.file &&
1690 touch .git/index.lock &&
1691
1692 cat >expect <<-EOF &&
1693 error: could not write index
1694 EOF
1695 test_must_fail git stash push 2>err &&
1696 test_cmp expect err
1697 )
1698'
1699
1700test_expect_success 'stash apply reports a locked index' '
1701 test_when_finished "rm -rf repo" &&
1702 git init repo &&
1703 (
1704 cd repo &&
1705 test_commit A A.file &&
1706 echo change >A.file &&
1707 git stash push &&
1708 touch .git/index.lock &&
1709
1710 cat >expect <<-EOF &&
1711 error: could not write index
1712 EOF
1713 test_must_fail git stash apply 2>err &&
1714 test_cmp expect err
1715 )
1716'
1717
ffb36c64
J
1718test_expect_success 'submodules does not affect the branch recorded in stash message' '
1719 git init sub_project &&
1720 (
1721 cd sub_project &&
1722 echo "Initial content in sub_project" >sub_file.txt &&
1723 git add sub_file.txt &&
1724 git commit -m "Initial commit in sub_project"
1725 ) &&
1726
1727 git init main_project &&
1728 (
1729 cd main_project &&
1730 echo "Initial content in main_project" >main_file.txt &&
1731 git add main_file.txt &&
1732 git commit -m "Initial commit in main_project" &&
1733
1734 git -c protocol.file.allow=always submodule add ../sub_project sub &&
1735 git commit -m "Added submodule sub_project" &&
1736
1737 git checkout -b feature_main &&
1738 git -C sub checkout -b feature_sub &&
1739
1740 git checkout -b work_branch &&
1741 echo "Important work to be stashed" >work_item.txt &&
1742 git add work_item.txt &&
1743 git stash push -m "custom stash for work_branch" &&
1744
1745 git stash list >../actual_stash_list.txt &&
1746 grep "On work_branch: custom stash for work_branch" ../actual_stash_list.txt
1747 )
1748'
1749
150937c4 1750test_done