Skip to content

Commit b87a9a2

Browse files
ak2gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a3883a6 commit b87a9a2

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

log-tree.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,7 @@ void format_decorations(struct strbuf *sb,
312312
{
313313
const struct name_decoration *decoration;
314314
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;
319316

320317
const char *prefix = " (";
321318
const char *suffix = ")";
@@ -334,6 +331,9 @@ void format_decorations(struct strbuf *sb,
334331
separator = opts->separator;
335332
}
336333

334+
color_commit = diff_get_color(use_color, DIFF_COMMIT);
335+
color_reset = decorate_get_color(use_color, DECORATION_NONE);
336+
337337
current_and_HEAD = current_pointed_by_HEAD(decoration);
338338
while (decoration) {
339339
/*
@@ -342,9 +342,12 @@ void format_decorations(struct strbuf *sb,
342342
* appeared, skipping the entry for current.
343343
*/
344344
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+
348351
strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
349352
if (decoration->type == DECORATION_REF_TAG)
350353
strbuf_addstr(sb, "tag: ");
@@ -364,9 +367,11 @@ void format_decorations(struct strbuf *sb,
364367
}
365368
decoration = decoration->next;
366369
}
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+
}
370375
}
371376

372377
void show_decorations(struct rev_info *opt, struct commit *commit)

0 commit comments

Comments
 (0)