pretty: avoid adding reset for %C(auto) if output is empty
authorRené Scharfe <l.s.r@web.de>
Thu, 29 Sep 2016 18:13:05 +0000 (20:13 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 30 Sep 2016 03:44:09 +0000 (20:44 -0700)
We emit an escape sequence for resetting color and attribute for
%C(auto) to make sure automatic coloring is displayed as intended.
Stop doing that if the output strbuf is empty, i.e. when %C(auto)
appears at the start of the format string, because then there is no
need for a reset and we save a few bytes in the output.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pretty.c
t/t6006-rev-list-format.sh
index b86c5bb907ee8607d054f8afde6ab15f8c90a183..bad1bfb225a32f51920c1c6fdd1085b8b46bfc58 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -1062,7 +1062,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
        case 'C':
                if (starts_with(placeholder + 1, "(auto)")) {
                        c->auto_color = want_color(c->pretty_ctx->color);
-                       if (c->auto_color)
+                       if (c->auto_color && sb->len)
                                strbuf_addstr(sb, GIT_COLOR_RESET);
                        return 7; /* consumed 7 bytes, "C(auto)" */
                } else {
index f6020cd2aa9b726f11ef0b93ecb2ce8227a1017c..a1dcdb81d789cfa2d0e8fcc178311a807a608771 100755 (executable)
@@ -225,7 +225,7 @@ test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
 
 test_expect_success '%C(auto) respects --color' '
        git log --color --format="%C(auto)%H" -1 >actual &&
-       printf "\\033[m\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
+       printf "\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
        test_cmp expect actual
 '