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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 17, 2011 at 22:03 UTC e269eb7946d0a4ba6a4e175133b5479446ac04a5
9 files changed +27 -29
builtin/branch.c
+1 -1
@@ -71,7 +71,7 @@ static int parse_branch_color_slot(const char *var, int ofs)
71 static int git_branch_config(const char *var, const char *value, void *cb)
72 {
73 if (!strcmp(var, "color.branch")) {
74 - branch_use_color = git_config_colorbool(var, value, -1);
74 + branch_use_color = git_config_colorbool(var, value);
75 return 0;
76 }
77 if (!prefixcmp(var, "color.branch.")) {
builtin/commit.c
+1 -1
@@ -1144,7 +1144,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
1144 return 0;
1145 }
1146 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);
1148 return 0;
1149 }
1150 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
builtin/config.c
+7 -16
@@ -303,24 +303,17 @@ static void get_color(const char *def_color)
303 fputs(parsed_color, stdout);
304 }
305
306 -static int stdout_is_tty;
306 static int get_colorbool_found;
307 static int get_diff_color_found;
308 static int git_get_colorbool_config(const char *var, const char *value,
309 void *cb)
310 {
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);
317 return 0;
318 }
319
@@ -510,9 +503,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
503 }
504 else if (actions == ACTION_GET_COLORBOOL) {
505 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]);
507 return get_colorbool(argc != 0);
508 }
509
builtin/grep.c
+1 -1
@@ -316,7 +316,7 @@ static int grep_config(const char *var, const char *value, void *cb)
316 }
317
318 if (!strcmp(var, "color.grep"))
319 - opt->color = git_config_colorbool(var, value, -1);
319 + opt->color = git_config_colorbool(var, value);
320 else if (!strcmp(var, "color.grep.context"))
321 color = opt->color_context;
322 else if (!strcmp(var, "color.grep.filename"))
builtin/show-branch.c
+1 -1
@@ -573,7 +573,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
573 }
574
575 if (!strcmp(var, "color.showbranch")) {
576 - showbranch_use_color = git_config_colorbool(var, value, -1);
576 + showbranch_use_color = git_config_colorbool(var, value);
577 return 0;
578 }
579
color.c
+6 -5
@@ -2,6 +2,7 @@
2 #include "color.h"
3
4 int git_use_color_default = 0;
5 +int color_stdout_is_tty = -1;
6
7 /*
8 * The list of available column colors.
@@ -157,7 +158,7 @@ bad:
158 die("bad color value '%.*s' for variable '%s'", value_len, value, var);
159 }
160
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)
162 {
163 if (value) {
164 if (!strcasecmp(value, "never"))
@@ -177,9 +178,9 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
178
179 /* any normal truth value defaults to 'auto' */
180 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)) {
184 char *term = getenv("TERM");
185 if (term && strcmp(term, "dumb"))
186 return 1;
@@ -190,7 +191,7 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
191 int git_color_default_config(const char *var, const char *value, void *cb)
192 {
193 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);
195 return 0;
196 }
197
color.h
+7 -1
@@ -57,12 +57,18 @@ extern int git_use_color_default;
57 extern const char *column_colors_ansi[];
58 extern const int column_colors_ansi_max;
59
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 +
66 /*
67 * Use this instead of git_default_config if you need the value of color.ui.
68 */
69 int git_color_default_config(const char *var, const char *value, void *cb);
70
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);
72 void color_parse(const char *value, const char *var, char *dst);
73 void color_parse_mem(const char *value, int len, const char *var, char *dst);
74 __attribute__((format (printf, 3, 4)))
diff.c
+2 -2
@@ -137,7 +137,7 @@ static int git_config_rename(const char *var, const char *value)
137 int git_diff_ui_config(const char *var, const char *value, void *cb)
138 {
139 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);
141 return 0;
142 }
143 if (!strcmp(var, "diff.renames")) {
@@ -3373,7 +3373,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3373 else if (!strcmp(arg, "--color"))
3374 options->use_color = 1;
3375 else if (!prefixcmp(arg, "--color=")) {
3376 - int value = git_config_colorbool(NULL, arg+8, -1);
3376 + int value = git_config_colorbool(NULL, arg+8);
3377 if (value == 0)
3378 options->use_color = 0;
3379 else if (value > 0)
parse-options.c
+1 -1
@@ -620,7 +620,7 @@ int parse_opt_color_flag_cb(const struct option *opt, const char *arg,
620
621 if (!arg)
622 arg = unset ? "never" : (const char *)opt->defval;
623 - value = git_config_colorbool(NULL, arg, -1);
623 + value = git_config_colorbool(NULL, arg);
624 if (value < 0)
625 return opterror(opt,
626 "expects \"always\", \"auto\", or \"never\"", 0);