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