sparse-index: add ensure_correct_sparsity function

The `ensure_correct_sparsity` function is intended to provide a means of aligning the in-core index with the sparsity required by the repository settings and other properties of the index. The function first checks whether a sparse index is allowed (per repository & sparse checkout pattern settings). If the sparse index may be used, the index is converted to sparse; otherwise, it is explicitly expanded with `ensure_full_index`. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Victoria Dye <vdye@github.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Nov 23, 2021 at 00:20 UTC b93fea08d24b0ceb498445cc80c91e26a6bff29b
2 files changed +30 -4
sparse-index.c
+29 -4
@@ -122,17 +122,17 @@ static int index_has_unmerged_entries(struct index_state *istate)
122 return 0;
123 }
124
125 -int convert_to_sparse(struct index_state *istate, int flags)
125 +static int is_sparse_index_allowed(struct index_state *istate, int flags)
126 {
127 - int test_env;
128 - if (istate->sparse_index || !istate->cache_nr ||
129 - !core_apply_sparse_checkout || !core_sparse_checkout_cone)
127 + if (!core_apply_sparse_checkout || !core_sparse_checkout_cone)
128 return 0;
129
130 if (!istate->repo)
131 istate->repo = the_repository;
132
133 if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
134 + int test_env;
135 +
136 /*
137 * The sparse index is not (yet) integrated with a split index.
138 */
@@ -168,6 +168,19 @@ int convert_to_sparse(struct index_state *istate, int flags)
168 if (!istate->sparse_checkout_patterns->use_cone_patterns)
169 return 0;
170
171 + return 1;
172 +}
173 +
174 +int convert_to_sparse(struct index_state *istate, int flags)
175 +{
176 + /*
177 + * If the index is already sparse, empty, or otherwise
178 + * cannot be converted to sparse, do not convert.
179 + */
180 + if (istate->sparse_index || !istate->cache_nr ||
181 + !is_sparse_index_allowed(istate, flags))
182 + return 0;
183 +
184 /*
185 * NEEDSWORK: If we have unmerged entries, then stay full.
186 * Unmerged entries prevent the cache-tree extension from working.
@@ -316,6 +329,18 @@ void ensure_full_index(struct index_state *istate)
329 trace2_region_leave("index", "ensure_full_index", istate->repo);
330 }
331
332 +void ensure_correct_sparsity(struct index_state *istate)
333 +{
334 + /*
335 + * If the index can be sparse, make it sparse. Otherwise,
336 + * ensure the index is full.
337 + */
338 + if (is_sparse_index_allowed(istate, 0))
339 + convert_to_sparse(istate, 0);
340 + else
341 + ensure_full_index(istate);
342 +}
343 +
344 /*
345 * This static global helps avoid infinite recursion between
346 * expand_to_path() and index_file_exists().
sparse-index.h
+1
@@ -4,6 +4,7 @@
4 struct index_state;
5 #define SPARSE_INDEX_MEMORY_ONLY (1 << 0)
6 int convert_to_sparse(struct index_state *istate, int flags);
7 +void ensure_correct_sparsity(struct index_state *istate);
8
9 /*
10 * Some places in the codebase expect to search for a specific path.