Skip to content

Add sparse-checkout cone builder, writer, and path matcher - #2190

Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
tyrielv/sparse-index/projection-expand-sparse-dirsfrom
tyrielv/sparse-index/cone-builder
Draft

Tyrie Vella (tyrielv) wants to merge 1 commit into
tyrielv/sparse-index/projection-expand-sparse-dirsfrom
tyrielv/sparse-index/cone-builder

Conversation

@tyrielv

@tyrielv Tyrie Vella (tyrielv) commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Note

This PR is part of a stack of PRs into vnext. It builds on #2193, and its base branch is the branch of #2193, so the diff shows only this layer.

Important

Test with the git changes. This stack needs the git-side changes in microsoft/git#1027, #1025, #1028, #1024, and #1026. VFS for Git CI uses a released git without them, so CI skips the AutoSparseIndex functional tests (NUnit reports them as ignored). To run the tests locally, install the unsigned preview git from sparse-index-preview-1. Its release notes give the install, test, and removal steps.

Latest preview validation: run 44388006189 on 64071d42: 4 passed, 0 failed, 0 ignored.

Problem and Context

A sparse index keeps one index entry for each directory that is fully outside the sparse-checkout cone. GVFS can use a sparse index only if it keeps a cone-mode info/sparse-checkout file that matches the user's work. This cone must include each modified path, because git does not show a file that is in a collapsed directory.

This PR adds the library that computes and writes that cone. Nothing calls it yet. A later change uses it from the mount process. Thus this PR does not change behavior. The unit tests and one local component functional test are the evidence that it is correct.

Why GVFS does not use git sparse-checkout set

In cone mode, git sparse-checkout set sends each input directory through strbuf_to_cone_pattern to insert_recursive_pattern (builtin/sparse-checkout.c). Thus every input becomes a recursive pattern. To include one modified file, set must include its whole parent directory recursively, with every file below it. If that directory is large, one edit adds all of its files back to the index. set also updates the working tree, which a virtualized enlistment must not do.

Git's cone format also has "parent" patterns: a directory's direct files are in the cone, but its child directories are not. set cannot make these patterns from a file path. So GVFS writes the file itself, in the same format that git's write_cone_to_file writes.

Case sensitivity

GVFS stores modified paths case-insensitively on Windows, so the list can contain both Src/a.cs and src/b.cs. When core.ignoreCase is true, git compares cone directories with ASCII-only case folding (fspathhash and fspathncmp). If the cone file names both Src and src, git prints pattern '/src' is repeated and turns off cone matching. Then the index cannot stay sparse. The cone must use the same case rule as the repository.

Git folds only ASCII letters. Ä and ä are different directories to git, even when core.ignoreCase is true. StringComparer.OrdinalIgnoreCase folds both, so it is not correct for cone paths. If GVFS merged Ä and ä, the files of one directory would be outside the cone.

Directories that a cone file cannot contain

Git reads the file line by line, and it reads some escaped names differently. A directory name with a line break would add extra pattern lines. For example, a directory named z\n!src\nb adds a !src line, and git turns off cone matching and leaves src/ out. Git also reads /A/\*/ back as /A. A cone with such a directory would leave modified files out of the cone, and git would not show them. So the library rejects these directories and does not write the file.

Changes

All new types are in GVFS.Common.Sparse.

  • ConeBuilder.BuildFromModifiedPaths(paths, ignoreCase) maps modified paths to a ConePatternSet.
    • A modified file adds parent-only patterns for its directory and each ancestor. It never adds a recursive pattern.
    • A modified folder (trailing /) adds one recursive pattern, and parent-only patterns for its ancestors.
    • The builder removes patterns that a recursive pattern already covers.
    • The builder accepts either separator, a leading separator, and duplicate separators.
    • When ignoreCase is true, directories that differ only in ASCII case become one directory. Each directory keeps its ordinally smallest spelling, so the output does not depend on input order.
    • The output is sorted ordinally, as git sorts with strcmp.
    • If a directory cannot be in a cone file, the builder throws ArgumentException. It checks each input directory before it removes covered patterns, so an invalid directory below a recursive directory also fails.
  • ConePatternSet is the in-memory cone: a list of parent-only directories, a list of recursive directories, and the case rule (IgnoreCase).
    • The constructor copies the lists. It rejects a directory that is in the lists twice under the case rule.
    • It also rejects a directory that git cannot read back: an empty, ., .., or * segment, a line break, NUL, a backslash, or an unpaired surrogate. IsValidDirectory makes the same check.
  • ConeFileWriter writes the cone file.
    • Serialize writes git's cone format: the /* and !/*/ header, a /D/ and !/D/*/ pair for each parent-only directory, and /D/ for each recursive directory. It escapes *, ?, [, and \ as git's escaped_pattern does.
    • Write returns ConeFileWriteResult.Unchanged, Written, or Failed.
    • If the file already has the same bytes, Write writes nothing, not even a backup. It compares bytes, because git cannot read the same text in another encoding.
    • Otherwise it copies the old file byte for byte to sparse-checkout.gvfsbackup, then writes the new content as UTF-8 to a temporary file and renames it over the target. A failure leaves the target with its previous content.
    • Write reads the existing file instead of asking whether it exists. File.Exists returns false when access is denied, and the write must not then replace the file without a backup. Only a missing file or directory counts as "no file".
    • A git process that reads info/sparse-checkout keeps it open without delete sharing. While git reads the file, the rename over it fails with ERROR_ACCESS_DENIED or ERROR_SHARING_VIOLATION. So Write tries the replace again after these two errors, as git's mingw_rename does. The waits are 0, 1, 10, 20, and 40 ms, as in git's retry_ask_yes_no, then 40 ms again. The caller gives the total wait time as replaceRetryBudget, because a hook can wait less than a background task. The default is 100 ms. Zero makes one attempt only. Other errors fail at once.
    • TryRestoreBackup copies the backup back byte for byte for a rollback, with the same retry. The writer does not lock the file. The caller must keep other writers, including git, away between the write and a possible restore.
  • ConePathMatcher.IsInCone(path) decides whether git would treat a path as inside the cone. It uses the case rule of the ConePatternSet. It follows path_matches_pattern_list in dir.c. A path with a trailing / is a directory. Git checks a directory as if it were a file named - directly inside that directory.
  • SparseCheckoutPathResolver.GetSparseCheckoutFilePath has a different job: it finds the location of the info/sparse-checkout file. A linked worktree has its own file in its per-worktree git dir. GVFSEnlistment.DotGitRoot points to the shared git dir for a worktree, so it cannot be used for this file.
  • GitPathComparer (internal) compares paths with git's rule: exact, or ASCII-only case folding.
  • MockFileSystem.ReadAllText and ReadAllBytes now throw FileNotFoundException for a missing file, as the real file system does. Before, they threw NullReferenceException. MockFileSystem.MoveAndOverwriteFile now keeps the exact bytes, and MockFileSystem implements TryCopyToTempFileAndRename. Like the real method, it returns false for a Win32Exception.
  • Add unit tests for the builder: parent-only chains, recursive folders, removal of covered patterns, normalization, ordinal sort, glob escaping, case merging with and without core.ignoreCase, and paths with a line break or a .. segment.
  • Add unit tests for ConePatternSet validation and for GitPathComparer.
  • Add unit tests for the writer: serialization, escaping, the first write, the replacement of existing content with a backup, unchanged content, CRLF content, UTF-16 content, a read failure, an access failure, a backup failure, a rename failure, a directory creation failure, and a byte-exact backup and restore.
  • Add unit tests for the replace retry. A test file system fails the rename over the target a set number of times, and the tests record the waits through a sleep seam instead of sleeping. The tests check a retry after access denied and after a sharing violation in each exception form, the stop when the default budget is spent, the attempt count for caller budgets of 0, 10, 11, and 150 ms, no retry after another error, a retry in TryRestoreBackup, and the rejection of a negative budget.
  • Add unit tests for the matcher, and for the file location resolver with a primary enlistment and a linked worktree.
  • Add a local component functional test (GVFS.FunctionalTests.Tests.LocalComponents.ConePathMatcherGitTests) that compares ConePathMatcher with git sparse-checkout check-rules. The test builds a cone from paths that contain ASCII case variants, non-ASCII case variants, and glob characters. It writes the cone to a temporary repository and asks git about 252 file and directory paths, with core.ignoreCase on and off. The test asserts that git and the matcher agree on every path, that git puts every modified path in the cone, and that git prints no warning. The test uses the repository's own patterns, because check-rules --rules-file with --cone reads directory names, not raw patterns. It configures only the git child process environment, and it needs no enlistment. This follows the local component test convention that Move system-dependent unit coverage to functional components #2163 added on master. The unit tests in this PR start no external programs.

Functional tests in this layer

  • GVFS.FunctionalTests.Tests.LocalComponents.ConePathMatcherGitTests (added). It is not an AutoSparseIndex test. It needs only a git that has git sparse-checkout check-rules, so CI runs it, and it does not need the preview git.

Known gaps and deliberate decisions

  • No caller and no feature flag. The library is not reachable from any product code. The change that calls it from the mount process also adds the runtime gate.
  • The writer does not lock the file. The caller must keep other writers, including git commands that write info/sparse-checkout, away between Write and a possible TryRestoreBackup. The writer also does not delete old backups. No follow-up planned in this library.
  • The retry budget counts only the waits. The time of each attempt is not in the budget. The writer also retries UnauthorizedAccessException, because .NET reports ERROR_ACCESS_DENIED that way. So a file that is always read-only fails only after the full budget, which is 100 ms by default.
  • Some directory names fail the build. ConeBuilder throws ArgumentException for a directory that a cone file cannot contain. Windows file names cannot contain most of these characters, but NTFS allows an unpaired surrogate. The caller must treat this as "cannot use a sparse index", not as an empty cone.
  • Cleanup is quadratic in recursive directories. The removal of covered patterns compares each pattern with each recursive directory. Recursive directories come only from modified folder entries, which are few. No follow-up planned.

@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/cone-builder branch 3 times, most recently from 2ad6c76 to 5ed6d29 Compare November 16, 2026 22:06
@tyrielv
Tyrie Vella (tyrielv) changed the base branch from vnext to tyrielv/sparse-index/projection-expand-sparse-dirs November 16, 2026 22:06
@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/cone-builder branch from 5ed6d29 to 3054c61 Compare November 17, 2026 18:06
@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/cone-builder branch from 3054c61 to 3a02c18 Compare November 18, 2026 01:13
@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/cone-builder branch from 3a02c18 to cde072a Compare November 18, 2026 18:04
Add a library in GVFS.Common.Sparse that computes the sparse-checkout
cone that GVFS needs for a sparse index, and writes it to
info/sparse-checkout. Nothing calls the library yet. A later change
uses it from the mount process.

- ConeBuilder maps modified paths to cone patterns. A modified file
  adds parent-only patterns for its directory and each ancestor, so it
  does not pull a whole subtree into the index. A modified folder adds
  one recursive pattern. GVFS does not use "git sparse-checkout set",
  because "set" makes every input directory recursive.
- ConePatternSet holds the cone and its case rule. It rejects a
  directory that is in the cone twice under that rule, and a directory
  that git cannot read back from a cone file. When core.ignoreCase is
  true, git treats "Src" and "src" as a repeated pattern and turns off
  cone matching. So the builder merges directories that differ only in
  ASCII case when core.ignoreCase is true.
- ConeFileWriter writes the file in git's cone format through a
  temporary file and a rename. It keeps a backup for rollback. It
  writes nothing when the bytes do not change. It fails, and does not
  write, when it cannot read the existing file. A git process that
  reads the file keeps it open without delete sharing, so the rename
  can fail with ERROR_ACCESS_DENIED or ERROR_SHARING_VIOLATION. The
  writer then tries the replace again with git's mingw_rename delays,
  within a total wait that the caller sets (100 ms by default).
- ConePathMatcher decides whether a path is in the cone with git's
  cone rules, including git's ASCII-only case folding.
- SparseCheckoutPathResolver finds the per-worktree sparse-checkout
  file.

MockFileSystem now throws FileNotFoundException when a test reads a
missing file, as the real file system does. It also keeps the exact
bytes when it moves or copies a file.

Add unit tests for the builder, the pattern set, the comparer, the
writer success and failure paths, the replace retry, the matcher, and
the resolver. The unit tests start no external programs.

Add a local component functional test that compares the matcher with
"git sparse-checkout check-rules" with core.ignoreCase on and off.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>
@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/cone-builder branch from cde072a to 64071d4 Compare November 18, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant