sparse-index: add SPARSE_INDEX_MEMORY_ONLY flag

The convert_to_sparse() method checks for the GIT_TEST_SPARSE_INDEX environment variable or the "index.sparse" config setting before converting the index to a sparse one. This is for ease of use since all current consumers are preparing to compress the index before writing it to disk. If these settings are not enabled, then convert_to_sparse() silently returns without doing anything. We will add a consumer in the next change that wants to use the sparse index as an in-memory data structure, regardless of whether the on-disk format should be sparse. To that end, create the SPARSE_INDEX_MEMORY_ONLY flag that will skip these config checks when enabled. All current consumers are modified to pass '0' in the new 'flags' parameter. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 8, 2021 at 01:42 UTC ce7a9f014167ccf137eb09d5546eb1e8b550f11b
3 files changed +26 -18
read-cache.c
+2 -2
@@ -3069,7 +3069,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
3069 int ret;
3070 int was_full = !istate->sparse_index;
3071
3072 - ret = convert_to_sparse(istate);
3072 + ret = convert_to_sparse(istate, 0);
3073
3074 if (ret) {
3075 warning(_("failed to convert to a sparse-index"));
@@ -3182,7 +3182,7 @@ static int write_shared_index(struct index_state *istate,
3182 int ret, was_full = !istate->sparse_index;
3183
3184 move_cache_to_base_index(istate);
3185 - convert_to_sparse(istate);
3185 + convert_to_sparse(istate, 0);
3186
3187 trace2_region_enter_printf("index", "shared/do_write_index",
3188 the_repository, "%s", get_tempfile_path(*temp));
sparse-index.c
+22 -15
@@ -122,30 +122,37 @@ static int index_has_unmerged_entries(struct index_state *istate)
122 return 0;
123 }
124
125 -int convert_to_sparse(struct index_state *istate)
125 +int convert_to_sparse(struct index_state *istate, int flags)
126 {
127 int test_env;
128 - if (istate->split_index || istate->sparse_index || !istate->cache_nr ||
128 + if (istate->sparse_index || !istate->cache_nr ||
129 !core_apply_sparse_checkout || !core_sparse_checkout_cone)
130 return 0;
131
132 if (!istate->repo)
133 istate->repo = the_repository;
134
135 - /*
136 - * The GIT_TEST_SPARSE_INDEX environment variable triggers the
137 - * index.sparse config variable to be on.
138 - */
139 - test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
140 - if (test_env >= 0)
141 - set_sparse_index_config(istate->repo, test_env);
135 + if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
136 + /*
137 + * The sparse index is not (yet) integrated with a split index.
138 + */
139 + if (istate->split_index)
140 + return 0;
141 + /*
142 + * The GIT_TEST_SPARSE_INDEX environment variable triggers the
143 + * index.sparse config variable to be on.
144 + */
145 + test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
146 + if (test_env >= 0)
147 + set_sparse_index_config(istate->repo, test_env);
148
143 - /*
144 - * Only convert to sparse if index.sparse is set.
145 - */
146 - prepare_repo_settings(istate->repo);
147 - if (!istate->repo->settings.sparse_index)
148 - return 0;
149 + /*
150 + * Only convert to sparse if index.sparse is set.
151 + */
152 + prepare_repo_settings(istate->repo);
153 + if (!istate->repo->settings.sparse_index)
154 + return 0;
155 + }
156
157 if (init_sparse_checkout_patterns(istate))
158 return 0;
sparse-index.h
+2 -1
@@ -2,7 +2,8 @@
2 #define SPARSE_INDEX_H__
3
4 struct index_state;
5 -int convert_to_sparse(struct index_state *istate);
5 +#define SPARSE_INDEX_MEMORY_ONLY (1 << 0)
6 +int convert_to_sparse(struct index_state *istate, int flags);
7
8 /*
9 * Some places in the codebase expect to search for a specific path.