fork of go-gitdiff with jj support
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Accept empty emails in ParsePatchIdentity (#42)

Git is actually more lenient here than I thought. As long as the
identity contains the "<>" delimiters, Git will allow an empty email, so
we should accept the same thing. I also discovered that an identity with
only an email set will use the email as the name, so I've implemented
that behavior as well.

authored by

Billy Keyes and committed by
GitHub
3f2ea5c1 13e8639a

+34 -9
+14 -7
gitdiff/patch_header.go
··· 82 82 return fmt.Sprintf("%s <%s>", name, i.Email) 83 83 } 84 84 85 - // ParsePatchIdentity parses a patch identity string. A valid string contains a 86 - // non-empty name followed by an email address in angle brackets. Like Git, 87 - // ParsePatchIdentity does not require that the email address is valid or 88 - // properly formatted, only that it is non-empty. The name must not contain a 89 - // left angle bracket, '<', and the email address must not contain a right 90 - // angle bracket, '>'. 85 + // ParsePatchIdentity parses a patch identity string. A valid string contains 86 + // an optional name followed by an email address in angle brackets. The angle 87 + // brackets must always exist, but may enclose an empty address. At least one 88 + // of the name or the email address must be non-empty. If the string only 89 + // contains an email address, that value is also used as the name. 90 + // 91 + // The name must not contain a left angle bracket, '<', and the email address 92 + // must not contain a right angle bracket, '>'. Otherwise, there are no 93 + // restrictions on the format of either field. 91 94 func ParsePatchIdentity(s string) (PatchIdentity, error) { 92 95 var emailStart, emailEnd int 93 96 for i, c := range s { ··· 110 113 if emailStart > 0 && emailEnd > 0 { 111 114 email = strings.TrimSpace(s[emailStart:emailEnd]) 112 115 } 113 - if name == "" || email == "" { 116 + if name == "" && email != "" { 117 + name = email 118 + } 119 + 120 + if name == "" && email == "" { 114 121 return PatchIdentity{}, fmt.Errorf("invalid identity string: %s", s) 115 122 } 116 123
+20 -2
gitdiff/patch_header_test.go
··· 32 32 Email: "mhaypenny@example.com", 33 33 }, 34 34 }, 35 - "missingName": { 35 + "onlyEmail": { 36 36 Input: "<mhaypenny@example.com>", 37 - Err: "invalid identity", 37 + Output: PatchIdentity{ 38 + Name: "mhaypenny@example.com", 39 + Email: "mhaypenny@example.com", 40 + }, 41 + }, 42 + "emptyEmail": { 43 + Input: "Morton Haypenny <>", 44 + Output: PatchIdentity{ 45 + Name: "Morton Haypenny", 46 + Email: "", 47 + }, 38 48 }, 39 49 "missingEmail": { 40 50 Input: "Morton Haypenny", 51 + Err: "invalid identity", 52 + }, 53 + "missingNameAndEmptyEmail": { 54 + Input: "<>", 55 + Err: "invalid identity", 56 + }, 57 + "empty": { 58 + Input: "", 41 59 Err: "invalid identity", 42 60 }, 43 61 "unclosedEmail": {