]>
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 | 11 | . ./test-lib.sh |
386e7a9d | 12 | . "$TEST_DIRECTORY"/lib-unique-files.sh |
150937c4 | 13 | |
ca7990ce ÆAB |
14 | test_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 | ||
22 | test_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 | 28 | test_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 | 33 | diff_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 | 45 | setup_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 | ||
61 | test_expect_success 'stash some dirty working directory' ' | |
62 | setup_stash | |
150937c4 JS |
63 | ' |
64 | ||
6a0b88b2 | 65 | cat >expect <<EOF |
150937c4 JS |
66 | diff --git a/file b/file |
67 | index 0cfbf08..00750ed 100644 | |
68 | --- a/file | |
69 | +++ b/file | |
70 | @@ -1 +1 @@ | |
71 | -2 | |
72 | +3 | |
73 | EOF | |
74 | ||
75 | test_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 |
81 | test_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 |
87 | test_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 | ||
94 | test_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 | ||
102 | test_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 | ||
114 | test_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 |
126 | test_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 |
135 | test_expect_success 'stash drop complains of extra options' ' |
136 | test_must_fail git stash drop --foo | |
137 | ' | |
138 | ||
b683c080 BC |
139 | test_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 | ||
153 | test_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 |
173 | test_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 |
193 | test_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 | ||
99a294bc | 203 | test_expect_success 'drop stash reflog updates refs/stash with rewrite' ' |
76bccbcf JC |
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 && | |
99a294bc JC |
216 | $new_oid |
217 | $old_oid | |
76bccbcf | 218 | EOF |
99a294bc | 219 | git -C repo reflog show refs/stash --format=%H >actual && |
76bccbcf JC |
220 | test_cmp expect actual && |
221 | ||
222 | git -C repo stash drop stash@{1} && | |
99a294bc | 223 | git -C repo reflog show refs/stash --format=%H >actual && |
76bccbcf | 224 | cat >expect <<-EOF && |
99a294bc | 225 | $new_oid |
76bccbcf JC |
226 | EOF |
227 | test_cmp expect actual | |
228 | ' | |
229 | ||
b683c080 BC |
230 | test_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 | 239 | cat >expect <<EOF |
4a588075 AMS |
240 | diff --git a/file2 b/file2 |
241 | new file mode 100644 | |
242 | index 0000000..1fe912c | |
243 | --- /dev/null | |
244 | +++ b/file2 | |
245 | @@ -0,0 +1 @@ | |
246 | +bar2 | |
247 | EOF | |
248 | ||
6a0b88b2 | 249 | cat >expect1 <<EOF |
4a588075 AMS |
250 | diff --git a/file b/file |
251 | index 257cc56..5716ca5 100644 | |
252 | --- a/file | |
253 | +++ b/file | |
254 | @@ -1 +1 @@ | |
255 | -foo | |
256 | +bar | |
257 | EOF | |
258 | ||
6a0b88b2 | 259 | cat >expect2 <<EOF |
4a588075 AMS |
260 | diff --git a/file b/file |
261 | index 7601807..5716ca5 100644 | |
262 | --- a/file | |
263 | +++ b/file | |
264 | @@ -1 +1 @@ | |
265 | -baz | |
266 | +bar | |
267 | diff --git a/file2 b/file2 | |
268 | new file mode 100644 | |
269 | index 0000000..1fe912c | |
270 | --- /dev/null | |
271 | +++ b/file2 | |
272 | @@ -0,0 +1 @@ | |
273 | +bar2 | |
274 | EOF | |
275 | ||
276 | test_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 | 299 | test_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 |
306 | test_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 | 318 | test_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 | 323 | test_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 | 331 | test_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 |
342 | test_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 |
348 | test_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 | ||
357 | test_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 | 369 | test_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 | 377 | test_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 |
385 | test_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 | ||
5fb76864 AJ |
396 | test_expect_success 'stash --staged with binary file' ' |
397 | printf "\0" >file && | |
398 | git add file && | |
399 | git stash --staged && | |
400 | git stash pop && | |
401 | printf "\0" >expect && | |
402 | test_cmp expect file | |
403 | ' | |
404 | ||
8c3713ce AM |
405 | test_expect_success 'dont assume push with non-option args' ' |
406 | test_must_fail git stash -q drop 2>err && | |
6789275d | 407 | test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err |
8c3713ce AM |
408 | ' |
409 | ||
3c2eb80f | 410 | test_expect_success 'stash --invalid-option' ' |
6a0b88b2 PSU |
411 | echo bar5 >file && |
412 | echo bar6 >file2 && | |
3c2eb80f MM |
413 | git add file2 && |
414 | test_must_fail git stash --invalid-option && | |
415 | test_must_fail git stash save --invalid-option && | |
1ada5020 | 416 | test bar5,bar6 = $(cat file),$(cat file2) |
3c2eb80f MM |
417 | ' |
418 | ||
2ba2fe29 CB |
419 | test_expect_success 'stash an added file' ' |
420 | git reset --hard && | |
421 | echo new >file3 && | |
422 | git add file3 && | |
423 | git stash save "added file" && | |
424 | ! test -r file3 && | |
425 | git stash apply && | |
426 | test new = "$(cat file3)" | |
427 | ' | |
428 | ||
b6e531e6 MK |
429 | test_expect_success 'stash --intent-to-add file' ' |
430 | git reset --hard && | |
431 | echo new >file4 && | |
432 | git add --intent-to-add file4 && | |
433 | test_when_finished "git rm -f file4" && | |
434 | test_must_fail git stash | |
435 | ' | |
436 | ||
2ba2fe29 CB |
437 | test_expect_success 'stash rm then recreate' ' |
438 | git reset --hard && | |
439 | git rm file && | |
440 | echo bar7 >file && | |
441 | git stash save "rm then recreate" && | |
442 | test bar = "$(cat file)" && | |
443 | git stash apply && | |
444 | test bar7 = "$(cat file)" | |
445 | ' | |
446 | ||
447 | test_expect_success 'stash rm and ignore' ' | |
448 | git reset --hard && | |
449 | git rm file && | |
450 | echo file >.gitignore && | |
451 | git stash save "rm and ignore" && | |
452 | test bar = "$(cat file)" && | |
a48fcd83 | 453 | test file = "$(cat .gitignore)" && |
2ba2fe29 CB |
454 | git stash apply && |
455 | ! test -r file && | |
456 | test file = "$(cat .gitignore)" | |
457 | ' | |
458 | ||
459 | test_expect_success 'stash rm and ignore (stage .gitignore)' ' | |
460 | git reset --hard && | |
461 | git rm file && | |
462 | echo file >.gitignore && | |
463 | git add .gitignore && | |
464 | git stash save "rm and ignore (stage .gitignore)" && | |
465 | test bar = "$(cat file)" && | |
a48fcd83 | 466 | ! test -r .gitignore && |
2ba2fe29 CB |
467 | git stash apply && |
468 | ! test -r file && | |
469 | test file = "$(cat .gitignore)" | |
470 | ' | |
471 | ||
472 | test_expect_success SYMLINKS 'stash file to symlink' ' | |
473 | git reset --hard && | |
474 | rm file && | |
475 | ln -s file2 file && | |
476 | git stash save "file to symlink" && | |
456296b5 | 477 | test_path_is_file_not_symlink file && |
2ba2fe29 CB |
478 | test bar = "$(cat file)" && |
479 | git stash apply && | |
cc143f12 CG |
480 | test_path_is_symlink file && |
481 | test "$(test_readlink file)" = file2 | |
2ba2fe29 CB |
482 | ' |
483 | ||
484 | test_expect_success SYMLINKS 'stash file to symlink (stage rm)' ' | |
485 | git reset --hard && | |
486 | git rm file && | |
487 | ln -s file2 file && | |
488 | git stash save "file to symlink (stage rm)" && | |
456296b5 | 489 | test_path_is_file_not_symlink file && |
2ba2fe29 CB |
490 | test bar = "$(cat file)" && |
491 | git stash apply && | |
cc143f12 CG |
492 | test_path_is_symlink file && |
493 | test "$(test_readlink file)" = file2 | |
2ba2fe29 CB |
494 | ' |
495 | ||
496 | test_expect_success SYMLINKS 'stash file to symlink (full stage)' ' | |
497 | git reset --hard && | |
498 | rm file && | |
499 | ln -s file2 file && | |
500 | git add file && | |
501 | git stash save "file to symlink (full stage)" && | |
456296b5 | 502 | test_path_is_file_not_symlink file && |
2ba2fe29 CB |
503 | test bar = "$(cat file)" && |
504 | git stash apply && | |
cc143f12 CG |
505 | test_path_is_symlink file && |
506 | test "$(test_readlink file)" = file2 | |
2ba2fe29 CB |
507 | ' |
508 | ||
509 | # This test creates a commit with a symlink used for the following tests | |
510 | ||
889c6f0e | 511 | test_expect_success 'stash symlink to file' ' |
2ba2fe29 | 512 | git reset --hard && |
889c6f0e | 513 | test_ln_s_add file filelink && |
2ba2fe29 CB |
514 | git commit -m "Add symlink" && |
515 | rm filelink && | |
516 | cp file filelink && | |
889c6f0e JS |
517 | git stash save "symlink to file" |
518 | ' | |
519 | ||
520 | test_expect_success SYMLINKS 'this must have re-created the symlink' ' | |
2ba2fe29 | 521 | test -h filelink && |
889c6f0e JS |
522 | case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac |
523 | ' | |
524 | ||
525 | test_expect_success 'unstash must re-create the file' ' | |
2ba2fe29 CB |
526 | git stash apply && |
527 | ! test -h filelink && | |
528 | test bar = "$(cat file)" | |
529 | ' | |
530 | ||
889c6f0e | 531 | test_expect_success 'stash symlink to file (stage rm)' ' |
2ba2fe29 CB |
532 | git reset --hard && |
533 | git rm filelink && | |
534 | cp file filelink && | |
889c6f0e JS |
535 | git stash save "symlink to file (stage rm)" |
536 | ' | |
537 | ||
538 | test_expect_success SYMLINKS 'this must have re-created the symlink' ' | |
2ba2fe29 | 539 | test -h filelink && |
889c6f0e JS |
540 | case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac |
541 | ' | |
542 | ||
543 | test_expect_success 'unstash must re-create the file' ' | |
2ba2fe29 CB |
544 | git stash apply && |
545 | ! test -h filelink && | |
546 | test bar = "$(cat file)" | |
547 | ' | |
548 | ||
889c6f0e | 549 | test_expect_success 'stash symlink to file (full stage)' ' |
2ba2fe29 CB |
550 | git reset --hard && |
551 | rm filelink && | |
552 | cp file filelink && | |
553 | git add filelink && | |
889c6f0e JS |
554 | git stash save "symlink to file (full stage)" |
555 | ' | |
556 | ||
557 | test_expect_success SYMLINKS 'this must have re-created the symlink' ' | |
2ba2fe29 | 558 | test -h filelink && |
889c6f0e JS |
559 | case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac |
560 | ' | |
561 | ||
562 | test_expect_success 'unstash must re-create the file' ' | |
2ba2fe29 CB |
563 | git stash apply && |
564 | ! test -h filelink && | |
565 | test bar = "$(cat file)" | |
566 | ' | |
567 | ||
568 | test_expect_failure 'stash directory to file' ' | |
569 | git reset --hard && | |
570 | mkdir dir && | |
571 | echo foo >dir/file && | |
572 | git add dir/file && | |
573 | git commit -m "Add file in dir" && | |
574 | rm -fr dir && | |
575 | echo bar >dir && | |
576 | git stash save "directory to file" && | |
f01f9482 | 577 | test_path_is_dir dir && |
2ba2fe29 CB |
578 | test foo = "$(cat dir/file)" && |
579 | test_must_fail git stash apply && | |
580 | test bar = "$(cat dir)" && | |
581 | git reset --soft HEAD^ | |
582 | ' | |
583 | ||
584 | test_expect_failure 'stash file to directory' ' | |
585 | git reset --hard && | |
586 | rm file && | |
587 | mkdir file && | |
588 | echo foo >file/file && | |
589 | git stash save "file to directory" && | |
f01f9482 | 590 | test_path_is_file file && |
2ba2fe29 CB |
591 | test bar = "$(cat file)" && |
592 | git stash apply && | |
f01f9482 | 593 | test_path_is_file file/file && |
2ba2fe29 CB |
594 | test foo = "$(cat file/file)" |
595 | ' | |
596 | ||
93415f58 JT |
597 | test_expect_success 'giving too many ref arguments does not modify files' ' |
598 | git stash clear && | |
599 | test_when_finished "git reset --hard HEAD" && | |
600 | echo foo >file2 && | |
601 | git stash && | |
602 | echo bar >file2 && | |
603 | git stash && | |
604 | test-tool chmtime =123456789 file2 && | |
605 | for type in apply pop "branch stash-branch" | |
606 | do | |
607 | test_must_fail git stash $type stash@{0} stash@{1} 2>err && | |
6789275d | 608 | test_grep "Too many revisions" err && |
93415f58 JT |
609 | test 123456789 = $(test-tool chmtime -g file2) || return 1 |
610 | done | |
611 | ' | |
612 | ||
613 | test_expect_success 'drop: too many arguments errors out (does nothing)' ' | |
614 | git stash list >expect && | |
615 | test_must_fail git stash drop stash@{0} stash@{1} 2>err && | |
6789275d | 616 | test_grep "Too many revisions" err && |
93415f58 JT |
617 | git stash list >actual && |
618 | test_cmp expect actual | |
619 | ' | |
620 | ||
621 | test_expect_success 'show: too many arguments errors out (does nothing)' ' | |
622 | test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out && | |
6789275d | 623 | test_grep "Too many revisions" err && |
93415f58 JT |
624 | test_must_be_empty out |
625 | ' | |
626 | ||
c95bc226 JT |
627 | test_expect_success 'stash create - no changes' ' |
628 | git stash clear && | |
629 | test_when_finished "git reset --hard HEAD" && | |
630 | git reset --hard && | |
631 | git stash create >actual && | |
632 | test_must_be_empty actual | |
633 | ' | |
634 | ||
daf7a0c0 JS |
635 | test_expect_success 'stash branch - no stashes on stack, stash-like argument' ' |
636 | git stash clear && | |
637 | test_when_finished "git reset --hard HEAD" && | |
638 | git reset --hard && | |
6a0b88b2 | 639 | echo foo >>file && |
daf7a0c0 JS |
640 | STASH_ID=$(git stash create) && |
641 | git reset --hard && | |
642 | git stash branch stash-branch ${STASH_ID} && | |
cbc75a12 | 643 | test_when_finished "git reset --hard HEAD && git checkout main && |
6a0b88b2 | 644 | git branch -D stash-branch" && |
daf7a0c0 JS |
645 | test $(git ls-files --modified | wc -l) -eq 1 |
646 | ' | |
647 | ||
648 | test_expect_success 'stash branch - stashes on stack, stash-like argument' ' | |
649 | git stash clear && | |
650 | test_when_finished "git reset --hard HEAD" && | |
651 | git reset --hard && | |
6a0b88b2 | 652 | echo foo >>file && |
daf7a0c0 JS |
653 | git stash && |
654 | test_when_finished "git stash drop" && | |
6a0b88b2 | 655 | echo bar >>file && |
daf7a0c0 JS |
656 | STASH_ID=$(git stash create) && |
657 | git reset --hard && | |
658 | git stash branch stash-branch ${STASH_ID} && | |
cbc75a12 | 659 | test_when_finished "git reset --hard HEAD && git checkout main && |
6a0b88b2 | 660 | git branch -D stash-branch" && |
daf7a0c0 JS |
661 | test $(git ls-files --modified | wc -l) -eq 1 |
662 | ' | |
663 | ||
93415f58 JT |
664 | test_expect_success 'stash branch complains with no arguments' ' |
665 | test_must_fail git stash branch 2>err && | |
6789275d | 666 | test_grep "No branch name specified" err |
93415f58 JT |
667 | ' |
668 | ||
11452114 | 669 | test_expect_success 'stash show format defaults to --stat' ' |
daf7a0c0 JS |
670 | git stash clear && |
671 | test_when_finished "git reset --hard HEAD" && | |
672 | git reset --hard && | |
6a0b88b2 | 673 | echo foo >>file && |
daf7a0c0 JS |
674 | git stash && |
675 | test_when_finished "git stash drop" && | |
6a0b88b2 | 676 | echo bar >>file && |
daf7a0c0 JS |
677 | STASH_ID=$(git stash create) && |
678 | git reset --hard && | |
3fcb8878 | 679 | cat >expected <<-EOF && |
dc801e71 | 680 | file | 1 + |
7f814632 | 681 | 1 file changed, 1 insertion(+) |
3fcb8878 BC |
682 | EOF |
683 | git stash show ${STASH_ID} >actual && | |
1108cea7 | 684 | test_cmp expected actual |
daf7a0c0 | 685 | ' |
3fcb8878 | 686 | |
11452114 JN |
687 | test_expect_success 'stash show - stashes on stack, stash-like argument' ' |
688 | git stash clear && | |
689 | test_when_finished "git reset --hard HEAD" && | |
690 | git reset --hard && | |
6a0b88b2 | 691 | echo foo >>file && |
11452114 JN |
692 | git stash && |
693 | test_when_finished "git stash drop" && | |
6a0b88b2 | 694 | echo bar >>file && |
11452114 JN |
695 | STASH_ID=$(git stash create) && |
696 | git reset --hard && | |
697 | echo "1 0 file" >expected && | |
698 | git stash show --numstat ${STASH_ID} >actual && | |
699 | test_cmp expected actual | |
700 | ' | |
701 | ||
9027fa9e | 702 | test_expect_success 'stash show -p - stashes on stack, stash-like argument' ' |
3fcb8878 BC |
703 | git stash clear && |
704 | test_when_finished "git reset --hard HEAD" && | |
705 | git reset --hard && | |
6a0b88b2 | 706 | echo foo >>file && |
3fcb8878 BC |
707 | git stash && |
708 | test_when_finished "git stash drop" && | |
6a0b88b2 | 709 | echo bar >>file && |
3fcb8878 BC |
710 | STASH_ID=$(git stash create) && |
711 | git reset --hard && | |
712 | cat >expected <<-EOF && | |
713 | diff --git a/file b/file | |
714 | index 7601807..935fbd3 100644 | |
715 | --- a/file | |
716 | +++ b/file | |
717 | @@ -1 +1,2 @@ | |
718 | baz | |
719 | +bar | |
720 | EOF | |
721 | git stash show -p ${STASH_ID} >actual && | |
c7848150 | 722 | diff_cmp expected actual |
3fcb8878 BC |
723 | ' |
724 | ||
9027fa9e | 725 | test_expect_success 'stash show - no stashes on stack, stash-like argument' ' |
3fcb8878 BC |
726 | git stash clear && |
727 | test_when_finished "git reset --hard HEAD" && | |
728 | git reset --hard && | |
6a0b88b2 | 729 | echo foo >>file && |
3fcb8878 BC |
730 | STASH_ID=$(git stash create) && |
731 | git reset --hard && | |
11452114 JN |
732 | echo "1 0 file" >expected && |
733 | git stash show --numstat ${STASH_ID} >actual && | |
734 | test_cmp expected actual | |
3fcb8878 BC |
735 | ' |
736 | ||
9027fa9e | 737 | test_expect_success 'stash show -p - no stashes on stack, stash-like argument' ' |
daf7a0c0 JS |
738 | git stash clear && |
739 | test_when_finished "git reset --hard HEAD" && | |
740 | git reset --hard && | |
6a0b88b2 | 741 | echo foo >>file && |
daf7a0c0 JS |
742 | STASH_ID=$(git stash create) && |
743 | git reset --hard && | |
3fcb8878 BC |
744 | cat >expected <<-EOF && |
745 | diff --git a/file b/file | |
746 | index 7601807..71b52c4 100644 | |
747 | --- a/file | |
748 | +++ b/file | |
749 | @@ -1 +1,2 @@ | |
750 | baz | |
751 | +foo | |
752 | EOF | |
753 | git stash show -p ${STASH_ID} >actual && | |
c7848150 | 754 | diff_cmp expected actual |
daf7a0c0 JS |
755 | ' |
756 | ||
8e407bc8 TG |
757 | test_expect_success 'stash show --patience shows diff' ' |
758 | git reset --hard && | |
759 | echo foo >>file && | |
760 | STASH_ID=$(git stash create) && | |
761 | git reset --hard && | |
762 | cat >expected <<-EOF && | |
763 | diff --git a/file b/file | |
764 | index 7601807..71b52c4 100644 | |
765 | --- a/file | |
766 | +++ b/file | |
767 | @@ -1 +1,2 @@ | |
768 | baz | |
769 | +foo | |
770 | EOF | |
771 | git stash show --patience ${STASH_ID} >actual && | |
c7848150 | 772 | diff_cmp expected actual |
8e407bc8 TG |
773 | ' |
774 | ||
ab8ad46e | 775 | test_expect_success 'drop: fail early if specified stash is not a stash ref' ' |
daf7a0c0 JS |
776 | git stash clear && |
777 | test_when_finished "git reset --hard HEAD && git stash clear" && | |
778 | git reset --hard && | |
6a0b88b2 | 779 | echo foo >file && |
daf7a0c0 | 780 | git stash && |
6a0b88b2 | 781 | echo bar >file && |
daf7a0c0 | 782 | git stash && |
8d66bb05 | 783 | test_must_fail git stash drop $(git rev-parse stash@{0}) && |
daf7a0c0 JS |
784 | git stash pop && |
785 | test bar = "$(cat file)" && | |
786 | git reset --hard HEAD | |
787 | ' | |
788 | ||
ab8ad46e | 789 | test_expect_success 'pop: fail early if specified stash is not a stash ref' ' |
daf7a0c0 JS |
790 | git stash clear && |
791 | test_when_finished "git reset --hard HEAD && git stash clear" && | |
792 | git reset --hard && | |
6a0b88b2 | 793 | echo foo >file && |
daf7a0c0 | 794 | git stash && |
6a0b88b2 | 795 | echo bar >file && |
daf7a0c0 | 796 | git stash && |
8d66bb05 | 797 | test_must_fail git stash pop $(git rev-parse stash@{0}) && |
daf7a0c0 JS |
798 | git stash pop && |
799 | test bar = "$(cat file)" && | |
800 | git reset --hard HEAD | |
801 | ' | |
802 | ||
7be8b3ba | 803 | test_expect_success 'ref with non-existent reflog' ' |
daf7a0c0 | 804 | git stash clear && |
6a0b88b2 PSU |
805 | echo bar5 >file && |
806 | echo bar6 >file2 && | |
daf7a0c0 JS |
807 | git add file2 && |
808 | git stash && | |
1ae96444 | 809 | test_must_fail git rev-parse --quiet --verify does-not-exist && |
8d66bb05 JS |
810 | test_must_fail git stash drop does-not-exist && |
811 | test_must_fail git stash drop does-not-exist@{0} && | |
812 | test_must_fail git stash pop does-not-exist && | |
813 | test_must_fail git stash pop does-not-exist@{0} && | |
814 | test_must_fail git stash apply does-not-exist && | |
815 | test_must_fail git stash apply does-not-exist@{0} && | |
816 | test_must_fail git stash show does-not-exist && | |
817 | test_must_fail git stash show does-not-exist@{0} && | |
818 | test_must_fail git stash branch tmp does-not-exist && | |
819 | test_must_fail git stash branch tmp does-not-exist@{0} && | |
daf7a0c0 JS |
820 | git stash drop |
821 | ' | |
822 | ||
823 | test_expect_success 'invalid ref of the form stash@{n}, n >= N' ' | |
824 | git stash clear && | |
8d66bb05 | 825 | test_must_fail git stash drop stash@{0} && |
6a0b88b2 PSU |
826 | echo bar5 >file && |
827 | echo bar6 >file2 && | |
daf7a0c0 JS |
828 | git add file2 && |
829 | git stash && | |
9355fc9c JS |
830 | test_must_fail git stash drop stash@{1} && |
831 | test_must_fail git stash pop stash@{1} && | |
832 | test_must_fail git stash apply stash@{1} && | |
833 | test_must_fail git stash show stash@{1} && | |
834 | test_must_fail git stash branch tmp stash@{1} && | |
daf7a0c0 JS |
835 | git stash drop |
836 | ' | |
837 | ||
a56c8f5a AW |
838 | test_expect_success 'invalid ref of the form "n", n >= N' ' |
839 | git stash clear && | |
840 | test_must_fail git stash drop 0 && | |
841 | echo bar5 >file && | |
842 | echo bar6 >file2 && | |
843 | git add file2 && | |
844 | git stash && | |
845 | test_must_fail git stash drop 1 && | |
846 | test_must_fail git stash pop 1 && | |
847 | test_must_fail git stash apply 1 && | |
848 | test_must_fail git stash show 1 && | |
849 | test_must_fail git stash branch tmp 1 && | |
850 | git stash drop | |
851 | ' | |
852 | ||
63b50c8f TG |
853 | test_expect_success 'valid ref of the form "n", n < N' ' |
854 | git stash clear && | |
855 | echo bar5 >file && | |
856 | echo bar6 >file2 && | |
857 | git add file2 && | |
858 | git stash && | |
859 | git stash show 0 && | |
860 | git stash branch tmp 0 && | |
cbc75a12 | 861 | git checkout main && |
63b50c8f TG |
862 | git stash && |
863 | git stash apply 0 && | |
864 | git reset --hard && | |
865 | git stash pop 0 && | |
866 | git stash && | |
867 | git stash drop 0 && | |
868 | test_must_fail git stash drop | |
869 | ' | |
870 | ||
ab8ad46e | 871 | test_expect_success 'branch: do not drop the stash if the branch exists' ' |
835d6a1f TC |
872 | git stash clear && |
873 | echo foo >file && | |
874 | git add file && | |
875 | git commit -m initial && | |
876 | echo bar >file && | |
877 | git stash && | |
cbc75a12 | 878 | test_must_fail git stash branch main stash@{0} && |
835d6a1f TC |
879 | git rev-parse stash@{0} -- |
880 | ' | |
881 | ||
ab8ad46e | 882 | test_expect_success 'branch: should not drop the stash if the apply fails' ' |
b04e6915 JT |
883 | git stash clear && |
884 | git reset HEAD~1 --hard && | |
885 | echo foo >file && | |
886 | git add file && | |
887 | git commit -m initial && | |
888 | echo bar >file && | |
889 | git stash && | |
890 | echo baz >file && | |
cbc75a12 | 891 | test_when_finished "git checkout main" && |
b04e6915 JT |
892 | test_must_fail git stash branch new_branch stash@{0} && |
893 | git rev-parse stash@{0} -- | |
894 | ' | |
895 | ||
ab8ad46e | 896 | test_expect_success 'apply: show same status as git status (relative to ./)' ' |
6eaf92f3 PK |
897 | git stash clear && |
898 | echo 1 >subdir/subfile1 && | |
899 | echo 2 >subdir/subfile2 && | |
900 | git add subdir/subfile1 && | |
901 | git commit -m subdir && | |
902 | ( | |
903 | cd subdir && | |
904 | echo x >subfile1 && | |
905 | echo x >../file && | |
906 | git status >../expect && | |
907 | git stash && | |
908 | sane_unset GIT_MERGE_VERBOSITY && | |
909 | git stash apply | |
910 | ) | | |
1790f4fe | 911 | sed -e 1d >actual && # drop "Saved..." |
1108cea7 | 912 | test_cmp expect actual |
6eaf92f3 PK |
913 | ' |
914 | ||
6a0b88b2 | 915 | cat >expect <<EOF |
44df2e29 JM |
916 | diff --git a/HEAD b/HEAD |
917 | new file mode 100644 | |
918 | index 0000000..fe0cbee | |
919 | --- /dev/null | |
920 | +++ b/HEAD | |
921 | @@ -0,0 +1 @@ | |
922 | +file-not-a-ref | |
923 | EOF | |
924 | ||
925 | test_expect_success 'stash where working directory contains "HEAD" file' ' | |
926 | git stash clear && | |
927 | git reset --hard && | |
6a0b88b2 | 928 | echo file-not-a-ref >HEAD && |
44df2e29 JM |
929 | git add HEAD && |
930 | test_tick && | |
931 | git stash && | |
932 | git diff-files --quiet && | |
933 | git diff-index --cached --quiet HEAD && | |
934 | test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" && | |
6a0b88b2 | 935 | git diff stash^..stash >output && |
c7848150 | 936 | diff_cmp expect output |
44df2e29 JM |
937 | ' |
938 | ||
bd514cad RR |
939 | test_expect_success 'store called with invalid commit' ' |
940 | test_must_fail git stash store foo | |
941 | ' | |
942 | ||
d9b66345 JH |
943 | test_expect_success 'store called with non-stash commit' ' |
944 | test_must_fail git stash store HEAD | |
945 | ' | |
946 | ||
bd514cad RR |
947 | test_expect_success 'store updates stash ref and reflog' ' |
948 | git stash clear && | |
949 | git reset --hard && | |
950 | echo quux >bazzy && | |
951 | git add bazzy && | |
952 | STASH_ID=$(git stash create) && | |
953 | git reset --hard && | |
79b04f9b | 954 | test_path_is_missing bazzy && |
bd514cad | 955 | git stash store -m quuxery $STASH_ID && |
cbc5cf7c | 956 | test $(git rev-parse stash) = $STASH_ID && |
d0ab0584 | 957 | git reflog --format=%H stash| grep $STASH_ID && |
bd514cad RR |
958 | git stash pop && |
959 | grep quux bazzy | |
960 | ' | |
961 | ||
2a07e437 ØW |
962 | test_expect_success 'handle stash specification with spaces' ' |
963 | git stash clear && | |
964 | echo pig >file && | |
965 | git stash && | |
966 | stamp=$(git log -g --format="%cd" -1 refs/stash) && | |
967 | test_tick && | |
968 | echo cow >file && | |
969 | git stash && | |
970 | git stash apply "stash@{$stamp}" && | |
971 | grep pig file | |
972 | ' | |
973 | ||
288c67ca JK |
974 | test_expect_success 'setup stash with index and worktree changes' ' |
975 | git stash clear && | |
976 | git reset --hard && | |
977 | echo index >file && | |
978 | git add file && | |
979 | echo working >file && | |
980 | git stash | |
981 | ' | |
982 | ||
1e20a407 | 983 | test_expect_success 'stash list -p shows simple diff' ' |
f2f3fc95 JK |
984 | cat >expect <<-EOF && |
985 | stash@{0} | |
288c67ca JK |
986 | |
987 | diff --git a/file b/file | |
988 | index 257cc56..d26b33d 100644 | |
989 | --- a/file | |
990 | +++ b/file | |
991 | @@ -1 +1 @@ | |
992 | -foo | |
993 | +working | |
994 | EOF | |
f2f3fc95 | 995 | git stash list --format=%gd -p >actual && |
c7848150 | 996 | diff_cmp expect actual |
288c67ca JK |
997 | ' |
998 | ||
999 | test_expect_success 'stash list --cc shows combined diff' ' | |
1000 | cat >expect <<-\EOF && | |
f2f3fc95 | 1001 | stash@{0} |
288c67ca JK |
1002 | |
1003 | diff --cc file | |
1004 | index 257cc56,9015a7a..d26b33d | |
1005 | --- a/file | |
1006 | +++ b/file | |
1007 | @@@ -1,1 -1,1 +1,1 @@@ | |
1008 | - foo | |
1009 | -index | |
1010 | ++working | |
1011 | EOF | |
f2f3fc95 | 1012 | git stash list --format=%gd -p --cc >actual && |
c7848150 | 1013 | diff_cmp expect actual |
288c67ca JK |
1014 | ' |
1015 | ||
9d4e28ea JK |
1016 | test_expect_success 'stash is not confused by partial renames' ' |
1017 | mv file renamed && | |
1018 | git add renamed && | |
1019 | git stash && | |
1020 | git stash apply && | |
1021 | test_path_is_file renamed && | |
1022 | test_path_is_missing file | |
1023 | ' | |
1024 | ||
f5727e26 TG |
1025 | test_expect_success 'push -m shows right message' ' |
1026 | >foo && | |
1027 | git add foo && | |
1028 | git stash push -m "test message" && | |
cbc75a12 | 1029 | echo "stash@{0}: On main: test message" >expect && |
f5727e26 TG |
1030 | git stash list -1 >actual && |
1031 | test_cmp expect actual | |
1032 | ' | |
1033 | ||
5675473f PH |
1034 | test_expect_success 'push -m also works without space' ' |
1035 | >foo && | |
1036 | git add foo && | |
1037 | git stash push -m"unspaced test message" && | |
cbc75a12 | 1038 | echo "stash@{0}: On main: unspaced test message" >expect && |
5675473f PH |
1039 | git stash list -1 >actual && |
1040 | test_cmp expect actual | |
1041 | ' | |
1042 | ||
1043 | test_expect_success 'store -m foo shows right message' ' | |
1044 | git stash clear && | |
1045 | git reset --hard && | |
1046 | echo quux >bazzy && | |
1047 | git add bazzy && | |
1048 | STASH_ID=$(git stash create) && | |
1049 | git stash store -m "store m" $STASH_ID && | |
1050 | echo "stash@{0}: store m" >expect && | |
1051 | git stash list -1 >actual && | |
1052 | test_cmp expect actual | |
1053 | ' | |
1054 | ||
1055 | test_expect_success 'store -mfoo shows right message' ' | |
1056 | git stash clear && | |
1057 | git reset --hard && | |
1058 | echo quux >bazzy && | |
1059 | git add bazzy && | |
1060 | STASH_ID=$(git stash create) && | |
1061 | git stash store -m"store mfoo" $STASH_ID && | |
1062 | echo "stash@{0}: store mfoo" >expect && | |
1063 | git stash list -1 >actual && | |
1064 | test_cmp expect actual | |
1065 | ' | |
1066 | ||
1067 | test_expect_success 'store --message=foo shows right message' ' | |
1068 | git stash clear && | |
1069 | git reset --hard && | |
1070 | echo quux >bazzy && | |
1071 | git add bazzy && | |
1072 | STASH_ID=$(git stash create) && | |
1073 | git stash store --message="store message=foo" $STASH_ID && | |
1074 | echo "stash@{0}: store message=foo" >expect && | |
1075 | git stash list -1 >actual && | |
1076 | test_cmp expect actual | |
1077 | ' | |
1078 | ||
1079 | test_expect_success 'store --message foo shows right message' ' | |
1080 | git stash clear && | |
1081 | git reset --hard && | |
1082 | echo quux >bazzy && | |
1083 | git add bazzy && | |
1084 | STASH_ID=$(git stash create) && | |
1085 | git stash store --message "store message foo" $STASH_ID && | |
1086 | echo "stash@{0}: store message foo" >expect && | |
1087 | git stash list -1 >actual && | |
1088 | test_cmp expect actual | |
1089 | ' | |
1090 | ||
1091 | test_expect_success 'push -mfoo uses right message' ' | |
1092 | >foo && | |
1093 | git add foo && | |
1094 | git stash push -m"test mfoo" && | |
cbc75a12 | 1095 | echo "stash@{0}: On main: test mfoo" >expect && |
5675473f PH |
1096 | git stash list -1 >actual && |
1097 | test_cmp expect actual | |
1098 | ' | |
1099 | ||
1100 | test_expect_success 'push --message foo is synonym for -mfoo' ' | |
1101 | >foo && | |
1102 | git add foo && | |
1103 | git stash push --message "test message foo" && | |
cbc75a12 | 1104 | echo "stash@{0}: On main: test message foo" >expect && |
5675473f PH |
1105 | git stash list -1 >actual && |
1106 | test_cmp expect actual | |
1107 | ' | |
1108 | ||
1109 | test_expect_success 'push --message=foo is synonym for -mfoo' ' | |
1110 | >foo && | |
1111 | git add foo && | |
1112 | git stash push --message="test message=foo" && | |
cbc75a12 | 1113 | echo "stash@{0}: On main: test message=foo" >expect && |
5675473f PH |
1114 | git stash list -1 >actual && |
1115 | test_cmp expect actual | |
1116 | ' | |
1117 | ||
1118 | test_expect_success 'push -m shows right message' ' | |
1119 | >foo && | |
1120 | git add foo && | |
1121 | git stash push -m "test m foo" && | |
cbc75a12 | 1122 | echo "stash@{0}: On main: test m foo" >expect && |
5675473f PH |
1123 | git stash list -1 >actual && |
1124 | test_cmp expect actual | |
1125 | ' | |
1126 | ||
6f5ccd4d TG |
1127 | test_expect_success 'create stores correct message' ' |
1128 | >foo && | |
1129 | git add foo && | |
1130 | STASH_ID=$(git stash create "create test message") && | |
cbc75a12 | 1131 | echo "On main: create test message" >expect && |
6f5ccd4d TG |
1132 | git show --pretty=%s -s ${STASH_ID} >actual && |
1133 | test_cmp expect actual | |
1134 | ' | |
1135 | ||
ceaf037f GC |
1136 | test_expect_success 'create when branch name has /' ' |
1137 | test_when_finished "git checkout main" && | |
1138 | git checkout -b some/topic && | |
1139 | >foo && | |
1140 | git add foo && | |
1141 | STASH_ID=$(git stash create "create test message") && | |
1142 | echo "On some/topic: create test message" >expect && | |
1143 | git show --pretty=%s -s ${STASH_ID} >actual && | |
1144 | test_cmp expect actual | |
1145 | ' | |
1146 | ||
6f5ccd4d TG |
1147 | test_expect_success 'create with multiple arguments for the message' ' |
1148 | >foo && | |
1149 | git add foo && | |
1150 | STASH_ID=$(git stash create test untracked) && | |
cbc75a12 | 1151 | echo "On main: test untracked" >expect && |
6f5ccd4d TG |
1152 | git show --pretty=%s -s ${STASH_ID} >actual && |
1153 | test_cmp expect actual | |
1154 | ' | |
1155 | ||
4e9bf3dd | 1156 | test_expect_success 'create in a detached state' ' |
cbc75a12 | 1157 | test_when_finished "git checkout main" && |
4e9bf3dd JT |
1158 | git checkout HEAD~1 && |
1159 | >foo && | |
1160 | git add foo && | |
1161 | STASH_ID=$(git stash create) && | |
1162 | HEAD_ID=$(git rev-parse --short HEAD) && | |
1163 | echo "WIP on (no branch): ${HEAD_ID} initial" >expect && | |
1164 | git show --pretty=%s -s ${STASH_ID} >actual && | |
1165 | test_cmp expect actual | |
1166 | ' | |
1167 | ||
df6bba09 TG |
1168 | test_expect_success 'stash -- <pathspec> stashes and restores the file' ' |
1169 | >foo && | |
1170 | >bar && | |
1171 | git add foo bar && | |
1172 | git stash push -- foo && | |
1173 | test_path_is_file bar && | |
1174 | test_path_is_missing foo && | |
1175 | git stash pop && | |
1176 | test_path_is_file foo && | |
1177 | test_path_is_file bar | |
1178 | ' | |
1179 | ||
22fc703e PS |
1180 | test_expect_success 'stash -- <pathspec> stashes in subdirectory' ' |
1181 | mkdir sub && | |
1182 | >foo && | |
1183 | >bar && | |
1184 | git add foo bar && | |
1185 | ( | |
1186 | cd sub && | |
1187 | git stash push -- ../foo | |
1188 | ) && | |
1189 | test_path_is_file bar && | |
1190 | test_path_is_missing foo && | |
1191 | git stash pop && | |
1192 | test_path_is_file foo && | |
1193 | test_path_is_file bar | |
1194 | ' | |
1195 | ||
df6bba09 TG |
1196 | test_expect_success 'stash with multiple pathspec arguments' ' |
1197 | >foo && | |
1198 | >bar && | |
1199 | >extra && | |
1200 | git add foo bar extra && | |
1201 | git stash push -- foo bar && | |
1202 | test_path_is_missing bar && | |
1203 | test_path_is_missing foo && | |
1204 | test_path_is_file extra && | |
1205 | git stash pop && | |
1206 | test_path_is_file foo && | |
1207 | test_path_is_file bar && | |
1208 | test_path_is_file extra | |
1209 | ' | |
1210 | ||
1211 | test_expect_success 'stash with file including $IFS character' ' | |
1212 | >"foo bar" && | |
1213 | >foo && | |
1214 | >bar && | |
1215 | git add foo* && | |
1216 | git stash push -- "foo b*" && | |
1217 | test_path_is_missing "foo bar" && | |
1218 | test_path_is_file foo && | |
1219 | test_path_is_file bar && | |
1220 | git stash pop && | |
1221 | test_path_is_file "foo bar" && | |
1222 | test_path_is_file foo && | |
1223 | test_path_is_file bar | |
1224 | ' | |
1225 | ||
1226 | test_expect_success 'stash with pathspec matching multiple paths' ' | |
a8fcc0ac JC |
1227 | echo original >file && |
1228 | echo original >other-file && | |
1229 | git commit -m "two" file other-file && | |
1230 | echo modified >file && | |
1231 | echo modified >other-file && | |
1232 | git stash push -- "*file" && | |
1233 | echo original >expect && | |
1234 | test_cmp expect file && | |
1235 | test_cmp expect other-file && | |
1236 | git stash pop && | |
1237 | echo modified >expect && | |
1238 | test_cmp expect file && | |
1239 | test_cmp expect other-file | |
df6bba09 TG |
1240 | ' |
1241 | ||
1242 | test_expect_success 'stash push -p with pathspec shows no changes only once' ' | |
1243 | >foo && | |
1244 | git add foo && | |
1245 | git commit -m "tmp" && | |
1246 | git stash push -p foo >actual && | |
1247 | echo "No local changes to save" >expect && | |
1248 | git reset --hard HEAD~ && | |
1108cea7 | 1249 | test_cmp expect actual |
df6bba09 TG |
1250 | ' |
1251 | ||
ab8ad46e | 1252 | test_expect_success 'push <pathspec>: show no changes when there are none' ' |
df6bba09 TG |
1253 | >foo && |
1254 | git add foo && | |
1255 | git commit -m "tmp" && | |
1256 | git stash push foo >actual && | |
1257 | echo "No local changes to save" >expect && | |
1258 | git reset --hard HEAD~ && | |
1108cea7 | 1259 | test_cmp expect actual |
df6bba09 TG |
1260 | ' |
1261 | ||
ab8ad46e | 1262 | test_expect_success 'push: <pathspec> not in the repository errors out' ' |
df6bba09 TG |
1263 | >untracked && |
1264 | test_must_fail git stash push untracked && | |
1265 | test_path_is_file untracked | |
1266 | ' | |
1267 | ||
1ac528c0 PSU |
1268 | test_expect_success 'push: -q is quiet with changes' ' |
1269 | >foo && | |
1270 | git add foo && | |
1271 | git stash push -q >output 2>&1 && | |
1272 | test_must_be_empty output | |
1273 | ' | |
1274 | ||
1275 | test_expect_success 'push: -q is quiet with no changes' ' | |
1276 | git stash push -q >output 2>&1 && | |
1277 | test_must_be_empty output | |
1278 | ' | |
1279 | ||
1280 | test_expect_success 'push: -q is quiet even if there is no initial commit' ' | |
1281 | git init foo_dir && | |
1282 | test_when_finished rm -rf foo_dir && | |
1283 | ( | |
1284 | cd foo_dir && | |
1285 | >bar && | |
1286 | test_must_fail git stash push -q >output 2>&1 && | |
1287 | test_must_be_empty output | |
1288 | ) | |
1289 | ' | |
1290 | ||
df6bba09 TG |
1291 | test_expect_success 'untracked files are left in place when -u is not given' ' |
1292 | >file && | |
1293 | git add file && | |
1294 | >untracked && | |
1295 | git stash push file && | |
1296 | test_path_is_file untracked | |
1297 | ' | |
1298 | ||
9e140909 TG |
1299 | test_expect_success 'stash without verb with pathspec' ' |
1300 | >"foo bar" && | |
1301 | >foo && | |
1302 | >bar && | |
1303 | git add foo* && | |
1304 | git stash -- "foo b*" && | |
1305 | test_path_is_missing "foo bar" && | |
1306 | test_path_is_file foo && | |
1307 | test_path_is_file bar && | |
1308 | git stash pop && | |
1309 | test_path_is_file "foo bar" && | |
1310 | test_path_is_file foo && | |
1311 | test_path_is_file bar | |
1312 | ' | |
1313 | ||
e0e7f99e TG |
1314 | test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' ' |
1315 | git reset && | |
1316 | >foo && | |
1317 | >bar && | |
1318 | git add foo bar && | |
1319 | git commit -m "test" && | |
1320 | echo "foo" >foo && | |
1321 | echo "bar" >bar && | |
1322 | git stash -k -- foo && | |
1323 | test "",bar = $(cat foo),$(cat bar) && | |
1324 | git stash pop && | |
1325 | test foo,bar = $(cat foo),$(cat bar) | |
1326 | ' | |
1327 | ||
bba067d2 TG |
1328 | test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' ' |
1329 | git reset && | |
1330 | >subdir/untracked && | |
1331 | >subdir/tracked1 && | |
1332 | >subdir/tracked2 && | |
1333 | git add subdir/tracked* && | |
1334 | git stash -- subdir/ && | |
1335 | test_path_is_missing subdir/tracked1 && | |
1336 | test_path_is_missing subdir/tracked2 && | |
1337 | test_path_is_file subdir/untracked && | |
1338 | git stash pop && | |
1339 | test_path_is_file subdir/tracked1 && | |
1340 | test_path_is_file subdir/tracked2 && | |
1341 | test_path_is_file subdir/untracked | |
1342 | ' | |
1343 | ||
1344 | test_expect_success 'stash -- <subdir> works with binary files' ' | |
1345 | git reset && | |
1346 | >subdir/untracked && | |
1347 | >subdir/tracked && | |
1348 | cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary && | |
1349 | git add subdir/tracked* && | |
1350 | git stash -- subdir/ && | |
1351 | test_path_is_missing subdir/tracked && | |
1352 | test_path_is_missing subdir/tracked-binary && | |
1353 | test_path_is_file subdir/untracked && | |
1354 | git stash pop && | |
1355 | test_path_is_file subdir/tracked && | |
1356 | test_path_is_file subdir/tracked-binary && | |
1357 | test_path_is_file subdir/untracked | |
1358 | ' | |
1359 | ||
0640897d TG |
1360 | test_expect_success 'stash with user.name and user.email set works' ' |
1361 | test_config user.name "A U Thor" && | |
1362 | test_config user.email "a.u@thor" && | |
1363 | git stash | |
1364 | ' | |
1365 | ||
3bc2111f SD |
1366 | test_expect_success 'stash works when user.name and user.email are not set' ' |
1367 | git reset && | |
1368 | >1 && | |
1369 | git add 1 && | |
1370 | echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect && | |
1371 | git stash && | |
1372 | git show -s --format="%an <%ae>" refs/stash >actual && | |
1373 | test_cmp expect actual && | |
1374 | >2 && | |
1375 | git add 2 && | |
1376 | test_config user.useconfigonly true && | |
3bc2111f SD |
1377 | ( |
1378 | sane_unset GIT_AUTHOR_NAME && | |
1379 | sane_unset GIT_AUTHOR_EMAIL && | |
1380 | sane_unset GIT_COMMITTER_NAME && | |
1381 | sane_unset GIT_COMMITTER_EMAIL && | |
1382 | test_unconfig user.email && | |
1383 | test_unconfig user.name && | |
1384 | test_must_fail git commit -m "should fail" && | |
1385 | echo "git stash <git@stash>" >expect && | |
1386 | >2 && | |
1387 | git stash && | |
1388 | git show -s --format="%an <%ae>" refs/stash >actual && | |
1389 | test_cmp expect actual | |
1390 | ) | |
1391 | ' | |
1392 | ||
b932f6a5 TG |
1393 | test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' ' |
1394 | test_commit to-remove to-remove && | |
1395 | git rm to-remove && | |
1396 | git stash --keep-index && | |
1397 | test_path_is_missing to-remove | |
1398 | ' | |
1399 | ||
34933d0e TG |
1400 | test_expect_success 'stash apply should succeed with unmodified file' ' |
1401 | echo base >file && | |
1402 | git add file && | |
1403 | git commit -m base && | |
1404 | ||
1405 | # now stash a modification | |
1406 | echo modified >file && | |
1407 | git stash && | |
1408 | ||
1409 | # make the file stat dirty | |
1410 | cp file other && | |
1411 | mv other file && | |
1412 | ||
1413 | git stash apply | |
1414 | ' | |
1415 | ||
4a58c3d7 JS |
1416 | test_expect_success 'stash handles skip-worktree entries nicely' ' |
1417 | test_commit A && | |
1418 | echo changed >A.t && | |
1419 | git add A.t && | |
1420 | git update-index --skip-worktree A.t && | |
1421 | rm A.t && | |
1422 | git stash && | |
1423 | ||
1424 | git rev-parse --verify refs/stash:A.t | |
1425 | ' | |
1426 | ||
d42bab44 NS |
1427 | |
1428 | BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch' | |
1429 | ||
1430 | test_expect_success 'stash with core.fsyncmethod=batch' " | |
1431 | test_create_unique_files 2 4 files_base_dir && | |
1432 | GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ && | |
1433 | ||
1434 | # The files were untracked, so use the third parent, | |
1435 | # which contains the untracked files | |
1436 | git ls-tree -r stash^3 -- ./files_base_dir/ | | |
1437 | test_parse_ls_tree_oids >stashed_files_oids && | |
1438 | ||
1439 | # We created 2 dirs with 4 files each (8 files total) above | |
1440 | test_line_count = 8 stashed_files_oids && | |
1441 | git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual && | |
1442 | test_cmp stashed_files_oids stashed_files_actual | |
1443 | " | |
1444 | ||
1445 | ||
3d40e372 | 1446 | test_expect_success 'git stash succeeds despite directory/file change' ' |
4dbf7f30 EN |
1447 | test_create_repo directory_file_switch_v1 && |
1448 | ( | |
1449 | cd directory_file_switch_v1 && | |
1450 | test_commit init && | |
1451 | ||
1452 | test_write_lines this file has some words >filler && | |
1453 | git add filler && | |
1454 | git commit -m filler && | |
1455 | ||
1456 | git rm filler && | |
1457 | mkdir filler && | |
1458 | echo contents >filler/file && | |
1459 | git stash push | |
1460 | ) | |
1461 | ' | |
1462 | ||
bee8691f | 1463 | test_expect_success 'git stash can pop file -> directory saved changes' ' |
4dbf7f30 EN |
1464 | test_create_repo directory_file_switch_v2 && |
1465 | ( | |
1466 | cd directory_file_switch_v2 && | |
1467 | test_commit init && | |
1468 | ||
1469 | test_write_lines this file has some words >filler && | |
1470 | git add filler && | |
1471 | git commit -m filler && | |
1472 | ||
1473 | git rm filler && | |
1474 | mkdir filler && | |
1475 | echo contents >filler/file && | |
1476 | cp filler/file expect && | |
1477 | git stash push --include-untracked && | |
1478 | git stash apply --index && | |
1479 | test_cmp expect filler/file | |
1480 | ) | |
1481 | ' | |
1482 | ||
bee8691f | 1483 | test_expect_success 'git stash can pop directory -> file saved changes' ' |
4dbf7f30 EN |
1484 | test_create_repo directory_file_switch_v3 && |
1485 | ( | |
1486 | cd directory_file_switch_v3 && | |
1487 | test_commit init && | |
1488 | ||
1489 | mkdir filler && | |
1490 | test_write_lines some words >filler/file1 && | |
1491 | test_write_lines and stuff >filler/file2 && | |
1492 | git add filler && | |
1493 | git commit -m filler && | |
1494 | ||
1495 | git rm -rf filler && | |
1496 | echo contents >filler && | |
1497 | cp filler expect && | |
1498 | git stash push --include-untracked && | |
1499 | git stash apply --index && | |
1500 | test_cmp expect filler | |
1501 | ) | |
1502 | ' | |
1503 | ||
71cade5a EN |
1504 | test_expect_success 'restore untracked files even when we hit conflicts' ' |
1505 | git init restore_untracked_after_conflict && | |
1506 | ( | |
1507 | cd restore_untracked_after_conflict && | |
1508 | ||
1509 | echo hi >a && | |
1510 | echo there >b && | |
1511 | git add . && | |
1512 | git commit -m first && | |
1513 | echo hello >a && | |
1514 | echo something >c && | |
1515 | ||
1516 | git stash push --include-untracked && | |
1517 | ||
1518 | echo conflict >a && | |
1519 | git add a && | |
1520 | git commit -m second && | |
1521 | ||
1522 | test_must_fail git stash pop && | |
1523 | ||
1524 | test_path_is_file c | |
1525 | ) | |
1526 | ' | |
1527 | ||
d2058cb2 PS |
1528 | test_expect_success 'stash create reports a locked index' ' |
1529 | test_when_finished "rm -rf repo" && | |
1530 | git init repo && | |
1531 | ( | |
1532 | cd repo && | |
1533 | test_commit A A.file && | |
1534 | echo change >A.file && | |
1535 | touch .git/index.lock && | |
1536 | ||
1537 | cat >expect <<-EOF && | |
1538 | error: could not write index | |
1539 | EOF | |
1540 | test_must_fail git stash create 2>err && | |
1541 | test_cmp expect err | |
1542 | ) | |
1543 | ' | |
1544 | ||
1545 | test_expect_success 'stash push reports a locked index' ' | |
1546 | test_when_finished "rm -rf repo" && | |
1547 | git init repo && | |
1548 | ( | |
1549 | cd repo && | |
1550 | test_commit A A.file && | |
1551 | echo change >A.file && | |
1552 | touch .git/index.lock && | |
1553 | ||
1554 | cat >expect <<-EOF && | |
1555 | error: could not write index | |
1556 | EOF | |
1557 | test_must_fail git stash push 2>err && | |
1558 | test_cmp expect err | |
1559 | ) | |
1560 | ' | |
1561 | ||
1562 | test_expect_success 'stash apply reports a locked index' ' | |
1563 | test_when_finished "rm -rf repo" && | |
1564 | git init repo && | |
1565 | ( | |
1566 | cd repo && | |
1567 | test_commit A A.file && | |
1568 | echo change >A.file && | |
1569 | git stash push && | |
1570 | touch .git/index.lock && | |
1571 | ||
1572 | cat >expect <<-EOF && | |
1573 | error: could not write index | |
1574 | EOF | |
1575 | test_must_fail git stash apply 2>err && | |
1576 | test_cmp expect err | |
1577 | ) | |
1578 | ' | |
1579 | ||
150937c4 | 1580 | test_done |