From fe3f39741626ff507c08a937e4f711c5e8be77cd Mon Sep 17 00:00:00 2001 From: Tyrie Vella Date: Fri, 13 Nov 2026 17:17:42 -0800 Subject: [PATCH 1/6] sparse-index: let the name-hash find entries that expand_index() reuses 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=" 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 --- sparse-index.c | 12 +++++--- t/helper/test-read-cache.c | 32 ++++++++++++++++++++++ t/t1092-sparse-checkout-compatibility.sh | 35 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/sparse-index.c b/sparse-index.c index e21a91c47649c3..cbeb0971e5cff7 100644 --- a/sparse-index.c +++ b/sparse-index.c @@ -411,6 +411,13 @@ void expand_index(struct index_state *istate, struct pattern_list *pl) struct pathspec ps; int dtype = DT_UNKNOWN; + /* + * The name-hash of 'istate' is discarded below, but 'full' + * reuses this entry. Clear CE_HASHED so that the next + * lazy_init_name_hash() adds the entry to the new name-hash. + */ + ce->ce_flags &= ~CE_HASHED; + if (!S_ISSPARSEDIR(ce->ce_mode)) { set_index_entry(full, full->cache_nr++, ce); continue; @@ -448,10 +455,7 @@ void expand_index(struct index_state *istate, struct pattern_list *pl) } /* Copy back into original index. */ - if (istate->name_hash_initialized) { - hashmap_clear(&istate->name_hash); - hashmap_clear(&istate->dir_hash); - } + free_name_hash(istate); istate->name_hash_initialized = full->name_hash_initialized; memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash)); diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c index 6b08ba8f078d00..34fc1e8e2b4bbb 100644 --- a/t/helper/test-read-cache.c +++ b/t/helper/test-read-cache.c @@ -3,27 +3,59 @@ #include "test-tool.h" #include "config.h" #include "environment.h" +#include "name-hash.h" #include "read-cache-ll.h" #include "repository.h" #include "setup.h" +#include "sparse-index.h" + +/* + * Look up 'name' in the name-hash, expand the index to a full index, and + * look it up again. The first lookup initializes the name-hash. + */ +static void expand_and_find(struct index_state *istate, const char *name) +{ + int before = !!index_file_exists(istate, name, strlen(name), 0); + + ensure_full_index(istate); + printf("%s: %s, %s\n", name, + before ? "found" : "not found", + index_file_exists(istate, name, strlen(name), 0) ? + "found" : "not found"); +} int cmd__read_cache(int argc, const char **argv) { int i, cnt = 1; const char *name = NULL; + const char *find_name = NULL; if (argc > 1 && skip_prefix(argv[1], "--print-and-refresh=", &name)) { argc--; argv++; } + if (argc > 1 && + skip_prefix(argv[1], "--expand-and-find=", &find_name)) { + argc--; + argv++; + } + if (argc == 2) cnt = strtol(argv[1], NULL, 0); setup_git_directory(the_repository); repo_config(the_repository, git_default_config, NULL); + if (find_name) { + /* Read the index as it is on disk, without an expansion. */ + prepare_repo_settings(the_repository); + the_repository->settings.command_requires_full_index = 0; + } + for (i = 0; i < cnt; i++) { repo_read_index(the_repository); + if (find_name) + expand_and_find(the_repository->index, find_name); if (name) { int pos; diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh index a64cdcec91a2fe..d4cfbfb6817076 100755 --- a/t/t1092-sparse-checkout-compatibility.sh +++ b/t/t1092-sparse-checkout-compatibility.sh @@ -2863,4 +2863,39 @@ test_expect_success 'ensure_full_index_with_reason' ' "failed to clear skip-worktree while sparse" actual && + echo "deep/a: found, found" >expect && + test_cmp expect actual +' + +test_expect_success 'status with core.ignoreCase after the index expands' ' + git init icase && + ( + cd icase && + mkdir out zin && + for f in out/f zin/a zin/b + do + echo "$f" >$f || return 1 + done && + git add . && + git commit -m init && + git config core.ignoreCase true && + git config index.sparse true && + git config sparse.expectFilesOutsideOfPatterns true && + git sparse-checkout set zin && + + # If readdir() returns "out/" before "zin/" (as on NTFS), + # the lookup in "out/" expands the index before the walk + # looks up "zin/". The test above does not depend on this. + mkdir out && + echo new >out/new && + git status --porcelain >../icase-actual + ) && + echo "?? out/new" >expect && + test_cmp expect icase-actual +' + test_done From 828fa678e8664c41c2b3e66801ea5a21ea441ad7 Mon Sep 17 00:00:00 2001 From: Tyrie Vella Date: Fri, 13 Nov 2026 16:35:04 -0800 Subject: [PATCH 2/6] virtualfilesystem: do not expand a sparse index 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 --- t/meson.build | 1 + t/t1095-virtualfilesystem-sparse-index.sh | 173 ++++++++++++++++++++++ virtualfilesystem.c | 24 ++- 3 files changed, 196 insertions(+), 2 deletions(-) create mode 100755 t/t1095-virtualfilesystem-sparse-index.sh diff --git a/t/meson.build b/t/meson.build index 139269d6ebd95a..517d16b549c208 100644 --- a/t/meson.build +++ b/t/meson.build @@ -188,6 +188,7 @@ integration_tests = [ 't1091-sparse-checkout-builtin.sh', 't1092-sparse-checkout-compatibility.sh', 't1093-virtualfilesystem.sh', + 't1095-virtualfilesystem-sparse-index.sh', 't1100-commit-tree-options.sh', 't1300-config.sh', 't1301-shared-repo.sh', diff --git a/t/t1095-virtualfilesystem-sparse-index.sh b/t/t1095-virtualfilesystem-sparse-index.sh new file mode 100755 index 00000000000000..d95dcd806c5843 --- /dev/null +++ b/t/t1095-virtualfilesystem-sparse-index.sh @@ -0,0 +1,173 @@ +#!/bin/sh + +test_description='virtual file system does not expand a sparse index + +apply_virtualfilesystem() runs on every index read. It sets the +skip-worktree bit on every index entry, then clears it for the paths that +the virtual file system hook reports. With a sparse index, a reported path +inside a sparse-directory entry must not expand the index, and the +sparse-directory entry must keep its skip-worktree bit.' + +GIT_TEST_SPLIT_INDEX=0 +GIT_TEST_SPARSE_INDEX= + +. ./test-lib.sh + +# Fail if the trace in "$1" shows that the index was expanded. +test_no_index_expansion () { + test_region ! index ensure_full_index "$1" && + test_region ! index expand_index "$1" && + test_grep ! expansion-reason "$1" +} + +# The hook reports the paths in .git/vfs-paths, one per line. +test_expect_success 'setup' ' + git init repo && + ( + cd repo && + mkdir -p in/deeper in/other out/sub && + for f in r in/a in/deeper/a in/other/a out/f1 out/f2 out/sub/f + do + echo "$f" >$f || return 1 + done && + git add . && + git commit -m init && + git config index.sparse true && + git sparse-checkout set in/deeper && + + git ls-files --sparse >files && + test_grep "^out/$" files && + test_grep "^in/other/$" files && + test_grep "^in/a$" files && + + write_script .git/hooks/virtualfilesystem <<-\EOF && + tr "\n" "\0" <.git/vfs-paths + EOF + git config core.virtualfilesystem .git/hooks/virtualfilesystem + ) +' + +test_expect_success 'paths inside sparse directories do not expand the index' ' + cat >repo/.git/vfs-paths <<-\EOF && + r + in/a + in/deeper/a + in/other/a + out/f1 + out/sub/f + out/sub/ + EOF + rm -f trace.txt && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git -C repo -c core.ignorecase=false \ + ls-files -t --sparse >actual && + test_no_index_expansion trace.txt && + test_trace2_data vfs apply/nr_unknown 3 expect <<-\EOF && + H in/a + H in/deeper/a + S in/other/ + S out/ + H r + EOF + test_cmp expect actual +' + +test_expect_success 'directory wildcard keeps nested sparse directory' ' + cat >repo/.git/vfs-paths <<-\EOF && + in/ + EOF + rm -f trace.txt && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git -C repo ls-files -t --sparse >actual && + test_no_index_expansion trace.txt && + + cat >expect <<-\EOF && + H in/a + H in/deeper/a + S in/other/ + S out/ + S r + EOF + test_cmp expect actual +' + +test_expect_success 'case-insensitive lookups still match in-cone paths' ' + cat >repo/.git/vfs-paths <<-\EOF && + IN/A + OUT/f1 + In/Other/ + EOF + rm -f trace.txt && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git -C repo -c core.ignorecase=true \ + ls-files -t --sparse >actual && + test_no_index_expansion trace.txt && + + cat >expect <<-\EOF && + H in/a + S in/deeper/a + S in/other/ + S out/ + S r + EOF + test_cmp expect actual +' + +test_expect_success 'status reports in-cone changes without expanding' ' + test_when_finished "git -C repo checkout -- in/a" && + cat >repo/.git/vfs-paths <<-\EOF && + r + in/a + in/deeper/a + out/f1 + EOF + echo changed >>repo/in/a && + rm -f trace.txt && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git -C repo status --porcelain --untracked-files=no >actual && + test_no_index_expansion trace.txt && + echo " M in/a" >expect && + test_cmp expect actual && + + git -C repo ls-files --sparse >files && + test_grep "^out/$" files +' + +test_expect_success 'full index keeps the original behavior' ' + git clone repo full && + ( + cd full && + git sparse-checkout set in/deeper && + git config index.sparse false && + git sparse-checkout reapply && + cp ../repo/.git/hooks/virtualfilesystem .git/hooks/ && + cat >.git/vfs-paths <<-\EOF && + r + in/a + in/deeper/a + out/f1 + out/sub/ + EOF + git config core.virtualfilesystem \ + .git/hooks/virtualfilesystem && + + GIT_TRACE2_EVENT="$(pwd)/../full-trace.txt" \ + git ls-files -t >../full-actual + ) && + test_trace2_data vfs apply/nr_unknown 0 expect <<-\EOF && + H in/a + H in/deeper/a + S in/other/a + H out/f1 + S out/f2 + H out/sub/f + H r + EOF + test_cmp expect full-actual +' + +test_done diff --git a/virtualfilesystem.c b/virtualfilesystem.c index 2d385896227aa0..787a99b6b5cea4 100644 --- a/virtualfilesystem.c +++ b/virtualfilesystem.c @@ -9,6 +9,7 @@ #include "hashmap.h" #include "run-command.h" #include "name-hash.h" +#include "object.h" #include "read-cache-ll.h" #include "virtualfilesystem.h" @@ -269,6 +270,17 @@ static void clear_ce_flags_virtualfilesystem_1(struct index_state *istate, int s if (!virtual_filesystem_data.len) get_virtual_filesystem_data(istate->repo, &virtual_filesystem_data); + /* + * The lookups below use index_name_pos_sparse(), not + * index_name_pos(). In a sparse index, a path inside a + * sparse-directory entry has no entry of its own, and + * index_name_pos() expands the whole index to find it. Here, such + * a path is not found, so the sparse-directory entry keeps its + * CE_SKIP_WORKTREE bit. With a full index, the two functions are + * the same. The name-hash does not contain sparse-directory + * entries, so the core.ignoreCase lookups do not expand the index. + */ + /* clear specified flag bits for everything in the virtual file system */ entry = buf = virtual_filesystem_data.buf; for (i = 0; i < virtual_filesystem_data.len; i++) { @@ -285,13 +297,21 @@ static void clear_ce_flags_virtualfilesystem_1(struct index_state *istate, int s stats->nr_vfs_dirs++; if (repo_ignore_case(the_repository)) adjust_dirname_case(istate, entry); - pos = index_name_pos(istate, entry, len); + pos = index_name_pos_sparse(istate, entry, len); if (pos < 0) { for (pos = -pos - 1; (size_t)pos < istate->cache_nr; pos++) { ce = istate->cache[pos]; if (fspathncmp(ce->name, entry, len)) break; + /* + * Keep CE_SKIP_WORKTREE on + * a sparse directory below + * this directory. + */ + if (S_ISSPARSEDIR(ce->ce_mode)) + continue; + if (select_mask && !(ce->ce_flags & select_mask)) continue; @@ -304,7 +324,7 @@ static void clear_ce_flags_virtualfilesystem_1(struct index_state *istate, int s if (repo_ignore_case(the_repository)) { ce = index_file_exists(istate, entry, len, repo_ignore_case(the_repository)); } else { - int pos = index_name_pos(istate, entry, len); + int pos = index_name_pos_sparse(istate, entry, len); ce = NULL; if (pos >= 0) From 4c6b9ebc7697704efbb412db6bd221ed1198e2db Mon Sep 17 00:00:00 2001 From: Tyrie Vella Date: Fri, 13 Nov 2026 17:18:32 -0800 Subject: [PATCH 3/6] dir.c: consult cone patterns for sparse-checkout under a virtual filesystem 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 --- Documentation/config/core.adoc | 8 +- builtin/sparse-checkout.c | 9 + dir.c | 18 +- read-cache.c | 8 +- t/meson.build | 1 + t/t1096-virtualfilesystem-sparse-cone.sh | 224 +++++++++++++++++++++++ virtualfilesystem.c | 25 +++ virtualfilesystem.h | 17 ++ 8 files changed, 304 insertions(+), 6 deletions(-) create mode 100755 t/t1096-virtualfilesystem-sparse-cone.sh diff --git a/Documentation/config/core.adoc b/Documentation/config/core.adoc index b9219080296e65..18b29c9507675e 100644 --- a/Documentation/config/core.adoc +++ b/Documentation/config/core.adoc @@ -116,7 +116,13 @@ core.virtualFilesystem:: will identify all files and directories that are present in the working directory. Git will only track and update files listed in the virtual file system. Using the virtual file system - will supersede the sparse-checkout settings which will be ignored. + will supersede the sparse-checkout settings which will be ignored, + unless the repository uses cone-mode sparse-checkout and sets + `index.sparse` to `true` (the default value does not count). Then + the cone patterns decide which directories collapse into + sparse-directory entries in the index. Git does not check the + working tree for a path inside such a directory, even when the + virtual file system lists the path. See the "virtual file system" section of linkgit:githooks[5]. core.trustctime:: diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index a2b0ef074a47f4..e0ec68fe0e8451 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -126,6 +126,15 @@ static void clean_tracked_sparse_directories(struct repository *r) */ if (!r || !r->index || !r->worktree) return; + + /* + * A virtual file system owns the working tree, including the + * directories outside of the sparse-checkout cone. Do not delete + * them. + */ + if (repo_config_get_virtualfilesystem(the_repository)) + return; + if (init_sparse_checkout_patterns(r->index) || !r->index->sparse_checkout_patterns->use_cone_patterns) return; diff --git a/dir.c b/dir.c index f1703356ee5502..b3b6ca919658fe 100644 --- a/dir.c +++ b/dir.c @@ -1539,12 +1539,19 @@ enum pattern_match_result path_matches_pattern_list( int result = NOT_MATCHED; size_t slash_pos; - if (core_virtualfilesystem) { + if (core_virtualfilesystem && + !(pl->use_cone_patterns && vfs_sparse_index_configured())) { /* * The virtual file system data is used to prevent git from traversing * any part of the tree that is not in the virtual file system. Return * 1 to exclude the entry if it is not found in the virtual file system, * else fall through to the regular excludes logic as it may further exclude. + * + * Skip this for cone-mode patterns when + * vfs_sparse_index_configured() is true. Here, a return value of + * 1 means "in the cone", so a directory that the virtual file + * system does not report would be in the cone, and it could + * never collapse into a sparse-directory entry. */ if (*dtype == DT_UNKNOWN) *dtype = resolve_dtype(DT_UNKNOWN, istate, pathname, pathlen); @@ -1644,10 +1651,13 @@ static int path_in_sparse_checkout_1(const char *path, const char *end, *slash; /* - * When using a virtual filesystem, there aren't really patterns - * to follow, but be extra careful to skip this check. + * A virtual file system decides which paths are present, so the + * sparse-checkout patterns do not apply. The exception is a + * repository that sets index.sparse to true and uses cone mode + * (see vfs_uses_cone_patterns()). There, the cone patterns decide, + * so that convert_to_sparse() can collapse out-of-cone directories. */ - if (core_virtualfilesystem) + if (core_virtualfilesystem && !vfs_uses_cone_patterns(istate)) return 1; /* diff --git a/read-cache.c b/read-cache.c index 2657161e54a8ed..74cd25bc343b93 100644 --- a/read-cache.c +++ b/read-cache.c @@ -2371,11 +2371,17 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) * If the command explicitly requires a full index, force it * to be full. Otherwise, correct the sparsity based on repository * settings and other properties of the index (if necessary). + * + * With a virtual file system, do not collapse a full index here. + * The skip-worktree bits that convert_to_sparse() checks are not + * correct until apply_virtualfilesystem() sets them, after this + * function. The index collapses when it is written. */ prepare_repo_settings(istate->repo); if (istate->repo->settings.command_requires_full_index) ensure_full_index_with_reason(istate, "incompatible builtin"); - else + else if (!repo_config_get_virtualfilesystem(the_repository) || + !vfs_uses_cone_patterns(istate)) ensure_correct_sparsity(istate); return istate->cache_nr; diff --git a/t/meson.build b/t/meson.build index 517d16b549c208..09ad8c1890d130 100644 --- a/t/meson.build +++ b/t/meson.build @@ -189,6 +189,7 @@ integration_tests = [ 't1092-sparse-checkout-compatibility.sh', 't1093-virtualfilesystem.sh', 't1095-virtualfilesystem-sparse-index.sh', + 't1096-virtualfilesystem-sparse-cone.sh', 't1100-commit-tree-options.sh', 't1300-config.sh', 't1301-shared-repo.sh', diff --git a/t/t1096-virtualfilesystem-sparse-cone.sh b/t/t1096-virtualfilesystem-sparse-cone.sh new file mode 100755 index 00000000000000..b57a3f09408c73 --- /dev/null +++ b/t/t1096-virtualfilesystem-sparse-cone.sh @@ -0,0 +1,224 @@ +#!/bin/sh + +test_description='cone-mode sparse-checkout under a virtual file system + +Without a sparse index, a virtual file system makes +path_in_sparse_checkout() report every path as inside the +sparse-checkout, so convert_to_sparse() never collapses a directory. +When the repository uses cone mode and sets index.sparse to true, the +cone patterns decide. The default value of index.sparse does not count. +Otherwise the behavior must not change.' + +GIT_TEST_SPLIT_INDEX=0 +GIT_TEST_SPARSE_INDEX= + +. ./test-lib.sh + +# Fail if the trace in "$1" shows that the index was expanded. +test_no_index_expansion () { + test_region ! index ensure_full_index "$1" && + test_region ! index expand_index "$1" && + test_grep ! expansion-reason "$1" +} + +# Create a cone-mode repository "$1" with the cone "in" and a full index. +create_cone_repo () { + git init "$1" && + ( + cd "$1" && + mkdir -p in out && + for f in r in/a out/f1 out/f2 + do + echo "$f" >$f || return 1 + done && + git add . && + git commit -m init && + git sparse-checkout set in && + git config index.sparse false && + git sparse-checkout reapply + ) +} + +# Enable a virtual file system in repository "$1". The hook reports the +# paths in .git/vfs-paths, one per line. A virtual file system shows all +# files in the working tree, so put the out-of-cone files back on disk. +enable_vfs () { + write_script "$1/.git/hooks/virtualfilesystem" <<-\EOF && + tr "\n" "\0" <.git/vfs-paths + EOF + cat >"$1/.git/vfs-paths" <<-\EOF && + r + in/a + EOF + mkdir "$1/out" && + echo out/f1 >"$1/out/f1" && + echo out/f2 >"$1/out/f2" && + git -C "$1" config core.virtualfilesystem .git/hooks/virtualfilesystem +} + +# Fail if a file of repository "$1" is missing from the working tree. +test_all_files_exist () { + test_path_is_file "$1/r" && + test_path_is_file "$1/in/a" && + test_path_is_file "$1/out/f1" && + test_path_is_file "$1/out/f2" +} + +test_expect_success 'setup' ' + create_cone_repo vfs && + enable_vfs vfs && + create_cone_repo novfs +' + +test_expect_success 'nothing collapses with index.sparse false' ' + git -C vfs sparse-checkout reapply && + test_all_files_exist vfs && + git -C vfs ls-files --sparse >actual && + cat >expect <<-\EOF && + in/a + out/f1 + out/f2 + r + EOF + test_cmp expect actual +' + +test_expect_success 'nothing collapses with the default index.sparse' ' + git -C vfs config --unset-all index.sparse && + test_might_fail git -C vfs config --worktree --unset-all index.sparse && + test_must_fail git -C vfs config index.sparse && + git -C vfs sparse-checkout reapply && + test_must_fail git -C vfs config index.sparse && + test_all_files_exist vfs && + git -C vfs ls-files --sparse >actual && + cat >expect <<-\EOF && + in/a + out/f1 + out/f2 + r + EOF + test_cmp expect actual +' + +test_expect_success 'out-of-cone directory collapses with index.sparse true' ' + git -C vfs config index.sparse true && + git -C vfs sparse-checkout reapply && + test_all_files_exist vfs && + git -C vfs ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + S out/ + H r + EOF + test_cmp expect actual +' + +test_expect_success 'status keeps the index collapsed' ' + test_when_finished "git -C vfs checkout -- in/a" && + echo changed >>vfs/in/a && + rm -f trace.txt && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git -C vfs status --porcelain --untracked-files=no >actual && + test_no_index_expansion trace.txt && + echo " M in/a" >expect && + test_cmp expect actual && + + git -C vfs ls-files --sparse >files && + test_grep "^out/$" files +' + +test_expect_success 'widening the cone keeps reported files' ' + test_when_finished "git -C vfs sparse-checkout set in" && + git -C vfs sparse-checkout add out && + test_all_files_exist vfs && + git -C vfs ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + S out/f1 + S out/f2 + H r + EOF + test_cmp expect actual +' + +test_expect_success 'reapply on a collapsed index does not expand it' ' + test_when_finished "cp vfs/.git/vfs-paths.bak vfs/.git/vfs-paths" && + cp vfs/.git/vfs-paths vfs/.git/vfs-paths.bak && + echo out/f1 >>vfs/.git/vfs-paths && + rm -f trace.txt && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git -C vfs sparse-checkout reapply && + test_region ! index ensure_full_index trace.txt && + test_grep ! expansion-reason trace.txt && + test_all_files_exist vfs && + git -C vfs ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + S out/ + H r + EOF + test_cmp expect actual +' + +test_expect_success 'directory with a reported path does not collapse' ' + test_when_finished "cp vfs/.git/vfs-paths.bak vfs/.git/vfs-paths" && + cp vfs/.git/vfs-paths vfs/.git/vfs-paths.bak && + git -C vfs config index.sparse false && + git -C vfs sparse-checkout reapply && + echo out/f1 >>vfs/.git/vfs-paths && + git -C vfs config index.sparse true && + git -C vfs sparse-checkout reapply && + test_all_files_exist vfs && + git -C vfs ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + H out/f1 + S out/f2 + H r + EOF + test_cmp expect actual +' + +test_expect_success 'non-cone sparse-checkout under a virtual file system' ' + git -C vfs sparse-checkout set --no-cone "/in/" && + git -C vfs config index.sparse true && + git -C vfs sparse-checkout reapply && + test_all_files_exist vfs && + git -C vfs ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + S out/f1 + S out/f2 + H r + EOF + test_cmp expect actual +' + +test_expect_success 'without a virtual file system, cone mode is unchanged' ' + git -C novfs ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + S out/f1 + S out/f2 + H r + EOF + test_cmp expect actual && + + git -C novfs config index.sparse true && + git -C novfs sparse-checkout reapply && + git -C novfs ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + S out/ + H r + EOF + test_cmp expect actual && + + rm -f trace.txt && + GIT_TRACE2_EVENT="$(pwd)/trace.txt" \ + git -C novfs status --porcelain >actual && + test_no_index_expansion trace.txt && + test_must_be_empty actual +' + +test_done diff --git a/virtualfilesystem.c b/virtualfilesystem.c index 787a99b6b5cea4..3d7d71d1450070 100644 --- a/virtualfilesystem.c +++ b/virtualfilesystem.c @@ -11,6 +11,7 @@ #include "name-hash.h" #include "object.h" #include "read-cache-ll.h" +#include "sparse-index.h" #include "virtualfilesystem.h" #define HOOK_INTERFACE_VERSION (1) @@ -253,6 +254,30 @@ int is_excluded_from_virtualfilesystem(const char *pathname, int pathlen, int dt return -1; } +int vfs_sparse_index_configured(void) +{ + struct repo_config_values *cfg = repo_config_values(the_repository); + int sparse_index; + + if (!cfg->apply_sparse_checkout || !cfg->core_sparse_checkout_cone) + return 0; + return !repo_config_get_bool(the_repository, "index.sparse", + &sparse_index) && sparse_index; +} + +int vfs_uses_cone_patterns(struct index_state *istate) +{ + if (!istate || istate->split_index || !vfs_sparse_index_configured()) + return 0; + + /* + * This runs for each path, so do not use + * is_sparse_index_allowed(istate, 0): it reads the environment + * and can write the configuration. + */ + return is_sparse_index_allowed(istate, SPARSE_INDEX_MEMORY_ONLY); +} + struct apply_virtual_filesystem_stats { int nr_unknown; int nr_vfs_dirs; diff --git a/virtualfilesystem.h b/virtualfilesystem.h index 7a31126ab9ea8c..31d3c139907bb1 100644 --- a/virtualfilesystem.h +++ b/virtualfilesystem.h @@ -24,6 +24,23 @@ int is_included_in_virtualfilesystem(const char *pathname, int pathlen); */ int is_excluded_from_virtualfilesystem(const char *pathname, int pathlen, int dtype); +/* + * With a virtual file system, return 1 if the configuration turns on a + * sparse index: cone-mode sparse-checkout and index.sparse set to true. + * The default value of index.sparse does not count. This reads only the + * configuration, not the sparse-checkout patterns. + */ +int vfs_sparse_index_configured(void); + +/* + * With a virtual file system, return 1 if the cone-mode sparse-checkout + * patterns of 'istate' decide which paths are in the sparse-checkout, + * so that the index can collapse into a sparse index. This requires + * vfs_sparse_index_configured() and a cone-mode pattern file. It loads + * the patterns into 'istate'. + */ +int vfs_uses_cone_patterns(struct index_state *istate); + /* * Free the virtual file system data structures. */ From f35969a6475ca695f068c63bcca104070f1f8b8c Mon Sep 17 00:00:00 2001 From: Tyrie Vella Date: Mon, 16 Nov 2026 23:30:10 -0800 Subject: [PATCH 4/6] unpack-trees: change only the index in update_sparsity() under a VFS 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 --- Documentation/config/core.adoc | 6 +- t/meson.build | 1 + ...7-virtualfilesystem-sparsity-index-only.sh | 205 ++++++++++++++++++ unpack-trees.c | 38 +++- 4 files changed, 243 insertions(+), 7 deletions(-) create mode 100755 t/t1097-virtualfilesystem-sparsity-index-only.sh diff --git a/Documentation/config/core.adoc b/Documentation/config/core.adoc index 18b29c9507675e..e269a10e20cc22 100644 --- a/Documentation/config/core.adoc +++ b/Documentation/config/core.adoc @@ -122,7 +122,11 @@ core.virtualFilesystem:: the cone patterns decide which directories collapse into sparse-directory entries in the index. Git does not check the working tree for a path inside such a directory, even when the - virtual file system lists the path. + virtual file system lists the path. In this mode, when + `git sparse-checkout set`, `add`, `reapply`, or `disable` applies + the patterns, Git changes only the index. It does not check out or + remove files in the working tree. `git sparse-checkout clean` still + removes directories outside of the cone. See the "virtual file system" section of linkgit:githooks[5]. core.trustctime:: diff --git a/t/meson.build b/t/meson.build index 09ad8c1890d130..dec52d2a1d60e5 100644 --- a/t/meson.build +++ b/t/meson.build @@ -190,6 +190,7 @@ integration_tests = [ 't1093-virtualfilesystem.sh', 't1095-virtualfilesystem-sparse-index.sh', 't1096-virtualfilesystem-sparse-cone.sh', + 't1097-virtualfilesystem-sparsity-index-only.sh', 't1100-commit-tree-options.sh', 't1300-config.sh', 't1301-shared-repo.sh', diff --git a/t/t1097-virtualfilesystem-sparsity-index-only.sh b/t/t1097-virtualfilesystem-sparsity-index-only.sh new file mode 100755 index 00000000000000..21bdf45d11c8d3 --- /dev/null +++ b/t/t1097-virtualfilesystem-sparsity-index-only.sh @@ -0,0 +1,205 @@ +#!/bin/sh + +test_description='sparse-checkout changes only the index under a VFS + +A virtual file system owns the working tree. With cone mode and +index.sparse set to true, "git sparse-checkout reapply" must change only +the index. It must not check out or remove files, also when it expands a +sparse directory whose files the virtual file system reports as modified +or deleted.' + +GIT_TEST_SPLIT_INDEX=0 +GIT_TEST_SPARSE_INDEX= + +. ./test-lib.sh + +# Write the cone-mode patterns for the directories "$2"... to the +# sparse-checkout file of repository "$1". +write_cone () { + repo=$1 && + shift && + printf "/*\n!/*/\n" >"$repo/.git/info/sparse-checkout" && + for d in "$@" + do + printf "/%s/\n" "$d" >>"$repo/.git/info/sparse-checkout" || + return 1 + done +} + +# Create repository "$1" with the cone "in" and a sparse index, in which +# "out/" and "out2/" are sparse directories. +create_repo () { + git init "$1" && + ( + cd "$1" && + mkdir -p in out out2/deep && + for f in r in/a out/f1 out/f2 out2/deep/g1 out2/deep/g2 + do + echo "$f" >$f || return 1 + done && + git add . && + git commit -m init && + git sparse-checkout set in && + git config index.sparse true + ) +} + +# Enable a virtual file system in repository "$1". The hook reports the +# paths in .git/vfs-paths, one per line. A virtual file system shows all +# files in the working tree, so put the out-of-cone files back on disk. +enable_vfs () { + write_script "$1/.git/hooks/virtualfilesystem" <<-\EOF && + tr "\n" "\0" <.git/vfs-paths + EOF + printf "r\nin/a\n" >"$1/.git/vfs-paths" && + ( + cd "$1" && + mkdir -p out out2/deep && + for f in out/f1 out/f2 out2/deep/g1 out2/deep/g2 + do + echo "$f" >$f || return 1 + done + ) && + git -C "$1" config core.virtualfilesystem \ + .git/hooks/virtualfilesystem && + git -C "$1" sparse-checkout reapply && + git -C "$1" ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + S out/ + S out2/ + H r + EOF + test_cmp expect actual +} + +# Write a list of the files and directories in the working tree of +# repository "$1", with the contents of the files, to "$2". +snapshot_worktree () { + ( + cd "$1" && + find . -path ./.git -prune -o -print | sort | + while read -r f + do + if test -f "$f" + then + echo "$f $(git hash-object "$f")" + else + echo "$f" + fi || return 1 + done + ) >"$2" +} + +test_expect_success 'widen keeps a deleted file deleted' ' + create_repo deleted && + enable_vfs deleted && + rm deleted/out/f1 && + echo out/f1 >>deleted/.git/vfs-paths && + + write_cone deleted in out && + git -C deleted sparse-checkout reapply && + + test_path_is_missing deleted/out/f1 && + git -C deleted ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + H out/f1 + S out/f2 + S out2/ + H r + EOF + test_cmp expect actual && + git -C deleted status --porcelain --untracked-files=no >actual && + echo " D out/f1" >expect && + test_cmp expect actual +' + +test_expect_success 'widen keeps the content of a modified file' ' + create_repo modified && + enable_vfs modified && + echo changed >modified/out/f2 && + echo out/f2 >>modified/.git/vfs-paths && + + write_cone modified in out && + git -C modified sparse-checkout reapply 2>err && + + test_must_be_empty err && + echo changed >expect && + test_cmp expect modified/out/f2 && + git -C modified status --porcelain --untracked-files=no >actual && + echo " M out/f2" >expect && + test_cmp expect actual +' + +test_expect_success 'reapply does not change the working tree' ' + create_repo nochange && + enable_vfs nochange && + rm nochange/out/f1 && + echo changed >nochange/out2/deep/g2 && + printf "out/f1\nout2/deep/g2\n" >>nochange/.git/vfs-paths && + snapshot_worktree nochange before && + + write_cone nochange in out out2 && + git -C nochange sparse-checkout reapply && + + snapshot_worktree nochange after && + test_cmp before after && + git -C nochange ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + H out/f1 + S out/f2 + S out2/deep/g1 + H out2/deep/g2 + H r + EOF + test_cmp expect actual +' + +test_expect_success 'add and disable keep deleted files deleted' ' + create_repo callers && + enable_vfs callers && + rm callers/out/f1 callers/out2/deep/g1 && + printf "out/f1\nout2/deep/g1\n" >>callers/.git/vfs-paths && + snapshot_worktree callers before && + + git -C callers sparse-checkout add out && + test_path_is_missing callers/out/f1 && + git -C callers sparse-checkout disable && + test_path_is_missing callers/out2/deep/g1 && + + snapshot_worktree callers after && + test_cmp before after && + git -C callers status --porcelain --untracked-files=no >actual && + cat >expect <<-\EOF && + D out/f1 + D out2/deep/g1 + EOF + test_cmp expect actual +' + +test_expect_success 'without a virtual file system, widen checks out files' ' + create_repo novfs && + git -C novfs sparse-checkout reapply && + test_path_is_missing novfs/out && + + write_cone novfs in out && + git -C novfs sparse-checkout reapply && + + echo out/f1 >expect && + test_cmp expect novfs/out/f1 && + echo out/f2 >expect && + test_cmp expect novfs/out/f2 && + git -C novfs ls-files -t --sparse >actual && + cat >expect <<-\EOF && + H in/a + H out/f1 + H out/f2 + S out2/ + H r + EOF + test_cmp expect actual +' + +test_done diff --git a/unpack-trees.c b/unpack-trees.c index d2af690479633c..5b95415002a71b 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -538,9 +538,12 @@ static int verify_absent_sparse(const struct cache_entry *ce, enum unpack_trees_error_types, struct unpack_trees_options *o); -static int apply_sparse_checkout(struct index_state *istate, - struct cache_entry *ce, - struct unpack_trees_options *o) +/* + * Copy CE_NEW_SKIP_WORKTREE to CE_SKIP_WORKTREE. Return the old value of + * the skip-worktree bit. + */ +static int update_skip_worktree_bit(struct index_state *istate, + struct cache_entry *ce) { int was_skip_worktree = ce_skip_worktree(ce); @@ -553,6 +556,14 @@ static int apply_sparse_checkout(struct index_state *istate, mark_fsmonitor_invalid(istate, ce); istate->cache_changed |= CE_ENTRY_CHANGED; } + return was_skip_worktree; +} + +static int apply_sparse_checkout(struct index_state *istate, + struct cache_entry *ce, + struct unpack_trees_options *o) +{ + int was_skip_worktree = update_skip_worktree_bit(istate, ce); /* * if (!was_skip_worktree && !ce_skip_worktree()) { @@ -2164,7 +2175,8 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options /* * Update SKIP_WORKTREE bits according to sparsity patterns, and update - * working directory to match. + * working directory to match. Under a virtual file system with a sparse + * index, update only the index (see vfs_sparse_index_configured()). * * CE_NEW_SKIP_WORKTREE is used internally. */ @@ -2175,6 +2187,7 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o, int i; unsigned old_show_all_errors; int free_pattern_list = 0; + int vfs_index_only; old_show_all_errors = o->internal.show_all_errors; o->internal.show_all_errors = 1; @@ -2196,6 +2209,17 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o, } o->internal.pl = pl; + /* + * A virtual file system owns the working tree, and its hook sets + * the skip-worktree bits. When the repository uses cone mode and + * turns on a sparse index under a virtual file system, change + * only the index. An expanded sparse directory can contain a path + * that the hook reports as modified or deleted. Do not check it + * out or remove it, so that the change in the working tree stays. + */ + vfs_index_only = core_virtualfilesystem && + vfs_sparse_index_configured(); + /* Expand sparse directories as needed */ expand_index(o->src_index, o->internal.pl); @@ -2217,11 +2241,13 @@ enum update_sparsity_result update_sparsity(struct unpack_trees_options *o, continue; } - if (apply_sparse_checkout(o->src_index, ce, o)) + if (vfs_index_only) + update_skip_worktree_bit(o->src_index, ce); + else if (apply_sparse_checkout(o->src_index, ce, o)) ret = UPDATE_SPARSITY_WARNINGS; } - if (check_updates(o, o->src_index)) + if (!vfs_index_only && check_updates(o, o->src_index)) ret = UPDATE_SPARSITY_WORKTREE_UPDATE_FAILURES; display_warning_msgs(o); From 0a9676183307d04420e32ee812231e973360f0b5 Mon Sep 17 00:00:00 2001 From: Tyrie Vella Date: Fri, 13 Nov 2026 15:33:54 -0800 Subject: [PATCH 5/6] sparse-index: do not expand the index in the working-tree walk under VFS A sparse index collapses each directory outside the sparse-checkout cone into one sparse-directory entry. Several lookups in the working-tree walk use index_name_pos(). When the path is inside a sparse-directory entry, index_name_pos() expands the whole index to answer a question about one path. A plain sparse checkout does not get to these lookups. The files outside the cone are not on disk, so the walk does not go into those directories. Under core.virtualFileSystem, the working tree shows the full tree while the index stays sparse, and the walk goes into them. In a repository with about 2.2 million index entries, an earlier version of this change made "git status" go from about 20 seconds to less than half a second. When the command writes the index, it expands the index a second time ("re-expanding after write"). Five lookups are involved. read_skip_worktree_file_from_index() reads a .gitignore from the index when the file is not on disk. add_patterns() looks up the object ID of a .gitignore on disk for the untracked cache. directory_exists_in_index() decides if a directory is tracked. get_index_dtype() gives the type of a path to resolve_dtype() when the caller does not know it. index_name_is_other() removes tracked paths from the untracked list in "git status" and "git clean". Add vfs_sparse_dir_containing(). If core.virtualFileSystem is set, the index is sparse, and a sparse-directory entry contains the path, it returns that entry without an expansion. Otherwise it returns NULL. Each of the five lookups calls it first. Under core.virtualFileSystem, a NULL result means that index_name_pos() cannot expand the index for that path, so the existing code runs. Without core.virtualFileSystem, the helper always returns NULL, and the behavior does not change. When a sparse-directory entry contains the path, each lookup gives the answer of the full index. Everything inside a sparse-directory entry has the skip-worktree bit, so read_skip_worktree_file_from_index() reads the blob from the tree of that entry. The untracked-cache lookup computes the object ID from the file on disk, as it does for any path that has no usable index entry. directory_exists_in_index() and index_name_is_other() look up the path in the tree. An index has entries for files only, so index_name_is_other() reports a directory in the tree as "other". directory_exists_in_index() does its lookup before the case-insensitive lookup, because the name hash does not contain the directories inside a sparse-directory entry. get_index_dtype() returns DT_UNKNOWN, and the caller uses lstat(). An index that was just expanded returns DT_UNKNOWN too, because expansion does not mark the new entries up to date. Add sparse_dir_get_tree_entry() for the tree lookups. If core.ignoreCase is set, directory_exists_in_index() and index_name_is_other() compare names without case, because with a full index the walk finds tracked paths with case-insensitive lookups. If a tree cannot be read, the helper dies. A failure to read must not make a tracked path look untracked, because "git clean" could then delete it. For these two lookups, vfs_sparse_dir_containing() also finds the sparse-directory entry when its name has a different case on disk, for example "OUT/a" for the entry "out/". The index sorts names with case, so such a path does not sort next to its sparse-directory entry, and the name hash does not contain the paths inside the entry. Without this, "git status" reports the tracked files below "OUT/" as untracked, and "git clean -f -d" deletes them. The helper looks up each leading directory of the path in the case-insensitive dir_hash, which contains the directory of each sparse-directory entry, and checks if the name that the index uses for it is a sparse-directory entry. In a test repository with about 41,000 paths on disk, this made the walk of "git status -uall" with core.ignoreCase take 0.245 seconds instead of 0.22. The other three lookups compare with case, as index_name_pos() does. Add t1094, which puts files on disk inside a sparse directory and uses a virtual file system hook. The tests compare "git status", "git clean -n", and "git add --ignore-missing" with the results in a clone that has a full index. They also use trace2 to make sure that the index does not expand and stays sparse. Other tests check directories spelled with a different case on disk (inside a sparse directory, the sparse directory itself, and a nested sparse directory), and an unreadable tree. A last test shows that without a virtual file system, the walk still expands the index. Assisted-by: Claude Opus 5.5 Signed-off-by: Tyrie Vella --- dir.c | 51 ++++ read-cache.c | 23 ++ sparse-index.c | 127 ++++++++++ sparse-index.h | 30 +++ t/meson.build | 1 + t/t1094-virtualfilesystem-sparse-walk.sh | 298 +++++++++++++++++++++++ 6 files changed, 530 insertions(+) create mode 100755 t/t1094-virtualfilesystem-sparse-walk.sh diff --git a/dir.c b/dir.c index b3b6ca919658fe..ade4eed734f536 100644 --- a/dir.c +++ b/dir.c @@ -1004,8 +1004,26 @@ static int read_skip_worktree_file_from_index(struct index_state *istate, struct oid_stat *oid_stat) { int pos, len; + struct cache_entry *sparse_dir; len = strlen(path); + + /* + * Everything inside a sparse-directory entry has the skip-worktree + * bit, so read the blob from the tree of that entry. Compare with + * case, as index_name_pos() does. + */ + sparse_dir = vfs_sparse_dir_containing(istate, path, len, 0); + if (sparse_dir) { + struct object_id oid; + unsigned short mode; + + if (sparse_dir_get_tree_entry(istate, sparse_dir, path, len, 0, + &oid, &mode)) + return -1; + return do_read_blob(&oid, oid_stat, size_out, data_out); + } + pos = index_name_pos(istate, path, len); if (pos < 0) return -1; @@ -1252,6 +1270,8 @@ static int add_patterns(const char *fname, const char *base, int baselen, !match_stat_data_racy(istate, &oid_stat->stat, &st)) ; /* no content change, oid_stat->oid still good */ else if (istate && + !vfs_sparse_dir_containing(istate, fname, + strlen(fname), 0) && (pos = index_name_pos(istate, fname, strlen(fname))) >= 0 && !ce_stage(istate->cache[pos]) && ce_uptodate(istate->cache[pos]) && @@ -2004,6 +2024,29 @@ static enum exist_status directory_exists_in_index(struct index_state *istate, const char *dirname, int len) { int pos; + struct cache_entry *sparse_dir; + + /* + * The name hash does not contain the directories inside a + * sparse-directory entry, so look in its tree before the + * case-insensitive lookup below. + */ + sparse_dir = vfs_sparse_dir_containing( + istate, dirname, len, repo_ignore_case(the_repository)); + if (sparse_dir) { + struct object_id oid; + unsigned short mode; + + if (sparse_dir_get_tree_entry(istate, sparse_dir, dirname, len, + repo_ignore_case(the_repository), + &oid, &mode)) + return index_nonexistent; + if (S_ISDIR(mode)) + return index_directory; + if (S_ISGITLINK(mode)) + return index_gitdir; + return index_nonexistent; + } if (repo_ignore_case(the_repository)) return directory_exists_in_index_icase(istate, dirname, len); @@ -2410,6 +2453,14 @@ static int get_index_dtype(struct index_state *istate, return DT_REG; } + /* + * A sparse-directory entry contains this path, so the index has no + * entries below it to scan. Return DT_UNKNOWN and let the caller + * use lstat(), instead of an expansion of the whole index. + */ + if (vfs_sparse_dir_containing(istate, path, len, 0)) + return DT_UNKNOWN; + /* Try to look it up as a directory */ pos = index_name_pos(istate, path, len); if (pos >= 0) diff --git a/read-cache.c b/read-cache.c index 74cd25bc343b93..38397b4b1be734 100644 --- a/read-cache.c +++ b/read-cache.c @@ -3507,8 +3507,31 @@ int index_name_is_other(struct index_state *istate, const char *name, int namelen) { int pos; + struct cache_entry *sparse_dir; + if (namelen && name[namelen - 1] == '/') namelen--; + + /* + * An index has entries for files only, so a directory in the tree + * of a sparse-directory entry is still "other". Compare without + * case if core.ignoreCase is set: with a full index, the walk + * already drops such tracked paths with index_file_exists(). + */ + sparse_dir = vfs_sparse_dir_containing( + istate, name, namelen, repo_ignore_case(the_repository)); + if (sparse_dir) { + struct object_id oid; + unsigned short mode; + + if (sparse_dir_get_tree_entry(istate, sparse_dir, name, namelen, + repo_ignore_case(the_repository), + &oid, &mode) || + S_ISDIR(mode)) + return 1; + return 0; + } + pos = index_name_pos(istate, name, namelen); if (0 <= pos) return 0; /* exact match */ diff --git a/sparse-index.c b/sparse-index.c index cbeb0971e5cff7..299c600045b34b 100644 --- a/sparse-index.c +++ b/sparse-index.c @@ -5,11 +5,14 @@ #include "environment.h" #include "ewah/ewok.h" #include "gettext.h" +#include "hex.h" #include "name-hash.h" +#include "odb.h" #include "read-cache-ll.h" #include "repository.h" #include "sparse-index.h" #include "tree.h" +#include "tree-walk.h" #include "pathspec.h" #include "trace2.h" #include "cache-tree.h" @@ -729,6 +732,130 @@ void clear_skip_worktree_from_present_files(struct index_state *istate) } } +/* + * The index sorts names with case, so a name with a different case does + * not sort next to its sparse-directory entry. The dir_hash of the name + * hash compares without case, and it contains the directory of each + * sparse-directory entry. Look up each leading directory of 'path' there, + * and check if the name that the index uses for it is a sparse-directory + * entry. A sparse-directory entry contains no other entries, so the first + * match is the only one. + */ +static struct cache_entry *sparse_dir_containing_icase( + struct index_state *istate, const char *path, int pathlen) +{ + struct strbuf dir = STRBUF_INIT; + struct cache_entry *found = NULL; + const char *end = path + pathlen; + const char *slash = path; + + while ((slash = memchr(slash, '/', end - slash)) && + slash + 1 < end) { + int pos; + + if (!index_dir_find(istate, path, slash - path, &dir)) + break; + strbuf_addch(&dir, '/'); + pos = index_name_pos_sparse(istate, dir.buf, dir.len); + if (pos >= 0 && S_ISSPARSEDIR(istate->cache[pos]->ce_mode)) { + found = istate->cache[pos]; + break; + } + slash++; + } + strbuf_release(&dir); + return found; +} + +struct cache_entry *vfs_sparse_dir_containing(struct index_state *istate, + const char *path, int pathlen, + int icase) +{ + struct cache_entry *ce; + int pos; + + if (!core_virtualfilesystem || !istate->sparse_index || pathlen <= 0) + return NULL; + + pos = index_name_pos_sparse(istate, path, pathlen); + if (pos >= 0) + return NULL; + pos = -pos - 1; + + /* + * Only the entry immediately before the insertion point can be a + * sparse-directory entry that contains 'path' with the same case. + * This is the same test that index_name_pos() uses to decide to + * expand the index. + */ + if (pos) { + ce = istate->cache[pos - 1]; + if (S_ISSPARSEDIR(ce->ce_mode) && + ce_namelen(ce) < pathlen && + !strncmp(path, ce->name, ce_namelen(ce))) + return ce; + } + + if (!icase) + return NULL; + return sparse_dir_containing_icase(istate, path, pathlen); +} + +int sparse_dir_get_tree_entry(struct index_state *istate, + const struct cache_entry *sparse_dir, + const char *path, int pathlen, int icase, + struct object_id *oid, unsigned short *mode) +{ + const char *name = path + ce_namelen(sparse_dir); + const char *end = path + pathlen; + struct object_id tree_oid; + + oidcpy(&tree_oid, &sparse_dir->oid); + for (;;) { + const char *slash = memchr(name, '/', end - name); + size_t len = (slash ? slash : end) - name; + struct tree_desc desc; + struct name_entry entry; + enum object_type type; + size_t size; + void *buf; + int found = 0; + + /* + * Do not report an unreadable tree as "not in the tree". The + * callers would then call tracked files untracked, and + * "git clean" could delete them. + */ + buf = odb_read_object(istate->repo->objects, &tree_oid, + &type, &size); + if (!buf || type != OBJ_TREE) + die(_("unable to read tree %s for '%.*s'"), + oid_to_hex(&tree_oid), pathlen, path); + + init_tree_desc(&desc, &tree_oid, buf, size); + while (tree_entry(&desc, &entry)) { + if (tree_entry_len(&entry) == len && + !(icase ? strncasecmp(entry.path, name, len) : + strncmp(entry.path, name, len))) { + oidcpy(oid, &entry.oid); + *mode = entry.mode; + found = 1; + break; + } + } + free(buf); + + if (!found) + return -1; + if (!slash) + return 0; + if (!S_ISDIR(*mode)) + return -1; + oidcpy(&tree_oid, oid); + name = slash + 1; + } +} + /* * This static global helps avoid infinite recursion between * expand_to_path() and index_file_exists(). diff --git a/sparse-index.h b/sparse-index.h index 15180b02ea6599..47935b3ae227e5 100644 --- a/sparse-index.h +++ b/sparse-index.h @@ -11,6 +11,8 @@ extern int give_advice_on_expansion; struct index_state; +struct cache_entry; +struct object_id; #define SPARSE_INDEX_MEMORY_ONLY (1 << 0) int is_sparse_index_allowed(struct index_state *istate, int flags); int convert_to_sparse(struct index_state *istate, int flags); @@ -30,6 +32,34 @@ void clear_skip_worktree_from_present_files(struct index_state *istate); void expand_to_path(struct index_state *istate, const char *path, size_t pathlen, int icase); +/* + * Under core.virtualFileSystem, the working tree can contain paths inside + * directories that a sparse index collapses into sparse-directory entries. + * A lookup of such a path with index_name_pos() expands the whole index. + * + * If core.virtualFileSystem is set, the index is sparse, and a + * sparse-directory entry contains 'path', return that entry. Otherwise + * return NULL. If 'icase' is set, the name of the sparse-directory entry + * can differ in case from the start of 'path'. This function never expands + * the index. If core.virtualFileSystem is set and this function returns + * NULL, then index_name_pos() for 'path' does not expand the index either. + */ +struct cache_entry *vfs_sparse_dir_containing(struct index_state *istate, + const char *path, int pathlen, + int icase); + +/* + * Find the part of 'path' below the sparse-directory entry 'sparse_dir' + * in the tree of that entry. 'sparse_dir' must contain 'path'. If 'icase' + * is set, compare names without case. Return 0 and set 'oid' and 'mode' + * if the tree contains the path. Return -1 if it does not. Die if a tree + * cannot be read. + */ +int sparse_dir_get_tree_entry(struct index_state *istate, + const struct cache_entry *sparse_dir, + const char *path, int pathlen, int icase, + struct object_id *oid, unsigned short *mode); + struct repository; int set_sparse_index_config(struct repository *repo, int enable); diff --git a/t/meson.build b/t/meson.build index dec52d2a1d60e5..2f001d08b5a55a 100644 --- a/t/meson.build +++ b/t/meson.build @@ -188,6 +188,7 @@ integration_tests = [ 't1091-sparse-checkout-builtin.sh', 't1092-sparse-checkout-compatibility.sh', 't1093-virtualfilesystem.sh', + 't1094-virtualfilesystem-sparse-walk.sh', 't1095-virtualfilesystem-sparse-index.sh', 't1096-virtualfilesystem-sparse-cone.sh', 't1097-virtualfilesystem-sparsity-index-only.sh', diff --git a/t/t1094-virtualfilesystem-sparse-walk.sh b/t/t1094-virtualfilesystem-sparse-walk.sh new file mode 100755 index 00000000000000..749173203ad5b6 --- /dev/null +++ b/t/t1094-virtualfilesystem-sparse-walk.sh @@ -0,0 +1,298 @@ +#!/bin/sh + +test_description='sparse index and virtual file system: working-tree walk + +A virtual file system projects files on disk inside directories that a +sparse index collapses into sparse-directory entries. The working-tree walk +must answer its index lookups for those paths without an expansion of the +index, and give the same results as an expanded index. +' + +# A split index cannot be sparse. +GIT_TEST_SPLIT_INDEX=0 +GIT_TEST_SPARSE_INDEX= + +. ./test-lib.sh + +# Run a command in "repo" with the virtual file system and record trace2 +# events in trace-vfs.txt. +run_vfs () { + rm -f trace-vfs.txt && + GIT_TRACE2_EVENT="$(pwd)/trace-vfs.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C repo "$@" +} + +# Run a command in "full", which has the same working tree and sparse-checkout +# definition as "repo" but a full index and no virtual file system. +run_full () { + git -C full -c sparse.expectFilesOutsideOfPatterns=true "$@" +} + +# Run a command in "repo" without the virtual file system. The out-of-cone +# files stay on disk, so the walk visits them, and its index lookups use +# the code that expands a sparse index. +run_no_vfs () { + rm -f trace-no-vfs.txt && + GIT_TRACE2_EVENT="$(pwd)/trace-no-vfs.txt" \ + GIT_TRACE2_EVENT_NESTING=10 \ + git -C repo -c core.virtualfilesystem= \ + -c sparse.expectFilesOutsideOfPatterns=true "$@" +} + +ensure_not_expanded () { + test_region ! index ensure_full_index "$1" && + test_grep ! "expansion-reason" "$1" +} + +ensure_index_is_sparse () { + git -C repo -c core.virtualfilesystem= \ + -c sparse.expectFilesOutsideOfPatterns=true \ + ls-files --sparse >sparse-entries && + test_grep "^out/$" sparse-entries +} + +# Run a command with the virtual file system over a sparse index, and in +# the full-index repository, then compare. The virtual file system run +# must not expand the index. +test_vfs_matches_full () { + run_vfs "$@" >actual-vfs && + ensure_not_expanded trace-vfs.txt && + ensure_index_is_sparse && + run_full "$@" >actual-full && + test_cmp actual-full actual-vfs +} + +# Rename the directory $1 to $2, a name that differs only in case, in each +# repository that follows them. +rename_case () { + from=$1 && + to=$2 && + shift 2 && + for r in "$@" + do + mv "$r/$from" "$r/$from.tmp" && + mv "$r/$from.tmp" "$r/$to" || return 1 + done +} + +# Put the out-of-cone files back on disk, as a virtual file system does, +# and add untracked and ignored files inside "out/". Leave +# out/deep/.gitignore absent, so that the walk reads it from the index. +populate_out_of_cone () { + mkdir -p $1/out/deep/sub $1/out/other && + for f in out/a out/deep/a out/deep/sub/a \ + out/other/a out/other/.gitignore + do + git -C $1 show HEAD:$f >$1/$f || return 1 + done && + echo new >$1/out/new.txt && + echo new >$1/out/deep/new.txt && + echo new >$1/out/deep/sub/new.txt && + echo new >$1/out/deep/x.ignored && + mkdir -p $1/out/deep/ignored-dir $1/out/untracked-dir && + echo new >$1/out/deep/ignored-dir/y.ignored && + echo new >$1/out/untracked-dir/new.txt && + echo new >$1/out/other/z.skip +} + +test_expect_success 'setup' ' + git init repo && + mkdir -p repo/in repo/out/deep/sub repo/out/other && + echo a >repo/a && + echo a >repo/in/a && + echo a >repo/out/a && + echo a >repo/out/deep/a && + echo a >repo/out/deep/sub/a && + echo a >repo/out/other/a && + echo "*.ignored" >repo/out/deep/.gitignore && + echo "*.skip" >repo/out/other/.gitignore && + git -C repo add . && + git -C repo commit -m initial && + git clone repo full && + + git -C repo sparse-checkout set --cone --sparse-index in && + test_path_is_missing repo/out && + populate_out_of_cone repo && + + git -C full sparse-checkout set --cone --no-sparse-index in && + test_path_is_missing full/out && + populate_out_of_cone full && + + # List "out/" as a whole, so the walk visits it. This name is the + # exact name of the sparse-directory entry, so the hook does not make + # git expand the index. + write_script repo/.git/hooks/virtualfilesystem <<-\EOF && + printf "a\0in/\0out/\0" + EOF + git -C repo config core.virtualfilesystem \ + .git/hooks/virtualfilesystem && + ensure_index_is_sparse +' + +test_expect_success 'status does not expand the index' ' + run_vfs status --porcelain=v1 >actual && + ensure_not_expanded trace-vfs.txt && + ensure_index_is_sparse && + cat >expect <<-\EOF && + ?? out/deep/new.txt + ?? out/deep/sub/new.txt + ?? out/new.txt + ?? out/untracked-dir/ + EOF + test_cmp expect actual +' + +test_expect_success 'status -uall does not expand the index' ' + run_vfs status --porcelain=v1 -uall >actual && + ensure_not_expanded trace-vfs.txt && + cat >expect <<-\EOF && + ?? out/deep/new.txt + ?? out/deep/sub/new.txt + ?? out/new.txt + ?? out/untracked-dir/new.txt + EOF + test_cmp expect actual +' + +for ignorecase in false true +do + for args in "" "-uall" "--ignored" "--ignored -uall" \ + "--ignored=matching" + do + test_expect_success "status $args (ignoreCase=$ignorecase)" " + test_vfs_matches_full -c core.ignoreCase=$ignorecase \ + status --porcelain=v1 $args + " + done + + for args in "" "-d" "-d -x" "-d -X" + do + test_expect_success "clean -n $args (ignoreCase=$ignorecase)" " + test_vfs_matches_full -c core.ignoreCase=$ignorecase \ + clean -n $args + " + done +done + +test_expect_success 'clean -n -d -x lists only untracked paths' ' + run_vfs clean -n -d -x >actual && + ensure_not_expanded trace-vfs.txt && + cat >expect <<-\EOF && + Would remove out/deep/ignored-dir/ + Would remove out/deep/new.txt + Would remove out/deep/sub/new.txt + Would remove out/deep/x.ignored + Would remove out/new.txt + Would remove out/other/z.skip + Would remove out/untracked-dir/ + EOF + test_cmp expect actual +' + +# Spell a tracked directory with a different case on disk. With +# core.ignoreCase, a full index treats it as tracked, so "git clean" +# must not offer to remove it. +test_expect_success 'directory with a different case on disk (ignoreCase)' ' + rename_case out/other out/OTHER repo full && + test_when_finished "rename_case out/OTHER out/other repo full" && + test_vfs_matches_full -c core.ignoreCase=true \ + status --porcelain=v1 -uall && + test_vfs_matches_full -c core.ignoreCase=true clean -n -d && + test_grep ! OTHER actual-vfs +' + +test_expect_success 'sparse directory with a different case on disk (ignoreCase)' ' + rename_case out OUT repo full && + test_when_finished "rename_case OUT out repo full" && + test_vfs_matches_full -c core.ignoreCase=true \ + status --porcelain=v1 -uall && + test_grep ! "OUT/a$" actual-vfs && + test_vfs_matches_full -c core.ignoreCase=true clean -n -d && + test_grep "OUT/new.txt" actual-vfs && + test_grep ! "OUT/a$" actual-vfs && + test_grep ! "OUT/deep/$" actual-vfs +' + +# Here "out/" is in the cone as a parent directory, and "out/deep/" is the +# sparse-directory entry. Both have a different case on disk. +test_expect_success 'nested sparse directory with a different case on disk' ' + test_when_finished "rm -rf repo2 full2" && + git clone full repo2 && + git clone full full2 && + git -C repo2 sparse-checkout set --cone --sparse-index in out/other && + git -C full2 sparse-checkout set --cone --no-sparse-index in out/other && + populate_out_of_cone repo2 && + populate_out_of_cone full2 && + write_script repo2/.git/hooks/virtualfilesystem <<-\EOF && + printf "a\0in/\0out/a\0out/new.txt\0out/deep/\0out/other/\0" + printf "out/untracked-dir/\0" + EOF + git -C repo2 config core.virtualfilesystem \ + .git/hooks/virtualfilesystem && + rename_case out OUT repo2 full2 && + rename_case OUT/deep OUT/Deep repo2 full2 && + + for args in "status --porcelain=v1 -uall" "clean -n -d" + do + rm -f trace-vfs.txt && + GIT_TRACE2_EVENT="$(pwd)/trace-vfs.txt" \ + GIT_TRACE2_EVENT_NESTING=10 \ + git -C repo2 -c core.ignoreCase=true $args >actual-vfs && + ensure_not_expanded trace-vfs.txt && + git -C full2 -c sparse.expectFilesOutsideOfPatterns=true \ + -c core.ignoreCase=true $args >actual-full && + test_cmp actual-full actual-vfs || return 1 + done && + test_grep "OUT/Deep/new.txt" actual-vfs && + test_grep ! "OUT/Deep/a$" actual-vfs && + test_grep ! "OUT/Deep/$" actual-vfs && + git -C repo2 -c core.virtualfilesystem= \ + -c sparse.expectFilesOutsideOfPatterns=true \ + ls-files --sparse >sparse-entries && + test_grep "^out/deep/$" sparse-entries +' + +test_expect_success 'status with the untracked cache' ' + test_vfs_matches_full -c core.untrackedCache=true \ + status --porcelain=v1 && + test_vfs_matches_full -c core.untrackedCache=true \ + status --porcelain=v1 -uall +' + +# With a glob pathspec, "git add --ignore-missing" asks is_excluded() for +# the type of the path, which looks up the path in the index. +test_expect_success 'add --ignore-missing of an ignored path' ' + rm -f trace-vfs.txt && + test_must_fail env GIT_TRACE2_EVENT="$(pwd)/trace-vfs.txt" \ + GIT_TRACE2_EVENT_NESTING=10 \ + git -C repo add --dry-run --ignore-missing \ + ":(glob)out/deep/x.ignored" >actual-vfs 2>err-vfs && + ensure_not_expanded trace-vfs.txt && + ensure_index_is_sparse && + test_must_fail git -C full -c sparse.expectFilesOutsideOfPatterns=true \ + add --dry-run --ignore-missing \ + ":(glob)out/deep/x.ignored" >actual-full 2>err-full && + test_cmp actual-full actual-vfs && + test_cmp err-full err-vfs && + test_grep "ignored by one of your .gitignore files" err-vfs && + test_grep "out/deep/x.ignored" err-vfs +' + +test_expect_success 'an unreadable tree in a sparse directory is an error' ' + test_when_finished "rm -rf broken" && + cp -R repo broken && + tree=$(git -C broken rev-parse HEAD:out/deep) && + rm broken/.git/objects/$(test_oid_to_path $tree) && + test_must_fail git -C broken clean -n -d >out 2>err && + test_grep "unable to read tree $tree" err && + test_grep ! "Would remove" out +' + +test_expect_success 'without a virtual file system, the walk still expands' ' + run_no_vfs status --porcelain=v1 >actual && + test_grep "found parent dir .out/." trace-no-vfs.txt && + run_full status --porcelain=v1 >expect && + test_cmp expect actual +' + +test_done From 55f945ac233a5043ecef776bb3aa17b44c26bb4f Mon Sep 17 00:00:00 2001 From: Tyrie Vella Date: Fri, 13 Nov 2026 23:04:27 -0800 Subject: [PATCH 6/6] help: advertise the vfs-sparse-index capability in build options A virtual file system such as VFS for Git can use a sparse index only when the Git that it runs supports a sparse index under core.virtualFilesystem. A Git without this support does not fail. It expands the sparse index to a full index on each read, and the index stays full. The virtual file system cannot see this, so it must ask Git before it enables a sparse index. Print "feature: vfs-sparse-index" in "git version --build-options". A virtual file system can run this command once and read the line. It must not enable a sparse index when the line is not present. A Git without this commit does not print the line, so the check fails safe. The support is always compiled in, and configuration controls it at run time, so the line is unconditional. The line is a promise. When core.virtualFilesystem is set, a build that prints the line has all of the behavior that follows. The working-tree walk does not expand a sparse index: its lookups for .gitignore files, tracked directories, path types, and untracked paths find the sparse-directory entry that contains the path, and answer without an expansion. apply_virtualfilesystem() does not expand a sparse index, and a path that the hook reports inside a sparse-directory entry does not clear the skip-worktree bit of that entry. When expand_index() expands the index, the name-hash still finds the entries that it reuses. When the repository uses cone-mode sparse-checkout and sets index.sparse to true in its configuration, the cone patterns decide which directories collapse into sparse-directory entries. The default value of index.sparse does not count, so the virtual file system must turn this on explicitly. A full index does not collapse when Git reads it, because the skip-worktree bits are not correct until apply_virtualfilesystem() runs. The index collapses when Git writes it. Last, in this mode, "git sparse-checkout set", "add", "reapply", "init", and "disable" change only the index. They do not check out or remove files, and their automatic cleanup does not delete the out-of-cone directories, because the virtual file system owns the working tree. The explicit "git sparse-checkout clean" command is not part of the promise. If a later change removes any part of this behavior, it must also remove the line. Otherwise a virtual file system will enable a sparse index that Git does not handle correctly. The comment next to the line lists the same parts. Also name the line in the description of core.virtualFilesystem. Add t1098. Its tests check that "git version --build-options" and "git --version --build-options" print the line, and that "git version" without the option does not print it. Its description names t1094, t1095, t1096, and t1097, which test the promised behavior. Assisted-by: Claude Opus 5.5 Signed-off-by: Tyrie Vella --- Documentation/config/core.adoc | 4 +++- help.c | 15 ++++++++++++++ t/meson.build | 1 + t/t1098-vfs-sparse-index-capability.sh | 28 ++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100755 t/t1098-vfs-sparse-index-capability.sh diff --git a/Documentation/config/core.adoc b/Documentation/config/core.adoc index e269a10e20cc22..151edef15bd46a 100644 --- a/Documentation/config/core.adoc +++ b/Documentation/config/core.adoc @@ -126,7 +126,9 @@ core.virtualFilesystem:: `git sparse-checkout set`, `add`, `reapply`, or `disable` applies the patterns, Git changes only the index. It does not check out or remove files in the working tree. `git sparse-checkout clean` still - removes directories outside of the cone. + removes directories outside of the cone. A build of Git that + supports a sparse index under a virtual file system prints + `feature: vfs-sparse-index` in `git version --build-options`. See the "virtual file system" section of linkgit:githooks[5]. core.trustctime:: diff --git a/help.c b/help.c index c634a5e521e298..184d336e6b8d7e 100644 --- a/help.c +++ b/help.c @@ -820,6 +820,21 @@ void get_version_info(struct strbuf *buf, int show_build_options) if (fsmonitor_ipc__is_supported()) strbuf_addstr(buf, "feature: fsmonitor--daemon\n"); + /* + * A virtual file system reads this line before it enables + * a sparse index. Under core.virtualFilesystem, the line + * promises all of this: the working-tree walk and + * apply_virtualfilesystem() do not expand a sparse index; + * expand_index() keeps the name-hash correct; with cone + * mode and an explicit index.sparse=true, the cone + * patterns decide which directories collapse, a full + * index collapses when it is written, not when it is read, + * and "git sparse-checkout set", "add", "reapply", "init", + * and "disable" change only the index: they do not check + * out, remove, or clean up files in the working tree. If + * any part goes away, remove the line. + */ + strbuf_addstr(buf, "feature: vfs-sparse-index\n"); #if !defined NO_GETTEXT strbuf_addstr(buf, "gettext: enabled\n"); #endif diff --git a/t/meson.build b/t/meson.build index 2f001d08b5a55a..f53dd9eb8f9938 100644 --- a/t/meson.build +++ b/t/meson.build @@ -192,6 +192,7 @@ integration_tests = [ 't1095-virtualfilesystem-sparse-index.sh', 't1096-virtualfilesystem-sparse-cone.sh', 't1097-virtualfilesystem-sparsity-index-only.sh', + 't1098-vfs-sparse-index-capability.sh', 't1100-commit-tree-options.sh', 't1300-config.sh', 't1301-shared-repo.sh', diff --git a/t/t1098-vfs-sparse-index-capability.sh b/t/t1098-vfs-sparse-index-capability.sh new file mode 100755 index 00000000000000..6261fa9250b2e3 --- /dev/null +++ b/t/t1098-vfs-sparse-index-capability.sh @@ -0,0 +1,28 @@ +#!/bin/sh + +test_description='git version advertises the vfs-sparse-index capability + +"git version --build-options" prints "feature: vfs-sparse-index", and +"git version" without the option does not. The line tells a virtual file +system that this build supports a sparse index under +core.virtualFilesystem. t1094, t1095, t1096, and t1097 test that +behavior.' + +. ./test-lib.sh + +test_expect_success 'git version --build-options prints the capability' ' + git version --build-options >out && + test_grep "^feature: vfs-sparse-index$" out +' + +test_expect_success 'git --version --build-options prints the capability' ' + git --version --build-options >out && + test_grep "^feature: vfs-sparse-index$" out +' + +test_expect_success 'git version without --build-options omits the capability' ' + git version >out && + test_grep ! "^feature: vfs-sparse-index$" out +' + +test_done