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

Accept empty emails in ParsePatchIdentity #42

Merged
merged 1 commit into from
Mar 7, 2024
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
21 changes: 14 additions & 7 deletions gitdiff/patch_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ func (i PatchIdentity) String() string {
return fmt.Sprintf("%s <%s>", name, i.Email)
}

// ParsePatchIdentity parses a patch identity string. A valid string contains a
// non-empty name followed by an email address in angle brackets. Like Git,
// ParsePatchIdentity does not require that the email address is valid or
// properly formatted, only that it is non-empty. The name must not contain a
// left angle bracket, '<', and the email address must not contain a right
// angle bracket, '>'.
// ParsePatchIdentity parses a patch identity string. A valid string contains
// an optional name followed by an email address in angle brackets. The angle
// brackets must always exist, but may enclose an empty address. At least one
// of the name or the email address must be non-empty. If the string only
// contains an email address, that value is also used as the name.
//
// The name must not contain a left angle bracket, '<', and the email address
// must not contain a right angle bracket, '>'. Otherwise, there are no
// restrictions on the format of either field.
func ParsePatchIdentity(s string) (PatchIdentity, error) {
var emailStart, emailEnd int
for i, c := range s {
Expand All @@ -110,7 +113,11 @@ func ParsePatchIdentity(s string) (PatchIdentity, error) {
if emailStart > 0 && emailEnd > 0 {
email = strings.TrimSpace(s[emailStart:emailEnd])
}
if name == "" || email == "" {
if name == "" && email != "" {
name = email
}

if name == "" && email == "" {
return PatchIdentity{}, fmt.Errorf("invalid identity string: %s", s)
}

Expand Down
22 changes: 20 additions & 2 deletions gitdiff/patch_header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,32 @@ func TestParsePatchIdentity(t *testing.T) {
Email: "[email protected]",
},
},
"missingName": {
"onlyEmail": {
Input: "<[email protected]>",
Err: "invalid identity",
Output: PatchIdentity{
Name: "[email protected]",
Email: "[email protected]",
},
},
"emptyEmail": {
Input: "Morton Haypenny <>",
Output: PatchIdentity{
Name: "Morton Haypenny",
Email: "",
},
},
"missingEmail": {
Input: "Morton Haypenny",
Err: "invalid identity",
},
"missingNameAndEmptyEmail": {
Input: "<>",
Err: "invalid identity",
},
"empty": {
Input: "",
Err: "invalid identity",
},
"unclosedEmail": {
Input: "Morton Haypenny <[email protected]",
Err: "unclosed email",
Expand Down
Loading