1
0
Fork 0
mirror of https://github.com/vbatts/git-validation.git synced 2024-12-26 23:46:30 +00:00

Merge pull request #38 from vbatts/fix37

git: compare version of git for known features
This commit is contained in:
Vincent Batts 2018-09-25 19:12:05 +01:00 committed by GitHub
commit bc61a7ad0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,7 @@ import (
"os/exec"
"strings"
version "github.com/hashicorp/go-version"
"github.com/sirupsen/logrus"
)
@ -59,6 +60,35 @@ var FieldNames = map[string]string{
"%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) {
@ -67,9 +97,15 @@ func Check(commit string) ([]byte, error) {
fmt.Sprintf("%s^..%s", commit, commit),
}
if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" {
excludeList := strings.Split(excludeEnvList, ":")
for _, exclude := range excludeList {
args = append(args, "--", ".", fmt.Sprintf(":(exclude)%s", exclude))
gitNewEnough, err := gitVersionNewerThan(versionWithExcludes)
if err != nil {
return nil, err
}
if gitNewEnough {
excludeList := strings.Split(excludeEnvList, ":")
for _, exclude := range excludeList {
args = append(args, "--", ".", fmt.Sprintf(":(exclude)%s", exclude))
}
}
}
cmd := exec.Command("git", args...)