sparse-checkout: break apart functions for sparse_checkout_(set|add)

sparse_checkout_set() was reused by sparse_checkout_add() with the only difference being a single parameter being passed to that function. However, we would like sparse_checkout_set() to do the same work that sparse_checkout_init() does if sparse checkouts are not already enabled. To facilitate this transition, give each mode their own copy of the function. This does not introduce any behavioral changes; that will come in a subsequent patch. 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 0b624e039cc2c9e30333445e8c67002f41d63678
1 file changed +40 -14
builtin/sparse-checkout.c
+40 -14
@@ -515,15 +515,6 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
515 insert_recursive_pattern(pl, line);
516 }
517
518 -static char const * const builtin_sparse_checkout_set_usage[] = {
519 - N_("git sparse-checkout (set|add) (--stdin | <patterns>)"),
520 - NULL
521 -};
522 -
523 -static struct sparse_checkout_set_opts {
524 - int use_stdin;
525 -} set_opts;
526 -
518 static void add_patterns_from_input(struct pattern_list *pl,
519 int argc, const char **argv,
520 int use_stdin)
@@ -663,8 +654,43 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin,
654 return result;
655 }
656
666 -static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
667 - enum modify_type m)
657 +static char const * const builtin_sparse_checkout_add_usage[] = {
658 + N_("git sparse-checkout add (--stdin | <patterns>)"),
659 + NULL
660 +};
661 +
662 +static struct sparse_checkout_add_opts {
663 + int use_stdin;
664 +} add_opts;
665 +
666 +static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
667 +{
668 + static struct option builtin_sparse_checkout_add_options[] = {
669 + OPT_BOOL(0, "stdin", &add_opts.use_stdin,
670 + N_("read patterns from standard in")),
671 + OPT_END(),
672 + };
673 +
674 + repo_read_index(the_repository);
675 +
676 + argc = parse_options(argc, argv, prefix,
677 + builtin_sparse_checkout_add_options,
678 + builtin_sparse_checkout_add_usage,
679 + PARSE_OPT_KEEP_UNKNOWN);
680 +
681 + return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
682 +}
683 +
684 +static char const * const builtin_sparse_checkout_set_usage[] = {
685 + N_("git sparse-checkout set (--stdin | <patterns>)"),
686 + NULL
687 +};
688 +
689 +static struct sparse_checkout_set_opts {
690 + int use_stdin;
691 +} set_opts;
692 +
693 +static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
694 {
695 static struct option builtin_sparse_checkout_set_options[] = {
696 OPT_BOOL(0, "stdin", &set_opts.use_stdin,
@@ -679,7 +705,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
705 builtin_sparse_checkout_set_usage,
706 PARSE_OPT_KEEP_UNKNOWN);
707
682 - return modify_pattern_list(argc, argv, set_opts.use_stdin, m);
708 + return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
709 }
710
711 static char const * const builtin_sparse_checkout_reapply_usage[] = {
@@ -762,9 +788,9 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
788 if (!strcmp(argv[0], "init"))
789 return sparse_checkout_init(argc, argv);
790 if (!strcmp(argv[0], "set"))
765 - return sparse_checkout_set(argc, argv, prefix, REPLACE);
791 + return sparse_checkout_set(argc, argv, prefix);
792 if (!strcmp(argv[0], "add"))
767 - return sparse_checkout_set(argc, argv, prefix, ADD);
793 + return sparse_checkout_add(argc, argv, prefix);
794 if (!strcmp(argv[0], "reapply"))
795 return sparse_checkout_reapply(argc, argv);
796 if (!strcmp(argv[0], "disable"))