In order to encode an email message in an mbox, a client must notice any lines in the email body that look like so-called From_ lines, (that is lines begin with "From "), and add a preceding '>' character. >From Jonathan de Boyne Pollard[*] we learn of two long-standing (since 1995 at least) conventions used for this escaping. The original "mboxo" format does only the escaping described above, which leads to unavoidable corruption of some messages. The newer "mboxrd" format also adds a '>' to any line originally beginning with one or more '>' characters followed by "From ". This ensures that the original email can be extracted without corruption. Git wasn't formerly un-escaping these lines in any case, so invocations of "git am" would lead to errant '>' characters in the commit message. Here, we now fix git-mailsplit to perform the necessary un-escaping. We assume mboxrd format, since designing for the original mboxo format would guarantee corruption in at least some cases. [*] http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/mail-mbox-formats.html Signed-off-by: Carl Worth <cworth@xxxxxxxxxx> --- builtin/mailsplit.c | 26 +++++++++++++++++++++++++- 1 files changed, 25 insertions(+), 1 deletions(-) diff --git a/builtin/mailsplit.c b/builtin/mailsplit.c index cdfc1b7..a3fb9f7 100644 --- a/builtin/mailsplit.c +++ b/builtin/mailsplit.c @@ -46,6 +46,30 @@ static int is_from_line(const char *line, int len) static struct strbuf buf = STRBUF_INIT; static int keep_cr; +/* Write the line in 'buf' to 'output', but if we are splitting an mbox, + * then remove the first '>' from any line that begins with one or more + * '>' characters followed by "From ". + * + * Return 0 if successful, 1 for any write error. + */ +static int write_buf_unescaping(FILE *output, int is_mbox) +{ + const char *line = buf.buf; + size_t len = buf.len; + + if (is_mbox && *line == '>') { + const char *s = line; + while (*s == '>') + s++; + if (strncmp (s, "From ", 5) == 0) { + line = line + 1; + len = len - 1; + } + } + + return fwrite(line, 1, len, output) != len; +} + /* Called with the first line (potentially partial) * already in buf[] -- normally that should begin with * the Unix "From " line. Write it into the specified @@ -76,7 +100,7 @@ static int split_one(FILE *mbox, const char *name, int allow_bare) strbuf_addch(&buf, '\n'); } - if (fwrite(buf.buf, 1, buf.len, output) != buf.len) + if (write_buf_unescaping(output, !is_bare)) die_errno("cannot write output"); if (strbuf_getwholeline(&buf, mbox, '\n')) { -- 1.7.0.4 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html