From 78001eac9923479a792189d1b3820878a3250492 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Fri, 1 Apr 2016 16:47:39 -0400 Subject: [PATCH] git: switch out the field names usage Signed-off-by: Vincent Batts --- git/commits.go | 57 +++++++------------------------------------------- 1 file changed, 7 insertions(+), 50 deletions(-) diff --git a/git/commits.go b/git/commits.go index e29c62f..0084877 100644 --- a/git/commits.go +++ b/git/commits.go @@ -2,9 +2,6 @@ package git import ( "bytes" - "encoding/json" - "fmt" - "log" "os" "os/exec" "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 single commit, all ancestor commits up through the hash provided. func Commits(commitrange string) ([]CommitEntry, error) { - cmdArgs := []string{"git", "log", prettyFormat + formatCommit, commitrange} + cmdArgs := []string{"git", "log", `--pretty=format:%H`, commitrange} if debug() { logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " ")) } @@ -65,58 +62,18 @@ var FieldNames = map[string]string{ // See also FieldNames type CommitEntry map[string]string -var ( - prettyFormat = `--pretty=format:` - formatAuthorEmail = `%aE` - formatAuthorName = `%aN` - formatBody = `%b` - formatCommit = `%H` - 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" }` -) - // LogCommit assembles the full information on a commit from its commit hash func LogCommit(commit string) (*CommitEntry, error) { 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{} - output := buf.Bytes() - if err := json.Unmarshal(output, &c); err != nil { - fmt.Println(string(output)) - return nil, err - } - - // 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 { + for k, v := range FieldNames { + cmd := exec.Command("git", "log", "-1", `--pretty=format:`+k+``, commit) + cmd.Stdout = buf + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { return nil, err } - c[k] = strings.TrimSpace(string(output)) + c[v] = strings.TrimSpace(string(buf.Bytes())) } return &c, nil