sparse-index: prevent repo root from becoming sparse

Prevent the repository root from being collapsed into a sparse directory by treating an empty path as "inside the sparse-checkout". When collapsing a sparse index (e.g. in 'git sparse-checkout reapply'), the root directory typically could not become a sparse directory due to the presence of in-cone root-level files and directories. However, if no such in-cone files or directories were present, there was no explicit check signaling that the "repository root path" (an empty string, in the case of 'convert_to_sparse(...)') was in-cone, and a sparse directory index entry would be created from the repository root directory. The documentation in Documentation/git-sparse-checkout.txt explicitly states that the files in the root directory are expected to be in-cone for a cone-mode sparse-checkout. Collapsing the root into a sparse directory entry violates that assumption, as sparse directory entries are expected to be outside the sparse cone and have SKIP_WORKTREE enabled. This invalid state in turn causes issues with commands that interact with the index, e.g. 'git status'. Treating an empty (root) path as in-cone prevents the creation of a root sparse directory in 'convert_to_sparse(...)'. Because the repository root is otherwise never compared with sparse patterns (in both cone-mode and non-cone sparse-checkouts), the new check does not cause additional changes to how sparse patterns are applied. Helped-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Mar 1, 2022 at 20:24 UTC 287fd17e3a1daec2b0d0d8e26d2b2318511ec153
2 files changed +22 -3
dir.c
+4 -3
@@ -1463,10 +1463,11 @@ static int path_in_sparse_checkout_1(const char *path,
1463 const char *end, *slash;
1464
1465 /*
1466 - * We default to accepting a path if there are no patterns or
1467 - * they are of the wrong type.
1466 + * We default to accepting a path if the path is empty, there are no
1467 + * patterns, or the patterns are of the wrong type.
1468 */
1469 - if (init_sparse_checkout_patterns(istate) ||
1469 + if (!*path ||
1470 + init_sparse_checkout_patterns(istate) ||
1471 (require_cone_mode &&
1472 !istate->sparse_checkout_patterns->use_cone_patterns))
1473 return 1;
t/t1092-sparse-checkout-compatibility.sh
+18
@@ -244,6 +244,24 @@ test_expect_success 'expanded in-memory index matches full index' '
244 test_sparse_match git ls-files --stage
245 '
246
247 +test_expect_success 'root directory cannot be sparse' '
248 + init_repos &&
249 +
250 + # Remove all in-cone files and directories from the index, collapse index
251 + # with `git sparse-checkout reapply`
252 + git -C sparse-index rm -r . &&
253 + git -C sparse-index sparse-checkout reapply &&
254 +
255 + # Verify sparse directories still present, root directory is not sparse
256 + cat >expect <<-EOF &&
257 + folder1/
258 + folder2/
259 + x/
260 + EOF
261 + git -C sparse-index ls-files --sparse >actual &&
262 + test_cmp expect actual
263 +'
264 +
265 test_expect_success 'status with options' '
266 init_repos &&
267 test_sparse_match ls &&