Skip to content

Commit ce7a9f0

Browse files
derrickstoleegitster
authored andcommitted
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]> Reviewed-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 77efbb3 commit ce7a9f0

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
@@ -122,30 +122,37 @@ static int index_has_unmerged_entries(struct index_state *istate)
122122
return 0;
123123
}
124124

125-
int convert_to_sparse(struct index_state *istate)
125+
int convert_to_sparse(struct index_state *istate, int flags)
126126
{
127127
int test_env;
128-
if (istate->split_index || istate->sparse_index || !istate->cache_nr ||
128+
if (istate->sparse_index || !istate->cache_nr ||
129129
!core_apply_sparse_checkout || !core_sparse_checkout_cone)
130130
return 0;
131131

132132
if (!istate->repo)
133133
istate->repo = the_repository;
134134

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);
142148

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+
}
149156

150157
if (init_sparse_checkout_patterns(istate))
151158
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)