transport: simplify duplicating a substring in transport_get() using xmemdupz()
[gitweb.git] / trailer.c
index b5666b3a72eb2e79d2cf3753ee6a607b472147f3..a905f5c50cef099c23d8a05acefc279168de69ec 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -1,5 +1,8 @@
 #include "cache.h"
 #include "string-list.h"
+#include "run-command.h"
+#include "string-list.h"
+#include "trailer.h"
 /*
  * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
  */
@@ -32,6 +35,8 @@ static struct trailer_item *first_conf_item;
 
 static char *separators = ":";
 
+#define TRAILER_ARG_STRING "$ARG"
+
 static int after_or_end(enum action_where where)
 {
        return (where == WHERE_AFTER) || (where == WHERE_END);
@@ -69,6 +74,21 @@ static int same_trailer(struct trailer_item *a, struct trailer_item *b)
        return same_token(a, b) && same_value(a, b);
 }
 
+static inline int contains_only_spaces(const char *str)
+{
+       const char *s = str;
+       while (*s && isspace(*s))
+               s++;
+       return !*s;
+}
+
+static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
+{
+       const char *ptr = strstr(sb->buf, a);
+       if (ptr)
+               strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
+}
+
 static void free_trailer_item(struct trailer_item *item)
 {
        free(item->conf.name);
@@ -79,6 +99,35 @@ static void free_trailer_item(struct trailer_item *item)
        free(item);
 }
 
+static char last_non_space_char(const char *s)
+{
+       int i;
+       for (i = strlen(s) - 1; i >= 0; i--)
+               if (!isspace(s[i]))
+                       return s[i];
+       return '\0';
+}
+
+static void print_tok_val(const char *tok, const char *val)
+{
+       char c = last_non_space_char(tok);
+       if (!c)
+               return;
+       if (strchr(separators, c))
+               printf("%s%s\n", tok, val);
+       else
+               printf("%s%c %s\n", tok, separators[0], val);
+}
+
+static void print_all(struct trailer_item *first, int trim_empty)
+{
+       struct trailer_item *item;
+       for (item = first; item; item = item->next) {
+               if (!trim_empty || strlen(item->value) > 0)
+                       print_tok_val(item->token, item->value);
+       }
+}
+
 static void update_last(struct trailer_item **last)
 {
        if (*last)
@@ -165,6 +214,62 @@ static struct trailer_item *remove_first(struct trailer_item **first)
        return item;
 }
 
+static int read_from_command(struct child_process *cp, struct strbuf *buf)
+{
+       if (run_command(cp))
+               return error("running trailer command '%s' failed", cp->argv[0]);
+       if (strbuf_read(buf, cp->out, 1024) < 1)
+               return error("reading from trailer command '%s' failed", cp->argv[0]);
+       strbuf_trim(buf);
+       return 0;
+}
+
+static const 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};
+       const char *result;
+
+       strbuf_addstr(&cmd, command);
+       if (arg)
+               strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
+
+       argv[0] = cmd.buf;
+       cp.argv = argv;
+       cp.env = local_repo_env;
+       cp.no_stdin = 1;
+       cp.out = -1;
+       cp.use_shell = 1;
+
+       if (read_from_command(&cp, &buf)) {
+               strbuf_release(&buf);
+               result = xstrdup("");
+       } else
+               result = strbuf_detach(&buf, NULL);
+
+       strbuf_release(&cmd);
+       return result;
+}
+
+static void apply_item_command(struct trailer_item *in_tok, struct trailer_item *arg_tok)
+{
+       if (arg_tok->conf.command) {
+               const char *arg;
+               if (arg_tok->value && arg_tok->value[0]) {
+                       arg = arg_tok->value;
+               } else {
+                       if (in_tok && in_tok->value)
+                               arg = xstrdup(in_tok->value);
+                       else
+                               arg = xstrdup("");
+               }
+               arg_tok->value = apply_command(arg_tok->conf.command, arg);
+               free((char *)arg);
+       }
+}
+
 static void apply_arg_if_exists(struct trailer_item *in_tok,
                                struct trailer_item *arg_tok,
                                struct trailer_item *on_tok,
@@ -176,16 +281,19 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
                free_trailer_item(arg_tok);
                break;
        case EXISTS_REPLACE:
+               apply_item_command(in_tok, arg_tok);
                add_arg_to_input_list(on_tok, arg_tok,
                                      in_tok_first, in_tok_last);
                remove_from_list(in_tok, in_tok_first, in_tok_last);
                free_trailer_item(in_tok);
                break;
        case EXISTS_ADD:
+               apply_item_command(in_tok, arg_tok);
                add_arg_to_input_list(on_tok, arg_tok,
                                      in_tok_first, in_tok_last);
                break;
        case EXISTS_ADD_IF_DIFFERENT:
+               apply_item_command(in_tok, arg_tok);
                if (check_if_different(in_tok, arg_tok, 1))
                        add_arg_to_input_list(on_tok, arg_tok,
                                              in_tok_first, in_tok_last);
@@ -193,6 +301,7 @@ static void apply_arg_if_exists(struct trailer_item *in_tok,
                        free_trailer_item(arg_tok);
                break;
        case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
+               apply_item_command(in_tok, arg_tok);
                if (check_if_different(on_tok, arg_tok, 0))
                        add_arg_to_input_list(on_tok, arg_tok,
                                              in_tok_first, in_tok_last);
@@ -216,6 +325,7 @@ static void apply_arg_if_missing(struct trailer_item **in_tok_first,
        case MISSING_ADD:
                where = arg_tok->conf.where;
                in_tok = after_or_end(where) ? in_tok_last : in_tok_first;
+               apply_item_command(NULL, arg_tok);
                if (*in_tok) {
                        add_arg_to_input_list(*in_tok, arg_tok,
                                              in_tok_first, in_tok_last);
@@ -472,8 +582,12 @@ static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tra
        strbuf_addch(&seps, '=');
        len = strcspn(trailer, seps.buf);
        strbuf_release(&seps);
-       if (len == 0)
-               return error(_("empty trailer token in trailer '%s'"), trailer);
+       if (len == 0) {
+               int l = strlen(trailer);
+               while (l > 0 && isspace(trailer[l - 1]))
+                       l--;
+               return error(_("empty trailer token in trailer '%.*s'"), l, trailer);
+       }
        if (len < strlen(trailer)) {
                strbuf_add(tok, trailer, len);
                strbuf_trim(tok);
@@ -499,7 +613,7 @@ static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
                                             char *tok, char *val)
 {
        struct trailer_item *new = xcalloc(sizeof(*new), 1);
-       new->value = val;
+       new->value = val ? val : xstrdup("");
 
        if (conf_item) {
                duplicate_conf(&new->conf, &conf_item->conf);
@@ -566,7 +680,17 @@ static struct trailer_item *process_command_line_args(struct string_list *traile
        struct trailer_item *arg_tok_first = NULL;
        struct trailer_item *arg_tok_last = NULL;
        struct string_list_item *tr;
+       struct trailer_item *item;
+
+       /* Add a trailer item for each configured trailer with a command */
+       for (item = first_conf_item; item; item = item->next) {
+               if (item->conf.command) {
+                       struct trailer_item *new = new_trailer_item(item, NULL, NULL);
+                       add_trailer_item(&arg_tok_first, &arg_tok_last, new);
+               }
+       }
 
+       /* Add a trailer item for each trailer on the command line */
        for_each_string_list_item(tr, trailers) {
                struct trailer_item *new = create_trailer_item(tr->string);
                add_trailer_item(&arg_tok_first, &arg_tok_last, new);
@@ -574,3 +698,159 @@ static struct trailer_item *process_command_line_args(struct string_list *traile
 
        return arg_tok_first;
 }
+
+static struct strbuf **read_input_file(const char *file)
+{
+       struct strbuf **lines;
+       struct strbuf sb = STRBUF_INIT;
+
+       if (file) {
+               if (strbuf_read_file(&sb, file, 0) < 0)
+                       die_errno(_("could not read input file '%s'"), file);
+       } else {
+               if (strbuf_read(&sb, fileno(stdin), 0) < 0)
+                       die_errno(_("could not read from stdin"));
+       }
+
+       lines = strbuf_split(&sb, '\n');
+
+       strbuf_release(&sb);
+
+       return lines;
+}
+
+/*
+ * Return the (0 based) index of the start of the patch or the line
+ * count if there is no patch in the message.
+ */
+static int find_patch_start(struct strbuf **lines, int count)
+{
+       int i;
+
+       /* Get the start of the patch part if any */
+       for (i = 0; i < count; i++) {
+               if (starts_with(lines[i]->buf, "---"))
+                       return i;
+       }
+
+       return count;
+}
+
+/*
+ * Return the (0 based) index of the first trailer line or count if
+ * there are no trailers. Trailers are searched only in the lines from
+ * index (count - 1) down to index 0.
+ */
+static int find_trailer_start(struct strbuf **lines, int count)
+{
+       int start, only_spaces = 1;
+
+       /*
+        * Get the start of the trailers by looking starting from the end
+        * for a line with only spaces before lines with one separator.
+        */
+       for (start = count - 1; start >= 0; start--) {
+               if (lines[start]->buf[0] == comment_line_char)
+                       continue;
+               if (contains_only_spaces(lines[start]->buf)) {
+                       if (only_spaces)
+                               continue;
+                       return start + 1;
+               }
+               if (strcspn(lines[start]->buf, separators) < lines[start]->len) {
+                       if (only_spaces)
+                               only_spaces = 0;
+                       continue;
+               }
+               return count;
+       }
+
+       return only_spaces ? count : 0;
+}
+
+static int has_blank_line_before(struct strbuf **lines, int start)
+{
+       for (;start >= 0; start--) {
+               if (lines[start]->buf[0] == comment_line_char)
+                       continue;
+               return contains_only_spaces(lines[start]->buf);
+       }
+       return 0;
+}
+
+static void print_lines(struct strbuf **lines, int start, int end)
+{
+       int i;
+       for (i = start; lines[i] && i < end; i++)
+               printf("%s", lines[i]->buf);
+}
+
+static int process_input_file(struct strbuf **lines,
+                             struct trailer_item **in_tok_first,
+                             struct trailer_item **in_tok_last)
+{
+       int count = 0;
+       int patch_start, trailer_start, i;
+
+       /* Get the line count */
+       while (lines[count])
+               count++;
+
+       patch_start = find_patch_start(lines, count);
+       trailer_start = find_trailer_start(lines, patch_start);
+
+       /* Print lines before the trailers as is */
+       print_lines(lines, 0, trailer_start);
+
+       if (!has_blank_line_before(lines, trailer_start - 1))
+               printf("\n");
+
+       /* Parse trailer lines */
+       for (i = trailer_start; i < patch_start; i++) {
+               if (lines[i]->buf[0] != comment_line_char) {
+                       struct trailer_item *new = create_trailer_item(lines[i]->buf);
+                       add_trailer_item(in_tok_first, in_tok_last, new);
+               }
+       }
+
+       return patch_start;
+}
+
+static void free_all(struct trailer_item **first)
+{
+       while (*first) {
+               struct trailer_item *item = remove_first(first);
+               free_trailer_item(item);
+       }
+}
+
+void process_trailers(const char *file, int trim_empty, struct string_list *trailers)
+{
+       struct trailer_item *in_tok_first = NULL;
+       struct trailer_item *in_tok_last = NULL;
+       struct trailer_item *arg_tok_first;
+       struct strbuf **lines;
+       int patch_start;
+
+       /* Default config must be setup first */
+       git_config(git_trailer_default_config, NULL);
+       git_config(git_trailer_config, NULL);
+
+       lines = read_input_file(file);
+
+       /* Print the lines before the trailers */
+       patch_start = process_input_file(lines, &in_tok_first, &in_tok_last);
+
+       arg_tok_first = process_command_line_args(trailers);
+
+       process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
+
+       print_all(in_tok_first, trim_empty);
+
+       free_all(&in_tok_first);
+
+       /* Print the lines after the trailers as is */
+       print_lines(lines, patch_start, INT_MAX);
+
+       strbuf_list_free(lines);
+}