Skip to content

Commit f1c9626

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

File tree

7 files changed

+29
-34
lines changed

7 files changed

+29
-34
lines changed

builtin/merge.c

Lines changed: 0 additions & 2 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);

combine-diff.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,8 @@ static void show_combined_header(struct combine_diff_path *elem,
702702
int abbrev = DIFF_OPT_TST(opt, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
703703
const char *a_prefix = opt->a_prefix ? opt->a_prefix : "a/";
704704
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);
708707
const char *abb;
709708
int added = 0;
710709
int deleted = 0;
@@ -964,7 +963,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
964963
show_combined_header(elem, num_parent, dense, rev,
965964
mode_differs, 1);
966965
dump_sline(sline, cnt, num_parent,
967-
DIFF_OPT_TST(opt, COLOR_DIFF), result_deleted);
966+
opt->use_color, result_deleted);
968967
}
969968
free(result);
970969

diff.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,10 @@ static void emit_rewrite_diff(const char *name_a,
583583
struct diff_options *o)
584584
{
585585
int lc_a, lc_b;
586-
int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
587586
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);
591590
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
592591
const char *a_prefix, *b_prefix;
593592
char *data_one, *data_two;
@@ -623,7 +622,7 @@ static void emit_rewrite_diff(const char *name_a,
623622
size_two = fill_textconv(textconv_two, two, &data_two);
624623

625624
memset(&ecbdata, 0, sizeof(ecbdata));
626-
ecbdata.color_diff = color_diff;
625+
ecbdata.color_diff = o->use_color > 0;
627626
ecbdata.found_changesp = &o->found_changes;
628627
ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
629628
ecbdata.opt = o;
@@ -1004,7 +1003,7 @@ static void free_diff_words_data(struct emit_callback *ecbdata)
10041003

10051004
const char *diff_get_color(int diff_use_color, enum color_diff ix)
10061005
{
1007-
if (diff_use_color)
1006+
if (diff_use_color > 0)
10081007
return diff_colors[ix];
10091008
return "";
10101009
}
@@ -1786,11 +1785,10 @@ static int is_conflict_marker(const char *line, int marker_size, unsigned long l
17861785
static void checkdiff_consume(void *priv, char *line, unsigned long len)
17871786
{
17881787
struct checkdiff_t *data = priv;
1789-
int color_diff = DIFF_OPT_TST(data->o, COLOR_DIFF);
17901788
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);
17941792
char *err;
17951793
char *line_prefix = "";
17961794
struct strbuf *msgbuf;
@@ -2135,7 +2133,7 @@ static void builtin_diff(const char *name_a,
21352133
memset(&xecfg, 0, sizeof(xecfg));
21362134
memset(&ecbdata, 0, sizeof(ecbdata));
21372135
ecbdata.label_path = lbl;
2138-
ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
2136+
ecbdata.color_diff = o->use_color > 0;
21392137
ecbdata.found_changesp = &o->found_changes;
21402138
ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
21412139
if (ecbdata.ws_rule & WS_BLANK_AT_EOF)
@@ -2183,7 +2181,7 @@ static void builtin_diff(const char *name_a,
21832181
break;
21842182
}
21852183
}
2186-
if (DIFF_OPT_TST(o, COLOR_DIFF)) {
2184+
if (o->use_color > 0) {
21872185
struct diff_words_style *st = ecbdata.diff_words->style;
21882186
st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
21892187
st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
@@ -2833,7 +2831,7 @@ static void run_diff_cmd(const char *pgm,
28332831
*/
28342832
fill_metainfo(msg, name, other, one, two, o, p,
28352833
&must_show_header,
2836-
DIFF_OPT_TST(o, COLOR_DIFF) && !pgm);
2834+
o->use_color > 0 && !pgm);
28372835
xfrm_msg = msg->len ? msg->buf : NULL;
28382836
}
28392837

@@ -2999,8 +2997,7 @@ void diff_setup(struct diff_options *options)
29992997

30002998
options->change = diff_change;
30012999
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;
30043001
options->detect_rename = diff_detect_rename_default;
30053002

30063003
if (diff_no_prefix) {
@@ -3374,24 +3371,24 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
33743371
else if (!strcmp(arg, "--follow"))
33753372
DIFF_OPT_SET(options, FOLLOW_RENAMES);
33763373
else if (!strcmp(arg, "--color"))
3377-
DIFF_OPT_SET(options, COLOR_DIFF);
3374+
options->use_color = 1;
33783375
else if (!prefixcmp(arg, "--color=")) {
33793376
int value = git_config_colorbool(NULL, arg+8, -1);
33803377
if (value == 0)
3381-
DIFF_OPT_CLR(options, COLOR_DIFF);
3378+
options->use_color = 0;
33823379
else if (value > 0)
3383-
DIFF_OPT_SET(options, COLOR_DIFF);
3380+
options->use_color = 1;
33843381
else
33853382
return error("option `color' expects \"always\", \"auto\", or \"never\"");
33863383
}
33873384
else if (!strcmp(arg, "--no-color"))
3388-
DIFF_OPT_CLR(options, COLOR_DIFF);
3385+
options->use_color = 0;
33893386
else if (!strcmp(arg, "--color-words")) {
3390-
DIFF_OPT_SET(options, COLOR_DIFF);
3387+
options->use_color = 1;
33913388
options->word_diff = DIFF_WORDS_COLOR;
33923389
}
33933390
else if (!prefixcmp(arg, "--color-words=")) {
3394-
DIFF_OPT_SET(options, COLOR_DIFF);
3391+
options->use_color = 1;
33953392
options->word_diff = DIFF_WORDS_COLOR;
33963393
options->word_regex = arg + 14;
33973394
}
@@ -3404,7 +3401,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
34043401
if (!strcmp(type, "plain"))
34053402
options->word_diff = DIFF_WORDS_PLAIN;
34063403
else if (!strcmp(type, "color")) {
3407-
DIFF_OPT_SET(options, COLOR_DIFF);
3404+
options->use_color = 1;
34083405
options->word_diff = DIFF_WORDS_COLOR;
34093406
}
34103407
else if (!strcmp(type, "porcelain"))

diff.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct strbuf *(*diff_prefix_fn_t)(struct diff_options *opt, void *data)
5858
#define DIFF_OPT_SILENT_ON_REMOVE (1 << 5)
5959
#define DIFF_OPT_FIND_COPIES_HARDER (1 << 6)
6060
#define DIFF_OPT_FOLLOW_RENAMES (1 << 7)
61-
#define DIFF_OPT_COLOR_DIFF (1 << 8)
61+
/* (1 << 8) unused */
6262
/* (1 << 9) unused */
6363
#define DIFF_OPT_HAS_CHANGES (1 << 10)
6464
#define DIFF_OPT_QUICK (1 << 11)
@@ -101,6 +101,7 @@ struct diff_options {
101101
const char *single_follow;
102102
const char *a_prefix, *b_prefix;
103103
unsigned flags;
104+
int use_color;
104105
int context;
105106
int interhunkcontext;
106107
int break_opt;
@@ -159,7 +160,7 @@ enum color_diff {
159160
};
160161
const char *diff_get_color(int diff_use_color, enum color_diff ix);
161162
#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)
163164

164165

165166
extern const char mime_boundary_leader[];

graph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ static struct commit_list *first_interesting_parent(struct git_graph *graph)
347347

348348
static unsigned short graph_get_current_column_color(const struct git_graph *graph)
349349
{
350-
if (!DIFF_OPT_TST(&graph->revs->diffopt, COLOR_DIFF))
350+
if (graph->revs->diffopt.use_color <= 0)
351351
return column_colors_max;
352352
return graph->default_column_color;
353353
}

log-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static char decoration_colors[][COLOR_MAXLEN] = {
3131

3232
static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
3333
{
34-
if (decorate_use_color)
34+
if (decorate_use_color > 0)
3535
return decoration_colors[ix];
3636
return "";
3737
}
@@ -77,7 +77,7 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
7777
* for showing the commit sha1, use the same check for --decorate
7878
*/
7979
#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)
8181

8282
static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
8383
{

wt-status.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ static void wt_status_print_verbose(struct wt_status *s)
681681
* will have checked isatty on stdout).
682682
*/
683683
if (s->fp != stdout)
684-
DIFF_OPT_CLR(&rev.diffopt, COLOR_DIFF);
684+
rev.diffopt.use_color = 0;
685685
run_diff_index(&rev, 1);
686686
}
687687

0 commit comments

Comments
 (0)