pretty: avoid adding reset for %C(auto) if output is empty

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>

René Scharfe committed Sep 29, 2016 at 20:13 UTC 82b83da8d30fb8d1f04f7dd7ac769ceb6ab431c3
2 files changed +2 -2
pretty.c
+1 -1
@@ -1062,7 +1062,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1062 case 'C':
1063 if (starts_with(placeholder + 1, "(auto)")) {
1064 c->auto_color = want_color(c->pretty_ctx->color);
1065 - if (c->auto_color)
1065 + if (c->auto_color && sb->len)
1066 strbuf_addstr(sb, GIT_COLOR_RESET);
1067 return 7; /* consumed 7 bytes, "C(auto)" */
1068 } else {
t/t6006-rev-list-format.sh
+1 -1
@@ -225,7 +225,7 @@ test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
225
226 test_expect_success '%C(auto) respects --color' '
227 git log --color --format="%C(auto)%H" -1 >actual &&
228 - printf "\\033[m\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
228 + printf "\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
229 test_cmp expect actual
230 '
231