/
agent.go
132 lines (114 loc) · 2.44 KB
/
agent.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package agent
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/go-git/go-git/v5"
)
type (
Agent struct {
Config *Config
repo *git.Repository
workTree *git.Worktree
commitTitle string
}
Config struct {
Commit Commit
Push Push
}
Commit struct {
Long bool
Breaking bool
}
Push struct {
Force bool
Tags bool
}
)
func New(config *Config) *Agent {
return &Agent{
Config: config,
}
}
func (a *Agent) Init() (err error) {
a.repo, err = git.PlainOpen(".")
if err != nil {
return
}
a.workTree, err = a.repo.Worktree()
return
}
func (a *Agent) Title() error {
a.commitTitle = fmt.Sprintf("%s%s: %s", a.label(), scope(), synopsis())
return nil
}
func (a *Agent) Add() error {
return try(exec.Command("git", "add", "."))
}
func (a *Agent) Commit() error {
if a.Config.Commit.Long {
return a.longCommit()
} else {
return a.shortCommit()
}
}
func (a *Agent) Push() error {
args := []string{"push"}
if a.Config.Push.Force {
args = append(args, "--force")
}
if a.Config.Push.Tags {
args = append(args, "--tags")
}
return try(exec.Command("git", args...))
}
func (a *Agent) longCommit() error {
path, err := a.createCommitTemplate()
if err != nil {
return fmt.Errorf("failed to create commit template file: %s", err)
}
msg, err := editCommitTemplate(path)
if err != nil {
return fmt.Errorf("failed to edit template: %s", err)
}
_, err = a.workTree.Commit(msg, &git.CommitOptions{})
return err
}
func (a *Agent) createCommitTemplate() (string, error) {
file, err := os.CreateTemp("", "sema-commit-template-")
if err != nil {
return "", err
}
defer file.Close()
_, err = file.WriteString(a.commitTitle + "\n\n" + a.maybeBreakingSuffix())
return file.Name(), err
}
func editCommitTemplate(path string) (string, error) {
if err := try(exec.Command(editor(), path)); err != nil {
return "", err
}
return readCommitMessageFromTemplate(path)
}
func editor() string {
output, err := exec.Command("git", "var", "GIT_EDITOR").Output()
if err != nil {
return defaultGitEditor
}
return strings.TrimSpace(string(output))
}
func readCommitMessageFromTemplate(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", nil
}
defer file.Close()
contents, err := io.ReadAll(file)
return string(contents), err
}
func (a *Agent) shortCommit() error {
display(a.commitTitle)
_, err := a.workTree.Commit(a.commitTitle, &git.CommitOptions{})
return err
}