@matheusbsbs / matheus-bagliano / commits / 6fb705abcb
Fork 0

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 <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Feb 11, 2020 at 15:02 UTC 6fb705abcb6044f07954b486d71c05151262b6b6
1 file changed +35 -29
builtin/sparse-checkout.c
+35 -29
@@ -412,36 +412,16 @@ static struct sparse_checkout_set_opts {
412 int use_stdin;
413 } set_opts;
414
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)
417 {
418 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 -
419 if (core_sparse_checkout_cone) {
420 struct strbuf line = STRBUF_INIT;
421
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;
425
426 if (set_opts.use_stdin) {
427 struct strbuf unquoted = STRBUF_INIT;
@@ -455,7 +435,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
435 strbuf_swap(&unquoted, &line);
436 }
437
458 - strbuf_to_cone_pattern(&line, &pl);
438 + strbuf_to_cone_pattern(&line, pl);
439 }
440
441 strbuf_release(&unquoted);
@@ -463,7 +443,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
443 for (i = 0; i < argc; i++) {
444 strbuf_setlen(&line, 0);
445 strbuf_addstr(&line, argv[i]);
466 - strbuf_to_cone_pattern(&line, &pl);
446 + strbuf_to_cone_pattern(&line, pl);
447 }
448 }
449 } else {
@@ -473,13 +453,39 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
453 while (!strbuf_getline(&line, stdin)) {
454 size_t len;
455 char *buf = strbuf_detach(&line, &len);
476 - add_pattern(buf, empty_base, 0, &pl, 0);
456 + add_pattern(buf, empty_base, 0, pl, 0);
457 }
458 } else {
459 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);
461 }
462 }
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);
489
490 if (!core_apply_sparse_checkout) {
491 set_config(MODE_ALL_PATTERNS);