]> git.ipfire.org Git - thirdparty/git.git/blob - t/t1091-sparse-checkout-builtin.sh
Merge branch 'master' of github.com:git-l10n/git-po
[thirdparty/git.git] / t / t1091-sparse-checkout-builtin.sh
1 #!/bin/sh
2
3 test_description='sparse checkout builtin tests'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 GIT_TEST_SPLIT_INDEX=false
9 export GIT_TEST_SPLIT_INDEX
10
11 . ./test-lib.sh
12
13 list_files() {
14 # Do not replace this with 'ls "$1"', as "ls" with BSD-lineage
15 # enables "-A" by default for root and ends up including ".git" and
16 # such in its output. (Note, though, that running the test suite as
17 # root is generally not recommended.)
18 (cd "$1" && printf '%s\n' *)
19 }
20
21 check_files() {
22 list_files "$1" >actual &&
23 shift &&
24 printf "%s\n" $@ >expect &&
25 test_cmp expect actual
26 }
27
28 test_expect_success 'setup' '
29 git init repo &&
30 (
31 cd repo &&
32 echo "initial" >a &&
33 mkdir folder1 folder2 deep &&
34 mkdir deep/deeper1 deep/deeper2 &&
35 mkdir deep/deeper1/deepest &&
36 cp a folder1 &&
37 cp a folder2 &&
38 cp a deep &&
39 cp a deep/deeper1 &&
40 cp a deep/deeper2 &&
41 cp a deep/deeper1/deepest &&
42 git add . &&
43 git commit -m "initial commit"
44 )
45 '
46
47 test_expect_success 'git sparse-checkout list (not sparse)' '
48 test_must_fail git -C repo sparse-checkout list >list 2>err &&
49 test_must_be_empty list &&
50 test_i18ngrep "this worktree is not sparse" err
51 '
52
53 test_expect_success 'git sparse-checkout list (not sparse)' '
54 git -C repo sparse-checkout set &&
55 rm repo/.git/info/sparse-checkout &&
56 git -C repo sparse-checkout list >list 2>err &&
57 test_must_be_empty list &&
58 test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err
59 '
60
61 test_expect_success 'git sparse-checkout list (populated)' '
62 test_when_finished rm -f repo/.git/info/sparse-checkout &&
63 cat >repo/.git/info/sparse-checkout <<-\EOF &&
64 /folder1/*
65 /deep/
66 **/a
67 !*bin*
68 EOF
69 cp repo/.git/info/sparse-checkout expect &&
70 git -C repo sparse-checkout list >list &&
71 test_cmp expect list
72 '
73
74 test_expect_success 'git sparse-checkout init' '
75 git -C repo sparse-checkout init &&
76 cat >expect <<-\EOF &&
77 /*
78 !/*/
79 EOF
80 test_cmp expect repo/.git/info/sparse-checkout &&
81 test_cmp_config -C repo true core.sparsecheckout &&
82 check_files repo a
83 '
84
85 test_expect_success 'git sparse-checkout init in empty repo' '
86 test_when_finished rm -rf empty-repo blank-template &&
87 git init --template= empty-repo &&
88 git -C empty-repo sparse-checkout init
89 '
90
91 test_expect_success 'git sparse-checkout list after init' '
92 git -C repo sparse-checkout list >actual &&
93 cat >expect <<-\EOF &&
94 /*
95 !/*/
96 EOF
97 test_cmp expect actual
98 '
99
100 test_expect_success 'init with existing sparse-checkout' '
101 echo "*folder*" >> repo/.git/info/sparse-checkout &&
102 git -C repo sparse-checkout init &&
103 cat >expect <<-\EOF &&
104 /*
105 !/*/
106 *folder*
107 EOF
108 test_cmp expect repo/.git/info/sparse-checkout &&
109 check_files repo a folder1 folder2
110 '
111
112 test_expect_success 'clone --sparse' '
113 git clone --sparse "file://$(pwd)/repo" clone &&
114 git -C clone sparse-checkout list >actual &&
115 cat >expect <<-\EOF &&
116 /*
117 !/*/
118 EOF
119 test_cmp expect actual &&
120 check_files clone a
121 '
122
123 test_expect_success 'switching to cone mode with non-cone mode patterns' '
124 git init bad-patterns &&
125 (
126 cd bad-patterns &&
127 git sparse-checkout init &&
128 git sparse-checkout add dir &&
129 git config --worktree core.sparseCheckoutCone true &&
130 test_must_fail git sparse-checkout add dir 2>err &&
131 grep "existing sparse-checkout patterns do not use cone mode" err
132 )
133 '
134
135 test_expect_success 'interaction with clone --no-checkout (unborn index)' '
136 git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
137 git -C clone_no_checkout sparse-checkout init --cone &&
138 git -C clone_no_checkout sparse-checkout set folder1 &&
139
140 git -C clone_no_checkout sparse-checkout list >actual &&
141 cat >expect <<-\EOF &&
142 folder1
143 EOF
144 test_cmp expect actual &&
145
146 # nothing checked out, expect "No such file or directory"
147 ! ls clone_no_checkout/* >actual &&
148 test_must_be_empty actual &&
149 test_path_is_missing clone_no_checkout/.git/index &&
150
151 # No branch is checked out until we manually switch to one
152 git -C clone_no_checkout switch main &&
153 test_path_is_file clone_no_checkout/.git/index &&
154 check_files clone_no_checkout a folder1
155 '
156
157 test_expect_success 'set enables config' '
158 git init worktree-config &&
159 (
160 cd worktree-config &&
161 test_commit test file &&
162 test_path_is_missing .git/config.worktree &&
163 git sparse-checkout set nothing &&
164 test_path_is_file .git/config.worktree &&
165 test_cmp_config true core.sparseCheckout
166 )
167 '
168
169 test_expect_success 'set sparse-checkout using builtin' '
170 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
171 cat >expect <<-\EOF &&
172 /*
173 !/*/
174 *folder*
175 EOF
176 git -C repo sparse-checkout list >actual &&
177 test_cmp expect actual &&
178 test_cmp expect repo/.git/info/sparse-checkout &&
179 check_files repo a folder1 folder2
180 '
181
182 test_expect_success 'set sparse-checkout using --stdin' '
183 cat >expect <<-\EOF &&
184 /*
185 !/*/
186 /folder1/
187 /folder2/
188 EOF
189 git -C repo sparse-checkout set --stdin <expect &&
190 git -C repo sparse-checkout list >actual &&
191 test_cmp expect actual &&
192 test_cmp expect repo/.git/info/sparse-checkout &&
193 check_files repo "a folder1 folder2"
194 '
195
196 test_expect_success 'add to sparse-checkout' '
197 cat repo/.git/info/sparse-checkout >old &&
198 test_when_finished cp old repo/.git/info/sparse-checkout &&
199 cat >add <<-\EOF &&
200 pattern1
201 /folder1/
202 pattern2
203 EOF
204 cat old >expect &&
205 cat add >>expect &&
206 git -C repo sparse-checkout add --stdin <add &&
207 git -C repo sparse-checkout list >actual &&
208 test_cmp expect actual &&
209 test_cmp expect repo/.git/info/sparse-checkout &&
210 check_files repo "a folder1 folder2"
211 '
212
213 test_expect_success 'worktree: add copies sparse-checkout patterns' '
214 cat repo/.git/info/sparse-checkout >old &&
215 test_when_finished cp old repo/.git/info/sparse-checkout &&
216 test_when_finished git -C repo worktree remove ../worktree &&
217 git -C repo sparse-checkout set --no-cone "/*" &&
218 git -C repo worktree add --quiet ../worktree 2>err &&
219 test_must_be_empty err &&
220 new="$(git -C worktree rev-parse --git-path info/sparse-checkout)" &&
221 test_path_is_file "$new" &&
222 test_cmp repo/.git/info/sparse-checkout "$new" &&
223 git -C worktree sparse-checkout set --cone &&
224 test_cmp_config -C worktree true core.sparseCheckoutCone &&
225 test_must_fail git -C repo core.sparseCheckoutCone
226 '
227
228 test_expect_success 'cone mode: match patterns' '
229 git -C repo config --worktree core.sparseCheckoutCone true &&
230 rm -rf repo/a repo/folder1 repo/folder2 &&
231 git -C repo read-tree -mu HEAD 2>err &&
232 test_i18ngrep ! "disabling cone patterns" err &&
233 git -C repo reset --hard &&
234 check_files repo a folder1 folder2
235 '
236
237 test_expect_success 'cone mode: warn on bad pattern' '
238 test_when_finished mv sparse-checkout repo/.git/info/ &&
239 cp repo/.git/info/sparse-checkout . &&
240 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
241 git -C repo read-tree -mu HEAD 2>err &&
242 test_i18ngrep "unrecognized negative pattern" err
243 '
244
245 test_expect_success 'sparse-checkout disable' '
246 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
247 git -C repo sparse-checkout disable &&
248 test_path_is_file repo/.git/info/sparse-checkout &&
249 git -C repo config --list >config &&
250 test_must_fail git config core.sparseCheckout &&
251 check_files repo a deep folder1 folder2
252 '
253
254 test_expect_success 'sparse-index enabled and disabled' '
255 git -C repo sparse-checkout init --cone --sparse-index &&
256 test_cmp_config -C repo true index.sparse &&
257 git -C repo ls-files --sparse >sparse &&
258 git -C repo sparse-checkout disable &&
259 git -C repo ls-files --sparse >full &&
260
261 cat >expect <<-\EOF &&
262 @@ -1,4 +1,7 @@
263 a
264 -deep/
265 -folder1/
266 -folder2/
267 +deep/a
268 +deep/deeper1/a
269 +deep/deeper1/deepest/a
270 +deep/deeper2/a
271 +folder1/a
272 +folder2/a
273 EOF
274
275 diff -u sparse full | tail -n +3 >actual &&
276 test_cmp expect actual &&
277
278 git -C repo config --list >config &&
279 test_cmp_config -C repo false index.sparse
280 '
281
282 test_expect_success 'cone mode: init and set' '
283 git -C repo sparse-checkout init --cone &&
284 git -C repo config --list >config &&
285 test_i18ngrep "core.sparsecheckoutcone=true" config &&
286 list_files repo >dir &&
287 echo a >expect &&
288 test_cmp expect dir &&
289 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
290 test_must_be_empty err &&
291 check_files repo a deep &&
292 check_files repo/deep a deeper1 &&
293 check_files repo/deep/deeper1 a deepest &&
294 cat >expect <<-\EOF &&
295 /*
296 !/*/
297 /deep/
298 !/deep/*/
299 /deep/deeper1/
300 !/deep/deeper1/*/
301 /deep/deeper1/deepest/
302 EOF
303 test_cmp expect repo/.git/info/sparse-checkout &&
304 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
305 folder1
306 folder2
307 EOF
308 test_must_be_empty err &&
309 check_files repo a folder1 folder2
310 '
311
312 test_expect_success 'cone mode: list' '
313 cat >expect <<-\EOF &&
314 folder1
315 folder2
316 EOF
317 git -C repo sparse-checkout set --stdin <expect &&
318 git -C repo sparse-checkout list >actual 2>err &&
319 test_must_be_empty err &&
320 test_cmp expect actual
321 '
322
323 test_expect_success 'cone mode: set with nested folders' '
324 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
325 test_line_count = 0 err &&
326 cat >expect <<-\EOF &&
327 /*
328 !/*/
329 /deep/
330 EOF
331 test_cmp repo/.git/info/sparse-checkout expect
332 '
333
334 test_expect_success 'cone mode: add independent path' '
335 git -C repo sparse-checkout set deep/deeper1 &&
336 git -C repo sparse-checkout add folder1 &&
337 cat >expect <<-\EOF &&
338 /*
339 !/*/
340 /deep/
341 !/deep/*/
342 /deep/deeper1/
343 /folder1/
344 EOF
345 test_cmp expect repo/.git/info/sparse-checkout &&
346 check_files repo a deep folder1
347 '
348
349 test_expect_success 'cone mode: add sibling path' '
350 git -C repo sparse-checkout set deep/deeper1 &&
351 git -C repo sparse-checkout add deep/deeper2 &&
352 cat >expect <<-\EOF &&
353 /*
354 !/*/
355 /deep/
356 !/deep/*/
357 /deep/deeper1/
358 /deep/deeper2/
359 EOF
360 test_cmp expect repo/.git/info/sparse-checkout &&
361 check_files repo a deep
362 '
363
364 test_expect_success 'cone mode: add parent path' '
365 git -C repo sparse-checkout set deep/deeper1 folder1 &&
366 git -C repo sparse-checkout add deep &&
367 cat >expect <<-\EOF &&
368 /*
369 !/*/
370 /deep/
371 /folder1/
372 EOF
373 test_cmp expect repo/.git/info/sparse-checkout &&
374 check_files repo a deep folder1
375 '
376
377 test_expect_success 'not-up-to-date does not block rest of sparsification' '
378 test_when_finished git -C repo sparse-checkout disable &&
379 test_when_finished git -C repo reset --hard &&
380 git -C repo sparse-checkout set deep &&
381
382 echo update >repo/deep/deeper2/a &&
383 cp repo/.git/info/sparse-checkout expect &&
384 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
385
386 git -C repo sparse-checkout set deep/deeper1 2>err &&
387
388 test_i18ngrep "The following paths are not up to date" err &&
389 test_cmp expect repo/.git/info/sparse-checkout &&
390 check_files repo/deep a deeper1 deeper2 &&
391 check_files repo/deep/deeper1 a deepest &&
392 check_files repo/deep/deeper1/deepest a &&
393 check_files repo/deep/deeper2 a
394 '
395
396 test_expect_success 'revert to old sparse-checkout on empty update' '
397 git init empty-test &&
398 (
399 echo >file &&
400 git add file &&
401 git commit -m "test" &&
402 git sparse-checkout set nothing 2>err &&
403 test_i18ngrep ! "Sparse checkout leaves no entry on working directory" err &&
404 test_i18ngrep ! ".git/index.lock" err &&
405 git sparse-checkout set file
406 )
407 '
408
409 test_expect_success 'fail when lock is taken' '
410 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
411 touch repo/.git/info/sparse-checkout.lock &&
412 test_must_fail git -C repo sparse-checkout set deep 2>err &&
413 test_i18ngrep "Unable to create .*\.lock" err
414 '
415
416 test_expect_success '.gitignore should not warn about cone mode' '
417 git -C repo config --worktree core.sparseCheckoutCone true &&
418 echo "**/bin/*" >repo/.gitignore &&
419 git -C repo reset --hard 2>err &&
420 test_i18ngrep ! "disabling cone patterns" err
421 '
422
423 test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status' '
424 git clone repo dirty &&
425 echo dirty >dirty/folder1/a &&
426
427 git -C dirty sparse-checkout init 2>err &&
428 test_i18ngrep "warning.*The following paths are not up to date" err &&
429
430 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
431 test_i18ngrep "warning.*The following paths are not up to date" err &&
432 test_path_is_file dirty/folder1/a &&
433
434 git -C dirty sparse-checkout disable 2>err &&
435 test_must_be_empty err &&
436
437 git -C dirty reset --hard &&
438 git -C dirty sparse-checkout init &&
439 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
440 test_path_is_missing dirty/folder1/a &&
441 git -C dirty sparse-checkout disable &&
442 test_path_is_file dirty/folder1/a
443 '
444
445 test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged status' '
446 git clone repo unmerged &&
447
448 cat >input <<-EOF &&
449 0 $ZERO_OID folder1/a
450 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
451 EOF
452 git -C unmerged update-index --index-info <input &&
453
454 git -C unmerged sparse-checkout init 2>err &&
455 test_i18ngrep "warning.*The following paths are unmerged" err &&
456
457 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
458 test_i18ngrep "warning.*The following paths are unmerged" err &&
459 test_path_is_file dirty/folder1/a &&
460
461 git -C unmerged sparse-checkout disable 2>err &&
462 test_i18ngrep "warning.*The following paths are unmerged" err &&
463
464 git -C unmerged reset --hard &&
465 git -C unmerged sparse-checkout init &&
466 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
467 git -C unmerged sparse-checkout disable
468 '
469
470 test_expect_failure 'sparse-checkout reapply' '
471 git clone repo tweak &&
472
473 echo dirty >tweak/deep/deeper2/a &&
474
475 cat >input <<-EOF &&
476 0 $ZERO_OID folder1/a
477 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
478 EOF
479 git -C tweak update-index --index-info <input &&
480
481 git -C tweak sparse-checkout init --cone 2>err &&
482 test_i18ngrep "warning.*The following paths are not up to date" err &&
483 test_i18ngrep "warning.*The following paths are unmerged" err &&
484
485 git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
486 test_i18ngrep "warning.*The following paths are not up to date" err &&
487 test_i18ngrep "warning.*The following paths are unmerged" err &&
488
489 git -C tweak sparse-checkout reapply 2>err &&
490 test_i18ngrep "warning.*The following paths are not up to date" err &&
491 test_path_is_file tweak/deep/deeper2/a &&
492 test_i18ngrep "warning.*The following paths are unmerged" err &&
493 test_path_is_file tweak/folder1/a &&
494
495 git -C tweak checkout HEAD deep/deeper2/a &&
496 git -C tweak sparse-checkout reapply 2>err &&
497 test_i18ngrep ! "warning.*The following paths are not up to date" err &&
498 test_path_is_missing tweak/deep/deeper2/a &&
499 test_i18ngrep "warning.*The following paths are unmerged" err &&
500 test_path_is_file tweak/folder1/a &&
501
502 # NEEDSWORK: We are asking to update a file outside of the
503 # sparse-checkout cone, but this is no longer allowed.
504 git -C tweak add folder1/a &&
505 git -C tweak sparse-checkout reapply 2>err &&
506 test_must_be_empty err &&
507 test_path_is_missing tweak/deep/deeper2/a &&
508 test_path_is_missing tweak/folder1/a &&
509
510 git -C tweak sparse-checkout disable
511 '
512
513 test_expect_success 'reapply can handle config options' '
514 git -C repo sparse-checkout init --cone --no-sparse-index &&
515 git -C repo config --worktree --list >actual &&
516 cat >expect <<-\EOF &&
517 core.sparsecheckout=true
518 core.sparsecheckoutcone=true
519 index.sparse=false
520 EOF
521 test_cmp expect actual &&
522
523 git -C repo sparse-checkout reapply --no-cone --no-sparse-index &&
524 git -C repo config --worktree --list >actual &&
525 cat >expect <<-\EOF &&
526 core.sparsecheckout=true
527 core.sparsecheckoutcone=false
528 index.sparse=false
529 EOF
530 test_cmp expect actual &&
531
532 git -C repo sparse-checkout reapply --cone --sparse-index &&
533 git -C repo config --worktree --list >actual &&
534 cat >expect <<-\EOF &&
535 core.sparsecheckout=true
536 core.sparsecheckoutcone=true
537 index.sparse=true
538 EOF
539 test_cmp expect actual &&
540
541 git -C repo sparse-checkout disable
542 '
543
544 test_expect_success 'cone mode: set with core.ignoreCase=true' '
545 rm repo/.git/info/sparse-checkout &&
546 git -C repo sparse-checkout init --cone &&
547 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
548 cat >expect <<-\EOF &&
549 /*
550 !/*/
551 /folder1/
552 EOF
553 test_cmp expect repo/.git/info/sparse-checkout &&
554 check_files repo a folder1
555 '
556
557 test_expect_success 'interaction with submodules' '
558 git clone repo super &&
559 (
560 cd super &&
561 mkdir modules &&
562 git submodule add ../repo modules/child &&
563 git add . &&
564 git commit -m "add submodule" &&
565 git sparse-checkout init --cone &&
566 git sparse-checkout set folder1
567 ) &&
568 check_files super a folder1 modules &&
569 check_files super/modules/child a deep folder1 folder2
570 '
571
572 test_expect_success 'different sparse-checkouts with worktrees' '
573 git -C repo sparse-checkout set --cone deep folder1 &&
574 git -C repo worktree add --detach ../worktree &&
575 check_files worktree "a deep folder1" &&
576 git -C repo sparse-checkout set --cone folder1 &&
577 git -C worktree sparse-checkout set --cone deep/deeper1 &&
578 check_files repo "a folder1" &&
579 check_files worktree "a deep"
580 '
581
582 test_expect_success 'set using filename keeps file on-disk' '
583 git -C repo sparse-checkout set --skip-checks a deep &&
584 cat >expect <<-\EOF &&
585 /*
586 !/*/
587 /a/
588 /deep/
589 EOF
590 test_cmp expect repo/.git/info/sparse-checkout &&
591 check_files repo a deep
592 '
593
594 check_read_tree_errors () {
595 REPO=$1
596 FILES=$2
597 ERRORS=$3
598 git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
599 test_must_be_empty err &&
600 check_files $REPO "$FILES" &&
601 git -C $REPO read-tree -mu HEAD 2>err &&
602 if test -z "$ERRORS"
603 then
604 test_must_be_empty err
605 else
606 test_i18ngrep "$ERRORS" err
607 fi &&
608 check_files $REPO $FILES
609 }
610
611 test_expect_success 'pattern-checks: /A/**' '
612 cat >repo/.git/info/sparse-checkout <<-\EOF &&
613 /*
614 !/*/
615 /folder1/**
616 EOF
617 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
618 '
619
620 test_expect_success 'pattern-checks: /A/**/B/' '
621 cat >repo/.git/info/sparse-checkout <<-\EOF &&
622 /*
623 !/*/
624 /deep/**/deepest
625 EOF
626 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
627 check_files repo/deep "deeper1" &&
628 check_files repo/deep/deeper1 "deepest"
629 '
630
631 test_expect_success 'pattern-checks: too short' '
632 cat >repo/.git/info/sparse-checkout <<-\EOF &&
633 /*
634 !/*/
635 /
636 EOF
637 check_read_tree_errors repo "a" "disabling cone pattern matching"
638 '
639 test_expect_success 'pattern-checks: not too short' '
640 cat >repo/.git/info/sparse-checkout <<-\EOF &&
641 /*
642 !/*/
643 /b/
644 EOF
645 git -C repo read-tree -mu HEAD 2>err &&
646 test_must_be_empty err &&
647 check_files repo a
648 '
649
650 test_expect_success 'pattern-checks: trailing "*"' '
651 cat >repo/.git/info/sparse-checkout <<-\EOF &&
652 /*
653 !/*/
654 /a*
655 EOF
656 check_read_tree_errors repo "a" "disabling cone pattern matching"
657 '
658
659 test_expect_success 'pattern-checks: starting "*"' '
660 cat >repo/.git/info/sparse-checkout <<-\EOF &&
661 /*
662 !/*/
663 *eep/
664 EOF
665 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
666 '
667
668 test_expect_success 'pattern-checks: contained glob characters' '
669 for c in "[a]" "\\" "?" "*"
670 do
671 cat >repo/.git/info/sparse-checkout <<-EOF &&
672 /*
673 !/*/
674 something$c-else/
675 EOF
676 check_read_tree_errors repo "a" "disabling cone pattern matching" || return 1
677 done
678 '
679
680 test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
681 git clone repo escaped &&
682 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
683 NEWTREE=$(git -C escaped mktree <<-EOF
684 $(git -C escaped ls-tree HEAD)
685 040000 tree $TREEOID zbad\\dir
686 040000 tree $TREEOID zdoes*exist
687 040000 tree $TREEOID zglob[!a]?
688 EOF
689 ) &&
690 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
691 git -C escaped reset --hard $COMMIT &&
692 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
693 git -C escaped sparse-checkout init --cone &&
694 git -C escaped sparse-checkout set --skip-checks zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
695 cat >expect <<-\EOF &&
696 /*
697 !/*/
698 /zbad\\dir/
699 !/zbad\\dir/*/
700 /zbad\\dir/bogus/
701 /zdoes\*exist/
702 /zdoes\*not\*exist/
703 /zglob\[!a]\?/
704 EOF
705 test_cmp expect escaped/.git/info/sparse-checkout &&
706 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
707 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
708 git -C escaped sparse-checkout set --stdin <list-expect &&
709 cat >expect <<-\EOF &&
710 /*
711 !/*/
712 /deep/
713 /folder1/
714 /folder2/
715 /zbad\\dir/
716 /zdoes\*exist/
717 /zglob\[!a]\?/
718 EOF
719 test_cmp expect escaped/.git/info/sparse-checkout &&
720 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
721 git -C escaped sparse-checkout list >list-actual &&
722 test_cmp list-expect list-actual
723 '
724
725 test_expect_success MINGW 'cone mode replaces backslashes with slashes' '
726 git -C repo sparse-checkout set deep\\deeper1 &&
727 cat >expect <<-\EOF &&
728 /*
729 !/*/
730 /deep/
731 !/deep/*/
732 /deep/deeper1/
733 EOF
734 test_cmp expect repo/.git/info/sparse-checkout &&
735 check_files repo a deep &&
736 check_files repo/deep a deeper1
737 '
738
739 test_expect_success 'cone mode clears ignored subdirectories' '
740 rm repo/.git/info/sparse-checkout &&
741
742 git -C repo sparse-checkout init --cone &&
743 git -C repo sparse-checkout set deep/deeper1 &&
744
745 cat >repo/.gitignore <<-\EOF &&
746 obj/
747 *.o
748 EOF
749
750 git -C repo add .gitignore &&
751 git -C repo commit -m ".gitignore" &&
752
753 mkdir -p repo/obj repo/folder1/obj repo/deep/deeper2/obj &&
754 for file in folder1/obj/a obj/a folder1/file.o folder1.o \
755 deep/deeper2/obj/a deep/deeper2/file.o file.o
756 do
757 echo ignored >repo/$file || return 1
758 done &&
759
760 git -C repo status --porcelain=v2 >out &&
761 test_must_be_empty out &&
762
763 git -C repo sparse-checkout reapply &&
764 test_path_is_missing repo/folder1 &&
765 test_path_is_missing repo/deep/deeper2 &&
766 test_path_is_dir repo/obj &&
767 test_path_is_file repo/file.o &&
768
769 git -C repo status --porcelain=v2 >out &&
770 test_must_be_empty out &&
771
772 git -C repo sparse-checkout set deep/deeper2 &&
773 test_path_is_missing repo/deep/deeper1 &&
774 test_path_is_dir repo/deep/deeper2 &&
775 test_path_is_dir repo/obj &&
776 test_path_is_file repo/file.o &&
777
778 >repo/deep/deeper2/ignored.o &&
779 >repo/deep/deeper2/untracked &&
780
781 # When an untracked file is in the way, all untracked files
782 # (even ignored files) are preserved.
783 git -C repo sparse-checkout set folder1 2>err &&
784 grep "contains untracked files" err &&
785 test_path_is_file repo/deep/deeper2/ignored.o &&
786 test_path_is_file repo/deep/deeper2/untracked &&
787
788 # The rest of the cone matches expectation
789 test_path_is_missing repo/deep/deeper1 &&
790 test_path_is_dir repo/obj &&
791 test_path_is_file repo/file.o &&
792
793 git -C repo status --porcelain=v2 >out &&
794 echo "? deep/deeper2/untracked" >expect &&
795 test_cmp expect out
796 '
797
798 test_expect_success 'malformed cone-mode patterns' '
799 git -C repo sparse-checkout init --cone &&
800 mkdir -p repo/foo/bar &&
801 touch repo/foo/bar/x repo/foo/y &&
802 cat >repo/.git/info/sparse-checkout <<-\EOF &&
803 /*
804 !/*/
805 /foo/
806 !/foo/*/
807 /foo/\*/
808 EOF
809
810 # Listing the patterns will notice the duplicate pattern and
811 # emit a warning. It will list the patterns directly instead
812 # of using the cone-mode translation to a set of directories.
813 git -C repo sparse-checkout list >actual 2>err &&
814 test_cmp repo/.git/info/sparse-checkout actual &&
815 grep "warning: your sparse-checkout file may have issues: pattern .* is repeated" err &&
816 grep "warning: disabling cone pattern matching" err
817 '
818
819 test_expect_success 'set from subdir pays attention to prefix' '
820 git -C repo sparse-checkout disable &&
821 git -C repo/deep sparse-checkout set --cone deeper2 ../folder1 &&
822
823 git -C repo sparse-checkout list >actual &&
824
825 cat >expect <<-\EOF &&
826 deep/deeper2
827 folder1
828 EOF
829 test_cmp expect actual
830 '
831
832 test_expect_success 'add from subdir pays attention to prefix' '
833 git -C repo sparse-checkout set --cone deep/deeper2 &&
834 git -C repo/deep sparse-checkout add deeper1/deepest ../folder1 &&
835
836 git -C repo sparse-checkout list >actual &&
837
838 cat >expect <<-\EOF &&
839 deep/deeper1/deepest
840 deep/deeper2
841 folder1
842 EOF
843 test_cmp expect actual
844 '
845
846 test_expect_success 'set from subdir in non-cone mode throws an error' '
847 git -C repo sparse-checkout disable &&
848 test_must_fail git -C repo/deep sparse-checkout set --no-cone deeper2 ../folder1 2>error &&
849
850 grep "run from the toplevel directory in non-cone mode" error
851 '
852
853 test_expect_success 'set from subdir in non-cone mode throws an error' '
854 git -C repo sparse-checkout set --no-cone deep/deeper2 &&
855 test_must_fail git -C repo/deep sparse-checkout add deeper1/deepest ../folder1 2>error &&
856
857 grep "run from the toplevel directory in non-cone mode" error
858 '
859
860 test_expect_success 'by default, cone mode will error out when passed files' '
861 git -C repo sparse-checkout reapply --cone &&
862 test_must_fail git -C repo sparse-checkout add .gitignore 2>error &&
863
864 grep ".gitignore.*is not a directory" error
865 '
866
867 test_expect_success 'by default, non-cone mode will warn on individual files' '
868 git -C repo sparse-checkout reapply --no-cone &&
869 git -C repo sparse-checkout add .gitignore 2>warning &&
870
871 grep "pass a leading slash before paths.*if you want a single file" warning
872 '
873
874 test_done