decorate: refactor format_decorations()

Rename the format_decorations_extended function to format_decorations and drop the format_decorations wrapper macro. Pass the prefix, suffix and separator strings as a single 'struct format_decorations' pointer argument instead of separate arguments. Use default values defined in the function when either the struct pointer or any of the struct fields are NULL. This is to ease extension with additional options. 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 a3883a6532163f0a53a7b202f32bf8fe565af056
3 files changed +35 -16
log-tree.c
+17 -6
@@ -303,14 +303,12 @@ static void show_name(struct strbuf *sb, const struct name_decoration *decoratio
303
304 /*
305 * The caller makes sure there is no funny color before calling.
306 - * format_decorations_extended makes sure the same after return.
306 + * format_decorations ensures the same after return.
307 */
308 -void format_decorations_extended(struct strbuf *sb,
308 +void format_decorations(struct strbuf *sb,
309 const struct commit *commit,
310 int use_color,
311 - const char *prefix,
312 - const char *separator,
313 - const char *suffix)
311 + const struct decoration_options *opts)
312 {
313 const struct name_decoration *decoration;
314 const struct name_decoration *current_and_HEAD;
@@ -319,10 +317,23 @@ void format_decorations_extended(struct strbuf *sb,
317 const char *color_reset =
318 decorate_get_color(use_color, DECORATION_NONE);
319
320 + const char *prefix = " (";
321 + const char *suffix = ")";
322 + const char *separator = ", ";
323 +
324 decoration = get_name_decoration(&commit->object);
325 if (!decoration)
326 return;
327
328 + if (opts) {
329 + if (opts->prefix)
330 + prefix = opts->prefix;
331 + if (opts->suffix)
332 + suffix = opts->suffix;
333 + if (opts->separator)
334 + separator = opts->separator;
335 + }
336 +
337 current_and_HEAD = current_pointed_by_HEAD(decoration);
338 while (decoration) {
339 /*
@@ -370,7 +381,7 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
381 }
382 if (!opt->show_decorations)
383 return;
373 - format_decorations(&sb, commit, opt->diffopt.use_color);
384 + format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
385 fputs(sb.buf, opt->diffopt.file);
386 strbuf_release(&sb);
387 }
log-tree.h
+8 -7
@@ -13,17 +13,18 @@ struct decoration_filter {
13 struct string_list *exclude_ref_config_pattern;
14 };
15
16 +struct decoration_options {
17 + char *prefix;
18 + char *suffix;
19 + char *separator;
20 +};
21 +
22 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
23 int log_tree_diff_flush(struct rev_info *);
24 int log_tree_commit(struct rev_info *, struct commit *);
25 void show_log(struct rev_info *opt);
20 -void format_decorations_extended(struct strbuf *sb, const struct commit *commit,
21 - int use_color,
22 - const char *prefix,
23 - const char *separator,
24 - const char *suffix);
25 -#define format_decorations(strbuf, commit, color) \
26 - format_decorations_extended((strbuf), (commit), (color), " (", ", ", ")")
26 +void format_decorations(struct strbuf *sb, const struct commit *commit,
27 + int use_color, const struct decoration_options *opts);
28 void show_decorations(struct rev_info *opt, struct commit *commit);
29 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
30 const char **extra_headers_p,
pretty.c
+10 -3
@@ -1537,11 +1537,18 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1537 strbuf_addstr(sb, get_revision_mark(NULL, commit));
1538 return 1;
1539 case 'd':
1540 - format_decorations(sb, commit, c->auto_color);
1540 + format_decorations(sb, commit, c->auto_color, NULL);
1541 return 1;
1542 case 'D':
1543 - format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
1544 - return 1;
1543 + {
1544 + const struct decoration_options opts = {
1545 + .prefix = "",
1546 + .suffix = ""
1547 + };
1548 +
1549 + format_decorations(sb, commit, c->auto_color, &opts);
1550 + return 1;
1551 + }
1552 case 'S': /* tag/branch like --source */
1553 if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
1554 return 0;