]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3903-stash.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[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
934test_expect_success 'store updates stash ref and reflog' '
935 git stash clear &&
936 git reset --hard &&
937 echo quux >bazzy &&
938 git add bazzy &&
939 STASH_ID=$(git stash create) &&
940 git reset --hard &&
79b04f9b 941 test_path_is_missing bazzy &&
bd514cad 942 git stash store -m quuxery $STASH_ID &&
cbc5cf7c 943 test $(git rev-parse stash) = $STASH_ID &&
d0ab0584 944 git reflog --format=%H stash| grep $STASH_ID &&
bd514cad
RR
945 git stash pop &&
946 grep quux bazzy
947'
948
2a07e437
ØW
949test_expect_success 'handle stash specification with spaces' '
950 git stash clear &&
951 echo pig >file &&
952 git stash &&
953 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
954 test_tick &&
955 echo cow >file &&
956 git stash &&
957 git stash apply "stash@{$stamp}" &&
958 grep pig file
959'
960
288c67ca
JK
961test_expect_success 'setup stash with index and worktree changes' '
962 git stash clear &&
963 git reset --hard &&
964 echo index >file &&
965 git add file &&
966 echo working >file &&
967 git stash
968'
969
1e20a407 970test_expect_success 'stash list -p shows simple diff' '
f2f3fc95
JK
971 cat >expect <<-EOF &&
972 stash@{0}
288c67ca
JK
973
974 diff --git a/file b/file
975 index 257cc56..d26b33d 100644
976 --- a/file
977 +++ b/file
978 @@ -1 +1 @@
979 -foo
980 +working
981 EOF
f2f3fc95 982 git stash list --format=%gd -p >actual &&
c7848150 983 diff_cmp expect actual
288c67ca
JK
984'
985
986test_expect_success 'stash list --cc shows combined diff' '
987 cat >expect <<-\EOF &&
f2f3fc95 988 stash@{0}
288c67ca
JK
989
990 diff --cc file
991 index 257cc56,9015a7a..d26b33d
992 --- a/file
993 +++ b/file
994 @@@ -1,1 -1,1 +1,1 @@@
995 - foo
996 -index
997 ++working
998 EOF
f2f3fc95 999 git stash list --format=%gd -p --cc >actual &&
c7848150 1000 diff_cmp expect actual
288c67ca
JK
1001'
1002
9d4e28ea
JK
1003test_expect_success 'stash is not confused by partial renames' '
1004 mv file renamed &&
1005 git add renamed &&
1006 git stash &&
1007 git stash apply &&
1008 test_path_is_file renamed &&
1009 test_path_is_missing file
1010'
1011
f5727e26
TG
1012test_expect_success 'push -m shows right message' '
1013 >foo &&
1014 git add foo &&
1015 git stash push -m "test message" &&
cbc75a12 1016 echo "stash@{0}: On main: test message" >expect &&
f5727e26
TG
1017 git stash list -1 >actual &&
1018 test_cmp expect actual
1019'
1020
5675473f
PH
1021test_expect_success 'push -m also works without space' '
1022 >foo &&
1023 git add foo &&
1024 git stash push -m"unspaced test message" &&
cbc75a12 1025 echo "stash@{0}: On main: unspaced test message" >expect &&
5675473f
PH
1026 git stash list -1 >actual &&
1027 test_cmp expect actual
1028'
1029
1030test_expect_success 'store -m foo shows right message' '
1031 git stash clear &&
1032 git reset --hard &&
1033 echo quux >bazzy &&
1034 git add bazzy &&
1035 STASH_ID=$(git stash create) &&
1036 git stash store -m "store m" $STASH_ID &&
1037 echo "stash@{0}: store m" >expect &&
1038 git stash list -1 >actual &&
1039 test_cmp expect actual
1040'
1041
1042test_expect_success 'store -mfoo shows right message' '
1043 git stash clear &&
1044 git reset --hard &&
1045 echo quux >bazzy &&
1046 git add bazzy &&
1047 STASH_ID=$(git stash create) &&
1048 git stash store -m"store mfoo" $STASH_ID &&
1049 echo "stash@{0}: store mfoo" >expect &&
1050 git stash list -1 >actual &&
1051 test_cmp expect actual
1052'
1053
1054test_expect_success 'store --message=foo shows right message' '
1055 git stash clear &&
1056 git reset --hard &&
1057 echo quux >bazzy &&
1058 git add bazzy &&
1059 STASH_ID=$(git stash create) &&
1060 git stash store --message="store message=foo" $STASH_ID &&
1061 echo "stash@{0}: store message=foo" >expect &&
1062 git stash list -1 >actual &&
1063 test_cmp expect actual
1064'
1065
1066test_expect_success 'store --message foo shows right message' '
1067 git stash clear &&
1068 git reset --hard &&
1069 echo quux >bazzy &&
1070 git add bazzy &&
1071 STASH_ID=$(git stash create) &&
1072 git stash store --message "store message foo" $STASH_ID &&
1073 echo "stash@{0}: store message foo" >expect &&
1074 git stash list -1 >actual &&
1075 test_cmp expect actual
1076'
1077
1078test_expect_success 'push -mfoo uses right message' '
1079 >foo &&
1080 git add foo &&
1081 git stash push -m"test mfoo" &&
cbc75a12 1082 echo "stash@{0}: On main: test mfoo" >expect &&
5675473f
PH
1083 git stash list -1 >actual &&
1084 test_cmp expect actual
1085'
1086
1087test_expect_success 'push --message foo is synonym for -mfoo' '
1088 >foo &&
1089 git add foo &&
1090 git stash push --message "test message foo" &&
cbc75a12 1091 echo "stash@{0}: On main: test message foo" >expect &&
5675473f
PH
1092 git stash list -1 >actual &&
1093 test_cmp expect actual
1094'
1095
1096test_expect_success 'push --message=foo is synonym for -mfoo' '
1097 >foo &&
1098 git add foo &&
1099 git stash push --message="test message=foo" &&
cbc75a12 1100 echo "stash@{0}: On main: test message=foo" >expect &&
5675473f
PH
1101 git stash list -1 >actual &&
1102 test_cmp expect actual
1103'
1104
1105test_expect_success 'push -m shows right message' '
1106 >foo &&
1107 git add foo &&
1108 git stash push -m "test m foo" &&
cbc75a12 1109 echo "stash@{0}: On main: test m foo" >expect &&
5675473f
PH
1110 git stash list -1 >actual &&
1111 test_cmp expect actual
1112'
1113
6f5ccd4d
TG
1114test_expect_success 'create stores correct message' '
1115 >foo &&
1116 git add foo &&
1117 STASH_ID=$(git stash create "create test message") &&
cbc75a12 1118 echo "On main: create test message" >expect &&
6f5ccd4d
TG
1119 git show --pretty=%s -s ${STASH_ID} >actual &&
1120 test_cmp expect actual
1121'
1122
ceaf037f
GC
1123test_expect_success 'create when branch name has /' '
1124 test_when_finished "git checkout main" &&
1125 git checkout -b some/topic &&
1126 >foo &&
1127 git add foo &&
1128 STASH_ID=$(git stash create "create test message") &&
1129 echo "On some/topic: create test message" >expect &&
1130 git show --pretty=%s -s ${STASH_ID} >actual &&
1131 test_cmp expect actual
1132'
1133
6f5ccd4d
TG
1134test_expect_success 'create with multiple arguments for the message' '
1135 >foo &&
1136 git add foo &&
1137 STASH_ID=$(git stash create test untracked) &&
cbc75a12 1138 echo "On main: test untracked" >expect &&
6f5ccd4d
TG
1139 git show --pretty=%s -s ${STASH_ID} >actual &&
1140 test_cmp expect actual
1141'
1142
4e9bf3dd 1143test_expect_success 'create in a detached state' '
cbc75a12 1144 test_when_finished "git checkout main" &&
4e9bf3dd
JT
1145 git checkout HEAD~1 &&
1146 >foo &&
1147 git add foo &&
1148 STASH_ID=$(git stash create) &&
1149 HEAD_ID=$(git rev-parse --short HEAD) &&
1150 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1151 git show --pretty=%s -s ${STASH_ID} >actual &&
1152 test_cmp expect actual
1153'
1154
df6bba09
TG
1155test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1156 >foo &&
1157 >bar &&
1158 git add foo bar &&
1159 git stash push -- foo &&
1160 test_path_is_file bar &&
1161 test_path_is_missing foo &&
1162 git stash pop &&
1163 test_path_is_file foo &&
1164 test_path_is_file bar
1165'
1166
22fc703e
PS
1167test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1168 mkdir sub &&
1169 >foo &&
1170 >bar &&
1171 git add foo bar &&
1172 (
1173 cd sub &&
1174 git stash push -- ../foo
1175 ) &&
1176 test_path_is_file bar &&
1177 test_path_is_missing foo &&
1178 git stash pop &&
1179 test_path_is_file foo &&
1180 test_path_is_file bar
1181'
1182
df6bba09
TG
1183test_expect_success 'stash with multiple pathspec arguments' '
1184 >foo &&
1185 >bar &&
1186 >extra &&
1187 git add foo bar extra &&
1188 git stash push -- foo bar &&
1189 test_path_is_missing bar &&
1190 test_path_is_missing foo &&
1191 test_path_is_file extra &&
1192 git stash pop &&
1193 test_path_is_file foo &&
1194 test_path_is_file bar &&
1195 test_path_is_file extra
1196'
1197
1198test_expect_success 'stash with file including $IFS character' '
1199 >"foo bar" &&
1200 >foo &&
1201 >bar &&
1202 git add foo* &&
1203 git stash push -- "foo b*" &&
1204 test_path_is_missing "foo bar" &&
1205 test_path_is_file foo &&
1206 test_path_is_file bar &&
1207 git stash pop &&
1208 test_path_is_file "foo bar" &&
1209 test_path_is_file foo &&
1210 test_path_is_file bar
1211'
1212
1213test_expect_success 'stash with pathspec matching multiple paths' '
1214 echo original >file &&
1215 echo original >other-file &&
1216 git commit -m "two" file other-file &&
1217 echo modified >file &&
1218 echo modified >other-file &&
1219 git stash push -- "*file" &&
1220 echo original >expect &&
1221 test_cmp expect file &&
1222 test_cmp expect other-file &&
1223 git stash pop &&
1224 echo modified >expect &&
1225 test_cmp expect file &&
1226 test_cmp expect other-file
1227'
1228
1229test_expect_success 'stash push -p with pathspec shows no changes only once' '
1230 >foo &&
1231 git add foo &&
1232 git commit -m "tmp" &&
1233 git stash push -p foo >actual &&
1234 echo "No local changes to save" >expect &&
1235 git reset --hard HEAD~ &&
1108cea7 1236 test_cmp expect actual
df6bba09
TG
1237'
1238
ab8ad46e 1239test_expect_success 'push <pathspec>: show no changes when there are none' '
df6bba09
TG
1240 >foo &&
1241 git add foo &&
1242 git commit -m "tmp" &&
1243 git stash push foo >actual &&
1244 echo "No local changes to save" >expect &&
1245 git reset --hard HEAD~ &&
1108cea7 1246 test_cmp expect actual
df6bba09
TG
1247'
1248
ab8ad46e 1249test_expect_success 'push: <pathspec> not in the repository errors out' '
df6bba09
TG
1250 >untracked &&
1251 test_must_fail git stash push untracked &&
1252 test_path_is_file untracked
1253'
1254
1ac528c0
PSU
1255test_expect_success 'push: -q is quiet with changes' '
1256 >foo &&
1257 git add foo &&
1258 git stash push -q >output 2>&1 &&
1259 test_must_be_empty output
1260'
1261
1262test_expect_success 'push: -q is quiet with no changes' '
1263 git stash push -q >output 2>&1 &&
1264 test_must_be_empty output
1265'
1266
1267test_expect_success 'push: -q is quiet even if there is no initial commit' '
1268 git init foo_dir &&
1269 test_when_finished rm -rf foo_dir &&
1270 (
1271 cd foo_dir &&
1272 >bar &&
1273 test_must_fail git stash push -q >output 2>&1 &&
1274 test_must_be_empty output
1275 )
1276'
1277
df6bba09
TG
1278test_expect_success 'untracked files are left in place when -u is not given' '
1279 >file &&
1280 git add file &&
1281 >untracked &&
1282 git stash push file &&
1283 test_path_is_file untracked
1284'
1285
9e140909
TG
1286test_expect_success 'stash without verb with pathspec' '
1287 >"foo bar" &&
1288 >foo &&
1289 >bar &&
1290 git add foo* &&
1291 git stash -- "foo b*" &&
1292 test_path_is_missing "foo bar" &&
1293 test_path_is_file foo &&
1294 test_path_is_file bar &&
1295 git stash pop &&
1296 test_path_is_file "foo bar" &&
1297 test_path_is_file foo &&
1298 test_path_is_file bar
1299'
1300
e0e7f99e
TG
1301test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1302 git reset &&
1303 >foo &&
1304 >bar &&
1305 git add foo bar &&
1306 git commit -m "test" &&
1307 echo "foo" >foo &&
1308 echo "bar" >bar &&
1309 git stash -k -- foo &&
1310 test "",bar = $(cat foo),$(cat bar) &&
1311 git stash pop &&
1312 test foo,bar = $(cat foo),$(cat bar)
1313'
1314
bba067d2
TG
1315test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1316 git reset &&
1317 >subdir/untracked &&
1318 >subdir/tracked1 &&
1319 >subdir/tracked2 &&
1320 git add subdir/tracked* &&
1321 git stash -- subdir/ &&
1322 test_path_is_missing subdir/tracked1 &&
1323 test_path_is_missing subdir/tracked2 &&
1324 test_path_is_file subdir/untracked &&
1325 git stash pop &&
1326 test_path_is_file subdir/tracked1 &&
1327 test_path_is_file subdir/tracked2 &&
1328 test_path_is_file subdir/untracked
1329'
1330
1331test_expect_success 'stash -- <subdir> works with binary files' '
1332 git reset &&
1333 >subdir/untracked &&
1334 >subdir/tracked &&
1335 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1336 git add subdir/tracked* &&
1337 git stash -- subdir/ &&
1338 test_path_is_missing subdir/tracked &&
1339 test_path_is_missing subdir/tracked-binary &&
1340 test_path_is_file subdir/untracked &&
1341 git stash pop &&
1342 test_path_is_file subdir/tracked &&
1343 test_path_is_file subdir/tracked-binary &&
1344 test_path_is_file subdir/untracked
1345'
1346
0640897d
TG
1347test_expect_success 'stash with user.name and user.email set works' '
1348 test_config user.name "A U Thor" &&
1349 test_config user.email "a.u@thor" &&
1350 git stash
1351'
1352
3bc2111f
SD
1353test_expect_success 'stash works when user.name and user.email are not set' '
1354 git reset &&
1355 >1 &&
1356 git add 1 &&
1357 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1358 git stash &&
1359 git show -s --format="%an <%ae>" refs/stash >actual &&
1360 test_cmp expect actual &&
1361 >2 &&
1362 git add 2 &&
1363 test_config user.useconfigonly true &&
3bc2111f
SD
1364 (
1365 sane_unset GIT_AUTHOR_NAME &&
1366 sane_unset GIT_AUTHOR_EMAIL &&
1367 sane_unset GIT_COMMITTER_NAME &&
1368 sane_unset GIT_COMMITTER_EMAIL &&
1369 test_unconfig user.email &&
1370 test_unconfig user.name &&
1371 test_must_fail git commit -m "should fail" &&
1372 echo "git stash <git@stash>" >expect &&
1373 >2 &&
1374 git stash &&
1375 git show -s --format="%an <%ae>" refs/stash >actual &&
1376 test_cmp expect actual
1377 )
1378'
1379
b932f6a5
TG
1380test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1381 test_commit to-remove to-remove &&
1382 git rm to-remove &&
1383 git stash --keep-index &&
1384 test_path_is_missing to-remove
1385'
1386
34933d0e
TG
1387test_expect_success 'stash apply should succeed with unmodified file' '
1388 echo base >file &&
1389 git add file &&
1390 git commit -m base &&
1391
1392 # now stash a modification
1393 echo modified >file &&
1394 git stash &&
1395
1396 # make the file stat dirty
1397 cp file other &&
1398 mv other file &&
1399
1400 git stash apply
1401'
1402
4a58c3d7
JS
1403test_expect_success 'stash handles skip-worktree entries nicely' '
1404 test_commit A &&
1405 echo changed >A.t &&
1406 git add A.t &&
1407 git update-index --skip-worktree A.t &&
1408 rm A.t &&
1409 git stash &&
1410
1411 git rev-parse --verify refs/stash:A.t
1412'
1413
d42bab44
NS
1414
1415BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
1416
1417test_expect_success 'stash with core.fsyncmethod=batch' "
1418 test_create_unique_files 2 4 files_base_dir &&
1419 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
1420
1421 # The files were untracked, so use the third parent,
1422 # which contains the untracked files
1423 git ls-tree -r stash^3 -- ./files_base_dir/ |
1424 test_parse_ls_tree_oids >stashed_files_oids &&
1425
1426 # We created 2 dirs with 4 files each (8 files total) above
1427 test_line_count = 8 stashed_files_oids &&
1428 git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
1429 test_cmp stashed_files_oids stashed_files_actual
1430"
1431
1432
3d40e372 1433test_expect_success 'git stash succeeds despite directory/file change' '
4dbf7f30
EN
1434 test_create_repo directory_file_switch_v1 &&
1435 (
1436 cd directory_file_switch_v1 &&
1437 test_commit init &&
1438
1439 test_write_lines this file has some words >filler &&
1440 git add filler &&
1441 git commit -m filler &&
1442
1443 git rm filler &&
1444 mkdir filler &&
1445 echo contents >filler/file &&
1446 git stash push
1447 )
1448'
1449
bee8691f 1450test_expect_success 'git stash can pop file -> directory saved changes' '
4dbf7f30
EN
1451 test_create_repo directory_file_switch_v2 &&
1452 (
1453 cd directory_file_switch_v2 &&
1454 test_commit init &&
1455
1456 test_write_lines this file has some words >filler &&
1457 git add filler &&
1458 git commit -m filler &&
1459
1460 git rm filler &&
1461 mkdir filler &&
1462 echo contents >filler/file &&
1463 cp filler/file expect &&
1464 git stash push --include-untracked &&
1465 git stash apply --index &&
1466 test_cmp expect filler/file
1467 )
1468'
1469
bee8691f 1470test_expect_success 'git stash can pop directory -> file saved changes' '
4dbf7f30
EN
1471 test_create_repo directory_file_switch_v3 &&
1472 (
1473 cd directory_file_switch_v3 &&
1474 test_commit init &&
1475
1476 mkdir filler &&
1477 test_write_lines some words >filler/file1 &&
1478 test_write_lines and stuff >filler/file2 &&
1479 git add filler &&
1480 git commit -m filler &&
1481
1482 git rm -rf filler &&
1483 echo contents >filler &&
1484 cp filler expect &&
1485 git stash push --include-untracked &&
1486 git stash apply --index &&
1487 test_cmp expect filler
1488 )
1489'
1490
71cade5a
EN
1491test_expect_success 'restore untracked files even when we hit conflicts' '
1492 git init restore_untracked_after_conflict &&
1493 (
1494 cd restore_untracked_after_conflict &&
1495
1496 echo hi >a &&
1497 echo there >b &&
1498 git add . &&
1499 git commit -m first &&
1500 echo hello >a &&
1501 echo something >c &&
1502
1503 git stash push --include-untracked &&
1504
1505 echo conflict >a &&
1506 git add a &&
1507 git commit -m second &&
1508
1509 test_must_fail git stash pop &&
1510
1511 test_path_is_file c
1512 )
1513'
1514
150937c4 1515test_done