-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_on_save.go
159 lines (129 loc) · 3.1 KB
/
push_on_save.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os/exec"
"time"
)
type Payload struct {
Model string `json:"model"`
System string `json:"system"`
Prompt string `json:"prompt"`
Stream bool `json:"stream"`
}
type Response struct {
Response string `json:"response"`
}
func main() {
for {
time.Sleep(1 * time.Second)
hasChanges, err := hasChanges()
if err != nil {
panic(err)
}
if !hasChanges {
fmt.Printf("No changes detected\n")
continue
}
err = addChanges()
if err != nil {
panic(err)
}
diff, err := getDiff()
if err != nil {
panic(err)
}
message, err := generateCommitMessage(diff)
if err != nil {
panic(err)
}
fmt.Printf("Committing with message %s\n", message)
err = commit(message)
if err != nil {
panic(err)
}
err = push()
if err != nil {
panic(err)
}
}
}
func push() error {
_, err := executeCommand("git", "push", "-u", "origin", "main")
if err != nil {
return fmt.Errorf("error pushing: %w", err)
}
return nil
}
func commit(message string) error {
_, err := executeCommand("git", "commit", "-m", message)
if err != nil {
return fmt.Errorf("error committing: %w", err)
}
return nil
}
func addChanges() error {
_, err := executeCommand("git", "add", ".")
return err
}
func hasChanges() (bool, error) {
output, err := executeCommand("git", "status", "--porcelain")
if err != nil {
return false, fmt.Errorf("error checking for changes: %w", err)
}
return len(output) > 0, nil
}
func executeCommand(command string, args ...string) (string, error) {
cmd := exec.Command(command, args...)
var out bytes.Buffer
cmd.Stdout = &out
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("command (%s) failed: %w, stderr: %s", command, err, stderr.String())
}
return out.String(), nil
}
func getDiff() (string, error) {
return executeCommand("git", "diff", "--staged")
}
func generateCommitMessage(diff string) (string, error) {
url := "http://localhost:11435/api/generate"
method := "POST"
payload := Payload{
Model: "llama3",
System: "You will receive a git diff. You will generate a good commit message from this diff. It must stay within 60 characters. Only output the commit message and nothing more.",
Prompt: diff,
Stream: false,
}
jsonData, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("error marshalling JSON: %w", err)
}
fmt.Println(string(jsonData))
req, err := http.NewRequest(method, url, bytes.NewBuffer(jsonData))
if err != nil {
return "", fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("the HTTP request failed with error %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response: %w", err)
}
var response Response
err = json.Unmarshal(body, &response)
if err != nil {
return "", fmt.Errorf("error unmarshalling response: %w", err)
}
return response.Response, nil
}