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 <dstolee@microsoft.com>
Reviewed-by: Victoria Dye <vdye@github.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Elijah Newren 2021-12-14 04:09:07 +00:00 committed by Junio C Hamano
parent f85751a147
commit be61fd1181

View file

@ -383,6 +383,41 @@ static int set_config(enum sparse_checkout_mode mode)
return 0; return 0;
} }
static int update_modes(int *cone_mode, int *sparse_index)
{
int mode, record_mode;
/* Determine if we need to record the mode; ensure sparse checkout on */
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
/* If not specified, use previous definition of cone mode */
if (*cone_mode == -1 && core_apply_sparse_checkout)
*cone_mode = core_sparse_checkout_cone;
/* Set cone/non-cone mode appropriately */
core_apply_sparse_checkout = 1;
if (*cone_mode == 1) {
mode = MODE_CONE_PATTERNS;
core_sparse_checkout_cone = 1;
} else {
mode = MODE_ALL_PATTERNS;
}
if (record_mode && set_config(mode))
return 1;
/* Set sparse-index/non-sparse-index mode if specified */
if (*sparse_index >= 0) {
if (set_sparse_index_config(the_repository, *sparse_index) < 0)
die(_("failed to modify sparse-index config"));
/* force an index rewrite */
repo_read_index(the_repository);
the_repository->index->updated_workdir = 1;
}
return 0;
}
static char const * const builtin_sparse_checkout_init_usage[] = { static char const * const builtin_sparse_checkout_init_usage[] = {
N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"), N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
NULL NULL
@ -399,7 +434,6 @@ static int sparse_checkout_init(int argc, const char **argv)
char *sparse_filename; char *sparse_filename;
int res; int res;
struct object_id oid; struct object_id oid;
int mode;
struct strbuf pattern = STRBUF_INIT; struct strbuf pattern = STRBUF_INIT;
static struct option builtin_sparse_checkout_init_options[] = { static struct option builtin_sparse_checkout_init_options[] = {
@ -412,19 +446,14 @@ static int sparse_checkout_init(int argc, const char **argv)
repo_read_index(the_repository); repo_read_index(the_repository);
init_opts.cone_mode = -1;
init_opts.sparse_index = -1; init_opts.sparse_index = -1;
argc = parse_options(argc, argv, NULL, argc = parse_options(argc, argv, NULL,
builtin_sparse_checkout_init_options, builtin_sparse_checkout_init_options,
builtin_sparse_checkout_init_usage, 0); builtin_sparse_checkout_init_usage, 0);
if (init_opts.cone_mode) { if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
mode = MODE_CONE_PATTERNS;
core_sparse_checkout_cone = 1;
} else
mode = MODE_ALL_PATTERNS;
if (set_config(mode))
return 1; return 1;
memset(&pl, 0, sizeof(pl)); memset(&pl, 0, sizeof(pl));
@ -432,17 +461,6 @@ static int sparse_checkout_init(int argc, const char **argv)
sparse_filename = get_sparse_checkout_filename(); sparse_filename = get_sparse_checkout_filename();
res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0); res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
if (init_opts.sparse_index >= 0) {
if (set_sparse_index_config(the_repository, init_opts.sparse_index) < 0)
die(_("failed to modify sparse-index config"));
/* force an index rewrite */
repo_read_index(the_repository);
the_repository->index->updated_workdir = 1;
}
core_apply_sparse_checkout = 1;
/* If we already have a sparse-checkout file, use it. */ /* If we already have a sparse-checkout file, use it. */
if (res >= 0) { if (res >= 0) {
free(sparse_filename); free(sparse_filename);