1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
package git
import (
"fmt"
"os"
"os/exec"
"strings"
version "github.com/hashicorp/go-version"
"github.com/sirupsen/logrus"
)
// Commits returns a 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 an empty commit range, then nil is returned.
func Commits(commitrange string) ([]CommitEntry, error) {
cmdArgs := []string{"git", "--no-pager", "log", `--pretty=format:%H`, commitrange}
if debug() {
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
}
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
if err != nil {
logrus.Errorf("mm[git] cmd: %q", strings.Join(cmdArgs, " "))
return nil, err
}
if len(output) == 0 {
return nil, nil
}
commitHashes := strings.Split(strings.TrimSpace(string(output)), "\n")
commits := make([]CommitEntry, len(commitHashes))
for i, commitHash := range commitHashes {
c, err := LogCommit(commitHash)
if err != nil {
return commits, err
}
commits[i] = *c
}
return commits, nil
}
// 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",
}
func gitVersion() (string, error) {
cmd := exec.Command("git", "version")
cmd.Stderr = os.Stderr
buf, err := cmd.Output()
if err != nil {
return "", err
}
return strings.Fields(string(buf))[2], nil
}
// https://github.com/vbatts/git-validation/issues/37
var versionWithExcludes = "1.9.5"
func gitVersionNewerThan(otherV string) (bool, error) {
gv, err := gitVersion()
if err != nil {
return false, err
}
v1, err := version.NewVersion(gv)
if err != nil {
return false, err
}
v2, err := version.NewVersion(otherV)
if err != nil {
return false, err
}
return v2.Equal(v1) || v2.LessThan(v1), nil
}
// Check warns if changes introduce whitespace errors.
// Returns non-zero if any issues are found.
func Check(commit string) ([]byte, error) {
args := []string{
"--no-pager", "log", "--check",
fmt.Sprintf("%s^..%s", commit, commit),
}
if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" {
gitNewEnough, err := gitVersionNewerThan(versionWithExcludes)
if err != nil {
return nil, err
}
if gitNewEnough {
excludeList := strings.Split(excludeEnvList, ":")
for _, exclude := range excludeList {
if exclude == "" {
continue
}
args = append(args, "--", ".", fmt.Sprintf(":(exclude)%s", exclude))
}
}
}
cmd := exec.Command("git", args...)
if debug() {
logrus.Infof("[git] cmd: %q", strings.Join(cmd.Args, " "))
}
cmd.Stderr = os.Stderr
return cmd.Output()
}
// Show returns the diff of a commit.
//
// NOTE: This could be expensive for very large commits.
func Show(commit string) ([]byte, error) {
cmd := exec.Command("git", "--no-pager", "show", commit)
if debug() {
logrus.Infof("[git] cmd: %q", strings.Join(cmd.Args, " "))
}
cmd.Stderr = os.Stderr
return cmd.Output()
}
// CommitEntry represents a single commit's information from `git`.
// See also FieldNames
type CommitEntry map[string]string
// LogCommit assembles the full information on a commit from its commit hash
func LogCommit(commit string) (*CommitEntry, error) {
c := CommitEntry{}
for k, v := range FieldNames {
cmd := exec.Command("git", "--no-pager", "log", "-1", `--pretty=format:`+k+``, commit)
if debug() {
logrus.Infof("[git] cmd: %q", strings.Join(cmd.Args, " "))
}
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
logrus.Errorf("[git] cmd: %q", strings.Join(cmd.Args, " "))
return nil, err
}
c[v] = strings.TrimSpace(string(out))
}
return &c, nil
}
func debug() bool {
return len(os.Getenv("DEBUG")) > 0
}
// FetchHeadCommit returns the hash of FETCH_HEAD
func FetchHeadCommit() (string, error) {
cmdArgs := []string{"git", "--no-pager", "rev-parse", "--verify", "FETCH_HEAD"}
if debug() {
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
}
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
if err != nil {
logrus.Errorf("[git] cmd: %q", strings.Join(cmdArgs, " "))
return "", err
}
return strings.TrimSpace(string(output)), nil
}
// HeadCommit returns the hash of HEAD
func HeadCommit() (string, error) {
cmdArgs := []string{"git", "--no-pager", "rev-parse", "--verify", "HEAD"}
if debug() {
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
}
output, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
if err != nil {
logrus.Errorf("[git] cmd: %q", strings.Join(cmdArgs, " "))
return "", err
}
return strings.TrimSpace(string(output)), nil
}
|