Skip to content

Commit e269eb7

Browse files
peffgitster
authored andcommitted
git_config_colorbool: refactor stdout_is_tty handling
Usually this function figures out for itself whether stdout is a tty. However, it has an extra parameter just to allow git-config to override the auto-detection for its --get-colorbool option. Instead of an extra parameter, let's just use a global variable. This makes calling easier in the common case, and will make refactoring the colorbool code much simpler. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f1c9626 commit e269eb7

File tree

9 files changed

+27
-29
lines changed

9 files changed

+27
-29
lines changed

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static int parse_branch_color_slot(const char *var, int ofs)
7171
static int git_branch_config(const char *var, const char *value, void *cb)
7272
{
7373
if (!strcmp(var, "color.branch")) {
74-
branch_use_color = git_config_colorbool(var, value, -1);
74+
branch_use_color = git_config_colorbool(var, value);
7575
return 0;
7676
}
7777
if (!prefixcmp(var, "color.branch.")) {

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
11441144
return 0;
11451145
}
11461146
if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1147-
s->use_color = git_config_colorbool(k, v, -1);
1147+
s->use_color = git_config_colorbool(k, v);
11481148
return 0;
11491149
}
11501150
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {

builtin/config.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -303,24 +303,17 @@ static void get_color(const char *def_color)
303303
fputs(parsed_color, stdout);
304304
}
305305

306-
static int stdout_is_tty;
307306
static int get_colorbool_found;
308307
static int get_diff_color_found;
309308
static int git_get_colorbool_config(const char *var, const char *value,
310309
void *cb)
311310
{
312-
if (!strcmp(var, get_colorbool_slot)) {
313-
get_colorbool_found =
314-
git_config_colorbool(var, value, stdout_is_tty);
315-
}
316-
if (!strcmp(var, "diff.color")) {
317-
get_diff_color_found =
318-
git_config_colorbool(var, value, stdout_is_tty);
319-
}
320-
if (!strcmp(var, "color.ui")) {
321-
git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
322-
return 0;
323-
}
311+
if (!strcmp(var, get_colorbool_slot))
312+
get_colorbool_found = git_config_colorbool(var, value);
313+
else if (!strcmp(var, "diff.color"))
314+
get_diff_color_found = git_config_colorbool(var, value);
315+
else if (!strcmp(var, "color.ui"))
316+
git_use_color_default = git_config_colorbool(var, value);
324317
return 0;
325318
}
326319

@@ -510,9 +503,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
510503
}
511504
else if (actions == ACTION_GET_COLORBOOL) {
512505
if (argc == 1)
513-
stdout_is_tty = git_config_bool("command line", argv[0]);
514-
else if (argc == 0)
515-
stdout_is_tty = isatty(1);
506+
color_stdout_is_tty = git_config_bool("command line", argv[0]);
516507
return get_colorbool(argc != 0);
517508
}
518509

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ static int grep_config(const char *var, const char *value, void *cb)
316316
}
317317

318318
if (!strcmp(var, "color.grep"))
319-
opt->color = git_config_colorbool(var, value, -1);
319+
opt->color = git_config_colorbool(var, value);
320320
else if (!strcmp(var, "color.grep.context"))
321321
color = opt->color_context;
322322
else if (!strcmp(var, "color.grep.filename"))

builtin/show-branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
573573
}
574574

575575
if (!strcmp(var, "color.showbranch")) {
576-
showbranch_use_color = git_config_colorbool(var, value, -1);
576+
showbranch_use_color = git_config_colorbool(var, value);
577577
return 0;
578578
}
579579

color.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "color.h"
33

44
int git_use_color_default = 0;
5+
int color_stdout_is_tty = -1;
56

67
/*
78
* The list of available column colors.
@@ -157,7 +158,7 @@ void color_parse_mem(const char *value, int value_len, const char *var,
157158
die("bad color value '%.*s' for variable '%s'", value_len, value, var);
158159
}
159160

160-
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
161+
int git_config_colorbool(const char *var, const char *value)
161162
{
162163
if (value) {
163164
if (!strcasecmp(value, "never"))
@@ -177,9 +178,9 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
177178

178179
/* any normal truth value defaults to 'auto' */
179180
auto_color:
180-
if (stdout_is_tty < 0)
181-
stdout_is_tty = isatty(1);
182-
if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
181+
if (color_stdout_is_tty < 0)
182+
color_stdout_is_tty = isatty(1);
183+
if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
183184
char *term = getenv("TERM");
184185
if (term && strcmp(term, "dumb"))
185186
return 1;
@@ -190,7 +191,7 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
190191
int git_color_default_config(const char *var, const char *value, void *cb)
191192
{
192193
if (!strcmp(var, "color.ui")) {
193-
git_use_color_default = git_config_colorbool(var, value, -1);
194+
git_use_color_default = git_config_colorbool(var, value);
194195
return 0;
195196
}
196197

color.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ extern int git_use_color_default;
5757
extern const char *column_colors_ansi[];
5858
extern const int column_colors_ansi_max;
5959

60+
/*
61+
* Generally the color code will lazily figure this out itself, but
62+
* this provides a mechanism for callers to override autodetection.
63+
*/
64+
extern int color_stdout_is_tty;
65+
6066
/*
6167
* Use this instead of git_default_config if you need the value of color.ui.
6268
*/
6369
int git_color_default_config(const char *var, const char *value, void *cb);
6470

65-
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
71+
int git_config_colorbool(const char *var, const char *value);
6672
void color_parse(const char *value, const char *var, char *dst);
6773
void color_parse_mem(const char *value, int len, const char *var, char *dst);
6874
__attribute__((format (printf, 3, 4)))

diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static int git_config_rename(const char *var, const char *value)
137137
int git_diff_ui_config(const char *var, const char *value, void *cb)
138138
{
139139
if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
140-
diff_use_color_default = git_config_colorbool(var, value, -1);
140+
diff_use_color_default = git_config_colorbool(var, value);
141141
return 0;
142142
}
143143
if (!strcmp(var, "diff.renames")) {
@@ -3373,7 +3373,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
33733373
else if (!strcmp(arg, "--color"))
33743374
options->use_color = 1;
33753375
else if (!prefixcmp(arg, "--color=")) {
3376-
int value = git_config_colorbool(NULL, arg+8, -1);
3376+
int value = git_config_colorbool(NULL, arg+8);
33773377
if (value == 0)
33783378
options->use_color = 0;
33793379
else if (value > 0)

parse-options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
620620

621621
if (!arg)
622622
arg = unset ? "never" : (const char *)opt->defval;
623-
value = git_config_colorbool(NULL, arg, -1);
623+
value = git_config_colorbool(NULL, arg);
624624
if (value < 0)
625625
return opterror(opt,
626626
"expects \"always\", \"auto\", or \"never\"", 0);

0 commit comments

Comments
 (0)