-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
295 lines (257 loc) · 6.62 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"slices"
"strconv"
"strings"
)
var allTypes []*Type
var mapTypes map[string]*Type
func main() {
arg := ""
if len(os.Args) > 1 {
arg = os.Args[1]
}
switch arg {
case "", "emoji":
printHelp()
os.Exit(0)
case "write-config":
debugf("git.emoji %q", os.Args[1:])
loadConfig()
writeConfigFile(allTypes)
case "rev-parse":
// no setup hooks
execGit(os.Args[1:])
case gmoji(_prepareCommitMsg):
debugf("git.emoji %q", os.Args[1:])
loadConfig()
execPrepareCommitMsg(os.Args[2:])
case gmoji(_commitMsg):
debugf("git.emoji %q", os.Args[1:])
loadConfig()
execCommitMsg(os.Args[2:])
case "setup-hooks":
debugf("git.emoji %q", os.Args[1:])
setupHooks()
infof("✅ Successfully setup git hooks")
case "remove-hooks":
debugf("git.emoji %q", os.Args[1:])
removeHooks()
infof("✅ Successfully removed git hooks")
case "commit":
if !isOptOut() {
setupHooks()
loadConfig()
execCommit(os.Args[1:])
return
}
fallthrough
default:
setupHooks()
execGit(os.Args[1:])
}
}
func which(command string, env []string) string {
stdout := &strings.Builder{}
cmd := exec.Command("which", command)
cmd.Env = env
cmd.Stdout = stdout
err := cmd.Run()
if err != nil {
return ""
}
return strings.TrimSpace(stdout.String())
}
func execGit(args []string) {
debugf("%v %q", origGit, args)
cmd := exec.Command(origGit(), args...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
cmd.Env = os.Environ()
err := cmd.Run()
if err == nil {
return
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
exit(exitErr.ExitCode())
}
fatalf("%v", err)
}
func execGitx(args ...string) (string, string, error) {
debugf("%v %q", origGit, args)
stdout, stderr := &strings.Builder{}, &strings.Builder{}
cmd := exec.Command(origGit(), args...)
cmd.Stdout, cmd.Stderr = stdout, stderr
err := cmd.Run()
return strings.TrimSpace(stdout.String()), stderr.String(), err
}
func execCommit(args []string) {
getFlagType := func() (_ *Type, remain []string) {
for i, arg := range args {
if !strings.HasPrefix(arg, "-") {
continue
}
arg = strings.TrimPrefix(arg, "-")
arg = strings.TrimPrefix(arg, "-")
if t, ok := mapTypes[arg]; ok {
var _args []string
_args = append(_args, args[:i]...)
_args = append(_args, args[i+1:]...)
return t, _args
}
}
return nil, args
}
editMsgArg := func(emoji string) (_ []string, ok bool) {
for i, arg := range args {
if arg != "-m" && strings.HasPrefix(arg, "-m") {
msg := strings.TrimPrefix(arg, "-m")
args[i] = "-m" + emoji + " " + msg
ok = true
continue
}
if arg != "-m" {
continue
}
if len(args) > i+1 {
args[i+1] = emoji + " " + args[i+1]
ok = true
}
}
return args, ok
}
// skip prompt if there is no -m arg
if !slices.Contains(args, "-m") {
execGit(args)
return
}
// detect -feat, -ch, etc. or show prompt if missing
idx := 0
flagType, args := getFlagType()
if flagType == nil {
flagType, idx = askFlagType("")
}
if flagType == nil {
fatalf("Can not read emoji!")
return
}
// update -m 'message' with emoji
args, _ = editMsgArg(flagType.Icons[idx])
execGit(args)
}
func askFlagType(firstLine string) (_ *Type, idx int) {
reNum := regexp.MustCompile(`^\d+`)
reTxt := regexp.MustCompile(`^[a-z]+`)
parse := func(re *regexp.Regexp, s string) (string, string, bool) {
first := re.FindString(s)
return first, strings.TrimPrefix(s, first), first != ""
}
ft, ch := getType("Features"), getType("Chores")
fmt.Println()
fmt.Println("--- 👉 Please choose an emoji 👈 ----------------------")
fmt.Println()
printHelpEmojis(os.Stdout, "")
fmt.Printf("\nHINT: You can use command line flag to choose the type:\n")
fmt.Printf(" git commit -feat -m 'message' # %s Features\n", ft.Icons[0])
fmt.Printf(" git commit -ft -m 'message' # %s Features\n", ft.Icons[0])
fmt.Printf(" git commit -ft1 -m 'message' # %s Features\n", ft.Icons[1])
fmt.Printf(" git commit -ch -m 'message' # %s Chore\n", ch.Icons[0])
fmt.Printf(" git commit -ch1 -m 'message' # %s Chore\n", ch.Icons[1])
fmt.Println("")
if firstLine != "" {
fmt.Println("--- 👉 Your commit message 👈 -------------------------")
fmt.Printf("%s\n\n", firstLine)
}
input, prompt := "", "Enter a number or abbr or emoji (1 | 1a | ft | ft1): "
mapEmoji := mapEmojis()
for {
fmt.Printf("\r%s\r", strings.Repeat(" ", len(prompt)+len(input)+1))
fmt.Print(prompt)
input = readLine()
in := strings.Trim(input, "- \t\n")
if first, second, ok := parse(reNum, in); ok {
id := must(strconv.Atoi(first))
id--
if id < 0 || id >= len(allTypes) {
continue
}
typ := allTypes[id]
if second == "" {
return typ, 0
}
idx = int(second[0]-'a') + 1
if idx < 0 || idx >= len(typ.Icons) {
continue
}
return typ, idx
}
if first, second, ok := parse(reTxt, in); ok {
typ := mapTypes[first]
if typ == nil {
continue
}
if second == "" {
return typ, 0
}
var err error
idx, err = strconv.Atoi(second)
if err != nil {
continue
}
if idx < 0 || idx >= len(typ.Icons) {
continue
}
return typ, idx
}
if _, ok := mapEmoji[in]; ok {
return &Type{Icons: []string{in}}, 0
}
}
}
func printHelp() {
msg := `git.emoji - git commit with emoji
git.emoji is a tool to commit your changes with emoji. It provides a simple command-line interface to help you select the right emoji for your commit. It will setup git hooks to commit with emoji, and can be used as a wrapper of git.
SETUP:
git.emoji setup-hooks
USAGE:
git.emoji commit -feat -m 'message' # Features
git.emoji commit -ft -m 'message' # Features
git.emoji commit -ft1 -m 'message' # Features
git.emoji commit -ch -m 'message' # Chore
git.emoji commit -ch1 -m 'message' # Chore
OPTIONAL: add this to your .zshrc or .bashrc:
alias git=git.emoji
then use git.emoji as git alias:
git commit -feat -m 'message' # Features
git commit -ft -m 'message' # Features
git commit -ft1 -m 'message' # Features
git commit -ch -m 'message' # Chore
git commit -ch1 -m 'message' # Chore
CONFIG: run this command to customize your emoji:
git.emoji write-config
`
printf(os.Stderr, "%s", msg)
}
func printHelpEmojis(w io.Writer, prefix string) {
pr := func(format string, args ...any) {
must(fmt.Fprintf(w, format, args...))
}
for i, typ := range allTypes {
pr(prefix)
pr("% 3d. % 16s\t", i+1, typ.Name)
for _, icon := range typ.Icons {
pr("%s ", icon)
}
pr("\t ")
for _, alias := range typ.Alias {
pr("-%s ", alias)
}
pr("\n")
}
}