forked from anasinnyk/git-convenient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
300 lines (261 loc) · 7.31 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
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
package main
import (
"os"
"fmt"
"regexp"
"strings"
"os/exec"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"github.com/spf13/cobra"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
)
var (
version string
revision string
)
type OptionalPart struct {
Skip bool
}
type Commit struct {
ValidatePattern string `git:"validate-pattern"`
Scope struct {
OptionalPart
Pattern string
Replace string
}
Body OptionalPart
Footer OptionalPart
}
type Config struct {
Commit Commit `git:"convenient"`
User struct {
Email string
Name string
}
}
type App struct {
Config *Config
}
type CommitType struct {
Value string
Description string
}
func (a *App) SelectCommitType() string {
types := []CommitType{
{Value: "feat", Description: "A new feature"},
{Value: "fix", Description: "A bug fix"},
{Value: "breaking", Description: "A commit with contain breaking changes"},
{Value: "chore", Description: "Any other changes not ralated to another categories (empty commit for trigger CI for example)"},
{Value: "ci", Description: "Changes to our CI configuration files and scripts"},
{Value: "docs", Description: "Documentation only changes"},
{Value: "build", Description: "Changes that affect the build system or external dependencies"},
{Value: "pref", Description: "A code change that improves performance"},
{Value: "refactor", Description: "A code change that neither fixes a bug nor adds a feature"},
{Value: "revert", Description: "When you revert some commit"},
{Value: "style", Description: "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)"},
{Value: "test", Description: "Adding missing tests or correcting existing tests"},
{Value: "improvement", Description: "A improve a current implementation without adding a new feature or fixing a bug"},
}
templatesType := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: `{{ "\u27A1" | yellow }} {{ .Value | cyan }}: {{ .Description | yellow }}`,
Inactive: " {{ .Value | cyan }}: {{ .Description | yellow }}",
Selected: `{{ "\u2714" | green }} {{ "Select Commit Type" | cyan | bold }} {{ .Value }}`,
}
promptType := promptui.Select{
Label: "Select Commit Type",
Items: types,
Templates: templatesType,
Size: len(types),
}
i, _, err := promptType.Run()
if err != nil {
log.Panicf("Incorrect commit type\nDetails: %v\n", err)
}
return types[i].Value
}
func (a *App) DetectScope() []string {
diff, err := exec.Command("git", "diff", "--staged", "--name-status").Output()
if err != nil {
log.Printf("Git diff %v\n", err)
return nil
}
var scopes []string
re, err := regexp.Compile(a.Config.Commit.Scope.Pattern)
if err != nil {
log.Error(err)
}
files := strings.Split(strings.Trim(string(diff), "\n"), "\n")
for _, f := range files {
f = strings.Trim(f, " ")
if re.MatchString(f) {
scopes = append(scopes, re.ReplaceAllString(f, "$1"))
}
}
func (xs *[]string) {
found := make(map[string]bool)
j := 0
for i, x := range *xs {
if !found[x] {
found[x] = true
(*xs)[j] = (*xs)[i]
j++
}
}
*xs = (*xs)[:j]
}(&scopes)
return scopes
}
func (a *App) prompt(label string, def string) string {
prompt := promptui.Prompt{
Label: label,
Default: def,
Templates: &promptui.PromptTemplates{
Prompt: "{{ . }} ",
Success: "{{ \"\u2714\" | green }} {{ . | cyan | bold }} ",
},
}
msg, _ := prompt.Run()
return msg
}
func (a *App) Commit() {
typeName := a.SelectCommitType()
var scopes string
if !a.Config.Commit.Scope.Skip {
var dScopes []string
if a.Config.Commit.Scope.Pattern != "" && a.Config.Commit.Scope.Replace != "" {
dScopes = a.DetectScope()
}
scopes = a.prompt("Enter your scope without breket or press enter for apply this", strings.Join(dScopes, ","))
}
msg := a.prompt("Enter your comment", "")
var body string
if !a.Config.Commit.Body.Skip {
body = a.prompt("Enter your body part", "")
}
var footer string
if !a.Config.Commit.Body.Skip {
footer = a.prompt("Enter your footer part", "")
}
commit := fmt.Sprintf("%s: %s", typeName, msg)
if scopes != "" {
commit = fmt.Sprintf("%s(%s): %s", typeName, scopes, msg)
}
if body != "" {
commit = fmt.Sprintf("%s\n\n%s", commit, body)
}
if footer != "" {
commit = fmt.Sprintf("%s\n\n%s", commit, footer)
}
if a.validate(commit) {
r, err := git.PlainOpen(".")
if err != nil {
log.Fatalf("Cannot repository open %v", err)
}
wt, err := r.Worktree()
if err != nil {
log.Fatalf("Cannot get worktree %v", err)
}
hash, err := wt.Commit(commit, &git.CommitOptions{
Author: &object.Signature{
Name: a.Config.User.Name,
Email: a.Config.User.Email,
},
})
if err != nil {
log.Fatalf("Cannot commited %v", err)
}
fmt.Println(hash)
} else {
fmt.Println("Commit is not valid")
}
}
func (a *App) validate(commit string) bool {
if a.Config.Commit.ValidatePattern == "" {
a.Config.Commit.ValidatePattern = "^(feat|fix|breaking|chore|ci|docs|build|pref|refactor|revert|style|test|improvement)(\\([a-zA-Z0-9_\\-,]+\\))?: ([^\n]+(\n[\\s\\S]+)$|.*$)"
}
validator := regexp.MustCompile(a.Config.Commit.ValidatePattern)
return validator.MatchString(commit)
}
func (a *App) CheckIfCommitValid(hash string) bool {
r, err := git.PlainOpen(".")
if err != nil {
log.Fatalf("Cannot repository open %v", err)
}
obj, err := r.CommitObject(plumbing.NewHash(hash))
if err != nil {
log.Fatalf("Cannto find commit %s. %v", hash, err)
}
return a.validate(obj.Message)
}
func (a *App) CheckIfMsgValid(msg string) bool {
return a.validate(msg)
}
func (a *App) InstallHook() {
hook := "MSG=`cat .git/COMMIT_EDITMSG`\ngit convenient validate -m \"$MSG\""
f, err := os.OpenFile(".git/hooks/commit-msg", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
if _, err = f.WriteString(hook); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
func main() {
var conf Config
g := GitConfig{}
g.ParseFile(&conf)
app := &App{
Config: &conf,
}
commit := &cobra.Command{
Use: "commit",
Short: "Create git commit in convenient style",
Run: func(cmd *cobra.Command, args []string) {
app.Commit()
},
}
var hash string
var msg string
validate := &cobra.Command{
Use: "validate",
Short: "Check commit message if it's valid",
Run: func(cmd *cobra.Command, args []string) {
var cValid = true
var mValid = true
if hash != "" {
cValid = app.CheckIfCommitValid(hash)
}
if msg != "" {
mValid = app.CheckIfMsgValid(msg)
}
if (cValid && mValid) {
fmt.Println("It's valid")
} else {
fmt.Println("Commit is not valid")
os.Exit(1)
}
},
}
validate.Flags().StringVarP(&hash, "commit", "c", "", "hash commit which need validate")
validate.Flags().StringVarP(&msg, "message", "m", "", "message which need validate")
installHook := &cobra.Command{
Use: "install-hook",
Short: "Install commit validation hook to your project",
Run: func(cmd *cobra.Command, args []string) {
app.InstallHook()
},
}
var rootCmd = &cobra.Command{
Use: "git-convenient",
Version: fmt.Sprintf("%s (%s)", version, revision),
}
rootCmd.AddCommand(commit, validate, installHook)
rootCmd.Execute()
}