-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgitcz.go
249 lines (232 loc) · 4.99 KB
/
gitcz.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
type CzType struct {
Type string
Message string
}
type CzCommit struct {
Type *CzType
Scope *string
Subject *string
Body *string
BreakingChange *string
Closes *string
}
var StdinInput = bufio.NewReader(os.Stdin)
var (
InputTypePrompt = "选择或输入一个提交类型(必填): "
InputScopePrompt = "说明本次提交的影响范围(必填): "
InputSubjectPrompt = "对本次提交进行简短描述(必填): "
InputBodyPrompt = "对本次提交进行完整描述(选填): "
InputBreakingChangePrompt = "如果当前代码版本与上一版本不兼容,对变动、变动的理由及迁移的方法进行描述(选填): "
InputClosesPrompt = "如果本次提交针对某个issue,列出关闭的issues(选填): "
)
var CzTypeList = []CzType{
{
Type: "feat",
Message: "新的功能",
},
{
Type: "fix",
Message: "修补错误",
},
{
Type: "docs",
Message: "文档修改",
},
{
Type: "style",
Message: "格式变化",
},
{
Type: "refactor",
Message: "重构代码",
},
{
Type: "perf",
Message: "性能提高",
},
{
Type: "test",
Message: "测试用例",
},
{
Type: "chore",
Message: "构建变动",
},
}
func main() {
amend := flag.Bool(
"amend",
false,
"覆盖上次提交信息",
)
sign := flag.Bool("S", false, "对commit进行签名")
author := flag.Bool(
"author",
false,
"关于本软件开发者",
)
flag.Parse()
if *author {
Author()
return
}
czCommit := &CzCommit{}
czCommit.Type = InputType()
czCommit.Scope = InputScope()
czCommit.Subject = InputSubject()
czCommit.Body = InputBody()
czCommit.BreakingChange = InputBreakingChange()
czCommit.Closes = InputCloses()
commit := GenerateCommit(czCommit)
if err := GitCommit(commit, *amend, *sign); err != nil {
fmt.Println(err)
}
}
func Author() {
fmt.Println("welcome to our website https://aite.xyz/")
fmt.Println("----------------------------------------")
fmt.Println("腾讯扣扣:88966001")
fmt.Println("电子邮箱:[email protected]")
fmt.Println("----------------------------------------")
fmt.Println("Copyright (c) 2020 [email protected]")
}
func NewLine() {
fmt.Println()
}
func GitCommit(commit string, amend bool, sign bool) (err error) {
tempFile, err := os.CreateTemp("", "git_commit_")
if err != nil {
return
}
defer func() {
_ = tempFile.Close()
_ = os.Remove(tempFile.Name())
}()
if _, err = tempFile.WriteString(commit); err != nil {
return
}
args := []string{"commit"}
if amend {
args = append(args, "--amend")
}
if sign {
args = append(args, "-S")
}
args = append(args, "-F", tempFile.Name())
cmd := exec.Command("git", args...)
result, err := cmd.CombinedOutput()
if err != nil && !strings.ContainsAny(err.Error(), "exit status") {
return
} else {
fmt.Println(string(bytes.TrimSpace(result)))
}
return nil
}
func InputType() *CzType {
typeNum := len(CzTypeList)
for i := 0; i < typeNum; i++ {
fmt.Printf("[%d] %s:\t%s\n", i+1, CzTypeList[i].Type, CzTypeList[i].Message)
}
fmt.Print(InputTypePrompt)
text, _ := StdinInput.ReadString('\n')
text = strings.TrimSpace(text)
selectId, err := strconv.Atoi(text)
if err == nil && (selectId > 0 && selectId <= typeNum) {
NewLine()
return &CzTypeList[selectId-1]
}
for i := 0; i < typeNum; i++ {
if text == CzTypeList[i].Type {
NewLine()
return &CzTypeList[i]
}
}
NewLine()
return InputType()
}
func InputScope() *string {
fmt.Print(InputScopePrompt)
text, _ := StdinInput.ReadString('\n')
text = strings.TrimSpace(text)
if text != "" {
NewLine()
return &text
}
NewLine()
return InputScope()
}
func InputSubject() *string {
fmt.Print(InputSubjectPrompt)
text, _ := StdinInput.ReadString('\n')
text = strings.TrimSpace(text)
if text != "" {
NewLine()
return &text
}
NewLine()
return InputSubject()
}
func InputBody() *string {
fmt.Print(InputBodyPrompt)
text, _ := StdinInput.ReadString('\n')
text = strings.TrimSpace(text)
if text != "" {
NewLine()
return &text
}
NewLine()
return nil
}
func InputBreakingChange() *string {
fmt.Print(InputBreakingChangePrompt)
text, _ := StdinInput.ReadString('\n')
text = strings.TrimSpace(text)
if text != "" {
NewLine()
return &text
}
NewLine()
return nil
}
func InputCloses() *string {
fmt.Print(InputClosesPrompt)
text, _ := StdinInput.ReadString('\n')
text = strings.TrimSpace(text)
if text != "" {
NewLine()
return &text
}
NewLine()
return nil
}
func GenerateCommit(czCommit *CzCommit) string {
commit := fmt.Sprintf(
"%s(%s): %s\n\n",
czCommit.Type.Type,
*czCommit.Scope,
*czCommit.Subject,
)
if czCommit.Body != nil {
commit += *czCommit.Body
commit += "\n\n"
}
if czCommit.BreakingChange != nil {
commit += "BREAKING CHANGE: " + *czCommit.BreakingChange
commit += "\n\n"
}
if czCommit.Closes != nil {
commit += "Closes fix " + *czCommit.Closes
}
return commit
}