mailinfo: handle in-body header continuations
Mailinfo currently handles multi-line headers, but it does not handle multi-line in-body headers. Teach it to handle such headers, for example, for this input: From: author <author@example.com> Date: Fri, 9 Jun 2006 00:44:16 -0700 Subject: a very long broken line Subject: another very long broken line interpret the in-body subject to be "another very long broken line" instead of "another very long". An existing test (t/t5100/msg0015) has an indented line immediately after an in-body header - it has been modified to reflect the new functionality. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Tan committed
Sep 20, 2016 at 10:17 UTC
6b4b013f1884a3b5e67877d65a9f1da598ab4a6f
12 files changed
+125
-4
mailinfo.c
+49
-1
@@ -500,6 +500,21 @@ check_header_out:
500
return ret;
501
}
502
503
+/*
504
+ * Returns 1 if the given line or any line beginning with the given line is an
505
+ * in-body header (that is, check_header will succeed when passed
506
+ * mi->s_hdr_data).
507
+ */
508
+static int is_inbody_header(const struct mailinfo *mi,
509
+ const struct strbuf *line)
510
+{
511
+ int i;
512
+ for (i = 0; header[i]; i++)
513
+ if (!mi->s_hdr_data[i] && cmp_header(line, header[i]))
514
+ return 1;
515
+ return 0;
516
+}
517
+
518
static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line)
519
{
520
struct strbuf *ret;
@@ -609,8 +624,33 @@ static int is_scissors_line(const char *line)
624
gap * 2 < perforation);
625
}
626
627
+static void flush_inbody_header_accum(struct mailinfo *mi)
628
+{
629
+ if (!mi->inbody_header_accum.len)
630
+ return;
631
+ assert(check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0));
632
+ strbuf_reset(&mi->inbody_header_accum);
633
+}
634
+
635
static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
636
{
637
+ if (mi->inbody_header_accum.len &&
638
+ (line->buf[0] == ' ' || line->buf[0] == '\t')) {
639
+ if (mi->use_scissors && is_scissors_line(line->buf)) {
640
+ /*
641
+ * This is a scissors line; do not consider this line
642
+ * as a header continuation line.
643
+ */
644
+ flush_inbody_header_accum(mi);
645
+ return 0;
646
+ }
647
+ strbuf_strip_suffix(&mi->inbody_header_accum, "\n");
648
+ strbuf_addbuf(&mi->inbody_header_accum, line);
649
+ return 1;
650
+ }
651
+
652
+ flush_inbody_header_accum(mi);
653
+
654
if (starts_with(line->buf, ">From") && isspace(line->buf[5]))
655
return is_format_patch_separator(line->buf + 1, line->len - 1);
656
if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
@@ -622,7 +662,11 @@ static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
662
}
663
return 0;
664
}
625
- return check_header(mi, line, mi->s_hdr_data, 0);
665
+ if (is_inbody_header(mi, line)) {
666
+ strbuf_addbuf(&mi->inbody_header_accum, line);
667
+ return 1;
668
+ }
669
+ return 0;
670
}
671
672
static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
@@ -888,6 +932,8 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
932
break;
933
} while (!strbuf_getwholeline(line, mi->input, '\n'));
934
935
+ flush_inbody_header_accum(mi);
936
+
937
handle_body_out:
938
strbuf_release(&prev);
939
}
@@ -1003,6 +1049,7 @@ void setup_mailinfo(struct mailinfo *mi)
1049
strbuf_init(&mi->email, 0);
1050
strbuf_init(&mi->charset, 0);
1051
strbuf_init(&mi->log_message, 0);
1052
+ strbuf_init(&mi->inbody_header_accum, 0);
1053
mi->header_stage = 1;
1054
mi->use_inbody_headers = 1;
1055
mi->content_top = mi->content;
@@ -1016,6 +1063,7 @@ void clear_mailinfo(struct mailinfo *mi)
1063
strbuf_release(&mi->name);
1064
strbuf_release(&mi->email);
1065
strbuf_release(&mi->charset);
1066
+ strbuf_release(&mi->inbody_header_accum);
1067
free(mi->message_id);
1068
1069
for (i = 0; mi->p_hdr_data[i]; i++)
mailinfo.h
+1
@@ -27,6 +27,7 @@ struct mailinfo {
27
int patch_lines;
28
int filter_stage; /* still reading log or are we copying patch? */
29
int header_stage; /* still checking in-body headers? */
30
+ struct strbuf inbody_header_accum;
31
struct strbuf **p_hdr_data;
32
struct strbuf **s_hdr_data;
33
t/t4150-am.sh
+23
@@ -977,4 +977,27 @@ test_expect_success 'am --patch-format=mboxrd handles mboxrd' '
977
test_cmp msg out
978
'
979
980
+test_expect_success 'am works with multi-line in-body headers' '
981
+ FORTY="String that has a length of more than forty characters" &&
982
+ LONG="$FORTY $FORTY" &&
983
+ rm -fr .git/rebase-apply &&
984
+ git checkout -f first &&
985
+ echo one >> file &&
986
+ git commit -am "$LONG" --author="$LONG <long@example.com>" &&
987
+ git format-patch --stdout -1 >patch &&
988
+ # bump from, date, and subject down to in-body header
989
+ perl -lpe "
990
+ if (/^From:/) {
991
+ print \"From: x <x\@example.com>\";
992
+ print \"Date: Sat, 1 Jan 2000 00:00:00 +0000\";
993
+ print \"Subject: x\n\";
994
+ }
995
+ " patch >msg &&
996
+ git checkout HEAD^ &&
997
+ git am msg &&
998
+ # Ensure that the author and full message are present
999
+ git cat-file commit HEAD | grep "^author.*long@example.com" &&
1000
+ git cat-file commit HEAD | grep "^$LONG"
1001
+'
1002
+
1003
test_done
t/t5100-mailinfo.sh
+1
-1
@@ -11,7 +11,7 @@ test_expect_success 'split sample box' \
11
'git mailsplit -o. "$TEST_DIRECTORY"/t5100/sample.mbox >last &&
12
last=$(cat last) &&
13
echo total is $last &&
14
- test $(cat last) = 17'
14
+ test $(cat last) = 18'
15
16
check_mailinfo () {
17
mail=$1 opt=$2
t/t5100/info0018
new
+5
@@ -0,0 +1,5 @@
1
+Author: Another Thor
2
+Email: a.thor@example.com
3
+Subject: This one contains a tab and a space
4
+Date: Fri, 9 Jun 2006 00:44:16 -0700
5
+
t/t5100/info0018--no-inbody-headers
new
+5
@@ -0,0 +1,5 @@
1
+Author: A U Thor
2
+Email: a.u.thor@example.com
3
+Subject: check multiline inbody headers
4
+Date: Fri, 9 Jun 2006 00:44:16 -0700
5
+
t/t5100/msg0015
-2
@@ -1,2 +0,0 @@
1
- - a list
2
- - of stuff
t/t5100/msg0018
new
+2
@@ -0,0 +1,2 @@
1
+a commit message
2
+
t/t5100/msg0018--no-inbody-headers
new
+8
@@ -0,0 +1,8 @@
1
+From: Another Thor
2
+ <a.thor@example.com>
3
+Subject: This one contains
4
+ a tab
5
+ and a space
6
+
7
+a commit message
8
+
t/t5100/patch0018
new
+6
@@ -0,0 +1,6 @@
1
+diff --git a/foo b/foo
2
+index e69de29..d95f3ad 100644
3
+--- a/foo
4
++++ b/foo
5
+@@ -0,0 +1 @@
6
++content
t/t5100/patch0018--no-inbody-headers
new
+6
@@ -0,0 +1,6 @@
1
+diff --git a/foo b/foo
2
+index e69de29..d95f3ad 100644
3
+--- a/foo
4
++++ b/foo
5
+@@ -0,0 +1 @@
6
++content
t/t5100/sample.mbox
+19
@@ -699,3 +699,22 @@ index e69de29..d95f3ad 100644
699
+++ b/foo
700
@@ -0,0 +1 @@
701
+New content
702
+From nobody Mon Sep 17 00:00:00 2001
703
+From: A U Thor <a.u.thor@example.com>
704
+Subject: check multiline inbody headers
705
+Date: Fri, 9 Jun 2006 00:44:16 -0700
706
+
707
+From: Another Thor
708
+ <a.thor@example.com>
709
+Subject: This one contains
710
+ a tab
711
+ and a space
712
+
713
+a commit message
714
+
715
+diff --git a/foo b/foo
716
+index e69de29..d95f3ad 100644
717
+--- a/foo
718
++++ b/foo
719
+@@ -0,0 +1 @@
720
++content