-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
commit.go
174 lines (150 loc) · 4.48 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
package cmd
import (
"errors"
"os"
"strings"
"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/spf13/cobra"
"github.com/spf13/viper"
)
var (
commitLang string
commitModel string
// disbale auot commit in Hook mode
preview bool
)
func init() {
commitCmd.PersistentFlags().StringP("file", "f", ".git/COMMIT_EDITMSG", "commit message file")
commitCmd.PersistentFlags().BoolVar(&preview, "preview", false, "preview commit message")
commitCmd.PersistentFlags().StringVar(&commitModel, "model", "gpt-3.5-turbo", "select openai model")
commitCmd.PersistentFlags().StringVar(&commitLang, "lang", "en", "summarizing language uses English by default")
_ = 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 {
var message string
// check git command exist
if !util.IsCommandAvailable("git") {
return errors.New("To use CodeGPT, you must have git on your PATH")
}
g := git.New()
diff, err := g.DiffFiles()
if err != nil {
return err
}
// check default language
if prompt.GetLanguage(commitLang) != prompt.DefaultLanguage {
viper.Set("output.lang", commitLang)
}
// check default model
if openai.GetModel(commitModel) != openai.DefaultModel {
viper.Set("openai.model", commitModel)
}
color.Green("Summarize the commit message use " + viper.GetString("openai.model") + " 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")),
)
if err != nil {
return err
}
out, err := util.GetTemplate(
prompt.SummarizeFileDiffTemplate,
util.Data{
"file_diffs": diff,
},
)
if err != nil {
return err
}
// Get summarize comment from diff datas
color.Cyan("We are trying to summarize a git diff")
summarizeDiff, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
out, err = util.GetTemplate(
prompt.SummarizeTitleTemplate,
util.Data{
"summary_points": summarizeDiff,
},
)
if err != nil {
return err
}
// Get summarize title from diff datas
color.Cyan("We are trying to summarize a title for pull request")
summarizeTitle, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
// support conventional commits
out, err = util.GetTemplate(
prompt.ConventionalCommitTemplate,
util.Data{
"summary_points": summarizeDiff,
},
)
if err != nil {
return err
}
conventionalCommitPrefix, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
if conventionalCommitPrefix != "" {
summarizeTitle = conventionalCommitPrefix + ": " + summarizeTitle
}
if prompt.GetLanguage(viper.GetString("output.lang")) != prompt.DefaultLanguage {
out, err = util.GetTemplate(
prompt.TranslationTemplate,
util.Data{
"output_language": prompt.GetLanguage(viper.GetString("output.lang")),
"commit_title": summarizeTitle,
"commit_message": summarizeDiff,
},
)
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")
summarize, err := client.Completion(cmd.Context(), out)
if err != nil {
return err
}
message = summarize
} else {
message = strings.TrimSpace(summarizeTitle) + "\n\n" + strings.TrimSpace(summarizeDiff)
}
// Output commit summary data from AI
color.Yellow("================Commit Summary====================")
color.Yellow("\n" + strings.TrimSpace(message) + "\n\n")
color.Yellow("==================================================")
color.Cyan("Write the commit message to " + viper.GetString("output.file") + " file")
// write commit message to git staging file
err = os.WriteFile(viper.GetString("output.file"), []byte(message), 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(viper.GetString("output.file"))
if err != nil {
return err
}
color.Yellow(output)
return nil
},
}