Skip to content

Commit 5282a86

Browse files
committed
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 <[email protected]>
1 parent 4537233 commit 5282a86

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

read-cache.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
30693069
int ret;
30703070
int was_full = !istate->sparse_index;
30713071

3072-
ret = convert_to_sparse(istate);
3072+
ret = convert_to_sparse(istate, 0);
30733073

30743074
if (ret) {
30753075
warning(_("failed to convert to a sparse-index"));
@@ -3182,7 +3182,7 @@ static int write_shared_index(struct index_state *istate,
31823182
int ret, was_full = !istate->sparse_index;
31833183

31843184
move_cache_to_base_index(istate);
3185-
convert_to_sparse(istate);
3185+
convert_to_sparse(istate, 0);
31863186

31873187
trace2_region_enter_printf("index", "shared/do_write_index",
31883188
the_repository, "%s", get_tempfile_path(*temp));

sparse-index.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,31 +124,38 @@ static int index_has_unmerged_entries(struct index_state *istate)
124124
return 0;
125125
}
126126

127-
int convert_to_sparse(struct index_state *istate)
127+
int convert_to_sparse(struct index_state *istate, int flags)
128128
{
129129
int test_env;
130130

131-
if (istate->split_index || istate->sparse_index || !istate->cache_nr ||
131+
if (istate->sparse_index || !istate->cache_nr ||
132132
!core_apply_sparse_checkout || !core_sparse_checkout_cone)
133133
return 0;
134134

135135
if (!istate->repo)
136136
istate->repo = the_repository;
137137

138-
/*
139-
* The GIT_TEST_SPARSE_INDEX environment variable triggers the
140-
* index.sparse config variable to be on.
141-
*/
142-
test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
143-
if (test_env >= 0)
144-
set_sparse_index_config(istate->repo, test_env);
138+
if (!(flags & SPARSE_INDEX_MEMORY_ONLY)) {
139+
/*
140+
* The sparse index is not (yet) integrated with a split index.
141+
*/
142+
if (istate->split_index)
143+
return 0;
144+
/*
145+
* The GIT_TEST_SPARSE_INDEX environment variable triggers the
146+
* index.sparse config variable to be on.
147+
*/
148+
test_env = git_env_bool("GIT_TEST_SPARSE_INDEX", -1);
149+
if (test_env >= 0)
150+
set_sparse_index_config(istate->repo, test_env);
145151

146-
/*
147-
* Only convert to sparse if index.sparse is set.
148-
*/
149-
prepare_repo_settings(istate->repo);
150-
if (!istate->repo->settings.sparse_index)
151-
return 0;
152+
/*
153+
* Only convert to sparse if index.sparse is set.
154+
*/
155+
prepare_repo_settings(istate->repo);
156+
if (!istate->repo->settings.sparse_index)
157+
return 0;
158+
}
152159

153160
if (init_sparse_checkout_patterns(istate) < 0)
154161
return 0;

sparse-index.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#define SPARSE_INDEX_H__
33

44
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);
67

78
/*
89
* Some places in the codebase expect to search for a specific path.

0 commit comments

Comments
 (0)