-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscm.go
69 lines (58 loc) · 1.42 KB
/
scm.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
package gitwrk
import (
"regexp"
"strings"
)
// SemanticCommitMessage is main structure that represent: 'type(scope): subject'
type SemanticCommitMessage struct {
Type string
Scope string
Subject string
}
var (
semanticReg *regexp.Regexp
)
func init() {
// 1st group is type, 3th group is scope, 4th group is subject
semanticReg, _ = regexp.Compile("^([a-zA-Z]+)(\\((.*)\\))?:(.*)$")
}
// ParseSemanticCommitMessage consume plain text of the commit message and
// produce the CommitMessage structure with type
// scope and subject.
//
// If commit message doesn't match to semantic commit message,
// then it's type NONE where subject is first line of messsage
func ParseSemanticCommitMessage(t string) *SemanticCommitMessage {
// get the first line
idx := strings.Index(t, "\n")
var line string
if idx > 0 {
line = t[0:idx]
} else {
line = t
}
// check if matching to semantic commit message
res := semanticReg.FindStringSubmatchIndex(line)
if len(res) == 10 { // it's 10 because 4 groups and main (4 + 1)*2 = 10
scmType := line[res[2]:res[3]]
scope := ""
if res[6] >= 0 {
scope = line[res[6]:res[7]]
}
subject := ""
if res[8] >= 0 {
subject = line[res[8]:res[9]]
}
return &SemanticCommitMessage{
Type: scmType,
Scope: scope,
Subject: subject,
}
}
// it's not a semantic commit message
return &SemanticCommitMessage{
Type: "none",
Scope: "",
Subject: t,
}
}