Imported Upstream version 2.30.0
[platform/upstream/git.git] / trailer.c
index 0796f32..3f7391d 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -221,15 +221,13 @@ static char *apply_command(const char *command, const char *arg)
        struct strbuf cmd = STRBUF_INIT;
        struct strbuf buf = STRBUF_INIT;
        struct child_process cp = CHILD_PROCESS_INIT;
-       const char *argv[] = {NULL, NULL};
        char *result;
 
        strbuf_addstr(&cmd, command);
        if (arg)
                strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
 
-       argv[0] = cmd.buf;
-       cp.argv = argv;
+       strvec_push(&cp.args, cmd.buf);
        cp.env = local_repo_env;
        cp.no_stdin = 1;
        cp.use_shell = 1;
@@ -1129,10 +1127,11 @@ static void format_trailer_info(struct strbuf *out,
                                const struct trailer_info *info,
                                const struct process_trailer_options *opts)
 {
+       size_t origlen = out->len;
        size_t i;
 
        /* If we want the whole block untouched, we can take the fast path. */
-       if (!opts->only_trailers && !opts->unfold) {
+       if (!opts->only_trailers && !opts->unfold && !opts->filter && !opts->separator) {
                strbuf_add(out, info->trailer_start,
                           info->trailer_end - info->trailer_start);
                return;
@@ -1147,15 +1146,29 @@ static void format_trailer_info(struct strbuf *out,
                        struct strbuf val = STRBUF_INIT;
 
                        parse_trailer(&tok, &val, NULL, trailer, separator_pos);
-                       if (opts->unfold)
-                               unfold_value(&val);
-
-                       strbuf_addf(out, "%s: %s\n", tok.buf, val.buf);
+                       if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
+                               if (opts->unfold)
+                                       unfold_value(&val);
+
+                               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);
+                               if (!opts->separator)
+                                       strbuf_addch(out, '\n');
+                       }
                        strbuf_release(&tok);
                        strbuf_release(&val);
 
                } else if (!opts->only_trailers) {
+                       if (opts->separator && out->len != origlen) {
+                               strbuf_addbuf(out, opts->separator);
+                       }
                        strbuf_addstr(out, trailer);
+                       if (opts->separator) {
+                               strbuf_rtrim(out);
+                       }
                }
        }
 
@@ -1170,3 +1183,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);
+}