diff options
author | Jeff King <peff@peff.net> | 2020-09-27 04:40:01 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-09-27 12:21:05 -0700 |
commit | f0939a0eb157cb23cd300d32ee0bff2f82aa5959 (patch) | |
tree | c39246541265cdd0d74f434bc0f6c05eaeb828cd /trailer.c | |
parent | shortlog: add grouping option (diff) | |
download | tgif-f0939a0eb157cb23cd300d32ee0bff2f82aa5959.tar.xz |
trailer: add interface for iterating over commit trailers
The trailer code knows how to parse out the trailers and re-format them,
but there's no easy way to iterate over the trailers (you can use
trailer_info, but you have to then do a bunch of extra parsing).
Let's add an iteration interface that makes this easy to do.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'trailer.c')
-rw-r--r-- | trailer.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -1185,3 +1185,39 @@ void format_trailers_from_commit(struct strbuf *out, const char *msg, format_trailer_info(out, &info, opts); trailer_info_release(&info); } + +void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) +{ + struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; + strbuf_init(&iter->key, 0); + strbuf_init(&iter->val, 0); + opts.no_divider = 1; + trailer_info_get(&iter->info, msg, &opts); + iter->cur = 0; +} + +int trailer_iterator_advance(struct trailer_iterator *iter) +{ + while (iter->cur < iter->info.trailer_nr) { + char *trailer = iter->info.trailers[iter->cur++]; + int separator_pos = find_separator(trailer, separators); + + if (separator_pos < 1) + continue; /* not a real trailer */ + + strbuf_reset(&iter->key); + strbuf_reset(&iter->val); + parse_trailer(&iter->key, &iter->val, NULL, + trailer, separator_pos); + unfold_value(&iter->val); + return 1; + } + return 0; +} + +void trailer_iterator_release(struct trailer_iterator *iter) +{ + trailer_info_release(&iter->info); + strbuf_release(&iter->val); + strbuf_release(&iter->key); +} |