2015-10-05 20:16:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2016-04-05 17:16:40 +00:00
|
|
|
"strings"
|
2015-10-05 20:16:52 +00:00
|
|
|
|
2016-04-05 14:11:34 +00:00
|
|
|
_ "github.com/vbatts/git-validation/rules/danglingwhitespace"
|
2015-10-05 20:16:52 +00:00
|
|
|
_ "github.com/vbatts/git-validation/rules/dco"
|
2017-10-09 19:49:59 +00:00
|
|
|
_ "github.com/vbatts/git-validation/rules/messageregexp"
|
2015-10-05 23:02:31 +00:00
|
|
|
_ "github.com/vbatts/git-validation/rules/shortsubject"
|
2015-10-05 20:16:52 +00:00
|
|
|
"github.com/vbatts/git-validation/validate"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-04-27 17:17:56 +00:00
|
|
|
flCommitRange = flag.String("range", "", "use this commit range instead (implies -no-travis)")
|
|
|
|
flListRules = flag.Bool("list-rules", false, "list the rules registered")
|
|
|
|
flRun = flag.String("run", "", "comma delimited list of rules to run. Defaults to all.")
|
|
|
|
flVerbose = flag.Bool("v", false, "verbose")
|
|
|
|
flDebug = flag.Bool("D", false, "debug output")
|
|
|
|
flQuiet = flag.Bool("q", false, "less output")
|
|
|
|
flDir = flag.String("d", ".", "git directory to validate from")
|
|
|
|
flNoTravis = flag.Bool("no-travis", false, "disables travis environment checks (when env TRAVIS=true is set)")
|
|
|
|
flTravisPROnly = flag.Bool("travis-pr-only", true, "when on travis, only run validations if the CI-Build is checking pull-request build")
|
2015-10-05 20:16:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-10-06 14:44:04 +00:00
|
|
|
if *flDebug {
|
|
|
|
os.Setenv("DEBUG", "1")
|
2015-10-05 22:45:33 +00:00
|
|
|
}
|
2016-01-14 15:29:35 +00:00
|
|
|
if *flQuiet {
|
|
|
|
os.Setenv("QUIET", "1")
|
|
|
|
}
|
2015-10-05 22:45:33 +00:00
|
|
|
|
2015-10-05 20:16:52 +00:00
|
|
|
if *flListRules {
|
|
|
|
for _, r := range validate.RegisteredRules {
|
|
|
|
fmt.Printf("%q -- %s\n", r.Name, r.Description)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:17:56 +00:00
|
|
|
if *flTravisPROnly && strings.ToLower(os.Getenv("TRAVIS_PULL_REQUEST")) == "false" {
|
|
|
|
fmt.Printf("only to check travis PR builds and this not a PR build. yielding.\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-09 19:49:59 +00:00
|
|
|
// rules to be used
|
|
|
|
var rules []validate.Rule
|
|
|
|
for _, r := range validate.RegisteredRules {
|
|
|
|
// only those that are Default
|
|
|
|
if r.Default {
|
|
|
|
rules = append(rules, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// or reduce the set being run to what the user provided
|
2015-10-05 22:29:24 +00:00
|
|
|
if *flRun != "" {
|
2017-10-09 19:49:59 +00:00
|
|
|
rules = validate.FilterRules(validate.RegisteredRules, validate.SanitizeFilters(*flRun))
|
|
|
|
}
|
|
|
|
if os.Getenv("DEBUG") != "" {
|
|
|
|
log.Printf("%#v", rules) // XXX maybe reduce this list
|
2015-10-05 22:29:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 17:16:40 +00:00
|
|
|
var commitRange = *flCommitRange
|
2017-01-27 05:13:02 +00:00
|
|
|
if commitRange == "" {
|
|
|
|
if strings.ToLower(os.Getenv("TRAVIS")) == "true" && !*flNoTravis {
|
2017-03-21 18:56:45 +00:00
|
|
|
if os.Getenv("TRAVIS_COMMIT_RANGE") != "" {
|
2017-03-21 22:06:01 +00:00
|
|
|
commitRange = strings.Replace(os.Getenv("TRAVIS_COMMIT_RANGE"), "...", "..", 1)
|
2017-01-27 05:13:02 +00:00
|
|
|
} else if os.Getenv("TRAVIS_COMMIT") != "" {
|
|
|
|
commitRange = os.Getenv("TRAVIS_COMMIT")
|
|
|
|
}
|
2016-04-05 17:16:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runner, err := validate.NewRunner(*flDir, rules, commitRange, *flVerbose)
|
2015-10-05 20:16:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-10-06 14:44:04 +00:00
|
|
|
if err := runner.Run(); err != nil {
|
|
|
|
log.Fatal(err)
|
2015-10-05 20:16:52 +00:00
|
|
|
}
|
2015-10-06 14:44:04 +00:00
|
|
|
_, fail := runner.Results.PassFail()
|
2015-10-05 20:16:52 +00:00
|
|
|
if fail > 0 {
|
2016-05-03 19:13:09 +00:00
|
|
|
fmt.Printf("%d commits to fix\n", fail)
|
2015-10-05 20:16:52 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-10-06 14:44:04 +00:00
|
|
|
|
2015-10-05 20:16:52 +00:00
|
|
|
}
|