Skip to content

Commit 920e156

Browse files
committed
fix: revert commit generated by tortoiseGit validate fail
cause: only allow `revert:` or `Revert:` but tortoiseGit generate `Revert ` solve: adapt to the tortoiseGit format fix #1
1 parent f0a086f commit 920e156

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

commit-msg.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ func checkHeader(header string) {
7474
checkType(typ)
7575

7676
isFixupOrSquash := (groups[2] != "")
77-
// scope := groups[4] // TODO: 根据配置对scope检查
78-
// subject := groups[5] // TODO: 根据规则对subject检查
77+
// TODO: 根据配置对scope检查
78+
// scope := groups[4]
79+
// TODO: 根据规则对subject检查
80+
// subject := groups[5]
7981

8082
length := len(header)
8183
if length > Config.LineLimit &&
@@ -115,7 +117,10 @@ func validateMsg(msg string) {
115117
}
116118

117119
sections := strings.SplitN(msg, "\n", 2)
118-
checkHeader(sections[0])
120+
121+
if m, _ := regexp.MatchString(revertPattern, sections[0]); !m {
122+
checkHeader(sections[0])
123+
}
119124

120125
if len(sections) == 2 {
121126
checkBody(sections[1])

commit-msg_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,33 @@ func TestGetMsg(t *testing.T) {
1111
}
1212
}
1313

14+
func handleState(t *testing.T, s interface{}, expected msgState) {
15+
state, ok := s.(msgState)
16+
if !ok || state != expected {
17+
t.Errorf("Failed! %v", s)
18+
}
19+
}
20+
1421
func TestValidatedSample(t *testing.T) {
1522
defer func() {
16-
err := recover()
17-
state, ok := err.(msgState)
18-
if !ok || state != Validated {
19-
t.Errorf("Failed! %v", err)
20-
}
23+
handleState(t, recover(), Validated)
2124
}()
2225

2326
validateMsg(getMsg("testcase/sample.txt"))
2427
}
2528

26-
func TestTortoiseGitMerge(t *testing.T) {
29+
func TestMerge(t *testing.T) {
2730
defer func() {
28-
err := recover()
29-
state, ok := err.(msgState)
30-
if !ok || state != Merge {
31-
t.Errorf("Failed! %v", err)
32-
}
31+
handleState(t, recover(), Merge)
3332
}()
3433

3534
validateMsg(getMsg("testcase/tortoiseGitMerge.txt"))
3635
}
36+
37+
func TestRevert(t *testing.T) {
38+
defer func() {
39+
handleState(t, recover(), Validated)
40+
}()
41+
42+
validateMsg(getMsg("testcase/tortoiseGitRevert.txt"))
43+
}

config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
const (
10-
mergePrefix = `Merge `
10+
mergePrefix = "Merge "
11+
revertPattern = `^(Revert|revert)(:| ).+`
1112
headerPattern = `^((fixup! |squash! )?(\w+)(?:\(([^\)\s]+)\))?: (.+))(?:\n|$)`
1213
configFileName = "commit-msg.cfg.json"
1314
hookDir = "./.git/hooks/"

testcase/tortoiseGitRevert.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Revert "fix: abc issue & xyz problems, some words to make it lonnnnnnnnger"
2+
3+
This reverts commit 1234567890abcdef1234567890abcdef12345678.

0 commit comments

Comments
 (0)