From: Andy Koppe <andy.koppe@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, glencbz@gmail.com, phillip.wood123@gmail.com,
Andy Koppe <andy.koppe@gmail.com>
Subject: [PATCH v5 6/8] pretty: add %(decorate[:<options>]) format
Date: Sun, 20 Aug 2023 19:50:07 +0100 [thread overview]
Message-ID: <20230820185009.20095-7-andy.koppe@gmail.com> (raw)
In-Reply-To: <20230820185009.20095-1-andy.koppe@gmail.com>
Add %(decorate[:<options>]) format that lists ref names similarly to the
%d format, but which allows the otherwise fixed prefix, suffix and
separator strings to be customized. Omitted options default to the
strings used in %d.
Rename expand_separator() function used to expand %x literal formatting
codes to expand_string_arg(), as it is now used on strings other than
separators.
Examples:
- %(decorate) is equivalent to %d.
- %(decorate:prefix=,suffix=) is equivalent to %D.
- %(decorate:prefix=[,suffix=],separator=%x3B) produces a list enclosed
in square brackets and separated by semicolons.
Test the format in t4205-log-pretty-formats.sh and document it in
pretty-formats.txt.
Signed-off-by: Andy Koppe <andy.koppe@gmail.com>
---
Documentation/pretty-formats.txt | 10 ++++++
pretty.c | 59 +++++++++++++++++++++++++++++---
t/t4205-log-pretty-formats.sh | 27 +++++++++++++++
3 files changed, 92 insertions(+), 4 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 851a9878e6..709d85af21 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -224,6 +224,16 @@ The placeholders are:
linkgit:git-rev-list[1])
'%d':: ref names, like the --decorate option of linkgit:git-log[1]
'%D':: ref names without the " (", ")" wrapping.
+'%(decorate[:<options>])'::
+ref names with custom decorations. The `decorate` string may be followed by a
+colon and zero or more comma-separated options. Option values may contain
+literal formatting codes. These must be used for commas (`%x2C`) and closing
+parentheses (`%x29`), due to their role in the option syntax.
++
+** 'prefix=<value>': Shown before the list of ref names. Defaults to "{nbsp}`(`".
+** 'suffix=<value>': Shown after the list of ref names. Defaults to "`)`".
+** 'separator=<value>': Shown between ref names. Defaults to "`,`{nbsp}".
+
'%(describe[:<options>])'::
human-readable name, like linkgit:git-describe[1]; empty string for
undescribable commits. The `describe` string may be followed by a colon and
diff --git a/pretty.c b/pretty.c
index 69b6db3340..1639efe2f8 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1252,8 +1252,8 @@ static int format_trailer_match_cb(const struct strbuf *key, void *ud)
return 0;
}
-static struct strbuf *expand_separator(struct strbuf *sb,
- const char *argval, size_t arglen)
+static struct strbuf *expand_string_arg(struct strbuf *sb,
+ const char *argval, size_t arglen)
{
char *fmt = xstrndup(argval, arglen);
const char *format = fmt;
@@ -1301,9 +1301,9 @@ int format_set_trailers_options(struct process_trailer_options *opts,
opts->filter_data = filter_list;
opts->only_trailers = 1;
} else if (match_placeholder_arg_value(*arg, "separator", arg, &argval, &arglen)) {
- opts->separator = expand_separator(sepbuf, argval, arglen);
+ opts->separator = expand_string_arg(sepbuf, argval, arglen);
} else if (match_placeholder_arg_value(*arg, "key_value_separator", arg, &argval, &arglen)) {
- opts->key_value_separator = expand_separator(kvsepbuf, argval, arglen);
+ opts->key_value_separator = expand_string_arg(kvsepbuf, argval, arglen);
} else if (!match_placeholder_bool_arg(*arg, "only", arg, &opts->only_trailers) &&
!match_placeholder_bool_arg(*arg, "unfold", arg, &opts->unfold) &&
!match_placeholder_bool_arg(*arg, "keyonly", arg, &opts->key_only) &&
@@ -1384,6 +1384,40 @@ static size_t parse_describe_args(const char *start, struct strvec *args)
return arg - start;
}
+
+static int parse_decoration_option(const char **arg,
+ const char *name,
+ char **opt)
+{
+ const char *argval;
+ size_t arglen;
+
+ if (match_placeholder_arg_value(*arg, name, arg, &argval, &arglen)) {
+ struct strbuf sb = STRBUF_INIT;
+
+ expand_string_arg(&sb, argval, arglen);
+ *opt = strbuf_detach(&sb, NULL);
+ return 1;
+ }
+ return 0;
+}
+
+static void parse_decoration_options(const char **arg,
+ struct decoration_options *opts)
+{
+ while (parse_decoration_option(arg, "prefix", &opts->prefix) ||
+ parse_decoration_option(arg, "suffix", &opts->suffix) ||
+ parse_decoration_option(arg, "separator", &opts->separator))
+ ;
+}
+
+static void free_decoration_options(const struct decoration_options *opts)
+{
+ free(opts->prefix);
+ free(opts->suffix);
+ free(opts->separator);
+}
+
static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
const char *placeholder,
void *context)
@@ -1645,6 +1679,23 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
return 2;
}
+ if (skip_prefix(placeholder, "(decorate", &arg)) {
+ struct decoration_options opts = { NULL };
+ size_t ret = 0;
+
+ if (*arg == ':') {
+ arg++;
+ parse_decoration_options(&arg, &opts);
+ }
+ if (*arg == ')') {
+ format_decorations(sb, commit, c->auto_color, &opts);
+ ret = arg - placeholder + 1;
+ }
+
+ free_decoration_options(&opts);
+ return ret;
+ }
+
/* For the rest we have to parse the commit header. */
if (!c->commit_header_parsed) {
msg = c->message =
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index dd9035aa38..6ba399c5be 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -576,6 +576,33 @@ test_expect_success 'clean log decoration' '
test_cmp expected actual1
'
+test_expect_success 'pretty format %decorate' '
+ git checkout -b foo &&
+ git commit --allow-empty -m "new commit" &&
+ git tag bar &&
+ git branch qux &&
+
+ echo " (HEAD -> foo, tag: bar, qux)" >expect1 &&
+ git log --format="%(decorate)" -1 >actual1 &&
+ test_cmp expect1 actual1 &&
+
+ echo "HEAD -> foo, tag: bar, qux" >expect2 &&
+ git log --format="%(decorate:prefix=,suffix=)" -1 >actual2 &&
+ test_cmp expect2 actual2 &&
+
+ echo "[ HEAD -> foo; tag: bar; qux ]" >expect3 &&
+ git log --format="%(decorate:prefix=[ ,suffix= ],separator=%x3B )" \
+ -1 >actual3 &&
+ test_cmp expect3 actual3 &&
+
+ # Try with a typo (in "separator"), in which case the placeholder should
+ # not be replaced.
+ echo "%(decorate:prefix=[ ,suffix= ],separater=; )" >expect4 &&
+ git log --format="%(decorate:prefix=[ ,suffix= ],separater=%x3B )" \
+ -1 >actual4 &&
+ test_cmp expect4 actual4
+'
+
cat >trailers <<EOF
Signed-off-by: A U Thor <author@example.com>
Acked-by: A U Thor <author@example.com>
--
2.42.0-rc2
next prev parent reply other threads:[~2023-08-20 18:56 UTC|newest]
Thread overview: 59+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-15 10:37 [PATCH] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-07-15 16:07 ` [PATCH v2] " Andy Koppe
2023-07-17 23:10 ` Junio C Hamano
2023-07-18 1:05 ` Junio C Hamano
2023-08-11 18:50 ` Andy Koppe
2023-07-19 18:16 ` Glen Choo
2023-07-23 16:25 ` Phillip Wood
2023-08-11 19:04 ` Andy Koppe
2023-08-11 20:38 ` Junio C Hamano
2023-08-11 22:06 ` Andy Koppe
2023-08-12 1:16 ` Junio C Hamano
2023-08-11 18:59 ` Andy Koppe
2023-08-15 18:13 ` Junio C Hamano
2023-08-15 18:28 ` Andy Koppe
2023-08-15 19:01 ` Junio C Hamano
2023-08-15 19:29 ` main != master at github.com/git/git Andy Koppe
2023-08-15 22:16 ` Taylor Blau
2023-08-16 2:24 ` Jeff King
2023-08-16 13:30 ` rsbecker
2023-08-18 0:35 ` Junio C Hamano
2023-08-21 14:56 ` Johannes Schindelin
2023-08-21 16:17 ` Junio C Hamano
2023-08-22 0:31 ` [PATCH] ci: avoid building from the same commit in parallel Junio C Hamano
2023-08-22 4:36 ` Junio C Hamano
2023-08-22 4:48 ` Johannes Schindelin
2023-08-22 15:31 ` Junio C Hamano
2023-08-23 8:42 ` Johannes Schindelin
2023-08-23 16:08 ` Junio C Hamano
2023-08-23 16:10 ` Junio C Hamano
2023-08-25 12:56 ` Johannes Schindelin
2023-08-10 21:16 ` [PATCH v3 1/7] pretty-formats: define "literal formatting code" Andy Koppe
2023-08-10 21:16 ` [PATCH v3 2/7] pretty-formats: enclose options in angle brackets Andy Koppe
2023-08-10 21:16 ` [PATCH v3 3/7] decorate: refactor format_decorations() Andy Koppe
2023-08-10 21:16 ` [PATCH v3 4/7] decorate: avoid some unnecessary color overhead Andy Koppe
2023-08-10 21:16 ` [PATCH v3 5/7] decorate: color each token separately Andy Koppe
2023-08-10 21:16 ` [PATCH v3 6/7] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-10 21:16 ` [PATCH v3 7/7] pretty: add pointer and tag options to %(decorate) Andy Koppe
2023-08-16 4:23 ` [PATCH v3 1/7] pretty-formats: define "literal formatting code" Junio C Hamano
2023-08-20 8:53 ` [PATCH v4 0/8] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-20 8:53 ` [PATCH v4 1/8] pretty-formats: define "literal formatting code" Andy Koppe
2023-08-20 8:53 ` [PATCH v4 2/8] pretty-formats: enclose options in angle brackets Andy Koppe
2023-08-20 8:53 ` [PATCH v4 3/8] decorate: refactor format_decorations() Andy Koppe
2023-08-20 8:53 ` [PATCH v4 4/8] decorate: avoid some unnecessary color overhead Andy Koppe
2023-08-20 8:53 ` [PATCH v4 5/8] decorate: color each token separately Andy Koppe
2023-08-20 8:53 ` [PATCH v4 6/8] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-20 8:53 ` [PATCH v4 7/8] pretty: add pointer and tag options to %(decorate) Andy Koppe
2023-08-20 8:53 ` [PATCH v4 8/8] decorate: use commit color for HEAD arrow Andy Koppe
2023-08-20 18:50 ` [PATCH v5 0/8] pretty: add %(decorate[:<options>]) format Andy Koppe
2023-08-20 18:50 ` [PATCH v5 1/8] pretty-formats: define "literal formatting code" Andy Koppe
2023-08-20 18:50 ` [PATCH v5 2/8] pretty-formats: enclose options in angle brackets Andy Koppe
2023-08-20 18:50 ` [PATCH v5 3/8] decorate: refactor format_decorations() Andy Koppe
2023-08-20 18:50 ` [PATCH v5 4/8] decorate: avoid some unnecessary color overhead Andy Koppe
2023-08-20 18:50 ` [PATCH v5 5/8] decorate: color each token separately Andy Koppe
2023-08-20 18:50 ` Andy Koppe [this message]
2023-08-20 18:50 ` [PATCH v5 7/8] pretty: add pointer and tag options to %(decorate) Andy Koppe
2023-08-20 18:50 ` [PATCH v5 8/8] decorate: use commit color for HEAD arrow Andy Koppe
2023-08-29 21:59 ` [PATCH v5 0/8] pretty: add %(decorate[:<options>]) format Junio C Hamano
2023-09-01 21:33 ` Andy Koppe
2023-08-21 19:01 ` [PATCH v4 " Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230820185009.20095-7-andy.koppe@gmail.com \
--to=andy.koppe@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=glencbz@gmail.com \
--cc=phillip.wood123@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).