Read trailers from a file or from stdin, parse the trailers and then
put the result into a doubly linked list.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
Signed-off-by: Junio C Hamano <gits...@pobox.com>
---
 trailer.c | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)

diff --git a/trailer.c b/trailer.c
index f79a369..4ca9157 100644
--- a/trailer.c
+++ b/trailer.c
@@ -51,6 +51,14 @@ static size_t alnum_len(const char *buf, size_t len)
        return len;
 }
 
+static inline int contains_only_spaces(const char *str)
+{
+       const char *s = str;
+       while (*s && isspace(*s))
+               s++;
+       return !*s;
+}
+
 static void free_trailer_item(struct trailer_item *item)
 {
        free(item->conf.name);
@@ -509,3 +517,111 @@ 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. The has_blank_line parameter
+ * tells if there is a blank line before the trailers.
+ */
+static int find_trailer_start(struct strbuf **lines, int count, int 
*has_blank_line)
+{
+       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 ':'.
+        */
+       for (start = count - 1; start >= 0; start--) {
+               if (contains_only_spaces(lines[start]->buf)) {
+                       if (only_spaces)
+                               continue;
+                       *has_blank_line = 1;
+                       return start + 1;
+               }
+               if (strchr(lines[start]->buf, ':')) {
+                       if (only_spaces)
+                               only_spaces = 0;
+                       continue;
+               }
+               *has_blank_line = start == count - 1 ?
+                 0 : contains_only_spaces(lines[start + 1]->buf);
+               return count;
+       }
+
+       *has_blank_line = only_spaces ? count > 0 : 0;
+       return only_spaces ? count : start + 1;
+}
+
+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, has_blank_line, i;
+
+       /* Get the line count */
+       while (lines[count])
+               count++;
+
+       patch_start = find_patch_start(lines, count);
+       trailer_start = find_trailer_start(lines, patch_start, &has_blank_line);
+
+       /* Print lines before the trailers as is */
+       print_lines(lines, 0, trailer_start);
+
+       if (!has_blank_line)
+               printf("\n");
+
+       /* Parse trailer lines */
+       for (i = trailer_start; i < patch_start; i++) {
+               struct trailer_item *new = create_trailer_item(lines[i]->buf);
+               add_trailer_item(in_tok_first, in_tok_last, new);
+       }
+
+       return patch_start;
+}
-- 
1.9.1.636.g20d5f34


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to