Skip to content

Commit 5d509d5

Browse files
committed
Merge branch 'jk/colors-fix' into maint
"git config --get-color" did not parse its command line arguments carefully. * jk/colors-fix: t4026: test "normal" color config: fix parsing of "git config --get-color some.key -1" docs: describe ANSI 256-color mode
2 parents 447c39a + cb35722 commit 5d509d5

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,10 @@ accepted are `normal`, `black`, `red`, `green`, `yellow`, `blue`,
850850
`blink` and `reverse`. The first color given is the foreground; the
851851
second is the background. The position of the attribute, if any,
852852
doesn't matter.
853+
+
854+
Colors (foreground and background) may also be given as numbers between
855+
0 and 255; these use ANSI 256-color mode (but note that not all
856+
terminals may support this).
853857

854858
color.diff::
855859
Whether to use ANSI escape sequences to add color to patches.

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),
@@ -303,8 +303,9 @@ static int git_get_color_config(const char *var, const char *value, void *cb)
303303
return 0;
304304
}
305305

306-
static void get_color(const char *def_color)
306+
static void get_color(const char *var, const char *def_color)
307307
{
308+
get_color_slot = var;
308309
get_color_found = 0;
309310
parsed_color[0] = '\0';
310311
git_config_with_options(git_get_color_config, NULL,
@@ -333,8 +334,9 @@ static int git_get_colorbool_config(const char *var, const char *value,
333334
return 0;
334335
}
335336

336-
static int get_colorbool(int print)
337+
static int get_colorbool(const char *var, int print)
337338
{
339+
get_colorbool_slot = var;
338340
get_colorbool_found = -1;
339341
get_diff_color_found = -1;
340342
get_color_ui_found = -1;
@@ -532,12 +534,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
532534
usage_with_options(builtin_config_usage, builtin_config_options);
533535
}
534536

535-
if (get_color_slot)
536-
actions |= ACTION_GET_COLOR;
537-
if (get_colorbool_slot)
538-
actions |= ACTION_GET_COLORBOOL;
539-
540-
if ((get_color_slot || get_colorbool_slot) && types) {
537+
if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
541538
error("--get-color and variable type are incoherent");
542539
usage_with_options(builtin_config_usage, builtin_config_options);
543540
}
@@ -683,12 +680,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
683680
die("No such section!");
684681
}
685682
else if (actions == ACTION_GET_COLOR) {
686-
get_color(argv[0]);
683+
check_argc(argc, 1, 2);
684+
get_color(argv[0], argv[1]);
687685
}
688686
else if (actions == ACTION_GET_COLORBOOL) {
689-
if (argc == 1)
690-
color_stdout_is_tty = git_config_bool("command line", argv[0]);
691-
return get_colorbool(argc != 0);
687+
check_argc(argc, 1, 2);
688+
if (argc == 2)
689+
color_stdout_is_tty = git_config_bool("command line", argv[1]);
690+
return get_colorbool(argv[0], argc == 2);
692691
}
693692

694693
return 0;

t/t4026-color.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ test_expect_success '256 colors' '
5353
color "254 bold 255" "[1;38;5;254;48;5;255m"
5454
'
5555

56+
test_expect_success '"normal" yields no color at all"' '
57+
color "normal black" "[40m"
58+
'
59+
60+
test_expect_success '-1 is a synonym for "normal"' '
61+
color "-1 black" "[40m"
62+
'
63+
5664
test_expect_success 'color too small' '
5765
invalid_color "-2"
5866
'

0 commit comments

Comments
 (0)