want_color: automatically fallback to color.ui

All of the "do we want color" flags default to -1 to
indicate that we don't have any color configured. This value
is handled in one of two ways:

  1. In porcelain, we check early on whether the value is
     still -1 after reading the config, and set it to the
     value of color.ui (which defaults to 0).

  2. In plumbing, it stays untouched as -1, and want_color
     defaults it to off.

This works fine, but means that every porcelain has to check
and reassign its color flag. Now that want_color gives us a
place to put this check in a single spot, we can do that,
simplifying the calling code.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2011-08-17 22:05:35 -07:00 committed by Junio C Hamano
parent 3e1dd17a89
commit c9bfb95348
9 changed files with 6 additions and 44 deletions

View File

@ -673,9 +673,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
git_config(git_branch_config, NULL); git_config(git_branch_config, NULL);
if (branch_use_color == -1)
branch_use_color = git_use_color_default;
track = git_branch_track; track = git_branch_track;
head = resolve_ref("HEAD", head_sha1, 0, NULL); head = resolve_ref("HEAD", head_sha1, 0, NULL);

View File

@ -1237,10 +1237,6 @@ int cmd_status(int argc, const char **argv, const char *prefix)
if (s.relative_paths) if (s.relative_paths)
s.prefix = prefix; s.prefix = prefix;
if (s.use_color == -1)
s.use_color = git_use_color_default;
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
switch (status_format) { switch (status_format) {
case STATUS_FORMAT_SHORT: case STATUS_FORMAT_SHORT:
@ -1394,15 +1390,10 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
git_config(git_commit_config, &s); git_config(git_commit_config, &s);
determine_whence(&s); determine_whence(&s);
if (s.use_color == -1)
s.use_color = git_use_color_default;
argc = parse_and_validate_options(argc, argv, builtin_commit_usage, argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
prefix, &s); prefix, &s);
if (dry_run) { if (dry_run)
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
return dry_run_commit(argc, argv, prefix, &s); return dry_run_commit(argc, argv, prefix, &s);
}
index_file = prepare_index(argc, argv, prefix, 0); index_file = prepare_index(argc, argv, prefix, 0);
/* Set up everything for writing the commit object. This includes /* Set up everything for writing the commit object. This includes

View File

@ -277,9 +277,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
gitmodules_config(); gitmodules_config();
git_config(git_diff_ui_config, NULL); git_config(git_diff_ui_config, NULL);
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
init_revisions(&rev, prefix); init_revisions(&rev, prefix);
/* If this is a no-index diff, just run it and exit there. */ /* If this is a no-index diff, just run it and exit there. */

View File

@ -883,8 +883,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
strcpy(opt.color_sep, GIT_COLOR_CYAN); strcpy(opt.color_sep, GIT_COLOR_CYAN);
opt.color = -1; opt.color = -1;
git_config(grep_config, &opt); git_config(grep_config, &opt);
if (opt.color == -1)
opt.color = git_use_color_default;
/* /*
* If there is no -- then the paths must exist in the working * If there is no -- then the paths must exist in the working

View File

@ -359,9 +359,6 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
git_config(git_log_config, NULL); git_config(git_log_config, NULL);
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
init_revisions(&rev, prefix); init_revisions(&rev, prefix);
rev.diff = 1; rev.diff = 1;
rev.simplify_history = 0; rev.simplify_history = 0;
@ -446,9 +443,6 @@ int cmd_show(int argc, const char **argv, const char *prefix)
git_config(git_log_config, NULL); git_config(git_log_config, NULL);
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
init_pathspec(&match_all, NULL); init_pathspec(&match_all, NULL);
init_revisions(&rev, prefix); init_revisions(&rev, prefix);
rev.diff = 1; rev.diff = 1;
@ -524,9 +518,6 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
git_config(git_log_config, NULL); git_config(git_log_config, NULL);
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
init_revisions(&rev, prefix); init_revisions(&rev, prefix);
init_reflog_walk(&rev.reflog_info); init_reflog_walk(&rev.reflog_info);
rev.verbose_header = 1; rev.verbose_header = 1;
@ -549,9 +540,6 @@ int cmd_log(int argc, const char **argv, const char *prefix)
git_config(git_log_config, NULL); git_config(git_log_config, NULL);
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
init_revisions(&rev, prefix); init_revisions(&rev, prefix);
rev.always_show_header = 1; rev.always_show_header = 1;
memset(&opt, 0, sizeof(opt)); memset(&opt, 0, sizeof(opt));

View File

@ -1031,10 +1031,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
git_config(git_merge_config, NULL); git_config(git_merge_config, NULL);
/* for color.ui */
if (diff_use_color_default == -1)
diff_use_color_default = git_use_color_default;
if (branch_mergeoptions) if (branch_mergeoptions)
parse_branch_merge_options(branch_mergeoptions); parse_branch_merge_options(branch_mergeoptions);
argc = parse_options(argc, argv, prefix, builtin_merge_options, argc = parse_options(argc, argv, prefix, builtin_merge_options,

View File

@ -685,9 +685,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
git_config(git_show_branch_config, NULL); git_config(git_show_branch_config, NULL);
if (showbranch_use_color == -1)
showbranch_use_color = git_use_color_default;
/* If nothing is specified, try the default first */ /* If nothing is specified, try the default first */
if (ac == 1 && default_num) { if (ac == 1 && default_num) {
ac = default_num; ac = default_num;

View File

@ -1,7 +1,7 @@
#include "cache.h" #include "cache.h"
#include "color.h" #include "color.h"
int git_use_color_default = 0; static int git_use_color_default = 0;
int color_stdout_is_tty = -1; int color_stdout_is_tty = -1;
/* /*
@ -196,12 +196,15 @@ int want_color(int var)
{ {
static int want_auto = -1; static int want_auto = -1;
if (var < 0)
var = git_use_color_default;
if (var == GIT_COLOR_AUTO) { if (var == GIT_COLOR_AUTO) {
if (want_auto < 0) if (want_auto < 0)
want_auto = check_auto_color(); want_auto = check_auto_color();
return want_auto; return want_auto;
} }
return var > 0; return var;
} }
int git_color_config(const char *var, const char *value, void *cb) int git_color_config(const char *var, const char *value, void *cb)

View File

@ -58,11 +58,6 @@ struct strbuf;
#define GIT_COLOR_ALWAYS 1 #define GIT_COLOR_ALWAYS 1
#define GIT_COLOR_AUTO 2 #define GIT_COLOR_AUTO 2
/*
* This variable stores the value of color.ui
*/
extern int git_use_color_default;
/* A default list of colors to use for commit graphs and show-branch output */ /* A default list of colors to use for commit graphs and show-branch output */
extern const char *column_colors_ansi[]; extern const char *column_colors_ansi[];
extern const int column_colors_ansi_max; extern const int column_colors_ansi_max;