Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working/relax email requirements #20

Merged
merged 2 commits into from
Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions gitdiff/patch_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand All @@ -380,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}
}
Expand Down
30 changes: 30 additions & 0 deletions gitdiff/patch_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,36 @@ CC: Joe Smith <joe.smith@company.com>
BodyAppendix: expectedBodyAppendix,
},
},
"mailboxMinimalNoName": {
Input: `From: <mhaypenny@example.com>
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 <mhaypenny@example.com>
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 <mhaypenny@example.com>
Expand Down