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