X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=color.c;h=fc0b72ad59b13e4bd86372e5e81b4f400c99d26e;hb=d88593fa0412a4f0a363860d879e97635c118616;hp=124ba331c7f798b9b922fc8482dbc215202b99a6;hpb=b52e985a4f824128d01220473b04057a4876060d;p=git.git diff --git a/color.c b/color.c index 124ba331c..fc0b72ad5 100644 --- a/color.c +++ b/color.c @@ -3,6 +3,8 @@ #define COLOR_RESET "\033[m" +int git_use_color_default = 0; + static int parse_color(const char *name, int len) { static const char * const color_names[] = { @@ -17,7 +19,7 @@ static int parse_color(const char *name, int len) return i - 1; } i = strtol(name, &end, 10); - if (*name && !*end && i >= -1 && i <= 255) + if (end - name == len && i >= -1 && i <= 255) return i; return -2; } @@ -116,23 +118,41 @@ bad: die("bad config value '%s' for variable '%s'", value, var); } -int git_config_colorbool(const char *var, const char *value) +int git_config_colorbool(const char *var, const char *value, int stdout_is_tty) { - if (!value) - return 1; - if (!strcasecmp(value, "auto")) { - if (isatty(1) || (pager_in_use && pager_use_color)) { - char *term = getenv("TERM"); - if (term && strcmp(term, "dumb")) - return 1; - } + if (value) { + if (!strcasecmp(value, "never")) + return 0; + if (!strcasecmp(value, "always")) + return 1; + if (!strcasecmp(value, "auto")) + goto auto_color; + } + + /* Missing or explicit false to turn off colorization */ + if (!git_config_bool(var, value)) return 0; + + /* any normal truth value defaults to 'auto' */ + auto_color: + if (stdout_is_tty < 0) + stdout_is_tty = isatty(1); + if (stdout_is_tty || (pager_in_use() && pager_use_color)) { + char *term = getenv("TERM"); + if (term && strcmp(term, "dumb")) + return 1; } - if (!strcasecmp(value, "never")) + return 0; +} + +int git_color_default_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "color.ui")) { + git_use_color_default = git_config_colorbool(var, value, -1); return 0; - if (!strcasecmp(value, "always")) - return 1; - return git_config_bool(var, value); + } + + return git_default_config(var, value, cb); } static int color_vfprintf(FILE *fp, const char *color, const char *fmt,