diff --git a/gitmap.go b/gitmap.go index 0711070..3a346ff 100644 --- a/gitmap.go +++ b/gitmap.go @@ -46,6 +46,7 @@ type GitInfo struct { AuthorEmail string `json:"authorEmail"` // The author email address, respecting .mailmap AuthorDate time.Time `json:"authorDate"` // The author date CommitDate time.Time `json:"commitDate"` // The commit date + Body string `json:"body"` // The commit message body } // 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) { topLevelPath := filepath.ToSlash(filepath.Join(absRepoPath, cdUp)) gitLogArgs := strings.Fields(fmt.Sprintf( - `--name-only --no-merges --format=format:%%x1e%%H%%x1f%%h%%x1f%%s%%x1f%%aN%%x1f%%aE%%x1f%%ai%%x1f%%ci %s`, + `--name-only --no-merges --format=format:%%x1e%%H%%x1f%%h%%x1f%%s%%x1f%%aN%%x1f%%aE%%x1f%%ai%%x1f%%ci%%x1f%%b%%x1d %s`, revision, )) @@ -84,13 +85,13 @@ func Map(repository, revision string) (*GitRepo, error) { entries := strings.Split(entriesStr, "\x1e") for _, e := range entries { - lines := strings.Split(e, "\n") + lines := strings.Split(e, "\x1d") gitInfo, err := toGitInfo(lines[0]) if err != nil { return nil, err } - - for _, filename := range lines[1:] { + filenames := strings.Split(lines[1], "\n") + for _, filename := range filenames { filename := strings.TrimSpace(filename) if filename == "" { continue @@ -121,6 +122,9 @@ func git(args ...string) ([]byte, error) { func toGitInfo(entry string) (*GitInfo, error) { items := strings.Split(entry, "\x1f") + if len(items) == 7 { + items = append(items, "") + } authorDate, err := time.Parse("2006-01-02 15:04:05 -0700", items[5]) if err != nil { return nil, err @@ -138,6 +142,7 @@ func toGitInfo(entry string) (*GitInfo, error) { AuthorEmail: items[4], AuthorDate: authorDate, CommitDate: commitDate, + Body: strings.TrimSpace(items[7]), }, nil } diff --git a/gitmap_test.go b/gitmap_test.go index 7203406..19426db 100644 --- a/gitmap_test.go +++ b/gitmap_test.go @@ -113,6 +113,67 @@ func assertFile( } } +func TestCommitMessage(t *testing.T) { + var ( + gm GitMap + gr *GitRepo + err error + ) + + if gr, err = Map(repository, "HEAD"); err != nil { + t.Fatal(err) + } + + gm = gr.Files + + assertMessage( + t, gm, + "testfiles/d1/d1.txt", + "Change the test files", + "To trigger a test variant.", + ) + + assertMessage( + t, gm, + "testfiles/r3.txt", + "Add test file for commit body", + "- This is a multi-line\n- commit body", + ) + + assertMessage( + t, gm, + "testfiles/amended.txt", + "Add testfile with different author/commit date", + "", + ) +} + +func assertMessage( + t *testing.T, + gm GitMap, + filename, + expectedSubject, + expectedBody string, +) { + var ( + gi *GitInfo + ok bool + ) + + if gi, ok = gm[filename]; !ok { + t.Fatal(filename) + } + + if gi.Subject != expectedSubject { + t.Error("Incorrect commit subject. Got", gi.Subject) + } + + if gi.Body != expectedBody { + t.Error("Incorrect commit body. Got", gi.Body) + } + +} + func TestActiveRevision(t *testing.T) { var ( gm GitMap @@ -172,7 +233,7 @@ func TestEncodeJSON(t *testing.T) { s := string(b) - 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"}` { + 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":""}` { t.Errorf("JSON marshal error: \n%s", s) } } diff --git a/testfiles/r3.txt b/testfiles/r3.txt new file mode 100644 index 0000000..e69de29