-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
84 lines (67 loc) · 1.88 KB
/
provider.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"bytes"
"fmt"
"os/exec"
"regexp"
"strings"
"unicode/utf8"
log "github.com/charmbracelet/log"
)
type Provider interface {
Type() ConventionType
Construct() (string, error)
}
func startsWithColonOrEmoji(s string) (bool, string) {
if strings.HasPrefix(s, ":") {
return true, "code"
}
// Check if the string starts with an emoji
firstRune, _ := utf8.DecodeRuneInString(s)
if isEmoji(string(firstRune)) {
return true, "emoji"
}
return false, ""
}
func isEmoji(s string) bool {
emojiPattern := regexp.MustCompile(`^[\x{1F600}-\x{1F64F}]|[\x{1F300}-\x{1F5FF}]|[\x{1F680}-\x{1F6FF}]|[\x{1F1E0}-\x{1F1FF}]|[\x{2600}-\x{26FF}]|[\x{2700}-\x{27BF}]`)
return emojiPattern.MatchString(s)
}
func determineConventionFromCommitMessage() (Provider, error) {
// Get last commit message excluding merge commits
msg, err := exec.Command("git", "log", "-1", "--no-merges", "--pretty=%B").Output()
if err != nil {
return nil, err
}
msg = bytes.TrimSpace(msg)
log.Debug("last commit message", "message", string(msg))
usesGitmoji, notation := startsWithColonOrEmoji(string(msg))
if usesGitmoji {
return NewGitmoji(notation), nil
} else {
return NewConventional(), nil
}
}
func determineConvention() (Provider, error) {
cfg, err := ReadConfig()
if err != nil {
log.Debug("error reading config", "error", err)
fallback, err := determineConventionFromCommitMessage()
if err != nil {
// TODO: Let user configure a default convention
return NewConventional(), nil
}
log.Debug("falling back to last used convention", "provider", fallback.Type())
return fallback, nil
}
var provider Provider
switch cfg.Convention {
case ConventionalCommitConvention:
provider = NewConventional()
case GitmojiConvention:
provider = NewGitmoji("code")
default:
return nil, fmt.Errorf("unsupported convention type: %s", cfg.Convention)
}
return provider, nil
}