t7501: add test of "commit --dry-run --short"
[gitweb.git] / mailinfo.c
index b4118a02757212871e3402532ab5c422a5ba043f..3281a37d51830a60e864c78dff16dbf4eba9d800 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "utf8.h"
 #include "strbuf.h"
 #include "mailinfo.h"
@@ -57,17 +58,17 @@ static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
 static const char *unquote_comment(struct strbuf *outbuf, const char *in)
 {
        int c;
-       int take_next_litterally = 0;
+       int take_next_literally = 0;
 
        strbuf_addch(outbuf, '(');
 
        while ((c = *in++) != 0) {
-               if (take_next_litterally == 1) {
-                       take_next_litterally = 0;
+               if (take_next_literally == 1) {
+                       take_next_literally = 0;
                } else {
                        switch (c) {
                        case '\\':
-                               take_next_litterally = 1;
+                               take_next_literally = 1;
                                continue;
                        case '(':
                                in = unquote_comment(outbuf, in);
@@ -87,15 +88,15 @@ static const char *unquote_comment(struct strbuf *outbuf, const char *in)
 static const char *unquote_quoted_string(struct strbuf *outbuf, const char *in)
 {
        int c;
-       int take_next_litterally = 0;
+       int take_next_literally = 0;
 
        while ((c = *in++) != 0) {
-               if (take_next_litterally == 1) {
-                       take_next_litterally = 0;
+               if (take_next_literally == 1) {
+                       take_next_literally = 0;
                } else {
                        switch (c) {
                        case '\\':
-                               take_next_litterally = 1;
+                               take_next_literally = 1;
                                continue;
                        case '"':
                                return in;
@@ -148,16 +149,14 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
        at = strchr(f.buf, '@');
        if (!at) {
                parse_bogus_from(mi, from);
-               return;
+               goto out;
        }
 
        /*
         * If we already have one email, don't take any confusing lines
         */
-       if (mi->email.len && strchr(at + 1, '@')) {
-               strbuf_release(&f);
-               return;
-       }
+       if (mi->email.len && strchr(at + 1, '@'))
+               goto out;
 
        /* Pick up the string around '@', possibly delimited with <>
         * pair; that is the email part.
@@ -197,6 +196,7 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
        }
 
        get_sane_name(&mi->name, &f, &mi->email);
+out:
        strbuf_release(&f);
 }
 
@@ -367,11 +367,16 @@ static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
 
        while ((c = *in++) != 0) {
                if (c == '=') {
-                       int d = *in++;
+                       int ch, d = *in;
                        if (d == '\n' || !d)
                                break; /* drop trailing newline */
-                       strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
-                       continue;
+                       ch = hex2chr(in);
+                       if (ch >= 0) {
+                               strbuf_addch(out, ch);
+                               in += 2;
+                               continue;
+                       }
+                       /* garbage -- fall through */
                }
                if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
                        c = 0x20;
@@ -577,26 +582,26 @@ static int check_header(struct mailinfo *mi,
                goto check_header_out;
        }
 
-       /* for inbody stuff */
-       if (starts_with(line->buf, ">From") && isspace(line->buf[5])) {
-               ret = is_format_patch_separator(line->buf + 1, line->len - 1);
-               goto check_header_out;
-       }
-       if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
-               for (i = 0; header[i]; i++) {
-                       if (!strcmp("Subject", header[i])) {
-                               handle_header(&hdr_data[i], line);
-                               ret = 1;
-                               goto check_header_out;
-                       }
-               }
-       }
-
 check_header_out:
        strbuf_release(&sb);
        return ret;
 }
 
+/*
+ * Returns 1 if the given line or any line beginning with the given line is an
+ * in-body header (that is, check_header will succeed when passed
+ * mi->s_hdr_data).
+ */
+static int is_inbody_header(const struct mailinfo *mi,
+                           const struct strbuf *line)
+{
+       int i;
+       for (i = 0; header[i]; i++)
+               if (!mi->s_hdr_data[i] && cmp_header(line, header[i]))
+                       return 1;
+       return 0;
+}
+
 static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line)
 {
        struct strbuf *ret;
@@ -654,37 +659,35 @@ static inline int patchbreak(const struct strbuf *line)
        return 0;
 }
 
-static int is_scissors_line(const struct strbuf *line)
+static int is_scissors_line(const char *line)
 {
-       size_t i, len = line->len;
+       const char *c;
        int scissors = 0, gap = 0;
-       int first_nonblank = -1;
-       int last_nonblank = 0, visible, perforation = 0, in_perforation = 0;
-       const char *buf = line->buf;
+       const char *first_nonblank = NULL, *last_nonblank = NULL;
+       int visible, perforation = 0, in_perforation = 0;
 
-       for (i = 0; i < len; i++) {
-               if (isspace(buf[i])) {
+       for (c = line; *c; c++) {
+               if (isspace(*c)) {
                        if (in_perforation) {
                                perforation++;
                                gap++;
                        }
                        continue;
                }
-               last_nonblank = i;
-               if (first_nonblank < 0)
-                       first_nonblank = i;
-               if (buf[i] == '-') {
+               last_nonblank = c;
+               if (first_nonblank == NULL)
+                       first_nonblank = c;
+               if (*c == '-') {
                        in_perforation = 1;
                        perforation++;
                        continue;
                }
-               if (i + 1 < len &&
-                   (!memcmp(buf + i, ">8", 2) || !memcmp(buf + i, "8<", 2) ||
-                    !memcmp(buf + i, ">%", 2) || !memcmp(buf + i, "%<", 2))) {
+               if ((!memcmp(c, ">8", 2) || !memcmp(c, "8<", 2) ||
+                    !memcmp(c, ">%", 2) || !memcmp(c, "%<", 2))) {
                        in_perforation = 1;
                        perforation += 2;
                        scissors += 2;
-                       i++;
+                       c++;
                        continue;
                }
                in_perforation = 0;
@@ -699,23 +702,77 @@ static int is_scissors_line(const struct strbuf *line)
         * than half of the perforation.
         */
 
-       visible = last_nonblank - first_nonblank + 1;
+       if (first_nonblank && last_nonblank)
+               visible = last_nonblank - first_nonblank + 1;
+       else
+               visible = 0;
        return (scissors && 8 <= visible &&
                visible < perforation * 3 &&
                gap * 2 < perforation);
 }
 
+static void flush_inbody_header_accum(struct mailinfo *mi)
+{
+       if (!mi->inbody_header_accum.len)
+               return;
+       if (!check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0))
+               BUG("inbody_header_accum, if not empty, must always contain a valid in-body header");
+       strbuf_reset(&mi->inbody_header_accum);
+}
+
+static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
+{
+       if (mi->inbody_header_accum.len &&
+           (line->buf[0] == ' ' || line->buf[0] == '\t')) {
+               if (mi->use_scissors && is_scissors_line(line->buf)) {
+                       /*
+                        * This is a scissors line; do not consider this line
+                        * as a header continuation line.
+                        */
+                       flush_inbody_header_accum(mi);
+                       return 0;
+               }
+               strbuf_strip_suffix(&mi->inbody_header_accum, "\n");
+               strbuf_addbuf(&mi->inbody_header_accum, line);
+               return 1;
+       }
+
+       flush_inbody_header_accum(mi);
+
+       if (starts_with(line->buf, ">From") && isspace(line->buf[5]))
+               return is_format_patch_separator(line->buf + 1, line->len - 1);
+       if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
+               int i;
+               for (i = 0; header[i]; i++)
+                       if (!strcmp("Subject", header[i])) {
+                               handle_header(&mi->s_hdr_data[i], line);
+                               return 1;
+                       }
+               return 0;
+       }
+       if (is_inbody_header(mi, line)) {
+               strbuf_addbuf(&mi->inbody_header_accum, line);
+               return 1;
+       }
+       return 0;
+}
+
 static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
 {
        assert(!mi->filter_stage);
 
        if (mi->header_stage) {
-               if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
+               if (!line->len || (line->len == 1 && line->buf[0] == '\n')) {
+                       if (mi->inbody_header_accum.len) {
+                               flush_inbody_header_accum(mi);
+                               mi->header_stage = 0;
+                       }
                        return 0;
+               }
        }
 
        if (mi->use_inbody_headers && mi->header_stage) {
-               mi->header_stage = check_header(mi, line, mi->s_hdr_data, 0);
+               mi->header_stage = check_inbody_header(mi, line);
                if (mi->header_stage)
                        return 0;
        } else
@@ -728,7 +785,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
        if (convert_to_utf8(mi, line, mi->charset.buf))
                return 0; /* mi->input_error already set */
 
-       if (mi->use_scissors && is_scissors_line(line)) {
+       if (mi->use_scissors && is_scissors_line(line->buf)) {
                int i;
 
                strbuf_setlen(&mi->log_message, 0);
@@ -770,6 +827,7 @@ static void handle_filter(struct mailinfo *mi, struct strbuf *line)
                if (!handle_commit_msg(mi, line))
                        break;
                mi->filter_stage++;
+               /* fallthrough */
        case 1:
                handle_patch(mi, line);
                break;
@@ -830,7 +888,10 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
        for (;;) {
                int peek;
 
-               peek = fgetc(in); ungetc(peek, in);
+               peek = fgetc(in);
+               if (peek == EOF)
+                       break;
+               ungetc(peek, in);
                if (peek != ' ' && peek != '\t')
                        break;
                if (strbuf_getline_lf(&continuation, in))
@@ -864,8 +925,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
                /* we hit an end boundary */
                /* pop the current boundary off the stack */
                strbuf_release(*(mi->content_top));
-               free(*(mi->content_top));
-               *(mi->content_top) = NULL;
+               FREE_AND_NULL(*(mi->content_top));
 
                /* technically won't happen as is_multipart_boundary()
                   will fail first.  But just in case..
@@ -874,6 +934,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
                        error("Detected mismatched boundaries, can't recover");
                        mi->input_error = -1;
                        mi->content_top = mi->content;
+                       strbuf_release(&newline);
                        return 0;
                }
                handle_filter(mi, &newline);
@@ -968,6 +1029,8 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
                        break;
        } while (!strbuf_getwholeline(line, mi->input, '\n'));
 
+       flush_inbody_header_accum(mi);
+
 handle_body_out:
        strbuf_release(&prev);
 }
@@ -1045,6 +1108,10 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
 
        do {
                peek = fgetc(mi->input);
+               if (peek == EOF) {
+                       fclose(cmitmsg);
+                       return error("empty patch: '%s'", patch);
+               }
        } while (isspace(peek));
        ungetc(peek, mi->input);
 
@@ -1083,6 +1150,7 @@ void setup_mailinfo(struct mailinfo *mi)
        strbuf_init(&mi->email, 0);
        strbuf_init(&mi->charset, 0);
        strbuf_init(&mi->log_message, 0);
+       strbuf_init(&mi->inbody_header_accum, 0);
        mi->header_stage = 1;
        mi->use_inbody_headers = 1;
        mi->content_top = mi->content;
@@ -1096,13 +1164,16 @@ void clear_mailinfo(struct mailinfo *mi)
        strbuf_release(&mi->name);
        strbuf_release(&mi->email);
        strbuf_release(&mi->charset);
+       strbuf_release(&mi->inbody_header_accum);
        free(mi->message_id);
 
-       for (i = 0; mi->p_hdr_data[i]; i++)
-               strbuf_release(mi->p_hdr_data[i]);
+       if (mi->p_hdr_data)
+               for (i = 0; mi->p_hdr_data[i]; i++)
+                       strbuf_release(mi->p_hdr_data[i]);
        free(mi->p_hdr_data);
-       for (i = 0; mi->s_hdr_data[i]; i++)
-               strbuf_release(mi->s_hdr_data[i]);
+       if (mi->s_hdr_data)
+               for (i = 0; mi->s_hdr_data[i]; i++)
+                       strbuf_release(mi->s_hdr_data[i]);
        free(mi->s_hdr_data);
 
        while (mi->content < mi->content_top) {