Skip to content

Commit b5ae0bd

Browse files
authored
Rename PatchHeader.Message to .Body (#7)
Git usually refers to the whole thing (title + body) as the "commit message", so using different terms for the parts clarifies things. Also add a new Message() function that returns the combined string.
1 parent 546a186 commit b5ae0bd

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

gitdiff/patch_header.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,24 @@ type PatchHeader struct {
3535
Committer *PatchIdentity
3636
CommitterDate *PatchDate
3737

38-
// The title and message summarizing the changes in the patch. Empty if a
39-
// title or message is not included in the header.
40-
Title string
41-
Message string
38+
// The title and body of the commit message describing the changes in the
39+
// patch. Empty if no message is included in the header.
40+
Title string
41+
Body string
42+
}
43+
44+
// Message returns the commit message for the header. The message consists of
45+
// the title and the body separated by an empty line.
46+
func (h *PatchHeader) Message() string {
47+
var msg strings.Builder
48+
if h != nil {
49+
msg.WriteString(h.Title)
50+
if h.Body != "" {
51+
msg.WriteString("\n\n")
52+
msg.WriteString(h.Body)
53+
}
54+
}
55+
return msg.String()
4256
}
4357

4458
// PatchIdentity identifies a person who authored or committed a patch.
@@ -253,24 +267,24 @@ func parseHeaderPretty(prettyLine string, r io.Reader) (*PatchHeader, error) {
253267
return nil, s.Err()
254268
}
255269

256-
title, indent := scanPatchTitle(s)
270+
title, indent := scanMessageTitle(s)
257271
if s.Err() != nil {
258272
return nil, s.Err()
259273
}
260274
h.Title = title
261275

262276
if title != "" {
263-
msg := scanPatchMessage(s, indent)
277+
body := scanMessageBody(s, indent)
264278
if s.Err() != nil {
265279
return nil, s.Err()
266280
}
267-
h.Message = msg
281+
h.Body = body
268282
}
269283

270284
return h, nil
271285
}
272286

273-
func scanPatchTitle(s *bufio.Scanner) (title string, indent string) {
287+
func scanMessageTitle(s *bufio.Scanner) (title string, indent string) {
274288
var b strings.Builder
275289
for i := 0; s.Scan(); i++ {
276290
line := s.Text()
@@ -292,7 +306,7 @@ func scanPatchTitle(s *bufio.Scanner) (title string, indent string) {
292306
return b.String(), indent
293307
}
294308

295-
func scanPatchMessage(s *bufio.Scanner, indent string) string {
309+
func scanMessageBody(s *bufio.Scanner, indent string) string {
296310
var b strings.Builder
297311
var empty int
298312
for i := 0; s.Scan(); i++ {
@@ -351,7 +365,7 @@ func parseHeaderMail(mailLine string, r io.Reader) (*PatchHeader, error) {
351365
h.Title = msg.Header.Get("Subject")
352366

353367
s := bufio.NewScanner(msg.Body)
354-
h.Message = scanPatchMessage(s, "")
368+
h.Body = scanMessageBody(s, "")
355369
if s.Err() != nil {
356370
return nil, s.Err()
357371
}

gitdiff/patch_header_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func TestParsePatchHeader(t *testing.T) {
163163
Raw: "Sat Apr 11 15:21:23 2020 -0700",
164164
}
165165
expectedTitle := "A sample commit to test header parsing"
166-
expectedMsg := "The medium format shows the body, which\nmay wrap on to multiple lines.\n\nAnother body line."
166+
expectedBody := "The medium format shows the body, which\nmay wrap on to multiple lines.\n\nAnother body line."
167167

168168
tests := map[string]struct {
169169
Input string
@@ -199,7 +199,7 @@ Date: Sat Apr 11 15:21:23 2020 -0700
199199
Author: expectedIdentity,
200200
AuthorDate: expectedDate,
201201
Title: expectedTitle,
202-
Message: expectedMsg,
202+
Body: expectedBody,
203203
},
204204
},
205205
"prettyFull": {
@@ -219,7 +219,7 @@ Commit: Morton Haypenny <[email protected]>
219219
Author: expectedIdentity,
220220
Committer: expectedIdentity,
221221
Title: expectedTitle,
222-
Message: expectedMsg,
222+
Body: expectedBody,
223223
},
224224
},
225225
"prettyFuller": {
@@ -243,7 +243,7 @@ CommitDate: Sat Apr 11 15:21:23 2020 -0700
243243
Committer: expectedIdentity,
244244
CommitterDate: expectedDate,
245245
Title: expectedTitle,
246-
Message: expectedMsg,
246+
Body: expectedBody,
247247
},
248248
},
249249
"mailbox": {
@@ -264,8 +264,8 @@ Another body line.
264264
Parsed: expectedDate.Parsed,
265265
Raw: "Sat, 11 Apr 2020 15:21:23 -0700",
266266
},
267-
Title: "[PATCH] " + expectedTitle,
268-
Message: expectedMsg,
267+
Title: "[PATCH] " + expectedTitle,
268+
Body: expectedBody,
269269
},
270270
},
271271
"unwrapTitle": {
@@ -304,7 +304,7 @@ Date: Sat Apr 11 15:21:23 2020 -0700
304304
Author: expectedIdentity,
305305
AuthorDate: expectedDate,
306306
Title: expectedTitle,
307-
Message: expectedMsg,
307+
Body: expectedBody,
308308
},
309309
},
310310
"ignoreLeadingBlankLines": {
@@ -354,8 +354,8 @@ Author: Morton Haypenny <[email protected]>
354354
if exp.Title != act.Title {
355355
t.Errorf("incorrect parsed title:\n expected: %q\n actual: %q", exp.Title, act.Title)
356356
}
357-
if exp.Message != act.Message {
358-
t.Errorf("incorrect parsed message:\n expected: %q\n actual: %q", exp.Message, act.Message)
357+
if exp.Body != act.Body {
358+
t.Errorf("incorrect parsed body:\n expected: %q\n actual: %q", exp.Body, act.Body)
359359
}
360360
})
361361
}

0 commit comments

Comments
 (0)