Skip to content

Commit 3c3e9b8

Browse files
peffgitster
authored andcommitted
color: use GIT_COLOR_* instead of numeric constants
Long ago Git's decision to show color for a subsytem was stored in a tri-state variable: it could be true (1), false (0), or unknown (-1). But since daa0c3d (color: delay auto-color decision until point of use, 2011-08-17) we want to carry around a new state, "auto", which bases the decision on the tty-ness of stdout (rather than collapsing that "auto" state to a true/false immediately). That commit introduced a set of GIT_COLOR_* defines to represent each state: UNKNOWN, ALWAYS, NEVER, and AUTO. But it only used the AUTO value, and left alone code using bare 0/1/-1 values. And of course since then we've grown many new spots that use those bare values. Let's switch all of these to use the named constants. That should make the code a bit easier to read, as it is more obvious that we're representing a color decision. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e335ff3 commit 3c3e9b8

21 files changed

+42
-40
lines changed

add-interactive.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static int check_color_config(struct repository *r, const char *var)
4242
int ret;
4343

4444
if (repo_config_get_value(r, var, &value))
45-
ret = -1;
45+
ret = GIT_COLOR_UNKNOWN;
4646
else
4747
ret = git_config_colorbool(var, value);
4848

@@ -51,7 +51,8 @@ static int check_color_config(struct repository *r, const char *var)
5151
* the value parsed by git_color_config(), which may not have been
5252
* called by the main command.
5353
*/
54-
if (ret < 0 && !repo_config_get_value(r, "color.ui", &value))
54+
if (ret == GIT_COLOR_UNKNOWN &&
55+
!repo_config_get_value(r, "color.ui", &value))
5556
ret = git_config_colorbool("color.ui", value);
5657

5758
return want_color(ret);
@@ -130,8 +131,8 @@ void clear_add_i_state(struct add_i_state *s)
130131
FREE_AND_NULL(s->interactive_diff_filter);
131132
FREE_AND_NULL(s->interactive_diff_algorithm);
132133
memset(s, 0, sizeof(*s));
133-
s->use_color_interactive = -1;
134-
s->use_color_diff = -1;
134+
s->use_color_interactive = GIT_COLOR_UNKNOWN;
135+
s->use_color_diff = GIT_COLOR_UNKNOWN;
135136
}
136137

137138
/*

advice.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "help.h"
88
#include "string-list.h"
99

10-
static int advice_use_color = -1;
10+
static int advice_use_color = GIT_COLOR_UNKNOWN;
1111
static char advice_colors[][COLOR_MAXLEN] = {
1212
GIT_COLOR_RESET,
1313
GIT_COLOR_YELLOW, /* HINT */

builtin/add.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static int edit_patch(struct repository *repo,
200200

201201
argc = setup_revisions(argc, argv, &rev, NULL);
202202
rev.diffopt.output_format = DIFF_FORMAT_PATCH;
203-
rev.diffopt.use_color = 0;
203+
rev.diffopt.use_color = GIT_COLOR_NEVER;
204204
rev.diffopt.flags.ignore_dirty_submodules = 1;
205205
out = xopen(file, O_CREAT | O_WRONLY | O_TRUNC, 0666);
206206
rev.diffopt.file = xfdopen(out, "w");

builtin/am.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ static void write_commit_patch(const struct am_state *state, struct commit *comm
14081408
rev_info.no_commit_id = 1;
14091409
rev_info.diffopt.flags.binary = 1;
14101410
rev_info.diffopt.flags.full_index = 1;
1411-
rev_info.diffopt.use_color = 0;
1411+
rev_info.diffopt.use_color = GIT_COLOR_NEVER;
14121412
rev_info.diffopt.file = fp;
14131413
rev_info.diffopt.close_file = 1;
14141414
add_pending_object(&rev_info, &commit->object, "");
@@ -1441,7 +1441,7 @@ static void write_index_patch(const struct am_state *state)
14411441
rev_info.disable_stdin = 1;
14421442
rev_info.no_commit_id = 1;
14431443
rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
1444-
rev_info.diffopt.use_color = 0;
1444+
rev_info.diffopt.use_color = GIT_COLOR_NEVER;
14451445
rev_info.diffopt.file = fp;
14461446
rev_info.diffopt.close_file = 1;
14471447
add_pending_object(&rev_info, &tree->object, "");

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static struct object_id head_oid;
4646
static int recurse_submodules = 0;
4747
static int submodule_propagate_branches = 0;
4848

49-
static int branch_use_color = -1;
49+
static int branch_use_color = GIT_COLOR_UNKNOWN;
5050
static char branch_colors[][COLOR_MAXLEN] = {
5151
GIT_COLOR_RESET,
5252
GIT_COLOR_NORMAL, /* PLAIN */

builtin/clean.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static const char *color_interactive_slots[] = {
6464
[CLEAN_COLOR_RESET] = "reset",
6565
};
6666

67-
static int clean_use_color = -1;
67+
static int clean_use_color = GIT_COLOR_UNKNOWN;
6868
static char clean_colors[][COLOR_MAXLEN] = {
6969
[CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
7070
[CLEAN_COLOR_HEADER] = GIT_COLOR_BOLD,

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
10161016
status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
10171017

10181018
saved_color_setting = s->use_color;
1019-
s->use_color = 0;
1019+
s->use_color = GIT_COLOR_NEVER;
10201020
committable = run_status(s->fp, index_file, prefix, 1, s);
10211021
s->use_color = saved_color_setting;
10221022
string_list_clear_func(&s->change, change_data_free);

builtin/config.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,23 +594,23 @@ static int get_colorbool(const struct config_location_options *opts,
594594
{
595595
struct get_colorbool_config_data data = {
596596
.get_colorbool_slot = var,
597-
.get_colorbool_found = -1,
598-
.get_diff_color_found = -1,
599-
.get_color_ui_found = -1,
597+
.get_colorbool_found = GIT_COLOR_UNKNOWN,
598+
.get_diff_color_found = GIT_COLOR_UNKNOWN,
599+
.get_color_ui_found = GIT_COLOR_UNKNOWN,
600600
};
601601

602602
config_with_options(git_get_colorbool_config, &data,
603603
&opts->source, the_repository,
604604
&opts->options);
605605

606-
if (data.get_colorbool_found < 0) {
606+
if (data.get_colorbool_found == GIT_COLOR_UNKNOWN) {
607607
if (!strcmp(data.get_colorbool_slot, "color.diff"))
608608
data.get_colorbool_found = data.get_diff_color_found;
609-
if (data.get_colorbool_found < 0)
609+
if (data.get_colorbool_found == GIT_COLOR_UNKNOWN)
610610
data.get_colorbool_found = data.get_color_ui_found;
611611
}
612612

613-
if (data.get_colorbool_found < 0)
613+
if (data.get_colorbool_found == GIT_COLOR_UNKNOWN)
614614
/* default value if none found in config */
615615
data.get_colorbool_found = GIT_COLOR_AUTO;
616616

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ int cmd_grep(int argc,
10911091
if (show_in_pager == default_pager)
10921092
show_in_pager = git_pager(the_repository, 1);
10931093
if (show_in_pager) {
1094-
opt.color = 0;
1094+
opt.color = GIT_COLOR_NEVER;
10951095
opt.name_only = 1;
10961096
opt.null_following_name = 1;
10971097
opt.output_priv = &path_list;

builtin/push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static const char * const push_usage[] = {
2727
NULL,
2828
};
2929

30-
static int push_use_color = -1;
30+
static int push_use_color = GIT_COLOR_UNKNOWN;
3131
static char push_colors[][COLOR_MAXLEN] = {
3232
GIT_COLOR_RESET,
3333
GIT_COLOR_RED, /* ERROR */

0 commit comments

Comments
 (0)