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

git: compare version of git for known features

Fixes #37

Reported-by: Brent Baude <@baude>
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2018-09-25 14:10:05 -04:00
parent 656ba47bb0
commit 8bd4195bb7
Signed by: vbatts
GPG key ID: 10937E57733F1362

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...)