1
0
Fork 0
mirror of https://github.com/vbatts/git-validation.git synced 2024-11-22 08:05:39 +00:00

git: restructured commit entries

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2016-04-01 16:20:18 -04:00
parent e3ba588c3a
commit 3ec88517cc
3 changed files with 89 additions and 7 deletions

View file

@ -36,20 +36,46 @@ func Commits(commitrange string) ([]CommitEntry, error) {
return commits, nil
}
// CommitEntry represents a single commit's information from `git`
// FieldNames are for the formating and rendering of the CommitEntry structs.
// Keys here are from git log pretty format "format:..."
var FieldNames = map[string]string{
"%h": "abbreviated_commit",
"%p": "abbreviated_parent",
"%t": "abbreviated_tree",
"%aD": "author_date",
"%aE": "author_email",
"%aN": "author_name",
"%b": "body",
"%H": "commit",
"%N": "commit_notes",
"%cD": "committer_date",
"%cE": "committer_email",
"%cN": "committer_name",
"%e": "encoding",
"%P": "parent",
"%D": "refs",
"%f": "sanitized_subject_line",
"%GS": "signer",
"%GK": "signer_key",
"%s": "subject",
"%G?": "verification_flag",
}
// CommitEntry represents a single commit's information from `git`.
// See also FieldNames
type CommitEntry map[string]string
var (
prettyFormat = `--pretty=format:`
formatSubject = `%s`
formatAuthorEmail = `%aE`
formatAuthorName = `%aN`
formatBody = `%b`
formatCommit = `%H`
formatAuthorName = `%aN`
formatAuthorEmail = `%aE`
formatCommitterName = `%cN`
formatCommitterEmail = `%cE`
formatSigner = `%GS`
formatCommitNotes = `%N`
formatCommitterEmail = `%cE`
formatCommitterName = `%cN`
formatSigner = `%GS`
formatSubject = `%s`
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" }`
)

55
git/commits_test.go Normal file
View file

@ -0,0 +1,55 @@
package git
import (
"encoding/json"
"io/ioutil"
"testing"
)
func TestCommitEntry(t *testing.T) {
c, err := HeadCommit()
if err != nil {
t.Fatal(err)
}
cr, err := Commits(c)
if err != nil {
t.Fatal(err)
}
for _, c := range cr {
for _, cV := range FieldNames {
found := false
for k := range c {
if k == cV {
found = true
}
}
if !found {
t.Errorf("failed to find field names: %q", c)
}
}
}
}
func TestMarshal(t *testing.T) {
buf, err := ioutil.ReadFile("testdata/commits.json")
if err != nil {
t.Fatal(err)
}
cr := []CommitEntry{}
if err := json.Unmarshal(buf, &cr); err != nil {
t.Error(err)
}
for _, c := range cr {
for _, cV := range FieldNames {
found := false
for k := range c {
if k == cV {
found = true
}
}
if !found {
t.Errorf("failed to find field names: %q", c)
}
}
}
}

1
git/testdata/commits.json vendored Normal file

File diff suppressed because one or more lines are too long