forked from JayceChant/commit-msg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg.go
156 lines (128 loc) · 2.67 KB
/
commit-msg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
func logAndExit(state msgState, v ...interface{}) {
if state <= Merge {
log.Printf(state.Hint(), v...)
} else if state <= FileMissing {
log.Printf(state.Hint(), v...)
} else {
log.Printf(state.Hint(), v...)
log.Printf(Lang.Rule, Types)
}
panic(state)
}
func getMsg(path string) string {
if path == "" {
logAndExit(ArgumentMissing)
}
f, err := os.Stat(path)
if err != nil {
log.Println(err)
logAndExit(FileMissing, path)
}
if f.IsDir() {
log.Println(path, "is not a file.")
logAndExit(FileMissing, path)
}
buf, err := ioutil.ReadFile(path)
if err != nil {
log.Println(err)
logAndExit(ReadError, path)
}
return string(buf)
}
func checkEmpty(str string) bool {
return strings.TrimSpace(str) == ""
}
func checkType(typ string) {
for _, t := range TypeList {
if typ == t {
return
}
}
logAndExit(WrongType, typ, Types)
}
func checkHeader(header string) {
if checkEmpty(header) {
logAndExit(EmptyHeader)
}
re := regexp.MustCompile(headerPattern)
groups := re.FindStringSubmatch(header)
if groups == nil || checkEmpty(groups[5]) {
logAndExit(BadHeaderFormat, header)
}
typ := groups[3]
checkType(typ)
isFixupOrSquash := (groups[2] != "")
// TODO: 根据配置对scope检查
// scope := groups[4]
// TODO: 根据规则对subject检查
// subject := groups[5]
length := len(header)
if length > Config.LineLimit &&
!(isFixupOrSquash || typ == "revert" || typ == "Revert") {
logAndExit(LineOverLong, length, Config.LineLimit, header)
}
}
func checkBody(body string) {
if checkEmpty(body) {
if Config.BodyRequired {
logAndExit(BodyMissing)
} else {
logAndExit(Validated)
}
}
if !checkEmpty(strings.SplitN(body, "\n", 2)[0]) {
logAndExit(NoBlankLineBeforeBody)
}
for _, line := range strings.Split(body, "\n") {
length := len(line)
if length > Config.LineLimit {
logAndExit(LineOverLong, length, Config.LineLimit, line)
}
}
}
func validateMsg(msg string) {
if checkEmpty(msg) {
logAndExit(EmptyMessage)
}
if strings.HasPrefix(msg, mergePrefix) {
logAndExit(Merge)
}
sections := strings.SplitN(msg, "\n", 2)
if m, _ := regexp.MatchString(revertPattern, sections[0]); !m {
checkHeader(sections[0])
}
if len(sections) == 2 {
checkBody(sections[1])
} else if Config.BodyRequired {
logAndExit(BodyMissing)
}
logAndExit(Validated)
}
func main() {
msgFile := ""
if len(os.Args) > 1 {
msgFile = os.Args[1]
}
msg := getMsg(msgFile)
defer func() {
err := recover()
state, ok := err.(msgState)
if !ok {
panic(err)
}
if state <= Merge {
os.Exit(0)
} else {
os.Exit(int(state))
}
}()
validateMsg(msg)
}