Skip to content

Commit d74a248

Browse files
committed
git: restructured commit entries
Signed-off-by: Vincent Batts <[email protected]>
1 parent 2117e29 commit d74a248

File tree

3 files changed

+88
-6
lines changed

3 files changed

+88
-6
lines changed

git/commits.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,46 @@ func Commits(commitrange string) ([]CommitEntry, error) {
3636
return commits, nil
3737
}
3838

39-
// CommitEntry represents a single commit's information from `git`
39+
// FieldNames are for the formating and rendering of the CommitEntry structs.
40+
// Keys here are from git log pretty format "format:..."
41+
var FieldNames = map[string]string{
42+
"%h": "abbreviated_commit",
43+
"%p": "abbreviated_parent",
44+
"%t": "abbreviated_tree",
45+
"%aD": "author_date",
46+
"%aE": "author_email",
47+
"%aN": "author_name",
48+
"%b": "body",
49+
"%H": "commit",
50+
"%N": "commit_notes",
51+
"%cD": "committer_date",
52+
"%cE": "committer_email",
53+
"%cN": "committer_name",
54+
"%e": "encoding",
55+
"%P": "parent",
56+
"%D": "refs",
57+
"%f": "sanitized_subject_line",
58+
"%GS": "signer",
59+
"%GK": "signer_key",
60+
"%s": "subject",
61+
"%G?": "verification_flag",
62+
}
63+
64+
// CommitEntry represents a single commit's information from `git`.
65+
// See also FieldNames
4066
type CommitEntry map[string]string
4167

4268
var (
4369
prettyFormat = `--pretty=format:`
44-
formatSubject = `%s`
70+
formatAuthorEmail = `%aE`
71+
formatAuthorName = `%aN`
4572
formatBody = `%b`
4673
formatCommit = `%H`
47-
formatAuthorName = `%aN`
48-
formatAuthorEmail = `%aE`
49-
formatCommitterName = `%cN`
74+
formatCommitNotes = `%N`
5075
formatCommitterEmail = `%cE`
76+
formatCommitterName = `%cN`
5177
formatSigner = `%GS`
52-
formatCommitNotes = `%N`
78+
formatSubject = `%s`
5379
formatMap = `{"commit": "%H", "abbreviated_commit": "%h", "tree": "%T", "abbreviated_tree": "%t", "parent": "%P", "abbreviated_parent": "%p", "refs": "%D", "encoding": "%e", "sanitized_subject_line": "%f", "verification_flag": "%G?", "signer_key": "%GK", "author_date": "%aD" , "committer_date": "%cD" }`
5480
)
5581

git/commits_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package git
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"testing"
7+
)
8+
9+
func TestCommitEntry(t *testing.T) {
10+
c, err := HeadCommit()
11+
if err != nil {
12+
t.Fatal(err)
13+
}
14+
cr, err := Commits(c)
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
for _, c := range cr {
19+
for _, cV := range FieldNames {
20+
found := false
21+
for k := range c {
22+
if k == cV {
23+
found = true
24+
}
25+
}
26+
if !found {
27+
t.Errorf("failed to find field names: %q", c)
28+
}
29+
}
30+
}
31+
}
32+
33+
func TestMarshal(t *testing.T) {
34+
buf, err := ioutil.ReadFile("testdata/commits.json")
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
cr := []CommitEntry{}
39+
if err := json.Unmarshal(buf, &cr); err != nil {
40+
t.Error(err)
41+
}
42+
for _, c := range cr {
43+
for _, cV := range FieldNames {
44+
found := false
45+
for k := range c {
46+
if k == cV {
47+
found = true
48+
}
49+
}
50+
if !found {
51+
t.Errorf("failed to find field names: %q", c)
52+
}
53+
}
54+
}
55+
}

git/testdata/commits.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"abbreviated_commit":"769b402","abbreviated_parent":"eba7575","abbreviated_tree":"f3490fa","author_date":"Thu, 14 Jan 2016 10:29:35 -0500","author_email":"[email protected]","author_name":"Vincent Batts","body":"so you can test and just check return code\n\nSigned-off-by: Vincent Batts \[email protected]\u003e","commit":"769b4028a4732f5134848aa970e73e2d6edc3175","commit_notes":"","committer_date":"Thu, 14 Jan 2016 10:29:35 -0500","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"eba7575f7eccc9ee1aa5874769b44a62b612947d","refs":"HEAD -\u003e master, origin/master, origin/HEAD","sanitized_subject_line":"main-adding-a-quiet-flag-to-reduce-the-output","signer":"","signer_key":"","subject":"main: adding a quiet flag to reduce the output","tree":"f3490fa57c956658a78348651bf75dfad15e5f68","verification_flag":"N"},{"abbreviated_commit":"eba7575","abbreviated_parent":"09c2bd4","abbreviated_tree":"77c40ce","author_date":"Tue, 6 Oct 2015 10:55:42 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"Signed-off-by: Vincent Batts \[email protected]\u003e","commit":"eba7575f7eccc9ee1aa5874769b44a62b612947d","commit_notes":"","committer_date":"Tue, 6 Oct 2015 10:55:42 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"09c2bd43dc6f0508f461f9e1088013abbd3fa3d5","refs":"","sanitized_subject_line":"README-information-about-rules","signer":"","signer_key":"","subject":"README: information about rules","tree":"77c40ce1580d215d8cac2d997d41d08baa6b2546","verification_flag":"N"},{"abbreviated_commit":"09c2bd4","abbreviated_parent":"b243ca4","abbreviated_tree":"f88ab5a","author_date":"Tue, 6 Oct 2015 10:51:22 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"Signed-off-by: Vincent Batts \[email protected]\u003e","commit":"09c2bd43dc6f0508f461f9e1088013abbd3fa3d5","commit_notes":"","committer_date":"Tue, 6 Oct 2015 10:51:22 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"b243ca47707576f7551094dd26c8e4647f093acf","refs":"","sanitized_subject_line":"README-more-usage","signer":"","signer_key":"","subject":"README: more usage","tree":"f88ab5a60a21f177e37b7eee3ee7c2c0e15f0892","verification_flag":"N"},{"abbreviated_commit":"b243ca4","abbreviated_parent":"d614ccf","abbreviated_tree":"a0c3f0b","author_date":"Tue, 6 Oct 2015 10:47:00 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"Signed-off-by: Vincent Batts \[email protected]\u003e","commit":"b243ca47707576f7551094dd26c8e4647f093acf","commit_notes":"","committer_date":"Tue, 6 Oct 2015 10:48:03 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"d614ccf9970296e9619e28883748be653801dbf3","refs":"","sanitized_subject_line":"README-adding-install-and-usage","signer":"","signer_key":"","subject":"README: adding install and usage","tree":"a0c3f0b74f503938d7d7c31a5d6e937410d520ae","verification_flag":"N"},{"abbreviated_commit":"d614ccf","abbreviated_parent":"b9413c6","abbreviated_tree":"785f2f5","author_date":"Tue, 6 Oct 2015 10:44:04 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"for cleanliness and ease of testing\n\nSigned-off-by: Vincent Batts \[email protected]\u003e","commit":"d614ccf9970296e9619e28883748be653801dbf3","commit_notes":"","committer_date":"Tue, 6 Oct 2015 10:44:04 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"b9413c60c83d6f25d8979292d09ddceff0cde111","refs":"","sanitized_subject_line":"run-tests-in-a-runner","signer":"","signer_key":"","subject":"*: run tests in a runner","tree":"785f2f51a0e92f590053745be8e915116e4e8c59","verification_flag":"N"},{"abbreviated_commit":"b9413c6","abbreviated_parent":"5e74abd","abbreviated_tree":"997fcb4","author_date":"Mon, 5 Oct 2015 19:02:31 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"Signed-off-by: Vincent Batts \[email protected]\u003e","commit":"b9413c60c83d6f25d8979292d09ddceff0cde111","commit_notes":"","committer_date":"Mon, 5 Oct 2015 19:02:31 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"5e74abd1b2560d91647280da8374978763cb3b0f","refs":"","sanitized_subject_line":"shortsubject-add-a-subject-length-check","signer":"","signer_key":"","subject":"shortsubject: add a subject length check","tree":"997fcb4cf30c0f25ac5feb907ff5a977b8a65ae6","verification_flag":"N"},{"abbreviated_commit":"5e74abd","abbreviated_parent":"07a982f","abbreviated_tree":"6605800","author_date":"Mon, 5 Oct 2015 18:55:05 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"Signed-off-by: Vincent Batts \[email protected]\u003e","commit":"5e74abd1b2560d91647280da8374978763cb3b0f","commit_notes":"","committer_date":"Mon, 5 Oct 2015 18:55:05 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"07a982ff94318262732e6422daa75f1f51340b60","refs":"","sanitized_subject_line":"comments-and-golint","signer":"","signer_key":"","subject":"*: comments and golint","tree":"6605800cc08fc9906b9ab9819413d2adaffd0ed5","verification_flag":"N"},{"abbreviated_commit":"07a982f","abbreviated_parent":"03bda4b","abbreviated_tree":"8a81024","author_date":"Mon, 5 Oct 2015 18:45:33 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"Signed-off-by: Vincent Batts \[email protected]\u003e","commit":"07a982ff94318262732e6422daa75f1f51340b60","commit_notes":"","committer_date":"Mon, 5 Oct 2015 18:49:06 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"03bda4bcb2e741bc3e540ac171ba229bcfb47b9c","refs":"","sanitized_subject_line":"git-add-verbose-output-of-the-commands-run","signer":"","signer_key":"","subject":"git: add verbose output of the commands run","tree":"8a81024ef0d4e93d5852180f3c3f9ce10ae09cdb","verification_flag":"N"},{"abbreviated_commit":"03bda4b","abbreviated_parent":"c10ba9c","abbreviated_tree":"e41fbcb","author_date":"Mon, 5 Oct 2015 18:29:24 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"Signed-off-by: Vincent Batts \[email protected]\u003e","commit":"03bda4bcb2e741bc3e540ac171ba229bcfb47b9c","commit_notes":"","committer_date":"Mon, 5 Oct 2015 18:49:02 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"c10ba9c097ba2d5e0d5e834ae43252276a177e94","refs":"","sanitized_subject_line":"main-add-filtering-of-rules-to-run","signer":"","signer_key":"","subject":"main: add filtering of rules to run","tree":"e41fbcb527138c2cf49ee05d8adc55ba5d4d22f6","verification_flag":"N"},{"abbreviated_commit":"c10ba9c","abbreviated_parent":"","abbreviated_tree":"06f0e7a","author_date":"Mon, 5 Oct 2015 16:16:52 -0400","author_email":"[email protected]","author_name":"Vincent Batts","body":"Signed-off-by: Vincent Batts \[email protected]\u003e","commit":"c10ba9c097ba2d5e0d5e834ae43252276a177e94","commit_notes":"","committer_date":"Mon, 5 Oct 2015 18:48:23 -0400","committer_email":"[email protected]","committer_name":"Vincent Batts","encoding":"","parent":"","refs":"","sanitized_subject_line":"Initial-commit","signer":"","signer_key":"","subject":"Initial commit","tree":"06f0e7a5b91783b052759a91c25fab7a22ce3ab4","verification_flag":"N"}]

0 commit comments

Comments
 (0)