Skip to content

Commit a3883a6

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

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

log-tree.c

+17-6
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,12 @@ static void show_name(struct strbuf *sb, const struct name_decoration *decoratio
303303

304304
/*
305305
* 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.
307307
*/
308-
void format_decorations_extended(struct strbuf *sb,
308+
void format_decorations(struct strbuf *sb,
309309
const struct commit *commit,
310310
int use_color,
311-
const char *prefix,
312-
const char *separator,
313-
const char *suffix)
311+
const struct decoration_options *opts)
314312
{
315313
const struct name_decoration *decoration;
316314
const struct name_decoration *current_and_HEAD;
@@ -319,10 +317,23 @@ void format_decorations_extended(struct strbuf *sb,
319317
const char *color_reset =
320318
decorate_get_color(use_color, DECORATION_NONE);
321319

320+
const char *prefix = " (";
321+
const char *suffix = ")";
322+
const char *separator = ", ";
323+
322324
decoration = get_name_decoration(&commit->object);
323325
if (!decoration)
324326
return;
325327

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+
326337
current_and_HEAD = current_pointed_by_HEAD(decoration);
327338
while (decoration) {
328339
/*
@@ -370,7 +381,7 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
370381
}
371382
if (!opt->show_decorations)
372383
return;
373-
format_decorations(&sb, commit, opt->diffopt.use_color);
384+
format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
374385
fputs(sb.buf, opt->diffopt.file);
375386
strbuf_release(&sb);
376387
}

log-tree.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ struct decoration_filter {
1313
struct string_list *exclude_ref_config_pattern;
1414
};
1515

16+
struct decoration_options {
17+
char *prefix;
18+
char *suffix;
19+
char *separator;
20+
};
21+
1622
int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
1723
int log_tree_diff_flush(struct rev_info *);
1824
int log_tree_commit(struct rev_info *, struct commit *);
1925
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);
2728
void show_decorations(struct rev_info *opt, struct commit *commit);
2829
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
2930
const char **extra_headers_p,

pretty.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -1537,11 +1537,18 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
15371537
strbuf_addstr(sb, get_revision_mark(NULL, commit));
15381538
return 1;
15391539
case 'd':
1540-
format_decorations(sb, commit, c->auto_color);
1540+
format_decorations(sb, commit, c->auto_color, NULL);
15411541
return 1;
15421542
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+
}
15451552
case 'S': /* tag/branch like --source */
15461553
if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
15471554
return 0;

0 commit comments

Comments
 (0)