diff options
author | Junio C Hamano <gitster@pobox.com> | 2015-10-19 22:40:57 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2015-10-21 16:01:43 -0700 |
commit | a2d2be35781ca1379eb4dae2e7651ac004eb878f (patch) | |
tree | 934c85c93dfaaaa97ef047468a79e5da8ffcc548 | |
parent | 4b98bae2cbc6bf49a5fd7bab330191d002e6e2fc (diff) | |
download | git-jc/mailinfo.tar.gz |
mailinfo: ignore in-body header that we do not care aboutjc/mailinfo
"git mailinfo" (hence "git am") understands some well-known headers,
like "Subject: ", "Date: " and "From: ", placed at the beginning of
the message body (and the "--scissors" can discard the part of the
body before a scissors-mark). However, some people throw other
kinds of header-looking things there, expecting them to be
discarded.
Finding and discarding anything that looks like RFC2822 header is
not a right solution. The body of the message may start with a line
that begins with a word followed by a colon that is a legitimate
part of the message that should not be discarded.
Instead, keep reading non-blank lines once we see an in-body header
at the beginning and discard them. Nobody will be insane enough to
reorder the headers to read like this:
Garbage-non-in-body-header: here
Subject: in-body subject
Here is the body of the commit log.
but it is common for lazy or misguided people to leave non-header
materials in-body like this:
From: Junio C Hamano <gitster@pobox.com>
Date: Mon, 28 Sep 2015 19:19:27 -0700
Subject: [PATCH] Git 2.6.1
MIME-Version: 1.0
Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r-- | mailinfo.c | 88 | ||||
-rwxr-xr-x | t/t5100-mailinfo.sh | 3 | ||||
-rw-r--r-- | t/t5100/info0018 | 5 | ||||
-rw-r--r-- | t/t5100/msg0018 | 2 | ||||
-rw-r--r-- | t/t5100/patch0018 | 6 | ||||
-rw-r--r-- | t/t5100/sample.mbox | 18 |
6 files changed, 90 insertions, 32 deletions
diff --git a/mailinfo.c b/mailinfo.c index e157ca6eb5..74d19aec0a 100644 --- a/mailinfo.c +++ b/mailinfo.c @@ -628,17 +628,71 @@ static int is_scissors_line(const struct strbuf *line) gap * 2 < perforation); } +static int is_rfc2822_header(const struct strbuf *line) +{ + /* + * The section that defines the loosest possible + * field name is "3.6.8 Optional fields". + * + * optional-field = field-name ":" unstructured CRLF + * field-name = 1*ftext + * ftext = %d33-57 / %59-126 + */ + int ch; + char *cp = line->buf; + + /* Count mbox From headers as headers */ + if (starts_with(cp, "From ") || starts_with(cp, ">From ")) + return 1; + + while ((ch = *cp++)) { + if (ch == ':') + return 1; + if ((33 <= ch && ch <= 57) || + (59 <= ch && ch <= 126)) + continue; + break; + } + return 0; +} + static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line) { + int is_empty_line; + assert(!mi->filter_stage); - if (mi->header_stage) { - if (!line->len || (line->len == 1 && line->buf[0] == '\n')) + is_empty_line = (!line->len || (line->len == 1 && line->buf[0] == '\n')); + if (mi->header_stage == 1) { + /* + * Haven't seen a known in-body header; discard an empty line. + */ + if (is_empty_line) return 0; } if (mi->use_inbody_headers && mi->header_stage) { - mi->header_stage = check_header(mi, line, mi->s_hdr_data, 0); + int is_known_header = check_header(mi, line, mi->s_hdr_data, 0); + + if (mi->header_stage == 2) { + /* + * an empty line after the in-body header block, + * or a line obviously not an attempt to invent + * an unsupported in-body header. + */ + if (is_empty_line || !is_rfc2822_header(line)) + mi->header_stage = 0; + if (is_empty_line) + return 0; + /* otherwise do not discard the line, but keep going */ + } else if (is_known_header) { + /* We know we are in the in-body header block now. */ + mi->header_stage = 2; + } else if (mi->header_stage != 2) { + /* garbage and we are not in the in-body header block */ + mi->header_stage = 0; + } + if (mi->header_stage) return 0; } else @@ -699,34 +753,6 @@ static void handle_filter(struct mailinfo *mi, struct strbuf *line) } } -static int is_rfc2822_header(const struct strbuf *line) -{ - /* - * The section that defines the loosest possible - * field name is "3.6.8 Optional fields". - * - * optional-field = field-name ":" unstructured CRLF - * field-name = 1*ftext - * ftext = %d33-57 / %59-126 - */ - int ch; - char *cp = line->buf; - - /* Count mbox From headers as headers */ - if (starts_with(cp, "From ") || starts_with(cp, ">From ")) - return 1; - - while ((ch = *cp++)) { - if (ch == ':') - return 1; - if ((33 <= ch && ch <= 57) || - (59 <= ch && ch <= 126)) - continue; - break; - } - return 0; -} - static int read_one_header_line(struct strbuf *line, FILE *in) { struct strbuf continuation = STRBUF_INIT; diff --git a/t/t5100-mailinfo.sh b/t/t5100-mailinfo.sh index e97cfb2ab8..3ce041bf65 100755 --- a/t/t5100-mailinfo.sh +++ b/t/t5100-mailinfo.sh @@ -11,7 +11,8 @@ test_expect_success 'split sample box' \ 'git mailsplit -o. "$TEST_DIRECTORY"/t5100/sample.mbox >last && last=`cat last` && echo total is $last && - test `cat last` = 17' + test `cat last` = 18 +' check_mailinfo () { mail=$1 opt=$2 diff --git a/t/t5100/info0018 b/t/t5100/info0018 new file mode 100644 index 0000000000..ec671fc2ad --- /dev/null +++ b/t/t5100/info0018 @@ -0,0 +1,5 @@ +Author: A U Thor +Email: a.u.thor@example.com +Subject: A E I O U +Date: Mon, 17 Sep 2012 14:23:49 -0700 + diff --git a/t/t5100/msg0018 b/t/t5100/msg0018 new file mode 100644 index 0000000000..2ee0900850 --- /dev/null +++ b/t/t5100/msg0018 @@ -0,0 +1,2 @@ +New content here + diff --git a/t/t5100/patch0018 b/t/t5100/patch0018 new file mode 100644 index 0000000000..35cf84c9a1 --- /dev/null +++ b/t/t5100/patch0018 @@ -0,0 +1,6 @@ +diff --git a/foo b/foo +index e69de29..d95f3ad 100644 +--- a/foo ++++ b/foo +@@ -0,0 +1 @@ ++New content diff --git a/t/t5100/sample.mbox b/t/t5100/sample.mbox index 8b2ae064c3..d7c58783d2 100644 --- a/t/t5100/sample.mbox +++ b/t/t5100/sample.mbox @@ -406,6 +406,7 @@ Subject: re: [PATCH] another patch From: A U Thor <a.u.thor@example.com> Subject: [PATCH] another patch + >Here is an empty patch from A U Thor. Hey you forgot the patch! @@ -699,3 +700,20 @@ index e69de29..d95f3ad 100644 +++ b/foo @@ -0,0 +1 @@ +New content +From nobody Mon Sep 17 00:00:00 2001 +From: A U Thor <a.u.thor@example.com> +Subject: Re: some discussion title +Date: Mon, 17 Sep 2012 14:23:49 -0700 + +Subject: A E I O U +MIME-VERSION: 1.0 +Garbage: Not a valid in-body header + +New content here + +diff --git a/foo b/foo +index e69de29..d95f3ad 100644 +--- a/foo ++++ b/foo +@@ -0,0 +1 @@ ++New content |