git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Taylor Blau <me@ttaylorr.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 5/8] builtin/config: track subcommands by action
Date: Wed, 6 Mar 2024 19:10:09 -0500	[thread overview]
Message-ID: <ZekF4aqq+NUf52go@nand.local> (raw)
In-Reply-To: <e39774d6495767c6505999bdc33110678139e347.1709724089.git.ps@pks.im>

On Wed, Mar 06, 2024 at 12:31:50PM +0100, Patrick Steinhardt wrote:
> ---
>  builtin/config.c | 207 +++++++++++++++++++++++------------------------
>  1 file changed, 99 insertions(+), 108 deletions(-)

This is definitely easier (for me, at least) to view with:

    $ git show --color-moved --color-moved-ws=ignore-all-space

which makes clearer that this change does not introduce or change any
existing behavior.

>  static struct option builtin_config_options[] = {
>  	OPT_GROUP(N_("Config file location")),
>  	OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
> @@ -851,20 +868,20 @@ static struct option builtin_config_options[] = {
>  	OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
>  	OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
>  	OPT_GROUP(N_("Action")),
> -	OPT_CMDMODE(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
> -	OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
> -	OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
> [...]

Got it, this whole hunk is changing "&actions" to "&action", and nothing
else.

> @@ -976,33 +993,43 @@ int cmd_config(int argc, const char **argv, const char *prefix)
>  		key_delim = '\n';
>  	}
>
> -	if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) {
> -		error(_("--get-color and variable type are incoherent"));
> -		usage_builtin_config();
> -	}
> -
> -	if (actions == 0)
> +	if (action == ACTION_NONE) {
>  		switch (argc) {
> -		case 1: actions = ACTION_GET; break;
> -		case 2: actions = ACTION_SET; break;
> -		case 3: actions = ACTION_SET_ALL; break;
> +		case 1: action = ACTION_GET; break;
> +		case 2: action = ACTION_SET; break;
> +		case 3: action = ACTION_SET_ALL; break;

Wondering aloud a little bit... is it safe to set us to "ACTION_SET",
for example, if we have exactly two arguments? On the one hand, it seems
like the answer is yes. But on the other hand, if we were invoked as:

    $ git config ste foo

We would get something like:

    $ git config ste foo
    error: key does not contain a section: ste

which is sensible. It would be nice to say something more along the
lines of "oops, you probably meant 'set' instead of 'ste'". I think you
could claim that the error on the user's part is one of:

  - (using the new style 'git config') misspelling "set" as "ste", and
    thus trying to invoke a non-existent sub-command

  - (using the old style) trying to set the key "ste", which does not
    contain a section name, and thus is not a valid configuration key

I have no idea which is more likely, and I think that there isn't a good
way to distinguish between the two at all. So I think the error message
is OK as-is, but let me know if you have other thoughts.

>  		default:
>  			usage_builtin_config();
>  		}
> +	}
> +	if (action <= ACTION_NONE || action >= ARRAY_SIZE(subcommands_by_action))
> +		BUG("invalid action %d", action);
> +	subcommand = subcommands_by_action[action];
> +
> +	if (type && (subcommand == cmd_config_get_color ||
> +		     subcommand == cmd_config_get_colorbool)) {
I don't think there's anything wrong with using the function-pointer
equality here to figure out which subcommand we're in, but I wonder if
it might be cleaner to write this as:

    if (type && (action == ACTION_GET_COLOR ||
                 action == ACTION_GET_COLORBOOL)) {
        ...

> -	if (actions == ACTION_LIST) {
> -		return cmd_config_list(argc, argv, prefix);
> -	} else if (actions == ACTION_EDIT) {
> -		return cmd_config_edit(argc, argv, prefix);
> -	} else if (actions == ACTION_SET) {
> -		return cmd_config_set(argc, argv, prefix);
> -	} else if (actions == ACTION_SET_ALL) {
> -		return cmd_config_set_all(argc, argv, prefix);
> -	} else if (actions == ACTION_ADD) {
> -		return cmd_config_add(argc, argv, prefix);
> -	} else if (actions == ACTION_REPLACE_ALL) {
> -		return cmd_config_replace_all(argc, argv, prefix);
> -	} else if (actions == ACTION_GET) {
> -		return cmd_config_get(argc, argv, prefix);
> -	} else if (actions == ACTION_GET_ALL) {
> -		return cmd_config_get_all(argc, argv, prefix);
> -	} else if (actions == ACTION_GET_REGEXP) {
> -		return cmd_config_get_regexp(argc, argv, prefix);
> -	} else if (actions == ACTION_GET_URLMATCH) {
> -		return cmd_config_get_urlmatch(argc, argv, prefix);
> -	} else if (actions == ACTION_UNSET) {
> -		return cmd_config_unset(argc, argv, prefix);
> -	} else if (actions == ACTION_UNSET_ALL) {
> -		return cmd_config_unset_all(argc, argv, prefix);
> -	} else if (actions == ACTION_RENAME_SECTION) {
> -		return cmd_config_rename_section(argc, argv, prefix);
> -	} else if (actions == ACTION_REMOVE_SECTION) {
> -		return cmd_config_remove_section(argc, argv, prefix);
> -	} else if (actions == ACTION_GET_COLOR) {
> -		return cmd_config_get_color(argc, argv, prefix);
> -	} else if (actions == ACTION_GET_COLORBOOL) {
> -		return cmd_config_get_colorbool(argc, argv, prefix);
> -	}
> -
> -	BUG("invalid action");
> +	return subcommand(argc, argv, prefix);

Very nice.

Thanks,
Taylor


  parent reply	other threads:[~2024-03-07  0:10 UTC|newest]

Thread overview: 113+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-06 11:31 [PATCH 0/8] builtin/config: introduce subcommands Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 1/8] builtin/config: move option array around Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 2/8] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 3/8] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-03-06 23:52   ` Taylor Blau
2024-03-07  7:02     ` Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 4/8] builtin/config: move modes into separate functions Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 5/8] builtin/config: track subcommands by action Patrick Steinhardt
2024-03-06 21:54   ` Jean-Noël AVILA
2024-03-07  6:37     ` Patrick Steinhardt
2024-03-07  0:10   ` Taylor Blau [this message]
2024-03-07  6:36     ` Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 6/8] builtin/config: introduce subcommands Patrick Steinhardt
2024-03-06 21:38   ` Karthik Nayak
2024-03-07  7:14     ` Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 7/8] t1300: exercise both old- and new-style modes Patrick Steinhardt
2024-03-06 11:32 ` [PATCH 8/8] Documentation/git-config: update to new-style syntax Patrick Steinhardt
2024-03-07  6:57   ` Eric Sunshine
2024-03-07  7:33     ` Patrick Steinhardt
2024-03-06 17:06 ` [PATCH 0/8] builtin/config: introduce subcommands Junio C Hamano
2024-03-06 23:46   ` Taylor Blau
2024-03-06 23:52     ` Junio C Hamano
2024-03-07  0:13       ` Taylor Blau
2024-03-07  0:31     ` Dragan Simic
2024-03-07  6:31     ` Patrick Steinhardt
2024-03-07 13:22       ` Junio C Hamano
2024-03-06 22:49 ` Kristoffer Haugsbakk
2024-03-11 23:19 ` [PATCH v2 00/13] " Patrick Steinhardt
2024-03-11 23:19   ` [PATCH v2 01/13] builtin/config: move option array around Patrick Steinhardt
2024-03-11 23:19   ` [PATCH v2 02/13] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 03/13] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 04/13] builtin/config: pull out function to handle config location Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 05/13] builtin/config: pull out function to handle `--null` Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 06/13] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-03-13  2:45     ` Eric Sunshine
2024-03-27  8:42       ` Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 07/13] builtin/config: introduce "get" subcommand Patrick Steinhardt
2024-03-13  3:11     ` Eric Sunshine
2024-03-27  8:42       ` Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 08/13] builtin/config: introduce "set" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 09/13] builtin/config: introduce "unset" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 10/13] builtin/config: introduce "rename-section" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 11/13] builtin/config: introduce "remove-section" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 12/13] builtin/config: introduce "edit" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 13/13] builtin/config: display subcommand help Patrick Steinhardt
2024-03-27  8:46 ` [PATCH v3 00/13] builtin/config: introduce subcommands Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 01/13] builtin/config: move option array around Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 02/13] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 03/13] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 04/13] builtin/config: pull out function to handle config location Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 05/13] builtin/config: pull out function to handle `--null` Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 06/13] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 07/13] builtin/config: introduce "get" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 08/13] builtin/config: introduce "set" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 09/13] builtin/config: introduce "unset" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 10/13] builtin/config: introduce "rename-section" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 11/13] builtin/config: introduce "remove-section" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 12/13] builtin/config: introduce "edit" subcommand Patrick Steinhardt
2024-03-27  8:47   ` [PATCH v3 13/13] builtin/config: display subcommand help Patrick Steinhardt
2024-03-27  8:53   ` [PATCH v3 00/13] builtin/config: introduce subcommands Eric Sunshine
2024-03-27  9:16     ` Patrick Steinhardt
2024-05-03  9:56 ` [PATCH v4 00/14] " Patrick Steinhardt
2024-05-03  9:56   ` [PATCH v4 01/14] config: clarify memory ownership when preparing comment strings Patrick Steinhardt
2024-05-03 10:13     ` Kristoffer Haugsbakk
2024-05-03  9:56   ` [PATCH v4 02/14] builtin/config: move option array around Patrick Steinhardt
2024-05-03  9:56   ` [PATCH v4 03/14] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-05-03 12:28     ` Karthik Nayak
2024-05-06  9:34       ` Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 04/14] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 05/14] builtin/config: pull out function to handle config location Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 06/14] builtin/config: pull out function to handle `--null` Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 07/14] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-05-03 13:08     ` Karthik Nayak
2024-05-03 13:13       ` rsbecker
2024-05-03 16:01         ` Junio C Hamano
2024-05-06  7:51           ` Patrick Steinhardt
2024-05-06 17:13             ` Junio C Hamano
2024-05-06 18:33               ` rsbecker
2024-05-06 18:45                 ` Dragan Simic
2024-05-07  6:20                 ` Kristoffer Haugsbakk
2024-05-06 21:33               ` Git 3.0? Junio C Hamano
2024-05-07  4:18                 ` Patrick Steinhardt
2024-05-07  4:02               ` [PATCH v4 07/14] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-05-06  7:58       ` Patrick Steinhardt
2024-05-06 11:26         ` Karthik Nayak
2024-05-03  9:57   ` [PATCH v4 08/14] builtin/config: introduce "get" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 09/14] builtin/config: introduce "set" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 10/14] builtin/config: introduce "unset" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 11/14] builtin/config: introduce "rename-section" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 12/14] builtin/config: introduce "remove-section" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 13/14] builtin/config: introduce "edit" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 14/14] builtin/config: display subcommand help Patrick Steinhardt
2024-05-03 13:36   ` [PATCH v4 00/14] builtin/config: introduce subcommands Dragan Simic
2024-05-03 16:09   ` Junio C Hamano
2024-05-06  8:55 ` [PATCH v5 " Patrick Steinhardt
2024-05-06  8:55   ` [PATCH v5 01/14] config: clarify memory ownership when preparing comment strings Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 02/14] builtin/config: move option array around Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 03/14] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 04/14] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 05/14] builtin/config: pull out function to handle config location Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 06/14] builtin/config: pull out function to handle `--null` Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 07/14] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 08/14] builtin/config: introduce "get" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 09/14] builtin/config: introduce "set" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 10/14] builtin/config: introduce "unset" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 11/14] builtin/config: introduce "rename-section" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 12/14] builtin/config: introduce "remove-section" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 13/14] builtin/config: introduce "edit" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 14/14] builtin/config: display subcommand help Patrick Steinhardt
2024-05-06 11:30   ` [PATCH v5 00/14] builtin/config: introduce subcommands Karthik Nayak
2024-05-06 20:21   ` Taylor Blau
2024-05-06 20:38     ` rsbecker
2024-05-07  4:07       ` Patrick Steinhardt

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=ZekF4aqq+NUf52go@nand.local \
    --to=me@ttaylorr.com \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    /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).