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>
Jeff King committed
Aug 17, 2011 at 22:03 UTC
f1c9626105d5e4962a5ccaa4620114d03f32ad02
7 files changed
+29
-34
builtin/merge.c
-2
@@ -390,8 +390,6 @@ static void finish(const unsigned char *new_head, const char *msg)
390
opts.output_format |=
391
DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
392
opts.detect_rename = DIFF_DETECT_RENAME;
393
- if (diff_use_color_default > 0)
394
- DIFF_OPT_SET(&opts, COLOR_DIFF);
393
if (diff_setup_done(&opts) < 0)
394
die(_("diff_setup_done failed"));
395
diff_tree_sha1(head, new_head, "", &opts);
combine-diff.c
+3
-4
@@ -702,9 +702,8 @@ static void show_combined_header(struct combine_diff_path *elem,
702
int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
703
const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
704
const char *b_prefix = opt->b_prefix ? opt->b_prefix : "b/";
705
- int use_color = DIFF_OPT_TST(opt, COLOR_DIFF);
706
- const char *c_meta = diff_get_color(use_color, DIFF_METAINFO);
707
- const char *c_reset = diff_get_color(use_color, DIFF_RESET);
705
+ const char *c_meta = diff_get_color_opt(opt, DIFF_METAINFO);
706
+ const char *c_reset = diff_get_color_opt(opt, DIFF_RESET);
707
const char *abb;
708
int added = 0;
709
int deleted = 0;
@@ -964,7 +963,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
963
show_combined_header(elem, num_parent, dense, rev,
964
mode_differs, 1);
965
dump_sline(sline, cnt, num_parent,
967
- DIFF_OPT_TST(opt, COLOR_DIFF), result_deleted);
966
+ opt->use_color, result_deleted);
967
}
968
free(result);
969
diff.c
+19
-22
@@ -583,11 +583,10 @@ static void emit_rewrite_diff(const char *name_a,
583
struct diff_options *o)
584
{
585
int lc_a, lc_b;
586
- int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
586
const char *name_a_tab, *name_b_tab;
588
- const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
589
- const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
590
- const char *reset = diff_get_color(color_diff, DIFF_RESET);
587
+ const char *metainfo = diff_get_color(o->use_color, DIFF_METAINFO);
588
+ const char *fraginfo = diff_get_color(o->use_color, DIFF_FRAGINFO);
589
+ const char *reset = diff_get_color(o->use_color, DIFF_RESET);
590
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
591
const char *a_prefix, *b_prefix;
592
char *data_one, *data_two;
@@ -623,7 +622,7 @@ static void emit_rewrite_diff(const char *name_a,
622
size_two = fill_textconv(textconv_two, two, &data_two);
623
624
memset(&ecbdata, 0, sizeof(ecbdata));
626
- ecbdata.color_diff = color_diff;
625
+ ecbdata.color_diff = o->use_color > 0;
626
ecbdata.found_changesp = &o->found_changes;
627
ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
628
ecbdata.opt = o;
@@ -1004,7 +1003,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
1003
1004
const char *diff_get_color(int diff_use_color, enum color_diff ix)
1005
{
1007
- if (diff_use_color)
1006
+ if (diff_use_color > 0)
1007
return diff_colors[ix];
1008
return "";
1009
}
@@ -1786,11 +1785,10 @@ static int is_conflict_marker(const char *line, int marker_size, unsigned long l
1785
static void checkdiff_consume(void *priv, char *line, unsigned long len)
1786
{
1787
struct checkdiff_t *data = priv;
1789
- int color_diff = DIFF_OPT_TST(data->o, COLOR_DIFF);
1788
int marker_size = data->conflict_marker_size;
1791
- const char *ws = diff_get_color(color_diff, DIFF_WHITESPACE);
1792
- const char *reset = diff_get_color(color_diff, DIFF_RESET);
1793
- const char *set = diff_get_color(color_diff, DIFF_FILE_NEW);
1789
+ const char *ws = diff_get_color(data->o->use_color, DIFF_WHITESPACE);
1790
+ const char *reset = diff_get_color(data->o->use_color, DIFF_RESET);
1791
+ const char *set = diff_get_color(data->o->use_color, DIFF_FILE_NEW);
1792
char *err;
1793
char *line_prefix = "";
1794
struct strbuf *msgbuf;
@@ -2135,7 +2133,7 @@ static void builtin_diff(const char *name_a,
2133
memset(&xecfg, 0, sizeof(xecfg));
2134
memset(&ecbdata, 0, sizeof(ecbdata));
2135
ecbdata.label_path = lbl;
2138
- ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
2136
+ ecbdata.color_diff = o->use_color > 0;
2137
ecbdata.found_changesp = &o->found_changes;
2138
ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
2139
if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
@@ -2183,7 +2181,7 @@ static void builtin_diff(const char *name_a,
2181
break;
2182
}
2183
}
2186
- if (DIFF_OPT_TST(o, COLOR_DIFF)) {
2184
+ if (o->use_color > 0) {
2185
struct diff_words_style *st = ecbdata.diff_words->style;
2186
st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
2187
st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
@@ -2833,7 +2831,7 @@ static void run_diff_cmd(const char *pgm,
2831
*/
2832
fill_metainfo(msg, name, other, one, two, o, p,
2833
&must_show_header,
2836
- DIFF_OPT_TST(o, COLOR_DIFF) && !pgm);
2834
+ o->use_color > 0 && !pgm);
2835
xfrm_msg = msg->len ? msg->buf : NULL;
2836
}
2837
@@ -2999,8 +2997,7 @@ void diff_setup(struct diff_options *options)
2997
2998
options->change = diff_change;
2999
options->add_remove = diff_addremove;
3002
- if (diff_use_color_default > 0)
3003
- DIFF_OPT_SET(options, COLOR_DIFF);
3000
+ options->use_color = diff_use_color_default;
3001
options->detect_rename = diff_detect_rename_default;
3002
3003
if (diff_no_prefix) {
@@ -3374,24 +3371,24 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3371
else if (!strcmp(arg, "--follow"))
3372
DIFF_OPT_SET(options, FOLLOW_RENAMES);
3373
else if (!strcmp(arg, "--color"))
3377
- DIFF_OPT_SET(options, COLOR_DIFF);
3374
+ options->use_color = 1;
3375
else if (!prefixcmp(arg, "--color=")) {
3376
int value = git_config_colorbool(NULL, arg+8, -1);
3377
if (value == 0)
3381
- DIFF_OPT_CLR(options, COLOR_DIFF);
3378
+ options->use_color = 0;
3379
else if (value > 0)
3383
- DIFF_OPT_SET(options, COLOR_DIFF);
3380
+ options->use_color = 1;
3381
else
3382
return error("option `color' expects \"always\", \"auto\", or \"never\"");
3383
}
3384
else if (!strcmp(arg, "--no-color"))
3388
- DIFF_OPT_CLR(options, COLOR_DIFF);
3385
+ options->use_color = 0;
3386
else if (!strcmp(arg, "--color-words")) {
3390
- DIFF_OPT_SET(options, COLOR_DIFF);
3387
+ options->use_color = 1;
3388
options->word_diff = DIFF_WORDS_COLOR;
3389
}
3390
else if (!prefixcmp(arg, "--color-words=")) {
3394
- DIFF_OPT_SET(options, COLOR_DIFF);
3391
+ options->use_color = 1;
3392
options->word_diff = DIFF_WORDS_COLOR;
3393
options->word_regex = arg + 14;
3394
}
@@ -3404,7 +3401,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
3401
if (!strcmp(type, "plain"))
3402
options->word_diff = DIFF_WORDS_PLAIN;
3403
else if (!strcmp(type, "color")) {
3407
- DIFF_OPT_SET(options, COLOR_DIFF);
3404
+ options->use_color = 1;
3405
options->word_diff = DIFF_WORDS_COLOR;
3406
}
3407
else if (!strcmp(type, "porcelain"))
diff.h
+3
-2
@@ -58,7 +58,7 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
58
#define DIFF_OPT_SILENT_ON_REMOVE (1 << 5)
59
#define DIFF_OPT_FIND_COPIES_HARDER (1 << 6)
60
#define DIFF_OPT_FOLLOW_RENAMES (1 << 7)
61
-#define DIFF_OPT_COLOR_DIFF (1 << 8)
61
+/* (1 << 8) unused */
62
/* (1 << 9) unused */
63
#define DIFF_OPT_HAS_CHANGES (1 << 10)
64
#define DIFF_OPT_QUICK (1 << 11)
@@ -101,6 +101,7 @@ struct diff_options {
101
const char *single_follow;
102
const char *a_prefix, *b_prefix;
103
unsigned flags;
104
+ int use_color;
105
int context;
106
int interhunkcontext;
107
int break_opt;
@@ -159,7 +160,7 @@ enum color_diff {
160
};
161
const char *diff_get_color(int diff_use_color, enum color_diff ix);
162
#define diff_get_color_opt(o, ix) \
162
- diff_get_color(DIFF_OPT_TST((o), COLOR_DIFF), ix)
163
+ diff_get_color((o)->use_color, ix)
164
165
166
extern const char mime_boundary_leader[];
graph.c
+1
-1
@@ -347,7 +347,7 @@ static struct commit_list *first_interesting_parent(struct git_graph *graph)
347
348
static unsigned short graph_get_current_column_color(const struct git_graph *graph)
349
{
350
- if (!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF))
350
+ if (graph->revs->diffopt.use_color <= 0)
351
return column_colors_max;
352
return graph->default_column_color;
353
}
log-tree.c
+2
-2
@@ -31,7 +31,7 @@ static char decoration_colors[][COLOR_MAXLEN] = {
31
32
static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
33
{
34
- if (decorate_use_color)
34
+ if (decorate_use_color > 0)
35
return decoration_colors[ix];
36
return "";
37
}
@@ -77,7 +77,7 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
77
* for showing the commit sha1, use the same check for --decorate
78
*/
79
#define decorate_get_color_opt(o, ix) \
80
- decorate_get_color(DIFF_OPT_TST((o), COLOR_DIFF), ix)
80
+ decorate_get_color((o)->use_color, ix)
81
82
static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
83
{
wt-status.c
+1
-1
@@ -681,7 +681,7 @@ static void wt_status_print_verbose(struct wt_status *s)
681
* will have checked isatty on stdout).
682
*/
683
if (s->fp != stdout)
684
- DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
684
+ rev.diffopt.use_color = 0;
685
run_diff_index(&rev, 1);
686
}
687