Skip to content

Commit c276c21

Browse files
committed
Merge branch 'ds/sparse-sparse-checkout'
"sparse-checkout" learns to work well with the sparse-index feature. * ds/sparse-sparse-checkout: sparse-checkout: integrate with sparse index p2000: add test for 'git sparse-checkout [add|set]' sparse-index: complete partial expansion sparse-index: partially expand directories sparse-checkout: --no-sparse-index needs a full index cache-tree: implement cache_tree_find_path() sparse-index: introduce partially-sparse indexes sparse-index: create expand_index() t1092: stress test 'git sparse-checkout set' t1092: refactor 'sparse-index contents' test
2 parents 0916804 + 598b1e7 commit c276c21

10 files changed

+275
-46
lines changed

builtin/sparse-checkout.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static void clean_tracked_sparse_directories(struct repository *r)
128128
* sparse index will not delete directories that contain
129129
* conflicted entries or submodules.
130130
*/
131-
if (!r->index->sparse_index) {
131+
if (r->index->sparse_index == INDEX_EXPANDED) {
132132
/*
133133
* If something, such as a merge conflict or other concern,
134134
* prevents us from converting to a sparse index, then do
@@ -413,6 +413,9 @@ static int update_modes(int *cone_mode, int *sparse_index)
413413
/* force an index rewrite */
414414
repo_read_index(the_repository);
415415
the_repository->index->updated_workdir = 1;
416+
417+
if (!*sparse_index)
418+
ensure_full_index(the_repository->index);
416419
}
417420

418421
return 0;
@@ -934,6 +937,9 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
934937

935938
git_config(git_default_config, NULL);
936939

940+
prepare_repo_settings(the_repository);
941+
the_repository->settings.command_requires_full_index = 0;
942+
937943
if (argc > 0) {
938944
if (!strcmp(argv[0], "list"))
939945
return sparse_checkout_list(argc, argv);

cache-tree.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
101101
return find_subtree(it, path, pathlen, 1);
102102
}
103103

104+
struct cache_tree *cache_tree_find_path(struct cache_tree *it, const char *path)
105+
{
106+
const char *slash;
107+
int namelen;
108+
struct cache_tree_sub it_sub = {
109+
.cache_tree = it,
110+
};
111+
struct cache_tree_sub *down = &it_sub;
112+
113+
while (down) {
114+
slash = strchrnul(path, '/');
115+
namelen = slash - path;
116+
down->cache_tree->entry_count = -1;
117+
if (!*slash) {
118+
int pos;
119+
pos = cache_tree_subtree_pos(down->cache_tree, path, namelen);
120+
if (0 <= pos)
121+
return down->cache_tree->down[pos]->cache_tree;
122+
return NULL;
123+
}
124+
down = find_subtree(it, path, namelen, 0);
125+
path = slash + 1;
126+
}
127+
128+
return NULL;
129+
}
130+
104131
static int do_invalidate_path(struct cache_tree *it, const char *path)
105132
{
106133
/* a/b/c

cache-tree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
2929

3030
int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen);
3131

32+
struct cache_tree *cache_tree_find_path(struct cache_tree *it, const char *path);
33+
3234
void cache_tree_write(struct strbuf *, struct cache_tree *root);
3335
struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
3436

cache.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,29 @@ struct untracked_cache;
310310
struct progress;
311311
struct pattern_list;
312312

313+
enum sparse_index_mode {
314+
/*
315+
* There are no sparse directories in the index at all.
316+
*
317+
* Repositories that don't use cone-mode sparse-checkout will
318+
* always have their indexes in this mode.
319+
*/
320+
INDEX_EXPANDED = 0,
321+
322+
/*
323+
* The index has already been collapsed to sparse directories
324+
* whereever possible.
325+
*/
326+
INDEX_COLLAPSED,
327+
328+
/*
329+
* The sparse directories that exist are outside the
330+
* sparse-checkout boundary, but it is possible that some file
331+
* entries could collapse to sparse directory entries.
332+
*/
333+
INDEX_PARTIALLY_SPARSE,
334+
};
335+
313336
struct index_state {
314337
struct cache_entry **cache;
315338
unsigned int version;
@@ -323,14 +346,8 @@ struct index_state {
323346
drop_cache_tree : 1,
324347
updated_workdir : 1,
325348
updated_skipworktree : 1,
326-
fsmonitor_has_run_once : 1,
327-
328-
/*
329-
* sparse_index == 1 when sparse-directory
330-
* entries exist. Requires sparse-checkout
331-
* in cone mode.
332-
*/
333-
sparse_index : 1;
349+
fsmonitor_has_run_once : 1;
350+
enum sparse_index_mode sparse_index;
334351
struct hashmap name_hash;
335352
struct hashmap dir_hash;
336353
struct object_id oid;

read-cache.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static const char *alternate_index_output;
112112
static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
113113
{
114114
if (S_ISSPARSEDIR(ce->ce_mode))
115-
istate->sparse_index = 1;
115+
istate->sparse_index = INDEX_COLLAPSED;
116116

117117
istate->cache[nr] = ce;
118118
add_name_hash(istate, ce);
@@ -1856,7 +1856,7 @@ static int read_index_extension(struct index_state *istate,
18561856
break;
18571857
case CACHE_EXT_SPARSE_DIRECTORIES:
18581858
/* no content, only an indicator */
1859-
istate->sparse_index = 1;
1859+
istate->sparse_index = INDEX_COLLAPSED;
18601860
break;
18611861
default:
18621862
if (*ext < 'A' || 'Z' < *ext)
@@ -3165,7 +3165,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
31653165
unsigned flags)
31663166
{
31673167
int ret;
3168-
int was_full = !istate->sparse_index;
3168+
int was_full = istate->sparse_index == INDEX_EXPANDED;
31693169

31703170
ret = convert_to_sparse(istate, 0);
31713171

sparse-index.c

Lines changed: 117 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#include "dir.h"
1010
#include "fsmonitor.h"
1111

12+
struct modify_index_context {
13+
struct index_state *write;
14+
struct pattern_list *pl;
15+
};
16+
1217
static struct cache_entry *construct_sparse_dir_entry(
1318
struct index_state *istate,
1419
const char *sparse_dir,
@@ -173,7 +178,7 @@ int convert_to_sparse(struct index_state *istate, int flags)
173178
* If the index is already sparse, empty, or otherwise
174179
* cannot be converted to sparse, do not convert.
175180
*/
176-
if (istate->sparse_index || !istate->cache_nr ||
181+
if (istate->sparse_index == INDEX_COLLAPSED || !istate->cache_nr ||
177182
!is_sparse_index_allowed(istate, flags))
178183
return 0;
179184

@@ -214,7 +219,7 @@ int convert_to_sparse(struct index_state *istate, int flags)
214219
FREE_AND_NULL(istate->fsmonitor_dirty);
215220
FREE_AND_NULL(istate->fsmonitor_last_update);
216221

217-
istate->sparse_index = 1;
222+
istate->sparse_index = INDEX_COLLAPSED;
218223
trace2_region_leave("index", "convert_to_sparse", istate->repo);
219224
return 0;
220225
}
@@ -231,56 +236,148 @@ static int add_path_to_index(const struct object_id *oid,
231236
struct strbuf *base, const char *path,
232237
unsigned int mode, void *context)
233238
{
234-
struct index_state *istate = (struct index_state *)context;
239+
struct modify_index_context *ctx = (struct modify_index_context *)context;
235240
struct cache_entry *ce;
236241
size_t len = base->len;
237242

238-
if (S_ISDIR(mode))
239-
return READ_TREE_RECURSIVE;
243+
if (S_ISDIR(mode)) {
244+
int dtype;
245+
size_t baselen = base->len;
246+
if (!ctx->pl)
247+
return READ_TREE_RECURSIVE;
248+
249+
/*
250+
* Have we expanded to a point outside of the sparse-checkout?
251+
*
252+
* Artificially pad the path name with a slash "/" to
253+
* indicate it as a directory, and add an arbitrary file
254+
* name ("-") so we can consider base->buf as a file name
255+
* to match against the cone-mode patterns.
256+
*
257+
* If we compared just "path", then we would expand more
258+
* than we should. Since every file at root is always
259+
* included, we would expand every directory at root at
260+
* least one level deep instead of using sparse directory
261+
* entries.
262+
*/
263+
strbuf_addstr(base, path);
264+
strbuf_add(base, "/-", 2);
265+
266+
if (path_matches_pattern_list(base->buf, base->len,
267+
NULL, &dtype,
268+
ctx->pl, ctx->write)) {
269+
strbuf_setlen(base, baselen);
270+
return READ_TREE_RECURSIVE;
271+
}
240272

241-
strbuf_addstr(base, path);
273+
/*
274+
* The path "{base}{path}/" is a sparse directory. Create the correct
275+
* name for inserting the entry into the index.
276+
*/
277+
strbuf_setlen(base, base->len - 1);
278+
} else {
279+
strbuf_addstr(base, path);
280+
}
242281

243-
ce = make_cache_entry(istate, mode, oid, base->buf, 0, 0);
282+
ce = make_cache_entry(ctx->write, mode, oid, base->buf, 0, 0);
244283
ce->ce_flags |= CE_SKIP_WORKTREE | CE_EXTENDED;
245-
set_index_entry(istate, istate->cache_nr++, ce);
284+
set_index_entry(ctx->write, ctx->write->cache_nr++, ce);
246285

247286
strbuf_setlen(base, len);
248287
return 0;
249288
}
250289

251-
void ensure_full_index(struct index_state *istate)
290+
void expand_index(struct index_state *istate, struct pattern_list *pl)
252291
{
253292
int i;
254293
struct index_state *full;
255294
struct strbuf base = STRBUF_INIT;
295+
const char *tr_region;
296+
struct modify_index_context ctx;
256297

257-
if (!istate || !istate->sparse_index)
298+
/*
299+
* If the index is already full, then keep it full. We will convert
300+
* it to a sparse index on write, if possible.
301+
*/
302+
if (!istate || istate->sparse_index == INDEX_EXPANDED)
258303
return;
259304

305+
/*
306+
* If our index is sparse, but our new pattern set does not use
307+
* cone mode patterns, then we need to expand the index before we
308+
* continue. A NULL pattern set indicates a full expansion to a
309+
* full index.
310+
*/
311+
if (pl && !pl->use_cone_patterns) {
312+
pl = NULL;
313+
} else {
314+
/*
315+
* We might contract file entries into sparse-directory
316+
* entries, and for that we will need the cache tree to
317+
* be recomputed.
318+
*/
319+
cache_tree_free(&istate->cache_tree);
320+
321+
/*
322+
* If there is a problem creating the cache tree, then we
323+
* need to expand to a full index since we cannot satisfy
324+
* the current request as a sparse index.
325+
*/
326+
if (cache_tree_update(istate, 0))
327+
pl = NULL;
328+
}
329+
260330
if (!istate->repo)
261331
istate->repo = the_repository;
262332

263-
trace2_region_enter("index", "ensure_full_index", istate->repo);
333+
/*
334+
* A NULL pattern set indicates we are expanding a full index, so
335+
* we use a special region name that indicates the full expansion.
336+
* This is used by test cases, but also helps to differentiate the
337+
* two cases.
338+
*/
339+
tr_region = pl ? "expand_index" : "ensure_full_index";
340+
trace2_region_enter("index", tr_region, istate->repo);
264341

265342
/* initialize basics of new index */
266343
full = xcalloc(1, sizeof(struct index_state));
267344
memcpy(full, istate, sizeof(struct index_state));
268345

346+
/*
347+
* This slightly-misnamed 'full' index might still be sparse if we
348+
* are only modifying the list of sparse directories. This hinges
349+
* on whether we have a non-NULL pattern list.
350+
*/
351+
full->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED;
352+
269353
/* then change the necessary things */
270-
full->sparse_index = 0;
271354
full->cache_alloc = (3 * istate->cache_alloc) / 2;
272355
full->cache_nr = 0;
273356
ALLOC_ARRAY(full->cache, full->cache_alloc);
274357

358+
ctx.write = full;
359+
ctx.pl = pl;
360+
275361
for (i = 0; i < istate->cache_nr; i++) {
276362
struct cache_entry *ce = istate->cache[i];
277363
struct tree *tree;
278364
struct pathspec ps;
365+
int dtype;
279366

280367
if (!S_ISSPARSEDIR(ce->ce_mode)) {
281368
set_index_entry(full, full->cache_nr++, ce);
282369
continue;
283370
}
371+
372+
/* We now have a sparse directory entry. Should we expand? */
373+
if (pl &&
374+
path_matches_pattern_list(ce->name, ce->ce_namelen,
375+
NULL, &dtype,
376+
pl, istate) == NOT_MATCHED) {
377+
set_index_entry(full, full->cache_nr++, ce);
378+
continue;
379+
}
380+
284381
if (!(ce->ce_flags & CE_SKIP_WORKTREE))
285382
warning(_("index entry is a directory, but not sparse (%08x)"),
286383
ce->ce_flags);
@@ -297,7 +394,7 @@ void ensure_full_index(struct index_state *istate)
297394
strbuf_add(&base, ce->name, strlen(ce->name));
298395

299396
read_tree_at(istate->repo, tree, &base, &ps,
300-
add_path_to_index, full);
397+
add_path_to_index, &ctx);
301398

302399
/* free directory entries. full entries are re-used */
303400
discard_cache_entry(ce);
@@ -306,7 +403,7 @@ void ensure_full_index(struct index_state *istate)
306403
/* Copy back into original index. */
307404
memcpy(&istate->name_hash, &full->name_hash, sizeof(full->name_hash));
308405
memcpy(&istate->dir_hash, &full->dir_hash, sizeof(full->dir_hash));
309-
istate->sparse_index = 0;
406+
istate->sparse_index = pl ? INDEX_PARTIALLY_SPARSE : INDEX_EXPANDED;
310407
free(istate->cache);
311408
istate->cache = full->cache;
312409
istate->cache_nr = full->cache_nr;
@@ -322,7 +419,12 @@ void ensure_full_index(struct index_state *istate)
322419
cache_tree_free(&istate->cache_tree);
323420
cache_tree_update(istate, 0);
324421

325-
trace2_region_leave("index", "ensure_full_index", istate->repo);
422+
trace2_region_leave("index", tr_region, istate->repo);
423+
}
424+
425+
void ensure_full_index(struct index_state *istate)
426+
{
427+
expand_index(istate, NULL);
326428
}
327429

328430
void ensure_correct_sparsity(struct index_state *istate)

sparse-index.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,17 @@ void expand_to_path(struct index_state *istate,
2424
struct repository;
2525
int set_sparse_index_config(struct repository *repo, int enable);
2626

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

t/perf/p2000-sparse-operations.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ test_perf_on_all git add -A
112112
test_perf_on_all git add .
113113
test_perf_on_all git commit -a -m A
114114
test_perf_on_all git checkout -f -
115+
test_perf_on_all "git sparse-checkout add f2/f3/f1 && git sparse-checkout set $SPARSE_CONE"
115116
test_perf_on_all git reset
116117
test_perf_on_all git reset --hard
117118
test_perf_on_all git reset -- does-not-exist

0 commit comments

Comments
 (0)