-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitcommit.go
119 lines (98 loc) · 3.55 KB
/
gitcommit.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"github.com/go-resty/resty/v2"
)
func main() {
openaiAPIKey := os.Getenv("OPENAI_API_KEY")
if openaiAPIKey == "" {
log.Fatal("ERROR: Please set your OpenAI API key as an environment variable named 'OPENAI_API_KEY'")
}
// Choose the GPT-3 model you want to use
modelEngine := "text-davinci-003"
// Define the input data
diff, err := exec.Command("git", "diff", "--cached").Output()
if err != nil {
log.Fatal("Failed to execute git diff command:", err)
}
for {
// Generate the commit message
prompt:= fmt.Sprintf("Read the following git diff \n%s and write a commit message in %s without mentioning code or files. Explain why these changes were made (summarize the reasoning):\n", diff, "english")
// prompt := fmt.Sprintf("Generate a git commit message for the following changes:\n%s", diff)
generatedText, err := generateCommitMessage(prompt, modelEngine, openaiAPIKey)
if err != nil {
log.Fatal("Failed to generate commit message:", err)
}
// Modify the commit message and present it to the user
message := generatedText
fmt.Printf("Suggested commit message:\n%s\n", message)
userInput := getUserInput("Accept this generated commit message? (y/n/e): ")
// Commit the changes or regenerate the message
if userInput == "y" {
commitChanges(message)
fmt.Println("Changes committed!")
break
} else if userInput == "e" {
editedText := getUserInput("Enter your commit message: ")
commitChanges(editedText)
fmt.Println("Changes committed!")
break
} else {
fmt.Println("Please wait...Regenerating commit message...")
}
}
}
type OpenAIResponse struct {
Choices []struct {
Text string `json:"text"`
} `json:"choices"`
}
func generateCommitMessage(prompt, modelEngine, openaiAPIKey string) (string, error) {
client := resty.New()
resp, err := client.R().
SetHeaders(map[string]string{
"Content-Type": "application/json",
"Authorization": fmt.Sprintf("Bearer %s", openaiAPIKey),
}).
SetBody(map[string]interface{}{
"model": modelEngine,
"prompt": prompt,
"max_tokens": 50, // Adjust the max tokens and other parameters as needed
"temperature": 0, // Adjust the temperature value as needed
}).
SetResult(&OpenAIResponse{}).
Post("https://api.openai.com/v1/completions")
if err != nil {
return "", err
}
if resp.StatusCode() != 200 {
return "", fmt.Errorf("API request failed with status code: %d", resp.StatusCode())
}
openAIResp, ok := resp.Result().(*OpenAIResponse)
if !ok {
return "", fmt.Errorf("Unable to retrieve commit message from Open API")
}
if len(openAIResp.Choices) == 0 {
return "", fmt.Errorf("No commit messages could be generated")
}
return openAIResp.Choices[0].Text, nil
}
func commitChanges(commitMessage string) {
cmd := exec.Command("git", "commit", "-m", commitMessage)
err := cmd.Run()
if err != nil {
log.Fatal("Failed to execute git commit command:", err)
}
}
func getUserInput(prompt string) string {
var userInput string
fmt.Print(prompt)
_, err := fmt.Scanln(&userInput)
if err != nil {
log.Fatal("Failed to read user input:", err)
}
return userInput
}