@@ -190,35 +190,35 @@ func ParsePatchDate(s string) (time.Time, error) {
190190// prefix and appendix material should use `PatchHeader.SubjectPrefix
191191// + PatchHeader.Title + "\n" + PatchHeader.Body + "\n" +
192192// PatchHeader.BodyAppendix`.
193- func ParsePatchHeader (s string ) (* PatchHeader , error ) {
194- r := bufio .NewReader (strings .NewReader (s ))
195-
196- var line string
197- for {
198- var err error
199- line , err = r .ReadString ('\n' )
200- if err == io .EOF {
201- break
202- }
203- if err != nil {
204- return nil , err
205- }
193+ func ParsePatchHeader (header string ) (* PatchHeader , error ) {
194+ header = strings .TrimSpace (header )
206195
207- line = strings .TrimSpace (line )
208- if len (line ) > 0 {
209- break
210- }
196+ if header == "" {
197+ return & PatchHeader {}, nil
198+ }
199+
200+ var firstLine , rest string
201+ if idx := strings .IndexByte (header , '\n' ); idx >= 0 {
202+ firstLine = header [:idx ]
203+ rest = header [idx + 1 :]
204+ } else {
205+ firstLine = header
206+ rest = ""
211207 }
212208
213209 switch {
214- case strings .HasPrefix (line , mailHeaderPrefix ):
215- return parseHeaderMail (line , r )
216- case strings .HasPrefix (line , mailMinimumHeaderPrefix ):
217- r = bufio .NewReader (strings .NewReader (s ))
218- return parseHeaderMail ("" , r )
219- case strings .HasPrefix (line , prettyHeaderPrefix ):
220- return parseHeaderPretty (line , r )
210+ case strings .HasPrefix (firstLine , mailHeaderPrefix ):
211+ return parseHeaderMail (firstLine , strings .NewReader (rest ))
212+
213+ case strings .HasPrefix (firstLine , mailMinimumHeaderPrefix ):
214+ // With a minimum header, the first line is part of the actual mail
215+ // content and needs to be parsed as part of the "rest"
216+ return parseHeaderMail ("" , strings .NewReader (header ))
217+
218+ case strings .HasPrefix (firstLine , prettyHeaderPrefix ):
219+ return parseHeaderPretty (firstLine , strings .NewReader (rest ))
221220 }
221+
222222 return nil , errors .New ("unrecognized patch header format" )
223223}
224224
@@ -233,7 +233,7 @@ func parseHeaderPretty(prettyLine string, r io.Reader) (*PatchHeader, error) {
233233
234234 h := & PatchHeader {}
235235
236- prettyLine = prettyLine [ len ( prettyHeaderPrefix ):]
236+ prettyLine = strings . TrimPrefix ( prettyLine , prettyHeaderPrefix )
237237 if i := strings .IndexByte (prettyLine , ' ' ); i > 0 {
238238 h .SHA = prettyLine [:i ]
239239 } else {
@@ -297,7 +297,7 @@ func parseHeaderPretty(prettyLine string, r io.Reader) (*PatchHeader, error) {
297297 h .Title = title
298298
299299 if title != "" {
300- // Don't check for an appendix
300+ // Don't check for an appendix, pretty headers do not contain them
301301 body , _ := scanMessageBody (s , indent , false )
302302 if s .Err () != nil {
303303 return nil , s .Err ()
@@ -374,8 +374,8 @@ func parseHeaderMail(mailLine string, r io.Reader) (*PatchHeader, error) {
374374
375375 h := & PatchHeader {}
376376
377- if len (mailLine ) > len ( mailHeaderPrefix ) {
378- mailLine = mailLine [ len ( mailHeaderPrefix ):]
377+ if strings . HasPrefix (mailLine , mailHeaderPrefix ) {
378+ mailLine = strings . TrimPrefix ( mailLine , mailHeaderPrefix )
379379 if i := strings .IndexByte (mailLine , ' ' ); i > 0 {
380380 h .SHA = mailLine [:i ]
381381 }
0 commit comments