Repository: bluekeyes/go-gitdiff
Pull request #7 (2020-04-27 02:59:18)

Rename PatchHeader.Message to .Body
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.


Patch:
diff --git a/gitdiff/patch_header.go b/gitdiff/patch_header.go
index 819d0f1..bc0dc55 100644
--- a/gitdiff/patch_header.go
+++ b/gitdiff/patch_header.go
@@ -35,10 +35,24 @@ type PatchHeader struct {
 	Committer     *PatchIdentity
 	CommitterDate *PatchDate
 
-	// The title and message summarizing the changes in the patch. Empty if a
-	// title or message is not included in the header.
-	Title   string
-	Message string
+	// The title and body of the commit message describing the changes in the
+	// patch. Empty if no message is included in the header.
+	Title string
+	Body  string
+}
+
+// Message returns the commit message for the header. The message consists of
+// the title and the body separated by an empty line.
+func (h *PatchHeader) Message() string {
+	var msg strings.Builder
+	if h != nil {
+		msg.WriteString(h.Title)
+		if h.Body != "" {
+			msg.WriteString("\n\n")
+			msg.WriteString(h.Body)
+		}
+	}
+	return msg.String()
 }
 
 // PatchIdentity identifies a person who authored or committed a patch.
@@ -253,24 +267,24 @@ func parseHeaderPretty(prettyLine string, r io.Reader) (*PatchHeader, error) {
 		return nil, s.Err()
 	}
 
-	title, indent := scanPatchTitle(s)
+	title, indent := scanMessageTitle(s)
 	if s.Err() != nil {
 		return nil, s.Err()
 	}
 	h.Title = title
 
 	if title != "" {
-		msg := scanPatchMessage(s, indent)
+		body := scanMessageBody(s, indent)
 		if s.Err() != nil {
 			return nil, s.Err()
 		}
-		h.Message = msg
+		h.Body = body
 	}
 
 	return h, nil
 }
 
-func scanPatchTitle(s *bufio.Scanner) (title string, indent string) {
+func scanMessageTitle(s *bufio.Scanner) (title string, indent string) {
 	var b strings.Builder
 	for i := 0; s.Scan(); i++ {
 		line := s.Text()
@@ -292,7 +306,7 @@ func scanPatchTitle(s *bufio.Scanner) (title string, indent string) {
 	return b.String(), indent
 }
 
-func scanPatchMessage(s *bufio.Scanner, indent string) string {
+func scanMessageBody(s *bufio.Scanner, indent string) string {
 	var b strings.Builder
 	var empty int
 	for i := 0; s.Scan(); i++ {
@@ -351,7 +365,7 @@ func parseHeaderMail(mailLine string, r io.Reader) (*PatchHeader, error) {
 	h.Title = msg.Header.Get("Subject")
 
 	s := bufio.NewScanner(msg.Body)
-	h.Message = scanPatchMessage(s, "")
+	h.Body = scanMessageBody(s, "")
 	if s.Err() != nil {
 		return nil, s.Err()
 	}


Test patch:
diff --git a/gitdiff/patch_header_test.go b/gitdiff/patch_header_test.go
index a8e0d4f..3be2564 100644
--- a/gitdiff/patch_header_test.go
+++ b/gitdiff/patch_header_test.go
@@ -163,7 +163,7 @@ func TestParsePatchHeader(t *testing.T) {
 		Raw:    "Sat Apr 11 15:21:23 2020 -0700",
 	}
 	expectedTitle := "A sample commit to test header parsing"
-	expectedMsg := "The medium format shows the body, which\nmay wrap on to multiple lines.\n\nAnother body line."
+	expectedBody := "The medium format shows the body, which\nmay wrap on to multiple lines.\n\nAnother body line."
 
 	tests := map[string]struct {
 		Input  string
@@ -199,7 +199,7 @@ Date:   Sat Apr 11 15:21:23 2020 -0700
 				Author:     expectedIdentity,
 				AuthorDate: expectedDate,
 				Title:      expectedTitle,
-				Message:    expectedMsg,
+				Body:       expectedBody,
 			},
 		},
 		"prettyFull": {
@@ -219,7 +219,7 @@ Commit: Morton Haypenny <mhaypenny@example.com>
 				Author:    expectedIdentity,
 				Committer: expectedIdentity,
 				Title:     expectedTitle,
-				Message:   expectedMsg,
+				Body:      expectedBody,
 			},
 		},
 		"prettyFuller": {
@@ -243,7 +243,7 @@ CommitDate: Sat Apr 11 15:21:23 2020 -0700
 				Committer:     expectedIdentity,
 				CommitterDate: expectedDate,
 				Title:         expectedTitle,
-				Message:       expectedMsg,
+				Body:          expectedBody,
 			},
 		},
 		"mailbox": {
@@ -264,8 +264,8 @@ Another body line.
 					Parsed: expectedDate.Parsed,
 					Raw:    "Sat, 11 Apr 2020 15:21:23 -0700",
 				},
-				Title:   "[PATCH] " + expectedTitle,
-				Message: expectedMsg,
+				Title: "[PATCH] " + expectedTitle,
+				Body:  expectedBody,
 			},
 		},
 		"unwrapTitle": {
@@ -304,7 +304,7 @@ Date:   Sat Apr 11 15:21:23 2020 -0700
 				Author:     expectedIdentity,
 				AuthorDate: expectedDate,
 				Title:      expectedTitle,
-				Message:    expectedMsg,
+				Body:       expectedBody,
 			},
 		},
 		"ignoreLeadingBlankLines": {
@@ -354,8 +354,8 @@ Author: Morton Haypenny <mhaypenny@example.com>
 			if exp.Title != act.Title {
 				t.Errorf("incorrect parsed title:\n  expected: %q\n    actual: %q", exp.Title, act.Title)
 			}
-			if exp.Message != act.Message {
-				t.Errorf("incorrect parsed message:\n  expected: %q\n    actual: %q", exp.Message, act.Message)
+			if exp.Body != act.Body {
+				t.Errorf("incorrect parsed body:\n  expected: %q\n    actual: %q", exp.Body, act.Body)
 			}
 		})
 	}