Merge branch 'jc/exec-cmd-system-path-leak-fix'
[gitweb.git] / builtin / mailinfo.c
index 2632fb037a6a285b17ca89911dc3da17cab231dd..c8a47c173d011713a57dc744a5f5e5ea5434eefa 100644 (file)
@@ -15,6 +15,7 @@ static const char *metainfo_charset;
 static struct strbuf line = STRBUF_INIT;
 static struct strbuf name = STRBUF_INIT;
 static struct strbuf email = STRBUF_INIT;
+static char *message_id;
 
 static enum  {
        TE_DONTCARE, TE_QP, TE_BASE64
@@ -24,6 +25,7 @@ static struct strbuf charset = STRBUF_INIT;
 static int patch_lines;
 static struct strbuf **p_hdr_data, **s_hdr_data;
 static int use_scissors;
+static int add_message_id;
 static int use_inbody_headers = 1;
 
 #define MAX_HDR_PARSED 10
@@ -198,6 +200,12 @@ static void handle_content_type(struct strbuf *line)
        }
 }
 
+static void handle_message_id(const struct strbuf *line)
+{
+       if (add_message_id)
+               message_id = strdup(line->buf);
+}
+
 static void handle_content_transfer_encoding(const struct strbuf *line)
 {
        if (strcasestr(line->buf, "base64"))
@@ -288,9 +296,10 @@ static inline int cmp_header(const struct strbuf *line, const char *hdr)
                        line->buf[len] == ':' && isspace(line->buf[len + 1]);
 }
 
-#define SAMPLE "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n"
 static int is_format_patch_separator(const char *line, int len)
 {
+       static const char SAMPLE[] =
+               "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
        const char *cp;
 
        if (len != strlen(SAMPLE))
@@ -341,6 +350,14 @@ static int check_header(const struct strbuf *line,
                ret = 1;
                goto check_header_out;
        }
+       if (cmp_header(line, "Message-Id")) {
+               len = strlen("Message-Id: ");
+               strbuf_add(&sb, line->buf + len, line->len - len);
+               decode_header(&sb);
+               handle_message_id(&sb);
+               ret = 1;
+               goto check_header_out;
+       }
 
        /* for inbody stuff */
        if (starts_with(line->buf, ">From") && isspace(line->buf[5])) {
@@ -815,6 +832,8 @@ static int handle_commit_msg(struct strbuf *line)
        }
 
        if (patchbreak(line)) {
+               if (message_id)
+                       fprintf(cmitmsg, "Message-Id: %s\n", message_id);
                fclose(cmitmsg);
                cmitmsg = NULL;
                return 1;
@@ -1012,7 +1031,7 @@ static int git_mailinfo_config(const char *var, const char *value, void *unused)
 }
 
 static const char mailinfo_usage[] =
-       "git mailinfo [-k|-b] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] msg patch < mail >info";
+       "git mailinfo [-k|-b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] msg patch < mail >info";
 
 int cmd_mailinfo(int argc, const char **argv, const char *prefix)
 {
@@ -1031,6 +1050,8 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
                        keep_subject = 1;
                else if (!strcmp(argv[1], "-b"))
                        keep_non_patch_brackets_in_subject = 1;
+               else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
+                       add_message_id = 1;
                else if (!strcmp(argv[1], "-u"))
                        metainfo_charset = def_charset;
                else if (!strcmp(argv[1], "-n"))