Skip to content

Commit 00823a3

Browse files
committed
message_regexp: regular expression rule for messages
Fixes: #30 now you can add a regular expression to the rules to be run i.e. `git-validation -run "dco,message_regexp='^JIRA-[0-9]+ [A-Z].*$'"` Signed-off-by: Vincent Batts <[email protected]>
1 parent d9bf419 commit 00823a3

File tree

8 files changed

+89
-10
lines changed

8 files changed

+89
-10
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ Usage of git-validation:
3030
The entire default rule set is run by default:
3131
```console
3232
vbatts@valse ~/src/vb/git-validation (master) $ git-validation -list-rules
33+
"dangling-whitespace" -- checking the presence of dangling whitespaces on line endings
3334
"DCO" -- makes sure the commits are signed
35+
"message_regexp" -- checks the commit message for a user provided regular expression
3436
"short-subject" -- commit subjects are strictly less than 90 (github ellipsis length)
3537
```
3638

@@ -92,6 +94,7 @@ vbatts@valse ~/src/vb/git-validation (master) $ GIT_CHECK_EXCLUDE="./vendor" git
9294
```
9395
using the `GIT_CHECK_EXCLUDE` environment variable
9496

97+
9598
## Rules
9699

97100
Default rules are added by registering them to the `validate` package.

main.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
_ "github.com/vbatts/git-validation/rules/danglingwhitespace"
1111
_ "github.com/vbatts/git-validation/rules/dco"
12+
_ "github.com/vbatts/git-validation/rules/messageregexp"
1213
_ "github.com/vbatts/git-validation/rules/shortsubject"
1314
"github.com/vbatts/git-validation/validate"
1415
)
@@ -47,10 +48,20 @@ func main() {
4748
return
4849
}
4950

50-
// reduce the set being run
51-
rules := validate.RegisteredRules
51+
// rules to be used
52+
var rules []validate.Rule
53+
for _, r := range validate.RegisteredRules {
54+
// only those that are Default
55+
if r.Default {
56+
rules = append(rules, r)
57+
}
58+
}
59+
// or reduce the set being run to what the user provided
5260
if *flRun != "" {
53-
rules = validate.FilterRules(rules, validate.SanitizeFilters(*flRun))
61+
rules = validate.FilterRules(validate.RegisteredRules, validate.SanitizeFilters(*flRun))
62+
}
63+
if os.Getenv("DEBUG") != "" {
64+
log.Printf("%#v", rules) // XXX maybe reduce this list
5465
}
5566

5667
var commitRange = *flCommitRange

rules/danglingwhitespace/rule.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var (
1212
Name: "dangling-whitespace",
1313
Description: "checking the presence of dangling whitespaces on line endings",
1414
Run: ValidateDanglingWhitespace,
15+
Default: true,
1516
}
1617
)
1718

@@ -20,7 +21,7 @@ func init() {
2021
}
2122

2223
// ValidateDanglingWhitespace runs Git's check to look for whitespace errors.
23-
func ValidateDanglingWhitespace(c git.CommitEntry) (vr validate.Result) {
24+
func ValidateDanglingWhitespace(r validate.Rule, c git.CommitEntry) (vr validate.Result) {
2425
vr.CommitEntry = c
2526
vr.Msg = "commit does not have any whitespace errors"
2627
vr.Pass = true

rules/dco/dco.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ var (
2020
Name: "DCO",
2121
Description: "makes sure the commits are signed",
2222
Run: ValidateDCO,
23+
Default: true,
2324
}
2425
)
2526

2627
// ValidateDCO checks that the commit has been signed off, per the DCO process
27-
func ValidateDCO(c git.CommitEntry) (vr validate.Result) {
28+
func ValidateDCO(r validate.Rule, c git.CommitEntry) (vr validate.Result) {
2829
vr.CommitEntry = c
2930
if len(strings.Split(c["parent"], " ")) > 1 {
3031
vr.Pass = true

rules/messageregexp/rule.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package messageregexp
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
8+
"github.com/vbatts/git-validation/git"
9+
"github.com/vbatts/git-validation/validate"
10+
)
11+
12+
func init() {
13+
validate.RegisterRule(RegexpRule)
14+
}
15+
16+
var (
17+
// RegexpRule for validating a user provided regex on the commit messages
18+
RegexpRule = validate.Rule{
19+
Name: "message_regexp",
20+
Description: "checks the commit message for a user provided regular expression",
21+
Run: ValidateMessageRegexp,
22+
Default: false, // only for users specifically calling it through -run ...
23+
}
24+
)
25+
26+
// ValidateMessageRegexp is the message regex func to run
27+
func ValidateMessageRegexp(r validate.Rule, c git.CommitEntry) (vr validate.Result) {
28+
if r.Value == "" {
29+
vr.Pass = true
30+
vr.Msg = "noop: message_regexp value is blank"
31+
return vr
32+
}
33+
34+
re := regexp.MustCompile(r.Value)
35+
vr.CommitEntry = c
36+
if len(strings.Split(c["parent"], " ")) > 1 {
37+
vr.Pass = true
38+
vr.Msg = "merge commits are not checked for message_regexp"
39+
return vr
40+
}
41+
42+
hasValid := false
43+
for _, line := range strings.Split(c["subject"], "\n") {
44+
if re.MatchString(line) {
45+
hasValid = true
46+
}
47+
}
48+
for _, line := range strings.Split(c["body"], "\n") {
49+
if re.MatchString(line) {
50+
hasValid = true
51+
}
52+
}
53+
if !hasValid {
54+
vr.Pass = false
55+
vr.Msg = fmt.Sprintf("commit message does not match %q", r.Value)
56+
} else {
57+
vr.Pass = true
58+
vr.Msg = fmt.Sprintf("commit message matches %q", r.Value)
59+
}
60+
return vr
61+
}

rules/shortsubject/shortsubject.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var (
1111
Name: "short-subject",
1212
Description: "commit subjects are strictly less than 90 (github ellipsis length)",
1313
Run: ValidateShortSubject,
14+
Default: true,
1415
}
1516
)
1617

@@ -20,7 +21,7 @@ func init() {
2021

2122
// ValidateShortSubject checks that the commit's subject is strictly less than
2223
// 90 characters (preferably not more than 72 chars).
23-
func ValidateShortSubject(c git.CommitEntry) (vr validate.Result) {
24+
func ValidateShortSubject(r validate.Rule, c git.CommitEntry) (vr validate.Result) {
2425
if len(c["subject"]) >= 90 {
2526
vr.Pass = false
2627
vr.Msg = "commit subject exceeds 90 characters"

validate/rules.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
var (
12-
// RegisteredRules are the standard validation to perform on git commits
12+
// RegisteredRules are the avaible validation to perform on git commits
1313
RegisteredRules = []Rule{}
1414
registerRuleLock = sync.Mutex{}
1515
)
@@ -26,14 +26,15 @@ type Rule struct {
2626
Name string // short name for reference in in the `-run=...` flag
2727
Value string // value to configure for the rule (i.e. a regexp to check for in the commit message)
2828
Description string // longer Description for readability
29-
Run func(git.CommitEntry) Result
29+
Run func(Rule, git.CommitEntry) Result
30+
Default bool // whether the registered rule is run by default
3031
}
3132

3233
// Commit processes the given rules on the provided commit, and returns the result set.
3334
func Commit(c git.CommitEntry, rules []Rule) Results {
3435
results := Results{}
3536
for _, r := range rules {
36-
results = append(results, r.Run(c))
37+
results = append(results, r.Run(r, c))
3738
}
3839
return results
3940
}

validate/rules_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestSliceHelpers(t *testing.T) {
4646
for i := range set {
4747
got := StringsSliceEqual(set[i].A, set[i].B)
4848
if got != set[i].Equal {
49-
t.Errorf("expected %d A and B comparison to be %q, but got %q", i, set[i].Equal, got)
49+
t.Errorf("expected %d A and B comparison to be %t, but got %t", i, set[i].Equal, got)
5050
}
5151
}
5252
}

0 commit comments

Comments
 (0)