Skip to content

Commit bec72b8

Browse files
authored
Expose commit body (#13)
* Expose commit body - Exposes commit body - Adds tests for commit subject and body * Add test file for commit body - This is a multi-line - commit body * Add tests for commit body messages
1 parent 58509b2 commit bec72b8

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

gitmap.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type GitInfo struct {
4646
AuthorEmail string `json:"authorEmail"` // The author email address, respecting .mailmap
4747
AuthorDate time.Time `json:"authorDate"` // The author date
4848
CommitDate time.Time `json:"commitDate"` // The commit date
49+
Body string `json:"body"` // The commit message body
4950
}
5051

5152
// Map creates a GitRepo with a file map from the given repository path and revision.
@@ -68,7 +69,7 @@ func Map(repository, revision string) (*GitRepo, error) {
6869
topLevelPath := filepath.ToSlash(filepath.Join(absRepoPath, cdUp))
6970

7071
gitLogArgs := strings.Fields(fmt.Sprintf(
71-
`--name-only --no-merges --format=format:%%x1e%%H%%x1f%%h%%x1f%%s%%x1f%%aN%%x1f%%aE%%x1f%%ai%%x1f%%ci %s`,
72+
`--name-only --no-merges --format=format:%%x1e%%H%%x1f%%h%%x1f%%s%%x1f%%aN%%x1f%%aE%%x1f%%ai%%x1f%%ci%%x1f%%b%%x1d %s`,
7273
revision,
7374
))
7475

@@ -84,13 +85,13 @@ func Map(repository, revision string) (*GitRepo, error) {
8485
entries := strings.Split(entriesStr, "\x1e")
8586

8687
for _, e := range entries {
87-
lines := strings.Split(e, "\n")
88+
lines := strings.Split(e, "\x1d")
8889
gitInfo, err := toGitInfo(lines[0])
8990
if err != nil {
9091
return nil, err
9192
}
92-
93-
for _, filename := range lines[1:] {
93+
filenames := strings.Split(lines[1], "\n")
94+
for _, filename := range filenames {
9495
filename := strings.TrimSpace(filename)
9596
if filename == "" {
9697
continue
@@ -121,6 +122,9 @@ func git(args ...string) ([]byte, error) {
121122

122123
func toGitInfo(entry string) (*GitInfo, error) {
123124
items := strings.Split(entry, "\x1f")
125+
if len(items) == 7 {
126+
items = append(items, "")
127+
}
124128
authorDate, err := time.Parse("2006-01-02 15:04:05 -0700", items[5])
125129
if err != nil {
126130
return nil, err
@@ -138,6 +142,7 @@ func toGitInfo(entry string) (*GitInfo, error) {
138142
AuthorEmail: items[4],
139143
AuthorDate: authorDate,
140144
CommitDate: commitDate,
145+
Body: strings.TrimSpace(items[7]),
141146
}, nil
142147
}
143148

gitmap_test.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,67 @@ func assertFile(
113113
}
114114
}
115115

116+
func TestCommitMessage(t *testing.T) {
117+
var (
118+
gm GitMap
119+
gr *GitRepo
120+
err error
121+
)
122+
123+
if gr, err = Map(repository, "HEAD"); err != nil {
124+
t.Fatal(err)
125+
}
126+
127+
gm = gr.Files
128+
129+
assertMessage(
130+
t, gm,
131+
"testfiles/d1/d1.txt",
132+
"Change the test files",
133+
"To trigger a test variant.",
134+
)
135+
136+
assertMessage(
137+
t, gm,
138+
"testfiles/r3.txt",
139+
"Add test file for commit body",
140+
"- This is a multi-line\n- commit body",
141+
)
142+
143+
assertMessage(
144+
t, gm,
145+
"testfiles/amended.txt",
146+
"Add testfile with different author/commit date",
147+
"",
148+
)
149+
}
150+
151+
func assertMessage(
152+
t *testing.T,
153+
gm GitMap,
154+
filename,
155+
expectedSubject,
156+
expectedBody string,
157+
) {
158+
var (
159+
gi *GitInfo
160+
ok bool
161+
)
162+
163+
if gi, ok = gm[filename]; !ok {
164+
t.Fatal(filename)
165+
}
166+
167+
if gi.Subject != expectedSubject {
168+
t.Error("Incorrect commit subject. Got", gi.Subject)
169+
}
170+
171+
if gi.Body != expectedBody {
172+
t.Error("Incorrect commit body. Got", gi.Body)
173+
}
174+
175+
}
176+
116177
func TestActiveRevision(t *testing.T) {
117178
var (
118179
gm GitMap
@@ -172,7 +233,7 @@ func TestEncodeJSON(t *testing.T) {
172233

173234
s := string(b)
174235

175-
if s != `{"hash":"0b830e458446fdb774b1688af9b402acf388d6ab","abbreviatedHash":"0b830e4","subject":"Add some more to README","authorName":"Bjørn Erik Pedersen","authorEmail":"bjorn.erik.pedersen@gmail.com","authorDate":"2016-07-22T21:40:27+02:00","commitDate":"2016-07-22T21:40:27+02:00"}` {
236+
if s != `{"hash":"0b830e458446fdb774b1688af9b402acf388d6ab","abbreviatedHash":"0b830e4","subject":"Add some more to README","authorName":"Bjørn Erik Pedersen","authorEmail":"bjorn.erik.pedersen@gmail.com","authorDate":"2016-07-22T21:40:27+02:00","commitDate":"2016-07-22T21:40:27+02:00","body":""}` {
176237
t.Errorf("JSON marshal error: \n%s", s)
177238
}
178239
}

testfiles/r3.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)