-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit.go
49 lines (37 loc) · 959 Bytes
/
commit.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
package main
import (
"errors"
"regexp"
)
type CType *string
type CScope *string
type CDesc *string
func ParseConventionalCommit(msg string) (cType CType, cScope CScope, cDesc CDesc, err error) {
reType := regexp.MustCompile(`^(\w+)(?:\(\w+\))?!?:\s`)
resType := reType.FindStringSubmatch(msg)
reScope := regexp.MustCompile(`\(([\w\d\s]+)\)!?:\s`)
resScope := reScope.FindStringSubmatch(msg)
reDesc := regexp.MustCompile(`: (.+)\n`)
resDesc := reDesc.FindStringSubmatch(msg)
if len(resType) > 1 {
cType = &resType[1]
} else {
return nil, nil, nil, errors.New("could not identify type")
}
if len(resScope) > 1 {
cScope = &resScope[1]
}
if len(resDesc) > 1 {
cDesc = &resDesc[1]
} else {
return nil, nil, nil, errors.New("could not identify description")
}
return cType, cScope, cDesc, nil
}
func ParseCommit(msg string) error {
_, _, _, err := ParseConventionalCommit(msg)
if err != nil {
return err
}
return nil
}