-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit_message.go
69 lines (58 loc) · 1.4 KB
/
commit_message.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 main
import (
"bytes"
"fmt"
"strings"
"text/template"
)
type commitMessage struct {
Env string
Emoji string
Location string
Prompt string
Template string
Command string
Body string
}
func newCommitMessage(command *Command, options *Options) *commitMessage {
argParts := make([]string, len(command.Args))
for i, arg := range command.Args {
if strings.Contains(arg, " ") && !(strings.HasPrefix(arg, "'") && strings.HasSuffix(arg, "'")) {
argParts[i] = fmt.Sprintf("'%s'", arg)
} else {
argParts[i] = arg
}
}
envs := []string{}
if len(command.Envs) > 0 {
envs = append(envs, strings.Join(command.Envs, " "))
}
commandParts := append(envs, argParts...)
return &commitMessage{
Env: strings.Join(envs, " "),
Emoji: options.Emoji,
Prompt: options.Prompt,
Template: options.Template,
Command: strings.Join(commandParts, " "),
Body: command.Output,
}
}
func (m *commitMessage) newTemplate() (*template.Template, error) {
return template.New("commitMessage").Parse(m.Template + "\n\n{{.Body}}\n")
}
func (m *commitMessage) Build() (string, error) {
location, err := getLocation()
if err != nil {
return "", err
}
m.Location = location
tmpl, err := m.newTemplate()
if err != nil {
return "", err
}
buf := bytes.NewBuffer(nil)
if err := tmpl.Execute(buf, m); err != nil {
return "", err
}
return strings.TrimSpace(buf.String()), nil
}