Skip to content

Commit 61a4420

Browse files
committed
Merge pull request #4 from vbatts/field_names
Refactor Field names
2 parents 2117e29 + 6f71141 commit 61a4420

File tree

3 files changed

+90
-51
lines changed

3 files changed

+90
-51
lines changed

git/commits.go

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package git
22

33
import (
44
"bytes"
5-
"encoding/json"
6-
"fmt"
7-
"log"
85
"os"
96
"os/exec"
107
"strings"
@@ -16,7 +13,7 @@ import (
1613
// If commitrange is a git still range 12345...54321, then it will be isolated set of commits.
1714
// If commitrange is a single commit, all ancestor commits up through the hash provided.
1815
func Commits(commitrange string) ([]CommitEntry, error) {
19-
cmdArgs := []string{"git", "log", prettyFormat + formatCommit, commitrange}
16+
cmdArgs := []string{"git", "log", `--pretty=format:%H`, commitrange}
2017
if debug() {
2118
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
2219
}
@@ -36,61 +33,47 @@ func Commits(commitrange string) ([]CommitEntry, error) {
3633
return commits, nil
3734
}
3835

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

42-
var (
43-
prettyFormat = `--pretty=format:`
44-
formatSubject = `%s`
45-
formatBody = `%b`
46-
formatCommit = `%H`
47-
formatAuthorName = `%aN`
48-
formatAuthorEmail = `%aE`
49-
formatCommitterName = `%cN`
50-
formatCommitterEmail = `%cE`
51-
formatSigner = `%GS`
52-
formatCommitNotes = `%N`
53-
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" }`
54-
)
61+
// CommitEntry represents a single commit's information from `git`.
62+
// See also FieldNames
63+
type CommitEntry map[string]string
5564

5665
// LogCommit assembles the full information on a commit from its commit hash
5766
func LogCommit(commit string) (*CommitEntry, error) {
5867
buf := bytes.NewBuffer([]byte{})
59-
cmdArgs := []string{"git", "log", "-1", prettyFormat + formatMap, commit}
60-
if debug() {
61-
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
62-
}
63-
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
64-
cmd.Stdout = buf
65-
cmd.Stderr = os.Stderr
66-
67-
if err := cmd.Run(); err != nil {
68-
log.Println(strings.Join(cmd.Args, " "))
69-
return nil, err
70-
}
7168
c := CommitEntry{}
72-
output := buf.Bytes()
73-
if err := json.Unmarshal(output, &c); err != nil {
74-
fmt.Println(string(output))
75-
return nil, err
76-
}
77-
78-
// any user provided fields can't be sanitized for the mock-json marshal above
79-
for k, v := range map[string]string{
80-
"subject": formatSubject,
81-
"body": formatBody,
82-
"author_name": formatAuthorName,
83-
"author_email": formatAuthorEmail,
84-
"committer_name": formatCommitterName,
85-
"committer_email": formatCommitterEmail,
86-
"commit_notes": formatCommitNotes,
87-
"signer": formatSigner,
88-
} {
89-
output, err := exec.Command("git", "log", "-1", prettyFormat+v, commit).Output()
90-
if err != nil {
69+
for k, v := range FieldNames {
70+
cmd := exec.Command("git", "log", "-1", `--pretty=format:`+k+``, commit)
71+
cmd.Stdout = buf
72+
cmd.Stderr = os.Stderr
73+
if err := cmd.Run(); err != nil {
9174
return nil, err
9275
}
93-
c[k] = strings.TrimSpace(string(output))
76+
c[v] = strings.TrimSpace(string(buf.Bytes()))
9477
}
9578

9679
return &c, nil

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)