Skip to content

Commit be61fd1

Browse files
newrengitster
authored andcommitted
sparse-checkout: split out code for tweaking settings config
`init` has some code for handling updates to either cone mode or the sparse-index setting. We would like to be able to reuse this elsewhere, namely in `set` and `reapply`. Split this function out, and make it slightly more general so it can handle being called from the new callers. Reviewed-by: Derrick Stolee <[email protected]> Reviewed-by: Victoria Dye <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f85751a commit be61fd1

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

builtin/sparse-checkout.c

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,41 @@ static int set_config(enum sparse_checkout_mode mode)
383383
return 0;
384384
}
385385

386+
static int update_modes(int *cone_mode, int *sparse_index)
387+
{
388+
int mode, record_mode;
389+
390+
/* Determine if we need to record the mode; ensure sparse checkout on */
391+
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
392+
393+
/* If not specified, use previous definition of cone mode */
394+
if (*cone_mode == -1 && core_apply_sparse_checkout)
395+
*cone_mode = core_sparse_checkout_cone;
396+
397+
/* Set cone/non-cone mode appropriately */
398+
core_apply_sparse_checkout = 1;
399+
if (*cone_mode == 1) {
400+
mode = MODE_CONE_PATTERNS;
401+
core_sparse_checkout_cone = 1;
402+
} else {
403+
mode = MODE_ALL_PATTERNS;
404+
}
405+
if (record_mode && set_config(mode))
406+
return 1;
407+
408+
/* Set sparse-index/non-sparse-index mode if specified */
409+
if (*sparse_index >= 0) {
410+
if (set_sparse_index_config(the_repository, *sparse_index) < 0)
411+
die(_("failed to modify sparse-index config"));
412+
413+
/* force an index rewrite */
414+
repo_read_index(the_repository);
415+
the_repository->index->updated_workdir = 1;
416+
}
417+
418+
return 0;
419+
}
420+
386421
static char const * const builtin_sparse_checkout_init_usage[] = {
387422
N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
388423
NULL
@@ -399,7 +434,6 @@ static int sparse_checkout_init(int argc, const char **argv)
399434
char *sparse_filename;
400435
int res;
401436
struct object_id oid;
402-
int mode;
403437
struct strbuf pattern = STRBUF_INIT;
404438

405439
static struct option builtin_sparse_checkout_init_options[] = {
@@ -412,37 +446,21 @@ static int sparse_checkout_init(int argc, const char **argv)
412446

413447
repo_read_index(the_repository);
414448

449+
init_opts.cone_mode = -1;
415450
init_opts.sparse_index = -1;
416451

417452
argc = parse_options(argc, argv, NULL,
418453
builtin_sparse_checkout_init_options,
419454
builtin_sparse_checkout_init_usage, 0);
420455

421-
if (init_opts.cone_mode) {
422-
mode = MODE_CONE_PATTERNS;
423-
core_sparse_checkout_cone = 1;
424-
} else
425-
mode = MODE_ALL_PATTERNS;
426-
427-
if (set_config(mode))
456+
if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
428457
return 1;
429458

430459
memset(&pl, 0, sizeof(pl));
431460

432461
sparse_filename = get_sparse_checkout_filename();
433462
res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
434463

435-
if (init_opts.sparse_index >= 0) {
436-
if (set_sparse_index_config(the_repository, init_opts.sparse_index) < 0)
437-
die(_("failed to modify sparse-index config"));
438-
439-
/* force an index rewrite */
440-
repo_read_index(the_repository);
441-
the_repository->index->updated_workdir = 1;
442-
}
443-
444-
core_apply_sparse_checkout = 1;
445-
446464
/* If we already have a sparse-checkout file, use it. */
447465
if (res >= 0) {
448466
free(sparse_filename);

0 commit comments

Comments
 (0)