Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose commit body #13

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions gitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
))

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -138,6 +142,7 @@ func toGitInfo(entry string) (*GitInfo, error) {
AuthorEmail: items[4],
AuthorDate: authorDate,
CommitDate: commitDate,
Body: strings.TrimSpace(items[7]),
}, nil
}

Expand Down
63 changes: 62 additions & 1 deletion gitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":"[email protected]","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":"[email protected]","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)
}
}
Expand Down
Empty file added testfiles/r3.txt
Empty file.