summaryrefslogtreecommitdiff
path: root/trailer.c
diff options
context:
space:
mode:
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>2020-12-09 15:52:07 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-12-09 22:16:42 (GMT)
commit9d87d5ae026433a2993fdb757fc80dbdcb078310 (patch)
tree342403fcb77eb8f7945f02ab653e9c21007a9d93 /trailer.c
parent8b966a0506f8b5aef7c6038fe518db45bc4a1852 (diff)
downloadgit-9d87d5ae026433a2993fdb757fc80dbdcb078310.zip
git-9d87d5ae026433a2993fdb757fc80dbdcb078310.tar.gz
git-9d87d5ae026433a2993fdb757fc80dbdcb078310.tar.bz2
pretty format %(trailers): add a "keyonly"
Add support for a "keyonly". This allows for easier parsing out of the key and value. Before if you didn't want to make assumptions about how the key was formatted. You'd need to parse it out as e.g.: --pretty=format:'%H%x00%(trailers:separator=%x00%x00)' \ '%x00%(trailers:separator=%x00%x00,valueonly)' And then proceed to deduce keys by looking at those two and subtracting the value plus the hardcoded ": " separator from the non-valueonly %(trailers) line. Now it's possible to simply do: --pretty=format:'%H%x00%(trailers:separator=%x00%x00,keyonly)' \ '%x00%(trailers:separator=%x00%x00,valueonly)' Which at least reduces it to a state machine where you get N keys and correlate them with N values. Even better would be to have a way to change the ": " delimiter to something easily machine-readable (a key might contain ": " too). A follow-up change will add support for that. I don't really have a use-case for just "keyonly" myself. I suppose it would be useful in some cases as "key=*" matches case-insensitively, so a plain "keyonly" will give you the variants of the keys you matched. I'm mainly adding it to fix the inconsistency with "valueonly". Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'trailer.c')
-rw-r--r--trailer.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/trailer.c b/trailer.c
index d2d0101..889b419 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1132,7 +1132,7 @@ static void format_trailer_info(struct strbuf *out,
/* If we want the whole block untouched, we can take the fast path. */
if (!opts->only_trailers && !opts->unfold && !opts->filter &&
- !opts->separator && !opts->value_only) {
+ !opts->separator && !opts->key_only && !opts->value_only) {
strbuf_add(out, info->trailer_start,
info->trailer_end - info->trailer_start);
return;
@@ -1154,8 +1154,11 @@ static void format_trailer_info(struct strbuf *out,
if (opts->separator && out->len != origlen)
strbuf_addbuf(out, opts->separator);
if (!opts->value_only)
- strbuf_addf(out, "%s: ", tok.buf);
- strbuf_addbuf(out, &val);
+ strbuf_addbuf(out, &tok);
+ if (!opts->key_only && !opts->value_only)
+ strbuf_addstr(out, ": ");
+ if (!opts->key_only)
+ strbuf_addbuf(out, &val);
if (!opts->separator)
strbuf_addch(out, '\n');
}