-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (92 loc) · 2.99 KB
/
main.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
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/google/generative-ai-go/genai"
"google.golang.org/api/option"
)
// 获取 scope 名称
func getScopeNames() string {
scopes := ""
_, err := os.Stat("./nx.json")
if err == nil {
scopeCmd := exec.Command("npx", "nx", "show", "projects")
scopeOut, err := scopeCmd.Output()
if err != nil {
return scopes
}
scopes = strings.ReplaceAll(string(scopeOut), "\n", " ")
}
return scopes
}
func debugLog(msg string) {
isDebug := len(os.Getenv("DEBUG")) > 0
if isDebug {
fmt.Printf("[DEBUG] %s", msg)
}
}
func main() {
ctx := context.Background()
client, err := genai.NewClient(ctx, option.WithAPIKey(os.Getenv("GEMINI_API_KEY")))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-pro")
diffArgs := []string{"diff"}
diffArgs = append(diffArgs, os.Args[1:]...)
diffCom := exec.Command("git", diffArgs...)
diffOut, _ := diffCom.Output()
diffText := string(diffOut)
if len(diffText) == 0 {
fmt.Println("[ERROR] git diff result is empty")
return
}
// question := fmt.Sprintf("请根据下面的git diff结果, 结合nx monorepo的代码组织特点, 编写一条符合约定式提交规则的commit信息: \n%s", diffText)
restriction1 := "1. 符合约定式提交的格式, 如: <type>: <description>\n 其中type的合法值有:fix feat chore docs build ci style refactor perf test"
names := getScopeNames()
if len(names) > 0 {
restriction1 = fmt.Sprintf(`1. 符合约定式提交的格式: <type>(scope?): <description>
1.1 其中type的合法值有:fix feat chore docs build ci style refactor perf test
1.2 其中scope的合法值有: %s
1.3 scope只能选取合法值中的相关性最大的一个
1.4 git diff 内容与scope中任何一个都不相关时, scope可以不提供, 新的格式为: <type>: <description>
`,
names)
}
question := fmt.Sprintf(`请根据下面的git diff结果, 编写一条commit信息, 编写要求有:
%s
2. 英文小写;
3. 长度不超过20个单词.
git diff 结果如下:
%s
`, restriction1, diffText)
debugLog(fmt.Sprintf("question:\n%s\n", question))
resp, err := model.GenerateContent(ctx, genai.Text(question))
if err != nil {
log.Fatal(err)
}
commitMsg := fmt.Sprint(resp.Candidates[0].Content.Parts[0])
fmt.Printf("AI Commit Message Result:\n %s\n", commitMsg)
reader := bufio.NewReader(os.Stdin)
fmt.Println("Above message is ok? continue or put your custom message at here")
userInput, _ := reader.ReadString('\n')
if userInput != "" && userInput != "y" && userInput != "yes" && userInput != "\n" {
commitMsg = userInput
}
debugLog(fmt.Sprintf("the commit mssage: %s\n", commitMsg))
changeCmd := exec.Command("git", "diff", "--name-only", "--staged")
changeFiles, _ := changeCmd.Output()
if len(changeFiles) == 0 {
debugLog("execute git add because no changes")
addCmd := exec.Command("git", "add", ".")
addCmd.Run()
}
commitCmd := exec.Command("git", "commit", "-m", commitMsg)
commitCmd.Run()
}