@matheusbsbs / matheus-bagliano / commits / 374664478f
Fork 0

diff: fix coloring of extended diff headers

Coloring the extended headers where done as a whole not per line. less with option -R (which is the default from git) does not support this coloring mode because of performance reasons. The -r option would be an alternative but has problems with lines that are longer than the screen. Therefore stick to the idiom to color each line separately. The problem is, that the result of ill_metainfo() will also be used as an parameter to an external diff driver, so we need to disable coloring in this case. Because coloring is now done inside fill_metainfo() we can simply add this string to the diff header and therefore keep the last newline in the extended header. This results also into the fact that the external diff driver now gets this last newline too. Which is a change in behavior but a good one. Signed-off-by: Bert Wesarg <bert.wesarg@googlemail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Bert Wesarg committed May 4, 2010 at 00:38 UTC 374664478f204ab45bbd494ab21492f331d8b1f0
1 file changed +35 -26
diff.c
+35 -26
@@ -1650,21 +1650,21 @@ static void builtin_diff(const char *name_a,
1650 if (lbl[0][0] == '/') {
1651 /* /dev/null */
1652 strbuf_addf(&header, "%snew file mode %06o%s\n", set, two->mode, reset);
1653 - if (xfrm_msg && xfrm_msg[0])
1654 - strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
1653 + if (xfrm_msg)
1654 + strbuf_addstr(&header, xfrm_msg);
1655 }
1656 else if (lbl[1][0] == '/') {
1657 strbuf_addf(&header, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
1658 - if (xfrm_msg && xfrm_msg[0])
1659 - strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
1658 + if (xfrm_msg)
1659 + strbuf_addstr(&header, xfrm_msg);
1660 }
1661 else {
1662 if (one->mode != two->mode) {
1663 strbuf_addf(&header, "%sold mode %06o%s\n", set, one->mode, reset);
1664 strbuf_addf(&header, "%snew mode %06o%s\n", set, two->mode, reset);
1665 }
1666 - if (xfrm_msg && xfrm_msg[0])
1667 - strbuf_addf(&header, "%s%s%s\n", set, xfrm_msg, reset);
1666 + if (xfrm_msg)
1667 + strbuf_addstr(&header, xfrm_msg);
1668
1669 /*
1670 * we do not run diff between different kind
@@ -2323,30 +2323,36 @@ static void fill_metainfo(struct strbuf *msg,
2323 struct diff_filespec *one,
2324 struct diff_filespec *two,
2325 struct diff_options *o,
2326 - struct diff_filepair *p)
2326 + struct diff_filepair *p,
2327 + int use_color)
2328 {
2329 + const char *set = diff_get_color(use_color, DIFF_METAINFO);
2330 + const char *reset = diff_get_color(use_color, DIFF_RESET);
2331 +
2332 strbuf_init(msg, PATH_MAX * 2 + 300);
2333 switch (p->status) {
2334 case DIFF_STATUS_COPIED:
2331 - strbuf_addf(msg, "similarity index %d%%", similarity_index(p));
2332 - strbuf_addstr(msg, "\ncopy from ");
2335 + strbuf_addf(msg, "%ssimilarity index %d%%",
2336 + set, similarity_index(p));
2337 + strbuf_addf(msg, "%s\n%scopy from ", reset, set);
2338 quote_c_style(name, msg, NULL, 0);
2334 - strbuf_addstr(msg, "\ncopy to ");
2339 + strbuf_addf(msg, "%s\n%scopy to ", reset, set);
2340 quote_c_style(other, msg, NULL, 0);
2336 - strbuf_addch(msg, '\n');
2341 + strbuf_addf(msg, "%s\n", reset);
2342 break;
2343 case DIFF_STATUS_RENAMED:
2339 - strbuf_addf(msg, "similarity index %d%%", similarity_index(p));
2340 - strbuf_addstr(msg, "\nrename from ");
2344 + strbuf_addf(msg, "%ssimilarity index %d%%",
2345 + set, similarity_index(p));
2346 + strbuf_addf(msg, "%s\n%srename from ", reset, set);
2347 quote_c_style(name, msg, NULL, 0);
2342 - strbuf_addstr(msg, "\nrename to ");
2348 + strbuf_addf(msg, "%s\n%srename to ", reset, set);
2349 quote_c_style(other, msg, NULL, 0);
2344 - strbuf_addch(msg, '\n');
2350 + strbuf_addf(msg, "%s\n", reset);
2351 break;
2352 case DIFF_STATUS_MODIFIED:
2353 if (p->score) {
2348 - strbuf_addf(msg, "dissimilarity index %d%%\n",
2349 - similarity_index(p));
2354 + strbuf_addf(msg, "%sdissimilarity index %d%%%s\n",
2355 + set, similarity_index(p), reset);
2356 break;
2357 }
2358 /* fallthru */
@@ -2363,15 +2369,13 @@ static void fill_metainfo(struct strbuf *msg,
2369 (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
2370 abbrev = 40;
2371 }
2366 - strbuf_addf(msg, "index %.*s..%.*s",
2372 + strbuf_addf(msg, "%sindex %.*s..%.*s", set,
2373 abbrev, sha1_to_hex(one->sha1),
2374 abbrev, sha1_to_hex(two->sha1));
2375 if (one->mode == two->mode)
2376 strbuf_addf(msg, " %06o", one->mode);
2371 - strbuf_addch(msg, '\n');
2377 + strbuf_addf(msg, "%s\n", reset);
2378 }
2373 - if (msg->len)
2374 - strbuf_setlen(msg, msg->len - 1);
2379 }
2380
2381 static void run_diff_cmd(const char *pgm,
@@ -2387,11 +2391,6 @@ static void run_diff_cmd(const char *pgm,
2391 const char *xfrm_msg = NULL;
2392 int complete_rewrite = (p->status == DIFF_STATUS_MODIFIED) && p->score;
2393
2390 - if (msg) {
2391 - fill_metainfo(msg, name, other, one, two, o, p);
2392 - xfrm_msg = msg->len ? msg->buf : NULL;
2393 - }
2394 -
2394 if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
2395 pgm = NULL;
2396 else {
@@ -2400,6 +2399,16 @@ static void run_diff_cmd(const char *pgm,
2399 pgm = drv->external;
2400 }
2401
2402 + if (msg) {
2403 + /*
2404 + * don't use colors when the header is intended for an
2405 + * external diff driver
2406 + */
2407 + fill_metainfo(msg, name, other, one, two, o, p,
2408 + DIFF_OPT_TST(o, COLOR_DIFF) && !pgm);
2409 + xfrm_msg = msg->len ? msg->buf : NULL;
2410 + }
2411 +
2412 if (pgm) {
2413 run_external_diff(pgm, name, other, one, two, xfrm_msg,
2414 complete_rewrite);