3 test_description
='sparse checkout builtin tests'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 # Do not replace this with 'ls "$1"', as "ls" with BSD-lineage
12 # enables "-A" by default for root and ends up including ".git" and
13 # such in its output. (Note, though, that running the test suite as
14 # root is generally not recommended.)
15 (cd "$1" && printf '%s\n' *)
19 list_files
"$1" >actual
&&
21 printf "%s\n" $@
>expect
&&
22 test_cmp expect actual
25 test_expect_success
'setup' '
30 mkdir folder1 folder2 deep &&
31 mkdir deep/deeper1 deep/deeper2 &&
32 mkdir deep/deeper1/deepest &&
38 cp a deep/deeper1/deepest &&
40 git commit -m "initial commit"
44 test_expect_success
'git sparse-checkout list (empty)' '
45 git -C repo sparse-checkout list >list 2>err &&
46 test_must_be_empty list &&
47 test_i18ngrep "this worktree is not sparse (sparse-checkout file may not exist)" err
50 test_expect_success
'git sparse-checkout list (populated)' '
51 test_when_finished rm -f repo/.git/info/sparse-checkout &&
52 cat >repo/.git/info/sparse-checkout <<-\EOF &&
58 cp repo/.git/info/sparse-checkout expect &&
59 git -C repo sparse-checkout list >list &&
63 test_expect_success
'git sparse-checkout init' '
64 git -C repo sparse-checkout init &&
65 cat >expect <<-\EOF &&
69 test_cmp expect repo/.git/info/sparse-checkout &&
70 test_cmp_config -C repo true core.sparsecheckout &&
74 test_expect_success
'git sparse-checkout list after init' '
75 git -C repo sparse-checkout list >actual &&
76 cat >expect <<-\EOF &&
80 test_cmp expect actual
83 test_expect_success
'init with existing sparse-checkout' '
84 echo "*folder*" >> repo/.git/info/sparse-checkout &&
85 git -C repo sparse-checkout init &&
86 cat >expect <<-\EOF &&
91 test_cmp expect repo/.git/info/sparse-checkout &&
92 check_files repo a folder1 folder2
95 test_expect_success
'clone --sparse' '
96 git clone --sparse "file://$(pwd)/repo" clone &&
97 git -C clone sparse-checkout list >actual &&
98 cat >expect <<-\EOF &&
102 test_cmp expect actual &&
106 test_expect_success
'interaction with clone --no-checkout (unborn index)' '
107 git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
108 git -C clone_no_checkout sparse-checkout init --cone &&
109 git -C clone_no_checkout sparse-checkout set folder1 &&
111 git -C clone_no_checkout sparse-checkout list >actual &&
112 cat >expect <<-\EOF &&
115 test_cmp expect actual &&
117 # nothing checked out, expect "No such file or directory"
118 ! ls clone_no_checkout/* >actual &&
119 test_must_be_empty actual &&
120 test_path_is_missing clone_no_checkout/.git/index &&
122 # No branch is checked out until we manually switch to one
123 git -C clone_no_checkout switch main &&
124 test_path_is_file clone_no_checkout/.git/index &&
125 check_files clone_no_checkout a folder1
128 test_expect_success
'set enables config' '
129 git init empty-config &&
132 test_commit test file &&
133 test_path_is_missing .git/config.worktree &&
134 git sparse-checkout set nothing &&
135 test_path_is_file .git/config.worktree &&
136 test_cmp_config true core.sparseCheckout
140 test_expect_success
'set sparse-checkout using builtin' '
141 git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
142 cat >expect <<-\EOF &&
147 git -C repo sparse-checkout list >actual &&
148 test_cmp expect actual &&
149 test_cmp expect repo/.git/info/sparse-checkout &&
150 check_files repo a folder1 folder2
153 test_expect_success
'set sparse-checkout using --stdin' '
154 cat >expect <<-\EOF &&
160 git -C repo sparse-checkout set --stdin <expect &&
161 git -C repo sparse-checkout list >actual &&
162 test_cmp expect actual &&
163 test_cmp expect repo/.git/info/sparse-checkout &&
164 check_files repo "a folder1 folder2"
167 test_expect_success
'add to sparse-checkout' '
168 cat repo/.git/info/sparse-checkout >expect &&
175 git -C repo sparse-checkout add --stdin <add &&
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"
182 test_expect_success
'cone mode: match patterns' '
183 git -C repo config --worktree core.sparseCheckoutCone true &&
184 rm -rf repo/a repo/folder1 repo/folder2 &&
185 git -C repo read-tree -mu HEAD 2>err &&
186 test_i18ngrep ! "disabling cone patterns" err &&
187 git -C repo reset --hard &&
188 check_files repo a folder1 folder2
191 test_expect_success
'cone mode: warn on bad pattern' '
192 test_when_finished mv sparse-checkout repo/.git/info/ &&
193 cp repo/.git/info/sparse-checkout . &&
194 echo "!/deep/deeper/*" >>repo/.git/info/sparse-checkout &&
195 git -C repo read-tree -mu HEAD 2>err &&
196 test_i18ngrep "unrecognized negative pattern" err
199 test_expect_success
'sparse-checkout disable' '
200 test_when_finished rm -rf repo/.git/info/sparse-checkout &&
201 git -C repo sparse-checkout disable &&
202 test_path_is_file repo/.git/info/sparse-checkout &&
203 git -C repo config --list >config &&
204 test_must_fail git config core.sparseCheckout &&
205 check_files repo a deep folder1 folder2
208 test_expect_success
'sparse-index enabled and disabled' '
210 sane_unset GIT_TEST_SPLIT_INDEX &&
211 git -C repo update-index --no-split-index &&
213 git -C repo sparse-checkout init --cone --sparse-index &&
214 test_cmp_config -C repo true index.sparse &&
215 test-tool -C repo read-cache --table >cache &&
216 grep " tree " cache &&
218 git -C repo sparse-checkout disable &&
219 test-tool -C repo read-cache --table >cache &&
220 ! grep " tree " cache &&
221 git -C repo config --list >config &&
222 ! grep index.sparse config
226 test_expect_success
'cone mode: init and set' '
227 git -C repo sparse-checkout init --cone &&
228 git -C repo config --list >config &&
229 test_i18ngrep "core.sparsecheckoutcone=true" config &&
230 list_files repo >dir &&
232 test_cmp expect dir &&
233 git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
234 test_must_be_empty err &&
235 check_files repo a deep &&
236 check_files repo/deep a deeper1 &&
237 check_files repo/deep/deeper1 a deepest &&
238 cat >expect <<-\EOF &&
245 /deep/deeper1/deepest/
247 test_cmp expect repo/.git/info/sparse-checkout &&
248 git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
252 test_must_be_empty err &&
253 check_files repo a folder1 folder2
256 test_expect_success
'cone mode: list' '
257 cat >expect <<-\EOF &&
261 git -C repo sparse-checkout set --stdin <expect &&
262 git -C repo sparse-checkout list >actual 2>err &&
263 test_must_be_empty err &&
264 test_cmp expect actual
267 test_expect_success
'cone mode: set with nested folders' '
268 git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
269 test_line_count = 0 err &&
270 cat >expect <<-\EOF &&
275 test_cmp repo/.git/info/sparse-checkout expect
278 test_expect_success
'cone mode: add independent path' '
279 git -C repo sparse-checkout set deep/deeper1 &&
280 git -C repo sparse-checkout add folder1 &&
281 cat >expect <<-\EOF &&
289 test_cmp expect repo/.git/info/sparse-checkout &&
290 check_files repo a deep folder1
293 test_expect_success
'cone mode: add sibling path' '
294 git -C repo sparse-checkout set deep/deeper1 &&
295 git -C repo sparse-checkout add deep/deeper2 &&
296 cat >expect <<-\EOF &&
304 test_cmp expect repo/.git/info/sparse-checkout &&
305 check_files repo a deep
308 test_expect_success
'cone mode: add parent path' '
309 git -C repo sparse-checkout set deep/deeper1 folder1 &&
310 git -C repo sparse-checkout add deep &&
311 cat >expect <<-\EOF &&
317 test_cmp expect repo/.git/info/sparse-checkout &&
318 check_files repo a deep folder1
321 test_expect_success
'not-up-to-date does not block rest of sparsification' '
322 test_when_finished git -C repo sparse-checkout disable &&
323 test_when_finished git -C repo reset --hard &&
324 git -C repo sparse-checkout set deep &&
326 echo update >repo/deep/deeper2/a &&
327 cp repo/.git/info/sparse-checkout expect &&
328 test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
330 git -C repo sparse-checkout set deep/deeper1 2>err &&
332 test_i18ngrep "The following paths are not up to date" err &&
333 test_cmp expect repo/.git/info/sparse-checkout &&
334 check_files repo/deep a deeper1 deeper2 &&
335 check_files repo/deep/deeper1 a deepest &&
336 check_files repo/deep/deeper1/deepest a &&
337 check_files repo/deep/deeper2 a
340 test_expect_success
'revert to old sparse-checkout on empty update' '
341 git init empty-test &&
345 git commit -m "test" &&
346 git sparse-checkout set nothing 2>err &&
347 test_i18ngrep ! "Sparse checkout leaves no entry on working directory" err &&
348 test_i18ngrep ! ".git/index.lock" err &&
349 git sparse-checkout set file
353 test_expect_success
'fail when lock is taken' '
354 test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
355 touch repo/.git/info/sparse-checkout.lock &&
356 test_must_fail git -C repo sparse-checkout set deep 2>err &&
357 test_i18ngrep "Unable to create .*\.lock" err
360 test_expect_success
'.gitignore should not warn about cone mode' '
361 git -C repo config --worktree core.sparseCheckoutCone true &&
362 echo "**/bin/*" >repo/.gitignore &&
363 git -C repo reset --hard 2>err &&
364 test_i18ngrep ! "disabling cone patterns" err
367 test_expect_success
'sparse-checkout (init|set|disable) warns with dirty status' '
368 git clone repo dirty &&
369 echo dirty >dirty/folder1/a &&
371 git -C dirty sparse-checkout init 2>err &&
372 test_i18ngrep "warning.*The following paths are not up to date" err &&
374 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
375 test_i18ngrep "warning.*The following paths are not up to date" err &&
376 test_path_is_file dirty/folder1/a &&
378 git -C dirty sparse-checkout disable 2>err &&
379 test_must_be_empty err &&
381 git -C dirty reset --hard &&
382 git -C dirty sparse-checkout init &&
383 git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
384 test_path_is_missing dirty/folder1/a &&
385 git -C dirty sparse-checkout disable &&
386 test_path_is_file dirty/folder1/a
389 test_expect_success
'sparse-checkout (init|set|disable) warns with unmerged status' '
390 git clone repo unmerged &&
393 0 $ZERO_OID folder1/a
394 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
396 git -C unmerged update-index --index-info <input &&
398 git -C unmerged sparse-checkout init 2>err &&
399 test_i18ngrep "warning.*The following paths are unmerged" err &&
401 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
402 test_i18ngrep "warning.*The following paths are unmerged" err &&
403 test_path_is_file dirty/folder1/a &&
405 git -C unmerged sparse-checkout disable 2>err &&
406 test_i18ngrep "warning.*The following paths are unmerged" err &&
408 git -C unmerged reset --hard &&
409 git -C unmerged sparse-checkout init &&
410 git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
411 git -C unmerged sparse-checkout disable
414 test_expect_failure
'sparse-checkout reapply' '
415 git clone repo tweak &&
417 echo dirty >tweak/deep/deeper2/a &&
420 0 $ZERO_OID folder1/a
421 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
423 git -C tweak update-index --index-info <input &&
425 git -C tweak sparse-checkout init --cone 2>err &&
426 test_i18ngrep "warning.*The following paths are not up to date" err &&
427 test_i18ngrep "warning.*The following paths are unmerged" err &&
429 git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
430 test_i18ngrep "warning.*The following paths are not up to date" err &&
431 test_i18ngrep "warning.*The following paths are unmerged" err &&
433 git -C tweak sparse-checkout reapply 2>err &&
434 test_i18ngrep "warning.*The following paths are not up to date" err &&
435 test_path_is_file tweak/deep/deeper2/a &&
436 test_i18ngrep "warning.*The following paths are unmerged" err &&
437 test_path_is_file tweak/folder1/a &&
439 git -C tweak checkout HEAD deep/deeper2/a &&
440 git -C tweak sparse-checkout reapply 2>err &&
441 test_i18ngrep ! "warning.*The following paths are not up to date" err &&
442 test_path_is_missing tweak/deep/deeper2/a &&
443 test_i18ngrep "warning.*The following paths are unmerged" err &&
444 test_path_is_file tweak/folder1/a &&
446 # NEEDSWORK: We are asking to update a file outside of the
447 # sparse-checkout cone, but this is no longer allowed.
448 git -C tweak add folder1/a &&
449 git -C tweak sparse-checkout reapply 2>err &&
450 test_must_be_empty err &&
451 test_path_is_missing tweak/deep/deeper2/a &&
452 test_path_is_missing tweak/folder1/a &&
454 git -C tweak sparse-checkout disable
457 test_expect_success
'cone mode: set with core.ignoreCase=true' '
458 rm repo/.git/info/sparse-checkout &&
459 git -C repo sparse-checkout init --cone &&
460 git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
461 cat >expect <<-\EOF &&
466 test_cmp expect repo/.git/info/sparse-checkout &&
467 check_files repo a folder1
470 test_expect_success
'interaction with submodules' '
471 git clone repo super &&
475 git submodule add ../repo modules/child &&
477 git commit -m "add submodule" &&
478 git sparse-checkout init --cone &&
479 git sparse-checkout set folder1
481 check_files super a folder1 modules &&
482 check_files super/modules/child a deep folder1 folder2
485 test_expect_success
'different sparse-checkouts with worktrees' '
486 git -C repo worktree add --detach ../worktree &&
487 check_files worktree "a deep folder1 folder2" &&
488 git -C worktree sparse-checkout init --cone &&
489 git -C repo sparse-checkout set folder1 &&
490 git -C worktree sparse-checkout set deep/deeper1 &&
491 check_files repo a folder1 &&
492 check_files worktree a deep
495 test_expect_success
'set using filename keeps file on-disk' '
496 git -C repo sparse-checkout set a deep &&
497 cat >expect <<-\EOF &&
503 test_cmp expect repo/.git/info/sparse-checkout &&
504 check_files repo a deep
507 check_read_tree_errors
() {
511 git
-C $REPO -c core.sparseCheckoutCone
=false read-tree
-mu HEAD
2>err
&&
512 test_must_be_empty err
&&
513 check_files
$REPO "$FILES" &&
514 git
-C $REPO read-tree
-mu HEAD
2>err
&&
517 test_must_be_empty err
519 test_i18ngrep
"$ERRORS" err
521 check_files
$REPO $FILES
524 test_expect_success
'pattern-checks: /A/**' '
525 cat >repo/.git/info/sparse-checkout <<-\EOF &&
530 check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
533 test_expect_success
'pattern-checks: /A/**/B/' '
534 cat >repo/.git/info/sparse-checkout <<-\EOF &&
539 check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
540 check_files repo/deep "deeper1" &&
541 check_files repo/deep/deeper1 "deepest"
544 test_expect_success
'pattern-checks: too short' '
545 cat >repo/.git/info/sparse-checkout <<-\EOF &&
550 check_read_tree_errors repo "a" "disabling cone pattern matching"
552 test_expect_success
'pattern-checks: not too short' '
553 cat >repo/.git/info/sparse-checkout <<-\EOF &&
558 git -C repo read-tree -mu HEAD 2>err &&
559 test_must_be_empty err &&
563 test_expect_success
'pattern-checks: trailing "*"' '
564 cat >repo/.git/info/sparse-checkout <<-\EOF &&
569 check_read_tree_errors repo "a" "disabling cone pattern matching"
572 test_expect_success
'pattern-checks: starting "*"' '
573 cat >repo/.git/info/sparse-checkout <<-\EOF &&
578 check_read_tree_errors repo "a deep" "disabling cone pattern matching"
581 test_expect_success
'pattern-checks: contained glob characters' '
582 for c in "[a]" "\\" "?" "*"
584 cat >repo/.git/info/sparse-checkout <<-EOF &&
589 check_read_tree_errors repo "a" "disabling cone pattern matching"
593 test_expect_success BSLASHPSPEC
'pattern-checks: escaped characters' '
594 git clone repo escaped &&
595 TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
596 NEWTREE=$(git -C escaped mktree <<-EOF
597 $(git -C escaped ls-tree HEAD)
598 040000 tree $TREEOID zbad\\dir
599 040000 tree $TREEOID zdoes*exist
600 040000 tree $TREEOID zglob[!a]?
603 COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
604 git -C escaped reset --hard $COMMIT &&
605 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
606 git -C escaped sparse-checkout init --cone &&
607 git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
608 cat >expect <<-\EOF &&
618 test_cmp expect escaped/.git/info/sparse-checkout &&
619 check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
620 git -C escaped ls-tree -d --name-only HEAD >list-expect &&
621 git -C escaped sparse-checkout set --stdin <list-expect &&
622 cat >expect <<-\EOF &&
632 test_cmp expect escaped/.git/info/sparse-checkout &&
633 check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
634 git -C escaped sparse-checkout list >list-actual &&
635 test_cmp list-expect list-actual
638 test_expect_success MINGW
'cone mode replaces backslashes with slashes' '
639 git -C repo sparse-checkout set deep\\deeper1 &&
640 cat >expect <<-\EOF &&
647 test_cmp expect repo/.git/info/sparse-checkout &&
648 check_files repo a deep &&
649 check_files repo/deep a deeper1
652 test_expect_success
'cone mode clears ignored subdirectories' '
653 rm repo/.git/info/sparse-checkout &&
655 git -C repo sparse-checkout init --cone &&
656 git -C repo sparse-checkout set deep/deeper1 &&
658 cat >repo/.gitignore <<-\EOF &&
663 git -C repo add .gitignore &&
664 git -C repo commit -m ".gitignore" &&
666 mkdir -p repo/obj repo/folder1/obj repo/deep/deeper2/obj &&
667 for file in folder1/obj/a obj/a folder1/file.o folder1.o \
668 deep/deeper2/obj/a deep/deeper2/file.o file.o
670 echo ignored >repo/$file || return 1
673 git -C repo status --porcelain=v2 >out &&
674 test_must_be_empty out &&
676 git -C repo sparse-checkout reapply &&
677 test_path_is_missing repo/folder1 &&
678 test_path_is_missing repo/deep/deeper2 &&
679 test_path_is_dir repo/obj &&
680 test_path_is_file repo/file.o &&
682 git -C repo status --porcelain=v2 >out &&
683 test_must_be_empty out &&
685 git -C repo sparse-checkout set deep/deeper2 &&
686 test_path_is_missing repo/deep/deeper1 &&
687 test_path_is_dir repo/deep/deeper2 &&
688 test_path_is_dir repo/obj &&
689 test_path_is_file repo/file.o &&
691 >repo/deep/deeper2/ignored.o &&
692 >repo/deep/deeper2/untracked &&
694 # When an untracked file is in the way, all untracked files
695 # (even ignored files) are preserved.
696 git -C repo sparse-checkout set folder1 2>err &&
697 grep "contains untracked files" err &&
698 test_path_is_file repo/deep/deeper2/ignored.o &&
699 test_path_is_file repo/deep/deeper2/untracked &&
701 # The rest of the cone matches expectation
702 test_path_is_missing repo/deep/deeper1 &&
703 test_path_is_dir repo/obj &&
704 test_path_is_file repo/file.o &&
706 git -C repo status --porcelain=v2 >out &&
707 echo "? deep/deeper2/untracked" >expect &&