fix(deps): update module github.com/vbatts/git-validation to v1.2.1

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
This commit is contained in:
renovate[bot]
2023-10-23 17:03:31 +00:00
committed by GitHub
parent 19c870da0d
commit a8e1a5ed1b
38 changed files with 3651 additions and 17 deletions

View File

@@ -102,6 +102,14 @@ vbatts@valse ~/src/vb/git-validation (master) $ GIT_CHECK_EXCLUDE="./vendor:./gi
using the `GIT_CHECK_EXCLUDE` environment variable. Multiple paths should be separated by colon(`:`)
## contributing
When making a change, verify it with:
```shell
go run mage.go lint vet build test
```
## Rules
Default rules are added by registering them to the `validate` package.

View File

@@ -15,13 +15,19 @@ import (
// 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) {
// TEST if it has _enough_ of a range from rev-list
commitrange, err := checkRevList(commitrange)
if err != nil {
logrus.Errorf("failed to validate the git commit range: %s", err)
return nil, err
}
cmdArgs := []string{"git", "rev-list", 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, " "))
logrus.Errorf("[git] cmd: %q", strings.Join(cmdArgs, " "))
return nil, err
}
if len(output) == 0 {
@@ -39,6 +45,39 @@ func Commits(commitrange string) ([]CommitEntry, error) {
return commits, nil
}
// Since the commitrange requested may be longer than the depth being cloned in CI,
// check for an error, if so do a git log to get the oldest available commit for a reduced range.
func checkRevList(commitrange string) (string, error) {
cmdArgs := []string{"git", "rev-list", commitrange}
if debug() {
logrus.Infof("[git] cmd: %q", strings.Join(cmdArgs, " "))
}
_, err := exec.Command(cmdArgs[0], cmdArgs[1:]...).Output()
if err == nil {
// no issues, return now
return commitrange, nil
}
cmdArgs = []string{"git", "log", "--pretty=oneline"}
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
}
// This "output" is now the list of available commits and short description.
// We want the last commit hash only.. (i.e. `| tail -n1 | awk '{ print $1 }'`)
chunks := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(chunks) == 1 {
return strings.Split(chunks[0], " ")[0], nil
}
last := chunks[len(chunks)-1]
lastCommit := strings.Split(last, " ")[0]
return fmt.Sprintf("%s..HEAD", lastCommit), 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{

View File

@@ -0,0 +1,119 @@
//go:build mage
// +build mage
package main
import (
"fmt"
"io"
"os"
"os/exec"
"github.com/fatih/color"
"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
)
var (
// Default target to run when none is specified
// If not set, running mage will list available targets
Default = Build
app string = "git-validation"
Stdout = cw{c: color.New(color.FgGreen), o: os.Stdout}
Stderr = cw{c: color.New(color.FgRed), o: os.Stderr}
)
// hack around color.Color not implementing Write()
type cw struct {
c *color.Color
o io.Writer
}
func (cw cw) Write(p []byte) (int, error) {
i := len(p)
_, err := cw.c.Fprint(cw.o, string(p)) // discarding the number of bytes written for now...
return i, err
}
// A build step that requires additional params, or platform specific steps for example
func Build() error {
mg.Deps(InstallDeps)
fmt.Println("Building...")
cmd := exec.Command("go", "build", "-v", "-o", app, ".")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Vet the codes
func Vet() error {
fmt.Println("go vet...")
cmd := exec.Command("go", "vet", "./...")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Run the Linters
func Lint() error {
mg.Deps(InstallToolsLint)
fmt.Println("Linting...")
cmd := exec.Command("golangci-lint", "run")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Run the tests available
func Test() error {
fmt.Println("Testing...")
cmd := exec.Command("go", "test", "-v", "./...")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// A custom install step if you need your bin someplace other than go/bin
func Install() error {
mg.Deps(Build)
fmt.Println("Installing...")
return os.Rename(app, "/usr/local/bin/"+app)
}
// Manage your deps, or running package managers.
func InstallDeps() error {
mg.Deps(Tidy)
fmt.Println("Installing Deps...")
cmd := exec.Command("go", "get", "./...")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Tools used during build/dev/test
func InstallTools() error {
mg.Deps(InstallToolsLint)
return nil
}
func InstallToolsLint() error {
fmt.Println("Installing Deps...")
cmd := exec.Command("go", "install", "github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.2")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Tidy go modules
func Tidy() error {
fmt.Println("Tidy up...")
cmd := exec.Command("go", "mod", "tidy")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// Clean up after yourself
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll(app)
}

View File

@@ -3,10 +3,10 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/sirupsen/logrus"
_ "github.com/vbatts/git-validation/rules/danglingwhitespace"
_ "github.com/vbatts/git-validation/rules/dco"
_ "github.com/vbatts/git-validation/rules/messageregexp"
@@ -27,6 +27,10 @@ var (
flTravisPROnly = flag.Bool("travis-pr-only", true, "when on travis, only run validations if the CI-Build is checking pull-request build")
)
func init() {
logrus.SetOutput(os.Stderr)
}
func main() {
flag.Parse()
@@ -62,7 +66,7 @@ func main() {
rules = validate.FilterRules(validate.RegisteredRules, validate.SanitizeFilters(*flRun))
}
if os.Getenv("DEBUG") != "" {
log.Printf("%#v", rules) // XXX maybe reduce this list
logrus.Printf("%#v", rules) // XXX maybe reduce this list
}
var commitRange = *flCommitRange
@@ -80,13 +84,14 @@ func main() {
}
}
logrus.Infof("using commit range: %s", commitRange)
runner, err := validate.NewRunner(*flDir, rules, commitRange, *flVerbose)
if err != nil {
log.Fatal(err)
logrus.Fatal(err)
}
if err := runner.Run(); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}
_, fail := runner.Results.PassFail()
if fail > 0 {

View File

@@ -75,7 +75,6 @@ func SanitizeFilters(filtStr string) (filters []string) {
//
// Some `includes` rules have values assigned to them.
// i.e. -run "dco,message_regexp='^JIRA-[0-9]+ [A-Z].*$'"
//
func FilterRules(rules []Rule, includes []string) []Rule {
ret := []Rule{}
@@ -83,7 +82,7 @@ func FilterRules(rules []Rule, includes []string) []Rule {
for i := range includes {
if strings.Contains(includes[i], "=") {
chunks := strings.SplitN(includes[i], "=", 2)
if strings.ToLower(r.Name) == strings.ToLower(chunks[0]) {
if strings.EqualFold(r.Name, chunks[0]) {
// for these rules, the Name won't be unique per se. There may be
// multiple "regexp=" with different values. We'll need to set the
// .Value = chunk[1] and ensure r is dup'ed so they don't clobber
@@ -93,7 +92,7 @@ func FilterRules(rules []Rule, includes []string) []Rule {
ret = append(ret, newR)
}
} else {
if strings.ToLower(r.Name) == strings.ToLower(includes[i]) {
if strings.EqualFold(r.Name, includes[i]) {
ret = append(ret, r)
}
}

View File

@@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
"github.com/sirupsen/logrus"
"github.com/vbatts/git-validation/git"
)
@@ -29,7 +30,11 @@ func NewRunner(root string, rules []Rule, commitrange string, verbose bool) (*Ru
if err != nil {
return nil, err
}
defer os.Chdir(cwd)
defer func() {
if err := os.Chdir(cwd); err != nil {
logrus.Warnf("changing working directory to %q failed: %s", cwd, err)
}
}()
if err := os.Chdir(newroot); err != nil {
return nil, err
@@ -56,7 +61,11 @@ func (r *Runner) Run() error {
if err != nil {
return err
}
defer os.Chdir(cwd)
defer func() {
if err := os.Chdir(cwd); err != nil {
logrus.Warnf("changing working directory to %q failed: %s", cwd, err)
}
}()
if err := os.Chdir(r.Root); err != nil {
return err