Sparse Index
Subsystems (2)
cover Sparse Index
This is based on ds/more-index-cleanups also available as GitGitGadget PR #839.
The sparse checkout feature allows users to specify a "populated set" that is smaller than the full list of files at HEAD. Files outside the sparse checkout definition are not present in the working directory, but are still present in the index (and marked with the CE_SKIP_WORKTREE bit).
This means that the working directory has size O(populated), and commands like "git status" or "git checkout" operate using an O(populated) number of filesystem operations. However, parsing the index still operates on the scale of O(HEAD).
This can be particularly jarring if you are merging a small repository with a large monorepo for the purpose of simplifying dependency management. Even if users have nothing more in their working directory than they had before, they suddenly see a significant increase in their "git status" or "git add" times. In these cases, simply parsing the index can be a huge portion of the command time.
This RFC proposes an update to the index formats to allow "sparse directory entries". These entries correspond to directories that are completely excluded from the sparse checkout definition. We can detect that a directory is excluded when using "cone mode" patterns.
Since having directory entries is a radical departure from the existing index format, a new extension "extensions.sparseIndex" is added. Using a sparse index should cause incompatible tools to fail because they do not understand this extension.
The index is a critical data structure, so making such a drastic change must be handled carefully. This RFC does only enough adjustments to demonstrate performance improvements for "git status" and "git add." Other commands should operate identically to before, since the other commands will expand a sparse index into a full index by parsing trees.
WARNING: I'm getting a failure on the FreeBSD build with my sparse-checkout tests. I'm not sure what is causing these failures, but I will explore while we discuss the possibility of the feature as a whole.
Here is an outline for this RFC:
* Patches 1-14: create and test the sparse index format. This is just enough to start writing the format, but all Git commands become slower for using it. This is because everything is guarded to expand to a full index before actually operating on the cache entries.
* Patch 15: This massive patch is actually a bunch of patches squashed together. I have a branch that adds "ensure_full_index()" guards in each necessary file along with some commentary about how the index is being used. This patch is presented here as one big dump because that commentary isn't particularly interesting if the RFC leads to a very different approach.
* Patches 16-27: These changes make enough code "sparse aware" such that "git status" and "git add" start operating in time O(populated) instead of O(HEAD).
Performance numbers are given in patch 27, but repeated somewhat here. The test environment I use has ~2.1 million paths at HEAD, but only 68,000 populated paths given the sparse-checkout I'm using. The sparse index has about 2,000 sparse directory entries.
1. Use the full index. The index size is ~186 MB.
2. Use the sparse index. The index size is ~5.5 MB.
3. Use a commit where HEAD matches the populated set. The full index size
is ~5.3MB.
The third benchmark is included as a theoretical optimum for a repository of the same object database.
First, a clean 'git status' improves from 3.1s to 240ms.
Benchmark #1: full index (git status) Time (mean ± σ): 3.167 s ± 0.036 s [User: 2.006 s, System: 1.078 s] Range (min … max): 3.100 s … 3.208 s 10 runs
Benchmark #2: sparse index (git status) Time (mean ± σ): 239.5 ms ± 8.1 ms [User: 189.4 ms, System: 226.8 ms] Range (min … max): 226.0 ms … 251.9 ms 13 runs
Benchmark #3: small tree (git status) Time (mean ± σ): 195.3 ms ± 4.5 ms [User: 116.5 ms, System: 84.4 ms] Range (min … max): 188.8 ms … 202.8 ms 15 runs
The performance numbers for 'git add .' are much closer to optimal:
Benchmark #1: full index (git add .) Time (mean ± σ): 3.076 s ± 0.022 s [User: 2.065 s, System: 0.943 s] Range (min … max): 3.044 s … 3.116 s 10 runs
Benchmark #2: sparse index (git add .) Time (mean ± σ): 218.0 ms ± 6.6 ms [User: 195.7 ms, System: 206.6 ms] Range (min … max): 209.8 ms … 228.2 ms 13 runs
Benchmark #3: small tree (git add .) Time (mean ± σ): 217.6 ms ± 5.4 ms [User: 131.9 ms, System: 86.7 ms] Range (min … max): 212.1 ms … 228.4 ms 14 runs
I expect that making a sparse index work optimally through the most common Git commands will take a year of effort. During this process, I expect to add a lot of testing infrastructure around the sparse-checkout feature, especially in corner cases. (This RFC focuses on the happy paths of operating only within the sparse cone, but that will change in the future.)
If this general approach is acceptable, then I would follow it with a sequence of patch submissions that follow this approach:
1. Basics of the format. (Patches 1-14)
2. Add additional guards around index interactions (Patch 15, but split appropriately.)
3. Speed up "git status" and "git add" (Patches 16-27)
After those three items that are represented in this RFC, the work starts to parallelize a bit. My basic ideas for moving forward from this point are to do these basic steps:
* Add new index API abstractions where appropriate, make them sparse-aware.
* Add new tests around sparse-checkout corner cases. Ensure the sparse index works correctly.
* For a given builtin, add extra testing for sparse-checkouts then it them sparse-aware.
Here are some specific questions I'm hoping to answer in this RFC period:
1. Are these sparse directory entries an appropriate way to extend the index format?
2. Is extensions.sparseIndex a good way to signal that these entries can exist?
3. Is git sparse-checkout init --cone --sparse-index an appropriate way to toggle the format?
4. Are there specific areas that I should target to harden the index API before I submit this work?
5. Does anyone have a good idea how to test a large portion of the test suite with sparse-index enabled? The problem I see is that most tests don't use sparse-checkout, so the sparse index is identical to the full index. Would it be interesting to enable the test setup to add such "extra" directories during the test setup?
Thanks, -Stolee
Derrick Stolee (27):
sparse-index: add guard to ensure full index
sparse-index: implement ensure_full_index()
t1092: compare sparse-checkout to sparse-index
test-read-cache: print cache entries with --table
test-tool: read-cache --table --no-stat
test-tool: don't force full index
unpack-trees: ensure full index
sparse-checkout: hold pattern list in index
sparse-index: convert from full to sparse
submodule: sparse-index should not collapse links
unpack-trees: allow sparse directories
sparse-index: check index conversion happens
sparse-index: create extension for compatibility
sparse-checkout: toggle sparse index from builtin [RFC-VERSION] *: ensure full index
unpack-trees: make sparse aware
dir.c: accept a directory as part of cone-mode patterns
status: use sparse-index throughout
status: skip sparse-checkout percentage with sparse-index
sparse-index: expand_to_path() trivial implementation
sparse-index: expand_to_path no-op if path exists
add: allow operating on a sparse-only index
submodule: die_path_inside_submodule is sparse aware
dir: use expand_to_path in add_patterns()
fsmonitor: disable if index is sparse
pathspec: stop calling ensure_full_index
cache-tree: integrate with sparse directory entries
Documentation/config/extensions.txt | 7 + Documentation/git-sparse-checkout.txt | 14 + Makefile | 1 + apply.c | 10 +- blame.c | 7 +- builtin/add.c | 3 + builtin/checkout-index.c | 5 +- builtin/commit.c | 3 + builtin/grep.c | 2 + builtin/ls-files.c | 9 +- builtin/merge-index.c | 2 + builtin/mv.c | 2 + builtin/rm.c | 2 + builtin/sparse-checkout.c | 35 ++- builtin/update-index.c | 2 + cache-tree.c | 21 ++ cache.h | 15 +- diff.c | 2 + dir.c | 19 +- dir.h | 2 +- entry.c | 2 + fsmonitor.c | 18 +- merge-recursive.c | 22 +- name-hash.c | 10 + pathspec.c | 4 +- pathspec.h | 4 +- preload-index.c | 2 + read-cache.c | 51 +++- repo-settings.c | 15 + repository.c | 11 +- repository.h | 3 + rerere.c | 2 + resolve-undo.c | 6 + setup.c | 3 + sha1-name.c | 3 + sparse-index.c | 360 +++++++++++++++++++++++ sparse-index.h | 23 ++ split-index.c | 2 + submodule.c | 22 +- submodule.h | 6 +- t/helper/test-read-cache.c | 77 ++++- t/t1092-sparse-checkout-compatibility.sh | 130 +++++++- tree.c | 2 + unpack-trees.c | 40 ++- wt-status.c | 21 +- wt-status.h | 1 + 46 files changed, 942 insertions(+), 61 deletions(-) create mode 100644 sparse-index.c create mode 100644 sparse-index.h
base-commit: 2271fe7848aa11b30e5313d95d9caebc2937fce5
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-847%2Fderrickstolee%2Fsparse-index%2Frfc-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-847/derrickstolee/sparse-index/rfc-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/847
Signature · 1 line
gitgitgadget
PATCH 1/27 sparse-index: add guard to ensure full index
From: Derrick Stolee <[email protected]>
Upcoming changes will introduce modifications to the index format that allow sparse directories. It will be useful to have a mechanism for converting those sparse index files into full indexes by walking the tree at those sparse directories. Name this method ensure_full_index() as it will guarantee that the index is fully expanded.
This method is not implemented yet, and instead we focus on the scaffolding to declare it and call it at the appropriate time.
Add a 'command_requires_full_index' member to struct repo_settings. This will be an indicator that we need the index in full mode to do certain index operations. This starts as being true for every command, then we will set it to false as some commands integrate with sparse indexes.
If 'command_requires_full_index' is true, then we will immediately expand a sparse index to a full one upon reading from disk. This suffices for now, but we will want to add more callers to ensure_full_index() later.
--- Makefile | 1 + repo-settings.c | 8 ++++++++ repository.c | 11 ++++++++++- repository.h | 2 ++ sparse-index.c | 8 ++++++++ sparse-index.h | 7 +++++++ 6 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 sparse-index.c create mode 100644 sparse-index.h
Makefile +1 -0
@@ -999,6 +999,7 @@ LIB_OBJS += sha1-name.o LIB_OBJS += shallow.o LIB_OBJS += sideband.o LIB_OBJS += sigchain.o +LIB_OBJS += sparse-index.o LIB_OBJS += split-index.o LIB_OBJS += stable-qsort.o LIB_OBJS += strbuf.o
repo-settings.c +8 -0
@@ -77,4 +77,12 @@ void prepare_repo_settings(struct repository *r)UPDATE_DEFAULT_BOOL (r->settings.core_untracked_cache,UNTRACKED_CACHE_KEEP );UPDATE_DEFAULT_BOOL (r->settings.fetch_negotiation_algorithm,FETCH_NEGOTIATION_DEFAULT ); + +/* +* This setting guards all index reads to require a full index +* over a sparse index. After suitable guards are placed in the +* codebase around uses of the index, this setting will be +* removed. +*/ + r->settings.command_requires_full_index =1 ; }
repository.c +10 -1
@@ -10,6 +10,7 @@#include "object.h" #include "lockfile.h" #include "submodule-config.h" +#include "sparse-index.h" /* The main repository */ static struct repository the_repo;
@@ -261,6 +262,8 @@ void repo_clear(struct repository *repo)int repo_read_index (struct repository *repo) { +int res; +if (!repo->index) repo->index =xcalloc (1 ,sizeof (*repo->index));
@@ -270,7 +273,13 @@ int repo_read_index(struct repository *repo)else if (repo->index->repo != repo)BUG ("repo's index should point back at itself" ); -return read_index_from (repo->index, repo->index_file, repo->gitdir); + res =read_index_from (repo->index, repo->index_file, repo->gitdir); + +prepare_repo_settings (repo); +if (repo->settings.command_requires_full_index) +ensure_full_index (repo->index); + +return res; } int repo_hold_locked_index(struct repository *repo,
repository.h +2 -0
@@ -41,6 +41,8 @@ struct repo_settings {enum fetch_negotiation_setting fetch_negotiation_algorithm;int core_multi_pack_index; + + unsigned command_requires_full_index:1; };struct repository {
sparse-index.c new file +8 -0
@@ -0,0 +1,8 @@ +#include "cache.h" +#include "repository.h" +#include "sparse-index.h" + +void ensure_full_index (struct index_state *istate) +{ +/* intentionally left blank */ +}
sparse-index.h new file +7 -0
@@ -0,0 +1,7 @@ +#ifndef SPARSE_INDEX_H__ +#define SPARSE_INDEX_H__ + +struct index_state ; +void ensure_full_index (struct index_state *istate); + +#endif
Signature · 1 line
gitgitgadget
PATCH 2/27 sparse-index: implement ensure_full_index()
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
We will mark an in-memory index_state as having sparse directory entries with the sparse_index bit. These currently cannot exist, but we will add a mechanism for collapsing a full index to a sparse one in a later change. That will happen at write time, so we must first allow parsing the format before writing it.
Commands or methods that require a full index in order to operate can call ensure_full_index() to expand that index in-memory. This requires parsing trees using that index's repository.
Sparse directory entries have a specific 'ce_mode' value. The macro S_ISSPARSEDIR(ce) can check if a cache_entry 'ce' has this type. This ce_mode is not possible with the existing index formats, so we don't also verify all properties of a sparse-directory entry, which are:
1. ce->ce_mode == 01000755
2. ce->flags & CE_SKIP_WORKTREE is true
3. ce->name[ce->namelen - 1] == '/' (ends in dir separator)
4. ce->oid references a tree object.
These are all semi-enforced in ensure_full_index() to some extent. Any deviation will cause a warning at minimum or a failure in the worst case.
--- cache.h | 11 +++++- read-cache.c | 9 +++++ sparse-index.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++- sparse-index.h | 1 + 4 files changed, 113 insertions(+), 2 deletions(-)
cache.h +10 -1
@@ -204,6 +204,10 @@ struct cache_entry {#error "CE_EXTENDED_FLAGS out of range"#endif +#define CE_MODE_SPARSE_DIRECTORY 01000755 +#define SPARSE_DIR_MODE 0100 +#define S_ISSPARSEDIR (m) ((m)->ce_mode == CE_MODE_SPARSE_DIRECTORY) +/* Forward structure decls */ struct pathspec ;struct child_process ;
@@ -249,6 +253,8 @@ static inline unsigned int create_ce_mode(unsigned int mode) {if (S_ISLNK (mode))return S_IFLNK ; +if (mode ==SPARSE_DIR_MODE ) +return CE_MODE_SPARSE_DIRECTORY ;if (S_ISDIR (mode) ||S_ISGITLINK (mode))return S_IFGITLINK ;return S_IFREG |ce_permissions (mode);
@@ -319,7 +325,8 @@ struct index_state { drop_cache_tree : 1, updated_workdir : 1, updated_skipworktree : 1, - fsmonitor_has_run_once : 1; + fsmonitor_has_run_once : 1, + sparse_index : 1;struct hashmap name_hash;struct hashmap dir_hash;struct object_id oid;
@@ -721,6 +728,8 @@ int read_index_from(struct index_state *, const char *path, const char *gitdir);int is_index_unborn (struct index_state *); +void ensure_full_index (struct index_state *istate); +/* For use with `write_locked_index()`. */ #define COMMIT_LOCK (1 << 0)#define SKIP_IF_UNCHANGED (1 << 1)
read-cache.c +9 -0
@@ -101,6 +101,9 @@ static const char *alternate_index_output;static void set_index_entry (struct index_state *istate,int nr,struct cache_entry *ce) { +if (S_ISSPARSEDIR (ce)) + istate->sparse_index =1 ; + istate->cache[nr] = ce;add_name_hash (istate, ce); }
@@ -2255,6 +2258,12 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)trace2_data_intmax ("index" , the_repository,"read/cache_nr" , istate->cache_nr); +if (!istate->repo) + istate->repo = the_repository; +prepare_repo_settings (istate->repo); +if (istate->repo->settings.command_requires_full_index) +ensure_full_index (istate); +return istate->cache_nr; unmap:
sparse-index.c +93 -1
101 lines not on this page · raw message
sparse-index.h +1 -0
@@ -3,5 +3,6 @@struct index_state ;void ensure_full_index (struct index_state *istate); +int convert_to_sparse (struct index_state *istate);#endif
Signature · 1 line
gitgitgadget
Re: [PATCH 02/27] sparse-index: implement ensure_full_index()
Quoted 13 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
We will mark an in-memory
index_stateas having sparse directory entries with thesparse_indexbit. These currently cannot exist, but we will add a mechanism for collapsing a full index to a sparse one in a later change. That will happen at write time, so we must first allow parsing the format before writing it.Commands or methods that require a full index in order to operate can call
ensure_full_index()to expand that index in-memory. This requires parsing trees using that index's repository.
Quoted 6 lines
Sparse directory entries have a specific '
ce_mode' value. The macroS_ISSPARSEDIR(ce)can check if acache_entry'ce' has this type. Thisce_modeis not possible with the existing index formats, so we don't also verify all properties of a sparse-directory entry, which are:1.
ce->ce_mode== 01000755
This is a weird number. What's the reason for choosing it? It looks deceptively close to 0100755, normal executable files, but has the extra 0, meaning that ce->ce_mode & S_IFMT is 0, suggesting it has no file type.
Since it's a directory, why not use S_IFDIR (040000)?
(GITLINK does use the weird 0160000 value, but it happens to be S_IFLNK | S_IFDIR == 0120000 | 040000, which conveys "it's both a directory and a symlink")
Quoted 1 line
2.
ce->flags&CE_SKIP_WORKTREEis true
Makes sense.
Quoted 1 line
3. ce->name[ce->namelen - 1] == '/' (ends in dir separator)
Is there a particular reason for this? I'm used to seeing names without the trailing slash, both in the index and in tree objects. I don't know enough to be for or against this idea; just curious at this point.
Quoted 1 line
4.
ce->oidreferences a tree object.
Makes sense...but doesn't that suggest we'd want to use ce->ce_mode = 040000?
Also, as a bit of a side comment: There have been other requests in the past to support directory objects in the index. The only use I remember for them requested from others was to allow tracking empty directories. However, I've long wanted to introduce a new "blobtree" object to git, so that a user can "git add" some big binary file, but internally git splits the binary file up and stores it as multiple blobs within git plus a new "blobtree" object that references all the individual blobs with enough information about how to stitch all the blobs together to get the original file. I had a few different forms of "blobtree" things that I was interested in. I think brian once suggested some similar idea.
Quoted 17 lines
These are all semi-enforced in
ensure_full_index()to some extent. Any deviation will cause a warning at minimum or a failure in the worst case.Signed-off-by Derrick Stolee <[email protected]>--- cache.h | 11 +++++- read-cache.c | 9 +++++ sparse-index.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++- sparse-index.h | 1 + 4 files changed, 113 insertions(+), 2 deletions(-)cache.h +0
-0
cache.h struct cache_entry { · line 208
Quoted 5 lines
cache.h +2
-0@@ -204,6 +204,10 @@ struct cache_entry {#error "CE_EXTENDED_FLAGS out of range"#endif +#define CE_MODE_SPARSE_DIRECTORY 01000755 +#define SPARSE_DIR_MODE 0100
Another magic value. Feels like the commit message should reference this one and why it was picked. Seems odd to me, and possibly problematic to re-use file permission bits that might collide with files recorded by really old versions of git. Maybe that's not a concern, though.
cache.h struct cache_entry { · line 209
Quoted 1 line
cache.h +1
-0+#define S_ISSPARSEDIR (m) ((m)->ce_mode == CE_MODE_SPARSE_DIRECTORY)
Should the special sauce apply to ce_flags rather than ce_mode? Thus, instead of an S_ISSPARSEDIR, perhaps have a ce_sparse_dir macro (similar to ce_skip_worktree) based on a CE_SPARSE_DIR value (similar to CE_SKIP_WORKTREE)?
Or, alternatively, do we need a single special state here? Could we check for a combination of ce_mode == 040000 && ce_skip_worktree(ce)?
Quoted 148 lines
cache.h +7
-1+/* Forward structure decls */ struct pathspec ;struct child_process ;@@ -249,6 +253,8 @@ static inline unsigned int create_ce_mode(unsigned int mode) {if (S_ISLNK (mode))return S_IFLNK ; +if (mode ==SPARSE_DIR_MODE ) +return CE_MODE_SPARSE_DIRECTORY ;if (S_ISDIR (mode) ||S_ISGITLINK (mode))return S_IFGITLINK ;return S_IFREG |ce_permissions (mode);@@ -319,7 +325,8 @@ struct index_state { drop_cache_tree : 1, updated_workdir : 1, updated_skipworktree : 1, - fsmonitor_has_run_once : 1; + fsmonitor_has_run_once : 1, + sparse_index : 1;struct hashmap name_hash;struct hashmap dir_hash;struct object_id oid;@@ -721,6 +728,8 @@ int read_index_from(struct index_state *, const char *path, const char *gitdir);int is_index_unborn (struct index_state *); +void ensure_full_index (struct index_state *istate); +/* For use with `write_locked_index()`. */ #define COMMIT_LOCK (1 << 0)#define SKIP_IF_UNCHANGED (1 << 1)read-cache.c +9
-0@@ -101,6 +101,9 @@ static const char *alternate_index_output;static void set_index_entry (struct index_state *istate,int nr,struct cache_entry *ce) { +if (S_ISSPARSEDIR (ce)) + istate->sparse_index =1 ; + istate->cache[nr] = ce;add_name_hash (istate, ce); }@@ -2255,6 +2258,12 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)trace2_data_intmax ("index" , the_repository,"read/cache_nr" , istate->cache_nr); +if (!istate->repo) + istate->repo = the_repository; +prepare_repo_settings (istate->repo); +if (istate->repo->settings.command_requires_full_index) +ensure_full_index (istate); +return istate->cache_nr; unmap:sparse-index.c +77
-1@@ -1,8 +1,100 @@#include "cache.h" #include "repository.h" #include "sparse-index.h" +#include "tree.h" +#include "pathspec.h" +#include "trace2.h" + +static void set_index_entry (struct index_state *istate,int nr,struct cache_entry *ce) +{ +ALLOC_GROW (istate->cache, nr +1 , istate->cache_alloc); + + istate->cache[nr] = ce; +add_name_hash (istate, ce); +} + +static int add_path_to_index (const struct object_id *oid, +struct strbuf *base,const char *path, +unsigned int mode,int stage,void *context) +{ +struct index_state *istate = (struct index_state *)context; +struct cache_entry *ce; +size_t len = base->len; + +if (S_ISDIR (mode)) +return READ_TREE_RECURSIVE ; + +strbuf_addstr (base, path); + + ce =make_cache_entry (istate, mode, oid, base->buf,0 ,0 ); + ce->ce_flags |=CE_SKIP_WORKTREE ; +set_index_entry (istate, istate->cache_nr++, ce); + +strbuf_setlen (base, len); +return 0 ; +}void ensure_full_index (struct index_state *istate) { -/* intentionally left blank */ +int i; +struct index_state *full; + +if (!istate || !istate->sparse_index) +return ; + +if (!istate->repo) + istate->repo = the_repository; + +trace2_region_enter ("index" ,"ensure_full_index" , istate->repo); + +/* initialize basics of new index */ + full =xcalloc (1 ,sizeof (struct index_state )); +memcpy (full, istate,sizeof (struct index_state )); + +/* then change the necessary things */ + full->sparse_index =0 ; + full->cache_alloc = (3 * istate->cache_alloc) /2 ; + full->cache_nr =0 ; +ALLOC_ARRAY (full->cache, full->cache_alloc); + +for (i =0 ; i < istate->cache_nr; i++) { +struct cache_entry *ce = istate->cache[i]; +struct tree *tree; +struct pathspec ps; + +if (!S_ISSPARSEDIR (ce)) { +set_index_entry (full, full->cache_nr++, ce); +continue ; + } +if (!(ce->ce_flags &CE_SKIP_WORKTREE )) +warning (_ ("index entry is a directory, but not sparse (%08x)" ), + ce->ce_flags); + +/* recursively walk into cd->name */ + tree =lookup_tree (istate->repo, &ce->oid); + +memset (&ps,0 ,sizeof (ps)); + ps.recursive =1 ; + ps.has_wildcard =1 ; + ps.max_depth =-1 ; + + read_tree_recursive(istate->repo, tree, + ce->name, strlen(ce->name), + 0, &ps,
sparse-index.c line 93
Quoted 10 lines
sparse-index.c +10
-0+ add_path_to_index, full); + +/* free directory entries. full entries are re-used */ +discard_cache_entry (ce); + } + +/* Copy back into original index. */ +memcpy (&istate->name_hash, &full->name_hash,sizeof (full->name_hash)); + istate->sparse_index =0 ; + istate->cache = full->cache;
Haven't you leaked the original istate->cache here?
Quoted 12 lines
sparse-index.c +6
-0+ istate->cache_nr = full->cache_nr; + istate->cache_alloc = full->cache_alloc; + +free (full); + +trace2_region_leave ("index" ,"ensure_full_index" , istate->repo); }sparse-index.h +0
-0
sparse-index.h line 6
Quoted 4 lines
sparse-index.h +1
-0@@ -3,5 +3,6 @@struct index_state ;void ensure_full_index (struct index_state *istate); +int convert_to_sparse (struct index_state *istate);
Seems logically that you'd add one, but was this meant to be included in a later patch?
Quoted 6 lines
sparse-index.h +0
-1#endif --gitgitgadget
Re: [PATCH 02/27] sparse-index: implement ensure_full_index()
Quoted 2 lines · Elijah Newren
On Mon, Jan 25, 2021 at 9:42 AM Derrick Stolee via GitGitGadget <[email protected]> wrote:
...
Quoted 12 lines
Quoted 6 lines
Sparse directory entries have a specific '
ce_mode' value. The macroS_ISSPARSEDIR(ce)can check if acache_entry'ce' has this type. Thisce_modeis not possible with the existing index formats, so we don't also verify all properties of a sparse-directory entry, which are:1.
ce->ce_mode== 01000755This is a weird number. What's the reason for choosing it? It looks deceptively close to 0100755, normal executable files, but has the extra 0, meaning that
ce->ce_mode&S_IFMTis 0, suggesting it has no file type.
Quoted 5 lines
Since it's a directory, why not use
S_IFDIR(040000)?(GITLINK does use the weird 0160000 value, but it happens to be
S_IFLNK|S_IFDIR== 0120000 | 040000, which conveys "it's both a directory and a symlink")
I forget how exactly I came up with these magic constants, but then completely forgot to think of them critically because I haven't had to look at them in a while. They _are_ important, especially because these values affect the file format itself.
I'll think harder on this before submitting a series intended for merging.
Quoted 10 lines
Quoted 1 line
2.
ce->flags&CE_SKIP_WORKTREEis trueMakes sense.
Quoted 1 line
3. ce->name[ce->namelen - 1] == '/' (ends in dir separator)
Is there a particular reason for this? I'm used to seeing names without the trailing slash, both in the index and in tree objects. I don't know enough to be for or against this idea; just curious at this point.
It's yet another way to distinguish directories from files, but there are cases where we do string searches up to a prefix, and having these directory separators did help, IIRC.
Quoted 3 lines
Quoted 1 line
4.
ce->oidreferences a tree object.Makes sense...but doesn't that suggest we'd want to use
ce->ce_mode= 040000?
...
Quoted 11 lines
cache.h struct cache_entry { · line 208
Quoted 2 lines
cache.h +2
-0+#define CE_MODE_SPARSE_DIRECTORY 01000755 +#define SPARSE_DIR_MODE 0100Another magic value. Feels like the commit message should reference this one and why it was picked. Seems odd to me, and possibly problematic to re-use file permission bits that might collide with files recorded by really old versions of git. Maybe that's not a concern, though.
Quoted 1 line
+#define
S_ISSPARSEDIR(m)((m)->ce_mode ==CE_MODE_SPARSE_DIRECTORY)
Quoted 7 lines
Should the special sauce apply to
ce_flagsrather thance_mode? Thus, instead of anS_ISSPARSEDIR, perhaps have ace_sparse_dirmacro (similar toce_skip_worktree) based on aCE_SPARSE_DIRvalue (similar toCE_SKIP_WORKTREE)?Or, alternatively, do we need a single special state here? Could we check for a combination of
ce_mode== 040000 &&ce_skip_worktree(ce)?
The intention was that ce_mode be a unique value that could only be assigned to a directory entry, which would then by necessity be sparse. Checking both ce_mode and ce_flags seemed wasteful with the given assumptions
...
Quoted 6 lines
sparse-index.c line 93
Quoted 4 lines
sparse-index.c +4
-0+/* Copy back into original index. */ +memcpy (&istate->name_hash, &full->name_hash,sizeof (full->name_hash)); + istate->sparse_index =0 ; + istate->cache = full->cache;Haven't you leaked the original
istate->cachehere?
Yes, seems so. Will fix.
Thanks,
-Stolee
Re: [PATCH 02/27] sparse-index: implement ensure_full_index()
Quoted 68 lines · Derrick Stolee
Quoted 2 lines · Elijah Newren
On Mon, Jan 25, 2021 at 9:42 AM Derrick Stolee via GitGitGadget <[email protected]> wrote:
...
Quoted 12 lines
Quoted 6 lines
Sparse directory entries have a specific '
ce_mode' value. The macroS_ISSPARSEDIR(ce)can check if acache_entry'ce' has this type. Thisce_modeis not possible with the existing index formats, so we don't also verify all properties of a sparse-directory entry, which are:1.
ce->ce_mode== 01000755This is a weird number. What's the reason for choosing it? It looks deceptively close to 0100755, normal executable files, but has the extra 0, meaning that
ce->ce_mode&S_IFMTis 0, suggesting it has no file type.Quoted 5 lines
Since it's a directory, why not use
S_IFDIR(040000)?(GITLINK does use the weird 0160000 value, but it happens to be
S_IFLNK|S_IFDIR== 0120000 | 040000, which conveys "it's both a directory and a symlink")I forget how exactly I came up with these magic constants, but then completely forgot to think of them critically because I haven't had to look at them in a while. They _are_ important, especially because these values affect the file format itself.
I'll think harder on this before submitting a series intended for merging.
Quoted 10 lines
Quoted 1 line
2.
ce->flags&CE_SKIP_WORKTREEis trueMakes sense.
Quoted 1 line
3. ce->name[ce->namelen - 1] == '/' (ends in dir separator)
Is there a particular reason for this? I'm used to seeing names without the trailing slash, both in the index and in tree objects. I don't know enough to be for or against this idea; just curious at this point.
It's yet another way to distinguish directories from files, but there are cases where we do string searches up to a prefix, and having these directory separators did help, IIRC.
Quoted 3 lines
Quoted 1 line
4.
ce->oidreferences a tree object.Makes sense...but doesn't that suggest we'd want to use
ce->ce_mode= 040000?...
Quoted 16 lines
cache.h struct cache_entry { · line 208
Quoted 2 lines
cache.h +2
-0+#define CE_MODE_SPARSE_DIRECTORY 01000755 +#define SPARSE_DIR_MODE 0100Another magic value. Feels like the commit message should reference this one and why it was picked. Seems odd to me, and possibly problematic to re-use file permission bits that might collide with files recorded by really old versions of git. Maybe that's not a concern, though.
Quoted 1 line
+#define
S_ISSPARSEDIR(m)((m)->ce_mode ==CE_MODE_SPARSE_DIRECTORY)Should the special sauce apply to
ce_flagsrather thance_mode? Thus, instead of anS_ISSPARSEDIR, perhaps have ace_sparse_dirmacro (similar toce_skip_worktree) based on aCE_SPARSE_DIRvalue (similar toCE_SKIP_WORKTREE)?
Quoted 7 lines
Quoted 2 lines
Or, alternatively, do we need a single special state here? Could we check for a combination of
ce_mode== 040000 &&ce_skip_worktree(ce)?The intention was that
ce_modebe a unique value that could only be assigned to a directory entry, which would then by necessity be sparse. Checking bothce_modeandce_flagsseemed wasteful with the given assumptions
040000 is a unique value that could only be assigned to a directory entry. Since we have no other uses of directories within the index, you are right, we wouldn't need to check ce_skip_worktree(ce) as well; just a check for the 040000 mode would be enough.
Quoted 13 lines
...
Quoted 6 lines
sparse-index.c line 93
Quoted 4 lines
sparse-index.c +4
-0+/* Copy back into original index. */ +memcpy (&istate->name_hash, &full->name_hash,sizeof (full->name_hash)); + istate->sparse_index =0 ; + istate->cache = full->cache;Haven't you leaked the original
istate->cachehere?Yes, seems so. Will fix.
Thanks,
-Stolee
Re: [PATCH 02/27] sparse-index: implement ensure_full_index()
In reply to Elijah NewrenQuoted 7 lines · Elijah Newren
Quoted 1 line
1.
ce->ce_mode== 01000755This is a weird number. What's the reason for choosing it? It looks deceptively close to 0100755, normal executable files, but has the extra 0, meaning that
ce->ce_mode&S_IFMTis 0, suggesting it has no file type.
Quoted 5 lines
Since it's a directory, why not use
S_IFDIR(040000)?(GITLINK does use the weird 0160000 value, but it happens to be
S_IFLNK|S_IFDIR== 0120000 | 040000, which conveys "it's both a directory and a symlink")
Yes, that combination of IFLNK/IFDIR was the reason why we use the value. I tend to think IFDIR is the best thing to use here.
PATCH 3/27 t1092: compare sparse-checkout to sparse-index
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
Add a new 'sparse-index' repo alongside the 'full-checkout' and 'sparse-checkout' repos in t1092-sparse-checkout-compatibility.sh. Also add run_on_sparse and test_sparse_match helpers. These helpers will be used when the sparse index is implemented.
--- t/t1092-sparse-checkout-compatibility.sh | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)
t/t1092-sparse-checkout-compatibility.sh +25 -4
@@ -7,6 +7,7 @@ test_description='compare full workdir to sparse workdir' test_expect_success 'setup' ' git init initial-repo && ( + (GIT_TEST_SPARSE_INDEX=0 && export GIT_TEST_SPARSE_INDEX) && cd initial-repo && echo a >a && echo "after deep" >e &&
@@ -87,23 +88,32 @@ init_repos () { cp -r initial-repo sparse-checkout && git -C sparse-checkout reset --hard && - git -C sparse-checkout sparse-checkout init --cone && + + cp -r initial-repo sparse-index && + git -C sparse-index reset --hard && # initialize sparse-checkout definitions - git -C sparse-checkout sparse-checkout set deep + git -C sparse-checkout sparse-checkout init --cone && + git -C sparse-checkout sparse-checkout set deep && + GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout init --cone && + GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set deep } run_on_sparse () { ( cd sparse-checkout && - $* >../sparse-checkout-out 2>../sparse-checkout-err + GIT_TEST_SPARSE_INDEX=0 $* >../sparse-checkout-out 2>../sparse-checkout-err + ) && + ( + cd sparse-index && + $* >../sparse-index-out 2>../sparse-index-err ) } run_on_all () { ( cd full-checkout && - $* >../full-checkout-out 2>../full-checkout-err + GIT_TEST_SPARSE_INDEX=0 $* >../full-checkout-out 2>../full-checkout-err ) && run_on_sparse $* }
@@ -114,6 +124,17 @@ test_all_match () { test_cmp full-checkout-err sparse-checkout-err } +test_sparse_match () { + run_on_sparse $* && + test_cmp sparse-checkout-out sparse-index-out && + test_cmp sparse-checkout-err sparse-index-err +} + +test_expect_success 'expanded in-memory index matches full index' ' + init_repos && + test_sparse_match test-tool read-cache --expand --table-no-stat +' + test_expect_success 'status with options' ' init_repos && test_all_match git status --porcelain=v2 &&
Signature · 1 line
gitgitgadget
Re: [PATCH 03/27] t1092: compare sparse-checkout to sparse-index
Quoted 18 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
Add a new 'sparse-index' repo alongside the 'full-checkout' and 'sparse-checkout' repos in
t1092-sparse-checkout-compatibility.sh. Also addrun_on_sparseandtest_sparse_matchhelpers. These helpers will be used when the sparse index is implemented.Signed-off-by Derrick Stolee <[email protected]>--- t/t1092-sparse-checkout-compatibility.sh | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)t/t1092-sparse-checkout-compatibility.sh +0
-0
t/t1092-sparse-checkout-compatibility.sh test_description='compare full workdir to sparse workdir' · line 10
Quoted 4 lines
t/t1092-sparse-checkout-compatibility.sh +1
-0@@ -7,6 +7,7 @@ test_description='compare full workdir to sparse workdir' test_expect_success 'setup' ' git init initial-repo && ( + (GIT_TEST_SPARSE_INDEX=0 && export GIT_TEST_SPARSE_INDEX) &&
I thought parentheses started a subshell; once the subshell ends, wouldn't the setting of GIT_TEST_SPARSE_INDEX be thrown away?
Quoted 61 lines
t/t1092-sparse-checkout-compatibility.sh +24
-5cd initial-repo && echo a >a && echo "after deep" >e &&@@ -87,23 +88,32 @@ init_repos () { cp -r initial-repo sparse-checkout && git -C sparse-checkout reset --hard && - git -C sparse-checkout sparse-checkout init --cone && + + cp -r initial-repo sparse-index && + git -C sparse-index reset --hard && # initialize sparse-checkout definitions - git -C sparse-checkout sparse-checkout set deep + git -C sparse-checkout sparse-checkout init --cone && + git -C sparse-checkout sparse-checkout set deep && + GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout init --cone && + GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set deep } run_on_sparse () { ( cd sparse-checkout && - $* >../sparse-checkout-out 2>../sparse-checkout-err + GIT_TEST_SPARSE_INDEX=0 $* >../sparse-checkout-out 2>../sparse-checkout-err + ) && + ( + cd sparse-index && + $* >../sparse-index-out 2>../sparse-index-err ) } run_on_all () { ( cd full-checkout && - $* >../full-checkout-out 2>../full-checkout-err + GIT_TEST_SPARSE_INDEX=0 $* >../full-checkout-out 2>../full-checkout-err ) && run_on_sparse $* }@@ -114,6 +124,17 @@ test_all_match () { test_cmp full-checkout-err sparse-checkout-err } +test_sparse_match () { + run_on_sparse $* && + test_cmp sparse-checkout-out sparse-index-out && + test_cmp sparse-checkout-err sparse-index-err +} + +test_expect_success 'expanded in-memory index matches full index' ' + init_repos && + test_sparse_match test-tool read-cache --expand --table-no-stat +' + test_expect_success 'status with options' ' init_repos && test_all_match git status --porcelain=v2 && --gitgitgadget
Re: [PATCH 03/27] t1092: compare sparse-checkout to sparse-index
Quoted 15 lines · Elijah Newren
Quoted 13 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
Add a new 'sparse-index' repo alongside the 'full-checkout' and 'sparse-checkout' repos in
t1092-sparse-checkout-compatibility.sh. Also addrun_on_sparseandtest_sparse_matchhelpers. These helpers will be used when the sparse index is implemented.Signed-off-by Derrick Stolee <[email protected]>--- t/t1092-sparse-checkout-compatibility.sh | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)
Quoted 12 lines
t/t1092-sparse-checkout-compatibility.sh test_description='compare full workdir to sparse workdir' · line 10
Quoted 9 lines
t/t1092-sparse-checkout-compatibility.sh +1
-0@@ -7,6 +7,7 @@ test_description='compare full workdir to sparse workdir' test_expect_success 'setup' ' git init initial-repo && ( + (GIT_TEST_SPARSE_INDEX=0 && export GIT_TEST_SPARSE_INDEX) &&I thought parentheses started a subshell; once the subshell ends, wouldn't the setting of
GIT_TEST_SPARSE_INDEXbe thrown away?
I think the "export" specifically pushes the setting out of the first level of subshell. This is the recommendation that comes up if one runs
export GIT_TEST_SPARSE_INDEX=1 &&
inside a test on macOS, since this isn't completely portable.
Thanks,
-Stolee
Re: [PATCH 03/27] t1092: compare sparse-checkout to sparse-index
Quoted 30 lines · Derrick Stolee
Quoted 15 lines · Elijah Newren
Quoted 13 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
Add a new 'sparse-index' repo alongside the 'full-checkout' and 'sparse-checkout' repos in
t1092-sparse-checkout-compatibility.sh. Also addrun_on_sparseandtest_sparse_matchhelpers. These helpers will be used when the sparse index is implemented.Signed-off-by Derrick Stolee <[email protected]>--- t/t1092-sparse-checkout-compatibility.sh | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-)Quoted 12 lines
t/t1092-sparse-checkout-compatibility.sh test_description='compare full workdir to sparse workdir' · line 10
Quoted 9 lines
t/t1092-sparse-checkout-compatibility.sh +1
-0@@ -7,6 +7,7 @@ test_description='compare full workdir to sparse workdir' test_expect_success 'setup' ' git init initial-repo && ( + (GIT_TEST_SPARSE_INDEX=0 && export GIT_TEST_SPARSE_INDEX) &&I thought parentheses started a subshell; once the subshell ends, wouldn't the setting of
GIT_TEST_SPARSE_INDEXbe thrown away?
Quoted 2 lines
I think the "export" specifically pushes the setting out of the first level of subshell. This is the recommendation that comes up
You're having a child process change the environment variables of a parent process? ...without some kind of gdb or other debugger wizardry?
Quoted 5 lines
if one runs
export GIT_TEST_SPARSE_INDEX=1 &&inside a test on macOS, since this isn't completely portable.
Um, I think you meant to run
GIT_TEST_SPARSE_INDEX=0 &&
export GIT_TEST_SPARSE_INDEX &&
in order to avoid the unportable
export GIT_TEST_SPARSE_INDEX=0 &&
because
(GIT_TEST_SPARSE_INDEX=0 &&
export GIT_TEST_SPARSE_INDEX) &&looks like a useless no-op. At least it would be in normal bash; is the test harness doing some special magic with it? In normal bash, the value definitely does NOT survive the subshell; (export just means that subprocesses of the subshell where the environment variable is set will see the value):
$ echo Before: $GIT_TEST_SPARSE_INDEX && (GIT_TEST_SPARSE_INDEX=0 && export GIT_TEST_SPARSE_INDEX) && echo After: $GIT_TEST_SPARSE_INDEX Before: After:
But in contrast, without the parentheses:
$ echo Before: $GIT_TEST_SPARSE_INDEX && GIT_TEST_SPARSE_INDEX=0 && export GIT_TEST_SPARSE_INDEX && echo After: $GIT_TEST_SPARSE_INDEX Before: After: 0
PATCH 4/27 test-read-cache: print cache entries with --table
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
This table is helpful for discovering data in the index to ensure it is being written correctly, especially as we build and test the sparse-index.
To make the option parsing slightly more robust, wrap the string comparisons in a loop adapted from test-dir-iterator.c.
Care must be taken with the final check for the 'cnt' variable. We continue the expectation that the numerical value is the final argument.
--- t/helper/test-read-cache.c | 49 ++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-)
t/helper/test-read-cache.c +44 -5
@@ -2,18 +2,55 @@#include "cache.h" #include "config.h" +static void print_cache_entry (struct cache_entry *ce) +{ +/* stat info */ +printf ("%08x %08x %08x %08x %08x %08x " , + ce->ce_stat_data.sd_ctime.sec, + ce->ce_stat_data.sd_ctime.nsec, + ce->ce_stat_data.sd_mtime.sec, + ce->ce_stat_data.sd_mtime.nsec, + ce->ce_stat_data.sd_dev, + ce->ce_stat_data.sd_ino); + +/* mode in binary */ +printf ("0b%d%d%d%d " , + (ce->ce_mode >>15 ) &1 , + (ce->ce_mode >>14 ) &1 , + (ce->ce_mode >>13 ) &1 , + (ce->ce_mode >>12 ) &1 ); + +/* output permissions? */ +printf ("%04o " , ce->ce_mode &01777 ); + +printf ("%s " ,oid_to_hex (&ce->oid)); + +printf ("%s\n" , ce->name); +} + +static void print_cache (struct index_state *cache) +{ +int i; +for (i =0 ; i < the_index.cache_nr; i++) +print_cache_entry (the_index.cache[i]); +} +int cmd__read_cache (int argc,const char **argv) {int i, cnt =1 ;const char *name =NULL ; +int table =0 ; -if (argc >1 &&skip_prefix (argv[1 ],"--print-and-refresh=" , &name)) { - argc--; - argv++; +for (++argv, --argc; *argv &&starts_with (*argv,"--" ); ++argv, --argc) { +if (skip_prefix (*argv,"--print-and-refresh=" , &name)) +continue ; +if (!strcmp (*argv,"--table" )) { + table =1 ; + } } -if (argc ==2 ) - cnt =strtol (argv[1 ],NULL ,0 ); +if (argc ==1 ) + cnt =strtol (argv[0 ],NULL ,0 );setup_git_directory ();git_config (git_default_config,NULL );for (i =0 ; i < cnt; i++) {
@@ -30,6 +67,8 @@ int cmd__read_cache(int argc, const char **argv) ce_uptodate(the_index.cache[pos]) ? "" : " not");write_file (name,"%d\n" , i); } +if (table) +print_cache (&the_index);discard_cache (); }return 0 ;
Signature · 1 line
gitgitgadget
Re: [PATCH 04/27] test-read-cache: print cache entries with --table
Quoted 26 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
This table is helpful for discovering data in the index to ensure it is being written correctly, especially as we build and test the sparse-index.
To make the option parsing slightly more robust, wrap the string comparisons in a loop adapted from
test-dir-iterator.c.Care must be taken with the final check for the 'cnt' variable. We continue the expectation that the numerical value is the final argument.
Signed-off-by Derrick Stolee <[email protected]>--- t/helper/test-read-cache.c | 49 ++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-)t/helper/test-read-cache.c +0
-0@@ -2,18 +2,55 @@#include "cache.h" #include "config.h"
t/helper/test-read-cache.c line 14
Quoted 10 lines
t/helper/test-read-cache.c +10
-0+static void print_cache_entry (struct cache_entry *ce) +{ +/* stat info */ +printf ("%08x %08x %08x %08x %08x %08x " , + ce->ce_stat_data.sd_ctime.sec, + ce->ce_stat_data.sd_ctime.nsec, + ce->ce_stat_data.sd_mtime.sec, + ce->ce_stat_data.sd_mtime.nsec, + ce->ce_stat_data.sd_dev, + ce->ce_stat_data.sd_ino);
Printing sec & nsec in hexidecimal? Why?
Also, if they'll be displayed in hex, do you want to format them as 0x%08x, similar to what you do with binary below?
t/helper/test-read-cache.c line 16
Quoted 2 lines
t/helper/test-read-cache.c +2
-0+ +/* mode in binary */
This comment feels misleading; I think this is the "S_IFMT portion of mode in binary" not "mode in binary".
t/helper/test-read-cache.c line 21
Quoted 5 lines
t/helper/test-read-cache.c +5
-0+printf ("0b%d%d%d%d " , + (ce->ce_mode >>15 ) &1 , + (ce->ce_mode >>14 ) &1 , + (ce->ce_mode >>13 ) &1 , + (ce->ce_mode >>12 ) &1 );
Why binary? Also, since you defined a special magic constant of 01000755 which utilizes bit 18; how come you aren't including any bits higher than 15?
t/helper/test-read-cache.c line 24
Quoted 2 lines
t/helper/test-read-cache.c +2
-0+/* output permissions? */ +printf ("%04o " , ce->ce_mode &01777 );
01777 instead of 07777 just because we don't have anything using the setuid or setgid bits? But if it's based on non-use, then we don't use the sticky bit (01000) either, so this could be just 0777.
Also, if you're using 0b for binary to distinguish and you're clearly using multiple bases in this code, perhaps use a print format of 0o%04o (or 0o%03o if you only use a mask of 0777).
Quoted 48 lines
t/helper/test-read-cache.c +23
-6+printf ("%s " ,oid_to_hex (&ce->oid)); + +printf ("%s\n" , ce->name); +} + +static void print_cache (struct index_state *cache) +{ +int i; +for (i =0 ; i < the_index.cache_nr; i++) +print_cache_entry (the_index.cache[i]); +} +int cmd__read_cache (int argc,const char **argv) {int i, cnt =1 ;const char *name =NULL ; +int table =0 ; -if (argc >1 &&skip_prefix (argv[1 ],"--print-and-refresh=" , &name)) { - argc--; - argv++; +for (++argv, --argc; *argv &&starts_with (*argv,"--" ); ++argv, --argc) { +if (skip_prefix (*argv,"--print-and-refresh=" , &name)) +continue ; +if (!strcmp (*argv,"--table" )) { + table =1 ; + } } -if (argc ==2 ) - cnt =strtol (argv[1 ],NULL ,0 ); +if (argc ==1 ) + cnt =strtol (argv[0 ],NULL ,0 );setup_git_directory ();git_config (git_default_config,NULL );for (i =0 ; i < cnt; i++) {@@ -30,6 +67,8 @@ int cmd__read_cache(int argc, const char **argv) ce_uptodate(the_index.cache[pos]) ? "" : " not");write_file (name,"%d\n" , i); } +if (table) +print_cache (&the_index);discard_cache (); }return 0 ; --gitgitgadget
PATCH 5/27 test-tool: read-cache --table --no-stat
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
The 'test-tool read-cache --table' output is helpful to understand the full contents of the index entries on-disk. This is particularly helpful when trying to diagnose issues with a real repository example.
However, for test cases we might want to compare the index contents of two repositories that were updated in similar ways, but will not actually share the same stat data. Add the '--no-stat' option to remove the timestamps and other stat data from the output. This allows us to compare index contents directly.
--- t/helper/test-read-cache.c | 44 ++++++++++++++---------- t/t1092-sparse-checkout-compatibility.sh | 2 +- 2 files changed, 26 insertions(+), 20 deletions(-)
t/helper/test-read-cache.c +25 -19
@@ -2,16 +2,18 @@#include "cache.h" #include "config.h" -static void print_cache_entry (struct cache_entry *ce) +static void print_cache_entry (struct cache_entry *ce,unsigned stat) { -/* stat info */ -printf ("%08x %08x %08x %08x %08x %08x " , - ce->ce_stat_data.sd_ctime.sec, - ce->ce_stat_data.sd_ctime.nsec, - ce->ce_stat_data.sd_mtime.sec, - ce->ce_stat_data.sd_mtime.nsec, - ce->ce_stat_data.sd_dev, - ce->ce_stat_data.sd_ino); +if (stat) { +/* stat info */ +printf ("%08x %08x %08x %08x %08x %08x " , + ce->ce_stat_data.sd_ctime.sec, + ce->ce_stat_data.sd_ctime.nsec, + ce->ce_stat_data.sd_mtime.sec, + ce->ce_stat_data.sd_mtime.nsec, + ce->ce_stat_data.sd_dev, + ce->ce_stat_data.sd_ino); + }/* mode in binary */ printf("0b%d%d%d%d ",
@@ -28,48 +30,52 @@ static void print_cache_entry(struct cache_entry *ce)printf ("%s\n" , ce->name); } -static void print_cache (struct index_state *cache) +static void print_cache (struct index_state *cache,unsigned stat) {int i;for (i =0 ; i < the_index.cache_nr; i++) -print_cache_entry (the_index.cache[i]); +print_cache_entry (the_index.cache[i], stat); }int cmd__read_cache (int argc,const char **argv) { +struct repository *r = the_repository;int i, cnt =1 ;const char *name =NULL ;int table =0 ; +int stat =1 ;for (++argv, --argc; *argv &&starts_with (*argv,"--" ); ++argv, --argc) {if (skip_prefix (*argv,"--print-and-refresh=" , &name))continue ; -if (!strcmp (*argv,"--table" )) { +if (!strcmp (*argv,"--table" )) table =1 ; - } +else if (!strcmp (*argv,"--no-stat" )) + stat =0 ; }if (argc ==1 ) cnt =strtol (argv[0 ],NULL ,0 );setup_git_directory ();git_config (git_default_config,NULL ); +for (i =0 ; i < cnt; i++) { -read_cache (); +repo_read_index (r);if (name) {int pos; -refresh_index (&the_index,REFRESH_QUIET , +refresh_index (r->index,REFRESH_QUIET ,NULL ,NULL ,NULL ); - pos =index_name_pos (&the_index, name,strlen (name)); + pos =index_name_pos (r->index, name,strlen (name));if (pos <0 )die ("%s not in index" , name);printf ("%s is%s up to date\n" , name, -ce_uptodate (the_index.cache[pos]) ?"" :" not" ); +ce_uptodate (r->index->cache[pos]) ?"" :" not" );write_file (name,"%d\n" , i); }if (table) -print_cache (&the_index); -discard_cache (); +print_cache (r->index, stat); +discard_index (r->index); }return 0 ; }
t/t1092-sparse-checkout-compatibility.sh +1 -1
@@ -132,7 +132,7 @@ test_sparse_match () { test_expect_success 'expanded in-memory index matches full index' ' init_repos && - test_sparse_match test-tool read-cache --expand --table-no-stat + test_sparse_match test-tool read-cache --expand --table --no-stat ' test_expect_success 'status with options' '
Signature · 1 line
gitgitgadget
PATCH 6/27 test-tool: don't force full index
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
We will use 'test-tool read-cache --table' to check that a sparse index is written as part of init_repos. Since we will no longer always expand a sparse index into a full index, add an '--expand' parameter that adds a call to ensure_full_index() so we can compare a sparse index directly against a full index, or at least what the in-memory index looks like when expanded in this way.
--- t/helper/test-read-cache.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
t/helper/test-read-cache.c +12 -0
@@ -1,6 +1,7 @@#include "test-tool.h" #include "cache.h" #include "config.h" +#include "sparse-index.h" static void print_cache_entry (struct cache_entry *ce,unsigned stat) {
@@ -44,6 +45,11 @@ int cmd__read_cache(int argc, const char **argv)const char *name =NULL ;int table =0 ;int stat =1 ; +int expand =0 ; + +initialize_the_repository (); +prepare_repo_settings (r); + r->settings.command_requires_full_index =0 ;for (++argv, --argc; *argv &&starts_with (*argv,"--" ); ++argv, --argc) {if (skip_prefix (*argv,"--print-and-refresh=" , &name))
@@ -52,6 +58,8 @@ int cmd__read_cache(int argc, const char **argv) table =1 ; else if (!strcmp(*argv, "--no-stat")) stat =0 ; + else if (!strcmp(*argv, "--expand")) + expand =1 ; }if (argc ==1 )
@@ -61,6 +69,10 @@ int cmd__read_cache(int argc, const char **argv)for (i =0 ; i < cnt; i++) {repo_read_index (r); + +if (expand) +ensure_full_index (r->index); +if (name) {int pos;
Signature · 1 line
gitgitgadget
PATCH 7/27 unpack-trees: ensure full index
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
The next change will translate full indexes into sparse indexes at write time. The existing logic provides a way for every sparse index to be expanded to a full index at read time. However, there are cases where an index is written and then continues to be used in-memory to perform further updates.
unpack_trees() is frequently called after such a write. In particular, commands like 'git reset' do this double-update of the index.
Ensure that we have a full index when entering unpack_trees(), but only when command_requires_full_index is true. This is always true at the moment, but we will later relax that after unpack_trees() is updated to handle sparse directory entries.
--- unpack-trees.c | 7 +++++++ 1 file changed, 7 insertions(+)
unpack-trees.c +7 -0
@@ -1567,6 +1567,7 @@ static int verify_absent(const struct cache_entry *,*/ int unpack_trees (unsigned len,struct tree_desc *t,struct unpack_trees_options *o) { +struct repository *repo = the_repository;int i, ret;static struct cache_entry *dfc;struct pattern_list pl;
@@ -1578,6 +1579,12 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_optionstrace_performance_enter ();trace2_region_enter ("unpack_trees" ,"unpack_trees" , the_repository); +prepare_repo_settings (repo); +if (repo->settings.command_requires_full_index) { +ensure_full_index (o->src_index); +ensure_full_index (o->dst_index); + } +if (!core_apply_sparse_checkout || !o->update) o->skip_sparse_checkout =1 ;if (!o->skip_sparse_checkout && !o->pl) {
Signature · 1 line
gitgitgadget
Re: [PATCH 07/27] unpack-trees: ensure full index
Quoted 35 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
The next change will translate full indexes into sparse indexes at write time. The existing logic provides a way for every sparse index to be expanded to a full index at read time. However, there are cases where an index is written and then continues to be used in-memory to perform further updates.
unpack_trees()is frequently called after such a write. In particular, commands like 'git reset' do this double-update of the index.Ensure that we have a full index when entering
unpack_trees(), but only whencommand_requires_full_indexis true. This is always true at the moment, but we will later relax that afterunpack_trees()is updated to handle sparse directory entries.Signed-off-by Derrick Stolee <[email protected]>--- unpack-trees.c | 7 +++++++ 1 file changed, 7 insertions(+)unpack-trees.c +1
-0@@ -1567,6 +1567,7 @@ static int verify_absent(const struct cache_entry *,*/ int unpack_trees (unsigned len,struct tree_desc *t,struct unpack_trees_options *o) { +struct repository *repo = the_repository;int i, ret;static struct cache_entry *dfc;struct pattern_list pl;
unpack-trees.c int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options · line 1585
Quoted 7 lines
unpack-trees.c +4
-0@@ -1578,6 +1579,12 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_optionstrace_performance_enter ();trace2_region_enter ("unpack_trees" ,"unpack_trees" , the_repository); +prepare_repo_settings (repo); +if (repo->settings.command_requires_full_index) { +ensure_full_index (o->src_index); +ensure_full_index (o->dst_index);
I was worried about o->result as well, since there is a
memset(&o->result, 0, sizeof(o->result));
followed by manually initializing the relevant fields of the index_state. However, the relevant field here is your new sparse_index bit, and you want that to be 0, i.e. full.
I also checked ensure_full_index() since it is often the case that o->src_index == o->dst_index, but it'll be safe to be called twice on the same index state -- at least as currently written.
So, this patch seems good.
Quoted 7 lines
unpack-trees.c +2
-1+ } +if (!core_apply_sparse_checkout || !o->update) o->skip_sparse_checkout =1 ;if (!o->skip_sparse_checkout && !o->pl) { --gitgitgadget
PATCH 8/27 sparse-checkout: hold pattern list in index
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
As we modify the sparse-checkout definition, we perform index operations on a pattern_list that only exists in-memory. This allows easy backing out in case the index update fails.
However, if the index write itself cares about the sparse-checkout pattern set, we need access to that in-memory copy. Place a pointer to a 'struct pattern_list' in the index so we can access this on-demand. This will be used in the next change which uses the sparse-checkout definition to filter out directories that are outsie the sparse cone.
--- builtin/sparse-checkout.c | 17 ++++++++++------- cache.h | 2 ++ 2 files changed, 12 insertions(+), 7 deletions(-)
builtin/sparse-checkout.c +10 -7
@@ -110,6 +110,8 @@ static int update_working_directory(struct pattern_list *pl)if (is_index_unborn (r->index))return UPDATE_SPARSITY_SUCCESS ; + r->index->sparse_checkout_patterns = pl; +memset (&o,0 ,sizeof (o)); o.verbose_update =isatty (2 ); o.update =1 ;
@@ -138,6 +140,7 @@ static int update_working_directory(struct pattern_list *pl)else rollback_lock_file (&lock_file); + r->index->sparse_checkout_patterns =NULL ;return result; }
@@ -517,19 +520,18 @@ static int modify_pattern_list(int argc, const char **argv, enum modify_type m) {int result;int changed_config =0 ; -struct pattern_list pl; -memset (&pl,0 ,sizeof (pl)); +struct pattern_list *pl =xcalloc (1 ,sizeof (*pl));switch (m) {case ADD :if (core_sparse_checkout_cone) -add_patterns_cone_mode (argc, argv, &pl); +add_patterns_cone_mode (argc, argv, pl);else -add_patterns_literal (argc, argv, &pl); +add_patterns_literal (argc, argv, pl);break ;case REPLACE : -add_patterns_from_input (&pl, argc, argv); +add_patterns_from_input (pl, argc, argv);break ; }
@@ -539,12 +541,13 @@ static int modify_pattern_list(int argc, const char **argv, enum modify_type m) changed_config =1 ; } - result =write_patterns_and_update (&pl); + result =write_patterns_and_update (pl);if (result && changed_config)set_config (MODE_NO_PATTERNS ); -clear_pattern_list (&pl); +clear_pattern_list (pl); +free (pl);return result; }
cache.h +2 -0
@@ -311,6 +311,7 @@ static inline unsigned int canon_mode(unsigned int mode)struct split_index ;struct untracked_cache ;struct progress ; +struct pattern_list ;struct index_state {struct cache_entry **cache;
@@ -336,6 +337,7 @@ struct index_state {struct mem_pool *ce_mem_pool;struct progress *progress;struct repository *repo; +struct pattern_list *sparse_checkout_patterns; };/* Name hashing */
Signature · 1 line
gitgitgadget
Re: [PATCH 08/27] sparse-checkout: hold pattern list in index
Quoted 7 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
As we modify the sparse-checkout definition, we perform index operations on a
pattern_listthat only exists in-memory. This allows easy backing out in case the index update fails.
Quoted 5 lines
However, if the index write itself cares about the sparse-checkout pattern set, we need access to that in-memory copy. Place a pointer to a 'struct
pattern_list' in the index so we can access this on-demand. This will be used in the next change which uses the sparse-checkout definition to filter out directories that are outsie the sparse cone.
s/outsie/outside/
Quoted 89 lines
Signed-off-by Derrick Stolee <[email protected]>--- builtin/sparse-checkout.c | 17 ++++++++++------- cache.h | 2 ++ 2 files changed, 12 insertions(+), 7 deletions(-)builtin/sparse-checkout.c +10
-7@@ -110,6 +110,8 @@ static int update_working_directory(struct pattern_list *pl)if (is_index_unborn (r->index))return UPDATE_SPARSITY_SUCCESS ; + r->index->sparse_checkout_patterns = pl; +memset (&o,0 ,sizeof (o)); o.verbose_update =isatty (2 ); o.update =1 ;@@ -138,6 +140,7 @@ static int update_working_directory(struct pattern_list *pl)else rollback_lock_file (&lock_file); + r->index->sparse_checkout_patterns =NULL ;return result; }@@ -517,19 +520,18 @@ static int modify_pattern_list(int argc, const char **argv, enum modify_type m) {int result;int changed_config =0 ; -struct pattern_list pl; -memset (&pl,0 ,sizeof (pl)); +struct pattern_list *pl =xcalloc (1 ,sizeof (*pl));switch (m) {case ADD :if (core_sparse_checkout_cone) -add_patterns_cone_mode (argc, argv, &pl); +add_patterns_cone_mode (argc, argv, pl);else -add_patterns_literal (argc, argv, &pl); +add_patterns_literal (argc, argv, pl);break ;case REPLACE : -add_patterns_from_input (&pl, argc, argv); +add_patterns_from_input (pl, argc, argv);break ; }@@ -539,12 +541,13 @@ static int modify_pattern_list(int argc, const char **argv, enum modify_type m) changed_config =1 ; } - result =write_patterns_and_update (&pl); + result =write_patterns_and_update (pl);if (result && changed_config)set_config (MODE_NO_PATTERNS ); -clear_pattern_list (&pl); +clear_pattern_list (pl); +free (pl);return result; }cache.h +2
-1@@ -311,6 +311,7 @@ static inline unsigned int canon_mode(unsigned int mode)struct split_index ;struct untracked_cache ;struct progress ; +struct pattern_list ;struct index_state {struct cache_entry **cache;@@ -336,6 +337,7 @@ struct index_state {struct mem_pool *ce_mem_pool;struct progress *progress;struct repository *repo; +struct pattern_list *sparse_checkout_patterns; };/* Name hashing */ --
Quoted 1 line
gitgitgadget
Isn't this the same patch you put in your index cleanup series, or am I getting confused? It looks very familiar.
Re: [PATCH 08/27] sparse-checkout: hold pattern list in index
Quoted 9 lines · Elijah Newren
Quoted 7 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
As we modify the sparse-checkout definition, we perform index operations on a
pattern_listthat only exists in-memory. This allows easy backing out in case the index update fails.
Quoted 7 lines
Quoted 5 lines
However, if the index write itself cares about the sparse-checkout pattern set, we need access to that in-memory copy. Place a pointer to a 'struct
pattern_list' in the index so we can access this on-demand. This will be used in the next change which uses the sparse-checkout definition to filter out directories that are outsie the sparse cone.s/outsie/outside/
Thanks!
Quoted 2 lines
Isn't this the same patch you put in your index cleanup series, or am I getting confused? It looks very familiar.
I removed it from v2 of that series because it didn't do anything of value until we start using the sparse_checkout_patterns member in the next patch of _this_ series.
Thanks,
-Stolee
PATCH 9/27 sparse-index: convert from full to sparse
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
If we have a full index, then we can convert it to a sparse index by replacing directories outside of the sparse cone with sparse directory entries. The convert_to_sparse() method does this, when the situation is appropriate.
For now, we avoid converting the index to a sparse index if:
1. the index is split.
2. the index is already sparse.
3. sparse-checkout is disabled.
4. sparse-checkout does not use cone mode.
Finally, we currently limit the conversion to when the GIT_TEST_SPARSE_INDEX environment variable is enabled. A mode using Git config will be added in a later change.
The trickiest thing about this conversion is that we might not be able to mark a directory as a sparse directory just because it is outside the sparse cone. There might be unmerged files within that directory, so we need to look for those. Also, if there is some strange reason why a file is not marked with CE_SKIP_WORKTREE, then we should give up on converting that directory. There is still hope that some of its subdirectories might be able to convert to sparse, so we keep looking deeper.
The conversion process is assisted by the cache-tree extension. This is calculated from the full index if it does not already exist. We then abandon the cache-tree as it no longer applies to the newly-sparse index. Thus, this cache-tree will be recalculated in every sparse-full-sparse round-trip until we integrate the cache-tree extension with the sparse index.
We can compare the behavior of the sparse-index in t1092-sparse-checkout-compability.sh by using GIT_TEST_SPARSE_INDEX=1 when operating on the 'sparse-index' repo. We can also compare the two sparse repos directly, such as comparing their indexes (when expanded to full in the case of the 'sparse-index' repo). We also verify that the index is actually populated with sparse directory entries.
The 'checkout and reset (mixed)' test is marked for failure when comparing a sparse repo to a full repo, but we can compare the two sparse-checkout cases directly to ensure that we are not changing the behavior when using a sparse index.
--- cache-tree.c | 3 + read-cache.c | 18 ++- sparse-index.c | 139 +++++++++++++++++++++++ t/t1092-sparse-checkout-compatibility.sh | 63 +++++++++- 4 files changed, 218 insertions(+), 5 deletions(-)
cache-tree.c +3 -0
@@ -6,6 +6,7 @@#include "object-store.h" #include "replace-object.h" #include "promisor-remote.h" +#include "sparse-index.h" #ifndef DEBUG_CACHE_TREE #define DEBUG_CACHE_TREE 0
@@ -442,6 +443,8 @@ int cache_tree_update(struct index_state *istate, int flags)if (i)return i; +ensure_full_index (istate); +if (!istate->cache_tree) istate->cache_tree =cache_tree ();
read-cache.c +17 -1
@@ -25,6 +25,7 @@#include "fsmonitor.h" #include "thread-utils.h" #include "progress.h" +#include "sparse-index.h" /* Mask for the name length in ce_flags in the on-disk index */
@@ -1002,8 +1003,15 @@ int verify_path(const char *path, unsigned mode) c = *path++;if ((c =='.' && !verify_dotfile (path, mode)) || -is_dir_sep (c) || c =='\0' ) +is_dir_sep (c))return 0 ; +/* +* allow terminating directory separators for +* sparse directory enries. +*/ +if (c =='\0' ) +return mode ==CE_MODE_SPARSE_DIRECTORY || + mode ==SPARSE_DIR_MODE ; }else if (c =='\\' && protect_ntfs) {if (is_ntfs_dotgit (path))return 0 ;
@@ -3062,6 +3070,13 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l {int ret; + ret =convert_to_sparse (istate); + +if (ret) { +warning (_ ("failed to convert to a sparse-index" )); +return ret; + } +/* * TODO trace2: replace "the_repository" with the actual repo instance * that is associated with the given "istate".
@@ -3165,6 +3180,7 @@ static int write_shared_index(struct index_state *istate,int ret;move_cache_to_base_index (istate); +convert_to_sparse (istate);trace2_region_enter_printf ("index" ,"shared/do_write_index" , the_repository,"%s" , (*temp)->filename.buf);
sparse-index.c +139 -0
145 lines not on this page · raw message
t/t1092-sparse-checkout-compatibility.sh +59 -4
118 lines not on this page · raw message
Signature · 1 line
gitgitgadget
Re: [PATCH 09/27] sparse-index: convert from full to sparse
Quoted 19 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
If we have a full index, then we can convert it to a sparse index by replacing directories outside of the sparse cone with sparse directory entries. The
convert_to_sparse()method does this, when the situation is appropriate.For now, we avoid converting the index to a sparse index if:
1. the index is split.
2. the index is already sparse.
3. sparse-checkout is disabled.
4. sparse-checkout does not use cone mode.Finally, we currently limit the conversion to when the
GIT_TEST_SPARSE_INDEXenvironment variable is enabled. A mode using Git config will be added in a later change.
Quoted 8 lines
The trickiest thing about this conversion is that we might not be able to mark a directory as a sparse directory just because it is outside the sparse cone. There might be unmerged files within that directory, so we need to look for those. Also, if there is some strange reason why a file is not marked with
CE_SKIP_WORKTREE, then we should give up on converting that directory. There is still hope that some of its subdirectories might be able to convert to sparse, so we keep looking deeper.
Oh good, you check for *both* unmerged entries and !CE_SKIP_WORKTREE ones. Very nice.
Quoted 7 lines
The conversion process is assisted by the cache-tree extension. This is calculated from the full index if it does not already exist. We then abandon the cache-tree as it no longer applies to the newly-sparse index. Thus, this cache-tree will be recalculated in every sparse-full-sparse round-trip until we integrate the cache-tree extension with the sparse index.
When going from full to sparse, won't the parts of the cache-tree for paths outside of sparsified directories still be valid? Can't we use those?
Also, when going from sparse to full, can't we just populate the cache-tree as well since we have to read trees to get the individual file entries and we will get all the directory (tree) values at the same time?
Quoted 54 lines
We can compare the behavior of the sparse-index in
t1092-sparse-checkout-compability.shby using GIT_TEST_SPARSE_INDEX=1 when operating on the 'sparse-index' repo. We can also compare the two sparse repos directly, such as comparing their indexes (when expanded to full in the case of the 'sparse-index' repo). We also verify that the index is actually populated with sparse directory entries.The 'checkout and reset (mixed)' test is marked for failure when comparing a sparse repo to a full repo, but we can compare the two sparse-checkout cases directly to ensure that we are not changing the behavior when using a sparse index.
Signed-off-by Derrick Stolee <[email protected]>--- cache-tree.c | 3 + read-cache.c | 18 ++- sparse-index.c | 139 +++++++++++++++++++++++ t/t1092-sparse-checkout-compatibility.sh | 63 +++++++++- 4 files changed, 218 insertions(+), 5 deletions(-)cache-tree.c +3
-0@@ -6,6 +6,7 @@#include "object-store.h" #include "replace-object.h" #include "promisor-remote.h" +#include "sparse-index.h" #ifndef DEBUG_CACHE_TREE #define DEBUG_CACHE_TREE 0@@ -442,6 +443,8 @@ int cache_tree_update(struct index_state *istate, int flags)if (i)return i; +ensure_full_index (istate); +if (!istate->cache_tree) istate->cache_tree =cache_tree ();read-cache.c +1
-0@@ -25,6 +25,7 @@#include "fsmonitor.h" #include "thread-utils.h" #include "progress.h" +#include "sparse-index.h" /* Mask for the name length in ce_flags in the on-disk index */
read-cache.c int verify_path(const char *path, unsigned mode) · line 1010
Quoted 9 lines
read-cache.c +4
-1@@ -1002,8 +1003,15 @@ int verify_path(const char *path, unsigned mode) c = *path++;if ((c =='.' && !verify_dotfile (path, mode)) || -is_dir_sep (c) || c =='\0' ) +is_dir_sep (c))return 0 ; +/* +* allow terminating directory separators for +* sparse directory enries.
s/enries/entries/
read-cache.c int verify_path(const char *path, unsigned mode) · line 1014
Quoted 4 lines
read-cache.c +4
-0+*/ +if (c =='\0' ) +return mode ==CE_MODE_SPARSE_DIRECTORY || + mode ==SPARSE_DIR_MODE ;
Why two values here? I get confused why you have both (which isn't new to this patch; I'm just still confused from when I saw SPARSE_DIR_MODE).
Quoted 66 lines
read-cache.c +8
-0}else if (c =='\\' && protect_ntfs) {if (is_ntfs_dotgit (path))return 0 ;@@ -3062,6 +3070,13 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l {int ret; + ret =convert_to_sparse (istate); + +if (ret) { +warning (_ ("failed to convert to a sparse-index" )); +return ret; + } +/* * TODO trace2: replace "the_repository" with the actual repo instance * that is associated with the given "istate". @@ -3165,6 +3180,7 @@ static int write_shared_index(struct index_state *istate,int ret;move_cache_to_base_index (istate); +convert_to_sparse (istate);trace2_region_enter_printf ("index" ,"shared/do_write_index" , the_repository,"%s" , (*temp)->filename.buf);sparse-index.c +33
-0@@ -4,6 +4,145 @@#include "tree.h" #include "pathspec.h" #include "trace2.h" +#include "cache-tree.h" +#include "config.h" +#include "dir.h" +#include "fsmonitor.h" + +static struct cache_entry *construct_sparse_dir_entry ( +struct index_state *istate, +const char *sparse_dir, +struct cache_tree *tree) +{ +struct cache_entry *de; + + de =make_cache_entry (istate,SPARSE_DIR_MODE , &tree->oid, sparse_dir,0 ,0 ); + + de->ce_flags |=CE_SKIP_WORKTREE ; +return de; +} + +/* +* Returns the number of entries "inserted" into the index. +*/ +static int convert_to_sparse_rec (struct index_state *istate, +int num_converted, +int start,int end, +const char *ct_path,size_t ct_pathlen, +struct cache_tree *ct) +{ +int i, can_convert =1 ; +int start_converted = num_converted; +enum pattern_match_result match; +int dtype; +struct strbuf child_path =STRBUF_INIT ; +struct pattern_list *pl = istate->sparse_checkout_patterns;
sparse-index.c line 49
Quoted 10 lines
sparse-index.c +10
-0+ +/* +* Is the current path outside of the sparse cone? +* Then check if the region can be replaced by a sparse +* directory entry (everything is sparse and merged). +*/ + match =path_matches_pattern_list (ct_path, ct_pathlen, +NULL , &dtype, pl, istate); +if (match !=NOT_MATCHED ) + can_convert =0 ;
I know some people hate gotos, but this seems like one of the cases where a goto jumping after the following for & if would be clearer then setting can_convert to 0.
sparse-index.c line 51
Quoted 2 lines
sparse-index.c +2
-0+ +for (i = start; can_convert && i < end; i++) {
Instead of checking can_convert here...
sparse-index.c line 56
Quoted 5 lines
sparse-index.c +5
-0+struct cache_entry *ce = istate->cache[i]; + +if (ce_stage (ce) || + !(ce->ce_flags &CE_SKIP_WORKTREE )) + can_convert =0 ;
...could you just insert a break here?
Quoted 7 lines
sparse-index.c +7
-0+ } + +if (can_convert) { +struct cache_entry *se; + se =construct_sparse_dir_entry (istate, ct_path, ct); + + istate->cache[num_converted++] = se;
sparse-index.c line 73
Quoted 10 lines
sparse-index.c +10
-0+return 1 ; + } + +for (i = start; i < end; ) { +int count, span, pos =-1 ; +const char *base, *slash; +struct cache_entry *ce = istate->cache[i]; + +/* +* Detect if this is a normal entry oustide of any subtree
s/oustide/outside/
Quoted 14 lines
sparse-index.c +14
-0+* entry. +*/ + base = ce->name + ct_pathlen; + slash =strchr (base,'/' ); + +if (slash) + pos =cache_tree_subtree_pos (ct, base, slash - base); + +if (pos <0 ) { + istate->cache[num_converted++] = ce; + i++; +continue ; + } +
sparse-index.c line 97
Quoted 10 lines
sparse-index.c +10
-0+strbuf_setlen (&child_path,0 ); +strbuf_add (&child_path, ce->name, slash - ce->name +1 ); + + span = ct->down[pos]->cache_tree->entry_count; + count =convert_to_sparse_rec (istate, + num_converted, i, i + span, + child_path.buf, child_path.len, + ct->down[pos]->cache_tree); + num_converted += count; + i += span;
And there's no i++ in the loop itself, so this is good.
Quoted 56 lines
sparse-index.c +48
-0+ } + +strbuf_release (&child_path); +return num_converted - start_converted; +} + +int convert_to_sparse (struct index_state *istate) +{ +if (istate->split_index || istate->sparse_index || + !core_apply_sparse_checkout || !core_sparse_checkout_cone) +return 0 ; + +/* +* For now, only create a sparse index with the +* GIT_TEST_SPARSE_INDEX environment variable. We will relax +* this once we have a proper way to opt-in (and later still, +* opt-out). +*/ +if (!git_env_bool ("GIT_TEST_SPARSE_INDEX" ,0 )) +return 0 ; + +if (!istate->sparse_checkout_patterns) { + istate->sparse_checkout_patterns =xcalloc (1 ,sizeof (struct pattern_list )); +if (get_sparse_checkout_patterns (istate->sparse_checkout_patterns) <0 ) +return 0 ; + } + +if (!istate->sparse_checkout_patterns->use_cone_patterns) { +warning (_ ("attempting to use sparse-index without cone mode" )); +return -1 ; + } + +if (cache_tree_update (istate,0 )) { +warning (_ ("unable to update cache-tree, staying full" )); +return -1 ; + } + +remove_fsmonitor (istate); + +trace2_region_enter ("index" ,"convert_to_sparse" , istate->repo); + istate->cache_nr =convert_to_sparse_rec (istate, +0 ,0 , istate->cache_nr, +"" ,0 , istate->cache_tree); + istate->drop_cache_tree =1 ; + istate->sparse_index =1 ; +trace2_region_leave ("index" ,"convert_to_sparse" , istate->repo); +return 0 ; +}static void set_index_entry (struct index_state *istate,int nr,struct cache_entry *ce) {t/t1092-sparse-checkout-compatibility.sh +0
-0
t/t1092-sparse-checkout-compatibility.sh line 5
Quoted 4 lines
t/t1092-sparse-checkout-compatibility.sh +1
-0@@ -2,6 +2,9 @@ test_description='compare full workdir to sparse workdir' +GIT_TEST_CHECK_CACHE_TREE=0
Why do you need to set this? I vaguely remember needing to mess with this when working with sparse checkouts because it did weird stuff but I don't remember details. But since you patch touches cache_trees, it seems weird to show up without explanation.
Quoted 37 lines
t/t1092-sparse-checkout-compatibility.sh +14
-2+GIT_TEST_SPLIT_INDEX=0 + . ./test-lib.sh test_expect_success 'setup' '@@ -106,7 +109,7 @@ run_on_sparse () { ) && ( cd sparse-index && - $* >../sparse-index-out 2>../sparse-index-err + GIT_TEST_SPARSE_INDEX=1 $* >../sparse-index-out 2>../sparse-index-err ) }@@ -121,7 +124,9 @@ run_on_all () { test_all_match () { run_on_all $* && test_cmp full-checkout-out sparse-checkout-out && - test_cmp full-checkout-err sparse-checkout-err + test_cmp full-checkout-out sparse-index-out && + test_cmp full-checkout-err sparse-checkout-err && + test_cmp full-checkout-err sparse-index-err } test_sparse_match () {@@ -130,6 +135,38 @@ test_sparse_match () { test_cmp sparse-checkout-err sparse-index-err } +test_expect_success 'sparse-index contents' ' + init_repos && + + test-tool -C sparse-index read-cache --table --no-stat >cache && + for dir in folder1 folder2 x + do + TREE=$(git -C sparse-index rev-parse HEAD:$dir) && + grep "0b0000 0755 $TREE $dir/" cache \
t/t1092-sparse-checkout-compatibility.sh test_sparse_match () { · line 155
Quoted 10 lines
t/t1092-sparse-checkout-compatibility.sh +10
-0+ || return 1 + done && + + GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set folder1 && + + test-tool -C sparse-index read-cache --table --no-stat >cache && + for dir in deep folder2 x + do + TREE=$(git -C sparse-index rev-parse HEAD:$dir) && + grep "0b0000 0755 $TREE $dir/" cache \
It would seem clearer to me if this output were to better match git ls-tree -rt ... output (or at least the ' tree ' lines from such output).
Quoted 26 lines
t/t1092-sparse-checkout-compatibility.sh +15
-0+ || return 1 + done && + + GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set deep/deeper1 && + + test-tool -C sparse-index read-cache --table --no-stat >cache && + for dir in deep/deeper2 folder1 folder2 x + do + TREE=$(git -C sparse-index rev-parse HEAD:$dir) && + grep "0b0000 0755 $TREE $dir/" cache \ + || return 1 + done +' + test_expect_success 'expanded in-memory index matches full index' ' init_repos && test_sparse_match test-tool read-cache --expand --table --no-stat@@ -137,6 +174,7 @@ test_expect_success 'expanded in-memory index matches full index' ' test_expect_success 'status with options' ' init_repos && + test_sparse_match ls && test_all_match git status --porcelain=v2 && test_all_match git status --porcelain=v2 -z -u && test_all_match git status --porcelain=v2 -uno &&
t/t1092-sparse-checkout-compatibility.sh test_expect_success 'add, commit, checkout' ' · line 210
Quoted 5 lines
t/t1092-sparse-checkout-compatibility.sh +1
-1@@ -169,7 +207,7 @@ test_expect_success 'add, commit, checkout' ' test_all_match git add -A && test_all_match git status --porcelain=v2 && - test_all_match git commit -m "Extend README.md" && + test_all_match git commit -m "Extend-README.md" &&
Why this change?
Quoted 45 lines
t/t1092-sparse-checkout-compatibility.sh +18
-2test_all_match git checkout HEAD~1 && test_all_match git checkout - &&@@ -273,6 +311,17 @@ test_expect_failure 'checkout and reset (mixed)' ' test_all_match git reset update-folder2 ' +# Ensure that sparse-index behaves identically to +# sparse-checkout with a full index. +test_expect_success 'checkout and reset (mixed) [sparse]' ' + init_repos && + + test_sparse_match git checkout -b reset-test update-deep && + test_sparse_match git reset deepest && + test_sparse_match git reset update-folder1 && + test_sparse_match git reset update-folder2 +' + test_expect_success 'merge' ' init_repos &&@@ -309,14 +358,20 @@ test_expect_success 'clean' ' test_all_match git status --porcelain=v2 && test_all_match git clean -f && test_all_match git status --porcelain=v2 && + test_sparse_match ls && + test_sparse_match ls folder1 && test_all_match git clean -xf && test_all_match git status --porcelain=v2 && + test_sparse_match ls && + test_sparse_match ls folder1 && test_all_match git clean -xdf && test_all_match git status --porcelain=v2 && + test_sparse_match ls && + test_sparse_match ls folder1 && - test_path_is_dir sparse-checkout/folder1 + test_sparse_match test_path_is_dir folder1 ' test_done --gitgitgadget
PATCH 10/27 submodule: sparse-index should not collapse links
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
A submodule is stored as a "Git link" that actually points to a commit within a submodule. Submodules are populated or not depending on submodule configuration, not sparse-checkout. To ensure that the sparse-index feature integrates correctly with submodules, we should not collapse a directory if there is a Git link within its range.
This allows us to remove ensure_full_index() from die_path_inside_submodule() because a sparse-index will not remove the entries for Git links. The loop already 'continue's if the cache entry is not a Git link.
--- sparse-index.c | 1 + 1 file changed, 1 insertion(+)
sparse-index.c +1 -0
@@ -52,6 +52,7 @@ static int convert_to_sparse_rec(struct index_state *istate,struct cache_entry *ce = istate->cache[i];if (ce_stage (ce) || +S_ISGITLINK (ce->ce_mode) || !(ce->ce_flags &CE_SKIP_WORKTREE )) can_convert =0 ; }
Signature · 1 line
gitgitgadget
PATCH 11/27 unpack-trees: allow sparse directories
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
The index_pos_by_traverse_info() currently throws a BUG() when a directory entry exists exactly in the index. We need to consider that it is possible to have a directory in a sparse index as long as that entry is itself marked with the skip-worktree bit.
The negation of the 'pos' variable must be conditioned to only when it starts as negative. This is identical behavior as before when the index is full.
The starts_with() condition matches because our name.buf terminates with a directory separator, just like our sparse directory entries.
--- unpack-trees.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
unpack-trees.c +6 -3
@@ -746,9 +746,12 @@ static int index_pos_by_traverse_info(struct name_entry *names,strbuf_make_traverse_path (&name, info, names->path, names->pathlen);strbuf_addch (&name,'/' ); pos =index_name_pos (o->src_index, name.buf, name.len); -if (pos >=0 ) -BUG ("This is a directory and should not exist in index" ); - pos = -pos -1 ; +if (pos >=0 ) { +if (!o->src_index->sparse_index || + !(o->src_index->cache[pos]->ce_flags &CE_SKIP_WORKTREE )) +BUG ("This is a directory and should not exist in index" ); + }else + pos = -pos -1 ;if (pos >= o->src_index->cache_nr || !starts_with (o->src_index->cache[pos]->name, name.buf) || (pos >0 &&starts_with (o->src_index->cache[pos-1 ]->name, name.buf)))
Signature · 1 line
gitgitgadget
Re: [PATCH 11/27] unpack-trees: allow sparse directories
Quoted 11 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
The
index_pos_by_traverse_info()currently throws aBUG()when a directory entry exists exactly in the index. We need to consider that it is possible to have a directory in a sparse index as long as that entry is itself marked with the skip-worktree bit.The negation of the 'pos' variable must be conditioned to only when it starts as negative. This is identical behavior as before when the index is full.
The first sentence of this paragraph was really hard for me to parse. Reading the code and then reading the sentence I could make sense of it, but I struggled to do so the other way around. Is there some way to reword this? Or is this just a reading comprehension issue on my part? (It might be...)
Quoted 29 lines
The
starts_with()condition matches because our name.buf terminates with a directory separator, just like our sparse directory entries.Signed-off-by Derrick Stolee <[email protected]>--- unpack-trees.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)unpack-trees.c +6
-4@@ -746,9 +746,12 @@ static int index_pos_by_traverse_info(struct name_entry *names,strbuf_make_traverse_path (&name, info, names->path, names->pathlen);strbuf_addch (&name,'/' ); pos =index_name_pos (o->src_index, name.buf, name.len); -if (pos >=0 ) -BUG ("This is a directory and should not exist in index" ); - pos = -pos -1 ; +if (pos >=0 ) { +if (!o->src_index->sparse_index || + !(o->src_index->cache[pos]->ce_flags &CE_SKIP_WORKTREE )) +BUG ("This is a directory and should not exist in index" ); + }else + pos = -pos -1 ;if (pos >= o->src_index->cache_nr || !starts_with (o->src_index->cache[pos]->name, name.buf) || (pos >0 &&starts_with (o->src_index->cache[pos-1 ]->name, name.buf))) --
Quoted 1 line
gitgitgadget
The patch looks pretty straightforward to me.
PATCH 12/27 sparse-index: check index conversion happens
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
Add a test case that uses test_region to ensure that we are truly expanding a sparse index to a full one, then converting back to sparse when writing the index. As we integrate more Git commands with the sparse index, we will convert these commands to check that we do _not_ convert the sparse index to a full index and instead stay sparse the entire time.
--- t/t1092-sparse-checkout-compatibility.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
t/t1092-sparse-checkout-compatibility.sh +17 -0
@@ -374,4 +374,21 @@ test_expect_success 'clean' ' test_sparse_match test_path_is_dir folder1 ' +test_expect_success 'sparse-index is expanded and converted back' ' + init_repos && + + ( + (GIT_TEST_SPARSE_INDEX=1 && export GIT_TEST_SPARSE_INDEX) && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" reset --hard && + test_region index convert_to_sparse trace2.txt && + test_region index ensure_full_index trace2.txt && + + rm trace2.txt && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" status -uno && + test_region index ensure_full_index trace2.txt + ) +' + test_done
Signature · 1 line
gitgitgadget
Re: [PATCH 12/27] sparse-index: check index conversion happens
Quoted 20 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
Add a test case that uses
test_regionto ensure that we are truly expanding a sparse index to a full one, then converting back to sparse when writing the index. As we integrate more Git commands with the sparse index, we will convert these commands to check that we do _not_ convert the sparse index to a full index and instead stay sparse the entire time.Signed-off-by Derrick Stolee <[email protected]>--- t/t1092-sparse-checkout-compatibility.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)t/t1092-sparse-checkout-compatibility.sh +0
-0
t/t1092-sparse-checkout-compatibility.sh test_expect_success 'clean' ' · line 381
Quoted 8 lines
t/t1092-sparse-checkout-compatibility.sh +5
-0@@ -374,4 +374,21 @@ test_expect_success 'clean' ' test_sparse_match test_path_is_dir folder1 ' +test_expect_success 'sparse-index is expanded and converted back' ' + init_repos && + + ( + (GIT_TEST_SPARSE_INDEX=1 && export GIT_TEST_SPARSE_INDEX) &&
Drop the parentheses.
What system are you running on that this test passed for you with those parentheses there? I checked out this particular commit and ran the test -- and it fails for me. Removing the parentheses makes the test pass.
Is there some shell where parentheses only function as grouping, similar to bash's {...}, rather than as a subshell, the way bash handles (...) ?
Quoted 14 lines
t/t1092-sparse-checkout-compatibility.sh +12
-1+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" reset --hard && + test_region index convert_to_sparse trace2.txt && + test_region index ensure_full_index trace2.txt && + + rm trace2.txt && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" status -uno && + test_region index ensure_full_index trace2.txt + ) +' + test_done --
Quoted 1 line
gitgitgadget
Otherwise, I like the test and this commit.
PATCH 13/27 sparse-index: create extension for compatibility
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
Previously, we enabled the sparse index format only using GIT_TEST_SPARSE_INDEX=1. This is not a feasible direction for users to actually select this mode. Further, sparse directory entries are not understood by the index formats as advertised.
We _could_ add a new index version that explicitly adds these capabilities, but there are nuances to index formats 2, 3, and 4 that are still valuable to select as options. For now, create a repo extension, "extensions.sparseIndex", that specifies that the tool reading this repository must understand sparse directory entries.
This change only encodes the extension and enables it when GIT_TEST_SPARSE_INDEX=1. Later, we will add a more user-friendly CLI mechanism.
--- Documentation/config/extensions.txt | 7 ++++++ cache.h | 1 + repo-settings.c | 7 ++++++ repository.h | 3 ++- setup.c | 3 +++ sparse-index.c | 38 +++++++++++++++++++++++++---- 6 files changed, 53 insertions(+), 6 deletions(-)
Documentation/config/extensions.txt +7 -0
@@ -6,3 +6,10 @@ extensions.objectFormat:: Note that this setting should only be set by linkgit:git-init[1] or linkgit:git-clone[1]. Trying to change it after initialization will not work and will produce hard-to-diagnose issues. + +extensions.sparseIndex:: + When combined with `core.sparseCheckout=true` and + `core.sparseCheckoutCone=true`, the index may contain entries + corresponding to directories outside of the sparse-checkout + definition. Versions of Git that do not understand this extension + do not expect directory entries in the index.
cache.h +1 -0
@@ -1054,6 +1054,7 @@ struct repository_format {int worktree_config;int is_bare;int hash_algo; +int sparse_index;char *work_tree;struct string_list unknown_extensions;struct string_list v1_only_extensions;
repo-settings.c +7 -0
@@ -85,4 +85,11 @@ void prepare_repo_settings(struct repository *r)* removed. */ r->settings.command_requires_full_index =1 ; + +/* +* Initialize this as off. +*/ + r->settings.sparse_index =0 ; +if (!repo_config_get_bool (r,"extensions.sparseindex" , &value) && value) + r->settings.sparse_index =1 ; }
repository.h +2 -1
@@ -42,7 +42,8 @@ struct repo_settings {int core_multi_pack_index; - unsigned command_requires_full_index:1; + unsigned command_requires_full_index:1, + sparse_index:1; };struct repository {
setup.c +3 -0
@@ -500,6 +500,9 @@ static enum extension_result handle_extension(const char *var,return error ("invalid value for 'extensions.objectformat'" ); data->hash_algo = format;return EXTENSION_OK ; + }else if (!strcmp (ext,"sparseindex" )) { + data->sparse_index =1 ; +return EXTENSION_OK ; }return EXTENSION_UNKNOWN ; }
sparse-index.c +33 -5
@@ -102,19 +102,47 @@ static int convert_to_sparse_rec(struct index_state *istate,return num_converted - start_converted; } +static int enable_sparse_index (struct repository *repo) +{ +const char *config_path =repo_git_path (repo,"config.worktree" ); + +if (upgrade_repository_format (1 ) <0 ) { +warning (_ ("unable to upgrade repository format to enable sparse-index" )); +return -1 ; + } +git_config_set_in_file_gently (config_path, +"extensions.sparseIndex" , +"true" ); + +prepare_repo_settings (repo); + repo->settings.sparse_index =1 ; +return 0 ; +} +int convert_to_sparse (struct index_state *istate) {if (istate->split_index || istate->sparse_index || !core_apply_sparse_checkout || !core_sparse_checkout_cone)return 0 ; +if (!istate->repo) + istate->repo = the_repository; + +/* +* The GIT_TEST_SPARSE_INDEX environment variable triggers the +* extensions.sparseIndex config variable to be on. +*/ +if (git_env_bool ("GIT_TEST_SPARSE_INDEX" ,0 )) { +int err =enable_sparse_index (istate->repo); +if (err <0 ) +return err; + } +/* -* For now, only create a sparse index with the -* GIT_TEST_SPARSE_INDEX environment variable. We will relax -* this once we have a proper way to opt-in (and later still, -* opt-out). +* Only convert to sparse if extensions.sparseIndex is set. */ -if (!git_env_bool ("GIT_TEST_SPARSE_INDEX" ,0 )) +prepare_repo_settings (istate->repo); +if (!istate->repo->settings.sparse_index)return 0 ;if (!istate->sparse_checkout_patterns) {
Signature · 1 line
gitgitgadget
Re: [PATCH 13/27] sparse-index: create extension for compatibility
Quoted 14 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
Previously, we enabled the sparse index format only using GIT_TEST_SPARSE_INDEX=1. This is not a feasible direction for users to actually select this mode. Further, sparse directory entries are not understood by the index formats as advertised.
We _could_ add a new index version that explicitly adds these capabilities, but there are nuances to index formats 2, 3, and 4 that are still valuable to select as options. For now, create a repo extension, "extensions.sparseIndex", that specifies that the tool reading this repository must understand sparse directory entries.
Quoted 3 lines
This change only encodes the extension and enables it when GIT_TEST_SPARSE_INDEX=1. Later, we will add a more user-friendly CLI mechanism.
One other interesting thing to note is that last I checked, jgit doesn't support index format v4, which makes us unable to use it. Making a v5 would force jgit to support all previous index formats in order to support your new feature.
However, the jgit thing is going to make it hard for me to find other users willing to test out this feature at $DAYJOB. But I don't think there's anyway around that; you need to change the index format. And you at least have jrnieder cc'ed. :-)
Quoted 16 lines
Signed-off-by Derrick Stolee <[email protected]>--- Documentation/config/extensions.txt | 7 ++++++ cache.h | 1 + repo-settings.c | 7 ++++++ repository.h | 3 ++- setup.c | 3 +++ sparse-index.c | 38 +++++++++++++++++++++++++---- 6 files changed, 53 insertions(+), 6 deletions(-)Documentation/config/extensions.txt +0
-0
Documentation/config/extensions.txt extensions.objectFormat:: · line 15
Quoted 10 lines
Documentation/config/extensions.txt +7
-0@@ -6,3 +6,10 @@ extensions.objectFormat:: Note that this setting should only be set by linkgit:git-init[1] or linkgit:git-clone[1]. Trying to change it after initialization will not work and will produce hard-to-diagnose issues. + +extensions.sparseIndex:: + When combined with `core.sparseCheckout=true` and + `core.sparseCheckoutCone=true`, the index may contain entries + corresponding to directories outside of the sparse-checkout + definition. Versions of Git that do not understand this extension + do not expect directory entries in the index.
Perhaps to make this slightly more explicit ("corresponding to" can be fuzzy and be read to assume you are talking about file entries underneath a directory rather than directory entries, so add an extra phrase to rule that out):
...the index may contain entries corresponding to directories outside of the sparse-checkout definition in lieu of containing each path under such directories...
Quoted 115 lines
cache.h +1
-0@@ -1054,6 +1054,7 @@ struct repository_format {int worktree_config;int is_bare;int hash_algo; +int sparse_index;char *work_tree;struct string_list unknown_extensions;struct string_list v1_only_extensions;repo-settings.c +7
-0@@ -85,4 +85,11 @@ void prepare_repo_settings(struct repository *r)* removed. */ r->settings.command_requires_full_index =1 ; + +/* +* Initialize this as off. +*/ + r->settings.sparse_index =0 ; +if (!repo_config_get_bool (r,"extensions.sparseindex" , &value) && value) + r->settings.sparse_index =1 ; }repository.h +2
-1@@ -42,7 +42,8 @@ struct repo_settings {int core_multi_pack_index; - unsigned command_requires_full_index:1; + unsigned command_requires_full_index:1, + sparse_index:1; };struct repository {setup.c +3
-0@@ -500,6 +500,9 @@ static enum extension_result handle_extension(const char *var,return error ("invalid value for 'extensions.objectformat'" ); data->hash_algo = format;return EXTENSION_OK ; + }else if (!strcmp (ext,"sparseindex" )) { + data->sparse_index =1 ; +return EXTENSION_OK ; }return EXTENSION_UNKNOWN ; }sparse-index.c +33
-6@@ -102,19 +102,47 @@ static int convert_to_sparse_rec(struct index_state *istate,return num_converted - start_converted; } +static int enable_sparse_index (struct repository *repo) +{ +const char *config_path =repo_git_path (repo,"config.worktree" ); + +if (upgrade_repository_format (1 ) <0 ) { +warning (_ ("unable to upgrade repository format to enable sparse-index" )); +return -1 ; + } +git_config_set_in_file_gently (config_path, +"extensions.sparseIndex" , +"true" ); + +prepare_repo_settings (repo); + repo->settings.sparse_index =1 ; +return 0 ; +} +int convert_to_sparse (struct index_state *istate) {if (istate->split_index || istate->sparse_index || !core_apply_sparse_checkout || !core_sparse_checkout_cone)return 0 ; +if (!istate->repo) + istate->repo = the_repository; + +/* +* The GIT_TEST_SPARSE_INDEX environment variable triggers the +* extensions.sparseIndex config variable to be on. +*/ +if (git_env_bool ("GIT_TEST_SPARSE_INDEX" ,0 )) { +int err =enable_sparse_index (istate->repo); +if (err <0 ) +return err; + } +/* -* For now, only create a sparse index with the -* GIT_TEST_SPARSE_INDEX environment variable. We will relax -* this once we have a proper way to opt-in (and later still, -* opt-out). +* Only convert to sparse if extensions.sparseIndex is set. */ -if (!git_env_bool ("GIT_TEST_SPARSE_INDEX" ,0 )) +prepare_repo_settings (istate->repo); +if (!istate->repo->settings.sparse_index)return 0 ;if (!istate->sparse_checkout_patterns) { --gitgitgadget
PATCH 14/27 sparse-checkout: toggle sparse index from builtin
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
The sparse index extension is used to signal that index writes should be in sparse mode. This was only updated using GIT_TEST_SPARSE_INDEX=1.
Add a '--[no-]sparse-index' option to 'git sparse-checkout init' that specifies if the sparse index should be used. It also updates the index to use the correct format, either way. Add a warning in the documentation that the use of a repository extension might reduce compatibility with third-party tools. 'git sparse-checkout init' already sets extension.worktreeConfig, which places most sparse-checkout users outside of the scope of most third-party tools.
Update t1092-sparse-checkout-compatibility.sh to use this CLI instead of GIT_TEST_SPARSE_INDEX=1.
--- Documentation/git-sparse-checkout.txt | 14 +++++++++ builtin/sparse-checkout.c | 17 ++++++++++- sparse-index.c | 38 ++++++++++++++++-------- sparse-index.h | 3 ++ t/t1092-sparse-checkout-compatibility.sh | 33 ++++++++++---------- 5 files changed, 75 insertions(+), 30 deletions(-)
Documentation/git-sparse-checkout.txt +14 -0
@@ -45,6 +45,20 @@ To avoid interfering with other worktrees, it first enables the When `--cone` is provided, the `core.sparseCheckoutCone` setting is also set, allowing for better performance with a limited set of patterns (see 'CONE PATTERN SET' below). ++ +Use the `--[no-]sparse-index` option to toggle the use of the sparse +index format. This reduces the size of the index to be more closely +aligned with your sparse-checkout definition. This can have significant +performance advantages for commands such as `git status` or `git add`. +This feature is still experimental. Some commands might be slower with +a sparse index until they are properly integrated with the feature. ++ +**WARNING:** Using a sparse index requires modifying the index in a way +that is not completely understood by other tools. Enabling sparse index +enables the `extensions.spareseIndex` config value, which might cause +other tools to stop working with your repository. If you have trouble with +this compatibility, then run `git sparse-checkout sparse-index disable` to +remove this config and rewrite your index to not be sparse. 'set':: Write a set of patterns to the sparse-checkout file, as given as
builtin/sparse-checkout.c +16 -1
@@ -14,6 +14,7 @@#include "unpack-trees.h" #include "wt-status.h" #include "quote.h" +#include "sparse-index.h" static const char *empty_base ="" ;
@@ -283,12 +284,13 @@ static int set_config(enum sparse_checkout_mode mode) }static char const *const builtin_sparse_checkout_init_usage[] = { -N_ ("git sparse-checkout init [--cone]" ), +N_ ("git sparse-checkout init [--cone] [--[no-]sparse-index]" ),NULL };static struct sparse_checkout_init_opts {int cone_mode; +int sparse_index; } init_opts;static int sparse_checkout_init (int argc,const char **argv)
@@ -303,11 +305,15 @@ static int sparse_checkout_init(int argc, const char **argv)static struct option builtin_sparse_checkout_init_options[] = {OPT_BOOL (0 ,"cone" , &init_opts.cone_mode,N_ ("initialize the sparse-checkout in cone mode" )), +OPT_BOOL (0 ,"sparse-index" , &init_opts.sparse_index, +N_ ("toggle the use of a sparse index" )),OPT_END (), };repo_read_index (the_repository); + init_opts.sparse_index =-1 ; + argc =parse_options (argc, argv,NULL , builtin_sparse_checkout_init_options, builtin_sparse_checkout_init_usage,0 );
@@ -326,6 +332,15 @@ static int sparse_checkout_init(int argc, const char **argv) sparse_filename =get_sparse_checkout_filename (); res =add_patterns_from_file_to_list (sparse_filename,"" ,0 , &pl,NULL ); +if (init_opts.sparse_index >=0 ) { +if (set_sparse_index_config (the_repository, init_opts.sparse_index) <0 ) +die (_ ("failed to modify sparse-index config" )); + +/* force an index rewrite */ +repo_read_index (the_repository); + the_repository->index->updated_workdir =1 ; + } +/* If we already have a sparse-checkout file, use it. */ if (res >=0 ) {free (sparse_filename);
sparse-index.c +26 -12
@@ -104,23 +104,38 @@ static int convert_to_sparse_rec(struct index_state *istate,static int enable_sparse_index (struct repository *repo) { -const char *config_path =repo_git_path (repo,"config.worktree" ); +int res;if (upgrade_repository_format (1 ) <0 ) {warning (_ ("unable to upgrade repository format to enable sparse-index" ));return -1 ; } -git_config_set_in_file_gently (config_path, -"extensions.sparseIndex" , -"true" ); + res =git_config_set_gently ("extensions.sparseindex" ,"true" );prepare_repo_settings (repo); repo->settings.sparse_index =1 ; -return 0 ; +return res; +} + +int set_sparse_index_config (struct repository *repo,int enable) +{ +int res; + +if (enable) +return enable_sparse_index (repo); + +/* Don't downgrade repository format, just remove the extension. */ + res =git_config_set_multivar_gently ("extensions.sparseindex" ,NULL ,"" , +CONFIG_FLAGS_MULTI_REPLACE ); + +prepare_repo_settings (repo); + repo->settings.sparse_index =0 ; +return res; }int convert_to_sparse (struct index_state *istate) { +int test_env;if (istate->split_index || istate->sparse_index || !core_apply_sparse_checkout || !core_sparse_checkout_cone)return 0 ;
@@ -129,14 +144,13 @@ int convert_to_sparse(struct index_state *istate) istate->repo = the_repository;/* -* The GIT_TEST_SPARSE_INDEX environment variable triggers the -* extensions.sparseIndex config variable to be on. +* If GIT_TEST_SPARSE_INDEX=1, then trigger extensions.sparseIndex +* to be fully enabled. If GIT_TEST_SPARSE_INDEX=0 (set explicitly), +* then purposefully disable the setting. */ -if (git_env_bool ("GIT_TEST_SPARSE_INDEX" ,0 )) { -int err =enable_sparse_index (istate->repo); -if (err <0 ) -return err; - } + test_env =git_env_bool ("GIT_TEST_SPARSE_INDEX" ,-1 ); +if (test_env >=0 ) +set_sparse_index_config (istate->repo, test_env);/* * Only convert to sparse if extensions.sparseIndex is set.
sparse-index.h +3 -0
@@ -5,4 +5,7 @@ struct index_state;void ensure_full_index (struct index_state *istate);int convert_to_sparse (struct index_state *istate); +struct repository ; +int set_sparse_index_config (struct repository *repo,int enable); +#endif
t/t1092-sparse-checkout-compatibility.sh +16 -17
@@ -4,6 +4,7 @@ test_description='compare full workdir to sparse workdir' GIT_TEST_CHECK_CACHE_TREE=0 GIT_TEST_SPLIT_INDEX=0 +GIT_TEST_SPARSE_INDEX= . ./test-lib.sh
@@ -98,8 +99,9 @@ init_repos () { # initialize sparse-checkout definitions git -C sparse-checkout sparse-checkout init --cone && git -C sparse-checkout sparse-checkout set deep && - GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout init --cone && - GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set deep + git -C sparse-index sparse-checkout init --cone --sparse-index && + test_cmp_config -C sparse-index true extensions.sparseindex && + git -C sparse-index sparse-checkout set deep } run_on_sparse () {
@@ -109,7 +111,7 @@ run_on_sparse () { ) && ( cd sparse-index && - GIT_TEST_SPARSE_INDEX=1 $* >../sparse-index-out 2>../sparse-index-err + $* >../sparse-index-out 2>../sparse-index-err ) }
@@ -146,7 +148,7 @@ test_expect_success 'sparse-index contents' ' || return 1 done && - GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set folder1 && + git -C sparse-index sparse-checkout set folder1 && test-tool -C sparse-index read-cache --table --no-stat >cache && for dir in deep folder2 x
@@ -156,7 +158,7 @@ test_expect_success 'sparse-index contents' ' || return 1 done && - GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set deep/deeper1 && + git -C sparse-index sparse-checkout set deep/deeper1 && test-tool -C sparse-index read-cache --table --no-stat >cache && for dir in deep/deeper2 folder1 folder2 x
@@ -377,18 +379,15 @@ test_expect_success 'clean' ' test_expect_success 'sparse-index is expanded and converted back' ' init_repos && - ( - (GIT_TEST_SPARSE_INDEX=1 && export GIT_TEST_SPARSE_INDEX) && - GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" reset --hard && - test_region index convert_to_sparse trace2.txt && - test_region index ensure_full_index trace2.txt && - - rm trace2.txt && - GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" status -uno && - test_region index ensure_full_index trace2.txt - ) + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" reset --hard && + test_region index convert_to_sparse trace2.txt && + test_region index ensure_full_index trace2.txt && + + rm trace2.txt && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" status -uno && + test_region index ensure_full_index trace2.txt ' test_done
Signature · 1 line
gitgitgadget
Re: [PATCH 14/27] sparse-checkout: toggle sparse index from builtin
Quoted 6 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
The sparse index extension is used to signal that index writes should be in sparse mode. This was only updated using GIT_TEST_SPARSE_INDEX=1.
Quoted 7 lines
Add a '--[no-]sparse-index' option to 'git sparse-checkout init' that specifies if the sparse index should be used. It also updates the index to use the correct format, either way. Add a warning in the documentation that the use of a repository extension might reduce compatibility with third-party tools. 'git sparse-checkout init' already sets extension.worktreeConfig, which places most sparse-checkout users outside of the scope of most third-party tools.
Heh, looks like you're addressing my comments on the last patch about jgit. If I would have just read on...
One side question, though -- I thought I remembered seeing that we record index versions or extension information directly in the index, so that third party tools have a way of noting that the index has something they won't understand, rather than just reading values that appear to be corrupt to them. Perhaps I missed it, but have you done anything like that with this series?
Quoted 22 lines
Update
t1092-sparse-checkout-compatibility.shto use this CLI instead of GIT_TEST_SPARSE_INDEX=1.Signed-off-by Derrick Stolee <[email protected]>--- Documentation/git-sparse-checkout.txt | 14 +++++++++ builtin/sparse-checkout.c | 17 ++++++++++- sparse-index.c | 38 ++++++++++++++++-------- sparse-index.h | 3 ++ t/t1092-sparse-checkout-compatibility.sh | 33 ++++++++++---------- 5 files changed, 75 insertions(+), 30 deletions(-)Documentation/git-sparse-checkout.txt +1
-0@@ -45,6 +45,20 @@ To avoid interfering with other worktrees, it first enables the When `--cone` is provided, the `core.sparseCheckoutCone` setting is also set, allowing for better performance with a limited set of patterns (see 'CONE PATTERN SET' below). ++
Documentation/git-sparse-checkout.txt To avoid interfering with other worktrees, it first enables the · line 58
Quoted 10 lines
Documentation/git-sparse-checkout.txt +10
-0+Use the `--[no-]sparse-index` option to toggle the use of the sparse +index format. This reduces the size of the index to be more closely +aligned with your sparse-checkout definition. This can have significant +performance advantages for commands such as `git status` or `git add`. +This feature is still experimental. Some commands might be slower with +a sparse index until they are properly integrated with the feature. ++ +**WARNING:** Using a sparse index requires modifying the index in a way +that is not completely understood by other tools. Enabling sparse index +enables the `extensions.spareseIndex` config value, which might cause
extensions.sparseIndex; you have an extra 'e' in there.
Quoted 19 lines
Documentation/git-sparse-checkout.txt +3
-0+other tools to stop working with your repository. If you have trouble with +this compatibility, then run `git sparse-checkout sparse-index disable` to +remove this config and rewrite your index to not be sparse. 'set':: Write a set of patterns to the sparse-checkout file, as given asbuiltin/sparse-checkout.c +1
-0@@ -14,6 +14,7 @@#include "unpack-trees.h" #include "wt-status.h" #include "quote.h" +#include "sparse-index.h" static const char *empty_base ="" ;
builtin/sparse-checkout.c static int set_config(enum sparse_checkout_mode mode) · line 287
Quoted 5 lines
builtin/sparse-checkout.c +1
-1@@ -283,12 +284,13 @@ static int set_config(enum sparse_checkout_mode mode) }static char const *const builtin_sparse_checkout_init_usage[] = { -N_ ("git sparse-checkout init [--cone]" ), +N_ ("git sparse-checkout init [--cone] [--[no-]sparse-index]" ),
This all makes sense, but between partial clones, sparse-checkouts and sparse-indexes, I wonder if we're overloading users with terms and conditions. Perhaps that's inevitable in the short-term due to the various caveats that exist, but I'd just like to put out a fuzzy high-level goal of allowing users in the future to just specify "I want a sparse clone of this stuff" with as few special knobs and flags as possible. I don't want them to have to specify all of the individual things that means, such as they want (a) the history to be sparse (i.e. partial clone), (b) the checkout to be sparse, (c) the index to be sparse, (d) several commands to operate in a sparse manner, limiting their output based on the sparsity paths (hopefully they aren't required to list each one), and (e) several other commands shouldn't be limited by the sparsity paths. I guess it might be nice to _allow_ them to specify all the things it means for users who want control, but it'd be nice to avoid requiring it of all users.
Quoted 203 lines
builtin/sparse-checkout.c +14
-0NULL };static struct sparse_checkout_init_opts {int cone_mode; +int sparse_index; } init_opts;static int sparse_checkout_init (int argc,const char **argv)@@ -303,11 +305,15 @@ static int sparse_checkout_init(int argc, const char **argv)static struct option builtin_sparse_checkout_init_options[] = {OPT_BOOL (0 ,"cone" , &init_opts.cone_mode,N_ ("initialize the sparse-checkout in cone mode" )), +OPT_BOOL (0 ,"sparse-index" , &init_opts.sparse_index, +N_ ("toggle the use of a sparse index" )),OPT_END (), };repo_read_index (the_repository); + init_opts.sparse_index =-1 ; + argc =parse_options (argc, argv,NULL , builtin_sparse_checkout_init_options, builtin_sparse_checkout_init_usage,0 );@@ -326,6 +332,15 @@ static int sparse_checkout_init(int argc, const char **argv) sparse_filename =get_sparse_checkout_filename (); res =add_patterns_from_file_to_list (sparse_filename,"" ,0 , &pl,NULL ); +if (init_opts.sparse_index >=0 ) { +if (set_sparse_index_config (the_repository, init_opts.sparse_index) <0 ) +die (_ ("failed to modify sparse-index config" )); + +/* force an index rewrite */ +repo_read_index (the_repository); + the_repository->index->updated_workdir =1 ; + } +/* If we already have a sparse-checkout file, use it. */ if (res >=0 ) {free (sparse_filename);sparse-index.c +26
-12@@ -104,23 +104,38 @@ static int convert_to_sparse_rec(struct index_state *istate,static int enable_sparse_index (struct repository *repo) { -const char *config_path =repo_git_path (repo,"config.worktree" ); +int res;if (upgrade_repository_format (1 ) <0 ) {warning (_ ("unable to upgrade repository format to enable sparse-index" ));return -1 ; } -git_config_set_in_file_gently (config_path, -"extensions.sparseIndex" , -"true" ); + res =git_config_set_gently ("extensions.sparseindex" ,"true" );prepare_repo_settings (repo); repo->settings.sparse_index =1 ; -return 0 ; +return res; +} + +int set_sparse_index_config (struct repository *repo,int enable) +{ +int res; + +if (enable) +return enable_sparse_index (repo); + +/* Don't downgrade repository format, just remove the extension. */ + res =git_config_set_multivar_gently ("extensions.sparseindex" ,NULL ,"" , +CONFIG_FLAGS_MULTI_REPLACE ); + +prepare_repo_settings (repo); + repo->settings.sparse_index =0 ; +return res; }int convert_to_sparse (struct index_state *istate) { +int test_env;if (istate->split_index || istate->sparse_index || !core_apply_sparse_checkout || !core_sparse_checkout_cone)return 0 ;@@ -129,14 +144,13 @@ int convert_to_sparse(struct index_state *istate) istate->repo = the_repository;/* -* The GIT_TEST_SPARSE_INDEX environment variable triggers the -* extensions.sparseIndex config variable to be on. +* If GIT_TEST_SPARSE_INDEX=1, then trigger extensions.sparseIndex +* to be fully enabled. If GIT_TEST_SPARSE_INDEX=0 (set explicitly), +* then purposefully disable the setting. */ -if (git_env_bool ("GIT_TEST_SPARSE_INDEX" ,0 )) { -int err =enable_sparse_index (istate->repo); -if (err <0 ) -return err; - } + test_env =git_env_bool ("GIT_TEST_SPARSE_INDEX" ,-1 ); +if (test_env >=0 ) +set_sparse_index_config (istate->repo, test_env);/* * Only convert to sparse if extensions.sparseIndex is set. sparse-index.h +3
-0@@ -5,4 +5,7 @@ struct index_state;void ensure_full_index (struct index_state *istate);int convert_to_sparse (struct index_state *istate); +struct repository ; +int set_sparse_index_config (struct repository *repo,int enable); +#endif t/t1092-sparse-checkout-compatibility.sh +16
-18@@ -4,6 +4,7 @@ test_description='compare full workdir to sparse workdir' GIT_TEST_CHECK_CACHE_TREE=0 GIT_TEST_SPLIT_INDEX=0 +GIT_TEST_SPARSE_INDEX= . ./test-lib.sh@@ -98,8 +99,9 @@ init_repos () { # initialize sparse-checkout definitions git -C sparse-checkout sparse-checkout init --cone && git -C sparse-checkout sparse-checkout set deep && - GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout init --cone && - GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set deep + git -C sparse-index sparse-checkout init --cone --sparse-index && + test_cmp_config -C sparse-index true extensions.sparseindex && + git -C sparse-index sparse-checkout set deep } run_on_sparse () {@@ -109,7 +111,7 @@ run_on_sparse () { ) && ( cd sparse-index && - GIT_TEST_SPARSE_INDEX=1 $* >../sparse-index-out 2>../sparse-index-err + $* >../sparse-index-out 2>../sparse-index-err ) }@@ -146,7 +148,7 @@ test_expect_success 'sparse-index contents' ' || return 1 done && - GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set folder1 && + git -C sparse-index sparse-checkout set folder1 && test-tool -C sparse-index read-cache --table --no-stat >cache && for dir in deep folder2 x@@ -156,7 +158,7 @@ test_expect_success 'sparse-index contents' ' || return 1 done && - GIT_TEST_SPARSE_INDEX=1 git -C sparse-index sparse-checkout set deep/deeper1 && + git -C sparse-index sparse-checkout set deep/deeper1 && test-tool -C sparse-index read-cache --table --no-stat >cache && for dir in deep/deeper2 folder1 folder2 x@@ -377,18 +379,15 @@ test_expect_success 'clean' ' test_expect_success 'sparse-index is expanded and converted back' ' init_repos && - ( - (GIT_TEST_SPARSE_INDEX=1 && export GIT_TEST_SPARSE_INDEX) && - GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" reset --hard && - test_region index convert_to_sparse trace2.txt && - test_region index ensure_full_index trace2.txt && - - rm trace2.txt && - GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" status -uno && - test_region index ensure_full_index trace2.txt - ) + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" reset --hard && + test_region index convert_to_sparse trace2.txt && + test_region index ensure_full_index trace2.txt && + + rm trace2.txt && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" status -uno && + test_region index ensure_full_index trace2.txt ' test_done --
Quoted 1 line
gitgitgadget
I need to take a break from reviewing again at this point and work on some other tasks. I'll resume reviewing the series later, perhaps tomorrow afternoon.
Re: [PATCH 14/27] sparse-checkout: toggle sparse index from builtin
Quoted 6 lines · Elijah Newren
On Mon, Jan 25, 2021 at 9:42 AM Derrick Stolee via GitGitGadget <[email protected]> wrote:
I need to take a break from reviewing again at this point and work on some other tasks. I'll resume reviewing the series later, perhaps tomorrow afternoon.
I appreciate your efforts here! I'm delaying detailed responses until enough time has passed for interested parties to comment. If it helps, then focusing on the "big things" is most important for now. I'll be more careful about typos and things when I submit patches for full review.
For my part, I'm taking time to look around at other things that need my attention after being heads-down on this prototype. The sparse index will need proper care to implement and I'm not expecting it to move very quickly.
Thanks,
-Stolee
PATCH 15/27 *: ensure full index
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
This giant patch is not intended for actual review. I have a branch that has these changes split out in a sane way with some commentary in each file that is modified.
The idea here is to guard certain portions of the codebase that do not know how to handle sparse indexes by ensuring that the index is expanded to a full index before proceeding with the logic.
This also provides a good mechanism for testing which code needs updating to enable the sparse index in a Git builtin. The builtin can set the_repository->settings.command_requires_full_index to zero and then we can debug the command with a breakpoint on ensure_full_index(). That identifies the portion of code that needs adjusting before enabling sparse indexes for that command.
Some index operations must be changed to operate on a non-const pointer, since ensuring a full index will modify the index itself.
There are likely some gaps to these protections, which is why it will be important to carefully test each scenario as we relax the requirements. I expect that to be a long effort.
--- apply.c | 10 +++++++++- blame.c | 7 ++++++- builtin/checkout-index.c | 5 ++++- builtin/grep.c | 2 ++ builtin/ls-files.c | 9 ++++++++- builtin/merge-index.c | 2 ++ builtin/mv.c | 2 ++ builtin/rm.c | 2 ++ builtin/sparse-checkout.c | 1 + builtin/update-index.c | 2 ++ cache.h | 1 + diff-lib.c | 2 ++ diff.c | 2 ++ dir.c | 14 +++++++++++++- entry.c | 2 ++ fsmonitor.c | 11 ++++++++++- merge-recursive.c | 22 +++++++++++++++++++--- name-hash.c | 6 ++++++ pathspec.c | 5 +++-- pathspec.h | 4 ++-- read-cache.c | 19 +++++++++++++++++-- rerere.c | 2 ++ resolve-undo.c | 6 ++++++ sha1-name.c | 3 +++ split-index.c | 2 ++ submodule.c | 24 +++++++++++++++++++----- submodule.h | 6 +++--- tree.c | 2 ++ wt-status.c | 7 +++++++ 29 files changed, 159 insertions(+), 23 deletions(-)
apply.c +9 -1
@@ -3523,6 +3523,8 @@ static int load_current(struct apply_state *state,if (!patch->is_new)BUG ("patch to %s is not a creation" , patch->old_name); +ensure_full_index (state->repo->index); + pos =index_name_pos (state->repo->index, name,strlen (name));if (pos <0 )return error (_ ("%s: does not exist in index" ), name);
@@ -3692,7 +3694,11 @@ static int check_preimage(struct apply_state *state, }if (state->check_index && !previous) { -int pos =index_name_pos (state->repo->index, old_name, +int pos; + +ensure_full_index (state->repo->index); + + pos =index_name_pos (state->repo->index, old_name,strlen (old_name));if (pos <0 ) {if (patch->is_new <0 )
@@ -3751,6 +3757,8 @@ static int check_to_create(struct apply_state *state,if (state->check_index && (!ok_if_exists || !state->cached)) {int pos; +ensure_full_index (state->repo->index); + pos =index_name_pos (state->repo->index, new_name,strlen (new_name));if (pos >=0 ) {struct cache_entry *ce = state->repo->index->cache[pos];
blame.c +6 -1
@@ -108,6 +108,7 @@ static void verify_working_tree_path(struct repository *r,return ; } +ensure_full_index (r->index); pos =index_name_pos (r->index, path,strlen (path));if (pos >=0 ) ;/* path is in the index */
@@ -277,7 +278,11 @@ static struct commit *fake_working_tree_commit(struct repository *r, len =strlen (path);if (!mode) { -int pos =index_name_pos (r->index, path, len); +int pos; + +ensure_full_index (r->index); + + pos =index_name_pos (r->index, path, len);if (0 <= pos) mode = r->index->cache[pos]->ce_mode;else
builtin/checkout-index.c +4 -1
@@ -48,11 +48,14 @@ static void write_tempfile_record(const char *name, const char *prefix)static int checkout_file (const char *name,const char *prefix) {int namelen =strlen (name); -int pos =cache_name_pos (name, namelen); +int pos;int has_same_name =0 ;int did_checkout =0 ;int errs =0 ; +ensure_full_index (the_repository->index); + pos =index_name_pos (the_repository->index, name, namelen); +if (pos <0 ) pos = -pos -1 ;
builtin/grep.c +2 -0
@@ -506,6 +506,8 @@ static int grep_cache(struct grep_opt *opt,if (repo_read_index (repo) <0 )die (_ ("index file corrupt" )); +ensure_full_index (repo->index); +for (nr =0 ; nr < repo->index->cache_nr; nr++) {const struct cache_entry *ce = repo->index->cache[nr];strbuf_setlen (&name, name_base_len);
builtin/ls-files.c +8 -1
@@ -150,7 +150,7 @@ static void show_other_files(const struct index_state *istate, } } -static void show_killed_files (const struct index_state *istate, +static void show_killed_files (struct index_state *istate,const struct dir_struct *dir) {int i;
@@ -159,6 +159,8 @@ static void show_killed_files(const struct index_state *istate,char *cp, *sp;int pos, len, killed =0 ; +ensure_full_index (istate); +for (cp = ent->name; cp - ent->name < ent->len; cp = sp +1 ) { sp =strchr (cp,'/' );if (!sp) {
@@ -313,6 +315,7 @@ static void show_files(struct repository *repo, struct dir_struct *dir)show_killed_files (repo->index, dir); }if (show_cached || show_stage) { +ensure_full_index (repo->index);for (i =0 ; i < repo->index->cache_nr; i++) {const struct cache_entry *ce = repo->index->cache[i];
@@ -332,6 +335,7 @@ static void show_files(struct repository *repo, struct dir_struct *dir) } }if (show_deleted || show_modified) { +ensure_full_index (repo->index);for (i =0 ; i < repo->index->cache_nr; i++) {const struct cache_entry *ce = repo->index->cache[i];struct stat st;
@@ -368,6 +372,7 @@ static void prune_index(struct index_state *istate,if (!prefix || !istate->cache_nr)return ; +ensure_full_index (istate); pos =index_name_pos (istate, prefix, prefixlen);if (pos <0 ) pos = -pos-1 ;
@@ -428,6 +433,8 @@ void overlay_tree_on_index(struct index_state *istate,if (!tree)die ("bad tree-ish %s" , tree_name); +ensure_full_index (istate); +/* Hoist the unmerged entries up to stage #3 to make room */ for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce = istate->cache[i];
builtin/merge-index.c +2 -0
@@ -80,6 +80,8 @@ int cmd_merge_index(int argc, const char **argv, const char *prefix)read_cache (); +ensure_full_index (&the_index); + i =1 ;if (!strcmp (argv[i],"-o" )) { one_shot =1 ;
builtin/mv.c +2 -0
@@ -145,6 +145,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (read_cache () <0 )die (_ ("index file corrupt" )); +ensure_full_index (&the_index); + source =internal_prefix_pathspec (prefix, argv, argc,0 ); modes =xcalloc (argc,sizeof (enum update_mode ));/*
builtin/rm.c +2 -0
@@ -291,6 +291,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)refresh_index (&the_index,REFRESH_QUIET |REFRESH_UNMERGED , &pathspec,NULL ,NULL ); +ensure_full_index (&the_index); + seen =xcalloc (pathspec.nr,1 );for (i =0 ; i < active_nr; i++) {
builtin/sparse-checkout.c +1 -0
@@ -123,6 +123,7 @@ static int update_working_directory(struct pattern_list *pl) o.pl = pl;setup_work_tree (); +ensure_full_index (r->index);repo_hold_locked_index (r, &lock_file,LOCK_DIE_ON_ERROR );
builtin/update-index.c +2 -0
@@ -1088,6 +1088,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) the_index.updated_skipworktree =1 ; +ensure_full_index (&the_index); +/* * Custom copy of parse_options() because we want to handle * filename arguments as they come.
cache.h +1 -0
@@ -346,6 +346,7 @@ void add_name_hash(struct index_state *istate, struct cache_entry *ce);void remove_name_hash (struct index_state *istate,struct cache_entry *ce);void free_name_hash (struct index_state *istate); +void ensure_full_index (struct index_state *istate);/* Cache entry creation and cleanup */
diff-lib.c +2 -0
@@ -96,6 +96,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)uint64_t start =getnanotime ();struct index_state *istate = revs->diffopt.repo->index; +ensure_full_index (istate); +diff_set_mnemonic_prefix (&revs->diffopt,"i/" ,"w/" );refresh_fsmonitor (istate);
diff.c +2 -0
@@ -3901,6 +3901,8 @@ static int reuse_worktree_file(struct index_state *istate,if (!want_file &&would_convert_to_git (istate, name))return 0 ; +ensure_full_index (istate); + len =strlen (name); pos =index_name_pos (istate, name, len);if (pos <0 )
dir.c +13 -1
@@ -892,13 +892,15 @@ void add_pattern(const char *string, const char *base,add_pattern_to_hashsets (pl, pattern); } -static int read_skip_worktree_file_from_index (const struct index_state *istate, +static int read_skip_worktree_file_from_index (struct index_state *istate,const char *path,size_t *size_out,char **data_out,struct oid_stat *oid_stat) {int pos, len; +ensure_full_index (istate); + len =strlen (path); pos =index_name_pos (istate, path, len);if (pos <0 )
@@ -1088,6 +1090,10 @@ static int add_patterns(const char *fname, const char *base, int baselen,close (fd);if (oid_stat) {int pos; + +if (istate) +ensure_full_index (istate); +if (oid_stat->valid && !match_stat_data_racy (istate, &oid_stat->stat, &st)) ;/* no content change, oid_stat->oid still good */
@@ -1696,6 +1702,8 @@ static enum exist_status directory_exists_in_index(struct index_state *istate,if (ignore_case)return directory_exists_in_index_icase (istate, dirname, len); +ensure_full_index (istate); + pos =index_name_pos (istate, dirname, len);if (pos <0 ) pos = -pos-1 ;
@@ -2050,6 +2058,8 @@ static int get_index_dtype(struct index_state *istate,int pos;const struct cache_entry *ce; +ensure_full_index (istate); + ce =index_file_exists (istate, path, len,0 );if (ce) {if (!ce_uptodate (ce))
@@ -3536,6 +3546,8 @@ static void connect_wt_gitdir_in_nested(const char *sub_worktree,if (repo_read_index (&subrepo) <0 )die (_ ("index file corrupt in repo %s" ), subrepo.gitdir); +ensure_full_index (subrepo.index); +for (i =0 ; i < subrepo.index->cache_nr; i++) {const struct cache_entry *ce = subrepo.index->cache[i];
entry.c +2 -0
@@ -412,6 +412,8 @@ static void mark_colliding_entries(const struct checkout *state, ce->ce_flags |=CE_MATCHED ; +ensure_full_index (state->istate); +for (i =0 ; i < state->istate->cache_nr; i++) {struct cache_entry *dup = state->istate->cache[i];
fsmonitor.c +10 -1
@@ -97,6 +97,9 @@ int read_fsmonitor_extension(struct index_state *istate, const void *data,void fill_fsmonitor_bitmap (struct index_state *istate) {unsigned int i, skipped =0 ; + +ensure_full_index (istate); + istate->fsmonitor_dirty =ewah_new ();for (i =0 ; i < istate->cache_nr; i++) {if (istate->cache[i]->ce_flags &CE_REMOVE )
@@ -158,7 +161,11 @@ static int query_fsmonitor(int version, const char *last_update, struct strbuf *static void fsmonitor_refresh_callback (struct index_state *istate,const char *name) { -int pos =index_name_pos (istate, name,strlen (name)); +int pos; + +ensure_full_index (istate); + + pos =index_name_pos (istate, name,strlen (name));if (pos >=0 ) {struct cache_entry *ce = istate->cache[pos];
@@ -330,6 +337,8 @@ void tweak_fsmonitor(struct index_state *istate)if (istate->fsmonitor_dirty) {if (fsmonitor_enabled) { +ensure_full_index (istate); +/* Mark all entries valid */ for (i =0 ; i < istate->cache_nr; i++) { istate->cache[i]->ce_flags |=CE_FSMONITOR_VALID ;
merge-recursive.c +19 -3
@@ -522,6 +522,8 @@ static struct string_list *get_unmerged(struct index_state *istate) unmerged->strdup_strings =1 ; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {struct string_list_item *item;struct stage_data *e;
@@ -762,6 +764,8 @@ static int dir_in_way(struct index_state *istate, const char *path,strbuf_addstr (&dirpath, path);strbuf_addch (&dirpath,'/' ); +ensure_full_index (istate); + pos =index_name_pos (istate, dirpath.buf, dirpath.len);if (pos <0 )
@@ -785,9 +789,13 @@ static int dir_in_way(struct index_state *istate, const char *path,static int was_tracked_and_matches (struct merge_options *opt,const char *path,const struct diff_filespec *blob) { -int pos =index_name_pos (&opt->priv->orig_index, path,strlen (path)); +int pos;struct cache_entry *ce; +ensure_full_index (&opt->priv->orig_index); + + pos =index_name_pos (&opt->priv->orig_index, path,strlen (path)); +if (0 > pos)/* we were not tracking this path before the merge */ return 0 ;
@@ -802,7 +810,11 @@ static int was_tracked_and_matches(struct merge_options *opt, const char *path,*/ static int was_tracked (struct merge_options *opt,const char *path) { -int pos =index_name_pos (&opt->priv->orig_index, path,strlen (path)); +int pos; + +ensure_full_index (&opt->priv->orig_index); + + pos =index_name_pos (&opt->priv->orig_index, path,strlen (path));if (0 <= pos)/* we were tracking this path before the merge */
@@ -814,6 +826,9 @@ static int was_tracked(struct merge_options *opt, const char *path)static int would_lose_untracked (struct merge_options *opt,const char *path) {struct index_state *istate = opt->repo->index; +int pos; + +ensure_full_index (istate);/* * This may look like it can be simplified to:
@@ -832,7 +847,7 @@ static int would_lose_untracked(struct merge_options *opt, const char *path)* update_file()/would_lose_untracked(); see every comment in this * file which mentions "update_stages". */ -int pos =index_name_pos (istate, path,strlen (path)); + pos =index_name_pos (istate, path,strlen (path));if (pos <0 ) pos =-1 - pos;
@@ -3086,6 +3101,7 @@ static int handle_content_merge(struct merge_file_info *mfi,* flag to avoid making the file appear as if it were * deleted by the user. */ +ensure_full_index (&opt->priv->orig_index); pos =index_name_pos (&opt->priv->orig_index, path,strlen (path)); ce = opt->priv->orig_index.cache[pos];if (ce_skip_worktree (ce)) {
name-hash.c +6 -0
@@ -679,6 +679,8 @@ int index_dir_exists(struct index_state *istate, const char *name, int namelen) {struct dir_entry *dir; +ensure_full_index (istate); +lazy_init_name_hash (istate); dir =find_dir_entry (istate, name, namelen);return dir && dir->nr;
@@ -689,6 +691,8 @@ void adjust_dirname_case(struct index_state *istate, char *name)const char *startPtr = name;const char *ptr = startPtr; +ensure_full_index ( istate); +lazy_init_name_hash (istate);while (*ptr) {while (*ptr && *ptr !='/' )
@@ -712,6 +716,8 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *nastruct cache_entry *ce;unsigned int hash =memihash (name, namelen); +ensure_full_index (istate); +lazy_init_name_hash (istate); ce = hashmap_get_entry_from_hash(&istate->name_hash, hash, NULL,
pathspec.c +3 -2
@@ -20,7 +20,7 @@* to use find_pathspecs_matching_against_index() instead. */ void add_pathspec_matches_against_index (const struct pathspec *pathspec, -const struct index_state *istate, +struct index_state *istate,char *seen) {int num_unmatched =0 , i;
@@ -36,6 +36,7 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec, num_unmatched++;if (!num_unmatched)return ; +ensure_full_index (istate);for (i =0 ; i < istate->cache_nr; i++) {const struct cache_entry *ce = istate->cache[i];ce_path_match (istate, ce, pathspec, seen);
@@ -51,7 +52,7 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,* given pathspecs achieves against all items in the index. */ char *find_pathspecs_matching_against_index (const struct pathspec *pathspec, -const struct index_state *istate) +struct index_state *istate) {char *seen =xcalloc (pathspec->nr,1 );add_pathspec_matches_against_index (pathspec, istate, seen);
pathspec.h +2 -2
@@ -150,10 +150,10 @@ static inline int ps_strcmp(const struct pathspec_item *item, }void add_pathspec_matches_against_index (const struct pathspec *pathspec, -const struct index_state *istate, +struct index_state *istate,char *seen);char *find_pathspecs_matching_against_index (const struct pathspec *pathspec, -const struct index_state *istate); +struct index_state *istate);int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item);
read-cache.c +17 -2
@@ -622,7 +622,11 @@ void remove_marked_cache_entries(struct index_state *istate, int invalidate)int remove_file_from_index (struct index_state *istate,const char *path) { -int pos =index_name_pos (istate, path,strlen (path)); +int pos; + +ensure_full_index (istate); + + pos =index_name_pos (istate, path,strlen (path));if (pos <0 ) pos = -pos-1 ;cache_tree_invalidate_path (istate, path);
@@ -640,9 +644,12 @@ static int compare_name(struct cache_entry *ce, const char *path, int namelen)static int index_name_pos_also_unmerged (struct index_state *istate,const char *path,int namelen) { -int pos =index_name_pos (istate, path, namelen); +int pos;struct cache_entry *ce; +ensure_full_index (istate); + + pos =index_name_pos (istate, path, namelen);if (pos >=0 )return pos;
@@ -717,6 +724,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,int hash_flags =HASH_WRITE_OBJECT ;struct object_id oid; +ensure_full_index (istate); +if (flags &ADD_CACHE_RENORMALIZE ) hash_flags |=HASH_RENORMALIZE ;
@@ -1095,6 +1104,8 @@ static int has_dir_name(struct index_state *istate,size_t len_eq_last;int cmp_last =0 ; +ensure_full_index (istate); +/* * We are frequently called during an iteration on a sorted * list of pathnames and while building a new index. Therefore,
@@ -1338,6 +1349,8 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti {int pos; +ensure_full_index (istate); +if (option &ADD_CACHE_JUST_APPEND ) pos = istate->cache_nr;else {
@@ -1547,6 +1560,8 @@ int refresh_index(struct index_state *istate, unsigned int flags,* we only have to do the special cases that are left. */ preload_index (istate, pathspec,0 ); + +ensure_full_index (istate);for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce, *new_entry;int cache_errno =0 ;
rerere.c +2 -0
@@ -962,6 +962,8 @@ static int handle_cache(struct index_state *istate,struct rerere_io_mem io;int marker_size =ll_merge_marker_size (istate, path); +ensure_full_index (istate); +/* * Reproduce the conflicted merge in-core */
resolve-undo.c +6 -0
@@ -125,6 +125,8 @@ int unmerge_index_entry_at(struct index_state *istate, int pos)if (!istate->resolve_undo)return pos; +ensure_full_index (istate); + ce = istate->cache[pos];if (ce_stage (ce)) {/* already unmerged */
@@ -172,6 +174,8 @@ void unmerge_marked_index(struct index_state *istate)if (!istate->resolve_undo)return ; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {const struct cache_entry *ce = istate->cache[i];if (ce->ce_flags &CE_MATCHED )
@@ -186,6 +190,8 @@ void unmerge_index(struct index_state *istate, const struct pathspec *pathspec)if (!istate->resolve_undo)return ; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {const struct cache_entry *ce = istate->cache[i];if (!ce_path_match (istate, ce, pathspec,NULL ))
sha1-name.c +3 -0
@@ -1734,6 +1734,8 @@ static void diagnose_invalid_index_path(struct repository *r,if (!prefix) prefix ="" ; +ensure_full_index (r->index); +/* Wrong stage number? */ pos =index_name_pos (istate, filename, namelen);if (pos <0 )
@@ -1854,6 +1856,7 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,if (!repo->index || !repo->index->cache)repo_read_index (repo); +ensure_full_index (repo->index); pos =index_name_pos (repo->index, cp, namelen);if (pos <0 ) pos = -pos -1 ;
split-index.c +2 -0
@@ -4,6 +4,8 @@struct split_index *init_split_index (struct index_state *istate) { +ensure_full_index (istate); +if (!istate->split_index) { istate->split_index =xcalloc (1 ,sizeof (*istate->split_index)); istate->split_index->refcount =1 ;
submodule.c +19 -5
@@ -33,9 +33,13 @@ static struct oid_array ref_tips_after_fetch;* will be disabled because we can't guess what might be configured in * .gitmodules unless the user resolves the conflict. */ -int is_gitmodules_unmerged (const struct index_state *istate) +int is_gitmodules_unmerged (struct index_state *istate) { -int pos =index_name_pos (istate,GITMODULES_FILE ,strlen (GITMODULES_FILE )); +int pos; + +ensure_full_index (istate); + + pos =index_name_pos (istate,GITMODULES_FILE ,strlen (GITMODULES_FILE ));if (pos <0 ) {/* .gitmodules not found or isn't merged */ pos =-1 - pos;if (istate->cache_nr > pos) {/* there is a .gitmodules */
@@ -77,7 +81,11 @@ int is_writing_gitmodules_ok(void)*/ int is_staging_gitmodules_ok (struct index_state *istate) { -int pos =index_name_pos (istate,GITMODULES_FILE ,strlen (GITMODULES_FILE )); +int pos; + +ensure_full_index (istate); + + pos =index_name_pos (istate,GITMODULES_FILE ,strlen (GITMODULES_FILE ));if ((pos >=0 ) && (pos < istate->cache_nr)) {struct stat st;
@@ -301,7 +309,7 @@ int is_submodule_populated_gently(const char *path, int *return_error_code)/* * Dies if the provided 'prefix' corresponds to an unpopulated submodule */ -void die_in_unpopulated_submodule (const struct index_state *istate, +void die_in_unpopulated_submodule (struct index_state *istate,const char *prefix) {int i, prefixlen;
@@ -311,6 +319,8 @@ void die_in_unpopulated_submodule(const struct index_state *istate, prefixlen =strlen (prefix); +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce = istate->cache[i];int ce_len =ce_namelen (ce);
@@ -331,11 +341,13 @@ void die_in_unpopulated_submodule(const struct index_state *istate,/* * Dies if any paths in the provided pathspec descends into a submodule */ -void die_path_inside_submodule (const struct index_state *istate, +void die_path_inside_submodule (struct index_state *istate,const struct pathspec *ps) {int i, j; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce = istate->cache[i];int ce_len =ce_namelen (ce);
@@ -1420,6 +1432,8 @@ static int get_next_submodule(struct child_process *cp, {struct submodule_parallel_fetch *spf = data; +ensure_full_index (spf->r->index); +for (; spf->count < spf->r->index->cache_nr; spf->count++) {const struct cache_entry *ce = spf->r->index->cache[spf->count];const char *default_argv;
submodule.h +3 -3
@@ -39,7 +39,7 @@ struct submodule_update_strategy { };#define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL} -int is_gitmodules_unmerged (const struct index_state *istate); +int is_gitmodules_unmerged (struct index_state *istate);int is_writing_gitmodules_ok (void );int is_staging_gitmodules_ok (struct index_state *istate);int update_path_in_gitmodules (const char *oldpath,const char *newpath);
@@ -60,9 +60,9 @@ int is_submodule_active(struct repository *repo, const char *path);* Otherwise the return error code is the same as of resolve_gitdir_gently. */ int is_submodule_populated_gently (const char *path,int *return_error_code); -void die_in_unpopulated_submodule (const struct index_state *istate, +void die_in_unpopulated_submodule (struct index_state *istate,const char *prefix); -void die_path_inside_submodule (const struct index_state *istate, +void die_path_inside_submodule (struct index_state *istate,const struct pathspec *ps);enum submodule_update_type parse_submodule_update_type (const char *value); int parse_submodule_update_strategy(const char *value,
tree.c +2 -0
@@ -170,6 +170,8 @@ int read_tree(struct repository *r, struct tree *tree, int stage,* to matter. */ +ensure_full_index (istate); +/* * See if we have cache entry at the stage. If so, * do it the original slow way, otherwise, append and then
wt-status.c +7 -0
@@ -509,6 +509,8 @@ static int unmerged_mask(struct index_state *istate, const char *path)int pos, mask;const struct cache_entry *ce; +ensure_full_index (istate); + pos =index_name_pos (istate, path,strlen (path));if (0 <= pos)return 0 ;
@@ -657,6 +659,8 @@ static void wt_status_collect_changes_initial(struct wt_status *s)struct index_state *istate = s->repo->index;int i; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {struct string_list_item *it;struct wt_status_change_data *d;
@@ -2295,6 +2299,9 @@ static void wt_porcelain_v2_print_unmerged_entry(*/ memset (stages,0 ,sizeof (stages)); sum =0 ; + +ensure_full_index (istate); + pos =index_name_pos (istate, it->string,strlen (it->string));assert (pos <0 ); pos = -pos-1 ;
Signature · 1 line
gitgitgadget
Re: [PATCH 15/27] [RFC-VERSION] *: ensure full index
Quoted 18 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
This giant patch is not intended for actual review. I have a branch that has these changes split out in a sane way with some commentary in each file that is modified.
The idea here is to guard certain portions of the codebase that do not know how to handle sparse indexes by ensuring that the index is expanded to a full index before proceeding with the logic.
This also provides a good mechanism for testing which code needs updating to enable the sparse index in a Git builtin. The builtin can set
the_repository->settings.command_requires_full_indexto zero and then we can debug the command with a breakpoint onensure_full_index(). That identifies the portion of code that needs adjusting before enabling sparse indexes for that command.
Quoted 6 lines
Some index operations must be changed to operate on a non-const pointer, since ensuring a full index will modify the index itself.
There are likely some gaps to these protections, which is why it will be important to carefully test each scenario as we relax the requirements. I expect that to be a long effort.
I think the idea makes sense; it provides a way for us to incrementally build support for this new feature.
I skimmed over the code and noticed various interesting places that had the ensure_full_index() call (e.g. read_skip_worktree_file_from_index() -- whose existence comes from sparsity; what irony...). Better breakouts would be great, so I'll defer commenting much until then. But, just to verify I'm
understanding: the primary defence is the command_requires_full_index setting, and you have added several ensure_full_index() calls throughout the code in places you believe would need to be fixed up in case someone switches the command_requires_full_index setting. Is that correct? And your comment on the gaps is just that there may be other places that are missing the secondary protection (as opposed to my first reading of that paragraph as suggesting we aren't sure if we have enough protections yet and need to add more before this moves out of RFC); is that right?
Quoted 840 lines
Signed-off-by Derrick Stolee <[email protected]>--- apply.c | 10 +++++++++- blame.c | 7 ++++++- builtin/checkout-index.c | 5 ++++- builtin/grep.c | 2 ++ builtin/ls-files.c | 9 ++++++++- builtin/merge-index.c | 2 ++ builtin/mv.c | 2 ++ builtin/rm.c | 2 ++ builtin/sparse-checkout.c | 1 + builtin/update-index.c | 2 ++ cache.h | 1 + diff-lib.c | 2 ++ diff.c | 2 ++ dir.c | 14 +++++++++++++- entry.c | 2 ++ fsmonitor.c | 11 ++++++++++- merge-recursive.c | 22 +++++++++++++++++++--- name-hash.c | 6 ++++++ pathspec.c | 5 +++-- pathspec.h | 4 ++-- read-cache.c | 19 +++++++++++++++++-- rerere.c | 2 ++ resolve-undo.c | 6 ++++++ sha1-name.c | 3 +++ split-index.c | 2 ++ submodule.c | 24 +++++++++++++++++++----- submodule.h | 6 +++--- tree.c | 2 ++ wt-status.c | 7 +++++++ 29 files changed, 159 insertions(+), 23 deletions(-)apply.c +9
-1@@ -3523,6 +3523,8 @@ static int load_current(struct apply_state *state,if (!patch->is_new)BUG ("patch to %s is not a creation" , patch->old_name); +ensure_full_index (state->repo->index); + pos =index_name_pos (state->repo->index, name,strlen (name));if (pos <0 )return error (_ ("%s: does not exist in index" ), name);@@ -3692,7 +3694,11 @@ static int check_preimage(struct apply_state *state, }if (state->check_index && !previous) { -int pos =index_name_pos (state->repo->index, old_name, +int pos; + +ensure_full_index (state->repo->index); + + pos =index_name_pos (state->repo->index, old_name,strlen (old_name));if (pos <0 ) {if (patch->is_new <0 )@@ -3751,6 +3757,8 @@ static int check_to_create(struct apply_state *state,if (state->check_index && (!ok_if_exists || !state->cached)) {int pos; +ensure_full_index (state->repo->index); + pos =index_name_pos (state->repo->index, new_name,strlen (new_name));if (pos >=0 ) {struct cache_entry *ce = state->repo->index->cache[pos];blame.c +6
-1@@ -108,6 +108,7 @@ static void verify_working_tree_path(struct repository *r,return ; } +ensure_full_index (r->index); pos =index_name_pos (r->index, path,strlen (path));if (pos >=0 ) ;/* path is in the index */ @@ -277,7 +278,11 @@ static struct commit *fake_working_tree_commit(struct repository *r, len =strlen (path);if (!mode) { -int pos =index_name_pos (r->index, path, len); +int pos; + +ensure_full_index (r->index); + + pos =index_name_pos (r->index, path, len);if (0 <= pos) mode = r->index->cache[pos]->ce_mode;else builtin/checkout-index.c +4
-1@@ -48,11 +48,14 @@ static void write_tempfile_record(const char *name, const char *prefix)static int checkout_file (const char *name,const char *prefix) {int namelen =strlen (name); -int pos =cache_name_pos (name, namelen); +int pos;int has_same_name =0 ;int did_checkout =0 ;int errs =0 ; +ensure_full_index (the_repository->index); + pos =index_name_pos (the_repository->index, name, namelen); +if (pos <0 ) pos = -pos -1 ;builtin/grep.c +2
-0@@ -506,6 +506,8 @@ static int grep_cache(struct grep_opt *opt,if (repo_read_index (repo) <0 )die (_ ("index file corrupt" )); +ensure_full_index (repo->index); +for (nr =0 ; nr < repo->index->cache_nr; nr++) {const struct cache_entry *ce = repo->index->cache[nr];strbuf_setlen (&name, name_base_len);builtin/ls-files.c +8
-1@@ -150,7 +150,7 @@ static void show_other_files(const struct index_state *istate, } } -static void show_killed_files (const struct index_state *istate, +static void show_killed_files (struct index_state *istate,const struct dir_struct *dir) {int i;@@ -159,6 +159,8 @@ static void show_killed_files(const struct index_state *istate,char *cp, *sp;int pos, len, killed =0 ; +ensure_full_index (istate); +for (cp = ent->name; cp - ent->name < ent->len; cp = sp +1 ) { sp =strchr (cp,'/' );if (!sp) {@@ -313,6 +315,7 @@ static void show_files(struct repository *repo, struct dir_struct *dir)show_killed_files (repo->index, dir); }if (show_cached || show_stage) { +ensure_full_index (repo->index);for (i =0 ; i < repo->index->cache_nr; i++) {const struct cache_entry *ce = repo->index->cache[i];@@ -332,6 +335,7 @@ static void show_files(struct repository *repo, struct dir_struct *dir) } }if (show_deleted || show_modified) { +ensure_full_index (repo->index);for (i =0 ; i < repo->index->cache_nr; i++) {const struct cache_entry *ce = repo->index->cache[i];struct stat st;@@ -368,6 +372,7 @@ static void prune_index(struct index_state *istate,if (!prefix || !istate->cache_nr)return ; +ensure_full_index (istate); pos =index_name_pos (istate, prefix, prefixlen);if (pos <0 ) pos = -pos-1 ;@@ -428,6 +433,8 @@ void overlay_tree_on_index(struct index_state *istate,if (!tree)die ("bad tree-ish %s" , tree_name); +ensure_full_index (istate); +/* Hoist the unmerged entries up to stage #3 to make room */ for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce = istate->cache[i];builtin/merge-index.c +2
-0@@ -80,6 +80,8 @@ int cmd_merge_index(int argc, const char **argv, const char *prefix)read_cache (); +ensure_full_index (&the_index); + i =1 ;if (!strcmp (argv[i],"-o" )) { one_shot =1 ;builtin/mv.c +2
-0@@ -145,6 +145,8 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (read_cache () <0 )die (_ ("index file corrupt" )); +ensure_full_index (&the_index); + source =internal_prefix_pathspec (prefix, argv, argc,0 ); modes =xcalloc (argc,sizeof (enum update_mode ));/* builtin/rm.c +2
-0@@ -291,6 +291,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix)refresh_index (&the_index,REFRESH_QUIET |REFRESH_UNMERGED , &pathspec,NULL ,NULL ); +ensure_full_index (&the_index); + seen =xcalloc (pathspec.nr,1 );for (i =0 ; i < active_nr; i++) {builtin/sparse-checkout.c +1
-0@@ -123,6 +123,7 @@ static int update_working_directory(struct pattern_list *pl) o.pl = pl;setup_work_tree (); +ensure_full_index (r->index);repo_hold_locked_index (r, &lock_file,LOCK_DIE_ON_ERROR );builtin/update-index.c +2
-0@@ -1088,6 +1088,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) the_index.updated_skipworktree =1 ; +ensure_full_index (&the_index); +/* * Custom copy of parse_options() because we want to handle * filename arguments as they come. cache.h +1
-0@@ -346,6 +346,7 @@ void add_name_hash(struct index_state *istate, struct cache_entry *ce);void remove_name_hash (struct index_state *istate,struct cache_entry *ce);void free_name_hash (struct index_state *istate); +void ensure_full_index (struct index_state *istate);/* Cache entry creation and cleanup */ diff-lib.c +2
-0@@ -96,6 +96,8 @@ int run_diff_files(struct rev_info *revs, unsigned int option)uint64_t start =getnanotime ();struct index_state *istate = revs->diffopt.repo->index; +ensure_full_index (istate); +diff_set_mnemonic_prefix (&revs->diffopt,"i/" ,"w/" );refresh_fsmonitor (istate);diff.c +2
-0@@ -3901,6 +3901,8 @@ static int reuse_worktree_file(struct index_state *istate,if (!want_file &&would_convert_to_git (istate, name))return 0 ; +ensure_full_index (istate); + len =strlen (name); pos =index_name_pos (istate, name, len);if (pos <0 )dir.c +13
-1@@ -892,13 +892,15 @@ void add_pattern(const char *string, const char *base,add_pattern_to_hashsets (pl, pattern); } -static int read_skip_worktree_file_from_index (const struct index_state *istate, +static int read_skip_worktree_file_from_index (struct index_state *istate,const char *path,size_t *size_out,char **data_out,struct oid_stat *oid_stat) {int pos, len; +ensure_full_index (istate); + len =strlen (path); pos =index_name_pos (istate, path, len);if (pos <0 )@@ -1088,6 +1090,10 @@ static int add_patterns(const char *fname, const char *base, int baselen,close (fd);if (oid_stat) {int pos; + +if (istate) +ensure_full_index (istate); +if (oid_stat->valid && !match_stat_data_racy (istate, &oid_stat->stat, &st)) ;/* no content change, oid_stat->oid still good */ @@ -1696,6 +1702,8 @@ static enum exist_status directory_exists_in_index(struct index_state *istate,if (ignore_case)return directory_exists_in_index_icase (istate, dirname, len); +ensure_full_index (istate); + pos =index_name_pos (istate, dirname, len);if (pos <0 ) pos = -pos-1 ;@@ -2050,6 +2058,8 @@ static int get_index_dtype(struct index_state *istate,int pos;const struct cache_entry *ce; +ensure_full_index (istate); + ce =index_file_exists (istate, path, len,0 );if (ce) {if (!ce_uptodate (ce))@@ -3536,6 +3546,8 @@ static void connect_wt_gitdir_in_nested(const char *sub_worktree,if (repo_read_index (&subrepo) <0 )die (_ ("index file corrupt in repo %s" ), subrepo.gitdir); +ensure_full_index (subrepo.index); +for (i =0 ; i < subrepo.index->cache_nr; i++) {const struct cache_entry *ce = subrepo.index->cache[i];entry.c +2
-0@@ -412,6 +412,8 @@ static void mark_colliding_entries(const struct checkout *state, ce->ce_flags |=CE_MATCHED ; +ensure_full_index (state->istate); +for (i =0 ; i < state->istate->cache_nr; i++) {struct cache_entry *dup = state->istate->cache[i];fsmonitor.c +10
-1@@ -97,6 +97,9 @@ int read_fsmonitor_extension(struct index_state *istate, const void *data,void fill_fsmonitor_bitmap (struct index_state *istate) {unsigned int i, skipped =0 ; + +ensure_full_index (istate); + istate->fsmonitor_dirty =ewah_new ();for (i =0 ; i < istate->cache_nr; i++) {if (istate->cache[i]->ce_flags &CE_REMOVE )@@ -158,7 +161,11 @@ static int query_fsmonitor(int version, const char *last_update, struct strbuf *static void fsmonitor_refresh_callback (struct index_state *istate,const char *name) { -int pos =index_name_pos (istate, name,strlen (name)); +int pos; + +ensure_full_index (istate); + + pos =index_name_pos (istate, name,strlen (name));if (pos >=0 ) {struct cache_entry *ce = istate->cache[pos];@@ -330,6 +337,8 @@ void tweak_fsmonitor(struct index_state *istate)if (istate->fsmonitor_dirty) {if (fsmonitor_enabled) { +ensure_full_index (istate); +/* Mark all entries valid */ for (i =0 ; i < istate->cache_nr; i++) { istate->cache[i]->ce_flags |=CE_FSMONITOR_VALID ;merge-recursive.c +19
-3@@ -522,6 +522,8 @@ static struct string_list *get_unmerged(struct index_state *istate) unmerged->strdup_strings =1 ; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {struct string_list_item *item;struct stage_data *e;@@ -762,6 +764,8 @@ static int dir_in_way(struct index_state *istate, const char *path,strbuf_addstr (&dirpath, path);strbuf_addch (&dirpath,'/' ); +ensure_full_index (istate); + pos =index_name_pos (istate, dirpath.buf, dirpath.len);if (pos <0 )@@ -785,9 +789,13 @@ static int dir_in_way(struct index_state *istate, const char *path,static int was_tracked_and_matches (struct merge_options *opt,const char *path,const struct diff_filespec *blob) { -int pos =index_name_pos (&opt->priv->orig_index, path,strlen (path)); +int pos;struct cache_entry *ce; +ensure_full_index (&opt->priv->orig_index); + + pos =index_name_pos (&opt->priv->orig_index, path,strlen (path)); +if (0 > pos)/* we were not tracking this path before the merge */ return 0 ;@@ -802,7 +810,11 @@ static int was_tracked_and_matches(struct merge_options *opt, const char *path,*/ static int was_tracked (struct merge_options *opt,const char *path) { -int pos =index_name_pos (&opt->priv->orig_index, path,strlen (path)); +int pos; + +ensure_full_index (&opt->priv->orig_index); + + pos =index_name_pos (&opt->priv->orig_index, path,strlen (path));if (0 <= pos)/* we were tracking this path before the merge */ @@ -814,6 +826,9 @@ static int was_tracked(struct merge_options *opt, const char *path)static int would_lose_untracked (struct merge_options *opt,const char *path) {struct index_state *istate = opt->repo->index; +int pos; + +ensure_full_index (istate);/* * This may look like it can be simplified to: @@ -832,7 +847,7 @@ static int would_lose_untracked(struct merge_options *opt, const char *path)* update_file()/would_lose_untracked(); see every comment in this * file which mentions "update_stages". */ -int pos =index_name_pos (istate, path,strlen (path)); + pos =index_name_pos (istate, path,strlen (path));if (pos <0 ) pos =-1 - pos;@@ -3086,6 +3101,7 @@ static int handle_content_merge(struct merge_file_info *mfi,* flag to avoid making the file appear as if it were * deleted by the user. */ +ensure_full_index (&opt->priv->orig_index); pos =index_name_pos (&opt->priv->orig_index, path,strlen (path)); ce = opt->priv->orig_index.cache[pos];if (ce_skip_worktree (ce)) {name-hash.c +6
-0@@ -679,6 +679,8 @@ int index_dir_exists(struct index_state *istate, const char *name, int namelen) {struct dir_entry *dir; +ensure_full_index (istate); +lazy_init_name_hash (istate); dir =find_dir_entry (istate, name, namelen);return dir && dir->nr;@@ -689,6 +691,8 @@ void adjust_dirname_case(struct index_state *istate, char *name)const char *startPtr = name;const char *ptr = startPtr; +ensure_full_index ( istate); +lazy_init_name_hash (istate);while (*ptr) {while (*ptr && *ptr !='/' )@@ -712,6 +716,8 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *nastruct cache_entry *ce;unsigned int hash =memihash (name, namelen); +ensure_full_index (istate); +lazy_init_name_hash (istate); ce = hashmap_get_entry_from_hash(&istate->name_hash, hash, NULL,pathspec.c +3
-2@@ -20,7 +20,7 @@* to use find_pathspecs_matching_against_index() instead. */ void add_pathspec_matches_against_index (const struct pathspec *pathspec, -const struct index_state *istate, +struct index_state *istate,char *seen) {int num_unmatched =0 , i;@@ -36,6 +36,7 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec, num_unmatched++;if (!num_unmatched)return ; +ensure_full_index (istate);for (i =0 ; i < istate->cache_nr; i++) {const struct cache_entry *ce = istate->cache[i];ce_path_match (istate, ce, pathspec, seen);@@ -51,7 +52,7 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,* given pathspecs achieves against all items in the index. */ char *find_pathspecs_matching_against_index (const struct pathspec *pathspec, -const struct index_state *istate) +struct index_state *istate) {char *seen =xcalloc (pathspec->nr,1 );add_pathspec_matches_against_index (pathspec, istate, seen);pathspec.h +2
-2@@ -150,10 +150,10 @@ static inline int ps_strcmp(const struct pathspec_item *item, }void add_pathspec_matches_against_index (const struct pathspec *pathspec, -const struct index_state *istate, +struct index_state *istate,char *seen);char *find_pathspecs_matching_against_index (const struct pathspec *pathspec, -const struct index_state *istate); +struct index_state *istate);int match_pathspec_attrs (const struct index_state *istate,const char *name,int namelen,const struct pathspec_item *item);read-cache.c +17
-2@@ -622,7 +622,11 @@ void remove_marked_cache_entries(struct index_state *istate, int invalidate)int remove_file_from_index (struct index_state *istate,const char *path) { -int pos =index_name_pos (istate, path,strlen (path)); +int pos; + +ensure_full_index (istate); + + pos =index_name_pos (istate, path,strlen (path));if (pos <0 ) pos = -pos-1 ;cache_tree_invalidate_path (istate, path);@@ -640,9 +644,12 @@ static int compare_name(struct cache_entry *ce, const char *path, int namelen)static int index_name_pos_also_unmerged (struct index_state *istate,const char *path,int namelen) { -int pos =index_name_pos (istate, path, namelen); +int pos;struct cache_entry *ce; +ensure_full_index (istate); + + pos =index_name_pos (istate, path, namelen);if (pos >=0 )return pos;@@ -717,6 +724,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,int hash_flags =HASH_WRITE_OBJECT ;struct object_id oid; +ensure_full_index (istate); +if (flags &ADD_CACHE_RENORMALIZE ) hash_flags |=HASH_RENORMALIZE ;@@ -1095,6 +1104,8 @@ static int has_dir_name(struct index_state *istate,size_t len_eq_last;int cmp_last =0 ; +ensure_full_index (istate); +/* * We are frequently called during an iteration on a sorted * list of pathnames and while building a new index. Therefore, @@ -1338,6 +1349,8 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti {int pos; +ensure_full_index (istate); +if (option &ADD_CACHE_JUST_APPEND ) pos = istate->cache_nr;else {@@ -1547,6 +1560,8 @@ int refresh_index(struct index_state *istate, unsigned int flags,* we only have to do the special cases that are left. */ preload_index (istate, pathspec,0 ); + +ensure_full_index (istate);for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce, *new_entry;int cache_errno =0 ;rerere.c +2
-0@@ -962,6 +962,8 @@ static int handle_cache(struct index_state *istate,struct rerere_io_mem io;int marker_size =ll_merge_marker_size (istate, path); +ensure_full_index (istate); +/* * Reproduce the conflicted merge in-core */ resolve-undo.c +6
-0@@ -125,6 +125,8 @@ int unmerge_index_entry_at(struct index_state *istate, int pos)if (!istate->resolve_undo)return pos; +ensure_full_index (istate); + ce = istate->cache[pos];if (ce_stage (ce)) {/* already unmerged */ @@ -172,6 +174,8 @@ void unmerge_marked_index(struct index_state *istate)if (!istate->resolve_undo)return ; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {const struct cache_entry *ce = istate->cache[i];if (ce->ce_flags &CE_MATCHED )@@ -186,6 +190,8 @@ void unmerge_index(struct index_state *istate, const struct pathspec *pathspec)if (!istate->resolve_undo)return ; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {const struct cache_entry *ce = istate->cache[i];if (!ce_path_match (istate, ce, pathspec,NULL ))sha1-name.c +3
-0@@ -1734,6 +1734,8 @@ static void diagnose_invalid_index_path(struct repository *r,if (!prefix) prefix ="" ; +ensure_full_index (r->index); +/* Wrong stage number? */ pos =index_name_pos (istate, filename, namelen);if (pos <0 )@@ -1854,6 +1856,7 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,if (!repo->index || !repo->index->cache)repo_read_index (repo); +ensure_full_index (repo->index); pos =index_name_pos (repo->index, cp, namelen);if (pos <0 ) pos = -pos -1 ;split-index.c +2
-0@@ -4,6 +4,8 @@struct split_index *init_split_index (struct index_state *istate) { +ensure_full_index (istate); +if (!istate->split_index) { istate->split_index =xcalloc (1 ,sizeof (*istate->split_index)); istate->split_index->refcount =1 ;submodule.c +19
-5@@ -33,9 +33,13 @@ static struct oid_array ref_tips_after_fetch;* will be disabled because we can't guess what might be configured in * .gitmodules unless the user resolves the conflict. */ -int is_gitmodules_unmerged (const struct index_state *istate) +int is_gitmodules_unmerged (struct index_state *istate) { -int pos =index_name_pos (istate,GITMODULES_FILE ,strlen (GITMODULES_FILE )); +int pos; + +ensure_full_index (istate); + + pos =index_name_pos (istate,GITMODULES_FILE ,strlen (GITMODULES_FILE ));if (pos <0 ) {/* .gitmodules not found or isn't merged */ pos =-1 - pos;if (istate->cache_nr > pos) {/* there is a .gitmodules */ @@ -77,7 +81,11 @@ int is_writing_gitmodules_ok(void)*/ int is_staging_gitmodules_ok (struct index_state *istate) { -int pos =index_name_pos (istate,GITMODULES_FILE ,strlen (GITMODULES_FILE )); +int pos; + +ensure_full_index (istate); + + pos =index_name_pos (istate,GITMODULES_FILE ,strlen (GITMODULES_FILE ));if ((pos >=0 ) && (pos < istate->cache_nr)) {struct stat st;@@ -301,7 +309,7 @@ int is_submodule_populated_gently(const char *path, int *return_error_code)/* * Dies if the provided 'prefix' corresponds to an unpopulated submodule */ -void die_in_unpopulated_submodule (const struct index_state *istate, +void die_in_unpopulated_submodule (struct index_state *istate,const char *prefix) {int i, prefixlen;@@ -311,6 +319,8 @@ void die_in_unpopulated_submodule(const struct index_state *istate, prefixlen =strlen (prefix); +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce = istate->cache[i];int ce_len =ce_namelen (ce);@@ -331,11 +341,13 @@ void die_in_unpopulated_submodule(const struct index_state *istate,/* * Dies if any paths in the provided pathspec descends into a submodule */ -void die_path_inside_submodule (const struct index_state *istate, +void die_path_inside_submodule (struct index_state *istate,const struct pathspec *ps) {int i, j; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce = istate->cache[i];int ce_len =ce_namelen (ce);@@ -1420,6 +1432,8 @@ static int get_next_submodule(struct child_process *cp, {struct submodule_parallel_fetch *spf = data; +ensure_full_index (spf->r->index); +for (; spf->count < spf->r->index->cache_nr; spf->count++) {const struct cache_entry *ce = spf->r->index->cache[spf->count];const char *default_argv;submodule.h +3
-3@@ -39,7 +39,7 @@ struct submodule_update_strategy { };#define SUBMODULE_UPDATE_STRATEGY_INIT {SM_UPDATE_UNSPECIFIED, NULL} -int is_gitmodules_unmerged (const struct index_state *istate); +int is_gitmodules_unmerged (struct index_state *istate);int is_writing_gitmodules_ok (void );int is_staging_gitmodules_ok (struct index_state *istate);int update_path_in_gitmodules (const char *oldpath,const char *newpath);@@ -60,9 +60,9 @@ int is_submodule_active(struct repository *repo, const char *path);* Otherwise the return error code is the same as of resolve_gitdir_gently. */ int is_submodule_populated_gently (const char *path,int *return_error_code); -void die_in_unpopulated_submodule (const struct index_state *istate, +void die_in_unpopulated_submodule (struct index_state *istate,const char *prefix); -void die_path_inside_submodule (const struct index_state *istate, +void die_path_inside_submodule (struct index_state *istate,const struct pathspec *ps);enum submodule_update_type parse_submodule_update_type (const char *value); int parse_submodule_update_strategy(const char *value,tree.c +2
-0@@ -170,6 +170,8 @@ int read_tree(struct repository *r, struct tree *tree, int stage,* to matter. */ +ensure_full_index (istate); +/* * See if we have cache entry at the stage. If so, * do it the original slow way, otherwise, append and then wt-status.c +7
-1@@ -509,6 +509,8 @@ static int unmerged_mask(struct index_state *istate, const char *path)int pos, mask;const struct cache_entry *ce; +ensure_full_index (istate); + pos =index_name_pos (istate, path,strlen (path));if (0 <= pos)return 0 ;@@ -657,6 +659,8 @@ static void wt_status_collect_changes_initial(struct wt_status *s)struct index_state *istate = s->repo->index;int i; +ensure_full_index (istate); +for (i =0 ; i < istate->cache_nr; i++) {struct string_list_item *it;struct wt_status_change_data *d;@@ -2295,6 +2299,9 @@ static void wt_porcelain_v2_print_unmerged_entry(*/ memset (stages,0 ,sizeof (stages)); sum =0 ; + +ensure_full_index (istate); + pos =index_name_pos (istate, it->string,strlen (it->string));assert (pos <0 ); pos = -pos-1 ; --gitgitgadget
Re: [PATCH 15/27] [RFC-VERSION] *: ensure full index
Quoted 30 lines · Elijah Newren
Quoted 18 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
This giant patch is not intended for actual review. I have a branch that has these changes split out in a sane way with some commentary in each file that is modified.
The idea here is to guard certain portions of the codebase that do not know how to handle sparse indexes by ensuring that the index is expanded to a full index before proceeding with the logic.
This also provides a good mechanism for testing which code needs updating to enable the sparse index in a Git builtin. The builtin can set
the_repository->settings.command_requires_full_indexto zero and then we can debug the command with a breakpoint onensure_full_index(). That identifies the portion of code that needs adjusting before enabling sparse indexes for that command.Quoted 6 lines
Some index operations must be changed to operate on a non-const pointer, since ensuring a full index will modify the index itself.
There are likely some gaps to these protections, which is why it will be important to carefully test each scenario as we relax the requirements. I expect that to be a long effort.
I think the idea makes sense; it provides a way for us to incrementally build support for this new feature.
Quoted 14 lines
I skimmed over the code and noticed various interesting places that had the
ensure_full_index()call (e.g.read_skip_worktree_file_from_index()-- whose existence comes from sparsity; what irony...). Better breakouts would be great, so I'll defer commenting much until then. But, just to verify I'm
understanding: the primary defence is thecommand_requires_full_indexsetting, and you have added severalensure_full_index()calls throughout the code in places you believe would need to be fixed up in case someone switches thecommand_requires_full_indexsetting. Is that correct? And your comment on the gaps is just that there may be other places that are missing the secondary protection (as opposed to my first reading of that paragraph as suggesting we aren't sure if we have enough protections yet and need to add more before this moves out of RFC); is that right?
Yes, the idea is that we can incrementally enable command_requires_full_index for some builtins and be confident that corner cases will be protected by ensure_full_index(). Further, we can test whether ensure_full_index() was called using test_region in test scripts to demonstrate that a command is truly "sparse aware" or if it is converting to full and back to sparse.
There is also the case that when we write the index into a sparse format, the in-memory structure is modified. If the index is re-used afterwards, then we must expand to full again for these code paths.
unpack_trees() already has one of these calls because it was necessary for the sparse-index write to work.
The ensure_full_index() pattern also works when updating a builtin to work with the sparse-index because of the breakpoint trick.
When I submit this as a full series, this patch will be one full patch series submission with careful comments about why each of these is added on a file-by-file basis.
Thanks,
-Stolee
PATCH 16/27 unpack-trees: make sparse aware
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
As a first step to integrate 'git status' and 'git add' with the sparse index, we must start integrating unpack_trees() with sparse directory entries. These changes are currently impossible to trigger because unpack_trees() calls ensure_full_index() if command_requires_full_index is true. This is the case for all commands at the moment. As we expand more commands to be sparse-aware, we might find that more changes are required to unpack_trees(). The current changes will suffice for 'status' and 'add'.
unpack_trees() calls the traverse_trees() API using unpack_callback() to decide if we should recurse into a subtree. We must add new abilities to skip a subtree if it corresponds to a sparse directory entry.
It is important to be careful about the trailing directory separator that exists in the sparse directory entries but not in the subtree paths.
--- dir.h | 2 +- preload-index.c | 2 ++ read-cache.c | 3 +++ unpack-trees.c | 24 ++++++++++++++++++++++-- 4 files changed, 28 insertions(+), 3 deletions(-)
dir.h +1 -1
@@ -503,7 +503,7 @@ static inline int ce_path_match(const struct index_state *istate, char *seen) {return match_pathspec (istate, pathspec, ce->name,ce_namelen (ce),0 , seen, -S_ISDIR (ce->ce_mode) ||S_ISGITLINK (ce->ce_mode)); +S_ISSPARSEDIR (ce) ||S_ISDIR (ce->ce_mode) ||S_ISGITLINK (ce->ce_mode)); } static inline int dir_path_match(const struct index_state *istate,
preload-index.c +2 -0
@@ -54,6 +54,8 @@ static void *preload_thread(void *_data)continue ;if (S_ISGITLINK (ce->ce_mode))continue ; +if (S_ISSPARSEDIR (ce)) +continue ;if (ce_uptodate (ce))continue ;if (ce_skip_worktree (ce))
read-cache.c +3 -0
@@ -1572,6 +1572,9 @@ int refresh_index(struct index_state *istate, unsigned int flags,if (ignore_submodules &&S_ISGITLINK (ce->ce_mode))continue ; +if (istate->sparse_index &&S_ISSPARSEDIR (ce)) +continue ; +if (pathspec && !ce_path_match (istate, ce, pathspec, seen)) filtered =1 ;
unpack-trees.c +22 -2
@@ -583,6 +583,13 @@ static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o) { ce->ce_flags |=CE_UNPACKED ; +/* +* If this is a sparse directory, don't advance cache_bottom. +* That will be advanced later using the cache-tree data. +*/ +if (S_ISSPARSEDIR (ce)) +return ; +if (o->cache_bottom < o->src_index->cache_nr && o->src_index->cache[o->cache_bottom] == ce) {int bottom = o->cache_bottom;
@@ -980,6 +987,9 @@ static int do_compare_entry(const struct cache_entry *ce, ce_len -= pathlen; ce_name = ce->name + pathlen; +/* remove directory separator if a sparse directory entry */ +if (S_ISSPARSEDIR (ce)) + ce_len--;return df_name_compare (ce_name, ce_len,S_IFREG , name, namelen, mode); }
@@ -989,6 +999,10 @@ static int compare_entry(const struct cache_entry *ce, const struct traverse_infif (cmp)return cmp; +/* If ce is a sparse directory, then allow equality here. */ +if (S_ISSPARSEDIR (ce)) +return 0 ; +/* * Even if the beginning compared identically, the ce should * compare as bigger than a directory leading up to it!
@@ -1239,6 +1253,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, strstruct cache_entry *src[MAX_UNPACK_TREES +1 ] = {NULL , };struct unpack_trees_options *o = info->data;const struct name_entry *p = names; +unsigned recurse =1 ;/* Find first entry with a real name (we could use "mask" too) */ while (!p->mode)
@@ -1280,12 +1295,16 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str } } src[0 ] = ce; + +if (S_ISSPARSEDIR (ce)) + recurse =0 ; }break ; } } -if (unpack_nondirectories (n, mask, dirmask, src, names, info) <0 ) +if (recurse && +unpack_nondirectories (n, mask, dirmask, src, names, info) <0 )return -1 ;if (o->merge && src[0 ]) {
@@ -1315,7 +1334,8 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str } } -if (traverse_trees_recursive (n, dirmask, mask & ~dirmask, +if (recurse && +traverse_trees_recursive (n, dirmask, mask & ~dirmask, names, info) <0 )return -1 ;return mask;
Signature · 1 line
gitgitgadget
Re: [PATCH 16/27] unpack-trees: make sparse aware
Quoted 12 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
As a first step to integrate 'git status' and 'git add' with the sparse index, we must start integrating
unpack_trees()with sparse directory entries. These changes are currently impossible to trigger becauseunpack_trees()callsensure_full_index()ifcommand_requires_full_indexis true. This is the case for all commands at the moment. As we expand more commands to be sparse-aware, we might find that more changes are required tounpack_trees(). The current changes will suffice for 'status' and 'add'.
Quoted 3 lines
unpack_trees()calls thetraverse_trees()API usingunpack_callback()to decide if we should recurse into a subtree. We must add new abilities to skip a subtree if it corresponds to a sparse directory entry.
Makes sense.
Quoted 3 lines
It is important to be careful about the trailing directory separator that exists in the sparse directory entries but not in the subtree paths.
The comment makes me wonder if leaving the trailing directory separator out would be better, as it'd allow direct comparisons. Of course, you have a better idea of what is easier or harder based on this decision. Is there any chance you have a quick list of the places that the code was simplified by this decision and a list of places like this one that were made slightly harder?
Quoted 13 lines
Signed-off-by Derrick Stolee <[email protected]>--- dir.h | 2 +- preload-index.c | 2 ++ read-cache.c | 3 +++ unpack-trees.c | 24 ++++++++++++++++++++++-- 4 files changed, 28 insertions(+), 3 deletions(-)dir.h +0
-0
dir.h static inline int ce_path_match(const struct index_state *istate, · line 506
Quoted 5 lines
dir.h +1
-1@@ -503,7 +503,7 @@ static inline int ce_path_match(const struct index_state *istate, char *seen) {return match_pathspec (istate, pathspec, ce->name,ce_namelen (ce),0 , seen, -S_ISDIR (ce->ce_mode) ||S_ISGITLINK (ce->ce_mode)); +S_ISSPARSEDIR (ce) ||S_ISDIR (ce->ce_mode) ||S_ISGITLINK (ce->ce_mode));
I think this hunk becomes unnecessary if you use ce_mode = 040000 for sparse directory entries.
Quoted 35 lines
dir.h +0
-0} static inline int dir_path_match(const struct index_state *istate,preload-index.c +2
-0@@ -54,6 +54,8 @@ static void *preload_thread(void *_data)continue ;if (S_ISGITLINK (ce->ce_mode))continue ; +if (S_ISSPARSEDIR (ce)) +continue ;if (ce_uptodate (ce))continue ;if (ce_skip_worktree (ce))read-cache.c +3
-0@@ -1572,6 +1572,9 @@ int refresh_index(struct index_state *istate, unsigned int flags,if (ignore_submodules &&S_ISGITLINK (ce->ce_mode))continue ; +if (istate->sparse_index &&S_ISSPARSEDIR (ce)) +continue ; +if (pathspec && !ce_path_match (istate, ce, pathspec, seen)) filtered =1 ;unpack-trees.c +0
-0
unpack-trees.c static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o) · line 591
Quoted 9 lines
unpack-trees.c +6
-0@@ -583,6 +583,13 @@ static void mark_ce_used(struct cache_entry *ce, struct unpack_trees_options *o) { ce->ce_flags |=CE_UNPACKED ; +/* +* If this is a sparse directory, don't advance cache_bottom. +* That will be advanced later using the cache-tree data. +*/ +if (S_ISSPARSEDIR (ce)) +return ;
I don't grok the cache_bottom stuff -- in general, nothing specific about your patch. But since I don't grok that stuff, it means I don't understand how your comment here relates; you may want to ping another reviewer about this portion of the patch.
unpack-trees.c static int do_compare_entry(const struct cache_entry *ce, · line 992
Quoted 11 lines
unpack-trees.c +4
-0+if (o->cache_bottom < o->src_index->cache_nr && o->src_index->cache[o->cache_bottom] == ce) {int bottom = o->cache_bottom;@@ -980,6 +987,9 @@ static int do_compare_entry(const struct cache_entry *ce, ce_len -= pathlen; ce_name = ce->name + pathlen; +/* remove directory separator if a sparse directory entry */ +if (S_ISSPARSEDIR (ce)) + ce_len--;
Here's where your comment about trailing separator comes in; makes sense.
unpack-trees.c static int compare_entry(const struct cache_entry *ce, const struct traverse_inf · line 1005
Quoted 11 lines
unpack-trees.c +4
-0return df_name_compare (ce_name, ce_len,S_IFREG , name, namelen, mode); }@@ -989,6 +999,10 @@ static int compare_entry(const struct cache_entry *ce, const struct traverse_infif (cmp)return cmp; +/* If ce is a sparse directory, then allow equality here. */ +if (S_ISSPARSEDIR (ce)) +return 0 ; +
This seems surprising to me. Is there a chance you are comparing sparse directory A with sparse directory B and you return with equality? Or sparse_directory A with regular file B? Do the callers still do the right thing? If your code change here is right, it seems like it deserves an extra comment either in the code or the commit message.
Quoted 30 lines
unpack-trees.c +6
-1/* * Even if the beginning compared identically, the ce should * compare as bigger than a directory leading up to it! @@ -1239,6 +1253,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, strstruct cache_entry *src[MAX_UNPACK_TREES +1 ] = {NULL , };struct unpack_trees_options *o = info->data;const struct name_entry *p = names; +unsigned recurse =1 ;/* Find first entry with a real name (we could use "mask" too) */ while (!p->mode)@@ -1280,12 +1295,16 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str } } src[0 ] = ce; + +if (S_ISSPARSEDIR (ce)) + recurse =0 ; }break ; } } -if (unpack_nondirectories (n, mask, dirmask, src, names, info) <0 ) +if (recurse && +unpack_nondirectories (n, mask, dirmask, src, names, info) <0 )return -1 ;if (o->merge && src[0 ]) {
unpack-trees.c static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str · line 1341
Quoted 9 lines
unpack-trees.c +2
-1@@ -1315,7 +1334,8 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str } } -if (traverse_trees_recursive (n, dirmask, mask & ~dirmask, +if (recurse && +traverse_trees_recursive (n, dirmask, mask & ~dirmask, names, info) <0 )return -1 ;return mask;
The unpack_callback() code has some comparison to a cache-tree, but I'd assume that you'd need to update cache-tree.c somewhat to take advantage of these sparse directory entries. Am I wrong, and you just get cache-tree.c working with sparse directory entries for free? Or is this something coming in a later patch?
Re: [PATCH 16/27] unpack-trees: make sparse aware
Quoted 6 lines · Elijah Newren
Quoted 3 lines · Derrick Stolee via GitGitGadget
It is important to be careful about the trailing directory separator that exists in the sparse directory entries but not in the subtree paths.
Quoted 6 lines
The comment makes me wonder if leaving the trailing directory separator out would be better, as it'd allow direct comparisons. Of course, you have a better idea of what is easier or harder based on this decision. Is there any chance you have a quick list of the places that the code was simplified by this decision and a list of places like this one that were made slightly harder?
I'm going through all of your comments and making notes about areas to fix and clean up before starting a new series for full review.
This question of the trailing slash is important, and I will take particular care about answering it as I rework the series. However, the questions in this patch poke at the right places...
Quoted 13 lines
unpack-trees.c static int do_compare_entry(const struct cache_entry *ce, · line 992
Quoted 3 lines
unpack-trees.c +3
-0+/* remove directory separator if a sparse directory entry */ +if (S_ISSPARSEDIR (ce)) + ce_len--;Here's where your comment about trailing separator comes in; makes sense.
Quoted 7 lines
return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode);}
unpack-trees.c +0
-0@@ -989,6 +999,10 @@ static int compare_entry(const struct cache_entry *ce, const struct traverse_infif (cmp)return cmp;
Quoted 11 lines
unpack-trees.c static int compare_entry(const struct cache_entry *ce, const struct traverse_inf · line 1005
Quoted 4 lines
unpack-trees.c +4
-0+/* If ce is a sparse directory, then allow equality here. */ +if (S_ISSPARSEDIR (ce)) +return 0 ; +This seems surprising to me. Is there a chance you are comparing sparse directory A with sparse directory B and you return with equality? Or
sparse_directoryA with regular file B? Do the callers still do the right thing? If your code change here is right, it seems like it deserves an extra comment either in the code or the commit message.
Sometimes a caller is asking for the first index entry corresponding to a directory. In these cases, the input could be "A/B/C/". We want to ensure that a sparse directory entry corresponding exactly to that directory is correctly matched. If we place "A/B/C" in the index instead, this search becomes more complicated (I think; I will justify this more after thinking about it).
At this point in time, we are just saying "We found the entry with equal path value!" and not failing with the check in the rest of the method:
/* * Even if the beginning compared identically, the ce should * compare as bigger than a directory leading up to it! */ return ce_namelen(ce) > traverse_path_len(info, tree_entry_len(n));
Quoted 7 lines
unpack-trees.c static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str · line 1341
Quoted 6 lines
unpack-trees.c +2
-1-if (traverse_trees_recursive (n, dirmask, mask & ~dirmask, +if (recurse && +traverse_trees_recursive (n, dirmask, mask & ~dirmask, names, info) <0 )return -1 ;return mask;
Quoted 5 lines
The
unpack_callback()code has some comparison to a cache-tree, but I'd assume that you'd need to updatecache-tree.csomewhat to take advantage of these sparse directory entries. Am I wrong, and you just getcache-tree.cworking with sparse directory entries for free? Or is this something coming in a later patch?
In the RFC, I integrate the cache-tree with the sparse-index at the very end. I will move that integration to be much earlier in the next submission, so it becomes part of the format discussion.
Thanks,
-Stolee
PATCH 17/27 dir.c: accept a directory as part of cone-mode patterns
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
When we have sparse directory entries in the index, we want to compare that directory against sparse-checkout patterns. Those pattern matching algorithms are built expecting a file path, not a directory path. This is especially important in the "cone mode" patterns which will match files that exist within the "parent directories" as well as the recursive directory matches.
If path_matches_pattern_list() is given a directory, we can add a bogus filename ("-") to the directory and get the same results as before, assuming we are in cone mode. Since sparse index requires cone mode patterns, this is an acceptable assumption.
--- dir.c | 5 +++++ 1 file changed, 5 insertions(+)
dir.c +5 -0
@@ -1384,6 +1384,11 @@ enum pattern_match_result path_matches_pattern_list(strbuf_addch (&parent_pathname,'/' );strbuf_add (&parent_pathname, pathname, pathlen); +/* Directory requests should be added as if they are a file */ +if (parent_pathname.len >1 && + parent_pathname.buf[parent_pathname.len -1 ] =='/' ) +strbuf_add (&parent_pathname,"-" ,1 ); +if (hashmap_contains_path (&pl->recursive_hashmap, &parent_pathname)) { result =MATCHED_RECURSIVE ;
Signature · 1 line
gitgitgadget
Re: [PATCH 17/27] dir.c: accept a directory as part of cone-mode patterns
Quoted 10 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
When we have sparse directory entries in the index, we want to compare that directory against sparse-checkout patterns. Those pattern matching algorithms are built expecting a file path, not a directory path. This is especially important in the "cone mode" patterns which will match files that exist within the "parent directories" as well as the recursive directory matches.
Quoted 4 lines
If
path_matches_pattern_list()is given a directory, we can add a bogus filename ("-") to the directory and get the same results as before, assuming we are in cone mode. Since sparse index requires cone mode patterns, this is an acceptable assumption.
Why is "-" a bogus filename? Is that only on certain operating systems, or are you just not expecting a user to name their file with such a bad name? What if there is a file with that name in that directory in the repository; do you need the pathname to be bogus?
What do you mean by "get the same results as before"? The first paragraph suggests the code wouldn't handle a directory path, and that not handling it was problematic, so it seems unlikely you want the same results as that. But it's not clear what the "before" refers to here.
Quoted 10 lines
Signed-off-by Derrick Stolee <[email protected]>--- dir.c | 5 +++++ 1 file changed, 5 insertions(+)dir.c +0
-0
dir.c enum pattern_match_result path_matches_pattern_list( · line 1389
Quoted 6 lines
dir.c +3
-0@@ -1384,6 +1384,11 @@ enum pattern_match_result path_matches_pattern_list(strbuf_addch (&parent_pathname,'/' );strbuf_add (&parent_pathname, pathname, pathlen); +/* Directory requests should be added as if they are a file */ +if (parent_pathname.len >1 && + parent_pathname.buf[parent_pathname.len -1 ] =='/' )
Ah, this looks like a case where the trailing slash is helpful; without it, you might have to feed extra data in through the call hierarchy to signify that this is a directory entry.
dir.c enum pattern_match_result path_matches_pattern_list( · line 1394
Quoted 5 lines
dir.c +2
-0+strbuf_add (&parent_pathname,"-" ,1 ); +if (hashmap_contains_path (&pl->recursive_hashmap, &parent_pathname)) { result =MATCHED_RECURSIVE ;
hashmap_contains_path? Don't we already know (modulo special cases of our bogus value not quite being bogus enough) that this is false since we were adding a bogus path? How could the hashmap have a bogus value in it? Won't this particular call fail with or without our adding "-" to the end of the path?
After this hashmap_contains_path() call, the subsequent code looks for the parent of the path by stripping off everything after the last '/'...which seems like the relevant code anyway. Is the problem that the hashmap_contains_path() call was returning true when we didn't add "-" to the end? If so, can we use and if or a goto instead to make the code skip this first check and move on to where we want it to go?
Or am I misunderstanding something about this code?
PATCH 18/27 status: use sparse-index throughout
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
By testing 'git -c core.fsmonitor= status -uno', we can check for the simplest index operations that can be made sparse-aware. The necessary implementation details are already integrated with sparse-checkout, so modify command_requires_full_index to be zero for cmd_status().
By running the debugger for 'git status -uno' after that change, we find two instances of ensure_full_index() that were added for extra safety, but can be removed without issue.
In refresh_index(), we loop through the index entries. The refresh_cache_ent() method copies the sparse directories into the refreshed index without issue.
The loop within run_diff_files() skips things that are in stage 0 and have skip-worktree enabled, so seems safe to disable ensure_full_index() here.
While this change avoids calling ensure_full_index(), it actually slows 'git status' because we do not have the cache-tree extension to help us.
--- builtin/commit.c | 3 +++ diff-lib.c | 2 -- read-cache.c | 1 - t/t1092-sparse-checkout-compatibility.sh | 10 +++++++--- 4 files changed, 10 insertions(+), 6 deletions(-)
builtin/commit.c +3 -0
@@ -1400,6 +1400,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)if (argc ==2 && !strcmp (argv[1 ],"-h" ))usage_with_options (builtin_status_usage, builtin_status_options); +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; +status_init_config (&s, git_status_config); argc = parse_options(argc, argv, prefix, builtin_status_options,
diff-lib.c +0 -2
@@ -96,8 +96,6 @@ int run_diff_files(struct rev_info *revs, unsigned int option)uint64_t start =getnanotime ();struct index_state *istate = revs->diffopt.repo->index; -ensure_full_index (istate); -diff_set_mnemonic_prefix (&revs->diffopt,"i/" ,"w/" );refresh_fsmonitor (istate);
read-cache.c +0 -1
@@ -1561,7 +1561,6 @@ int refresh_index(struct index_state *istate, unsigned int flags,*/ preload_index (istate, pathspec,0 ); -ensure_full_index (istate);for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce, *new_entry;int cache_errno =0 ;
t/t1092-sparse-checkout-compatibility.sh +7 -3
@@ -382,12 +382,16 @@ test_expect_success 'sparse-index is expanded and converted back' ' GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ git -C sparse-index -c core.fsmonitor="" reset --hard && test_region index convert_to_sparse trace2.txt && - test_region index ensure_full_index trace2.txt && + test_region index ensure_full_index trace2.txt +' - rm trace2.txt && +test_expect_success 'sparse-index is not expanded' ' + init_repos && + + rm -f trace2.txt && GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ git -C sparse-index -c core.fsmonitor="" status -uno && - test_region index ensure_full_index trace2.txt + test_region ! index ensure_full_index trace2.txt ' test_done
Signature · 1 line
gitgitgadget
PATCH 19/27 status: skip sparse-checkout percentage with sparse-index
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
'git status' began reporting a percentage of populated paths when sparse-checkout is enabled in 051df3cf (wt-status: show sparse checkout status as well, 2020-07-18). This percentage is incorrect when the index has sparse directories. It would also be expensive to calculate as we would need to parse trees to count the total number of possible paths.
Avoid the expensive computation by simplifying the output to only report that a sparse checkout exists, without the percentage.
--- t/t1092-sparse-checkout-compatibility.sh | 8 ++++++++ wt-status.c | 14 +++++++++++--- wt-status.h | 1 + 3 files changed, 20 insertions(+), 3 deletions(-)
t/t1092-sparse-checkout-compatibility.sh +8 -0
@@ -190,6 +190,14 @@ test_expect_success 'status with options' ' test_all_match git status --porcelain=v2 -uno ' +test_expect_success 'status reports sparse-checkout' ' + init_repos && + git -C sparse-checkout status >full && + git -C sparse-index status >sparse && + test_i18ngrep "You are in a sparse checkout with " full && + test_i18ngrep "You are in a sparse checkout." sparse +' + test_expect_success 'add, commit, checkout' ' init_repos &&
wt-status.c +11 -3
@@ -1492,9 +1492,12 @@ static void show_sparse_checkout_in_use(struct wt_status *s,if (s->state.sparse_checkout_percentage ==SPARSE_CHECKOUT_DISABLED )return ; -status_printf_ln (s, color, -_ ("You are in a sparse checkout with %d%% of tracked files present." ), - s->state.sparse_checkout_percentage); +if (s->state.sparse_checkout_percentage ==SPARSE_CHECKOUT_SPARSE_INDEX ) +status_printf_ln (s, color,_ ("You are in a sparse checkout." )); +else +status_printf_ln (s, color, +_ ("You are in a sparse checkout with %d%% of tracked files present." ), + s->state.sparse_checkout_percentage);wt_longstatus_print_trailer (s); }
@@ -1652,6 +1655,11 @@ static void wt_status_check_sparse_checkout(struct repository *r,return ; } +if (r->index->sparse_index) { + state->sparse_checkout_percentage =SPARSE_CHECKOUT_SPARSE_INDEX ; +return ; + } +for (i =0 ; i < r->index->cache_nr; i++) {struct cache_entry *ce = r->index->cache[i];if (ce_skip_worktree (ce))
wt-status.h +1 -0
@@ -80,6 +80,7 @@ enum wt_status_format {#define HEAD_DETACHED_AT _("HEAD detached at ")#define HEAD_DETACHED_FROM _("HEAD detached from ")#define SPARSE_CHECKOUT_DISABLED -1 +#define SPARSE_CHECKOUT_SPARSE_INDEX -2struct wt_status_state {int merge_in_progress;
Signature · 1 line
gitgitgadget
PATCH 20/27 sparse-index: expand_to_path() trivial implementation
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
Before we check if a specific file or directory exists in the index, it would be good to see if a leading directory is a sparse-directory. If so, we will want to expand the index _just enough_ to be sure that the paths we are interested in are in the index.
The actually-interesting implementation will follow in a later change. For now, simply call ensure_full_index() to expand every directory simultaneously.
Calls like index_dir_exists(), adjust_dirname_case(), and index_file_exists() in name-hash.c can trust the name hash if the index was properly expanded for the requested names. These methods can transition from ensure_full_index() to expand_to_path().
--- name-hash.c | 10 ++++------ sparse-index.c | 7 +++++++ sparse-index.h | 12 ++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-)
name-hash.c +4 -6
@@ -8,6 +8,7 @@#include "cache.h" #include "thread-utils.h" #include "trace2.h" +#include "sparse-index.h" struct dir_entry {struct hashmap_entry ent;
@@ -679,9 +680,8 @@ int index_dir_exists(struct index_state *istate, const char *name, int namelen) {struct dir_entry *dir; -ensure_full_index (istate); -lazy_init_name_hash (istate); +expand_to_path (istate, name, namelen,0 ); dir =find_dir_entry (istate, name, namelen);return dir && dir->nr; }
@@ -691,9 +691,8 @@ void adjust_dirname_case(struct index_state *istate, char *name)const char *startPtr = name;const char *ptr = startPtr; -ensure_full_index ( istate); -lazy_init_name_hash (istate); +expand_to_path (istate, name,strlen (name),0 );while (*ptr) {while (*ptr && *ptr !='/' ) ptr++;
@@ -716,9 +715,8 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *nastruct cache_entry *ce;unsigned int hash =memihash (name, namelen); -ensure_full_index (istate); -lazy_init_name_hash (istate); +expand_to_path (istate, name, namelen, icase); ce =hashmap_get_entry_from_hash (&istate->name_hash, hash,NULL , struct cache_entry, ent);
sparse-index.c +7 -0
@@ -280,3 +280,10 @@ void ensure_full_index(struct index_state *istate)trace2_region_leave ("index" ,"ensure_full_index" , istate->repo); } + +void expand_to_path (struct index_state *istate, +const char *path,size_t pathlen,int icase) +{ +/* for now, do the obviously-correct, slow thing */ +ensure_full_index (istate); +}
sparse-index.h +12 -0
@@ -4,6 +4,18 @@struct index_state ;void ensure_full_index (struct index_state *istate);int convert_to_sparse (struct index_state *istate); +/* +* Some places in the codebase expect to search for a specific path. +* This path might be outside of the sparse-checkout definition, in +* which case a sparse-index may not contain a path for that index. +* +* Given an index and a path, check to see if a leading directory for +* 'path' exists in the index as a sparse directory. In that case, +* expand that sparse directory to a full range of cache entries and +* populate the index accordingly. +*/ +void expand_to_path (struct index_state *istate, +const char *path,size_t pathlen,int icase);struct repository ;int set_sparse_index_config (struct repository *repo,int enable);
Signature · 1 line
gitgitgadget
PATCH 21/27 sparse-index: expand_to_path no-op if path exists
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
We need to check the file hashmap first, then look to see if the directory signals a non-sparse directory entry. In such a case, we can rely on the contents of the sparse-index.
We still use ensure_full_index() in the case that we hit a path that is within a sparse-directory entry.
--- name-hash.c | 6 ++++++ sparse-index.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+)
name-hash.c +6 -0
@@ -110,6 +110,12 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)if (ce->ce_flags &CE_HASHED )return ; ce->ce_flags |=CE_HASHED ; + +if (ce->ce_mode ==CE_MODE_SPARSE_DIRECTORY ) { +add_dir_entry (istate, ce); +return ; + } +hashmap_entry_init (&ce->ent,memihash (ce->name,ce_namelen (ce)));hashmap_add (&istate->name_hash, &ce->ent);
sparse-index.c +53 -0
62 lines not on this page · raw message
Signature · 1 line
gitgitgadget
Re: [PATCH 21/27] sparse-index: expand_to_path no-op if path exists
Quoted 42 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
We need to check the file hashmap first, then look to see if the directory signals a non-sparse directory entry. In such a case, we can rely on the contents of the sparse-index.
We still use
ensure_full_index()in the case that we hit a path that is within a sparse-directory entry.Signed-off-by Derrick Stolee <[email protected]>--- name-hash.c | 6 ++++++ sparse-index.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+)name-hash.c +6
-0@@ -110,6 +110,12 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)if (ce->ce_flags &CE_HASHED )return ; ce->ce_flags |=CE_HASHED ; + +if (ce->ce_mode ==CE_MODE_SPARSE_DIRECTORY ) { +add_dir_entry (istate, ce); +return ; + } +hashmap_entry_init (&ce->ent,memihash (ce->name,ce_namelen (ce)));hashmap_add (&istate->name_hash, &ce->ent);sparse-index.c +1
-0@@ -281,9 +281,62 @@ void ensure_full_index(struct index_state *istate)trace2_region_leave ("index" ,"ensure_full_index" , istate->repo); } +static int in_expand_to_path =0 ;
sparse-index.c void ensure_full_index(struct index_state *istate) · line 294
Quoted 10 lines
sparse-index.c +7
-0+void expand_to_path (struct index_state *istate,const char *path,size_t pathlen,int icase) { +struct strbuf path_as_dir =STRBUF_INIT ; +int pos; + +/* prevent extra recursion */ +if (in_expand_to_path) +return ;
Maybe "prevent extra expand_to_path() <-> index_file_exists() recursion", just to be extra explicit?
sparse-index.c void ensure_full_index(struct index_state *istate) · line 300
Quoted 6 lines
sparse-index.c +6
-0+ +if (!istate || !istate->sparse_index) +return ; + +if (!istate->repo) + istate->repo = the_repository;
So, we assume the_repository if istate->repo isn't set. I guess given the number of the_repository assumptions we have in the code, this isn't a big deal. And instead of a USE_THE_REPOSITORY_COMPATIBILITY_MACROS we have a NO_THE_REPOSITORY_COMPATIBILITY_MACROS, so there's nothing to mark this either.
Quoted 7 lines
sparse-index.c +7
-0+ + in_expand_to_path =1 ; + +/* +* We only need to actually expand a region if the +* following are both true: +*
sparse-index.c void ensure_full_index(struct index_state *istate) · line 317
Quoted 10 lines
sparse-index.c +10
-0+* 1. 'path' is not already in the index. +* 2. Some parent directory of 'path' is a sparse directory. +*/ + +strbuf_add (&path_as_dir, path, pathlen); +strbuf_addch (&path_as_dir,'/' ); + +/* in_expand_to_path prevents infinite recursion here */ +if (index_file_exists (istate, path, pathlen, icase)) + goto cleanup;
Shouldn't the editing of path_as_dir be done after the index_file_exists() call? In the case that the entry already exists, writing to path_as_dir is wasted work.
sparse-index.c void ensure_full_index(struct index_state *istate) · line 330
Quoted 12 lines
sparse-index.c +12
-0+ pos =index_name_pos (istate, path_as_dir.buf, path_as_dir.len); + +if (pos <0 ) + pos = -pos -1 ; + +/* +* Even if the path doesn't exist, if the value isn't exactly a +* sparse-directory entry, then there is no need to expand the +* index. +*/ +if (istate->cache[pos]->ce_mode !=CE_MODE_SPARSE_DIRECTORY ) + goto cleanup;
This looked wrong to me until I tried to come up with a counter-example. Here you are relying on the fact that before the comment, pos is going to be the index of a sparse directory entry -- either for path_as_dir or some ancestor directory. It would be nice if the comment mentioned that.
Quoted 13 lines
sparse-index.c +9
-1+ +trace2_region_enter ("index" ,"expand_to_path" , istate->repo); +/* for now, do the obviously-correct, slow thing */ ensure_full_index (istate); + +trace2_region_leave ("index" ,"expand_to_path" , istate->repo); + +cleanup: +strbuf_release (&path_as_dir); + in_expand_to_path =0 ; } --
Quoted 1 line
gitgitgadget
Looks good otherwise.
PATCH 22/27 add: allow operating on a sparse-only index
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
Replace enough callers to ensure_full_index() to instead call expand_to_path() to reduce how often 'git add' expands a sparse index in memory (before writing a sparse index again).
One non-obvious case is index_name_pos_also_unmerged() which is only hit on the Windows platform (in my tests). Use expand_to_path() instead of ensure_full_index().
Add a test to check that 'git add -A' and 'git add <file>' does not expand the index at all, as long as <file> is not within a sparse directory.
--- builtin/add.c | 3 +++ dir.c | 8 ++++---- read-cache.c | 10 +++++----- sparse-index.c | 18 ++++++++++++++---- t/t1092-sparse-checkout-compatibility.sh | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 13 deletions(-)
builtin/add.c +3 -0
@@ -491,6 +491,9 @@ int cmd_add(int argc, const char **argv, const char *prefix) add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize; require_pathspec = !(take_worktree_changes || (0 < addremove_explicit)); +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; +hold_locked_index (&lock_file,LOCK_DIE_ON_ERROR );/*
dir.c +4 -4
@@ -18,6 +18,7 @@#include "ewah/ewok.h" #include "fsmonitor.h" #include "submodule-config.h" +#include "sparse-index.h" /* * Tells read_directory_recursive how a file or directory should be treated.
@@ -899,9 +900,9 @@ static int read_skip_worktree_file_from_index(struct index_state *istate, {int pos, len; -ensure_full_index (istate); - len =strlen (path); + +expand_to_path (istate, path, len,0 ); pos =index_name_pos (istate, path, len);if (pos <0 )return -1 ;
@@ -1707,8 +1708,7 @@ static enum exist_status directory_exists_in_index(struct index_state *istate,if (ignore_case)return directory_exists_in_index_icase (istate, dirname, len); -ensure_full_index (istate); - +expand_to_path (istate, dirname, len,0 ); pos =index_name_pos (istate, dirname, len);if (pos <0 ) pos = -pos-1 ;
read-cache.c +5 -5
@@ -647,7 +647,7 @@ static int index_name_pos_also_unmerged(struct index_state *istate,int pos;struct cache_entry *ce; -ensure_full_index (istate); +expand_to_path (istate, path, namelen,0 ); pos =index_name_pos (istate, path, namelen);if (pos >=0 )
@@ -724,8 +724,6 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,int hash_flags =HASH_WRITE_OBJECT ;struct object_id oid; -ensure_full_index (istate); -if (flags &ADD_CACHE_RENORMALIZE ) hash_flags |=HASH_RENORMALIZE ;
@@ -733,6 +731,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,return error (_ ("%s: can only add regular files, symbolic links or git-directories" ), path); namelen =strlen (path); +expand_to_path (istate, path, namelen,0 ); +if (S_ISDIR (st_mode)) {if (resolve_gitlink_ref (path,"HEAD" , &oid) <0 )return error (_ ("'%s' does not have a commit checked out" ), path);
@@ -1104,7 +1104,7 @@ static int has_dir_name(struct index_state *istate,size_t len_eq_last;int cmp_last =0 ; -ensure_full_index (istate); +expand_to_path (istate, ce->name, ce->ce_namelen,0 );/* * We are frequently called during an iteration on a sorted
@@ -1349,7 +1349,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti {int pos; -ensure_full_index (istate); +expand_to_path (istate, ce->name, ce->ce_namelen,0 );if (option &ADD_CACHE_JUST_APPEND ) pos = istate->cache_nr;
sparse-index.c +14 -4
@@ -286,6 +286,7 @@ static int in_expand_to_path = 0;void expand_to_path (struct index_state *istate,const char *path,size_t pathlen,int icase) { +struct cache_entry *ce =NULL ;struct strbuf path_as_dir =STRBUF_INIT ;int pos;
@@ -320,13 +321,22 @@ void expand_to_path(struct index_state *istate,if (pos <0 ) pos = -pos -1 ; +if (pos < istate->cache_nr) + ce = istate->cache[pos];/* -* Even if the path doesn't exist, if the value isn't exactly a -* sparse-directory entry, then there is no need to expand the -* index. +* If we didn't land on a sparse directory, then there is +* nothing to expand. */ -if (istate->cache[pos]->ce_mode !=CE_MODE_SPARSE_DIRECTORY ) +if (ce && !S_ISSPARSEDIR (ce)) + goto cleanup; +/* +* If that sparse directory is not a prefix of the path we +* are looking for, then we don't need to expand. +*/ +if (ce && + (ce->ce_namelen >= path_as_dir.len || +strncmp (ce->name, path_as_dir.buf, ce->ce_namelen))) goto cleanup;trace2_region_enter ("index" ,"expand_to_path" , istate->repo);
t/t1092-sparse-checkout-compatibility.sh +14 -0
@@ -390,6 +390,20 @@ test_expect_success 'sparse-index is expanded and converted back' ' GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ git -C sparse-index -c core.fsmonitor="" reset --hard && test_region index convert_to_sparse trace2.txt && + test_region index ensure_full_index trace2.txt && + + rm trace2.txt && + echo >>sparse-index/README.md && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" add -A && + test_region index convert_to_sparse trace2.txt && + test_region index ensure_full_index trace2.txt && + + rm trace2.txt && + echo >>sparse-index/extra.txt && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" add extra.txt && + test_region index convert_to_sparse trace2.txt && test_region index ensure_full_index trace2.txt '
Signature · 1 line
gitgitgadget
Re: [PATCH 22/27] add: allow operating on a sparse-only index
Quoted 10 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
Replace enough callers to
ensure_full_index()to instead callexpand_to_path()to reduce how often 'git add' expands a sparse index in memory (before writing a sparse index again).One non-obvious case is
index_name_pos_also_unmerged()which is only hit on the Windows platform (in my tests). Useexpand_to_path()instead ofensure_full_index().
I read this paragraph as saying that the conversion of index_name_pos_also_unmerged() was tricky, whereas after reading the patch it looks like the conversion was trivial and you were perhaps meaning to say that it is easy to miss that this function also needs a conversion. Also, since the second sentence is true of all the conversions, not sure how much it helps to highlight it when just talking about this one function.
Both of these are minor quibbles, but if there's a clever way to reword here that reduces potential confusion, that'd be great.
Quoted 129 lines
Add a test to check that 'git add -A' and 'git add <file>' does not expand the index at all, as long as <file> is not within a sparse directory.
Signed-off-by Derrick Stolee <[email protected]>--- builtin/add.c | 3 +++ dir.c | 8 ++++---- read-cache.c | 10 +++++----- sparse-index.c | 18 ++++++++++++++---- t/t1092-sparse-checkout-compatibility.sh | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 13 deletions(-)builtin/add.c +3
-0@@ -491,6 +491,9 @@ int cmd_add(int argc, const char **argv, const char *prefix) add_new_files = !take_worktree_changes && !refresh_only && !add_renormalize; require_pathspec = !(take_worktree_changes || (0 < addremove_explicit)); +prepare_repo_settings (the_repository); + the_repository->settings.command_requires_full_index =0 ; +hold_locked_index (&lock_file,LOCK_DIE_ON_ERROR );/* dir.c +4
-4@@ -18,6 +18,7 @@#include "ewah/ewok.h" #include "fsmonitor.h" #include "submodule-config.h" +#include "sparse-index.h" /* * Tells read_directory_recursive how a file or directory should be treated. @@ -899,9 +900,9 @@ static int read_skip_worktree_file_from_index(struct index_state *istate, {int pos, len; -ensure_full_index (istate); - len =strlen (path); + +expand_to_path (istate, path, len,0 ); pos =index_name_pos (istate, path, len);if (pos <0 )return -1 ;@@ -1707,8 +1708,7 @@ static enum exist_status directory_exists_in_index(struct index_state *istate,if (ignore_case)return directory_exists_in_index_icase (istate, dirname, len); -ensure_full_index (istate); - +expand_to_path (istate, dirname, len,0 ); pos =index_name_pos (istate, dirname, len);if (pos <0 ) pos = -pos-1 ;read-cache.c +5
-5@@ -647,7 +647,7 @@ static int index_name_pos_also_unmerged(struct index_state *istate,int pos;struct cache_entry *ce; -ensure_full_index (istate); +expand_to_path (istate, path, namelen,0 ); pos =index_name_pos (istate, path, namelen);if (pos >=0 )@@ -724,8 +724,6 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,int hash_flags =HASH_WRITE_OBJECT ;struct object_id oid; -ensure_full_index (istate); -if (flags &ADD_CACHE_RENORMALIZE ) hash_flags |=HASH_RENORMALIZE ;@@ -733,6 +731,8 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,return error (_ ("%s: can only add regular files, symbolic links or git-directories" ), path); namelen =strlen (path); +expand_to_path (istate, path, namelen,0 ); +if (S_ISDIR (st_mode)) {if (resolve_gitlink_ref (path,"HEAD" , &oid) <0 )return error (_ ("'%s' does not have a commit checked out" ), path);@@ -1104,7 +1104,7 @@ static int has_dir_name(struct index_state *istate,size_t len_eq_last;int cmp_last =0 ; -ensure_full_index (istate); +expand_to_path (istate, ce->name, ce->ce_namelen,0 );/* * We are frequently called during an iteration on a sorted @@ -1349,7 +1349,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti {int pos; -ensure_full_index (istate); +expand_to_path (istate, ce->name, ce->ce_namelen,0 );if (option &ADD_CACHE_JUST_APPEND ) pos = istate->cache_nr;sparse-index.c +3
-0@@ -286,6 +286,7 @@ static int in_expand_to_path = 0;void expand_to_path (struct index_state *istate,const char *path,size_t pathlen,int icase) { +struct cache_entry *ce =NULL ;struct strbuf path_as_dir =STRBUF_INIT ;int pos;@@ -320,13 +321,22 @@ void expand_to_path(struct index_state *istate,if (pos <0 ) pos = -pos -1 ; +if (pos < istate->cache_nr) + ce = istate->cache[pos];
sparse-index.c void expand_to_path(struct index_state *istate, · line 332
Quoted 10 lines
sparse-index.c +4
-4/* -* Even if the path doesn't exist, if the value isn't exactly a -* sparse-directory entry, then there is no need to expand the -* index. +* If we didn't land on a sparse directory, then there is +* nothing to expand. */ -if (istate->cache[pos]->ce_mode !=CE_MODE_SPARSE_DIRECTORY ) +if (ce && !S_ISSPARSEDIR (ce)) + goto cleanup;
Seems like these changes to expand_to_path() could and maybe should be squashed into the commit that introduces expand_to_path()?
sparse-index.c void expand_to_path(struct index_state *istate, · line 339
Quoted 7 lines
sparse-index.c +7
-0+/* +* If that sparse directory is not a prefix of the path we +* are looking for, then we don't need to expand. +*/ +if (ce && + (ce->ce_namelen >= path_as_dir.len || +strncmp (ce->name, path_as_dir.buf, ce->ce_namelen)))
Should this also be squashed into the commit that introduces expand_to_path()? Why is this check added here?
Quoted 31 lines
sparse-index.c +0
-0goto cleanup;trace2_region_enter ("index" ,"expand_to_path" , istate->repo);t/t1092-sparse-checkout-compatibility.sh +14
-1@@ -390,6 +390,20 @@ test_expect_success 'sparse-index is expanded and converted back' ' GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ git -C sparse-index -c core.fsmonitor="" reset --hard && test_region index convert_to_sparse trace2.txt && + test_region index ensure_full_index trace2.txt && + + rm trace2.txt && + echo >>sparse-index/README.md && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" add -A && + test_region index convert_to_sparse trace2.txt && + test_region index ensure_full_index trace2.txt && + + rm trace2.txt && + echo >>sparse-index/extra.txt && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" add extra.txt && + test_region index convert_to_sparse trace2.txt && test_region index ensure_full_index trace2.txt ' --gitgitgadget
PATCH 23/27 submodule: die_path_inside_submodule is sparse aware
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
Since we already do not collapse a sparse directory if it contains a submodule, we don't need to expand to a full index in die_path_inside_submodule(). A simple scan of the index entries is sufficient.
--- submodule.c | 2 -- t/t1092-sparse-checkout-compatibility.sh | 24 +++++++++++------------- 2 files changed, 11 insertions(+), 15 deletions(-)
submodule.c +0 -2
@@ -346,8 +346,6 @@ void die_path_inside_submodule(struct index_state *istate, {int i, j; -ensure_full_index (istate); -for (i =0 ; i < istate->cache_nr; i++) {struct cache_entry *ce = istate->cache[i];int ce_len =ce_namelen (ce);
t/t1092-sparse-checkout-compatibility.sh +11 -13
@@ -390,29 +390,27 @@ test_expect_success 'sparse-index is expanded and converted back' ' GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ git -C sparse-index -c core.fsmonitor="" reset --hard && test_region index convert_to_sparse trace2.txt && - test_region index ensure_full_index trace2.txt && + test_region index ensure_full_index trace2.txt +' + +test_expect_success 'sparse-index is not expanded' ' + init_repos && + + rm -f trace2.txt && + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ + git -C sparse-index -c core.fsmonitor="" status -uno && + test_region ! index ensure_full_index trace2.txt && rm trace2.txt && echo >>sparse-index/README.md && GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ git -C sparse-index -c core.fsmonitor="" add -A && - test_region index convert_to_sparse trace2.txt && - test_region index ensure_full_index trace2.txt && + test_region ! index ensure_full_index trace2.txt && rm trace2.txt && echo >>sparse-index/extra.txt && GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ git -C sparse-index -c core.fsmonitor="" add extra.txt && - test_region index convert_to_sparse trace2.txt && - test_region index ensure_full_index trace2.txt -' - -test_expect_success 'sparse-index is not expanded' ' - init_repos && - - rm -f trace2.txt && - GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" status -uno && test_region ! index ensure_full_index trace2.txt '
Signature · 1 line
gitgitgadget
PATCH 24/27 dir: use expand_to_path in add_patterns()
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
The add_patterns() method has a way to extract a pattern file from the index. If this pattern file is sparse and within a sparse directory entry, then we need to expand the index before looking for that entry as a file path.
For now, convert ensure_full_index() into expand_to_path() to only expand this way when absolutely necessary.
--- dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
dir.c +1 -1
@@ -1093,7 +1093,7 @@ static int add_patterns(const char *fname, const char *base, int baselen,int pos;if (istate) -ensure_full_index (istate); +expand_to_path (istate, fname,strlen (fname),0 );if (oid_stat->valid && !match_stat_data_racy (istate, &oid_stat->stat, &st))
Signature · 1 line
gitgitgadget
Re: [PATCH 24/27] dir: use expand_to_path in add_patterns()
Quoted 7 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
The
add_patterns()method has a way to extract a pattern file from the index. If this pattern file is sparse and within a sparse directory entry, then we need to expand the index before looking for that entry as a file path.
Why?
Correct me if I'm wrong, but I thought the point of add_patterns() was to read .gitignore entries, so that we can know whether to e.g. have status report untracked files within some directory or have clean delete files within a directory. But if we have a sparse directory entry in the index, we probably have no such directory in the working directory. And if we have no such working directory, getting .gitignore entries for those directories is a big waste of time.
Quoted 2 lines
For now, convert
ensure_full_index()intoexpand_to_path()to only expand this way when absolutely necessary.
Not only should we probably not need to read these files at all, expand_to_path() still expands a lot more than necessary, right? If two directories are sparse -- moduleA and moduleB, and we need something from under moduleA/, then expand_to_path() will call ensure_full_index() and fill out every entry under both modules, even if moduleB is way bigger than moduleA. Unless I've misunderstood something, there's multiple ways we're falling short of "only...when absolutely necessary".
Perhaps both of these things are future work you already had planned; if so, some tweaks to the commit message may help keep this reader oriented. :-)
Quoted 19 lines
Signed-off-by Derrick Stolee <[email protected]>--- dir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)dir.c +1
-2@@ -1093,7 +1093,7 @@ static int add_patterns(const char *fname, const char *base, int baselen,int pos;if (istate) -ensure_full_index (istate); +expand_to_path (istate, fname,strlen (fname),0 );if (oid_stat->valid && !match_stat_data_racy (istate, &oid_stat->stat, &st)) --
Quoted 1 line
gitgitgadget
There's also a read_skip_worktree_file_from_index() call earlier in the same function, which in your big RFC patch you protected with the ensure_full_index() call already. Perhaps it should have an expand_to_path() conversion as well? But, in the big picture, it seems like checking if we can avoid reading in that pattern file (whenever the directory doesn't exist within the working copy) would be a better first step.
PATCH 25/27 fsmonitor: disable if index is sparse
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
The FS Monitor feature uses a bitmap over the index entries. This currently interacts poorly with a sparse index. We will revisit this interaction in the future, but for now protect the index by refusing to use the FS Monitor extension at all if the index is sparse.
--- fsmonitor.c | 21 ++++++++++++++------- read-cache.c | 3 ++- t/t1092-sparse-checkout-compatibility.sh | 8 ++++---- 3 files changed, 20 insertions(+), 12 deletions(-)
fsmonitor.c +14 -7
@@ -58,6 +58,9 @@ int read_fsmonitor_extension(struct index_state *istate, const void *data,uint64_t timestamp;struct strbuf last_update =STRBUF_INIT ; +if (istate->sparse_index) +return 0 ; +if (sz <sizeof (uint32_t ) +1 +sizeof (uint32_t ))return error ("corrupt fsmonitor extension (too short)" );
@@ -98,7 +101,8 @@ void fill_fsmonitor_bitmap(struct index_state *istate) {unsigned int i, skipped =0 ; -ensure_full_index (istate); +if (istate->sparse_index) +return ; istate->fsmonitor_dirty =ewah_new ();for (i =0 ; i < istate->cache_nr; i++) {
@@ -161,11 +165,7 @@ static int query_fsmonitor(int version, const char *last_update, struct strbuf *static void fsmonitor_refresh_callback (struct index_state *istate,const char *name) { -int pos; - -ensure_full_index (istate); - - pos =index_name_pos (istate, name,strlen (name)); +int pos =index_name_pos (istate, name,strlen (name));if (pos >=0 ) {struct cache_entry *ce = istate->cache[pos];
@@ -190,7 +190,8 @@ void refresh_fsmonitor(struct index_state *istate)char *buf;unsigned int i; -if (!core_fsmonitor || istate->fsmonitor_has_run_once) +if (!core_fsmonitor || istate->fsmonitor_has_run_once || + istate->sparse_index)return ; hook_version =fsmonitor_hook_version ();
@@ -300,6 +301,9 @@ void add_fsmonitor(struct index_state *istate)unsigned int i;struct strbuf last_update =STRBUF_INIT ; +if (istate->sparse_index) +return ; +if (!istate->fsmonitor_last_update) {trace_printf_key (&trace_fsmonitor,"add fsmonitor" ); istate->cache_changed |=FSMONITOR_CHANGED ;
@@ -335,6 +339,9 @@ void tweak_fsmonitor(struct index_state *istate)unsigned int i;int fsmonitor_enabled =git_config_get_fsmonitor (); +if (istate->sparse_index) + fsmonitor_enabled =0 ; +if (istate->fsmonitor_dirty) {if (fsmonitor_enabled) {ensure_full_index (istate);
read-cache.c +2 -1
@@ -3017,7 +3017,8 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,if (err)return -1 ; } -if (!strip_extensions && istate->fsmonitor_last_update) { +if (!strip_extensions && istate->fsmonitor_last_update && + !istate->sparse_index) {struct strbuf sb =STRBUF_INIT ;write_fsmonitor_extension (&sb, istate);
t/t1092-sparse-checkout-compatibility.sh +4 -4
@@ -388,7 +388,7 @@ test_expect_success 'sparse-index is expanded and converted back' ' init_repos && GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" reset --hard && + git -C sparse-index reset --hard && test_region index convert_to_sparse trace2.txt && test_region index ensure_full_index trace2.txt '
@@ -398,19 +398,19 @@ test_expect_success 'sparse-index is not expanded' ' rm -f trace2.txt && GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" status -uno && + git -C sparse-index status -uno && test_region ! index ensure_full_index trace2.txt && rm trace2.txt && echo >>sparse-index/README.md && GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" add -A && + git -C sparse-index add -A && test_region ! index ensure_full_index trace2.txt && rm trace2.txt && echo >>sparse-index/extra.txt && GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \ - git -C sparse-index -c core.fsmonitor="" add extra.txt && + git -C sparse-index add extra.txt && test_region ! index ensure_full_index trace2.txt '
Signature · 1 line
gitgitgadget
PATCH 26/27 pathspec: stop calling ensure_full_index
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
The add_pathspec_matches_against_index() focuses on matching a pathspec to file entries in the index. It is possible that this already works correctly for its only use: checking if untracked files exist in the index.
It is likely that this causes a behavior issue when adding a directory that exists at HEAD but is outside the sparse cone. I'm marking this as a place to pursue with future tests.
--- pathspec.c | 1 - 1 file changed, 1 deletion(-)
pathspec.c +0 -1
@@ -36,7 +36,6 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec, num_unmatched++;if (!num_unmatched)return ; -ensure_full_index (istate);for (i =0 ; i < istate->cache_nr; i++) {const struct cache_entry *ce = istate->cache[i];ce_path_match (istate, ce, pathspec, seen);
Signature · 1 line
gitgitgadget
Re: [PATCH 26/27] pathspec: stop calling ensure_full_index
Quoted 11 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
The
add_pathspec_matches_against_index()focuses on matching a pathspec to file entries in the index. It is possible that this already works correctly for its only use: checking if untracked files exist in the index.It is likely that this causes a behavior issue when adding a directory that exists at HEAD but is outside the sparse cone. I'm marking this as a place to pursue with future tests.
Sounds like you're unsure if this patch is good. Should it be marked RFC or something?
Quoted 20 lines
Signed-off-by Derrick Stolee <[email protected]>--- pathspec.c | 1 - 1 file changed, 1 deletion(-)pathspec.c +0
-2@@ -36,7 +36,6 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec, num_unmatched++;if (!num_unmatched)return ; -ensure_full_index (istate);for (i =0 ; i < istate->cache_nr; i++) {const struct cache_entry *ce = istate->cache[i];ce_path_match (istate, ce, pathspec, seen); --gitgitgadget
Re: [PATCH 26/27] pathspec: stop calling ensure_full_index
Quoted 10 lines · Elijah Newren
Quoted 8 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
The
add_pathspec_matches_against_index()focuses on matching a pathspec to file entries in the index. It is possible that this already works correctly for its only use: checking if untracked files exist in the index.
Quoted 6 lines
Quoted 3 lines
It is likely that this causes a behavior issue when adding a directory that exists at HEAD but is outside the sparse cone. I'm marking this as a place to pursue with future tests.
Sounds like you're unsure if this patch is good. Should it be marked RFC or something?
...isn't the whole series marked as RFC? I only specifically marked the ensure_full_index() one because I purposefully squashed it.
But in general, everything I'm touching in these areas seems like a potentially problematic change. So many things are used and re-used that I'm not sure what is safe or not. More testing is required for commands to ensure their behavior.
I can enable things like GIT_TEST_SPARSE_INDEX to get other tests using sparse-checkout to work (but only if they use cone mode). Hence, I'm relying on what tests I can write to cover the behavior instead of a robust history of valuable tests.
I hope to gather more confidence as this goes forward. I definitely work to be confident that I am not making any errors that cause problems for users who do not enable the sparse-index, but I expect there to be a long tail of adjustments required as more corner cases are discovered during the development and testing of the feature.
The good news is that I have some engaged users who are willing to test the feature and provide feedback if they hit any snags once there is a minimal set of functionality.
Thanks,
-Stolee
PATCH 27/27 cache-tree: integrate with sparse directory entries
In reply to Derrick Stolee via GitGitGadgetFrom: Derrick Stolee <[email protected]>
The cache-tree extension was previously disabled with sparse indexes. However, the cache-tree is an important performance feature for commands like 'git status' and 'git add'. Integrate it with sparse directory entries.
When writing a sparse index, completely clear and recalculate the cache tree. By starting from scratch, the only integration necessary is to check if we hit a sparse directory entry and create a leaf of the cache-tree that has an entry_count of one and no subtrees.
Once the cache-tree exists within a sparse index, we finally get improved performance. I test the sparse index performance using a private monorepo with over 2.1 million files at HEAD, but with a sparse-checkout definition that has only 68,000 paths in the populated cone. The sparse index has about 2,000 sparse directory entries. I compare three scenarios:
1. Use the full index. The index size is ~186 MB.
2. Use the sparse index. The index size is ~5.5 MB.
3. Use a commit where HEAD matches the populated set. The full index
size is ~5.3MB.
The third benchmark is included as a theoretical optimium for a repository of the same object database.
First, a clean 'git status' improves from 3.1s to 240ms.
Benchmark #1: full index (git status)
Time (mean ± σ): 3.167 s ± 0.036 s [User: 2.006 s, System: 1.078 s]
Range (min … max): 3.100 s … 3.208 s 10 runs
Benchmark #2: sparse index (git status)
Time (mean ± σ): 239.5 ms ± 8.1 ms [User: 189.4 ms, System: 226.8 ms]
Range (min … max): 226.0 ms … 251.9 ms 13 runs
Benchmark #3: small tree (git status)
Time (mean ± σ): 195.3 ms ± 4.5 ms [User: 116.5 ms, System: 84.4 ms]
Range (min … max): 188.8 ms … 202.8 ms 15 runs
The optimimum is still 45ms faster. This is due in part to the 2,000+ sparse directory entries, but there might be other optimizations to make in the sparse-index case. In particular, I find that this performance difference disappears when I disable FS Monitor, which is somewhat disabled in the sparse-index case, but might still be adding overhead.
The performance numbers for 'git add .' are much closer to optimal:
Benchmark #1: full index (git add .)
Time (mean ± σ): 3.076 s ± 0.022 s [User: 2.065 s, System: 0.943 s]
Range (min … max): 3.044 s … 3.116 s 10 runs
Benchmark #2: sparse index (git add .)
Time (mean ± σ): 218.0 ms ± 6.6 ms [User: 195.7 ms, System: 206.6 ms]
Range (min … max): 209.8 ms … 228.2 ms 13 runs
Benchmark #3: small tree (git add .)
Time (mean ± σ): 217.6 ms ± 5.4 ms [User: 131.9 ms, System: 86.7 ms]
Range (min … max): 212.1 ms … 228.4 ms 14 runs
In this test, I also used "echo >>README.md" to append a line to the README.md file, so the 'git add .' command is doing _something_ other than a no-op. Without this edit (and FS Monitor enabled) the small tree case again gains about 30ms on the sparse index case.
--- cache-tree.c | 18 ++++++++++++++++++ sparse-index.c | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-)
cache-tree.c +18 -0
@@ -256,6 +256,24 @@ static int update_one(struct cache_tree *it, *skip_count =0 ; +/* +* If the first entry of this region is a sparse directory +* entry corresponding exactly to 'base', then this cache_tree +* struct is a "leaf" in the data structure, pointing to the +* tree OID specified in the entry. +*/ +if (entries >0 ) { +const struct cache_entry *ce = cache[0 ]; + +if (S_ISSPARSEDIR (ce) && + ce->ce_namelen == baselen && + !strncmp (ce->name, base, baselen)) { + it->entry_count =1 ; +oidcpy (&it->oid, &ce->oid); +return 1 ; + } + } +if (0 <= it->entry_count &&has_object_file (&it->oid))return it->entry_count;
sparse-index.c +9 -1
@@ -181,7 +181,11 @@ int convert_to_sparse(struct index_state *istate) istate->cache_nr =convert_to_sparse_rec (istate,0 ,0 , istate->cache_nr,"" ,0 , istate->cache_tree); - istate->drop_cache_tree =1 ; + +/* Clear and recompute the cache-tree */ +cache_tree_free (&istate->cache_tree); +cache_tree_update (istate,0 ); + istate->sparse_index =1 ;trace2_region_leave ("index" ,"convert_to_sparse" , istate->repo);return 0 ;
@@ -278,6 +282,10 @@ void ensure_full_index(struct index_state *istate)free (full); +/* Clear and recompute the cache-tree */ +cache_tree_free (&istate->cache_tree); +cache_tree_update (istate,0 ); +trace2_region_leave ("index" ,"ensure_full_index" , istate->repo); }
Signature · 1 line
gitgitgadget
Re: [PATCH 27/27] cache-tree: integrate with sparse directory entries
Quoted 13 lines · Derrick Stolee via GitGitGadget
From: Derrick Stolee <[email protected]>
The cache-tree extension was previously disabled with sparse indexes. However, the cache-tree is an important performance feature for commands like 'git status' and 'git add'. Integrate it with sparse directory entries.
When writing a sparse index, completely clear and recalculate the cache tree. By starting from scratch, the only integration necessary is to check if we hit a sparse directory entry and create a leaf of the cache-tree that has an
entry_countof one and no subtrees.
Quoted 6 lines
Once the cache-tree exists within a sparse index, we finally get improved performance. I test the sparse index performance using a private monorepo with over 2.1 million files at HEAD, but with a sparse-checkout definition that has only 68,000 paths in the populated cone. The sparse index has about 2,000 sparse directory entries. I compare three scenarios:
How many .gitignore entries does this monorepo have? What percentage of those are populated for #2 and #3?
Can a testcase be devised that others can repeat? For example, I created https://github.com/newren/gvfs-like-git-bomb once upon a time to create a very small repository with a very large index and a lot of skip-worktree entries, mostly to test some stuff that someone (Ben Peart?) mentioned as being slow and verify for myself that it wasn't Windows specific.
Quoted 4 lines
1. Use the full index. The index size is ~186 MB.
2. Use the sparse index. The index size is ~5.5 MB.
3. Use a commit where HEAD matches the populated set. The full indexsize is ~5.3MB.
I'm not sure I'm understanding the difference between #2 and #3, other than #3 is smaller. How did you form #2? Also, what do you mean by "full index size" for #3, when it's the smallest? Isn't that index the most sparse (or least full)? Or is it an index for a different commit entirely that has far fewer files in it?
Quoted 1 line
The third benchmark is included as a theoretical optimium for a
s/optimium/optimum/
Quoted 1 line
repository of the same object database.
This I'm also not understanding, but maybe this goes back to not understanding the difference in how #2 and #3 are constructed.
Quoted 6 lines
First, a clean 'git status' improves from 3.1s to 240ms.
Benchmark #1: full index (git status)
Time (mean ± σ): 3.167 s ± 0.036 s [User: 2.006 s, System: 1.078 s]
Range (min … max): 3.100 s … 3.208 s 10 runs
Quoted 7 lines
Benchmark #2: sparse index (git status)
Time (mean ± σ): 239.5 ms ± 8.1 ms [User: 189.4 ms, System: 226.8 ms]
Range (min … max): 226.0 ms … 251.9 ms 13 runsBenchmark #3: small tree (git status)
Time (mean ± σ): 195.3 ms ± 4.5 ms [User: 116.5 ms, System: 84.4 ms]
Range (min … max): 188.8 ms … 202.8 ms 15 runs
Always nice to see a speedup factor greater than 10. :-)
Quoted 2 lines
The optimimum is still 45ms faster. This is due in part to the 2,000+
s/optimimum/optimum/
Quoted 4 lines
sparse directory entries, but there might be other optimizations to make in the sparse-index case. In particular, I find that this performance difference disappears when I disable FS Monitor, which is somewhat disabled in the sparse-index case, but might still be adding overhead.
The FS monitor wording is unclear to me; it feels like multiple negatives.
Quoted 10 lines
The performance numbers for 'git add .' are much closer to optimal:
Benchmark #1: full index (git add .)
Time (mean ± σ): 3.076 s ± 0.022 s [User: 2.065 s, System: 0.943 s]
Range (min … max): 3.044 s … 3.116 s 10 runsBenchmark #2: sparse index (git add .)
Time (mean ± σ): 218.0 ms ± 6.6 ms [User: 195.7 ms, System: 206.6 ms]
Range (min … max): 209.8 ms … 228.2 ms 13 runs
Quoted 8 lines
Benchmark #3: small tree (git add .)
Time (mean ± σ): 217.6 ms ± 5.4 ms [User: 131.9 ms, System: 86.7 ms]
Range (min … max): 212.1 ms … 228.4 ms 14 runsIn this test, I also used "echo >>README.md" to append a line to the README.md file, so the 'git add .' command is doing _something_ other than a no-op. Without this edit (and FS Monitor enabled) the small tree case again gains about 30ms on the sparse index case.
Meaning the small tree is 30 ms faster than reported here, or 30 ms slower, or that both sparse index and small tree are faster but the small tree decreases its time more than the sparse index one does?
Sorry, I don't mean to be dense, I'm just struggling with understanding words today it seems. (Also, it seems like there's a joke in there about me being "dense" in a review of a "sparse" feature...but I'm not quite coming up with it.)
Quoted 64 lines
Signed-off-by Derrick Stolee <[email protected]>--- cache-tree.c | 18 ++++++++++++++++++ sparse-index.c | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-)cache-tree.c +18
-0@@ -256,6 +256,24 @@ static int update_one(struct cache_tree *it, *skip_count =0 ; +/* +* If the first entry of this region is a sparse directory +* entry corresponding exactly to 'base', then this cache_tree +* struct is a "leaf" in the data structure, pointing to the +* tree OID specified in the entry. +*/ +if (entries >0 ) { +const struct cache_entry *ce = cache[0 ]; + +if (S_ISSPARSEDIR (ce) && + ce->ce_namelen == baselen && + !strncmp (ce->name, base, baselen)) { + it->entry_count =1 ; +oidcpy (&it->oid, &ce->oid); +return 1 ; + } + } +if (0 <= it->entry_count &&has_object_file (&it->oid))return it->entry_count;sparse-index.c +9
-2@@ -181,7 +181,11 @@ int convert_to_sparse(struct index_state *istate) istate->cache_nr =convert_to_sparse_rec (istate,0 ,0 , istate->cache_nr,"" ,0 , istate->cache_tree); - istate->drop_cache_tree =1 ; + +/* Clear and recompute the cache-tree */ +cache_tree_free (&istate->cache_tree); +cache_tree_update (istate,0 ); + istate->sparse_index =1 ;trace2_region_leave ("index" ,"convert_to_sparse" , istate->repo);return 0 ;@@ -278,6 +282,10 @@ void ensure_full_index(struct index_state *istate)free (full); +/* Clear and recompute the cache-tree */ +cache_tree_free (&istate->cache_tree); +cache_tree_update (istate,0 ); +trace2_region_leave ("index" ,"ensure_full_index" , istate->repo); } --
Quoted 1 line
gitgitgadget
This is very exciting work!!
Re: [PATCH 27/27] cache-tree: integrate with sparse directory entries
Quoted 7 lines · Elijah Newren
Quoted 4 lines · Derrick Stolee via GitGitGadget
In this test, I also used "echo >>README.md" to append a line to the README.md file, so the 'git add .' command is doing _something_ other than a no-op. Without this edit (and FS Monitor enabled) the small tree case again gains about 30ms on the sparse index case.
Quoted 8 lines
Meaning the small tree is 30 ms faster than reported here, or 30 ms slower, or that both sparse index and small tree are faster but the small tree decreases its time more than the sparse index one does?
Sorry, I don't mean to be dense, I'm just struggling with understanding words today it seems. (Also, it seems like there's a joke in there about me being "dense" in a review of a "sparse" feature...but I'm not quite coming up with it.)
I don't blame you! This is a lot to digest, and I appreciate you pushing through to the end of it.
Clearly, I was getting a bit inexact near the end. My excitement to share this RFC clearly overshadowed my attention to grammatical detail. I'll go through your feedback more carefully soon and hopefully clarify these and many other questions.
Thanks,
-Stolee
Re: [PATCH 27/27] cache-tree: integrate with sparse directory entries
Quoted 18 lines · Derrick Stolee
Quoted 7 lines · Elijah Newren
Quoted 4 lines · Derrick Stolee via GitGitGadget
In this test, I also used "echo >>README.md" to append a line to the README.md file, so the 'git add .' command is doing _something_ other than a no-op. Without this edit (and FS Monitor enabled) the small tree case again gains about 30ms on the sparse index case.
Quoted 8 lines
Meaning the small tree is 30 ms faster than reported here, or 30 ms slower, or that both sparse index and small tree are faster but the small tree decreases its time more than the sparse index one does?
Sorry, I don't mean to be dense, I'm just struggling with understanding words today it seems. (Also, it seems like there's a joke in there about me being "dense" in a review of a "sparse" feature...but I'm not quite coming up with it.)
Quoted 7 lines
I don't blame you! This is a lot to digest, and I appreciate you pushing through to the end of it.
Clearly, I was getting a bit inexact near the end. My excitement to share this RFC clearly overshadowed my attention to grammatical detail. I'll go through your feedback more carefully soon and hopefully clarify these and many other questions.
I can't blame you for being excited; this series is awesome. I've thought we should do something along this direction for years[1]. Sure I found lots of little nitpicks here and there in your series, but that's just attempting to help find any issues so it can be made even better; overall I'm super excited about it.
[1] See "crazy idea" paragraph at
https://lore.kernel.org/git/CABPp-BGir_5xyqEfwytDog0rZDydPHXjuqXCpNKk67dVPXjUjA@mail.gmail.com/
Quoted 4 lines · Derrick Stolee via GitGitGadget
This RFC proposes an update to the index formats to allow "sparse directory entries". These entries correspond to directories that are completely excluded from the sparse checkout definition. We can detect that a directory is excluded when using "cone mode" patterns.
Yay.
Quoted 4 lines
Since having directory entries is a radical departure from the existing index format, a new extension "extensions.sparseIndex" is added. Using a sparse index should cause incompatible tools to fail because they do not understand this extension.
Safety is good, but because the index is purely a local matter, we do not have to be so careful as updating the network protocols or pack/object formats.
I think the use of "extensions.*" mechanism to render the repository that uses the new feature unusable by older Git is safe enough, but it may be too draconian. For example, when things go wrong, don't you want to "fetch"/"clone" from it into another repository to first save the objects and refs? You do not need a version of the index file you understand in order to do that.
The index format has a mechanism to make older versions of Git bail when it encounters a file that uses newer feature that they do not understand. Perhaps using it is sufficient instead?
Quoted 25 lines · Junio C Hamano
Quoted 4 lines · Derrick Stolee via GitGitGadget
This RFC proposes an update to the index formats to allow "sparse directory entries". These entries correspond to directories that are completely excluded from the sparse checkout definition. We can detect that a directory is excluded when using "cone mode" patterns.
Yay.
Quoted 4 lines
Since having directory entries is a radical departure from the existing index format, a new extension "extensions.sparseIndex" is added. Using a sparse index should cause incompatible tools to fail because they do not understand this extension.
Safety is good, but because the index is purely a local matter, we do not have to be so careful as updating the network protocols or pack/object formats.
I think the use of "extensions.*" mechanism to render the repository that uses the new feature unusable by older Git is safe enough, but it may be too draconian. For example, when things go wrong, don't you want to "fetch"/"clone" from it into another repository to first save the objects and refs? You do not need a version of the index file you understand in order to do that.
Quoted 3 lines
The index format has a mechanism to make older versions of Git bail when it encounters a file that uses newer feature that they do not understand. Perhaps using it is sufficient instead?
There are interesting subtleties with the differences between index formats 2, 3, and 4 that are worth keeping around. Perhaps the extension could be a mechanism for allowing sparse directories in those versions, but then a future "index version 5" includes sparse directories without the extension.
I could spend time working on such an index v5 in parallel with the updates to Git commands to make them sparse aware. The logic from patches 1-14 in this series will be required before that could begin.
Thanks,
-Stolee
Quoted 140 lines · Derrick Stolee via GitGitGadget
This is based on ds/more-index-cleanups also available as GitGitGadget PR #839.
The sparse checkout feature allows users to specify a "populated set" that is smaller than the full list of files at HEAD. Files outside the sparse checkout definition are not present in the working directory, but are still present in the index (and marked with the
CE_SKIP_WORKTREEbit).This means that the working directory has size O(populated), and commands like "git status" or "git checkout" operate using an O(populated) number of filesystem operations. However, parsing the index still operates on the scale of O(HEAD).
This can be particularly jarring if you are merging a small repository with a large monorepo for the purpose of simplifying dependency management. Even if users have nothing more in their working directory than they had before, they suddenly see a significant increase in their "git status" or "git add" times. In these cases, simply parsing the index can be a huge portion of the command time.
This RFC proposes an update to the index formats to allow "sparse directory entries". These entries correspond to directories that are completely excluded from the sparse checkout definition. We can detect that a directory is excluded when using "cone mode" patterns.
Since having directory entries is a radical departure from the existing index format, a new extension "extensions.sparseIndex" is added. Using a sparse index should cause incompatible tools to fail because they do not understand this extension.
The index is a critical data structure, so making such a drastic change must be handled carefully. This RFC does only enough adjustments to demonstrate performance improvements for "git status" and "git add." Other commands should operate identically to before, since the other commands will expand a sparse index into a full index by parsing trees.
WARNING: I'm getting a failure on the FreeBSD build with my sparse-checkout tests. I'm not sure what is causing these failures, but I will explore while we discuss the possibility of the feature as a whole.
Here is an outline for this RFC:
* Patches 1-14: create and test the sparse index format. This is just enough to start writing the format, but all Git commands become slower for using it. This is because everything is guarded to expand to a full index before actually operating on the cache entries.
* Patch 15: This massive patch is actually a bunch of patches squashed together. I have a branch that adds "
ensure_full_index()" guards in each necessary file along with some commentary about how the index is being used. This patch is presented here as one big dump because that commentary isn't particularly interesting if the RFC leads to a very different approach.* Patches 16-27: These changes make enough code "sparse aware" such that "git status" and "git add" start operating in time O(populated) instead of O(HEAD).
Performance numbers are given in patch 27, but repeated somewhat here. The test environment I use has ~2.1 million paths at HEAD, but only 68,000 populated paths given the sparse-checkout I'm using. The sparse index has about 2,000 sparse directory entries.
1. Use the full index. The index size is ~186 MB.
2. Use the sparse index. The index size is ~5.5 MB.
3. Use a commit where HEAD matches the populated set. The full index sizeis ~5.3MB.The third benchmark is included as a theoretical optimum for a repository of the same object database.
First, a clean 'git status' improves from 3.1s to 240ms.
Benchmark #1: full index (git status) Time (mean ± σ): 3.167 s ± 0.036 s [User: 2.006 s, System: 1.078 s] Range (min … max): 3.100 s … 3.208 s 10 runs
Benchmark #2: sparse index (git status) Time (mean ± σ): 239.5 ms ± 8.1 ms [User: 189.4 ms, System: 226.8 ms] Range (min … max): 226.0 ms … 251.9 ms 13 runs
Benchmark #3: small tree (git status) Time (mean ± σ): 195.3 ms ± 4.5 ms [User: 116.5 ms, System: 84.4 ms] Range (min … max): 188.8 ms … 202.8 ms 15 runs
The performance numbers for 'git add .' are much closer to optimal:
Benchmark #1: full index (git add .) Time (mean ± σ): 3.076 s ± 0.022 s [User: 2.065 s, System: 0.943 s] Range (min … max): 3.044 s … 3.116 s 10 runs
Benchmark #2: sparse index (git add .) Time (mean ± σ): 218.0 ms ± 6.6 ms [User: 195.7 ms, System: 206.6 ms] Range (min … max): 209.8 ms … 228.2 ms 13 runs
Benchmark #3: small tree (git add .) Time (mean ± σ): 217.6 ms ± 5.4 ms [User: 131.9 ms, System: 86.7 ms] Range (min … max): 212.1 ms … 228.4 ms 14 runs
I expect that making a sparse index work optimally through the most common Git commands will take a year of effort. During this process, I expect to add a lot of testing infrastructure around the sparse-checkout feature, especially in corner cases. (This RFC focuses on the happy paths of operating only within the sparse cone, but that will change in the future.)
If this general approach is acceptable, then I would follow it with a sequence of patch submissions that follow this approach:
1. Basics of the format. (Patches 1-14)
2. Add additional guards around index interactions (Patch 15, but split appropriately.)
3. Speed up "git status" and "git add" (Patches 16-27)After those three items that are represented in this RFC, the work starts to parallelize a bit. My basic ideas for moving forward from this point are to do these basic steps:
* Add new index API abstractions where appropriate, make them sparse-aware.
* Add new tests around sparse-checkout corner cases. Ensure the sparse index works correctly.
* For a given builtin, add extra testing for sparse-checkouts then it them sparse-aware.Here are some specific questions I'm hoping to answer in this RFC period:
1. Are these sparse directory entries an appropriate way to extend the index format?
2. Is extensions.sparseIndex a good way to signal that these entries can exist?
3. Is git sparse-checkout init --cone --sparse-index an appropriate way to toggle the format?
4. Are there specific areas that I should target to harden the index API before I submit this work?
5. Does anyone have a good idea how to test a large portion of the test suite with sparse-index enabled? The problem I see is that most tests don't use sparse-checkout, so the sparse index is identical to the full index. Would it be interesting to enable the test setup to add such "extra" directories during the test setup?
Quoted 1 line
Thanks, -Stolee
Thanks for working on this. It's very exciting seeing this idea come alive. I had lots of little nitpicks and questions and whatnot here and there, but that almost feels like a diversion. Overall, I think you divided up the series in a very logical and easy to follow fashion, and actually achieved quite a bit already.
I suspect I have partially answered some of your questions above among all my comments, and left others unanswered or worse, just re-asked the same question(s) myself. Feel free to ping again with the next round and I'll see if I can dodge your questions again...er, I mean, try to think of something helpful to say. :-)
Quoted 87 lines
Derrick Stolee (27):
sparse-index: add guard to ensure full index
sparse-index: implementensure_full_index()
t1092: compare sparse-checkout to sparse-index
test-read-cache: print cache entries with --table
test-tool: read-cache --table --no-stat
test-tool: don't force full index
unpack-trees: ensure full index
sparse-checkout: hold pattern list in index
sparse-index: convert from full to sparse
submodule: sparse-index should not collapse links
unpack-trees: allow sparse directories
sparse-index: check index conversion happens
sparse-index: create extension for compatibility
sparse-checkout: toggle sparse index from builtin [RFC-VERSION] *: ensure full index
unpack-trees: make sparse aware
dir.c: accept a directory as part of cone-mode patterns
status: use sparse-index throughout
status: skip sparse-checkout percentage with sparse-index
sparse-index:expand_to_path()trivial implementation
sparse-index:expand_to_pathno-op if path exists
add: allow operating on a sparse-only index
submodule:die_path_inside_submoduleis sparse aware
dir: useexpand_to_pathinadd_patterns()
fsmonitor: disable if index is sparse
pathspec: stop callingensure_full_index
cache-tree: integrate with sparse directory entriesDocumentation/config/extensions.txt | 7 + Documentation/git-sparse-checkout.txt | 14 + Makefile | 1 + apply.c | 10 +- blame.c | 7 +- builtin/add.c | 3 + builtin/checkout-index.c | 5 +- builtin/commit.c | 3 + builtin/grep.c | 2 + builtin/ls-files.c | 9 +- builtin/merge-index.c | 2 + builtin/mv.c | 2 + builtin/rm.c | 2 + builtin/sparse-checkout.c | 35 ++- builtin/update-index.c | 2 + cache-tree.c | 21 ++ cache.h | 15 +- diff.c | 2 + dir.c | 19 +- dir.h | 2 +- entry.c | 2 + fsmonitor.c | 18 +- merge-recursive.c | 22 +- name-hash.c | 10 + pathspec.c | 4 +- pathspec.h | 4 +- preload-index.c | 2 + read-cache.c | 51 +++- repo-settings.c | 15 + repository.c | 11 +- repository.h | 3 + rerere.c | 2 + resolve-undo.c | 6 + setup.c | 3 + sha1-name.c | 3 + sparse-index.c | 360 +++++++++++++++++++++++ sparse-index.h | 23 ++ split-index.c | 2 + submodule.c | 22 +- submodule.h | 6 +- t/helper/test-read-cache.c | 77 ++++- t/t1092-sparse-checkout-compatibility.sh | 130 +++++++- tree.c | 2 + unpack-trees.c | 40 ++- wt-status.c | 21 +- wt-status.h | 1 + 46 files changed, 942 insertions(+), 61 deletions(-) create mode 100644 sparse-index.c create mode 100644 sparse-index.hbase-commit:
2271fe7848aa11b30e5313d95d9caebc2937fce5
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-847%2Fderrickstolee%2Fsparse-index%2Frfc-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-847/derrickstolee/sparse-index/rfc-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/847
--
gitgitgadget