]> git.ipfire.org Git - thirdparty/git.git/commitdiff
trailer: add interface for iterating over commit trailers
authorJeff King <peff@peff.net>
Sun, 27 Sep 2020 08:40:01 +0000 (04:40 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sun, 27 Sep 2020 19:21:05 +0000 (12:21 -0700)
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>
trailer.c
trailer.h

index 0c414f2fed2b5701f616eb2d88a386e5da16fda8..deee6551cea442b6901ce02cc0ce073442612f9a 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -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);
+}
index 203acf4ee1db99ea35a1d202010834330ce4af58..cd93e7ddea789cb6c320af2a7b984bd6f8a8215f 100644 (file)
--- a/trailer.h
+++ b/trailer.h
@@ -2,8 +2,7 @@
 #define TRAILER_H
 
 #include "list.h"
-
-struct strbuf;
+#include "strbuf.h"
 
 enum trailer_where {
        WHERE_DEFAULT,
@@ -103,4 +102,46 @@ void trailer_info_release(struct trailer_info *info);
 void format_trailers_from_commit(struct strbuf *out, const char *msg,
                                 const struct process_trailer_options *opts);
 
+/*
+ * An interface for iterating over the trailers found in a particular commit
+ * message. Use like:
+ *
+ *   struct trailer_iterator iter;
+ *   trailer_iterator_init(&iter, msg);
+ *   while (trailer_iterator_advance(&iter))
+ *      ... do something with iter.key and iter.val ...
+ *   trailer_iterator_release(&iter);
+ */
+struct trailer_iterator {
+       struct strbuf key;
+       struct strbuf val;
+
+       /* private */
+       struct trailer_info info;
+       size_t cur;
+};
+
+/*
+ * Initialize "iter" in preparation for walking over the trailers in the commit
+ * message "msg". The "msg" pointer must remain valid until the iterator is
+ * released.
+ *
+ * After initializing, note that key/val will not yet point to any trailer.
+ * Call advance() to parse the first one (if any).
+ */
+void trailer_iterator_init(struct trailer_iterator *iter, const char *msg);
+
+/*
+ * Advance to the next trailer of the iterator. Returns 0 if there is no such
+ * trailer, and 1 otherwise. The key and value of the trailer can be
+ * fetched from the iter->key and iter->value fields (which are valid
+ * only until the next advance).
+ */
+int trailer_iterator_advance(struct trailer_iterator *iter);
+
+/*
+ * Release all resources associated with the trailer iteration.
+ */
+void trailer_iterator_release(struct trailer_iterator *iter);
+
 #endif /* TRAILER_H */