Skip to content

Change only the index in update_sparsity() under a virtual file system - #1028

Draft
Tyrie Vella (tyrielv) wants to merge 4 commits into
microsoft:vfs-2.56.1from
tyrielv:tyrielv/sparse-index/vfs-sparsity-index-only
Draft

Tyrie Vella (tyrielv) wants to merge 4 commits into
microsoft:vfs-2.56.1from
tyrielv:tyrielv/sparse-index/vfs-sparsity-index-only

Conversation

@tyrielv

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

Copy link
Copy Markdown

Important

This PR builds on #1025, which includes #1027. It must merge after #1027 and #1025. Review only the last commit, "unpack-trees: change only the index in update_sparsity() under a VFS". The other commits belong to those PRs.

Problem and Context

#1025 lets a repository with core.virtualFilesystem use a cone-mode sparse index when it sets index.sparse to true. A virtual file system then widens the cone with git sparse-checkout reapply.

update_sparsity() expands each sparse directory that is now in the cone. The new entries start with the skip-worktree bit set. Under a virtual file system, mark_new_skip_worktree() uses the hook, so it clears the bit for each path that the hook reports as modified. apply_sparse_checkout() sees that the bit went from set to clear, and check_updates() checks the file out. This causes these problems:

  • If the user deleted a tracked file in that directory, Git writes the file again. The deletion is lost.
  • If the user modified a file, Git does not overwrite it, but it prints a warning that the path "was already present and thus not updated".
  • An entry whose bit becomes set gets CE_WT_REMOVE, unless core.gvfs prevents it.

A virtual file system owns the working tree, and its hook decides the skip-worktree bits. A change of sparsity must not check out or remove files there.

Changes

  • Under core.virtualFilesystem, when vfs_sparse_index_configured() is true (cone mode and index.sparse set to true), update_sparsity() changes only the index. It copies CE_NEW_SKIP_WORKTREE to CE_SKIP_WORKTREE. It does not run the up-to-date and absent checks, does not set CE_UPDATE or CE_WT_REMOVE, and does not call check_updates(). This applies to every caller of update_sparsity(): git sparse-checkout set, add, reapply, init, and disable.

  • Move the code that copies the bit out of apply_sparse_checkout() into the new helper update_skip_worktree_bit(), so that both paths share it.

  • Without a virtual file system, nothing changes. A virtual file system repository without the opt-in also behaves as before. When its index has no sparse directories, apply_virtualfilesystem() already clears the bit of each reported path when Git reads the index, so update_sparsity() has no working-tree change to make.

  • Update the core.virtualFilesystem documentation. In this mode, set, add, reapply, and disable change only the index. git sparse-checkout clean still removes directories outside of the cone.

  • Add t1097 and register it in t/meson.build. Its tests check that:

    • a widen with reapply keeps a deleted file deleted, with its index entry present and git status reporting the deletion;
    • a widen keeps the content of a modified file and prints no warning;
    • a widen of two sparse directories, with a deleted file and a modified file, does not change the working tree (the test compares a snapshot of the files and directories before and after);
    • git sparse-checkout add and disable keep deleted files deleted;
    • a widen without a virtual file system still checks out the files.

    The first four tests fail without this change.

index.deleteSparseDirectories

A virtual file system can keep passing -c index.deleteSparseDirectories=false to reapply for older versions of Git. This change does not need it. The setting controls only clean_tracked_sparse_directories(), and #1025 already makes that function return early under a virtual file system.

Known gaps and deliberate decisions

  • Commands that leave the opt-in can still restore a deleted file. These commands are not index-only: git sparse-checkout reapply --no-sparse-index, git sparse-checkout set --no-sparse-index, git sparse-checkout init --no-sparse-index, git sparse-checkout reapply --no-cone, git sparse-checkout set --no-cone, and a widen (set, add, or reapply) after index.sparse is unset. Each one reads a collapsed index while the opt-in is on. Then the opt-in is off when update_sparsity() runs. The expanded entries keep skip-worktree bits that the hook did not process, and the old code path checks out a reported deleted file. The gate stays on the explicit opt-in, so that a virtual file system repository without the opt-in keeps its current behavior. A virtual file system must not run these commands while the index has sparse directories. To leave the opt-in safely, first set a cone that includes all directories and run reapply with the opt-in on, then turn the opt-in off.
  • git sparse-checkout clean -f still removes directories. git sparse-checkout clean -f (--force) is an explicit request to delete out-of-cone directories, and it does not use update_sparsity(). This change does not make it index-only.
  • checkout, reset, and merge are not covered. These commands use unpack_trees(), not update_sparsity(). When they expand a sparse directory under a virtual file system, they can also check out a path that the hook reports.
  • The gate checks only the configuration. vfs_sparse_index_configured() does not check for a split index, unlike vfs_uses_cone_patterns(). When the sparse index is not in use, do_read_index() keeps the index full and the hook sets every bit, so the index-only path makes the same index change as the old path.

expand_index() builds a new cache array that reuses the cache entries of
the sparse index. It then discards the name-hash of the index and leaves
it uninitialized. The reused entries keep the CE_HASHED flag, so the next
lazy_init_name_hash() skips them: hash_index_entry() returns early for an
entry that has CE_HASHED. After the expansion, index_file_exists() and
index_dir_find() cannot find these entries, and the dir_hash does not
contain their directories.

This happens when something initializes the name-hash before the index
expands. With core.ignoreCase, the untracked-file walk of "git status"
does this. For example, take a cone that contains "zin/", with
sparse.expectFilesOutsideOfPatterns set and an untracked file in the
out-of-cone directory "out/". The walk initializes the name-hash, then
the lookup of "out/.gitignore" expands the index. Then the walk cannot
find "zin/" in the dir_hash and reports this tracked directory as
untracked:

	?? out/new
	?? zin/

Clear CE_HASHED on each entry that expand_index() reuses, so that the
next lazy_init_name_hash() adds it to the new name-hash.

The test for the "git status" case also exposes a leak in the same
code. expand_index() discards the dir_hash with hashmap_clear(), which
frees the table but not the dir_entry structs, so they leak whenever
the name-hash was initialized with core.ignoreCase before the
expansion. Use free_name_hash(), which also frees the dir_entry
structs. Without this, the new test fails under the leak checker.

Add a "--expand-and-find=<path>" option to "test-tool read-cache". It
looks up a path in the name-hash, expands the index to a full index, and
looks up the path again. Add a t1092 test that uses it, and a t1092 test
for the "git status" case above. The order in which readdir() returns
the directories can hide the "git status" case on some platforms, so the
test-tool test is the deterministic check.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>
apply_virtualfilesystem() runs on every index read. It sets
CE_SKIP_WORKTREE on every cache entry, then clears the bit for each
path that the virtual file system hook reports. To find these paths,
clear_ce_flags_virtualfilesystem_1() uses index_name_pos(), or the
name-hash helpers when core.ignoreCase is set.

In a sparse index, a reported path can be inside a sparse-directory
entry. Such a path has no cache entry of its own, and index_name_pos()
then expands the whole index to a full index. Because this happens on
every index read, a virtual file system repository can never keep a
sparse index. A directory wildcard (for example "dir/") has a second
problem: the loop that clears the bit on all entries below the
directory also clears it on a nested sparse-directory entry.

Use index_name_pos_sparse() instead. It does not expand the index, so a
path inside a sparse-directory entry is not found, and the
sparse-directory entry keeps its CE_SKIP_WORKTREE bit. This is correct:
the contents of the sparse directory are outside of the sparse-checkout
cone. With a full index, index_name_pos_sparse() and index_name_pos()
are the same, so a full index behaves as before. The core.ignoreCase
lookups do not change: the name-hash does not contain sparse-directory
entries, so they do not expand the index. In the directory wildcard
loop, skip sparse-directory entries.

apply_virtualfilesystem() runs only when core.virtualFilesystem is set,
so a repository without a virtual file system behaves as before.

A virtual file system that uses a sparse index must keep modified
paths inside the cone. Git does not look at the working tree for a
path inside a sparse-directory entry.

Add t1095. The tests use the trace2 "ensure_full_index" and
"expand_index" regions and the "expansion-reason" data to assert that
the index does not expand. They check the skip-worktree bits with
"git ls-files -t --sparse" for reported paths inside sparse
directories, for a directory wildcard that contains a nested sparse
directory, and for case-insensitive matches of in-cone paths. They
also check that "git status" reports an in-cone change, and that a
full index keeps the original behavior.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>
…system

With core.virtualFilesystem, path_in_sparse_checkout_1() reports every
path as inside the sparse-checkout. convert_to_sparse() collapses a
directory only when path_in_sparse_checkout() reports it as outside, so
an out-of-cone directory never collapses into a sparse-directory entry,
and a virtual file system repository cannot have a sparse index.

When the repository uses cone-mode sparse-checkout and sets index.sparse
to true in its configuration, let the cone patterns decide. The new
helper vfs_sparse_index_configured() checks the configuration, and
vfs_uses_cone_patterns() also checks the pattern file. The default value
of index.sparse is true, but it does not count here: a virtual file
system must turn this on explicitly, because a virtual file system that
cannot read a sparse index must never get one. These helpers run for
each path, so they do not use is_sparse_index_allowed(istate, 0), which
reads the environment and can write index.sparse when
GIT_TEST_SPARSE_INDEX is set.

 - path_in_sparse_checkout_1() no longer returns early under a virtual
   file system when vfs_uses_cone_patterns() is true.

 - path_matches_pattern_list() skips its virtual file system block for
   a cone-mode pattern list when vfs_sparse_index_configured() is true.
   The list is already in cone mode, so this checks only the
   configuration and does not load patterns into the temporary index
   of expand_index(). For a directory that the virtual file
   system does not report, that block returns 1. For the cone patterns,
   1 means "in the cone", so such a directory would still never
   collapse. Pattern lists that do not use cone mode keep the virtual
   file system block. The path-walk API, for example in "git backfill
   --sparse", also uses a cone-mode pattern list. Under a virtual file
   system, it now also matches only the patterns, as it does without a
   virtual file system.
   This change also fixes "git sparse-checkout reapply" on a collapsed
   index, which died with "is_excluded_from_virtualfilesystem passed
   unhandled dtype" when expand_index() passed a sparse directory with
   DT_UNKNOWN to that block.

 - do_read_index() no longer collapses a full index under a virtual
   file system. It calls ensure_correct_sparsity() before
   post_read_index_from() calls apply_virtualfilesystem(), so
   convert_to_sparse() would use the skip-worktree bits that are stored
   in the index file. Under a virtual file system these bits are stale:
   the hook can now report a path that had the bit when the index was
   written. Then the directory collapses, and apply_virtualfilesystem()
   cannot clear the bit of a path inside a sparse directory. The index
   collapses when it is written, after the bits are correct.

 - clean_tracked_sparse_directories() returns early under a virtual
   file system. After "git sparse-checkout set", "add", or "reapply",
   it deletes each out-of-cone directory that has no untracked files.
   A virtual file system shows these directories in the working tree,
   and the virtual file system owns them. Before this change, nothing
   collapsed under a virtual file system, so this function deleted
   nothing.

All new behavior requires core.virtualFilesystem. The cone patterns
are used only when the repository also uses cone mode and sets
index.sparse to true. Without a virtual file system, nothing changes. A
virtual file system repository without these settings also behaves as
before. Update the description of core.virtualFilesystem to match.

Add t1096. The out-of-cone files are on disk, as a virtual file system
shows them. The tests check that nothing collapses with index.sparse
false or with the default value of index.sparse, and that the
out-of-cone directory collapses with index.sparse true. They check that
"git status" reports an in-cone change without an expansion, and that
"git sparse-checkout add" and "reapply" keep all files on disk. They
check that a directory with a reported path does not collapse, that
non-cone mode under a virtual file system does not change, and that
cone mode without a virtual file system does not change.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>
Under core.virtualFilesystem, with cone-mode sparse-checkout and
index.sparse set to true, the index can contain sparse-directory
entries. A virtual file system widens the cone with "git
sparse-checkout reapply". Then update_sparsity() expands each sparse
directory that is now in the cone. The new entries start with the
skip-worktree bit set. mark_new_skip_worktree() uses the virtual file
system hook, so it clears the bit for each path that the hook reports
as modified. apply_sparse_checkout() then sees that the bit went from
set to clear, and sets CE_UPDATE, and check_updates() checks the file
out.

If the user deleted a tracked file in that directory, Git writes the
file again, and the deletion is lost. If the user modified a file,
verify_absent_sparse() refuses to overwrite it and prints a warning
that the path "was already present and thus not updated". In the other
direction, an entry whose bit becomes set gets CE_WT_REMOVE unless
core.gvfs prevents it.

A virtual file system owns the working tree, and its hook decides the
skip-worktree bits. A change of sparsity must not check out or remove
files there. When vfs_sparse_index_configured() is true (cone mode and
index.sparse set to true), let update_sparsity() copy
CE_NEW_SKIP_WORKTREE to CE_SKIP_WORKTREE and do nothing else: do not
call verify_uptodate_sparse() or verify_absent_sparse(), do not set
CE_UPDATE or CE_WT_REMOVE, and do not call check_updates(). Move the
code that copies the bit into the new helper update_skip_worktree_bit(),
so that apply_sparse_checkout() and this path share it.

This uses the same explicit opt-in as the other sparse-index changes
under a virtual file system. Without a virtual file system, nothing
changes. A virtual file system repository without the opt-in also
behaves as before. When its index has no sparse directories,
apply_virtualfilesystem() already clears the bit of each reported path
when Git reads the index, so update_sparsity() has no working-tree
change to make. A command that leaves the opt-in while the index still
has sparse directories, for example "git sparse-checkout reapply
--no-sparse-index" or "--no-cone", still uses the old code path.

The index.deleteSparseDirectories=false setting is not necessary for
this change. It controls only clean_tracked_sparse_directories(),
which already returns early under a virtual file system.

Add t1097. The tests check that a widen keeps a deleted file deleted,
with its index entry present and "git status" reporting the deletion.
They check that a widen keeps the content of a modified file without
a warning, and that a widen of two sparse directories with a deleted
and a modified file does not change the working tree. They check that
"git sparse-checkout add" and "disable" keep deleted files deleted. A
last test checks that a widen without a virtual file system still
checks out the files.

Update the description of core.virtualFilesystem. It names the
subcommands that change only the index in this mode, and it states
that "git sparse-checkout clean" still removes directories outside of
the cone.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>

This branch has not been deployed

No deployments
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