Skip to content

Commit f946b46

Browse files
committed
Merge branch 'jk/color-and-pager'
* jk/color-and-pager: want_color: automatically fallback to color.ui diff: don't load color config in plumbing config: refactor get_colorbool function color: delay auto-color decision until point of use git_config_colorbool: refactor stdout_is_tty handling diff: refactor COLOR_DIFF from a flag into an int setup_pager: set GIT_PAGER_IN_USE t7006: use test_config helpers test-lib: add helper functions for config t7006: modernize calls to unset Conflicts: builtin/commit.c parse-options.c
2 parents e5cfcb0 + c9bfb95 commit f946b46

File tree

21 files changed

+176
-147
lines changed

21 files changed

+176
-147
lines changed

builtin/branch.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static int parse_branch_color_slot(const char *var, int ofs)
7171
static int git_branch_config(const char *var, const char *value, void *cb)
7272
{
7373
if (!strcmp(var, "color.branch")) {
74-
branch_use_color = git_config_colorbool(var, value, -1);
74+
branch_use_color = git_config_colorbool(var, value);
7575
return 0;
7676
}
7777
if (!prefixcmp(var, "color.branch.")) {
@@ -88,7 +88,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
8888

8989
static const char *branch_get_color(enum color_branch ix)
9090
{
91-
if (branch_use_color > 0)
91+
if (want_color(branch_use_color))
9292
return branch_colors[ix];
9393
return "";
9494
}
@@ -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: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
11411141
return 0;
11421142
}
11431143
if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1144-
s->use_color = git_config_colorbool(k, v, -1);
1144+
s->use_color = git_config_colorbool(k, v);
11451145
return 0;
11461146
}
11471147
if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
@@ -1234,10 +1234,6 @@ int cmd_status(int argc, const char **argv, const char *prefix)
12341234

12351235
if (s.relative_paths)
12361236
s.prefix = prefix;
1237-
if (s.use_color == -1)
1238-
s.use_color = git_use_color_default;
1239-
if (diff_use_color_default == -1)
1240-
diff_use_color_default = git_use_color_default;
12411237

12421238
switch (status_format) {
12431239
case STATUS_FORMAT_SHORT:
@@ -1393,8 +1389,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
13931389
git_config(git_commit_config, &s);
13941390
determine_whence(&s);
13951391

1396-
if (s.use_color == -1)
1397-
s.use_color = git_use_color_default;
13981392
if (get_sha1("HEAD", sha1))
13991393
current_head = NULL;
14001394
else {
@@ -1404,11 +1398,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14041398
}
14051399
argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
14061400
prefix, current_head, &s);
1407-
if (dry_run) {
1408-
if (diff_use_color_default == -1)
1409-
diff_use_color_default = git_use_color_default;
1401+
if (dry_run)
14101402
return dry_run_commit(argc, argv, prefix, current_head, &s);
1411-
}
14121403
index_file = prepare_index(argc, argv, prefix, current_head, 0);
14131404

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

builtin/config.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -303,24 +303,18 @@ static void get_color(const char *def_color)
303303
fputs(parsed_color, stdout);
304304
}
305305

306-
static int stdout_is_tty;
307306
static int get_colorbool_found;
308307
static int get_diff_color_found;
308+
static int get_color_ui_found;
309309
static int git_get_colorbool_config(const char *var, const char *value,
310310
void *cb)
311311
{
312-
if (!strcmp(var, get_colorbool_slot)) {
313-
get_colorbool_found =
314-
git_config_colorbool(var, value, stdout_is_tty);
315-
}
316-
if (!strcmp(var, "diff.color")) {
317-
get_diff_color_found =
318-
git_config_colorbool(var, value, stdout_is_tty);
319-
}
320-
if (!strcmp(var, "color.ui")) {
321-
git_use_color_default = git_config_colorbool(var, value, stdout_is_tty);
322-
return 0;
323-
}
312+
if (!strcmp(var, get_colorbool_slot))
313+
get_colorbool_found = git_config_colorbool(var, value);
314+
else if (!strcmp(var, "diff.color"))
315+
get_diff_color_found = git_config_colorbool(var, value);
316+
else if (!strcmp(var, "color.ui"))
317+
get_color_ui_found = git_config_colorbool(var, value);
324318
return 0;
325319
}
326320

@@ -334,9 +328,11 @@ static int get_colorbool(int print)
334328
if (!strcmp(get_colorbool_slot, "color.diff"))
335329
get_colorbool_found = get_diff_color_found;
336330
if (get_colorbool_found < 0)
337-
get_colorbool_found = git_use_color_default;
331+
get_colorbool_found = get_color_ui_found;
338332
}
339333

334+
get_colorbool_found = want_color(get_colorbool_found);
335+
340336
if (print) {
341337
printf("%s\n", get_colorbool_found ? "true" : "false");
342338
return 0;
@@ -510,9 +506,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
510506
}
511507
else if (actions == ACTION_GET_COLORBOOL) {
512508
if (argc == 1)
513-
stdout_is_tty = git_config_bool("command line", argv[0]);
514-
else if (argc == 0)
515-
stdout_is_tty = isatty(1);
509+
color_stdout_is_tty = git_config_bool("command line", argv[0]);
516510
return get_colorbool(argc != 0);
517511
}
518512

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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ static int grep_config(const char *var, const char *value, void *cb)
325325
}
326326

327327
if (!strcmp(var, "color.grep"))
328-
opt->color = git_config_colorbool(var, value, -1);
328+
opt->color = git_config_colorbool(var, value);
329329
else if (!strcmp(var, "color.grep.context"))
330330
color = opt->color_context;
331331
else if (!strcmp(var, "color.grep.filename"))
@@ -898,8 +898,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
898898
strcpy(opt.color_sep, GIT_COLOR_CYAN);
899899
opt.color = -1;
900900
git_config(grep_config, &opt);
901-
if (opt.color == -1)
902-
opt.color = git_use_color_default;
903901

904902
/*
905903
* 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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,6 @@ static void finish(const unsigned char *new_head, const char *msg)
390390
opts.output_format |=
391391
DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
392392
opts.detect_rename = DIFF_DETECT_RENAME;
393-
if (diff_use_color_default > 0)
394-
DIFF_OPT_SET(&opts, COLOR_DIFF);
395393
if (diff_setup_done(&opts) < 0)
396394
die(_("diff_setup_done failed"));
397395
diff_tree_sha1(head, new_head, "", &opts);
@@ -1033,10 +1031,6 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
10331031

10341032
git_config(git_merge_config, NULL);
10351033

1036-
/* for color.ui */
1037-
if (diff_use_color_default == -1)
1038-
diff_use_color_default = git_use_color_default;
1039-
10401034
if (branch_mergeoptions)
10411035
parse_branch_merge_options(branch_mergeoptions);
10421036
argc = parse_options(argc, argv, prefix, builtin_merge_options,

builtin/show-branch.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ static const char **default_arg;
2626

2727
static const char *get_color_code(int idx)
2828
{
29-
if (showbranch_use_color)
29+
if (want_color(showbranch_use_color))
3030
return column_colors_ansi[idx % column_colors_ansi_max];
3131
return "";
3232
}
3333

3434
static const char *get_color_reset_code(void)
3535
{
36-
if (showbranch_use_color)
36+
if (want_color(showbranch_use_color))
3737
return GIT_COLOR_RESET;
3838
return "";
3939
}
@@ -573,7 +573,7 @@ static int git_show_branch_config(const char *var, const char *value, void *cb)
573573
}
574574

575575
if (!strcmp(var, "color.showbranch")) {
576-
showbranch_use_color = git_config_colorbool(var, value, -1);
576+
showbranch_use_color = git_config_colorbool(var, value);
577577
return 0;
578578
}
579579

@@ -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: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "cache.h"
22
#include "color.h"
33

4-
int git_use_color_default = 0;
4+
static int git_use_color_default = 0;
5+
int color_stdout_is_tty = -1;
56

67
/*
78
* The list of available column colors.
@@ -157,15 +158,15 @@ void color_parse_mem(const char *value, int value_len, const char *var,
157158
die("bad color value '%.*s' for variable '%s'", value_len, value, var);
158159
}
159160

160-
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
161+
int git_config_colorbool(const char *var, const char *value)
161162
{
162163
if (value) {
163164
if (!strcasecmp(value, "never"))
164165
return 0;
165166
if (!strcasecmp(value, "always"))
166167
return 1;
167168
if (!strcasecmp(value, "auto"))
168-
goto auto_color;
169+
return GIT_COLOR_AUTO;
169170
}
170171

171172
if (!var)
@@ -176,24 +177,51 @@ int git_config_colorbool(const char *var, const char *value, int stdout_is_tty)
176177
return 0;
177178

178179
/* any normal truth value defaults to 'auto' */
179-
auto_color:
180-
if (stdout_is_tty < 0)
181-
stdout_is_tty = isatty(1);
182-
if (stdout_is_tty || (pager_in_use() && pager_use_color)) {
180+
return GIT_COLOR_AUTO;
181+
}
182+
183+
static int check_auto_color(void)
184+
{
185+
if (color_stdout_is_tty < 0)
186+
color_stdout_is_tty = isatty(1);
187+
if (color_stdout_is_tty || (pager_in_use() && pager_use_color)) {
183188
char *term = getenv("TERM");
184189
if (term && strcmp(term, "dumb"))
185190
return 1;
186191
}
187192
return 0;
188193
}
189194

190-
int git_color_default_config(const char *var, const char *value, void *cb)
195+
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+
}
207+
return var;
208+
}
209+
210+
int git_color_config(const char *var, const char *value, void *cb)
191211
{
192212
if (!strcmp(var, "color.ui")) {
193-
git_use_color_default = git_config_colorbool(var, value, -1);
213+
git_use_color_default = git_config_colorbool(var, value);
194214
return 0;
195215
}
196216

217+
return 0;
218+
}
219+
220+
int git_color_default_config(const char *var, const char *value, void *cb)
221+
{
222+
if (git_color_config(var, value, cb) < 0)
223+
return -1;
224+
197225
return git_default_config(var, value, cb);
198226
}
199227

color.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,34 @@ struct strbuf;
4949
#define GIT_COLOR_NIL "NIL"
5050

5151
/*
52-
* This variable stores the value of color.ui
52+
* The first three are chosen to match common usage in the code, and what is
53+
* returned from git_config_colorbool. The "auto" value can be returned from
54+
* config_colorbool, and will be converted by want_color() into either 0 or 1.
5355
*/
54-
extern int git_use_color_default;
56+
#define GIT_COLOR_UNKNOWN -1
57+
#define GIT_COLOR_NEVER 0
58+
#define GIT_COLOR_ALWAYS 1
59+
#define GIT_COLOR_AUTO 2
5560

5661
/* A default list of colors to use for commit graphs and show-branch output */
5762
extern const char *column_colors_ansi[];
5863
extern const int column_colors_ansi_max;
5964

6065
/*
61-
* Use this instead of git_default_config if you need the value of color.ui.
66+
* Generally the color code will lazily figure this out itself, but
67+
* this provides a mechanism for callers to override autodetection.
6268
*/
69+
extern int color_stdout_is_tty;
70+
71+
/*
72+
* Use the first one if you need only color config; the second is a convenience
73+
* if you are just going to change to git_default_config, too.
74+
*/
75+
int git_color_config(const char *var, const char *value, void *cb);
6376
int git_color_default_config(const char *var, const char *value, void *cb);
6477

65-
int git_config_colorbool(const char *var, const char *value, int stdout_is_tty);
78+
int git_config_colorbool(const char *var, const char *value);
79+
int want_color(int var);
6680
void color_parse(const char *value, const char *var, char *dst);
6781
void color_parse_mem(const char *value, int len, const char *var, char *dst);
6882
__attribute__((format (printf, 3, 4)))

0 commit comments

Comments
 (0)