Skip to content

Commit d0e08d6

Browse files
peffgitster
authored andcommitted
config: fix parsing of "git config --get-color some.key -1"
Most of git-config's command line options use OPT_BIT to choose an action, and then parse the non-option arguments in a context-dependent way. However, --get-color and --get-colorbool are unlike the rest of the options, in that they are OPT_STRING, taking the option name as a parameter. This generally works, because we then use the presence of those strings to set an action bit anyway. But it does mean that the option-parser will continue looking for options even after the key (because it is not a non-option; it is an argument to an option). And running: git config --get-color some.key -1 (to use "-1" as the default color spec) will barf, claiming that "-1" is not an option. Instead, we should treat --get-color and --get-colorbool as action bits, just like --add, --get, and all the other actions, and then check that the non-option arguments we got are sane. This fixes the weirdness above, and makes those two options like all the others. This "fixes" a test in t4026, which checked that feeding "-2" as a color should fail (it does fail, but prior to this patch, because parseopt barfed, not because we actually ever tried to parse the color). This also catches other errors, like: git config --get-color some.key black blue which previously silently ignored "blue" (and now will complain that you gave too many arguments). There are some possible regressions, though. We now disallow these, which currently do what you would expect: # specifying other options after the action git config --get-color some.key --file whatever # using long-arg syntax git config --get-color=some.key However, we have never advertised these in the documentation, and in fact they did not work in some older versions of git. The behavior was apparently switched as an accidental side effect of d64ec16 (git config: reorganize to use parseopt, 2009-02-21). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0edad17 commit d0e08d6

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

builtin/config.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ static struct option builtin_config_options[] = {
6969
OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
7070
OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
7171
OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
72-
OPT_STRING(0, "get-color", &get_color_slot, N_("slot"), N_("find the color configured: [default]")),
73-
OPT_STRING(0, "get-colorbool", &get_colorbool_slot, N_("slot"), N_("find the color setting: [stdout-is-tty]")),
72+
OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
73+
OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
7474
OPT_GROUP(N_("Type")),
7575
OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
7676
OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
@@ -302,8 +302,9 @@ static int git_get_color_config(const char *var, const char *value, void *cb)
302302
return 0;
303303
}
304304

305-
static void get_color(const char *def_color)
305+
static void get_color(const char *var, const char *def_color)
306306
{
307+
get_color_slot = var;
307308
get_color_found = 0;
308309
parsed_color[0] = '\0';
309310
git_config_with_options(git_get_color_config, NULL,
@@ -330,8 +331,9 @@ static int git_get_colorbool_config(const char *var, const char *value,
330331
return 0;
331332
}
332333

333-
static int get_colorbool(int print)
334+
static int get_colorbool(const char *var, int print)
334335
{
336+
get_colorbool_slot = var;
335337
get_colorbool_found = -1;
336338
get_diff_color_found = -1;
337339
get_color_ui_found = -1;
@@ -515,12 +517,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
515517
usage_with_options(builtin_config_usage, builtin_config_options);
516518
}
517519

518-
if (get_color_slot)
519-
actions |= ACTION_GET_COLOR;
520-
if (get_colorbool_slot)
521-
actions |= ACTION_GET_COLORBOOL;
522-
523-
if ((get_color_slot || get_colorbool_slot) && types) {
520+
if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
524521
error("--get-color and variable type are incoherent");
525522
usage_with_options(builtin_config_usage, builtin_config_options);
526523
}
@@ -655,12 +652,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
655652
die("No such section!");
656653
}
657654
else if (actions == ACTION_GET_COLOR) {
658-
get_color(argv[0]);
655+
check_argc(argc, 1, 2);
656+
get_color(argv[0], argv[1]);
659657
}
660658
else if (actions == ACTION_GET_COLORBOOL) {
661-
if (argc == 1)
662-
color_stdout_is_tty = git_config_bool("command line", argv[0]);
663-
return get_colorbool(argc != 0);
659+
check_argc(argc, 1, 2);
660+
if (argc == 2)
661+
color_stdout_is_tty = git_config_bool("command line", argv[1]);
662+
return get_colorbool(argv[0], argc == 2);
664663
}
665664

666665
return 0;

0 commit comments

Comments
 (0)