Skip to content

Commit 1c3502b

Browse files
szedergitster
authored andcommitted
builtin/sparse-checkout.c: let parse-options parse subcommands
'git sparse-checkout' parses its subcommands with a couple of if statements. parse-options has just learned to parse subcommands, so let's use that facility instead, with the benefits of shorter code, handling missing or unknown subcommands, and listing subcommands for Bash completion. Note that some of the functions implementing each subcommand only accept the 'argc' and '**argv' parameters, so add a (unused) '*prefix' parameter to make them match the type expected by parse-options, and thus avoid casting function pointers. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b26a412 commit 1c3502b

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

builtin/sparse-checkout.c

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static char const * const builtin_sparse_checkout_list_usage[] = {
4848
NULL
4949
};
5050

51-
static int sparse_checkout_list(int argc, const char **argv)
51+
static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
5252
{
5353
static struct option builtin_sparse_checkout_list_options[] = {
5454
OPT_END(),
@@ -431,7 +431,7 @@ static struct sparse_checkout_init_opts {
431431
int sparse_index;
432432
} init_opts;
433433

434-
static int sparse_checkout_init(int argc, const char **argv)
434+
static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
435435
{
436436
struct pattern_list pl;
437437
char *sparse_filename;
@@ -843,7 +843,8 @@ static struct sparse_checkout_reapply_opts {
843843
int sparse_index;
844844
} reapply_opts;
845845

846-
static int sparse_checkout_reapply(int argc, const char **argv)
846+
static int sparse_checkout_reapply(int argc, const char **argv,
847+
const char *prefix)
847848
{
848849
static struct option builtin_sparse_checkout_reapply_options[] = {
849850
OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
@@ -876,7 +877,8 @@ static char const * const builtin_sparse_checkout_disable_usage[] = {
876877
NULL
877878
};
878879

879-
static int sparse_checkout_disable(int argc, const char **argv)
880+
static int sparse_checkout_disable(int argc, const char **argv,
881+
const char *prefix)
880882
{
881883
static struct option builtin_sparse_checkout_disable_options[] = {
882884
OPT_END(),
@@ -922,39 +924,25 @@ static int sparse_checkout_disable(int argc, const char **argv)
922924

923925
int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
924926
{
925-
static struct option builtin_sparse_checkout_options[] = {
927+
parse_opt_subcommand_fn *fn = NULL;
928+
struct option builtin_sparse_checkout_options[] = {
929+
OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
930+
OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
931+
OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
932+
OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
933+
OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
934+
OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
926935
OPT_END(),
927936
};
928937

929-
if (argc == 2 && !strcmp(argv[1], "-h"))
930-
usage_with_options(builtin_sparse_checkout_usage,
931-
builtin_sparse_checkout_options);
932-
933938
argc = parse_options(argc, argv, prefix,
934939
builtin_sparse_checkout_options,
935-
builtin_sparse_checkout_usage,
936-
PARSE_OPT_STOP_AT_NON_OPTION);
940+
builtin_sparse_checkout_usage, 0);
937941

938942
git_config(git_default_config, NULL);
939943

940944
prepare_repo_settings(the_repository);
941945
the_repository->settings.command_requires_full_index = 0;
942946

943-
if (argc > 0) {
944-
if (!strcmp(argv[0], "list"))
945-
return sparse_checkout_list(argc, argv);
946-
if (!strcmp(argv[0], "init"))
947-
return sparse_checkout_init(argc, argv);
948-
if (!strcmp(argv[0], "set"))
949-
return sparse_checkout_set(argc, argv, prefix);
950-
if (!strcmp(argv[0], "add"))
951-
return sparse_checkout_add(argc, argv, prefix);
952-
if (!strcmp(argv[0], "reapply"))
953-
return sparse_checkout_reapply(argc, argv);
954-
if (!strcmp(argv[0], "disable"))
955-
return sparse_checkout_disable(argc, argv);
956-
}
957-
958-
usage_with_options(builtin_sparse_checkout_usage,
959-
builtin_sparse_checkout_options);
947+
return fn(argc, argv, prefix);
960948
}

0 commit comments

Comments
 (0)