Skip to content

Commit c9bfb95

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3e1dd17 commit c9bfb95

File tree

9 files changed

+6
-44
lines changed

9 files changed

+6
-44
lines changed

builtin/branch.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
673673

674674
git_config(git_branch_config, NULL);
675675

676-
if (branch_use_color == -1)
677-
branch_use_color = git_use_color_default;
678-
679676
track = git_branch_track;
680677

681678
head = resolve_ref("HEAD", head_sha1, 0, NULL);

builtin/commit.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,10 +1237,6 @@ int cmd_status(int argc, const char **argv, const char *prefix)
12371237

12381238
if (s.relative_paths)
12391239
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;
12441240

12451241
switch (status_format) {
12461242
case STATUS_FORMAT_SHORT:
@@ -1394,15 +1390,10 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
13941390
git_config(git_commit_config, &s);
13951391
determine_whence(&s);
13961392

1397-
if (s.use_color == -1)
1398-
s.use_color = git_use_color_default;
13991393
argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
14001394
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)
14041396
return dry_run_commit(argc, argv, prefix, &s);
1405-
}
14061397
index_file = prepare_index(argc, argv, prefix, 0);
14071398

14081399
/* Set up everything for writing the commit object. This includes

builtin/diff.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,6 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
277277
gitmodules_config();
278278
git_config(git_diff_ui_config, NULL);
279279

280-
if (diff_use_color_default == -1)
281-
diff_use_color_default = git_use_color_default;
282-
283280
init_revisions(&rev, prefix);
284281

285282
/* If this is a no-index diff, just run it and exit there. */

builtin/grep.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
883883
strcpy(opt.color_sep, GIT_COLOR_CYAN);
884884
opt.color = -1;
885885
git_config(grep_config, &opt);
886-
if (opt.color == -1)
887-
opt.color = git_use_color_default;
888886

889887
/*
890888
* If there is no -- then the paths must exist in the working

builtin/log.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,6 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
359359

360360
git_config(git_log_config, NULL);
361361

362-
if (diff_use_color_default == -1)
363-
diff_use_color_default = git_use_color_default;
364-
365362
init_revisions(&rev, prefix);
366363
rev.diff = 1;
367364
rev.simplify_history = 0;
@@ -446,9 +443,6 @@ int cmd_show(int argc, const char **argv, const char *prefix)
446443

447444
git_config(git_log_config, NULL);
448445

449-
if (diff_use_color_default == -1)
450-
diff_use_color_default = git_use_color_default;
451-
452446
init_pathspec(&match_all, NULL);
453447
init_revisions(&rev, prefix);
454448
rev.diff = 1;
@@ -524,9 +518,6 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
524518

525519
git_config(git_log_config, NULL);
526520

527-
if (diff_use_color_default == -1)
528-
diff_use_color_default = git_use_color_default;
529-
530521
init_revisions(&rev, prefix);
531522
init_reflog_walk(&rev.reflog_info);
532523
rev.verbose_header = 1;
@@ -549,9 +540,6 @@ int cmd_log(int argc, const char **argv, const char *prefix)
549540

550541
git_config(git_log_config, NULL);
551542

552-
if (diff_use_color_default == -1)
553-
diff_use_color_default = git_use_color_default;
554-
555543
init_revisions(&rev, prefix);
556544
rev.always_show_header = 1;
557545
memset(&opt, 0, sizeof(opt));

builtin/merge.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,10 +1031,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
10311031

10321032
git_config(git_merge_config, NULL);
10331033

1034-
/* for color.ui */
1035-
if (diff_use_color_default == -1)
1036-
diff_use_color_default = git_use_color_default;
1037-
10381034
if (branch_mergeoptions)
10391035
parse_branch_merge_options(branch_mergeoptions);
10401036
argc = parse_options(argc, argv, prefix, builtin_merge_options,

builtin/show-branch.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,9 +685,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
685685

686686
git_config(git_show_branch_config, NULL);
687687

688-
if (showbranch_use_color == -1)
689-
showbranch_use_color = git_use_color_default;
690-
691688
/* If nothing is specified, try the default first */
692689
if (ac == 1 && default_num) {
693690
ac = default_num;

color.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "cache.h"
22
#include "color.h"
33

4-
int git_use_color_default = 0;
4+
static int git_use_color_default = 0;
55
int color_stdout_is_tty = -1;
66

77
/*
@@ -196,12 +196,15 @@ int want_color(int var)
196196
{
197197
static int want_auto = -1;
198198

199+
if (var < 0)
200+
var = git_use_color_default;
201+
199202
if (var == GIT_COLOR_AUTO) {
200203
if (want_auto < 0)
201204
want_auto = check_auto_color();
202205
return want_auto;
203206
}
204-
return var > 0;
207+
return var;
205208
}
206209

207210
int git_color_config(const char *var, const char *value, void *cb)

color.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ struct strbuf;
5858
#define GIT_COLOR_ALWAYS 1
5959
#define GIT_COLOR_AUTO 2
6060

61-
/*
62-
* This variable stores the value of color.ui
63-
*/
64-
extern int git_use_color_default;
65-
6661
/* A default list of colors to use for commit graphs and show-branch output */
6762
extern const char *column_colors_ansi[];
6863
extern const int column_colors_ansi_max;

0 commit comments

Comments
 (0)