-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercury.go
159 lines (134 loc) · 3.31 KB
/
mercury.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 (
"context"
"fmt"
"log"
"os"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/joho/godotenv"
openai "github.com/sashabaranov/go-openai"
)
func openRepo(repoPath string) (*git.Repository, error) {
return git.PlainOpen(repoPath)
}
func getDiffBetweenCommits(repo *git.Repository, commit1, commit2 *object.Commit) (object.Changes, error) {
commit1Tree, err := commit1.Tree()
if err != nil {
return nil, err
}
commit2Tree, err := commit2.Tree()
if err != nil {
return nil, err
}
return commit1Tree.Diff(commit2Tree)
}
func preLine(input string, delim string) string {
// Split the input string into lines
lines := strings.Split(input, "\n")
lines = lines[:len(lines)-1]
// Add "+" at the start of each line
for i := range lines {
lines[i] = delim + lines[i]
}
// Join the lines back into a single string
result := strings.Join(lines, "\n")
return result + "\n"
}
func gitDiff(repo *git.Repository, from, to *object.Commit) (string, error) {
changes, err := getDiffBetweenCommits(repo, from, to)
if err != nil {
return "", err
}
diff := ""
for _, change := range changes {
fmt.Println(change.From.Name, change.To.Name)
patch, err := change.Patch()
if err != nil {
return "", err
}
for _, patch := range patch.FilePatches() {
// from, to := patch.Files()
// fmt.Println(from.Path, to.Path)
// var previous diff.Chunk
// prev := patch.Chunks()[0]
// cur := patch.Chunks()[1]
// next := patch.Chunks()[1]
for _, chunk := range patch.Chunks() {
switch chunk.Type() {
case 0: // Equal
// fmt.Println("Equal")
case 1: // Add
// if prev.Type() == 0 {
// fmt.Println(preLine(prev.Content(), " "))
// }
diff += preLine(chunk.Content(), "+ ")
// if next.Type() == 0 {
// fmt.Println(preLine(next.Content(), " "))
// }
case 2: // Delete
diff += preLine(chunk.Content(), "- ")
}
// prev = chunk
}
}
}
return diff, nil
}
func chatCompletion(prompt string) (openai.ChatCompletionResponse, error) {
client := openai.NewClient(os.Getenv("OPENAI_TOKEN"))
return client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: prompt,
},
},
},
)
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
repoPath := "."
repo, err := openRepo(repoPath)
if err != nil {
fmt.Println("Error opening the repository:", err)
os.Exit(1)
}
head, err := repo.Head()
if err != nil {
fmt.Println("Error getting head:", err)
os.Exit(1)
}
child, err := repo.CommitObject(head.Hash())
if err != nil {
fmt.Println("Error getting commit objects:", err)
os.Exit(1)
}
parent, err := child.Parent(0)
if err != nil {
fmt.Println("Error getting commit objects:", err)
os.Exit(1)
}
fmt.Println(child.Hash)
fmt.Println(parent.Hash)
diff, err := gitDiff(repo, parent, child)
if err != nil {
fmt.Printf("Diff Error: %v\n", err)
return
}
fmt.Println(diff)
// resp, err := chatCompletion("Hello!")
// if err != nil {
// fmt.Printf("ChatCompletion error: %v\n", err)
// return
// }
// fmt.Println(resp.Choices[0].Message.Content)
}