mirror of
https://github.com/vbatts/git-validation.git
synced 2025-04-16 20:14:33 +00:00
commit
61a4420f38
3 changed files with 90 additions and 51 deletions
|
@ -2,9 +2,6 @@ package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -16,7 +13,7 @@ import (
|
||||||
// If commitrange is a git still range 12345...54321, then it will be isolated set of commits.
|
// If commitrange is a git still range 12345...54321, then it will be isolated set of commits.
|
||||||
// If commitrange is a single commit, all ancestor commits up through the hash provided.
|
// If commitrange is a single commit, all ancestor commits up through the hash provided.
|
||||||
func Commits(commitrange string) ([]CommitEntry, error) {
|
func Commits(commitrange string) ([]CommitEntry, error) {
|
||||||
cmdArgs := []string{"git", "log", prettyFormat + formatCommit, commitrange}
|
cmdArgs := []string{"git", "log", `--pretty=format:%H`, commitrange}
|
||||||
if debug() {
|
if debug() {
|
||||||
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
|
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
|
||||||
}
|
}
|
||||||
|
@ -36,61 +33,47 @@ func Commits(commitrange string) ([]CommitEntry, error) {
|
||||||
return commits, nil
|
return commits, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitEntry represents a single commit's information from `git`
|
// FieldNames are for the formating and rendering of the CommitEntry structs.
|
||||||
type CommitEntry map[string]string
|
// 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",
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
// CommitEntry represents a single commit's information from `git`.
|
||||||
prettyFormat = `--pretty=format:`
|
// See also FieldNames
|
||||||
formatSubject = `%s`
|
type CommitEntry map[string]string
|
||||||
formatBody = `%b`
|
|
||||||
formatCommit = `%H`
|
|
||||||
formatAuthorName = `%aN`
|
|
||||||
formatAuthorEmail = `%aE`
|
|
||||||
formatCommitterName = `%cN`
|
|
||||||
formatCommitterEmail = `%cE`
|
|
||||||
formatSigner = `%GS`
|
|
||||||
formatCommitNotes = `%N`
|
|
||||||
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" }`
|
|
||||||
)
|
|
||||||
|
|
||||||
// LogCommit assembles the full information on a commit from its commit hash
|
// LogCommit assembles the full information on a commit from its commit hash
|
||||||
func LogCommit(commit string) (*CommitEntry, error) {
|
func LogCommit(commit string) (*CommitEntry, error) {
|
||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
cmdArgs := []string{"git", "log", "-1", prettyFormat + formatMap, commit}
|
|
||||||
if debug() {
|
|
||||||
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
|
|
||||||
}
|
|
||||||
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
|
||||||
cmd.Stdout = buf
|
|
||||||
cmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
|
||||||
log.Println(strings.Join(cmd.Args, " "))
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c := CommitEntry{}
|
c := CommitEntry{}
|
||||||
output := buf.Bytes()
|
for k, v := range FieldNames {
|
||||||
if err := json.Unmarshal(output, &c); err != nil {
|
cmd := exec.Command("git", "log", "-1", `--pretty=format:`+k+``, commit)
|
||||||
fmt.Println(string(output))
|
cmd.Stdout = buf
|
||||||
return nil, err
|
cmd.Stderr = os.Stderr
|
||||||
}
|
if err := cmd.Run(); err != nil {
|
||||||
|
|
||||||
// any user provided fields can't be sanitized for the mock-json marshal above
|
|
||||||
for k, v := range map[string]string{
|
|
||||||
"subject": formatSubject,
|
|
||||||
"body": formatBody,
|
|
||||||
"author_name": formatAuthorName,
|
|
||||||
"author_email": formatAuthorEmail,
|
|
||||||
"committer_name": formatCommitterName,
|
|
||||||
"committer_email": formatCommitterEmail,
|
|
||||||
"commit_notes": formatCommitNotes,
|
|
||||||
"signer": formatSigner,
|
|
||||||
} {
|
|
||||||
output, err := exec.Command("git", "log", "-1", prettyFormat+v, commit).Output()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c[k] = strings.TrimSpace(string(output))
|
c[v] = strings.TrimSpace(string(buf.Bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &c, nil
|
return &c, nil
|
||||||
|
|
55
git/commits_test.go
Normal file
55
git/commits_test.go
Normal 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
1
git/testdata/commits.json
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue