Skip to content

Commit d80627e

Browse files
committed
sparse-index: create expand_to_pattern_list()
In order to allow modifying the sparse-checkout cone using a sparse index without expanding to a full one, we need to be able to replace sparse directory entries with their contained files and subdirectories so other code paths can discover those cache entries and write the corresponding files to disk before committing the index. We already have logic in ensure_full_index() that expands the index entries, so we will use that as our base. Create expand_to_pattern_list() which takes a pattern list, but for now mostly ignores it. The current implementation is only correct when the pattern list is NULL as that does the same as ensure_full_index(). In fact, ensure_full_index() is converted to a shim over expand_to_pattern_list(). A future update will actually implement expand_to_pattern_list() to its full capabilities. For now, it is created and documented. We also start using doc-style comments in sparse-index.h. Signed-off-by: Derrick Stolee <[email protected]>
1 parent a5b4290 commit d80627e

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

sparse-index.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,20 +239,42 @@ static int add_path_to_index(const struct object_id *oid,
239239
return 0;
240240
}
241241

242-
void ensure_full_index(struct index_state *istate)
242+
void expand_to_pattern_list(struct index_state *istate,
243+
struct pattern_list *pl)
243244
{
244245
int i;
245246
struct index_state *full;
246247
struct strbuf base = STRBUF_INIT;
247248

249+
/*
250+
* If the index is already full, then keep it full. We will convert
251+
* it to a sparse index on write, if possible.
252+
*/
248253
if (!istate || !istate->sparse_index)
249254
return;
250255

256+
/*
257+
* If our index is sparse, but our new pattern set does not use
258+
* cone mode patterns, then we need to expand the index before we
259+
* continue. A NULL pattern set indicates a full expansion to a
260+
* full index.
261+
*/
262+
if (pl && !pl->use_cone_patterns)
263+
pl = NULL;
264+
265+
/*
266+
* A NULL pattern set indicates we are expanding a full index, so
267+
* we use a special region name that indicates the full expansion.
268+
* This is used by test cases, but also helps to differentiate the
269+
* two cases.
270+
*/
271+
trace2_region_enter("index",
272+
pl ? "expand_to_pattern_list" : "ensure_full_index",
273+
istate->repo);
274+
251275
if (!istate->repo)
252276
istate->repo = the_repository;
253277

254-
trace2_region_enter("index", "ensure_full_index", istate->repo);
255-
256278
/* initialize basics of new index */
257279
full = xcalloc(1, sizeof(struct index_state));
258280
memcpy(full, istate, sizeof(struct index_state));
@@ -313,7 +335,14 @@ void ensure_full_index(struct index_state *istate)
313335
cache_tree_free(&istate->cache_tree);
314336
cache_tree_update(istate, 0);
315337

316-
trace2_region_leave("index", "ensure_full_index", istate->repo);
338+
trace2_region_leave("index",
339+
pl ? "expand_to_pattern_list" : "ensure_full_index",
340+
istate->repo);
341+
}
342+
343+
void ensure_full_index(struct index_state *istate)
344+
{
345+
expand_to_pattern_list(istate, NULL);
317346
}
318347

319348
/*

sparse-index.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct index_state;
55
#define SPARSE_INDEX_MEMORY_ONLY (1 << 0)
66
int convert_to_sparse(struct index_state *istate, int flags);
77

8-
/*
8+
/**
99
* Some places in the codebase expect to search for a specific path.
1010
* This path might be outside of the sparse-checkout definition, in
1111
* which case a sparse-index may not contain a path for that index.
@@ -21,4 +21,18 @@ void expand_to_path(struct index_state *istate,
2121
struct repository;
2222
int set_sparse_index_config(struct repository *repo, int enable);
2323

24+
struct pattern_list;
25+
26+
/**
27+
* Scan the given index and compare its entries to the given pattern list.
28+
* If the index is sparse and the pattern list uses cone mode patterns,
29+
* then modify the index to contain the all of the file entries within that
30+
* new pattern list. This expands sparse directories only as far as needed.
31+
*
32+
* If the pattern list is NULL or does not use cone mode patterns, then the
33+
* index is expanded to a full index.
34+
*/
35+
void expand_to_pattern_list(struct index_state *istate,
36+
struct pattern_list *pl);
37+
2438
#endif

0 commit comments

Comments
 (0)