-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
67 lines (50 loc) · 1.35 KB
/
option.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
package convention
import (
"errors"
"fmt"
"regexp"
"strings"
"time"
"github.com/make-go-great/date-go"
"github.com/haunt98/changeloguru/internal/git"
)
const (
leftScope = "("
rightScope = ")"
)
var (
ErrEmptyCommit = errors.New("empty commit")
headerRegex = regexp.MustCompile(`(?P<type>[a-zA-Z]+)(?P<scope>\([a-zA-Z]+\))?(?P<attention>!)?:\s(?P<description>.+)`)
)
type OptionFn func(*Commit) error
func GetRawHeader(gitCommit git.Commit) OptionFn {
return func(c *Commit) error {
if gitCommit.Message == "" {
return ErrEmptyCommit
}
message := strings.TrimSpace(gitCommit.Message)
messages := strings.Split(message, "\n")
c.RawHeader = messages[0]
return nil
}
}
func GetTypeAndScope(gitCommit git.Commit) OptionFn {
return func(c *Commit) error {
if !headerRegex.MatchString(c.RawHeader) {
c.Type = MiscType
return nil
}
headerSubmatches := headerRegex.FindStringSubmatch(c.RawHeader)
c.Type = strings.ToLower(headerSubmatches[1])
c.Scope = strings.ToLower(headerSubmatches[2])
c.Scope = strings.TrimLeft(c.Scope, leftScope)
c.Scope = strings.TrimRight(c.Scope, rightScope)
return nil
}
}
func AddAuthorDate(gitCommit git.Commit) OptionFn {
return func(c *Commit) error {
c.RawHeader = fmt.Sprintf("%s (%s)", c.RawHeader, date.FormatDateByDefault(gitCommit.Author.When, time.Local))
return nil
}
}