]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1091-sparse-checkout-builtin.sh
sparse-checkout: avoid staging deletions of all files
[thirdparty/git.git] / t / t1091-sparse-checkout-builtin.sh
1 #!/bin/sh
2
3 test_description='sparse checkout builtin tests'
4
5 . ./test-lib.sh
6
7 list_files() {
8 # Do not replace this with 'ls "$1"', as "ls" with BSD-lineage
9 # enables "-A" by default for root and ends up including ".git" and
10 # such in its output. (Note, though, that running the test suite as
11 # root is generally not recommended.)
12 (cd "$1" && printf '%s\n' *)
13 }
14
15 check_files() {
16 list_files "$1" >actual &&
17 shift &&
18 printf "%s\n" $@ >expect &&
19 test_cmp expect actual
20 }
21
22 test_expect_success 'setup' '
23 git init repo &&
24 (
25 cd repo &&
26 echo "initial" >a &&
27 mkdir folder1 folder2 deep &&
28 mkdir deep/deeper1 deep/deeper2 &&
29 mkdir deep/deeper1/deepest &&
30 cp a folder1 &&
31 cp a folder2 &&
32 cp a deep &&
33 cp a deep/deeper1 &&
34 cp a deep/deeper2 &&
35 cp a deep/deeper1/deepest &&
36 git add . &&
37 git commit -m "initial commit"
38 )
39 '
40
41 test_expect_success 'git sparse-checkout list (empty)' '
42 git -C repo sparse-checkout list >list 2>err &&
43 test_must_be_empty list &&
44 test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err
45 '
46
47 test_expect_success 'git sparse-checkout list (populated)' '
48 test_when_finished rm -f repo/.git/info/sparse-checkout &&
49 cat >repo/.git/info/sparse-checkout <<-\EOF &&
50 /folder1/*
51 /deep/
52 **/a
53 !*bin*
54 EOF
55 cp repo/.git/info/sparse-checkout expect &&
56 git -C repo sparse-checkout list >list &&
57 test_cmp expect list
58 '
59
60 test_expect_success 'git sparse-checkout init' '
61 git -C repo sparse-checkout init &&
62 cat >expect <<-\EOF &&
63 /*
64 !/*/
65 EOF
66 test_cmp expect repo/.git/info/sparse-checkout &&
67 test_cmp_config -C repo true core.sparsecheckout &&
68 check_files repo a
69 '
70
71 test_expect_success 'git sparse-checkout list after init' '
72 git -C repo sparse-checkout list >actual &&
73 cat >expect <<-\EOF &&
74 /*
75 !/*/
76 EOF
77 test_cmp expect actual
78 '
79
80 test_expect_success 'init with existing sparse-checkout' '
81 echo "*folder*" >> repo/.git/info/sparse-checkout &&
82 git -C repo sparse-checkout init &&
83 cat >expect <<-\EOF &&
84 /*
85 !/*/
86 *folder*
87 EOF
88 test_cmp expect repo/.git/info/sparse-checkout &&
89 check_files repo a folder1 folder2
90 '
91
92 test_expect_success 'clone --sparse' '
93 git clone --sparse "file://$(pwd)/repo" clone &&
94 git -C clone sparse-checkout list >actual &&
95 cat >expect <<-\EOF &&
96 /*
97 !/*/
98 EOF
99 test_cmp expect actual &&
100 check_files clone a
101 '
102
103 test_expect_success 'interaction with clone --no-checkout (unborn index)' '
104 git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
105 git -C clone_no_checkout sparse-checkout init --cone &&
106 git -C clone_no_checkout sparse-checkout set folder1 &&
107
108 git -C clone_no_checkout sparse-checkout list >actual &&
109 cat >expect <<-\EOF &&
110 folder1
111 EOF
112 test_cmp expect actual &&
113
114 # nothing checked out, expect "No such file or directory"
115 ! ls clone_no_checkout/* >actual &&
116 test_must_be_empty actual &&
117 test_path_is_missing clone_no_checkout/.git/index &&
118
119 # No branch is checked out until we manually switch to one
120 git -C clone_no_checkout switch master &&
121 test_path_is_file clone_no_checkout/.git/index &&
122 check_files clone_no_checkout a folder1
123 '
124
125 test_expect_success 'set enables config' '
126 git init empty-config &&
127 (
128 cd empty-config &&
129 test_commit test file &&
130 test_path_is_missing .git/config.worktree &&
131 test_must_fail git sparse-checkout set nothing &&
132 test_path_is_file .git/config.worktree &&
133 test_must_fail git config core.sparseCheckout &&
134 git sparse-checkout set "/*" &&
135 test_cmp_config true core.sparseCheckout
136 )
137 '
138
139 test_expect_success 'set sparse-checkout using builtin' '
140 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
141 cat >expect <<-\EOF &&
142 /*
143 !/*/
144 *folder*
145 EOF
146 git -C repo sparse-checkout list >actual &&
147 test_cmp expect actual &&
148 test_cmp expect repo/.git/info/sparse-checkout &&
149 check_files repo a folder1 folder2
150 '
151
152 test_expect_success 'set sparse-checkout using --stdin' '
153 cat >expect <<-\EOF &&
154 /*
155 !/*/
156 /folder1/
157 /folder2/
158 EOF
159 git -C repo sparse-checkout set --stdin <expect &&
160 git -C repo sparse-checkout list >actual &&
161 test_cmp expect actual &&
162 test_cmp expect repo/.git/info/sparse-checkout &&
163 check_files repo "a folder1 folder2"
164 '
165
166 test_expect_success 'add to sparse-checkout' '
167 cat repo/.git/info/sparse-checkout >expect &&
168 cat >add <<-\EOF &&
169 pattern1
170 /folder1/
171 pattern2
172 EOF
173 cat add >>expect &&
174 git -C repo sparse-checkout add --stdin <add &&
175 git -C repo sparse-checkout list >actual &&
176 test_cmp expect actual &&
177 test_cmp expect repo/.git/info/sparse-checkout &&
178 check_files repo "a folder1 folder2"
179 '
180
181 test_expect_success 'cone mode: match patterns' '
182 git -C repo config --worktree core.sparseCheckoutCone true &&
183 rm -rf repo/a repo/folder1 repo/folder2 &&
184 git -C repo read-tree -mu HEAD 2>err &&
185 test_i18ngrep ! "disabling cone patterns" err &&
186 git -C repo reset --hard &&
187 check_files repo a folder1 folder2
188 '
189
190 test_expect_success 'cone mode: warn on bad pattern' '
191 test_when_finished mv sparse-checkout repo/.git/info/ &&
192 cp repo/.git/info/sparse-checkout . &&
193 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
194 git -C repo read-tree -mu HEAD 2>err &&
195 test_i18ngrep "unrecognized negative pattern" err
196 '
197
198 test_expect_success 'sparse-checkout disable' '
199 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
200 git -C repo sparse-checkout disable &&
201 test_path_is_file repo/.git/info/sparse-checkout &&
202 git -C repo config --list >config &&
203 test_must_fail git config core.sparseCheckout &&
204 check_files repo a deep folder1 folder2
205 '
206
207 test_expect_success 'cone mode: init and set' '
208 git -C repo sparse-checkout init --cone &&
209 git -C repo config --list >config &&
210 test_i18ngrep "core.sparsecheckoutcone=true" config &&
211 list_files repo >dir &&
212 echo a >expect &&
213 test_cmp expect dir &&
214 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
215 test_must_be_empty err &&
216 check_files repo a deep &&
217 check_files repo/deep a deeper1 &&
218 check_files repo/deep/deeper1 a deepest &&
219 cat >expect <<-\EOF &&
220 /*
221 !/*/
222 /deep/
223 !/deep/*/
224 /deep/deeper1/
225 !/deep/deeper1/*/
226 /deep/deeper1/deepest/
227 EOF
228 test_cmp expect repo/.git/info/sparse-checkout &&
229 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
230 folder1
231 folder2
232 EOF
233 test_must_be_empty err &&
234 check_files repo a folder1 folder2
235 '
236
237 test_expect_success 'cone mode: list' '
238 cat >expect <<-\EOF &&
239 folder1
240 folder2
241 EOF
242 git -C repo sparse-checkout set --stdin <expect &&
243 git -C repo sparse-checkout list >actual 2>err &&
244 test_must_be_empty err &&
245 test_cmp expect actual
246 '
247
248 test_expect_success 'cone mode: set with nested folders' '
249 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
250 test_line_count = 0 err &&
251 cat >expect <<-\EOF &&
252 /*
253 !/*/
254 /deep/
255 EOF
256 test_cmp repo/.git/info/sparse-checkout expect
257 '
258
259 test_expect_success 'cone mode: add independent path' '
260 git -C repo sparse-checkout set deep/deeper1 &&
261 git -C repo sparse-checkout add folder1 &&
262 cat >expect <<-\EOF &&
263 /*
264 !/*/
265 /deep/
266 !/deep/*/
267 /deep/deeper1/
268 /folder1/
269 EOF
270 test_cmp expect repo/.git/info/sparse-checkout &&
271 check_files repo a deep folder1
272 '
273
274 test_expect_success 'cone mode: add sibling path' '
275 git -C repo sparse-checkout set deep/deeper1 &&
276 git -C repo sparse-checkout add deep/deeper2 &&
277 cat >expect <<-\EOF &&
278 /*
279 !/*/
280 /deep/
281 !/deep/*/
282 /deep/deeper1/
283 /deep/deeper2/
284 EOF
285 test_cmp expect repo/.git/info/sparse-checkout &&
286 check_files repo a deep
287 '
288
289 test_expect_success 'cone mode: add parent path' '
290 git -C repo sparse-checkout set deep/deeper1 folder1 &&
291 git -C repo sparse-checkout add deep &&
292 cat >expect <<-\EOF &&
293 /*
294 !/*/
295 /deep/
296 /folder1/
297 EOF
298 test_cmp expect repo/.git/info/sparse-checkout &&
299 check_files repo a deep folder1
300 '
301
302 test_expect_success 'not-up-to-date does not block rest of sparsification' '
303 test_when_finished git -C repo sparse-checkout disable &&
304 test_when_finished git -C repo reset --hard &&
305 git -C repo sparse-checkout set deep &&
306
307 echo update >repo/deep/deeper2/a &&
308 cp repo/.git/info/sparse-checkout expect &&
309 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
310
311 git -C repo sparse-checkout set deep/deeper1 2>err &&
312
313 test_i18ngrep "The following paths are not up to date" err &&
314 test_cmp expect repo/.git/info/sparse-checkout &&
315 check_files repo/deep a deeper1 deeper2 &&
316 check_files repo/deep/deeper1 a deepest &&
317 check_files repo/deep/deeper1/deepest a &&
318 check_files repo/deep/deeper2 a
319 '
320
321 test_expect_success 'revert to old sparse-checkout on empty update' '
322 git init empty-test &&
323 (
324 echo >file &&
325 git add file &&
326 git commit -m "test" &&
327 test_must_fail git sparse-checkout set nothing 2>err &&
328 test_i18ngrep "Sparse checkout leaves no entry on working directory" err &&
329 test_i18ngrep ! ".git/index.lock" err &&
330 git sparse-checkout set file
331 )
332 '
333
334 test_expect_success 'fail when lock is taken' '
335 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
336 touch repo/.git/info/sparse-checkout.lock &&
337 test_must_fail git -C repo sparse-checkout set deep 2>err &&
338 test_i18ngrep "Unable to create .*\.lock" err
339 '
340
341 test_expect_success '.gitignore should not warn about cone mode' '
342 git -C repo config --worktree core.sparseCheckoutCone true &&
343 echo "**/bin/*" >repo/.gitignore &&
344 git -C repo reset --hard 2>err &&
345 test_i18ngrep ! "disabling cone patterns" err
346 '
347
348 test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status' '
349 git clone repo dirty &&
350 echo dirty >dirty/folder1/a &&
351
352 git -C dirty sparse-checkout init 2>err &&
353 test_i18ngrep "warning.*The following paths are not up to date" err &&
354
355 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
356 test_i18ngrep "warning.*The following paths are not up to date" err &&
357 test_path_is_file dirty/folder1/a &&
358
359 git -C dirty sparse-checkout disable 2>err &&
360 test_must_be_empty err &&
361
362 git -C dirty reset --hard &&
363 git -C dirty sparse-checkout init &&
364 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
365 test_path_is_missing dirty/folder1/a &&
366 git -C dirty sparse-checkout disable &&
367 test_path_is_file dirty/folder1/a
368 '
369
370 test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged status' '
371 git clone repo unmerged &&
372
373 cat >input <<-EOF &&
374 0 0000000000000000000000000000000000000000 folder1/a
375 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
376 EOF
377 git -C unmerged update-index --index-info <input &&
378
379 git -C unmerged sparse-checkout init 2>err &&
380 test_i18ngrep "warning.*The following paths are unmerged" err &&
381
382 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
383 test_i18ngrep "warning.*The following paths are unmerged" err &&
384 test_path_is_file dirty/folder1/a &&
385
386 git -C unmerged sparse-checkout disable 2>err &&
387 test_i18ngrep "warning.*The following paths are unmerged" err &&
388
389 git -C unmerged reset --hard &&
390 git -C unmerged sparse-checkout init &&
391 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
392 git -C unmerged sparse-checkout disable
393 '
394
395 test_expect_success 'sparse-checkout reapply' '
396 git clone repo tweak &&
397
398 echo dirty >tweak/deep/deeper2/a &&
399
400 cat >input <<-EOF &&
401 0 0000000000000000000000000000000000000000 folder1/a
402 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
403 EOF
404 git -C tweak update-index --index-info <input &&
405
406 git -C tweak sparse-checkout init --cone 2>err &&
407 test_i18ngrep "warning.*The following paths are not up to date" err &&
408 test_i18ngrep "warning.*The following paths are unmerged" err &&
409
410 git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
411 test_i18ngrep "warning.*The following paths are not up to date" err &&
412 test_i18ngrep "warning.*The following paths are unmerged" err &&
413
414 git -C tweak sparse-checkout reapply 2>err &&
415 test_i18ngrep "warning.*The following paths are not up to date" err &&
416 test_path_is_file tweak/deep/deeper2/a &&
417 test_i18ngrep "warning.*The following paths are unmerged" err &&
418 test_path_is_file tweak/folder1/a &&
419
420 git -C tweak checkout HEAD deep/deeper2/a &&
421 git -C tweak sparse-checkout reapply 2>err &&
422 test_i18ngrep ! "warning.*The following paths are not up to date" err &&
423 test_path_is_missing tweak/deep/deeper2/a &&
424 test_i18ngrep "warning.*The following paths are unmerged" err &&
425 test_path_is_file tweak/folder1/a &&
426
427 git -C tweak add folder1/a &&
428 git -C tweak sparse-checkout reapply 2>err &&
429 test_must_be_empty err &&
430 test_path_is_missing tweak/deep/deeper2/a &&
431 test_path_is_missing tweak/folder1/a &&
432
433 git -C tweak sparse-checkout disable
434 '
435
436 test_expect_success 'cone mode: set with core.ignoreCase=true' '
437 rm repo/.git/info/sparse-checkout &&
438 git -C repo sparse-checkout init --cone &&
439 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
440 cat >expect <<-\EOF &&
441 /*
442 !/*/
443 /folder1/
444 EOF
445 test_cmp expect repo/.git/info/sparse-checkout &&
446 check_files repo a folder1
447 '
448
449 test_expect_success 'interaction with submodules' '
450 git clone repo super &&
451 (
452 cd super &&
453 mkdir modules &&
454 git submodule add ../repo modules/child &&
455 git add . &&
456 git commit -m "add submodule" &&
457 git sparse-checkout init --cone &&
458 git sparse-checkout set folder1
459 ) &&
460 check_files super a folder1 modules &&
461 check_files super/modules/child a deep folder1 folder2
462 '
463
464 test_expect_success 'different sparse-checkouts with worktrees' '
465 git -C repo worktree add --detach ../worktree &&
466 check_files worktree "a deep folder1 folder2" &&
467 git -C worktree sparse-checkout init --cone &&
468 git -C repo sparse-checkout set folder1 &&
469 git -C worktree sparse-checkout set deep/deeper1 &&
470 check_files repo a folder1 &&
471 check_files worktree a deep
472 '
473
474 test_expect_success 'set using filename keeps file on-disk' '
475 git -C repo sparse-checkout set a deep &&
476 cat >expect <<-\EOF &&
477 /*
478 !/*/
479 /a/
480 /deep/
481 EOF
482 test_cmp expect repo/.git/info/sparse-checkout &&
483 check_files repo a deep
484 '
485
486 check_read_tree_errors () {
487 REPO=$1
488 FILES=$2
489 ERRORS=$3
490 git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
491 test_must_be_empty err &&
492 check_files $REPO "$FILES" &&
493 git -C $REPO read-tree -mu HEAD 2>err &&
494 if test -z "$ERRORS"
495 then
496 test_must_be_empty err
497 else
498 test_i18ngrep "$ERRORS" err
499 fi &&
500 check_files $REPO $FILES
501 }
502
503 test_expect_success 'pattern-checks: /A/**' '
504 cat >repo/.git/info/sparse-checkout <<-\EOF &&
505 /*
506 !/*/
507 /folder1/**
508 EOF
509 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
510 '
511
512 test_expect_success 'pattern-checks: /A/**/B/' '
513 cat >repo/.git/info/sparse-checkout <<-\EOF &&
514 /*
515 !/*/
516 /deep/**/deepest
517 EOF
518 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
519 check_files repo/deep "deeper1" &&
520 check_files repo/deep/deeper1 "deepest"
521 '
522
523 test_expect_success 'pattern-checks: too short' '
524 cat >repo/.git/info/sparse-checkout <<-\EOF &&
525 /*
526 !/*/
527 /
528 EOF
529 check_read_tree_errors repo "a" "disabling cone pattern matching"
530 '
531 test_expect_success 'pattern-checks: not too short' '
532 cat >repo/.git/info/sparse-checkout <<-\EOF &&
533 /*
534 !/*/
535 /b/
536 EOF
537 git -C repo read-tree -mu HEAD 2>err &&
538 test_must_be_empty err &&
539 check_files repo a
540 '
541
542 test_expect_success 'pattern-checks: trailing "*"' '
543 cat >repo/.git/info/sparse-checkout <<-\EOF &&
544 /*
545 !/*/
546 /a*
547 EOF
548 check_read_tree_errors repo "a" "disabling cone pattern matching"
549 '
550
551 test_expect_success 'pattern-checks: starting "*"' '
552 cat >repo/.git/info/sparse-checkout <<-\EOF &&
553 /*
554 !/*/
555 *eep/
556 EOF
557 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
558 '
559
560 test_expect_success 'pattern-checks: contained glob characters' '
561 for c in "[a]" "\\" "?" "*"
562 do
563 cat >repo/.git/info/sparse-checkout <<-EOF &&
564 /*
565 !/*/
566 something$c-else/
567 EOF
568 check_read_tree_errors repo "a" "disabling cone pattern matching"
569 done
570 '
571
572 test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
573 git clone repo escaped &&
574 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
575 NEWTREE=$(git -C escaped mktree <<-EOF
576 $(git -C escaped ls-tree HEAD)
577 040000 tree $TREEOID zbad\\dir
578 040000 tree $TREEOID zdoes*exist
579 040000 tree $TREEOID zglob[!a]?
580 EOF
581 ) &&
582 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
583 git -C escaped reset --hard $COMMIT &&
584 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
585 git -C escaped sparse-checkout init --cone &&
586 git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
587 cat >expect <<-\EOF &&
588 /*
589 !/*/
590 /zbad\\dir/
591 !/zbad\\dir/*/
592 /zbad\\dir/bogus/
593 /zdoes\*exist/
594 /zdoes\*not\*exist/
595 /zglob\[!a]\?/
596 EOF
597 test_cmp expect escaped/.git/info/sparse-checkout &&
598 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
599 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
600 git -C escaped sparse-checkout set --stdin <list-expect &&
601 cat >expect <<-\EOF &&
602 /*
603 !/*/
604 /deep/
605 /folder1/
606 /folder2/
607 /zbad\\dir/
608 /zdoes\*exist/
609 /zglob\[!a]\?/
610 EOF
611 test_cmp expect escaped/.git/info/sparse-checkout &&
612 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
613 git -C escaped sparse-checkout list >list-actual &&
614 test_cmp list-expect list-actual
615 '
616
617 test_expect_success MINGW 'cone mode replaces backslashes with slashes' '
618 git -C repo sparse-checkout set deep\\deeper1 &&
619 cat >expect <<-\EOF &&
620 /*
621 !/*/
622 /deep/
623 !/deep/*/
624 /deep/deeper1/
625 EOF
626 test_cmp expect repo/.git/info/sparse-checkout &&
627 check_files repo a deep &&
628 check_files repo/deep a deeper1
629 '
630
631 test_done