Skip to content

Commit

Permalink
git_config_colorbool: refactor stdout_is_tty handling
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
peff authored and gitster committed Aug 18, 2011
1 parent f1c9626 commit e269eb7
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static int parse_branch_color_slot(const char *var, int ofs)
static int git_branch_config(const char *var, const char *value, void *cb)
{
if (!strcmp(var, "color.branch")) {
branch_use_color = git_config_colorbool(var, value, -1);
branch_use_color = git_config_colorbool(var, value);
return 0;
}
if (!prefixcmp(var, "color.branch.")) {
Expand Down
2 changes: 1 addition & 1 deletion builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
return 0;
}
if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
s->use_color = git_config_colorbool(k, v, -1);
s->use_color = git_config_colorbool(k, v);
return 0;
}
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
Expand Down
23 changes: 7 additions & 16 deletions builtin/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,24 +303,17 @@ static void get_color(const char *def_color)
fputs(parsed_color, stdout);
}

static int stdout_is_tty;
static int get_colorbool_found;
static int get_diff_color_found;
static int git_get_colorbool_config(const char *var, const char *value,
void *cb)
{
if (!strcmp(var, get_colorbool_slot)) {
get_colorbool_found =
git_config_colorbool(var, value, stdout_is_tty);
}
if (!strcmp(var, "diff.color")) {
get_diff_color_found =
git_config_colorbool(var, value, stdout_is_tty);
}
if (!strcmp(var, "color.ui")) {
git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
return 0;
}
if (!strcmp(var, get_colorbool_slot))
get_colorbool_found = git_config_colorbool(var, value);
else if (!strcmp(var, "diff.color"))
get_diff_color_found = git_config_colorbool(var, value);
else if (!strcmp(var, "color.ui"))
git_use_color_default = git_config_colorbool(var, value);
return 0;
}

Expand Down Expand Up @@ -510,9 +503,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
}
else if (actions == ACTION_GET_COLORBOOL) {
if (argc == 1)
stdout_is_tty = git_config_bool("command line", argv[0]);
else if (argc == 0)
stdout_is_tty = isatty(1);
color_stdout_is_tty = git_config_bool("command line", argv[0]);
return get_colorbool(argc != 0);
}

Expand Down
2 changes: 1 addition & 1 deletion builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ static int grep_config(const char *var, const char *value, void *cb)
}

if (!strcmp(var, "color.grep"))
opt->color = git_config_colorbool(var, value, -1);
opt->color = git_config_colorbool(var, value);
else if (!strcmp(var, "color.grep.context"))
color = opt->color_context;
else if (!strcmp(var, "color.grep.filename"))
Expand Down
2 changes: 1 addition & 1 deletion builtin/show-branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
}

if (!strcmp(var, "color.showbranch")) {
showbranch_use_color = git_config_colorbool(var, value, -1);
showbranch_use_color = git_config_colorbool(var, value);
return 0;
}

Expand Down
11 changes: 6 additions & 5 deletions color.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "color.h"

int git_use_color_default = 0;
int color_stdout_is_tty = -1;

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

int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
int git_config_colorbool(const char *var, const char *value)
{
if (value) {
if (!strcasecmp(value, "never"))
Expand All @@ -177,9 +178,9 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)

/* 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)) {
if (color_stdout_is_tty < 0)
color_stdout_is_tty = isatty(1);
if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
char *term = getenv("TERM");
if (term && strcmp(term, "dumb"))
return 1;
Expand All @@ -190,7 +191,7 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
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);
git_use_color_default = git_config_colorbool(var, value);
return 0;
}

Expand Down
8 changes: 7 additions & 1 deletion color.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ extern int git_use_color_default;
extern const char *column_colors_ansi[];
extern const int column_colors_ansi_max;

/*
* Generally the color code will lazily figure this out itself, but
* this provides a mechanism for callers to override autodetection.
*/
extern int color_stdout_is_tty;

/*
* Use this instead of git_default_config if you need the value of color.ui.
*/
int git_color_default_config(const char *var, const char *value, void *cb);

int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
int git_config_colorbool(const char *var, const char *value);
void color_parse(const char *value, const char *var, char *dst);
void color_parse_mem(const char *value, int len, const char *var, char *dst);
__attribute__((format (printf, 3, 4)))
Expand Down
4 changes: 2 additions & 2 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static int git_config_rename(const char *var, const char *value)
int git_diff_ui_config(const char *var, const char *value, void *cb)
{
if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
diff_use_color_default = git_config_colorbool(var, value, -1);
diff_use_color_default = git_config_colorbool(var, value);
return 0;
}
if (!strcmp(var, "diff.renames")) {
Expand Down Expand Up @@ -3373,7 +3373,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
else if (!strcmp(arg, "--color"))
options->use_color = 1;
else if (!prefixcmp(arg, "--color=")) {
int value = git_config_colorbool(NULL, arg+8, -1);
int value = git_config_colorbool(NULL, arg+8);
if (value == 0)
options->use_color = 0;
else if (value > 0)
Expand Down
2 changes: 1 addition & 1 deletion parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ int parse_opt_color_flag_cb(const struct option *opt, const char *arg,

if (!arg)
arg = unset ? "never" : (const char *)opt->defval;
value = git_config_colorbool(NULL, arg, -1);
value = git_config_colorbool(NULL, arg);
if (value < 0)
return opterror(opt,
"expects \"always\", \"auto\", or \"never\"", 0);
Expand Down

0 comments on commit e269eb7

Please sign in to comment.