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>
Elijah Newren committed
Dec 14, 2021 at 04:09 UTC
be61fd1181f62dcb7b6a5f41021e7f086d7c30b0
1 file changed
+37
-19
builtin/sparse-checkout.c
+37
-19
@@ -383,6 +383,41 @@ static int set_config(enum sparse_checkout_mode mode)
383
return 0;
384
}
385
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
+
421
static char const * const builtin_sparse_checkout_init_usage[] = {
422
N_("git sparse-checkout init [--cone] [--[no-]sparse-index]"),
423
NULL
@@ -399,7 +434,6 @@ static int sparse_checkout_init(int argc, const char **argv)
434
char *sparse_filename;
435
int res;
436
struct object_id oid;
402
- int mode;
437
struct strbuf pattern = STRBUF_INIT;
438
439
static struct option builtin_sparse_checkout_init_options[] = {
@@ -412,19 +446,14 @@ static int sparse_checkout_init(int argc, const char **argv)
446
447
repo_read_index(the_repository);
448
449
+ init_opts.cone_mode = -1;
450
init_opts.sparse_index = -1;
451
452
argc = parse_options(argc, argv, NULL,
453
builtin_sparse_checkout_init_options,
454
builtin_sparse_checkout_init_usage, 0);
455
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))
457
return 1;
458
459
memset(&pl, 0, sizeof(pl));
@@ -432,17 +461,6 @@ static int sparse_checkout_init(int argc, const char **argv)
461
sparse_filename = get_sparse_checkout_filename();
462
res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
463
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
-
464
/* If we already have a sparse-checkout file, use it. */
465
if (res >= 0) {
466
free(sparse_filename);