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>

Jeff King committed Aug 17, 2011 at 22:05 UTC c9bfb953489e559d513c1627150aa16f8d42d6c5
9 files changed +6 -44
builtin/branch.c
-3
@@ -673,9 +673,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
673
674 git_config(git_branch_config, NULL);
675
676 - if (branch_use_color == -1)
677 - branch_use_color = git_use_color_default;
678 -
676 track = git_branch_track;
677
678 head = resolve_ref("HEAD", head_sha1, 0, NULL);
builtin/commit.c
+1 -10
@@ -1237,10 +1237,6 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1237
1238 if (s.relative_paths)
1239 s.prefix = prefix;
1240 - if (s.use_color == -1)
1241 - s.use_color = git_use_color_default;
1242 - if (diff_use_color_default == -1)
1243 - diff_use_color_default = git_use_color_default;
1240
1241 switch (status_format) {
1242 case STATUS_FORMAT_SHORT:
@@ -1394,15 +1390,10 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1390 git_config(git_commit_config, &s);
1391 determine_whence(&s);
1392
1397 - if (s.use_color == -1)
1398 - s.use_color = git_use_color_default;
1393 argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
1394 prefix, &s);
1401 - if (dry_run) {
1402 - if (diff_use_color_default == -1)
1403 - diff_use_color_default = git_use_color_default;
1395 + if (dry_run)
1396 return dry_run_commit(argc, argv, prefix, &s);
1405 - }
1397 index_file = prepare_index(argc, argv, prefix, 0);
1398
1399 /* Set up everything for writing the commit object. This includes
builtin/diff.c
-3
@@ -277,9 +277,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
277 gitmodules_config();
278 git_config(git_diff_ui_config, NULL);
279
280 - if (diff_use_color_default == -1)
281 - diff_use_color_default = git_use_color_default;
282 -
280 init_revisions(&rev, prefix);
281
282 /* If this is a no-index diff, just run it and exit there. */
builtin/grep.c
-2
@@ -883,8 +883,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
883 strcpy(opt.color_sep, GIT_COLOR_CYAN);
884 opt.color = -1;
885 git_config(grep_config, &opt);
886 - if (opt.color == -1)
887 - opt.color = git_use_color_default;
886
887 /*
888 * If there is no -- then the paths must exist in the working
builtin/log.c
-12
@@ -359,9 +359,6 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
359
360 git_config(git_log_config, NULL);
361
362 - if (diff_use_color_default == -1)
363 - diff_use_color_default = git_use_color_default;
364 -
362 init_revisions(&rev, prefix);
363 rev.diff = 1;
364 rev.simplify_history = 0;
@@ -446,9 +443,6 @@ int cmd_show(int argc, const char **argv, const char *prefix)
443
444 git_config(git_log_config, NULL);
445
449 - if (diff_use_color_default == -1)
450 - diff_use_color_default = git_use_color_default;
451 -
446 init_pathspec(&match_all, NULL);
447 init_revisions(&rev, prefix);
448 rev.diff = 1;
@@ -524,9 +518,6 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
518
519 git_config(git_log_config, NULL);
520
527 - if (diff_use_color_default == -1)
528 - diff_use_color_default = git_use_color_default;
529 -
521 init_revisions(&rev, prefix);
522 init_reflog_walk(&rev.reflog_info);
523 rev.verbose_header = 1;
@@ -549,9 +540,6 @@ int cmd_log(int argc, const char **argv, const char *prefix)
540
541 git_config(git_log_config, NULL);
542
552 - if (diff_use_color_default == -1)
553 - diff_use_color_default = git_use_color_default;
554 -
543 init_revisions(&rev, prefix);
544 rev.always_show_header = 1;
545 memset(&opt, 0, sizeof(opt));
builtin/merge.c
-4
@@ -1031,10 +1031,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1031
1032 git_config(git_merge_config, NULL);
1033
1034 - /* for color.ui */
1035 - if (diff_use_color_default == -1)
1036 - diff_use_color_default = git_use_color_default;
1037 -
1034 if (branch_mergeoptions)
1035 parse_branch_merge_options(branch_mergeoptions);
1036 argc = parse_options(argc, argv, prefix, builtin_merge_options,
builtin/show-branch.c
-3
@@ -685,9 +685,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
685
686 git_config(git_show_branch_config, NULL);
687
688 - if (showbranch_use_color == -1)
689 - showbranch_use_color = git_use_color_default;
690 -
688 /* If nothing is specified, try the default first */
689 if (ac == 1 && default_num) {
690 ac = default_num;
color.c
+5 -2
@@ -1,7 +1,7 @@
1 #include "cache.h"
2 #include "color.h"
3
4 -int git_use_color_default = 0;
4 +static int git_use_color_default = 0;
5 int color_stdout_is_tty = -1;
6
7 /*
@@ -196,12 +196,15 @@ int want_color(int var)
196 {
197 static int want_auto = -1;
198
199 + if (var < 0)
200 + var = git_use_color_default;
201 +
202 if (var == GIT_COLOR_AUTO) {
203 if (want_auto < 0)
204 want_auto = check_auto_color();
205 return want_auto;
206 }
204 - return var > 0;
207 + return var;
208 }
209
210 int git_color_config(const char *var, const char *value, void *cb)
color.h
-5
@@ -58,11 +58,6 @@ struct strbuf;
58 #define GIT_COLOR_ALWAYS 1
59 #define GIT_COLOR_AUTO 2
60
61 -/*
62 - * This variable stores the value of color.ui
63 - */
64 -extern int git_use_color_default;
65 -
61 /* A default list of colors to use for commit graphs and show-branch output */
62 extern const char *column_colors_ansi[];
63 extern const int column_colors_ansi_max;