Skip to content

Commit 6f71141

Browse files
committed
git: switch out the field names usage
Signed-off-by: Vincent Batts <[email protected]>
1 parent d74a248 commit 6f71141

File tree

1 file changed

+7
-50
lines changed

1 file changed

+7
-50
lines changed

git/commits.go

Lines changed: 7 additions & 50 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
}
@@ -65,58 +62,18 @@ var FieldNames = map[string]string{
6562
// See also FieldNames
6663
type CommitEntry map[string]string
6764

68-
var (
69-
prettyFormat = `--pretty=format:`
70-
formatAuthorEmail = `%aE`
71-
formatAuthorName = `%aN`
72-
formatBody = `%b`
73-
formatCommit = `%H`
74-
formatCommitNotes = `%N`
75-
formatCommitterEmail = `%cE`
76-
formatCommitterName = `%cN`
77-
formatSigner = `%GS`
78-
formatSubject = `%s`
79-
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" }`
80-
)
81-
8265
// LogCommit assembles the full information on a commit from its commit hash
8366
func LogCommit(commit string) (*CommitEntry, error) {
8467
buf := bytes.NewBuffer([]byte{})
85-
cmdArgs := []string{"git", "log", "-1", prettyFormat + formatMap, commit}
86-
if debug() {
87-
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
88-
}
89-
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
90-
cmd.Stdout = buf
91-
cmd.Stderr = os.Stderr
92-
93-
if err := cmd.Run(); err != nil {
94-
log.Println(strings.Join(cmd.Args, " "))
95-
return nil, err
96-
}
9768
c := CommitEntry{}
98-
output := buf.Bytes()
99-
if err := json.Unmarshal(output, &c); err != nil {
100-
fmt.Println(string(output))
101-
return nil, err
102-
}
103-
104-
// any user provided fields can't be sanitized for the mock-json marshal above
105-
for k, v := range map[string]string{
106-
"subject": formatSubject,
107-
"body": formatBody,
108-
"author_name": formatAuthorName,
109-
"author_email": formatAuthorEmail,
110-
"committer_name": formatCommitterName,
111-
"committer_email": formatCommitterEmail,
112-
"commit_notes": formatCommitNotes,
113-
"signer": formatSigner,
114-
} {
115-
output, err := exec.Command("git", "log", "-1", prettyFormat+v, commit).Output()
116-
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 {
11774
return nil, err
11875
}
119-
c[k] = strings.TrimSpace(string(output))
76+
c[v] = strings.TrimSpace(string(buf.Bytes()))
12077
}
12178

12279
return &c, nil

0 commit comments

Comments
 (0)