From: Carl Worth <cworth@cworth.org>
To: git@vger.kernel.org
Cc: Carl Worth <cworth@cworth.org>
Subject: [PATCH 1/2] mailsplit: Remove any '>' characters used to escape From_ lines in mbox.
Date: Tue, 8 Jun 2010 13:02:28 -0700 [thread overview]
Message-ID: <1276027349-4064-1-git-send-email-cworth@cworth.org> (raw)
In-Reply-To: <87hbldjo0s.fsf@yoom.home.cworth.org>
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@cworth.org>
---
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
next parent reply other threads:[~2010-06-08 20:02 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <87hbldjo0s.fsf@yoom.home.cworth.org>
2010-06-08 20:02 ` Carl Worth [this message]
2010-06-08 20:02 ` [PATCH 2/2] Add test from From_-line escaping Carl Worth
2010-06-08 20:47 ` Make "git am" properly unescape lines matching ">>*From " Carl Worth
2010-06-08 20:54 ` H. Peter Anvin
2010-06-08 21:30 ` Carl Worth
2010-06-08 20:50 ` H. Peter Anvin
2010-06-08 21:52 ` Carl Worth
2010-06-08 22:10 ` H. Peter Anvin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1276027349-4064-1-git-send-email-cworth@cworth.org \
--to=cworth@cworth.org \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).