diff: refactor COLOR_DIFF from a flag into an int

This lets us store more than just a bit flag for whether we
want color; we can also store whether we want automatic
colors. This can be useful for making the automatic-color
decision closer to the point of use.

This mostly just involves replacing DIFF_OPT_* calls with
manipulations of the flag. The biggest exception is that
calls to DIFF_OPT_TST must check for "o->use_color > 0",
which lets an "unknown" value (i.e., the default) stay at
"no color". In the previous code, a value of "-1" was not
propagated at all.

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:03:12 -07:00
committed by Junio C Hamano
parent 2e6c012e10
commit f1c9626105
7 changed files with 29 additions and 34 deletions

41
diff.c
View File

@ -583,11 +583,10 @@ static void emit_rewrite_diff(const char *name_a,
struct diff_options *o)
{
int lc_a, lc_b;
int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
const char *name_a_tab, *name_b_tab;
const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
const char *reset = diff_get_color(color_diff, DIFF_RESET);
const char *metainfo = diff_get_color(o->use_color, DIFF_METAINFO);
const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
const char *reset = diff_get_color(o->use_color, DIFF_RESET);
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
const char *a_prefix, *b_prefix;
char *data_one, *data_two;
@ -623,7 +622,7 @@ static void emit_rewrite_diff(const char *name_a,
size_two = fill_textconv(textconv_two, two, &data_two);
memset(&ecbdata, 0, sizeof(ecbdata));
ecbdata.color_diff = color_diff;
ecbdata.color_diff = o->use_color > 0;
ecbdata.found_changesp = &o->found_changes;
ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
ecbdata.opt = o;
@ -1004,7 +1003,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
const char *diff_get_color(int diff_use_color, enum color_diff ix)
{
if (diff_use_color)
if (diff_use_color > 0)
return diff_colors[ix];
return "";
}
@ -1786,11 +1785,10 @@ static int is_conflict_marker(const char *line, int marker_size, unsigned long l
static void checkdiff_consume(void *priv, char *line, unsigned long len)
{
struct checkdiff_t *data = priv;
int color_diff = DIFF_OPT_TST(data->o, COLOR_DIFF);
int marker_size = data->conflict_marker_size;
const char *ws = diff_get_color(color_diff, DIFF_WHITESPACE);
const char *reset = diff_get_color(color_diff, DIFF_RESET);
const char *set = diff_get_color(color_diff, DIFF_FILE_NEW);
const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
char *err;
char *line_prefix = "";
struct strbuf *msgbuf;
@ -2135,7 +2133,7 @@ static void builtin_diff(const char *name_a,
memset(&xecfg, 0, sizeof(xecfg));
memset(&ecbdata, 0, sizeof(ecbdata));
ecbdata.label_path = lbl;
ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
ecbdata.color_diff = o->use_color > 0;
ecbdata.found_changesp = &o->found_changes;
ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
@ -2183,7 +2181,7 @@ static void builtin_diff(const char *name_a,
break;
}
}
if (DIFF_OPT_TST(o, COLOR_DIFF)) {
if (o->use_color > 0) {
struct diff_words_style *st = ecbdata.diff_words->style;
st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
@ -2833,7 +2831,7 @@ static void run_diff_cmd(const char *pgm,
*/
fill_metainfo(msg, name, other, one, two, o, p,
&must_show_header,
DIFF_OPT_TST(o, COLOR_DIFF) && !pgm);
o->use_color > 0 && !pgm);
xfrm_msg = msg->len ? msg->buf : NULL;
}
@ -2999,8 +2997,7 @@ void diff_setup(struct diff_options *options)
options->change = diff_change;
options->add_remove = diff_addremove;
if (diff_use_color_default > 0)
DIFF_OPT_SET(options, COLOR_DIFF);
options->use_color = diff_use_color_default;
options->detect_rename = diff_detect_rename_default;
if (diff_no_prefix) {
@ -3374,24 +3371,24 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
else if (!strcmp(arg, "--follow"))
DIFF_OPT_SET(options, FOLLOW_RENAMES);
else if (!strcmp(arg, "--color"))
DIFF_OPT_SET(options, COLOR_DIFF);
options->use_color = 1;
else if (!prefixcmp(arg, "--color=")) {
int value = git_config_colorbool(NULL, arg+8, -1);
if (value == 0)
DIFF_OPT_CLR(options, COLOR_DIFF);
options->use_color = 0;
else if (value > 0)
DIFF_OPT_SET(options, COLOR_DIFF);
options->use_color = 1;
else
return error("option `color' expects \"always\", \"auto\", or \"never\"");
}
else if (!strcmp(arg, "--no-color"))
DIFF_OPT_CLR(options, COLOR_DIFF);
options->use_color = 0;
else if (!strcmp(arg, "--color-words")) {
DIFF_OPT_SET(options, COLOR_DIFF);
options->use_color = 1;
options->word_diff = DIFF_WORDS_COLOR;
}
else if (!prefixcmp(arg, "--color-words=")) {
DIFF_OPT_SET(options, COLOR_DIFF);
options->use_color = 1;
options->word_diff = DIFF_WORDS_COLOR;
options->word_regex = arg + 14;
}
@ -3404,7 +3401,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
if (!strcmp(type, "plain"))
options->word_diff = DIFF_WORDS_PLAIN;
else if (!strcmp(type, "color")) {
DIFF_OPT_SET(options, COLOR_DIFF);
options->use_color = 1;
options->word_diff = DIFF_WORDS_COLOR;
}
else if (!strcmp(type, "porcelain"))