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