Skip to content

Commit 8bd4195

Browse files
committed
git: compare version of git for known features
Fixes #37 Reported-by: Brent Baude <@baude> Signed-off-by: Vincent Batts <[email protected]>
1 parent 656ba47 commit 8bd4195

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

git/commits.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os/exec"
77
"strings"
88

9+
version "github.com/hashicorp/go-version"
910
"github.com/sirupsen/logrus"
1011
)
1112

@@ -59,6 +60,35 @@ var FieldNames = map[string]string{
5960
"%G?": "verification_flag",
6061
}
6162

63+
func gitVersion() (string, error) {
64+
cmd := exec.Command("git", "version")
65+
cmd.Stderr = os.Stderr
66+
buf, err := cmd.Output()
67+
if err != nil {
68+
return "", err
69+
}
70+
return strings.Fields(string(buf))[2], nil
71+
}
72+
73+
// https://github.com/vbatts/git-validation/issues/37
74+
var versionWithExcludes = "1.9.5"
75+
76+
func gitVersionNewerThan(otherV string) (bool, error) {
77+
gv, err := gitVersion()
78+
if err != nil {
79+
return false, err
80+
}
81+
v1, err := version.NewVersion(gv)
82+
if err != nil {
83+
return false, err
84+
}
85+
v2, err := version.NewVersion(otherV)
86+
if err != nil {
87+
return false, err
88+
}
89+
return v2.Equal(v1) || v2.LessThan(v1), nil
90+
}
91+
6292
// Check warns if changes introduce whitespace errors.
6393
// Returns non-zero if any issues are found.
6494
func Check(commit string) ([]byte, error) {
@@ -67,9 +97,15 @@ func Check(commit string) ([]byte, error) {
6797
fmt.Sprintf("%s^..%s", commit, commit),
6898
}
6999
if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" {
70-
excludeList := strings.Split(excludeEnvList, ":")
71-
for _, exclude := range excludeList {
72-
args = append(args, "--", ".", fmt.Sprintf(":(exclude)%s", exclude))
100+
gitNewEnough, err := gitVersionNewerThan(versionWithExcludes)
101+
if err != nil {
102+
return nil, err
103+
}
104+
if gitNewEnough {
105+
excludeList := strings.Split(excludeEnvList, ":")
106+
for _, exclude := range excludeList {
107+
args = append(args, "--", ".", fmt.Sprintf(":(exclude)%s", exclude))
108+
}
73109
}
74110
}
75111
cmd := exec.Command("git", args...)

0 commit comments

Comments
 (0)