git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Glen Choo <chooglen@google.com>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>, git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Emily Shaffer" <emilyshaffer@google.com>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: Re: [PATCH 8/8] submodule--helper: use OPT_SUBCOMMAND() API
Date: Thu, 03 Nov 2022 16:31:07 -0700	[thread overview]
Message-ID: <kl6l7d0bobt0.fsf@chooglen-macbookpro.roam.corp.google.com> (raw)
In-Reply-To: <patch-8.8-105853cd358-20221102T074148Z-avarab@gmail.com>

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> 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().

Even if nothing else, this is a nice standardization change :)

> 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>
> ---
>  builtin/submodule--helper.c | 78 ++++++++++++++++++-------------------
>  git.c                       |  2 +-
>  2 files changed, 39 insertions(+), 41 deletions(-)
>
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 2012ad31d7f..0bc25dcf5ae 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -3350,47 +3350,45 @@ static int module_add(int argc, const char **argv, const char *prefix)
>  	return ret;
>  }
>  
> -#define SUPPORT_SUPER_PREFIX (1<<0)
> -
> -struct cmd_struct {
> -	const char *cmd;
> -	int (*fn)(int, const char **, const char *);
> -	unsigned option;
> -};
> -
> -static struct cmd_struct commands[] = {
> -	{"clone", module_clone, SUPPORT_SUPER_PREFIX},
> -	{"add", module_add, 0},
> -	{"update", module_update, SUPPORT_SUPER_PREFIX},
> -	{"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
> -	{"init", module_init, 0},
> -	{"status", module_status, SUPPORT_SUPER_PREFIX},
> -	{"sync", module_sync, SUPPORT_SUPER_PREFIX},
> -	{"deinit", module_deinit, 0},
> -	{"summary", module_summary, 0},
> -	{"push-check", push_check, 0},
> -	{"absorbgitdirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
> -	{"set-url", module_set_url, 0},
> -	{"set-branch", module_set_branch, 0},
> -	{"create-branch", module_create_branch, 0},
> -};
> -
>  int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
>  {
> -	int i;
> -	if (argc < 2 || !strcmp(argv[1], "-h"))
> -		usage("git submodule--helper <command>");
> -
> -	for (i = 0; i < ARRAY_SIZE(commands); i++) {
> -		if (!strcmp(argv[1], commands[i].cmd)) {
> -			if (get_super_prefix() &&
> -			    !(commands[i].option & SUPPORT_SUPER_PREFIX))
> -				die(_("%s doesn't support --super-prefix"),
> -				    commands[i].cmd);
> -			return commands[i].fn(argc - 1, argv + 1, prefix);
> -		}
> -	}
> +	const char *cmd = argv[0];
> +	const char *subcmd;
> +	parse_opt_subcommand_fn *fn = NULL;
> +	const char *const usage[] = {
> +		N_("git submodule--helper <command>"),
> +		NULL
> +	};
> +	struct option options[] = {
> +		OPT_SUBCOMMAND("clone", &fn, module_clone),
> +		OPT_SUBCOMMAND("add", &fn, module_add),
> +		OPT_SUBCOMMAND("update", &fn, module_update),
> +		OPT_SUBCOMMAND("foreach", &fn, module_foreach),
> +		OPT_SUBCOMMAND("init", &fn, module_init),
> +		OPT_SUBCOMMAND("status", &fn, module_status),
> +		OPT_SUBCOMMAND("sync", &fn, module_sync),
> +		OPT_SUBCOMMAND("deinit", &fn, module_deinit),
> +		OPT_SUBCOMMAND("summary", &fn, module_summary),
> +		OPT_SUBCOMMAND("push-check", &fn, push_check),
> +		OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
> +		OPT_SUBCOMMAND("set-url", &fn, module_set_url),
> +		OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
> +		OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
> +		OPT_END()
> +	};
> +	argc = parse_options(argc, argv, prefix, options, usage, 0);
> +	subcmd = argv[0];
> +
> +	if (strcmp(subcmd, "clone") && strcmp(subcmd, "update") &&
> +	    strcmp(subcmd, "foreach") && strcmp(subcmd, "status") &&
> +	    strcmp(subcmd, "sync") && strcmp(subcmd, "absorbgitdirs") &&
> +	    get_super_prefix())
> +		/*
> +		 * xstrfmt() rather than "%s %s" to keep the translated
> +		 * string identical to git.c's.
> +		 */
> +		die(_("%s doesn't support --super-prefix"),
> +		    xstrfmt("'%s %s'", cmd, subcmd));

FYI I'm preparing a series to get rid of the SUPPORT_SUPER_PREFIX checks
in both the top level and in submodule--helper (i.e. revisiting my
complaints from [1]), but I haven't sent it out yet because I haven't
found the right way to motivate that change. Feel free to chime in on
that series when it comes out.

[1] https://lore.kernel.org/git/20220630221147.45689-5-chooglen@google.com/

> -	die(_("'%s' is not a valid submodule--helper "
> -	      "subcommand"), argv[1]);
> +	return fn(argc, argv, prefix);
>  }
> diff --git a/git.c b/git.c
> index ee7758dcb0e..fb69e605912 100644
> --- a/git.c
> +++ b/git.c
> @@ -610,7 +610,7 @@ static struct cmd_struct commands[] = {
>  	{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
>  	{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
>  	{ "stripspace", cmd_stripspace },
> -	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
> +	{ "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX },
>  	{ "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE },
>  	{ "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
>  	{ "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG },
> -- 
> 2.38.0.1280.g8136eb6fab2

  reply	other threads:[~2022-11-03 23:31 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-02  7:53 [PATCH 0/8] submodule: tests, cleanup to prepare for built-in Ævar Arnfjörð Bjarmason
2022-11-02  7:53 ` [PATCH 1/8] submodule--helper: move "config" to a test-tool Ævar Arnfjörð Bjarmason
2022-11-03 22:09   ` Glen Choo
2022-11-02  7:53 ` [PATCH 2/8] submodule tests: add tests for top-level flag output Ævar Arnfjörð Bjarmason
2022-11-03 22:30   ` Glen Choo
2022-11-02  7:54 ` [PATCH 3/8] submodule tests: test for a "foreach" blind-spot Ævar Arnfjörð Bjarmason
2022-11-02  7:54 ` [PATCH 4/8] submodule.c: refactor recursive block out of absorb function Ævar Arnfjörð Bjarmason
2022-11-02  7:54 ` [PATCH 5/8] submodule API & "absorbgitdirs": remove "----recursive" option Ævar Arnfjörð Bjarmason
2022-11-03 22:53   ` Glen Choo
2022-11-04  1:42     ` Ævar Arnfjörð Bjarmason
2022-11-04 13:07       ` FW: " Simpson, Phyllis
2022-11-04 17:08       ` Glen Choo
2022-11-02  7:54 ` [PATCH 6/8] submodule--helper: remove --prefix from "absorbgitdirs" Ævar Arnfjörð Bjarmason
2022-11-02  7:54 ` [PATCH 7/8] submodule--helper: drop "update --prefix <pfx>" for "-C <pfx> update" Ævar Arnfjörð Bjarmason
2022-11-02  7:54 ` [PATCH 8/8] submodule--helper: use OPT_SUBCOMMAND() API Ævar Arnfjörð Bjarmason
2022-11-03 23:31   ` Glen Choo [this message]
2022-11-04  1:22     ` Ævar Arnfjörð Bjarmason
2022-11-04 17:02       ` Glen Choo
2022-11-05 14:23         ` Ævar Arnfjörð Bjarmason
2022-11-07 17:16           ` Glen Choo
2022-11-04 17:10 ` [PATCH 0/8] submodule: tests, cleanup to prepare for built-in Glen Choo
2022-11-04 19:07   ` Taylor Blau
2022-11-08 14:10 ` [PATCH v2 0/9] " Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 1/9] submodule--helper: move "config" to a test-tool Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 2/9] submodule tests: add tests for top-level flag output Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 3/9] submodule--helper: fix a memory leak in "status" Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 4/9] submodule tests: test for a "foreach" blind-spot Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 5/9] submodule.c: refactor recursive block out of absorb function Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 6/9] submodule API & "absorbgitdirs": remove "----recursive" option Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 7/9] submodule--helper: remove --prefix from "absorbgitdirs" Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 8/9] submodule--helper: drop "update --prefix <pfx>" for "-C <pfx> update" Ævar Arnfjörð Bjarmason
2022-11-08 14:10   ` [PATCH v2 9/9] submodule--helper: use OPT_SUBCOMMAND() API Ævar Arnfjörð Bjarmason
2022-11-08 18:30   ` [PATCH v2 0/9] submodule: tests, cleanup to prepare for built-in Glen Choo
2022-11-08 18:34     ` Ævar Arnfjörð Bjarmason
2022-11-08 19:20       ` Glen Choo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=kl6l7d0bobt0.fsf@chooglen-macbookpro.roam.corp.google.com \
    --to=chooglen@google.com \
    --cc=avarab@gmail.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).