-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
commit.go
324 lines (295 loc) · 10.4 KB
/
commit.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
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
package cmd
import (
"html"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/appleboy/CodeGPT/git"
"github.com/appleboy/CodeGPT/openai"
"github.com/appleboy/CodeGPT/prompt"
"github.com/appleboy/CodeGPT/util"
"github.com/fatih/color"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
commitLang string
commitModel string
preview bool
diffUnified int
excludeList []string
httpsProxy string
socksProxy string
templateFile string
templateString string
commitAmend bool
timeout time.Duration
promptOnly bool
templateVars []string
templateVarsFile string
)
func init() {
commitCmd.PersistentFlags().StringP("file", "f", "", "commit message file")
commitCmd.PersistentFlags().BoolVar(&preview, "preview", false, "preview commit message")
commitCmd.PersistentFlags().IntVar(&diffUnified, "diff_unified", 3, "generate diffs with <n> lines of context, default is 3")
commitCmd.PersistentFlags().StringVar(&commitModel, "model", "gpt-3.5-turbo", "select openai model")
commitCmd.PersistentFlags().StringVar(&commitLang, "lang", "en", "summarizing language uses English by default")
commitCmd.PersistentFlags().StringSliceVar(&excludeList, "exclude_list", []string{}, "exclude file from git diff command")
commitCmd.PersistentFlags().StringVar(&httpsProxy, "proxy", "", "http proxy")
commitCmd.PersistentFlags().StringVar(&socksProxy, "socks", "", "socks proxy")
commitCmd.PersistentFlags().StringVar(&templateFile, "template_file", "", "git commit message file")
commitCmd.PersistentFlags().StringVar(&templateString, "template_string", "", "git commit message string")
commitCmd.PersistentFlags().StringSliceVar(&templateVars, "template_vars", []string{}, "template variables")
commitCmd.PersistentFlags().StringVar(&templateVarsFile, "template_vars_file", "", "template variables file")
commitCmd.PersistentFlags().BoolVar(&commitAmend, "amend", false, "replace the tip of the current branch by creating a new commit.")
commitCmd.PersistentFlags().DurationVarP(&timeout, "timeout", "t", 10*time.Second, "http timeout")
commitCmd.PersistentFlags().BoolVar(&promptOnly, "prompt_only", false, "show prompt only, don't send request to openai")
_ = viper.BindPFlag("output.file", commitCmd.PersistentFlags().Lookup("file"))
}
var commitCmd = &cobra.Command{
Use: "commit",
Short: "Auto generate commit message",
RunE: func(cmd *cobra.Command, args []string) error {
if err := check(); err != nil {
return err
}
g := git.New(
git.WithDiffUnified(viper.GetInt("git.diff_unified")),
git.WithExcludeList(viper.GetStringSlice("git.exclude_list")),
git.WithEnableAmend(commitAmend),
)
diff, err := g.DiffFiles()
if err != nil {
return err
}
// Update the OpenAI client request timeout if the timeout value is greater than the default openai.timeout
if timeout > viper.GetDuration("openai.timeout") {
viper.Set("openai.timeout", timeout)
}
currentModel := viper.GetString("openai.model")
if viper.GetString("openai.provider") == openai.AZURE {
currentModel = viper.GetString("openai.model_name")
}
color.Green("Summarize the commit message use " + currentModel + " model")
client, err := openai.New(
openai.WithToken(viper.GetString("openai.api_key")),
openai.WithModel(viper.GetString("openai.model")),
openai.WithOrgID(viper.GetString("openai.org_id")),
openai.WithProxyURL(viper.GetString("openai.proxy")),
openai.WithSocksURL(viper.GetString("openai.socks")),
openai.WithBaseURL(viper.GetString("openai.base_url")),
openai.WithTimeout(viper.GetDuration("openai.timeout")),
openai.WithMaxTokens(viper.GetInt("openai.max_tokens")),
openai.WithTemperature(float32(viper.GetFloat64("openai.temperature"))),
openai.WithProvider(viper.GetString("openai.provider")),
openai.WithModelName(viper.GetString("openai.model_name")),
openai.WithSkipVerify(viper.GetBool("openai.skip_verify")),
openai.WithHeaders(viper.GetStringSlice("openai.headers")),
openai.WithApiVersion(viper.GetString("openai.api_version")),
openai.WithTopP(float32(viper.GetFloat64("openai.top_p"))),
openai.WithFrequencyPenalty(float32(viper.GetFloat64("openai.frequency_penalty"))),
openai.WithPresencePenalty(float32(viper.GetFloat64("openai.presence_penalty"))),
)
if err != nil && !promptOnly {
return err
}
data := util.Data{}
// add template vars
if vars := util.ConvertToMap(templateVars); len(vars) > 0 {
for k, v := range vars {
data[k] = v
}
}
// add template vars from file
if templateVarsFile != "" {
allENV, err := godotenv.Read(templateVarsFile)
if err != nil {
return err
}
for k, v := range allENV {
data[k] = v
}
}
// Get code review message from diff datas
if _, ok := data[prompt.SummarizeMessageKey]; !ok {
out, err := util.GetTemplateByString(
prompt.SummarizeFileDiffTemplate,
util.Data{
"file_diffs": diff,
},
)
if err != nil {
return err
}
// determine if the user wants to use the prompt only
if promptOnly {
color.Yellow("====================Prompt========================")
color.Yellow("\n" + strings.TrimSpace(out) + "\n\n")
color.Yellow("==================================================")
return nil
}
// Get summarize comment from diff datas
color.Cyan("We are trying to summarize a git diff")
resp, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
data[prompt.SummarizeMessageKey] = strings.TrimSpace(resp.Content)
color.Magenta("PromptTokens: " + strconv.Itoa(resp.Usage.PromptTokens) +
", CompletionTokens: " + strconv.Itoa(resp.Usage.CompletionTokens) +
", TotalTokens: " + strconv.Itoa(resp.Usage.TotalTokens),
)
}
// Get summarize title from diff datas
if _, ok := data[prompt.SummarizeTitleKey]; !ok {
out, err := util.GetTemplateByString(
prompt.SummarizeTitleTemplate,
util.Data{
"summary_points": data[prompt.SummarizeMessageKey],
},
)
if err != nil {
return err
}
// Get summarize title from diff datas
color.Cyan("We are trying to summarize a title for pull request")
resp, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
summarizeTitle := resp.Content
color.Magenta("PromptTokens: " + strconv.Itoa(resp.Usage.PromptTokens) +
", CompletionTokens: " + strconv.Itoa(resp.Usage.CompletionTokens) +
", TotalTokens: " + strconv.Itoa(resp.Usage.TotalTokens),
)
// lowercase the first character of first word of the commit message and remove last period
summarizeTitle = strings.TrimRight(strings.ToLower(string(summarizeTitle[0]))+summarizeTitle[1:], ".")
data[prompt.SummarizeTitleKey] = strings.TrimSpace(summarizeTitle)
}
if _, ok := data[prompt.SummarizePrefixKey]; !ok {
out, err := util.GetTemplateByString(
prompt.ConventionalCommitTemplate,
util.Data{
"summary_points": data[prompt.SummarizeMessageKey],
},
)
if err != nil {
return err
}
color.Cyan("We are trying to get conventional commit prefix")
summaryPrix := ""
if client.AllowFuncCall() {
resp, err := client.CreateFunctionCall(cmd.Context(), out, openai.SummaryPrefixFunc)
if err != nil {
return err
}
if len(resp.Choices) > 0 {
args := openai.GetSummaryPrefixArgs(resp.Choices[0].Message.FunctionCall.Arguments)
summaryPrix = args.Prefix
}
color.Magenta("PromptTokens: " + strconv.Itoa(resp.Usage.PromptTokens) +
", CompletionTokens: " + strconv.Itoa(resp.Usage.CompletionTokens) +
", TotalTokens: " + strconv.Itoa(resp.Usage.TotalTokens),
)
} else {
resp, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
summaryPrix = strings.TrimSpace(resp.Content)
color.Magenta("PromptTokens: " + strconv.Itoa(resp.Usage.PromptTokens) +
", CompletionTokens: " + strconv.Itoa(resp.Usage.CompletionTokens) +
", TotalTokens: " + strconv.Itoa(resp.Usage.TotalTokens),
)
}
data[prompt.SummarizePrefixKey] = summaryPrix
}
var commitMessage string
if viper.GetString("git.template_file") != "" {
format, err := os.ReadFile(viper.GetString("git.template_file"))
if err != nil {
return err
}
commitMessage, err = util.NewTemplateByString(
string(format),
data,
)
if err != nil {
return err
}
} else if viper.GetString("git.template_string") != "" {
commitMessage, err = util.NewTemplateByString(
viper.GetString("git.template_string"),
data,
)
if err != nil {
return err
}
} else {
commitMessage, err = util.GetTemplateByString(
git.CommitMessageTemplate,
data,
)
if err != nil {
return err
}
}
if prompt.GetLanguage(viper.GetString("output.lang")) != prompt.DefaultLanguage {
out, err := util.GetTemplateByString(
prompt.TranslationTemplate,
util.Data{
"output_language": prompt.GetLanguage(viper.GetString("output.lang")),
"output_message": commitMessage,
},
)
if err != nil {
return err
}
// translate a git commit message
color.Cyan("We are trying to translate a git commit message to " + prompt.GetLanguage(viper.GetString("output.lang")) + " language")
resp, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
color.Magenta("PromptTokens: " + strconv.Itoa(resp.Usage.PromptTokens) +
", CompletionTokens: " + strconv.Itoa(resp.Usage.CompletionTokens) +
", TotalTokens: " + strconv.Itoa(resp.Usage.TotalTokens),
)
commitMessage = resp.Content
}
// unescape html entities in commit message
commitMessage = html.UnescapeString(commitMessage)
// Output commit summary data from AI
color.Yellow("================Commit Summary====================")
color.Yellow("\n" + strings.TrimSpace(commitMessage) + "\n\n")
color.Yellow("==================================================")
outputFile := viper.GetString("output.file")
if outputFile == "" {
out, err := g.GitDir()
if err != nil {
return err
}
outputFile = path.Join(strings.TrimSpace(out), "COMMIT_EDITMSG")
}
color.Cyan("Write the commit message to " + outputFile + " file")
// write commit message to git staging file
err = os.WriteFile(outputFile, []byte(commitMessage), 0o644)
if err != nil {
return err
}
if preview {
return nil
}
// git commit automatically
color.Cyan("Git record changes to the repository")
output, err := g.Commit(commitMessage)
if err != nil {
return err
}
color.Yellow(output)
return nil
},
}