From 08d7d82f2c3714945dfe838279e7c6249ef22e9b Mon Sep 17 00:00:00 2001 From: George Dunlap Date: Fri, 18 Sep 2020 14:49:24 +0100 Subject: [PATCH 1/2] Relax email format requirements `git am` will accept patches which aren't fully RFC-compliant, as long as they start with `From` and have a `Subject` line. Relax PatchPatchHeader() so that it will accept such patches as well. Signed-off-by: George Dunlap --- gitdiff/patch_header.go | 16 +++++++++++----- gitdiff/patch_header_test.go | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/gitdiff/patch_header.go b/gitdiff/patch_header.go index ae50870..4386205 100644 --- a/gitdiff/patch_header.go +++ b/gitdiff/patch_header.go @@ -13,8 +13,9 @@ import ( ) const ( - mailHeaderPrefix = "From " - prettyHeaderPrefix = "commit " + mailHeaderPrefix = "From " + prettyHeaderPrefix = "commit " + mailMinimumHeaderPrefix = "From:" ) // PatchHeader is a parsed version of the preamble content that appears before @@ -210,6 +211,9 @@ func ParsePatchHeader(s string) (*PatchHeader, error) { switch { case strings.HasPrefix(line, mailHeaderPrefix): return parseHeaderMail(line, r) + case strings.HasPrefix(line, mailMinimumHeaderPrefix): + r = bufio.NewReader(strings.NewReader(s)) + return parseHeaderMail("", r) case strings.HasPrefix(line, prettyHeaderPrefix): return parseHeaderPretty(line, r) } @@ -368,9 +372,11 @@ func parseHeaderMail(mailLine string, r io.Reader) (*PatchHeader, error) { h := &PatchHeader{} - mailLine = mailLine[len(mailHeaderPrefix):] - if i := strings.IndexByte(mailLine, ' '); i > 0 { - h.SHA = mailLine[:i] + if len(mailLine) > len(mailHeaderPrefix) { + mailLine = mailLine[len(mailHeaderPrefix):] + if i := strings.IndexByte(mailLine, ' '); i > 0 { + h.SHA = mailLine[:i] + } } addrs, err := msg.Header.AddressList("From") diff --git a/gitdiff/patch_header_test.go b/gitdiff/patch_header_test.go index ca7e053..e7ee96a 100644 --- a/gitdiff/patch_header_test.go +++ b/gitdiff/patch_header_test.go @@ -289,6 +289,21 @@ CC: Joe Smith BodyAppendix: expectedBodyAppendix, }, }, + "mailboxMinimal": { + Input: `From: Morton Haypenny +Subject: [PATCH] A sample commit to test header parsing + +The medium format shows the body, which +may wrap on to multiple lines. + +Another body line. +`, + Header: PatchHeader{ + Author: expectedIdentity, + Title: expectedTitle, + Body: expectedBody, + }, + }, "unwrapTitle": { Input: `commit 61f5cd90bed4d204ee3feb3aa41ee91d4734855b Author: Morton Haypenny From 20b239603861a4b20aac1ebe492c7ba589a421da Mon Sep 17 00:00:00 2001 From: George Dunlap Date: Fri, 18 Sep 2020 15:04:17 +0100 Subject: [PATCH 2/2] Tolerate email-only From lines in mail `git am` will tolerate patches of the form: ``` From: ``` In this case, the `Author` field will contain `foo@company.com `. Duplicate this behavior. Signed-off-by: George Dunlap --- gitdiff/patch_header.go | 2 +- gitdiff/patch_header_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/gitdiff/patch_header.go b/gitdiff/patch_header.go index 4386205..16e3d3e 100644 --- a/gitdiff/patch_header.go +++ b/gitdiff/patch_header.go @@ -386,7 +386,7 @@ func parseHeaderMail(mailLine string, r io.Reader) (*PatchHeader, error) { if len(addrs) > 0 { addr := addrs[0] if addr.Name == "" { - return nil, fmt.Errorf("invalid user string: %s", addr) + addr.Name = addr.Address } h.Author = &PatchIdentity{Name: addr.Name, Email: addr.Address} } diff --git a/gitdiff/patch_header_test.go b/gitdiff/patch_header_test.go index e7ee96a..7dc7f13 100644 --- a/gitdiff/patch_header_test.go +++ b/gitdiff/patch_header_test.go @@ -289,6 +289,21 @@ CC: Joe Smith BodyAppendix: expectedBodyAppendix, }, }, + "mailboxMinimalNoName": { + Input: `From: +Subject: [PATCH] A sample commit to test header parsing + +The medium format shows the body, which +may wrap on to multiple lines. + +Another body line. +`, + Header: PatchHeader{ + Author: &PatchIdentity{expectedIdentity.Email, expectedIdentity.Email}, + Title: expectedTitle, + Body: expectedBody, + }, + }, "mailboxMinimal": { Input: `From: Morton Haypenny Subject: [PATCH] A sample commit to test header parsing