-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
365 lines (307 loc) · 7.68 KB
/
main.go
File metadata and controls
365 lines (307 loc) · 7.68 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Command gitsloth generates Conventional Commit messages from staged Git
// changes using the OpenAI API. It can generate one or more suggestions,
// allow user selection, optionally confirm, and either create the commit
// or copy it to the clipboard.
//
// Usage:
//
// gitsloth Generate one message (with confirmation)
// gitsloth -g 3 Generate multiple messages and select one
// gitsloth [-a | --all] Stage all changes before generating
// gitsloth [-c | --clipboard] Copy message instead of committing
//
// Requirements:
// - Must be executed inside a Git repository
// - OPENAI_API_KEY environment variable must be set
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
func main() {
var all bool
var clipboard bool
var generate int
flag.BoolVar(&all, "all", false, "stage all changes before committing")
flag.BoolVar(&all, "a", false, "shorthand for --all")
flag.BoolVar(&clipboard, "clipboard", false, "copy selected message to clipboard")
flag.BoolVar(&clipboard, "c", false, "shorthand for --clipboard")
flag.IntVar(&generate, "generate", 1, "number of commit messages to generate")
flag.IntVar(&generate, "g", 1, "shorthand for --generate")
flag.Parse()
if generate < 1 {
fmt.Println("generate must be >= 1")
os.Exit(1)
}
if !isGitRepoHere() {
fmt.Println("Not inside a Git repository (.git not found here)")
os.Exit(1)
}
if all {
if err := stageAllChanges(); err != nil {
fmt.Println("Failed to stage changes:", err)
os.Exit(1)
}
}
ctx, err := buildGitContext()
if err != nil {
fmt.Println("Failed to build git context:", err)
os.Exit(1)
}
if strings.TrimSpace(ctx.Diff) == "" {
fmt.Println("No changes to commit")
os.Exit(0)
}
messages, err := generateCommitMessages(*ctx, generate)
if err != nil {
fmt.Println("Failed to generate commit messages:", err)
os.Exit(1)
}
message, ok := selectMessage(messages)
if !ok {
fmt.Println("Operation aborted")
os.Exit(0)
}
if generate == 1 && !askForConfirmation(message) {
fmt.Println("Operation aborted")
os.Exit(0)
}
if clipboard {
if err := copyToClipBoard(message); err != nil {
fmt.Println("Failed to copy to clipboard:", err)
os.Exit(1)
}
fmt.Println("Message copied to clipboard")
return
}
if err := createCommit(message); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// isGitRepoHere checks if the current directory contains a .git folder.
func isGitRepoHere() bool {
cwd, err := os.Getwd()
if err != nil {
return false
}
info, err := os.Stat(filepath.Join(cwd, ".git"))
return err == nil && info != nil
}
// stageAllChanges runs `git add -A`.
func stageAllChanges() error {
cmd := exec.Command("git", "add", "-A")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("git add failed: %s", out)
}
return nil
}
// isCommandAvailable reports whether a command exists in PATH.
func isCommandAvailable(name string) bool {
_, err := exec.LookPath(name)
return err == nil
}
// copyToClipBoard copies text to the system clipboard using an available tool.
func copyToClipBoard(text string) error {
var cmd *exec.Cmd
switch {
case isCommandAvailable("pbcopy"):
cmd = exec.Command("pbcopy")
case isCommandAvailable("xclip"):
cmd = exec.Command("xclip", "-selection", "clipboard")
case isCommandAvailable("wl-copy"):
cmd = exec.Command("wl-copy")
case isCommandAvailable("clip"):
cmd = exec.Command("cmd", "/c", "clip")
default:
return fmt.Errorf("no clipboard utility found")
}
in, err := cmd.StdinPipe()
if err != nil {
return err
}
if err := cmd.Start(); err != nil {
return err
}
if _, err := io.WriteString(in, text); err != nil {
return err
}
in.Close()
return cmd.Wait()
}
func getBranchName() (string, error) {
out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output()
return strings.TrimSpace(string(out)), err
}
func getShortGitStatus() (string, error) {
out, err := exec.Command("git", "status", "--short").Output()
return strings.TrimSpace(string(out)), err
}
func getTruncatedDiff(max int) (string, error) {
out, err := exec.Command("git", "diff", "--cached").Output()
if err != nil {
return "", err
}
diff := string(out)
if len(diff) > max {
diff = diff[:max] + "\n... (truncated)"
}
return diff, nil
}
// GitContext groups repository state used for prompt generation.
type GitContext struct {
Branch string
Status string
Diff string
}
func buildGitContext() (*GitContext, error) {
branch, err := getBranchName()
if err != nil {
return nil, err
}
status, err := getShortGitStatus()
if err != nil {
return nil, err
}
diff, err := getTruncatedDiff(8000)
if err != nil {
return nil, err
}
return &GitContext{branch, status, diff}, nil
}
// startSpinner displays a terminal spinner and returns a stop function.
func startSpinner(msg string) func() {
chars := []rune("⣷⣯⣟⡿⢿⣻⣽⣾")
stop := make(chan struct{})
done := make(chan struct{})
go func() {
defer close(done)
for i := 0; ; i++ {
select {
case <-stop:
fmt.Print("\r\033[K")
return
default:
fmt.Printf("\r%c %s", chars[i%len(chars)], msg)
time.Sleep(100 * time.Millisecond)
}
}
}()
return func() {
close(stop)
<-done
}
}
const ConventionalCommitRules = `
Use Conventional Commits:
<type>(optional scope): <summary>
Types:
feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
Summary rules:
- lowercase
- no trailing period
- max 72 chars
- imperative mood
`
func generateCommitMessages(ctx GitContext, n int) ([]string, error) {
key := os.Getenv("OPENAI_API_KEY")
if key == "" {
return nil, fmt.Errorf("OPENAI_API_KEY not set")
}
stop := startSpinner("Generating commit messages...")
prompt := fmt.Sprintf(`
%s
Branch:
%s
Status:
%s
Diff:
%s
Generate %d commit messages as JSON array of strings.`,
ConventionalCommitRules,
ctx.Branch,
ctx.Status,
ctx.Diff,
n,
)
body, _ := json.Marshal(map[string]any{
"model": "gpt-4o-mini",
"messages": []map[string]string{
{"role": "user", "content": prompt},
},
})
req, _ := http.NewRequest("POST",
"https://api.openai.com/v1/chat/completions",
bytes.NewBuffer(body),
)
req.Header.Set("Authorization", "Bearer "+key)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
stop()
return nil, err
}
defer resp.Body.Close()
stop()
var parsed struct {
Choices []struct {
Message struct {
Content string
}
}
}
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
return nil, err
}
content := strings.TrimSpace(parsed.Choices[0].Message.Content)
content = strings.ReplaceAll(content, "```json", "")
content = strings.ReplaceAll(content, "```", "")
var msgs []string
if err := json.Unmarshal([]byte(content), &msgs); err != nil {
return nil, err
}
return msgs, nil
}
func selectMessage(msgs []string) (string, bool) {
if len(msgs) == 1 {
return msgs[0], true
}
fmt.Println("Generated messages:")
for i, m := range msgs {
fmt.Printf("%d) %s\n", i+1, m)
}
fmt.Print("Select (0 to abort): ")
var choice int
fmt.Scanf("%d", &choice)
if choice <= 0 || choice > len(msgs) {
return "", false
}
return msgs[choice-1], true
}
func askForConfirmation(msg string) bool {
fmt.Println("Proposed message:\n", msg)
fmt.Print("Confirm? (y/n): ")
var input string
fmt.Scanln(&input)
input = strings.ToLower(strings.TrimSpace(input))
return input == "y" || input == "yes"
}
func createCommit(msg string) error {
cmd := exec.Command("git", "commit", "-m", msg)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("commit failed: %s", out)
}
fmt.Println(string(out))
return nil
}