Skip to content

Hooks: parse git command lines and request sparse-index cone widening - #2192

Draft
Tyrie Vella (tyrielv) wants to merge 1 commit into
tyrielv/sparse-index/cone-builderfrom
tyrielv/sparse-index/hooks-cone-requests
Draft

Tyrie Vella (tyrielv) wants to merge 1 commit into
tyrielv/sparse-index/cone-builderfrom
tyrielv/sparse-index/hooks-cone-requests

Conversation

@tyrielv

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

Copy link
Copy Markdown
Contributor

Note

This PR is part of a stack of PRs into vnext. It builds on #2190, and its base branch is the branch of #2190, 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 44393183272 on a5fe19cc: 6 passed, 0 failed, 0 ignored.

Problem and Context

With a sparse index, Git keeps directories outside the sparse-checkout cone as single index entries. When a Git command names a path inside such a directory, Git expands the whole index in memory to find the path. On a repository with about 2.2 million index entries, this expansion costs seconds for each command.

The GVFS pre-command hook runs before every Git command, so it can prevent the expansion: it can find the paths that the command names, and ask the mount to widen the cone to cover them before Git runs. This PR adds the hook side of that protocol. #2196 adds the mount handler. Without it, the mount answers the new requests with UnknownRequest, and the hook lets Git run. Git is then slower, but correct.

The behavior is off by default, behind the gvfs.auto-sparse-index git config flag, which #2193 adds.

Why a table-driven parser and not System.CommandLine

GVFS.Hooks.exe starts for every Git command, so its startup time is latency for all of Git. System.CommandLine was measured against a table-driven parser in NativeAOT executables that parse one hook command line and exit:

Parser Warm startup (median, vs. no parser) Cold startup (median, vs. no parser) Executable size
Table-driven +0.1 ms +1 ms +52 KB
System.CommandLine 2.0.0-beta5 +1.5 ms +24 ms +1.5 MB

Both results reproduced on a second run. Also, Git's grammar does not fit a fixed command and option schema: a positional argument is a ref or a path by its position (git checkout <ref> -- <path>), and flags like --ours remove the ref operand. A System.CommandLine model would need the same custom logic around it.

Extraction does not have to be exact. If the parser misses a path, Git expands the index, which is the behavior today. If the parser finds too many paths, the cone is wider than necessary. Thus the table classifies ambiguous arguments conservatively, and it treats an unknown command as "only paths after --".

Hook startup cost

Warm startup of the NativeAOT GVFS.Hooks.exe, before and after this PR. The hook ran pre-command diff ... (or post-command diff ...) in a test enlistment with a real Git repository and no mount. Each row has 200 interleaved samples after 20 warmup rounds. Times are in milliseconds. The flag-on rows ran while the machine had other load, so their p95 values are wide.

Case Before: median (p05-p95) After: median (p05-p95)
Flag off, no sparse-checkout file, no paths 57.1 (53.8-62.2) 56.8 (53.7-60.9)
Flag off, no sparse-checkout file, names a path 57.0 (54.0-60.5) 57.3 (54.5-61.1)
Flag off, legacy non-cone sparse-checkout file, names a path 56.9 (54.4-65.4) 58.3 (55.4-102.3)
Flag off, cone-mode sparse-checkout file, names a path 56.9 (54.0-64.3) 89.8 (78.9-204.9)
Flag on, path in the cone (no request) 56.8 (53.7-70.9) 84.5 (76.9-240.5)
Flag on, path outside the cone, no mount 56.8 (53.7-70.9) 149.1 (132.9-268.4)
Flag on, post-command, no mount 56.8 (53.8-73.1) 149.1 (132.9-324.1)
Flag on, path outside the cone, mount answers UnknownRequest 57.1 (54.5-60.3) 88.4 (80.6-125.8)
  • The hook reads the sparse-checkout file before the flag. It reads the flag only when the file has the cone-mode header, because the flag read opens the repository through libgit2, which costs about 28 ms. No layer up to this PR writes a cone-mode file. The mount handler in Mount: widen and narrow the sparse-index cone #2196 writes it. Enlistments that went through an old disk-layout upgrade have a one-line non-cone file, which the hook ignores.
  • The round trip to a live mount pipe costs about 2 ms.
  • Without a mount, each hook pays the 50 ms connect timeout. Commands that take the GVFS lock cannot run without a mount. Commands that do not take the lock (for example git diff -- <path>) can.
  • The hook binary is 69,632 bytes larger.

Changes

  • Add GitPathspecParser and ParsedGitCommand in GVFS.Common. The parser extracts the pathspecs from the hook arguments. A per-verb table states which positional arguments are pathspecs, which options take a value, and which flags remove the leading ref operand (checkout --ours, --theirs, -p). The table covers add, stage, rm, mv, restore, commit, checkout, switch, reset, stash, diff, log, show, grep, and blame. The parser handles --, --end-of-options, --pathspec-from-file, --pathspec-file-nul, combined short options, value options in the --opt value and --opt=value forms, and the --git-pid and --exit_code arguments that Git appends.
  • Add ConePathspecResolver. It maps each pathspec to a repo-relative cone entry. A trailing slash makes a recursive folder entry. Any other pathspec makes a file entry, which puts only its parent directory in the cone. The resolver drops magic pathspecs, paths outside the worktree, wildcards in a directory segment, and any path that IsValidGitPath rejects (rooted, drive-relative, .., .git, Windows device names, trailing dots or spaces, colons, backslashes, and control characters).
  • Resolve relative pathspecs against the worktree root plus GIT_PREFIX. Git runs setup_git_directory() before the pre-command hook, so the hook's current directory is the top of the worktree, not the directory where the user ran Git. The resolver ignores a GIT_PREFIX that could leave the worktree.
  • Add ConeCoverage. It reads a cone-mode info/sparse-checkout file with the same rules as Git's reader: it skips comment lines, the header comes first, each parent-only marker follows its positive line, and no directory repeats under the repository's core.ignoreCase rule. It decodes the file as Git does (UTF-8, with an optional UTF-8 byte order mark). It uses ConePathMatcher to find the paths that the cone does not cover. A folder entry is covered only when its whole subtree is in the cone.
  • Add the ConeWiden and ConeNarrow named-pipe messages, their result codes, and the wait budgets: 50 ms to connect, 1000 ms to connect and send a widen, a 10-second ceiling for the widen reply, and 100 ms for a full narrow. A widen carries the Git process id and only the repo-relative paths outside the cone. It carries no absolute path and no file for the mount to read, and WidenParameters.TryParse rejects any other path, because any local user can write to the mount pipe. A narrow carries the Git process id, which both hooks of one Git command receive. The contract documents what the mount handler must do about missing narrows and concurrent commands.
  • Add ConeManagementHook in GVFS.Hooks, and call it at the end of the pre-command and post-command hooks. It does nothing unless the command names paths, the worktree has a cone-mode sparse-checkout file, and the flag is on. It does not send a widen when the cone covers every named path, or for git stash push <pathspec>, which still expanded the index after a widen in measurements and became slower. Every failure lets Git run: no mount, a non-success reply, no reply within the budget, and an exception. With GIT_TRACE=1, the hook writes a one-line diagnostic for each skipped or failed request.
  • After the hook sent a widen, it waits for the reply up to the 10-second ceiling. The mount applies a widen with git sparse-checkout reapply, which holds index.lock. If the hook let the user's Git run during that time, Git could fail with index.lock: File exists. Measured on a large repository, a widen takes about 1.3 seconds at most, including the wait for earlier requests. If the hook cannot connect and send within 1000 ms, it does not send the widen, so no mount Git process can start after the user's Git. A narrow only writes the cone file and does not run Git, so its 100 ms budget is unchanged.
  • Add unit tests for the parser table (verbs, option forms, --, --end-of-options, pathspec files, and the injected arguments), the pathspec resolver and the GIT_PREFIX base directory, the cone-file reader (Git's ordering rules, byte order marks, and the case rule) and coverage, and the message round trips and validation.
  • Add unit tests for ConeManagementHook with the existing MockNamedPipeClient, which now records the requests that it receives and can block in Connect. The tests check that the hook sends nothing when the flag is off, when the worktree has no cone-mode file, and when the cone covers every path. The tests check that the hook sends only the out-of-cone paths, and that it lets Git run when the mount answers UnknownRequest, does not answer within the budget, is absent, or throws. Other tests check that the hook waits for a widen reply that arrives after the send budget, that it lets Git run at the reply ceiling, and that it never sends a widen after the send budget ended.
  • Add a functional test (GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests) that turns on the flag, writes a cone-mode sparse-checkout file, and runs git add from two subdirectories. The test checks that the hook skips the in-cone path, sends a widen for the out-of-cone path, and that Git stages both files although the mount does not handle the request.

Functional-test fixtures in this layer

  • GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests (new). It does not need the git changes, so CI runs it too. Run it with --test=GVFS.FunctionalTests.Tests.EnlistmentPerFixture.ConeManagementHookTests.

Known gaps and deliberate decisions

  • Do not turn on the flag before the mount handler in Mount: widen and narrow the sparse-index cone #2196 ships. The current mount logs an error event for every request that it does not know. With the flag on, every path-naming command sends a narrow, and an out-of-cone command also sends a widen.
  • Pathspec files are not widened. The hook does not read a --pathspec-from-file file, so Git expands the index for those commands.
  • Conflict paths from a rebase or merge. These paths come from the commits, not from the command line, so the hook cannot widen for them in advance. Git expands the index once. The hook widens the cone when the user later names the conflicted path (for example with git add or git checkout --theirs).
  • git stash is skipped. In measurements on a repository with about 2.2 million index entries, git stash push -- <path> still expanded the index after a widen and became slower.
  • Flag-on latency. When a cone-mode file exists, each hook reads the flag through libgit2 (about 28 ms). A mount that accepts a widen but does not reply can add up to 11 seconds before Git runs (1000 ms to send and the 10-second reply ceiling). A stalled narrow can add 100 ms after Git runs.

When the sparse index is enabled, a git command that names a path
outside the sparse-checkout cone makes git expand the whole index in
memory. The pre-command hook can prevent this: it can ask the mount to
widen the cone before git runs. This change adds the hook side of that
protocol. A mount that does not handle the requests replies
UnknownRequest, and the hook treats every reply other than success as
"not applied" and lets git run.

Add GitPathspecParser, a table-driven parser that extracts the
pathspecs from the hook argv. Each git verb in the table states which
positional arguments are pathspecs, which options take a value, and
which flags remove the leading ref operand. The parser handles "--",
--end-of-options, --pathspec-from-file, --pathspec-file-nul, combined
short options, and the --git-pid and --exit_code arguments that git
appends.

Add ConePathspecResolver, which maps pathspecs to repo-relative cone
entries and drops any path that could leave the worktree. Git changes
to the top of the worktree before it runs the pre-command hook, so
relative pathspecs resolve against the worktree root plus GIT_PREFIX,
not against the current directory of the hook.

Add ConeCoverage, which reads a cone-mode sparse-checkout file with the
same rules as git's reader into a ConePatternSet, and uses
ConePathMatcher to find the paths that the cone does not cover.

Add the ConeWiden and ConeNarrow pipe messages and their wait budgets.
A widen carries only the git process id and repo-relative paths, and
the parser rejects any other path, because any local user can write to
the mount pipe.

Add ConeManagementHook, which the pre-command and post-command hooks
call. It does nothing unless the command names paths, the worktree has
a cone-mode sparse-checkout file, and gvfs.auto-sparse-index is true.
It sends only the paths outside the cone, and it skips git stash, which
still expanded the index after a widen in measurements. Every failure
lets git run.

After the hook sent a widen, it waits for the reply up to a 10 second
ceiling, not only for the 1 second send budget. The mount applies a
widen with a git process that holds index.lock. If the hook let the
user's git run first, that git could fail with "index.lock: File
exists". A narrow does not run git, so its 100 ms budget stays.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>
@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/hooks-cone-requests branch from 55398d0 to a5fe19c Compare November 18, 2026 20: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