decorate: avoid some unnecessary color overhead

In format_decorations(), don't obtain color sequences if there are no decorations, and don't emit color sequences around empty strings. Signed-off-by: Andy Koppe <andy.koppe@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Andy Koppe committed Aug 20, 2023 at 19:50 UTC b87a9a2c1ef8c39d4fc3d1e28ebb79563d4804be
1 file changed +15 -10
log-tree.c
+15 -10
@@ -312,10 +312,7 @@ void format_decorations(struct strbuf *sb,
312 {
313 const struct name_decoration *decoration;
314 const struct name_decoration *current_and_HEAD;
315 - const char *color_commit =
316 - diff_get_color(use_color, DIFF_COMMIT);
317 - const char *color_reset =
318 - decorate_get_color(use_color, DECORATION_NONE);
315 + const char *color_commit, *color_reset;
316
317 const char *prefix = " (";
318 const char *suffix = ")";
@@ -334,6 +331,9 @@ void format_decorations(struct strbuf *sb,
331 separator = opts->separator;
332 }
333
334 + color_commit = diff_get_color(use_color, DIFF_COMMIT);
335 + color_reset = decorate_get_color(use_color, DECORATION_NONE);
336 +
337 current_and_HEAD = current_pointed_by_HEAD(decoration);
338 while (decoration) {
339 /*
@@ -342,9 +342,12 @@ void format_decorations(struct strbuf *sb,
342 * appeared, skipping the entry for current.
343 */
344 if (decoration != current_and_HEAD) {
345 - strbuf_addstr(sb, color_commit);
346 - strbuf_addstr(sb, prefix);
347 - strbuf_addstr(sb, color_reset);
345 + if (*prefix) {
346 + strbuf_addstr(sb, color_commit);
347 + strbuf_addstr(sb, prefix);
348 + strbuf_addstr(sb, color_reset);
349 + }
350 +
351 strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
352 if (decoration->type == DECORATION_REF_TAG)
353 strbuf_addstr(sb, "tag: ");
@@ -364,9 +367,11 @@ void format_decorations(struct strbuf *sb,
367 }
368 decoration = decoration->next;
369 }
367 - strbuf_addstr(sb, color_commit);
368 - strbuf_addstr(sb, suffix);
369 - strbuf_addstr(sb, color_reset);
370 + if (*suffix) {
371 + strbuf_addstr(sb, color_commit);
372 + strbuf_addstr(sb, suffix);
373 + strbuf_addstr(sb, color_reset);
374 + }
375 }
376
377 void show_decorations(struct rev_info *opt, struct commit *commit)