Remove "appendix" information from commit message (#19)

...when parsing emails, similar to `git am`.

Add a new field, `BodyAppendix` to PatchHeader.

Modify `scanMessageBody` to accept a boolean argument saying whether
to separate out the appendix or not.  Do this by keeping two string
builders, and having it switch to the appendix builder when it finds a
`---` line.

Handling the newlines at the end as expected requires moving things
around a bit.

First, we were trimming space from the line once to decide whether the
line was empty, and then trimming space again if we determined it
wasn't empty.  This only needs to be done once.

Then, do all the trimming (both of whitespace and the prefix) first,
before deciding what to do about the line.

Request BodyAppendix separately when parsing a mail, but not a commit
message.

Add some tests to verify that it works as expected.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
Co-authored-by: George Dunlap <george.dunlap@citrix.com>
This commit is contained in:
George Dunlap
2020-10-24 04:39:43 +01:00
committed by GitHub
parent 379b893435
commit 1bd59c4f11
2 changed files with 95 additions and 16 deletions
+53
View File
@@ -139,6 +139,7 @@ func TestParsePatchHeader(t *testing.T) {
expectedDate := time.Date(2020, 04, 11, 15, 21, 23, 0, time.FixedZone("PDT", -7*60*60))
expectedTitle := "A sample commit to test header parsing"
expectedBody := "The medium format shows the body, which\nmay wrap on to multiple lines.\n\nAnother body line."
expectedBodyAppendix := "CC: Joe Smith <joe.smith@company.com>"
tests := map[string]struct {
Input string
@@ -221,6 +222,32 @@ CommitDate: Sat Apr 11 15:21:23 2020 -0700
Body: expectedBody,
},
},
"prettyAppendix": {
Input: `commit 61f5cd90bed4d204ee3feb3aa41ee91d4734855b
Author: Morton Haypenny <mhaypenny@example.com>
AuthorDate: Sat Apr 11 15:21:23 2020 -0700
Commit: Morton Haypenny <mhaypenny@example.com>
CommitDate: Sat Apr 11 15:21:23 2020 -0700
A sample commit to test header parsing
The medium format shows the body, which
may wrap on to multiple lines.
Another body line.
---
CC: Joe Smith <joe.smith@company.com>
`,
Header: PatchHeader{
SHA: expectedSHA,
Author: expectedIdentity,
AuthorDate: expectedDate,
Committer: expectedIdentity,
CommitterDate: expectedDate,
Title: expectedTitle,
Body: expectedBody + "\n---\n" + expectedBodyAppendix,
},
},
"mailbox": {
Input: `From 61f5cd90bed4d204ee3feb3aa41ee91d4734855b Mon Sep 17 00:00:00 2001
From: Morton Haypenny <mhaypenny@example.com>
@@ -240,6 +267,28 @@ Another body line.
Body: expectedBody,
},
},
"mailboxAppendix": {
Input: `From 61f5cd90bed4d204ee3feb3aa41ee91d4734855b Mon Sep 17 00:00:00 2001
From: Morton Haypenny <mhaypenny@example.com>
Date: Sat, 11 Apr 2020 15:21:23 -0700
Subject: [PATCH] A sample commit to test header parsing
The medium format shows the body, which
may wrap on to multiple lines.
Another body line.
---
CC: Joe Smith <joe.smith@company.com>
`,
Header: PatchHeader{
SHA: expectedSHA,
Author: expectedIdentity,
AuthorDate: expectedDate,
Title: expectedTitle,
Body: expectedBody,
BodyAppendix: expectedBodyAppendix,
},
},
"unwrapTitle": {
Input: `commit 61f5cd90bed4d204ee3feb3aa41ee91d4734855b
Author: Morton Haypenny <mhaypenny@example.com>
@@ -333,6 +382,10 @@ Author: Morton Haypenny <mhaypenny@example.com>
if exp.Body != act.Body {
t.Errorf("incorrect parsed body:\n expected: %q\n actual: %q", exp.Body, act.Body)
}
if exp.BodyAppendix != act.BodyAppendix {
t.Errorf("incorrect parsed body appendix:\n expected: %q\n actual: %q",
exp.BodyAppendix, act.BodyAppendix)
}
})
}
}