From: "Martin Ågren" <martin.agren@gmail.com>
To: git@vger.kernel.org
Cc: Andy Koppe <andy.koppe@gmail.com>
Subject: [PATCH 8/8] pretty: refactor parsing of decoration options
Date: Wed, 19 Mar 2025 08:23:41 +0100 [thread overview]
Message-ID: <f4d0d5c00ab7d314d19d82335d7381959ee6fb41.1742367347.git.martin.agren@gmail.com> (raw)
In-Reply-To: <cover.1742367347.git.martin.agren@gmail.com>
After having spotted "%(decorate", we see if there's a ':' and, if so,
reach out to `parse_decoration_options()`. We then verify there's a
closing ')' before actually considering the placeholder valid. Pull the
handling of ':' and ')' into `parse_decoration_options()` so that it's
more of a one-stop shop for handling everything after "%(decorate". Let
this include freeing up resources in the error path to make it really
easy to use this function.
Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
pretty.c | 52 ++++++++++++++++++++++++++++++----------------------
1 file changed, 30 insertions(+), 22 deletions(-)
diff --git a/pretty.c b/pretty.c
index ddc7fd6aab..d5a8ceb7ef 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1430,17 +1430,6 @@ static int parse_decoration_option(const char **arg,
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) ||
- parse_decoration_option(arg, "pointer", &opts->pointer) ||
- parse_decoration_option(arg, "tag", &opts->tag))
- ;
-}
-
static void free_decoration_options(const struct decoration_options *opts)
{
free(opts->prefix);
@@ -1450,6 +1439,30 @@ static void free_decoration_options(const struct decoration_options *opts)
free(opts->tag);
}
+static int parse_decoration_options(const char **arg,
+ struct decoration_options *opts)
+{
+ memset(opts, 0, sizeof(*opts));
+
+ if (**arg == ':') {
+ (*arg)++;
+ while (parse_decoration_option(arg, "prefix", &opts->prefix) ||
+ parse_decoration_option(arg, "suffix", &opts->suffix) ||
+ parse_decoration_option(arg, "separator", &opts->separator) ||
+ parse_decoration_option(arg, "pointer", &opts->pointer) ||
+ parse_decoration_option(arg, "tag", &opts->tag))
+ ;
+ }
+
+ if (**arg != ')') {
+ free_decoration_options(opts);
+ return -1;
+ }
+ (*arg)++;
+
+ return 0;
+}
+
static size_t parse_rewrap(const char *placeholder, struct rewrap_args *rewrap)
{
unsigned long width = 0, indent1 = 0, indent2 = 0;
@@ -1735,20 +1748,15 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
}
if (skip_prefix(placeholder, "(decorate", &arg)) {
- struct decoration_options opts = { NULL };
- size_t ret = 0;
+ struct decoration_options opts;
- if (*arg == ':') {
- arg++;
- parse_decoration_options(&arg, &opts);
- }
- if (*arg == ')') {
- format_decorations(sb, commit, c->auto_color, &opts);
- ret = arg - placeholder + 1;
- }
+ if (parse_decoration_options(&arg, &opts) < 0)
+ return 0;
+
+ format_decorations(sb, commit, c->auto_color, &opts);
free_decoration_options(&opts);
- return ret;
+ return arg - placeholder;
}
/* For the rest we have to parse the commit header. */
--
2.49.0.472.ge94155a9ec
prev parent reply other threads:[~2025-03-19 7:25 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-19 7:23 [PATCH 0/8] pretty: minor bugfixing, some refactorings Martin Ågren
2025-03-19 7:23 ` [PATCH 1/8] pretty: tighten function signature to not take `void *` Martin Ågren
2025-03-20 9:18 ` Patrick Steinhardt
2025-03-19 7:23 ` [PATCH 2/8] pretty: simplify if-else to reduce code duplication Martin Ågren
2025-03-20 9:17 ` Patrick Steinhardt
2025-03-20 16:10 ` Martin Ågren
2025-03-24 3:50 ` Jeff King
2025-03-19 7:23 ` [PATCH 3/8] pretty: collect padding-related fields in separate struct Martin Ågren
2025-03-19 7:23 ` [PATCH 4/8] pretty: fix parsing of half-valid "%<" and "%>" placeholders Martin Ågren
2025-03-20 9:18 ` Patrick Steinhardt
2025-03-20 16:11 ` Martin Ågren
2025-03-24 10:10 ` Patrick Steinhardt
2025-03-19 7:23 ` [PATCH 5/8] pretty: after padding, reset padding info Martin Ågren
2025-03-20 9:18 ` Patrick Steinhardt
2025-03-20 16:11 ` Martin Ågren
2025-03-19 7:23 ` [PATCH 6/8] pretty: refactor parsing of line-wrapping "%w" placeholder Martin Ågren
2025-03-20 9:18 ` Patrick Steinhardt
2025-03-20 16:11 ` Martin Ågren
2025-03-19 7:23 ` [PATCH 7/8] pretty: refactor parsing of magic Martin Ågren
2025-03-20 9:18 ` Patrick Steinhardt
2025-03-20 16:12 ` Martin Ågren
2025-03-19 7:23 ` Martin Ågren [this message]
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=f4d0d5c00ab7d314d19d82335d7381959ee6fb41.1742367347.git.martin.agren@gmail.com \
--to=martin.agren@gmail.com \
--cc=andy.koppe@gmail.com \
--cc=git@vger.kernel.org \
/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).