Skip to content

Commit b846b2c

Browse files
gwdGeorge Dunlap
and
George Dunlap
authored
Relax email header requirements (#20)
`git am` will accept patches which aren't fully RFC-compliant, as long as they start with `From` and have a `Subject` line. Relax ParsePatchHeader() so that it will accept such patches as well. Specifically, it will tolerate patches of the form: From: <foo@company.com> In this case, the `Author` field will contain `foo@company.com <foo@company.com>`. Duplicate this behavior. Signed-off-by: George Dunlap <george.dunlap@citrix.com> Co-authored-by: George Dunlap <george.dunlap@citrix.com>
1 parent 1bd59c4 commit b846b2c

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

gitdiff/patch_header.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
)
1414

1515
const (
16-
mailHeaderPrefix = "From "
17-
prettyHeaderPrefix = "commit "
16+
mailHeaderPrefix = "From "
17+
prettyHeaderPrefix = "commit "
18+
mailMinimumHeaderPrefix = "From:"
1819
)
1920

2021
// PatchHeader is a parsed version of the preamble content that appears before
@@ -210,6 +211,9 @@ func ParsePatchHeader(s string) (*PatchHeader, error) {
210211
switch {
211212
case strings.HasPrefix(line, mailHeaderPrefix):
212213
return parseHeaderMail(line, r)
214+
case strings.HasPrefix(line, mailMinimumHeaderPrefix):
215+
r = bufio.NewReader(strings.NewReader(s))
216+
return parseHeaderMail("", r)
213217
case strings.HasPrefix(line, prettyHeaderPrefix):
214218
return parseHeaderPretty(line, r)
215219
}
@@ -368,9 +372,11 @@ func parseHeaderMail(mailLine string, r io.Reader) (*PatchHeader, error) {
368372

369373
h := &PatchHeader{}
370374

371-
mailLine = mailLine[len(mailHeaderPrefix):]
372-
if i := strings.IndexByte(mailLine, ' '); i > 0 {
373-
h.SHA = mailLine[:i]
375+
if len(mailLine) > len(mailHeaderPrefix) {
376+
mailLine = mailLine[len(mailHeaderPrefix):]
377+
if i := strings.IndexByte(mailLine, ' '); i > 0 {
378+
h.SHA = mailLine[:i]
379+
}
374380
}
375381

376382
addrs, err := msg.Header.AddressList("From")
@@ -380,7 +386,7 @@ func parseHeaderMail(mailLine string, r io.Reader) (*PatchHeader, error) {
380386
if len(addrs) > 0 {
381387
addr := addrs[0]
382388
if addr.Name == "" {
383-
return nil, fmt.Errorf("invalid user string: %s", addr)
389+
addr.Name = addr.Address
384390
}
385391
h.Author = &PatchIdentity{Name: addr.Name, Email: addr.Address}
386392
}

gitdiff/patch_header_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,36 @@ CC: Joe Smith <joe.smith@company.com>
289289
BodyAppendix: expectedBodyAppendix,
290290
},
291291
},
292+
"mailboxMinimalNoName": {
293+
Input: `From: <mhaypenny@example.com>
294+
Subject: [PATCH] A sample commit to test header parsing
295+
296+
The medium format shows the body, which
297+
may wrap on to multiple lines.
298+
299+
Another body line.
300+
`,
301+
Header: PatchHeader{
302+
Author: &PatchIdentity{expectedIdentity.Email, expectedIdentity.Email},
303+
Title: expectedTitle,
304+
Body: expectedBody,
305+
},
306+
},
307+
"mailboxMinimal": {
308+
Input: `From: Morton Haypenny <mhaypenny@example.com>
309+
Subject: [PATCH] A sample commit to test header parsing
310+
311+
The medium format shows the body, which
312+
may wrap on to multiple lines.
313+
314+
Another body line.
315+
`,
316+
Header: PatchHeader{
317+
Author: expectedIdentity,
318+
Title: expectedTitle,
319+
Body: expectedBody,
320+
},
321+
},
292322
"unwrapTitle": {
293323
Input: `commit 61f5cd90bed4d204ee3feb3aa41ee91d4734855b
294324
Author: Morton Haypenny <mhaypenny@example.com>

0 commit comments

Comments
 (0)