Skip to content

Commit 6fb705a

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: extract add_patterns_from_input()
In anticipation of extending the sparse-checkout builtin with "add" and "remove" subcommands, extract the code that fills a pattern list based on the input values. The input changes depending on the presence of "--stdin" or the value of core.sparseCheckoutCone. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f998a3f commit 6fb705a

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

builtin/sparse-checkout.c

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -412,36 +412,16 @@ static struct sparse_checkout_set_opts {
412412
int use_stdin;
413413
} set_opts;
414414

415-
static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
415+
static void add_patterns_from_input(struct pattern_list *pl,
416+
int argc, const char **argv)
416417
{
417418
int i;
418-
struct pattern_list pl;
419-
int result;
420-
int changed_config = 0;
421-
422-
static struct option builtin_sparse_checkout_set_options[] = {
423-
OPT_BOOL(0, "stdin", &set_opts.use_stdin,
424-
N_("read patterns from standard in")),
425-
OPT_END(),
426-
};
427-
428-
repo_read_index(the_repository);
429-
require_clean_work_tree(the_repository,
430-
N_("set sparse-checkout patterns"), NULL, 1, 0);
431-
432-
memset(&pl, 0, sizeof(pl));
433-
434-
argc = parse_options(argc, argv, prefix,
435-
builtin_sparse_checkout_set_options,
436-
builtin_sparse_checkout_set_usage,
437-
PARSE_OPT_KEEP_UNKNOWN);
438-
439419
if (core_sparse_checkout_cone) {
440420
struct strbuf line = STRBUF_INIT;
441421

442-
hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
443-
hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
444-
pl.use_cone_patterns = 1;
422+
hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
423+
hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
424+
pl->use_cone_patterns = 1;
445425

446426
if (set_opts.use_stdin) {
447427
struct strbuf unquoted = STRBUF_INIT;
@@ -455,15 +435,15 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
455435
strbuf_swap(&unquoted, &line);
456436
}
457437

458-
strbuf_to_cone_pattern(&line, &pl);
438+
strbuf_to_cone_pattern(&line, pl);
459439
}
460440

461441
strbuf_release(&unquoted);
462442
} else {
463443
for (i = 0; i < argc; i++) {
464444
strbuf_setlen(&line, 0);
465445
strbuf_addstr(&line, argv[i]);
466-
strbuf_to_cone_pattern(&line, &pl);
446+
strbuf_to_cone_pattern(&line, pl);
467447
}
468448
}
469449
} else {
@@ -473,13 +453,39 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
473453
while (!strbuf_getline(&line, stdin)) {
474454
size_t len;
475455
char *buf = strbuf_detach(&line, &len);
476-
add_pattern(buf, empty_base, 0, &pl, 0);
456+
add_pattern(buf, empty_base, 0, pl, 0);
477457
}
478458
} else {
479459
for (i = 0; i < argc; i++)
480-
add_pattern(argv[i], empty_base, 0, &pl, 0);
460+
add_pattern(argv[i], empty_base, 0, pl, 0);
481461
}
482462
}
463+
}
464+
465+
static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
466+
{
467+
struct pattern_list pl;
468+
int result;
469+
int changed_config = 0;
470+
471+
static struct option builtin_sparse_checkout_set_options[] = {
472+
OPT_BOOL(0, "stdin", &set_opts.use_stdin,
473+
N_("read patterns from standard in")),
474+
OPT_END(),
475+
};
476+
477+
repo_read_index(the_repository);
478+
require_clean_work_tree(the_repository,
479+
N_("set sparse-checkout patterns"), NULL, 1, 0);
480+
481+
memset(&pl, 0, sizeof(pl));
482+
483+
argc = parse_options(argc, argv, prefix,
484+
builtin_sparse_checkout_set_options,
485+
builtin_sparse_checkout_set_usage,
486+
PARSE_OPT_KEEP_UNKNOWN);
487+
488+
add_patterns_from_input(&pl, argc, argv);
483489

484490
if (!core_apply_sparse_checkout) {
485491
set_config(MODE_ALL_PATTERNS);

0 commit comments

Comments
 (0)