mirror of
https://github.com/vbatts/git-validation.git
synced 2024-11-22 16:15:40 +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:
parent
656ba47bb0
commit
8bd4195bb7
1 changed files with 39 additions and 3 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
version "github.com/hashicorp/go-version"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -59,6 +60,35 @@ var FieldNames = map[string]string{
|
||||||
"%G?": "verification_flag",
|
"%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.
|
// Check warns if changes introduce whitespace errors.
|
||||||
// Returns non-zero if any issues are found.
|
// Returns non-zero if any issues are found.
|
||||||
func Check(commit string) ([]byte, error) {
|
func Check(commit string) ([]byte, error) {
|
||||||
|
@ -67,9 +97,15 @@ func Check(commit string) ([]byte, error) {
|
||||||
fmt.Sprintf("%s^..%s", commit, commit),
|
fmt.Sprintf("%s^..%s", commit, commit),
|
||||||
}
|
}
|
||||||
if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" {
|
if excludeEnvList := os.Getenv("GIT_CHECK_EXCLUDE"); excludeEnvList != "" {
|
||||||
excludeList := strings.Split(excludeEnvList, ":")
|
gitNewEnough, err := gitVersionNewerThan(versionWithExcludes)
|
||||||
for _, exclude := range excludeList {
|
if err != nil {
|
||||||
args = append(args, "--", ".", fmt.Sprintf(":(exclude)%s", exclude))
|
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...)
|
cmd := exec.Command("git", args...)
|
||||||
|
|
Loading…
Reference in a new issue