Skip to content

Commit daa3325

Browse files
pks-tgitster
authored andcommitted
builtin/config: use OPT_CMDMODE() to specify modes
The git-config(1) command has various different modes which are accessible via e.g. `--get-urlmatch` or `--unset-all`. These modes are declared with `OPT_BIT()`, which causes two minor issues: - The respective modes also have a negated form `--no-get-urlmatch`, which is unintended. - We have to manually handle exclusiveness of the modes. Switch these options to instead use `OPT_CMDMODE()`, which is made exactly for this usecase. Remove the now-unneeded check that only a single mode is given, which is now handled by the parse-options interface. While at it, format optional placeholders for arguments to conform to our style guidelines by using `[<placeholder>]`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8415507 commit daa3325

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

builtin/config.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -632,20 +632,20 @@ static struct option builtin_config_options[] = {
632632
OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
633633
OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
634634
OPT_GROUP(N_("Action")),
635-
OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET),
636-
OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL),
637-
OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP),
638-
OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
639-
OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL),
640-
OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
641-
OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET),
642-
OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL),
643-
OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
644-
OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
645-
OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
646-
OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
647-
OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
648-
OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
635+
OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET),
636+
OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL),
637+
OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP),
638+
OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
639+
OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL),
640+
OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
641+
OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET),
642+
OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL),
643+
OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
644+
OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
645+
OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST),
646+
OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
647+
OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
648+
OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
649649
OPT_GROUP(N_("Type")),
650650
OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
651651
OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
@@ -769,10 +769,6 @@ int cmd_config(int argc, const char **argv, const char *prefix)
769769
usage_builtin_config();
770770
}
771771

772-
if (HAS_MULTI_BITS(actions)) {
773-
error(_("only one action at a time"));
774-
usage_builtin_config();
775-
}
776772
if (actions == 0)
777773
switch (argc) {
778774
case 1: actions = ACTION_GET; break;

t/t1300-config.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,4 +2738,17 @@ test_expect_success 'includeIf.hasconfig:remote.*.url forbids remote url in such
27382738
grep "fatal: remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url" err
27392739
'
27402740

2741+
test_expect_success 'negated mode causes failure' '
2742+
test_must_fail git config --no-get 2>err &&
2743+
grep "unknown option \`no-get${SQ}" err
2744+
'
2745+
2746+
test_expect_success 'specifying multiple modes causes failure' '
2747+
cat >expect <<-EOF &&
2748+
error: options ${SQ}--get-all${SQ} and ${SQ}--get${SQ} cannot be used together
2749+
EOF
2750+
test_must_fail git config --get --get-all 2>err &&
2751+
test_cmp expect err
2752+
'
2753+
27412754
test_done

0 commit comments

Comments
 (0)