@matheusbsbs / matheus-bagliano / commits / cd112cef99
Fork 0

diff options: add --color

This patch is a slightly adjusted version of Junio's patch: http://www.gelato.unsw.edu.au/archives/git/0604/19354.html However, instead of using a config variable, this patch makes it available as a diff option. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jun 13, 2006 at 18:45 UTC cd112cef999c59a7ca2a96c37b197d303a355924
3 files changed +73 -11
diff.c
+70 -9
@@ -25,6 +25,20 @@ int git_diff_config(const char *var, const char *value)
25 return git_default_config(var, value);
26 }
27
28 +enum color_diff {
29 + DIFF_PLAIN = 0,
30 + DIFF_METAINFO = 1,
31 + DIFF_FILE_OLD = 2,
32 + DIFF_FILE_NEW = 3,
33 +};
34 +
35 +static const char *diff_colors[] = {
36 + "\033[0;0m",
37 + "\033[1;35m",
38 + "\033[1;31m",
39 + "\033[1;34m",
40 +};
41 +
42 static char *quote_one(const char *str)
43 {
44 int needlen;
@@ -177,23 +191,54 @@ static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
191 }
192
193 struct emit_callback {
194 + struct xdiff_emit_state xm;
195 + int nparents, color_diff;
196 const char **label_path;
197 };
198
183 -static int fn_out(void *priv, mmbuffer_t *mb, int nbuf)
199 +static inline void color_diff(int diff_use_color, enum color_diff ix)
200 +{
201 + if (diff_use_color)
202 + fputs(diff_colors[ix], stdout);
203 +}
204 +
205 +static void fn_out_consume(void *priv, char *line, unsigned long len)
206 {
207 int i;
208 struct emit_callback *ecbdata = priv;
209
210 if (ecbdata->label_path[0]) {
211 + color_diff(ecbdata->color_diff, DIFF_METAINFO);
212 printf("--- %s\n", ecbdata->label_path[0]);
213 + color_diff(ecbdata->color_diff, DIFF_METAINFO);
214 printf("+++ %s\n", ecbdata->label_path[1]);
215 ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
216 }
193 - for (i = 0; i < nbuf; i++)
194 - if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout))
195 - return -1;
196 - return 0;
217 +
218 + /* This is not really necessary for now because
219 + * this codepath only deals with two-way diffs.
220 + */
221 + for (i = 0; i < len && line[i] == '@'; i++)
222 + ;
223 + if (2 <= i && i < len && line[i] == ' ') {
224 + ecbdata->nparents = i - 1;
225 + color_diff(ecbdata->color_diff, DIFF_METAINFO);
226 + }
227 + else if (len < ecbdata->nparents)
228 + color_diff(ecbdata->color_diff, DIFF_PLAIN);
229 + else {
230 + int nparents = ecbdata->nparents;
231 + int color = DIFF_PLAIN;
232 + for (i = 0; i < nparents && len; i++) {
233 + if (line[i] == '-')
234 + color = DIFF_FILE_OLD;
235 + else if (line[i] == '+')
236 + color = DIFF_FILE_NEW;
237 + }
238 + color_diff(ecbdata->color_diff, color);
239 + }
240 + fwrite(line, len, 1, stdout);
241 + color_diff(ecbdata->color_diff, DIFF_PLAIN);
242 }
243
244 static char *pprint_rename(const char *a, const char *b)
@@ -549,25 +594,35 @@ static void builtin_diff(const char *name_a,
594 b_two = quote_two("b/", name_b);
595 lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
596 lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
597 + color_diff(o->color_diff, DIFF_METAINFO);
598 printf("diff --git %s %s\n", a_one, b_two);
599 if (lbl[0][0] == '/') {
600 /* /dev/null */
601 + color_diff(o->color_diff, DIFF_METAINFO);
602 printf("new file mode %06o\n", two->mode);
556 - if (xfrm_msg && xfrm_msg[0])
603 + if (xfrm_msg && xfrm_msg[0]) {
604 + color_diff(o->color_diff, DIFF_METAINFO);
605 puts(xfrm_msg);
606 + }
607 }
608 else if (lbl[1][0] == '/') {
609 printf("deleted file mode %06o\n", one->mode);
561 - if (xfrm_msg && xfrm_msg[0])
610 + if (xfrm_msg && xfrm_msg[0]) {
611 + color_diff(o->color_diff, DIFF_METAINFO);
612 puts(xfrm_msg);
613 + }
614 }
615 else {
616 if (one->mode != two->mode) {
617 + color_diff(o->color_diff, DIFF_METAINFO);
618 printf("old mode %06o\n", one->mode);
619 + color_diff(o->color_diff, DIFF_METAINFO);
620 printf("new mode %06o\n", two->mode);
621 }
569 - if (xfrm_msg && xfrm_msg[0])
622 + if (xfrm_msg && xfrm_msg[0]) {
623 + color_diff(o->color_diff, DIFF_METAINFO);
624 puts(xfrm_msg);
625 + }
626 /*
627 * we do not run diff between different kind
628 * of objects.
@@ -575,6 +630,7 @@ static void builtin_diff(const char *name_a,
630 if ((one->mode ^ two->mode) & S_IFMT)
631 goto free_ab_and_return;
632 if (complete_rewrite) {
633 + color_diff(o->color_diff, DIFF_PLAIN);
634 emit_rewrite_diff(name_a, name_b, one, two);
635 goto free_ab_and_return;
636 }
@@ -602,7 +658,9 @@ static void builtin_diff(const char *name_a,
658 xdemitcb_t ecb;
659 struct emit_callback ecbdata;
660
661 + memset(&ecbdata, 0, sizeof(ecbdata));
662 ecbdata.label_path = lbl;
663 + ecbdata.color_diff = o->color_diff;
664 xpp.flags = XDF_NEED_MINIMAL;
665 xecfg.ctxlen = o->context;
666 xecfg.flags = XDL_EMIT_FUNCNAMES;
@@ -612,8 +670,9 @@ static void builtin_diff(const char *name_a,
670 xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
671 else if (!strncmp(diffopts, "-u", 2))
672 xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
615 - ecb.outf = fn_out;
673 + ecb.outf = xdiff_outf;
674 ecb.priv = &ecbdata;
675 + ecbdata.xm.consume = fn_out_consume;
676 xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
677 }
678
@@ -1456,6 +1515,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1515 else if (40 < options->abbrev)
1516 options->abbrev = 40;
1517 }
1518 + else if (!strcmp(arg, "--color"))
1519 + options->color_diff = 1;
1520 else
1521 return 0;
1522 return 1;
diff.h
+2 -1
@@ -32,7 +32,8 @@ struct diff_options {
32 full_index:1,
33 silent_on_remove:1,
34 find_copies_harder:1,
35 - summary:1;
35 + summary:1,
36 + color_diff:1;
37 int context;
38 int break_opt;
39 int detect_rename;
pager.c
+1 -1
@@ -46,7 +46,7 @@ void setup_pager(void)
46 close(fd[0]);
47 close(fd[1]);
48
49 - setenv("LESS", "-S", 0);
49 + setenv("LESS", "-RS", 0);
50 run_pager(pager);
51 die("unable to execute pager '%s'", pager);
52 exit(255);