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