pretty: add pointer and tag options to %(decorate)

Add pointer and tag options to %(decorate) format, to allow to override the " -> " string used to show where HEAD points and the "tag: " string used to mark tags. Document in pretty-formats.txt and test in t4205-log-pretty-formats.sh. 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 f1f8a258567268974b9bbd012c33ad219a31aa0e
5 files changed +31 -5
Documentation/pretty-formats.txt
+9
@@ -233,6 +233,15 @@ parentheses (`%x29`), due to their role in the option syntax.
233 ** 'prefix=<value>': Shown before the list of ref names. Defaults to "{nbsp}`(`".
234 ** 'suffix=<value>': Shown after the list of ref names. Defaults to "`)`".
235 ** 'separator=<value>': Shown between ref names. Defaults to "`,`{nbsp}".
236 +** 'pointer=<value>': Shown between HEAD and the branch it points to, if any.
237 + Defaults to "{nbsp}`->`{nbsp}".
238 +** 'tag=<value>': Shown before tag names. Defaults to "`tag:`{nbsp}".
239 +
240 ++
241 +For example, to produce decorations with no wrapping
242 +or tag annotations, and spaces as separators:
243 ++
244 +`%(decorate:prefix=,suffix=,tag=,separator= )`
245
246 '%(describe[:<options>])'::
247 human-readable name, like linkgit:git-describe[1]; empty string for
log-tree.c
+9 -3
@@ -317,6 +317,8 @@ void format_decorations(struct strbuf *sb,
317 const char *prefix = " (";
318 const char *suffix = ")";
319 const char *separator = ", ";
320 + const char *pointer = " -> ";
321 + const char *tag = "tag: ";
322
323 decoration = get_name_decoration(&commit->object);
324 if (!decoration)
@@ -329,6 +331,10 @@ void format_decorations(struct strbuf *sb,
331 suffix = opts->suffix;
332 if (opts->separator)
333 separator = opts->separator;
334 + if (opts->pointer)
335 + pointer = opts->pointer;
336 + if (opts->tag)
337 + tag = opts->tag;
338 }
339
340 color_commit = diff_get_color(use_color, DIFF_COMMIT);
@@ -351,9 +357,9 @@ void format_decorations(struct strbuf *sb,
357 strbuf_addstr(sb, color_reset);
358 }
359
354 - if (decoration->type == DECORATION_REF_TAG) {
360 + if (*tag && decoration->type == DECORATION_REF_TAG) {
361 strbuf_addstr(sb, color);
356 - strbuf_addstr(sb, "tag: ");
362 + strbuf_addstr(sb, tag);
363 strbuf_addstr(sb, color_reset);
364 }
365
@@ -364,7 +370,7 @@ void format_decorations(struct strbuf *sb,
370 if (current_and_HEAD &&
371 decoration->type == DECORATION_REF_HEAD) {
372 strbuf_addstr(sb, color);
367 - strbuf_addstr(sb, " -> ");
373 + strbuf_addstr(sb, pointer);
374 strbuf_addstr(sb, color_reset);
375 strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
376 show_name(sb, current_and_HEAD);
log-tree.h
+2
@@ -17,6 +17,8 @@ struct decoration_options {
17 char *prefix;
18 char *suffix;
19 char *separator;
20 + char *pointer;
21 + char *tag;
22 };
23
24 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
pretty.c
+5 -1
@@ -1407,7 +1407,9 @@ static void parse_decoration_options(const char **arg,
1407 {
1408 while (parse_decoration_option(arg, "prefix", &opts->prefix) ||
1409 parse_decoration_option(arg, "suffix", &opts->suffix) ||
1410 - parse_decoration_option(arg, "separator", &opts->separator))
1410 + parse_decoration_option(arg, "separator", &opts->separator) ||
1411 + parse_decoration_option(arg, "pointer", &opts->pointer) ||
1412 + parse_decoration_option(arg, "tag", &opts->tag))
1413 ;
1414 }
1415
@@ -1416,6 +1418,8 @@ static void free_decoration_options(const struct decoration_options *opts)
1418 free(opts->prefix);
1419 free(opts->suffix);
1420 free(opts->separator);
1421 + free(opts->pointer);
1422 + free(opts->tag);
1423 }
1424
1425 static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
t/t4205-log-pretty-formats.sh
+6 -1
@@ -600,7 +600,12 @@ test_expect_success 'pretty format %decorate' '
600 echo "%(decorate:prefix=[ ,suffix= ],separater=; )" >expect4 &&
601 git log --format="%(decorate:prefix=[ ,suffix= ],separater=%x3B )" \
602 -1 >actual4 &&
603 - test_cmp expect4 actual4
603 + test_cmp expect4 actual4 &&
604 +
605 + echo "HEAD->foo bar qux" >expect5 &&
606 + git log --format="%(decorate:prefix=,suffix=,separator= ,tag=,pointer=->)" \
607 + -1 >actual5 &&
608 + test_cmp expect5 actual5
609 '
610
611 cat >trailers <<EOF