pack-objects: check return value from read_sha1_file()
[gitweb.git] / builtin-mailinfo.c
index d94578cb4ac0649913db1542f876d5010ece7f0f..b558754142b26399df78472b0bb124144dc194da 100644 (file)
@@ -237,8 +237,6 @@ static int eatspace(char *line)
 
 static char *cleanup_subject(char *subject)
 {
-       if (keep_subject)
-               return subject;
        for (;;) {
                char *p;
                int len, remove;
@@ -294,14 +292,14 @@ static char *header[MAX_HDR_PARSED] = {
        "From","Subject","Date",
 };
 
-static int check_header(char *line, char **hdr_data)
+static int check_header(char *line, char **hdr_data, int overwrite)
 {
        int i;
 
        /* search for the interesting parts */
        for (i = 0; header[i]; i++) {
                int len = strlen(header[i]);
-               if (!hdr_data[i] &&
+               if ((!hdr_data[i] || overwrite) &&
                    !strncasecmp(line, header[i], len) &&
                    line[len] == ':' && isspace(line[len + 1])) {
                        /* Unwrap inline B and Q encoding, and optionally
@@ -425,6 +423,7 @@ static int read_one_header_line(char *line, int sz, FILE *in)
                        if (addlen >= sz - len)
                                addlen = sz - len - 1;
                        memcpy(line + len, continuation, addlen);
+                       line[len] = '\n';
                        len += addlen;
                }
        }
@@ -499,15 +498,42 @@ static int decode_b_segment(char *in, char *ot, char *ep)
        return 0;
 }
 
+/*
+ * When there is no known charset, guess.
+ *
+ * Right now we assume that if the target is UTF-8 (the default),
+ * and it already looks like UTF-8 (which includes US-ASCII as its
+ * subset, of course) then that is what it is and there is nothing
+ * to do.
+ *
+ * Otherwise, we default to assuming it is Latin1 for historical
+ * reasons.
+ */
+static const char *guess_charset(const char *line, const char *target_charset)
+{
+       if (is_encoding_utf8(target_charset)) {
+               if (is_utf8(line))
+                       return NULL;
+       }
+       return "latin1";
+}
+
 static void convert_to_utf8(char *line, const char *charset)
 {
-       static const char latin_one[] = "latin1";
-       const char *input_charset = *charset ? charset : latin_one;
-       char *out = reencode_string(line, metainfo_charset, input_charset);
+       char *out;
+
+       if (!charset || !*charset) {
+               charset = guess_charset(line, metainfo_charset);
+               if (!charset)
+                       return;
+       }
 
+       if (!strcmp(metainfo_charset, charset))
+               return;
+       out = reencode_string(line, metainfo_charset, charset);
        if (!out)
                die("cannot convert from %s to %s\n",
-                   input_charset, metainfo_charset);
+                   charset, metainfo_charset);
        strcpy(line, out);
        free(out);
 }
@@ -614,6 +640,7 @@ static int find_boundary(void)
 
 static int handle_boundary(void)
 {
+       char newline[]="\n";
 again:
        if (!memcmp(line+content_top->boundary_len, "--", 2)) {
                /* we hit an end boundary */
@@ -628,7 +655,7 @@ static int handle_boundary(void)
                                        "can't recover\n");
                        exit(1);
                }
-               handle_filter("\n");
+               handle_filter(newline);
 
                /* skip to the next boundary */
                if (!find_boundary())
@@ -643,7 +670,7 @@ static int handle_boundary(void)
 
        /* slurp in this section's info */
        while (read_one_header_line(line, sizeof(line), fin))
-               check_header(line, p_hdr_data);
+               check_header(line, p_hdr_data, 0);
 
        /* eat the blank line after section info */
        return (fgets(line, sizeof(line), fin) != NULL);
@@ -699,10 +726,14 @@ static int handle_commit_msg(char *line)
                        if (!*cp)
                                return 0;
                }
-               if ((still_looking = check_header(cp, s_hdr_data)) != 0)
+               if ((still_looking = check_header(cp, s_hdr_data, 0)) != 0)
                        return 0;
        }
 
+       /* normalize the log message to UTF-8. */
+       if (metainfo_charset)
+               convert_to_utf8(line, charset);
+
        if (patchbreak(line)) {
                fclose(cmitmsg);
                cmitmsg = NULL;
@@ -767,12 +798,8 @@ static void handle_body(void)
                                return;
                }
 
-               /* Unwrap transfer encoding and optionally
-                * normalize the log message to UTF-8.
-                */
+               /* Unwrap transfer encoding */
                decode_transfer_encoding(line);
-               if (metainfo_charset)
-                       convert_to_utf8(line, charset);
 
                switch (transfer_encoding) {
                case TE_BASE64:
@@ -818,6 +845,22 @@ static void handle_body(void)
        return;
 }
 
+static void output_header_lines(FILE *fout, const char *hdr, char *data)
+{
+       while (1) {
+               char *ep = strchr(data, '\n');
+               int len;
+               if (!ep)
+                       len = strlen(data);
+               else
+                       len = ep - data;
+               fprintf(fout, "%s: %.*s\n", hdr, len, data);
+               if (!ep)
+                       break;
+               data = ep + 1;
+       }
+}
+
 static void handle_info(void)
 {
        char *sub;
@@ -835,9 +878,13 @@ static void handle_info(void)
                        continue;
 
                if (!memcmp(header[i], "Subject", 7)) {
-                       sub = cleanup_subject(hdr);
-                       cleanup_space(sub);
-                       fprintf(fout, "Subject: %s\n", sub);
+                       if (keep_subject)
+                               sub = hdr;
+                       else {
+                               sub = cleanup_subject(hdr);
+                               cleanup_space(sub);
+                       }
+                       output_header_lines(fout, "Subject", sub);
                } else if (!memcmp(header[i], "From", 4)) {
                        handle_from(hdr);
                        fprintf(fout, "Author: %s\n", name);
@@ -850,8 +897,8 @@ static void handle_info(void)
        fprintf(fout, "\n");
 }
 
-int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
-            const char *msg, const char *patch)
+static int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
+                   const char *msg, const char *patch)
 {
        keep_subject = ks;
        metainfo_charset = encoding;
@@ -875,7 +922,7 @@ int mailinfo(FILE *in, FILE *out, int ks, const char *encoding,
 
        /* process the email header */
        while (read_one_header_line(line, sizeof(line), fin))
-               check_header(line, p_hdr_data);
+               check_header(line, p_hdr_data, 1);
 
        handle_body();
        handle_info();