Skip to content

Commit 591c5da

Browse files
committed
refactor: extract some code into functions to improve readability
and remove revert/Revert from the type list, since they are already treated as special cases.
1 parent cf1b2aa commit 591c5da

3 files changed

Lines changed: 71 additions & 62 deletions

File tree

validator/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ var (
4444
"build": {}, // build process 构建过程
4545
"ci": {}, // continuous integration 持续集成相关
4646
"docker": {}, // 容器相关
47-
"revert": {}, // 撤销以前的 commit
48-
"Revert": {}, // 有些工具生成的 revert 首字母大写
4947
}
5048
// TypesStr ...
5149
TypesStr string

validator/validate.go

Lines changed: 70 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func getMsg(path string) string {
4141
}
4242

4343
f, err := os.Stat(path)
44-
if err != nil {
44+
if err != nil && !os.IsExist(err) {
4545
log.Println(err)
4646
state.FileMissing.LogAndExit(path)
4747
}
@@ -60,48 +60,55 @@ func getMsg(path string) string {
6060
return string(buf)
6161
}
6262

63-
func checkEmpty(str string) bool {
64-
return strings.TrimSpace(str) == ""
65-
}
66-
67-
func checkType(typ string) {
68-
for t := range TypeSet {
69-
if typ == t {
70-
return
71-
}
63+
func validateMsg(msg string, config *globalConfig) {
64+
if isEmpty(msg) {
65+
state.EmptyMessage.LogAndExit()
7266
}
73-
state.WrongType.LogAndExit(typ, TypesStr)
74-
}
7567

76-
func checkScope(scope string, config *globalConfig) {
77-
if checkEmpty(scope) {
78-
if config.ScopeRequired {
79-
state.ScopeMissing.LogAndExit()
80-
}
81-
return
68+
isMergeCommit(msg)
69+
70+
sections := strings.SplitN(msg, "\n", 2)
71+
72+
if config.LineLimit <= 0 {
73+
config.LineLimit = 80
8274
}
8375

84-
if len(config.Scopes) == 0 {
85-
return
76+
checkHeader(sections[0], config)
77+
78+
if len(sections) == 2 {
79+
checkBody(sections[1], config)
80+
} else if config.BodyRequired {
81+
state.BodyMissing.LogAndExit()
8682
}
8783

88-
for _, s := range config.Scopes {
89-
if scope == s {
90-
return
91-
}
84+
state.Validated.LogAndExit()
85+
}
86+
87+
func isEmpty(str string) bool {
88+
return strings.TrimSpace(str) == ""
89+
}
90+
91+
func isMergeCommit(msg string) {
92+
if strings.HasPrefix(msg, mergePrefix) {
93+
state.Merge.LogAndExit()
9294
}
93-
state.WrongScope.LogAndExit(scope, strings.Join(config.Scopes, ", "))
9495
}
9596

9697
func checkHeader(header string, config *globalConfig) {
97-
if checkEmpty(header) {
98+
if isEmpty(header) {
9899
state.EmptyHeader.LogAndExit()
99100
}
100101

102+
if isRevertHeader(header) {
103+
// skip revert header checking
104+
return
105+
// but later body check is still required
106+
}
107+
101108
re := regexp.MustCompile(headerPattern)
102109
groups := re.FindStringSubmatch(header)
103110

104-
if groups == nil || checkEmpty(groups[5]) {
111+
if groups == nil || isEmpty(groups[5]) {
105112
state.BadHeaderFormat.LogAndExit(header)
106113
}
107114

@@ -115,60 +122,64 @@ func checkHeader(header string, config *globalConfig) {
115122
// TODO: 根据规则对subject检查
116123
// subject := groups[5]
117124

118-
if config.LineLimit <= 0 {
119-
config.LineLimit = 80
120-
}
121125
length := len(header)
122126
if length > config.LineLimit &&
123127
!(isFixupOrSquash || typ == "revert" || typ == "Revert") {
124128
state.LineOverLong.LogAndExit(length, config.LineLimit, header)
125129
}
126130
}
127131

132+
func isRevertHeader(header string) bool {
133+
m, _ := regexp.MatchString(revertPattern, header)
134+
return m
135+
}
136+
137+
func checkType(typ string) {
138+
for t := range TypeSet {
139+
if typ == t {
140+
return
141+
}
142+
}
143+
state.WrongType.LogAndExit(typ, TypesStr)
144+
}
145+
146+
func checkScope(scope string, config *globalConfig) {
147+
if isEmpty(scope) {
148+
if config.ScopeRequired {
149+
state.ScopeMissing.LogAndExit()
150+
}
151+
return
152+
}
153+
154+
if len(config.Scopes) == 0 {
155+
return
156+
}
157+
158+
for _, s := range config.Scopes {
159+
if scope == s {
160+
return
161+
}
162+
}
163+
state.WrongScope.LogAndExit(scope, strings.Join(config.Scopes, ", "))
164+
}
165+
128166
func checkBody(body string, config *globalConfig) {
129-
if checkEmpty(body) {
167+
if isEmpty(body) {
130168
if config.BodyRequired {
131169
state.BodyMissing.LogAndExit()
132170
} else {
133171
state.Validated.LogAndExit()
134172
}
135173
}
136174

137-
if !checkEmpty(strings.SplitN(body, "\n", 2)[0]) {
175+
if !isEmpty(strings.SplitN(body, "\n", 2)[0]) {
138176
state.NoBlankLineBeforeBody.LogAndExit()
139177
}
140178

141-
if config.LineLimit <= 0 {
142-
config.LineLimit = 80
143-
}
144179
for _, line := range strings.Split(body, "\n") {
145180
length := len(line)
146181
if length > config.LineLimit {
147182
state.LineOverLong.LogAndExit(length, config.LineLimit, line)
148183
}
149184
}
150185
}
151-
152-
func validateMsg(msg string, config *globalConfig) {
153-
if checkEmpty(msg) {
154-
state.EmptyMessage.LogAndExit()
155-
}
156-
157-
if strings.HasPrefix(msg, mergePrefix) {
158-
state.Merge.LogAndExit()
159-
}
160-
161-
sections := strings.SplitN(msg, "\n", 2)
162-
163-
if m, _ := regexp.MatchString(revertPattern, sections[0]); !m {
164-
checkHeader(sections[0], config)
165-
}
166-
167-
if len(sections) == 2 {
168-
checkBody(sections[1], config)
169-
} else if config.BodyRequired {
170-
state.BodyMissing.LogAndExit()
171-
}
172-
173-
state.Validated.LogAndExit()
174-
}

validator/validate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestCheckEmpty(t *testing.T) {
8080
}
8181
for _, tt := range emptyCases {
8282
t.Run("checkEmpty", func(t *testing.T) {
83-
if got := checkEmpty(tt.text); got != tt.want {
83+
if got := isEmpty(tt.text); got != tt.want {
8484
t.Errorf(`checkEmpty("%s")=%v, want %v`, tt.text, got, tt.want)
8585
}
8686
})

0 commit comments

Comments
 (0)