submodule--helper: use OPT_SUBCOMMAND() API

Have the cmd_submodule__helper() use the OPT_SUBCOMMAND() API introduced in fa83cc834da (parse-options: add support for parsing subcommands, 2022-08-19). This is only a marginal reduction in line count, but once we start unifying this with a yet-to-be-added "builtin/submodule.c" it'll be much easier to reason about those changes, as they'll both use OPT_SUBCOMMAND(). We don't need to worry about "argv[0]" being NULL in the die() because we'd have errored out in parse_options() as we're not using "PARSE_OPT_SUBCOMMAND_OPTIONAL". Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Ævar Arnfjörð Bjarmason committed Nov 8, 2022 at 15:10 UTC 69d94464e14de859ff56bcde7ebe0132201eceb9
2 files changed +39 -41
builtin/submodule--helper.c
+38 -40
@@ -3351,47 +3351,45 @@ cleanup:
3351 return ret;
3352 }
3353
3354 -#define SUPPORT_SUPER_PREFIX (1<<0)
3355 -
3356 -struct cmd_struct {
3357 - const char *cmd;
3358 - int (*fn)(int, const char **, const char *);
3359 - unsigned option;
3360 -};
3361 -
3362 -static struct cmd_struct commands[] = {
3363 - {"clone", module_clone, SUPPORT_SUPER_PREFIX},
3364 - {"add", module_add, 0},
3365 - {"update", module_update, SUPPORT_SUPER_PREFIX},
3366 - {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
3367 - {"init", module_init, 0},
3368 - {"status", module_status, SUPPORT_SUPER_PREFIX},
3369 - {"sync", module_sync, SUPPORT_SUPER_PREFIX},
3370 - {"deinit", module_deinit, 0},
3371 - {"summary", module_summary, 0},
3372 - {"push-check", push_check, 0},
3373 - {"absorbgitdirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3374 - {"set-url", module_set_url, 0},
3375 - {"set-branch", module_set_branch, 0},
3376 - {"create-branch", module_create_branch, 0},
3377 -};
3378 -
3354 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3355 {
3381 - int i;
3382 - if (argc < 2 || !strcmp(argv[1], "-h"))
3383 - usage("git submodule--helper <command>");
3384 -
3385 - for (i = 0; i < ARRAY_SIZE(commands); i++) {
3386 - if (!strcmp(argv[1], commands[i].cmd)) {
3387 - if (get_super_prefix() &&
3388 - !(commands[i].option & SUPPORT_SUPER_PREFIX))
3389 - die(_("%s doesn't support --super-prefix"),
3390 - commands[i].cmd);
3391 - return commands[i].fn(argc - 1, argv + 1, prefix);
3392 - }
3393 - }
3356 + const char *cmd = argv[0];
3357 + const char *subcmd;
3358 + parse_opt_subcommand_fn *fn = NULL;
3359 + const char *const usage[] = {
3360 + N_("git submodule--helper <command>"),
3361 + NULL
3362 + };
3363 + struct option options[] = {
3364 + OPT_SUBCOMMAND("clone", &fn, module_clone),
3365 + OPT_SUBCOMMAND("add", &fn, module_add),
3366 + OPT_SUBCOMMAND("update", &fn, module_update),
3367 + OPT_SUBCOMMAND("foreach", &fn, module_foreach),
3368 + OPT_SUBCOMMAND("init", &fn, module_init),
3369 + OPT_SUBCOMMAND("status", &fn, module_status),
3370 + OPT_SUBCOMMAND("sync", &fn, module_sync),
3371 + OPT_SUBCOMMAND("deinit", &fn, module_deinit),
3372 + OPT_SUBCOMMAND("summary", &fn, module_summary),
3373 + OPT_SUBCOMMAND("push-check", &fn, push_check),
3374 + OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
3375 + OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3376 + OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3377 + OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3378 + OPT_END()
3379 + };
3380 + argc = parse_options(argc, argv, prefix, options, usage, 0);
3381 + subcmd = argv[0];
3382 +
3383 + if (strcmp(subcmd, "clone") && strcmp(subcmd, "update") &&
3384 + strcmp(subcmd, "foreach") && strcmp(subcmd, "status") &&
3385 + strcmp(subcmd, "sync") && strcmp(subcmd, "absorbgitdirs") &&
3386 + get_super_prefix())
3387 + /*
3388 + * xstrfmt() rather than "%s %s" to keep the translated
3389 + * string identical to git.c's.
3390 + */
3391 + die(_("%s doesn't support --super-prefix"),
3392 + xstrfmt("'%s %s'", cmd, subcmd));
3393
3395 - die(_("'%s' is not a valid submodule--helper "
3396 - "subcommand"), argv[1]);
3394 + return fn(argc, argv, prefix);
3395 }
git.c
+1 -1
@@ -610,7 +610,7 @@ static struct cmd_struct commands[] = {
610 { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
611 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
612 { "stripspace", cmd_stripspace },
613 - { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
613 + { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX },
614 { "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE },
615 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
616 { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG },