This repository was archived by the owner on Jan 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
231 lines (211 loc) · 5.93 KB
/
main.go
File metadata and controls
231 lines (211 loc) · 5.93 KB
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
package main
import (
"fmt"
"strings"
fatihcolor "github.com/fatih/color"
"github.com/feiandxs/agcommits/config"
"github.com/feiandxs/agcommits/service/openai_api"
"github.com/feiandxs/agcommits/utils"
"github.com/shibukawa/cdiff"
)
func main() {
// if config file not exists, create it
exists, err := config.IsConfigFileExists()
if err != nil {
fmt.Println("检查配置文件时出错:", err)
return
}
// 如果配置文件不存在,创建默认配置
if !exists {
if err := config.CreateConfigFile(); err != nil {
fatihcolor.Red("创建配置文件失败: %v", err)
return
}
fatihcolor.Green("已创建默认配置文件")
}
// 加载配置
cfg, err := config.LoadConfig()
if err != nil {
fatihcolor.Red("加载配置文件失败: %v", err)
return
}
// 检查并补充缺失的必填项
changed := false
for _, field := range config.ConfigFields {
value := utils.GetConfigValue(cfg, field.Name)
if (field.Required && value == "") || (!field.Required && value == "" && utils.AskForOptional(field)) {
newValue, err := utils.PromptForValue(field)
if err != nil {
fatihcolor.Red("获取输入失败: %v", err)
return
}
if newValue != "" {
if err := config.UpdateConfigField(field.Name, newValue); err != nil {
fatihcolor.Red("更新配置失败: %v", err)
return
}
changed = true
}
}
}
if changed {
fatihcolor.Green("配置已更新")
}
// 最终验证
if err := config.CheckConfig(); err != nil {
fatihcolor.Red("配置验证失败: %v", err)
return
}
fatihcolor.Green("配置验证通过")
// 检查是否在 Git 仓库中
isRepo, err := utils.IsGitRepository()
if err != nil {
fatihcolor.Red("检查 Git 仓库时出错: %v", err)
return
}
// 如果不是Git仓库,询问用户是否要初始化一个
if !isRepo {
if utils.ConfirmGitInit() {
fatihcolor.Yellow("正在初始化Git仓库...")
if err := utils.InitGitRepository(); err != nil {
fatihcolor.Red("初始化Git仓库失败: %v", err)
return
}
fatihcolor.Green("Git仓库初始化成功")
} else {
fatihcolor.Yellow("未初始化Git仓库,程序退出")
return
}
}
// 检查是否有暂存的更改
hasStagedChanges, err := utils.HasStagedChanges()
if err != nil {
fatihcolor.Red("检查暂存区状态失败: %v", err)
return
}
// 如果没有暂存的更改,根据配置决定是否自动执行或询问用户
if !hasStagedChanges {
shouldAdd := cfg.AutoAdd
if !cfg.AutoAdd {
// 如果未启用自动模式,询问用户
shouldAdd = utils.ConfirmGitAdd()
} else {
fatihcolor.Green("自动模式已启用,正在自动执行 git add ...")
}
if shouldAdd {
fatihcolor.Yellow("正在执行 git add . 命令...")
if err := utils.GitAddAll(); err != nil {
fatihcolor.Red("执行 git add . 命令失败: %v", err)
return
}
fatihcolor.Green("成功将所有更改添加到暂存区")
} else {
fatihcolor.Yellow("未执行 git add . 命令,程序退出")
return
}
}
// 获取 Git 暂存区的 diff 信息
diff, err := utils.GetGitDiff()
if err != nil {
fatihcolor.Red("获取 Git 暂存区 diff 信息失败: %v", err)
return
}
// 打印 diff 信息
if diff == "" {
fatihcolor.Yellow("暂存区没有更改,无法生成提交消息")
return
} else {
fatihcolor.Green("已获取暂存区的更改信息")
// 使用cdiff库美化diff输出
fmt.Println("\n========== 暂存区更改内容 ==========")
// 获取当前目录作为文件路径前缀
diffResult := cdiff.Diff("", diff, cdiff.LineByLine)
// 使用String方法直接获取格式化后的字符串,然后手动添加颜色
diffText := diffResult.String()
printColoredDiff(diffText)
fmt.Println("===================================\n")
}
// 使用 OpenAI API 生成提交消息
fatihcolor.Yellow("正在使用 AI 生成提交消息...")
commitMsg, err := openai_api.GenerateCommitMessage(cfg, diff)
if err != nil {
fatihcolor.Red("生成提交消息失败: %v", err)
return
}
// 根据配置决定是否自动提交或询问用户
shouldCommit := cfg.AutoCommit
if !cfg.AutoCommit {
// 如果未启用自动提交,询问用户
shouldCommit = utils.ConfirmCommitMessage(commitMsg)
} else {
// 自动模式下也显示生成的提交消息
fatihcolor.Green("自动提交模式已启用")
fmt.Println("AI 生成的 Git 提交消息如下:")
fmt.Println(commitMsg)
}
if shouldCommit {
// 执行 Git 提交
fatihcolor.Yellow("正在执行 Git 提交...")
if err := utils.PerformGitCommit(commitMsg); err != nil {
fatihcolor.Red("Git 提交失败: %v", err)
return
}
fatihcolor.Green("Git 提交成功")
} else {
fatihcolor.Yellow("已取消 Git 提交")
}
}
// printColoredDiff 打印彩色的 diff 输出
func printColoredDiff(diff string) {
lines := strings.Split(diff, "\n")
for _, line := range lines {
if len(line) == 0 {
fmt.Println()
continue
}
switch line[0] {
case '+':
if len(line) > 1 && line[1] == '+' {
// 文件头部信息
fatihcolor.Cyan("%s", line)
} else {
// 添加的行 - 绿色
green := fatihcolor.New(fatihcolor.FgGreen, fatihcolor.Bold)
green.Print("+ ")
green.Println(line[1:])
}
case '-':
if len(line) > 1 && line[1] == '-' {
// 文件头部信息
fatihcolor.Cyan("%s", line)
} else {
// 删除的行 - 红色
red := fatihcolor.New(fatihcolor.FgRed, fatihcolor.Bold)
red.Print("- ")
red.Println(line[1:])
}
case '@':
// 区块信息
cyan := fatihcolor.New(fatihcolor.FgCyan)
cyan.Println(line)
case 'd':
if strings.HasPrefix(line, "diff --git") {
// diff 命令行
yellow := fatihcolor.New(fatihcolor.FgYellow)
yellow.Println(line)
} else {
fmt.Println(line)
}
case 'i':
if strings.HasPrefix(line, "index ") {
// index 行
yellow := fatihcolor.New(fatihcolor.FgYellow)
yellow.Println(line)
} else {
fmt.Println(line)
}
default:
fmt.Println(line)
}
}
}