Skip to content

Commit

Permalink
Merge branch 'jt/mailinfo-fold-in-body-headers'
Browse files Browse the repository at this point in the history
When "git format-patch --stdout" output is placed as an in-body
header and it uses the RFC2822 header folding, "git am" failed to
put the header line back into a single logical line.  The
underlying "git mailinfo" was taught to handle this properly.

* jt/mailinfo-fold-in-body-headers:
  mailinfo: handle in-body header continuations
  mailinfo: make is_scissors_line take plain char *
  mailinfo: separate in-body header processing
  • Loading branch information
gitster committed Sep 29, 2016
2 parents 21f862b + 6b4b013 commit 4cff50b
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 36 deletions.
116 changes: 83 additions & 33 deletions mailinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,26 +495,26 @@ static int check_header(struct mailinfo *mi,
goto check_header_out;
}

/* for inbody stuff */
if (starts_with(line->buf, ">From") && isspace(line->buf[5])) {
ret = is_format_patch_separator(line->buf + 1, line->len - 1);
goto check_header_out;
}
if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
for (i = 0; header[i]; i++) {
if (!strcmp("Subject", header[i])) {
handle_header(&hdr_data[i], line);
ret = 1;
goto check_header_out;
}
}
}

check_header_out:
strbuf_release(&sb);
return ret;
}

/*
* Returns 1 if the given line or any line beginning with the given line is an
* in-body header (that is, check_header will succeed when passed
* mi->s_hdr_data).
*/
static int is_inbody_header(const struct mailinfo *mi,
const struct strbuf *line)
{
int i;
for (i = 0; header[i]; i++)
if (!mi->s_hdr_data[i] && cmp_header(line, header[i]))
return 1;
return 0;
}

static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line)
{
struct strbuf *ret;
Expand Down Expand Up @@ -572,37 +572,35 @@ static inline int patchbreak(const struct strbuf *line)
return 0;
}

static int is_scissors_line(const struct strbuf *line)
static int is_scissors_line(const char *line)
{
size_t i, len = line->len;
const char *c;
int scissors = 0, gap = 0;
int first_nonblank = -1;
int last_nonblank = 0, visible, perforation = 0, in_perforation = 0;
const char *buf = line->buf;
const char *first_nonblank = NULL, *last_nonblank = NULL;
int visible, perforation = 0, in_perforation = 0;

for (i = 0; i < len; i++) {
if (isspace(buf[i])) {
for (c = line; *c; c++) {
if (isspace(*c)) {
if (in_perforation) {
perforation++;
gap++;
}
continue;
}
last_nonblank = i;
if (first_nonblank < 0)
first_nonblank = i;
if (buf[i] == '-') {
last_nonblank = c;
if (first_nonblank == NULL)
first_nonblank = c;
if (*c == '-') {
in_perforation = 1;
perforation++;
continue;
}
if (i + 1 < len &&
(!memcmp(buf + i, ">8", 2) || !memcmp(buf + i, "8<", 2) ||
!memcmp(buf + i, ">%", 2) || !memcmp(buf + i, "%<", 2))) {
if ((!memcmp(c, ">8", 2) || !memcmp(c, "8<", 2) ||
!memcmp(c, ">%", 2) || !memcmp(c, "%<", 2))) {
in_perforation = 1;
perforation += 2;
scissors += 2;
i++;
c++;
continue;
}
in_perforation = 0;
Expand All @@ -617,12 +615,60 @@ static int is_scissors_line(const struct strbuf *line)
* than half of the perforation.
*/

visible = last_nonblank - first_nonblank + 1;
if (first_nonblank && last_nonblank)
visible = last_nonblank - first_nonblank + 1;
else
visible = 0;
return (scissors && 8 <= visible &&
visible < perforation * 3 &&
gap * 2 < perforation);
}

static void flush_inbody_header_accum(struct mailinfo *mi)
{
if (!mi->inbody_header_accum.len)
return;
assert(check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0));
strbuf_reset(&mi->inbody_header_accum);
}

static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
{
if (mi->inbody_header_accum.len &&
(line->buf[0] == ' ' || line->buf[0] == '\t')) {
if (mi->use_scissors && is_scissors_line(line->buf)) {
/*
* This is a scissors line; do not consider this line
* as a header continuation line.
*/
flush_inbody_header_accum(mi);
return 0;
}
strbuf_strip_suffix(&mi->inbody_header_accum, "\n");
strbuf_addbuf(&mi->inbody_header_accum, line);
return 1;
}

flush_inbody_header_accum(mi);

if (starts_with(line->buf, ">From") && isspace(line->buf[5]))
return is_format_patch_separator(line->buf + 1, line->len - 1);
if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
int i;
for (i = 0; header[i]; i++)
if (!strcmp("Subject", header[i])) {
handle_header(&mi->s_hdr_data[i], line);
return 1;
}
return 0;
}
if (is_inbody_header(mi, line)) {
strbuf_addbuf(&mi->inbody_header_accum, line);
return 1;
}
return 0;
}

static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
{
assert(!mi->filter_stage);
Expand All @@ -633,7 +679,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
}

if (mi->use_inbody_headers && mi->header_stage) {
mi->header_stage = check_header(mi, line, mi->s_hdr_data, 0);
mi->header_stage = check_inbody_header(mi, line);
if (mi->header_stage)
return 0;
} else
Expand All @@ -646,7 +692,7 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
if (convert_to_utf8(mi, line, mi->charset.buf))
return 0; /* mi->input_error already set */

if (mi->use_scissors && is_scissors_line(line)) {
if (mi->use_scissors && is_scissors_line(line->buf)) {
int i;

strbuf_setlen(&mi->log_message, 0);
Expand Down Expand Up @@ -886,6 +932,8 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
break;
} while (!strbuf_getwholeline(line, mi->input, '\n'));

flush_inbody_header_accum(mi);

handle_body_out:
strbuf_release(&prev);
}
Expand Down Expand Up @@ -1001,6 +1049,7 @@ void setup_mailinfo(struct mailinfo *mi)
strbuf_init(&mi->email, 0);
strbuf_init(&mi->charset, 0);
strbuf_init(&mi->log_message, 0);
strbuf_init(&mi->inbody_header_accum, 0);
mi->header_stage = 1;
mi->use_inbody_headers = 1;
mi->content_top = mi->content;
Expand All @@ -1014,6 +1063,7 @@ void clear_mailinfo(struct mailinfo *mi)
strbuf_release(&mi->name);
strbuf_release(&mi->email);
strbuf_release(&mi->charset);
strbuf_release(&mi->inbody_header_accum);
free(mi->message_id);

for (i = 0; mi->p_hdr_data[i]; i++)
Expand Down
1 change: 1 addition & 0 deletions mailinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct mailinfo {
int patch_lines;
int filter_stage; /* still reading log or are we copying patch? */
int header_stage; /* still checking in-body headers? */
struct strbuf inbody_header_accum;
struct strbuf **p_hdr_data;
struct strbuf **s_hdr_data;

Expand Down
23 changes: 23 additions & 0 deletions t/t4150-am.sh
Original file line number Diff line number Diff line change
Expand Up @@ -977,4 +977,27 @@ test_expect_success 'am --patch-format=mboxrd handles mboxrd' '
test_cmp msg out
'

test_expect_success 'am works with multi-line in-body headers' '
FORTY="String that has a length of more than forty characters" &&
LONG="$FORTY $FORTY" &&
rm -fr .git/rebase-apply &&
git checkout -f first &&
echo one >> file &&
git commit -am "$LONG" --author="$LONG <long@example.com>" &&
git format-patch --stdout -1 >patch &&
# bump from, date, and subject down to in-body header
perl -lpe "
if (/^From:/) {
print \"From: x <x\@example.com>\";
print \"Date: Sat, 1 Jan 2000 00:00:00 +0000\";
print \"Subject: x\n\";
}
" patch >msg &&
git checkout HEAD^ &&
git am msg &&
# Ensure that the author and full message are present
git cat-file commit HEAD | grep "^author.*long@example.com" &&
git cat-file commit HEAD | grep "^$LONG"
'

test_done
2 changes: 1 addition & 1 deletion t/t5100-mailinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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
Expand Down
5 changes: 5 additions & 0 deletions t/t5100/info0018
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Author: Another Thor
Email: a.thor@example.com
Subject: This one contains a tab and a space
Date: Fri, 9 Jun 2006 00:44:16 -0700

5 changes: 5 additions & 0 deletions t/t5100/info0018--no-inbody-headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Author: A U Thor
Email: a.u.thor@example.com
Subject: check multiline inbody headers
Date: Fri, 9 Jun 2006 00:44:16 -0700

2 changes: 0 additions & 2 deletions t/t5100/msg0015
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
- a list
- of stuff
2 changes: 2 additions & 0 deletions t/t5100/msg0018
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a commit message

8 changes: 8 additions & 0 deletions t/t5100/msg0018--no-inbody-headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
From: Another Thor
<a.thor@example.com>
Subject: This one contains
a tab
and a space

a commit message

6 changes: 6 additions & 0 deletions t/t5100/patch0018
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
diff --git a/foo b/foo
index e69de29..d95f3ad 100644
--- a/foo
+++ b/foo
@@ -0,0 +1 @@
+content
6 changes: 6 additions & 0 deletions t/t5100/patch0018--no-inbody-headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
diff --git a/foo b/foo
index e69de29..d95f3ad 100644
--- a/foo
+++ b/foo
@@ -0,0 +1 @@
+content
19 changes: 19 additions & 0 deletions t/t5100/sample.mbox
Original file line number Diff line number Diff line change
Expand Up @@ -699,3 +699,22 @@ 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: check multiline inbody headers
Date: Fri, 9 Jun 2006 00:44:16 -0700

From: Another Thor
<a.thor@example.com>
Subject: This one contains
a tab
and a space

a commit message

diff --git a/foo b/foo
index e69de29..d95f3ad 100644
--- a/foo
+++ b/foo
@@ -0,0 +1 @@
+content

0 comments on commit 4cff50b

Please sign in to comment.