-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
277 lines (226 loc) · 7.62 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
/*
Copyright © 2024 Austin Sabel [email protected]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
"github.com/go-git/go-git/v5"
"github.com/pterm/pterm"
"github.com/spf13/viper"
)
// Build Info Vars
var (
version = "dev"
commit = "none"
date = "unknown"
)
// Global Vars
var (
commitTypes []string
scopes []string
gitRoot string
)
func gitStatus() {
repo, err := openGitRepo()
if err != nil {
pterm.Error.Println(err)
os.Exit(1)
}
Worktree, err := repo.Worktree()
if err != nil {
pterm.Fatal.Println("Error opening Git repository:", err)
}
gitRoot = Worktree.Filesystem.Root()
pterm.Debug.Println("Root directory of Git repository:", gitRoot)
status, err := Worktree.Status()
if err != nil {
fmt.Println("Failed to get status:", err)
os.Exit(1)
}
// Check if there are staged changes
hasStagedChanges := false
hasUntracked := false
for _, entry := range status {
if entry.Staging != git.Untracked && entry.Staging != git.Unmodified {
hasStagedChanges = true
break
} else if entry.Staging == git.Untracked {
hasUntracked = true
}
}
// Error out if nothing is staged
if !hasStagedChanges && hasUntracked {
pterm.Error.Println("nothing added to commit but untracked files present (use \"git add\" to track)")
os.Exit(2)
} else if !hasStagedChanges {
pterm.Error.Println("nothing added to commit")
os.Exit(2)
}
}
func loadConfig() {
// Set the file name of the configuration file
viper.SetConfigName(".git-cc.yaml")
// config file format
viper.SetConfigType("yaml")
// Add the path to look for the config file
viper.AddConfigPath(gitRoot)
// Optional. If you want to support environment variables, use this
viper.AutomaticEnv()
// Set Default Config Values
viper.SetDefault("use_defaults", true)
viper.SetDefault("custom_commit_types", []string{})
viper.SetDefault("scopes", []string{})
default_commit_types := []string{"feat", "fix", "build", "chore", "ci", "docs", "refactor", "test"}
// Read the configuration file
if err := viper.ReadInConfig(); err != nil {
pterm.Debug.Printfln("Error reading config file: %s \n", err)
}
use_defaults := viper.GetBool("use_defaults")
if use_defaults {
commitTypes = append(default_commit_types, viper.GetStringSlice("custom_commit_types")...)
if len(viper.GetStringSlice("scopes")) > 0 {
scopes = append([]string{"none"}, viper.GetStringSlice("scopes")...)
}
} else {
commitTypes = viper.GetStringSlice("custom_commit_types")
scopes = viper.GetStringSlice("scopes")
}
// dedup slices just in case
commitTypes = removeDuplicateStr(commitTypes)
scopes = removeDuplicateStr(scopes)
}
func openGitRepo() (*git.Repository, error) {
// Validate the current directory is a git repository
cwd, err := os.Getwd()
if err != nil {
pterm.Fatal.Println("Error getting current working directory:", err)
}
// Open the Git repository at the current working directory
repo, err := git.PlainOpenWithOptions(cwd, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return nil, fmt.Errorf("not a git repository (or any of the parent directories): .git")
}
return repo, nil
}
func parseFlags() {
var showVersion bool
// Define a flag for version
flag.BoolVar(&showVersion, "version", false, "Show version information")
// Parse command-line arguments
flag.Parse()
// show version info and exist
if showVersion {
fmt.Printf("version: %s, commit: %s, built at %s\n", version, commit, date)
os.Exit(0)
}
}
func promptForCommit(commitTypes []string) (string, error) {
var commitMessage strings.Builder
var scope string
// Use PTerm's interactive select feature to present the options to the user and capture their selection
commitType, _ := pterm.DefaultInteractiveSelect.WithOptions(commitTypes).WithDefaultText("Commit Type").WithMaxHeight(20).Show()
if len(scopes) > 0 {
scope, _ = pterm.DefaultInteractiveSelect.WithOptions(scopes).WithDefaultText("Scope").WithMaxHeight(10).WithDefaultOption("none").Show()
} else {
scope, _ = pterm.DefaultInteractiveTextInput.WithDefaultText("Scope (optional)").Show()
}
// Prompt for single line short description
shortDescription, _ := pterm.DefaultInteractiveTextInput.WithDefaultText("Short Description").Show()
// Pompt for optional multiline long description
longDescription, _ := pterm.DefaultInteractiveTextInput.WithMultiLine().WithDefaultText("Long Description (optional)").Show()
if len(longDescription) > 0 {
longDescription = strings.TrimSpace(longDescription)
}
// confirm is this commit includes a breaking change
breakingChange, _ := pterm.DefaultInteractiveConfirm.WithDefaultText("Breaking Change").WithDefaultValue(false).Show()
// build commit message
commitMessage.WriteString(commitType)
if len(scope) > 0 && scope != "none" {
commitMessage.WriteString("(" + scope + ")")
}
var breakingChangeMessage string
if breakingChange {
// Prompt for breaking change message
breakingChangeMessage, _ = pterm.DefaultInteractiveTextInput.WithDefaultText("Breaking Change Note").Show()
commitMessage.WriteString("!: " + shortDescription)
} else {
commitMessage.WriteString(": " + shortDescription)
}
if len(longDescription) > 0 {
commitMessage.WriteString("\n\n" + longDescription)
}
if len(breakingChangeMessage) > 0 {
commitMessage.WriteString("\n\nBREAKING CHANGE: " + breakingChangeMessage)
}
return commitMessage.String(), nil
}
func removeDuplicateStr(strSlice []string) []string {
allKeys := make(map[string]bool)
list := []string{}
for _, item := range strSlice {
if _, value := allKeys[item]; !value {
allKeys[item] = true
list = append(list, item)
}
}
return list
}
func init() {
if strings.ToLower(os.Getenv("DEBUG")) == "true" {
// Enable debug messages in PTerm.
pterm.EnableDebugMessages()
}
// Parse argument flags
parseFlags()
// Validate we are running in a git repo and get status
gitStatus()
// load optional config file
loadConfig()
}
func main() {
// Prompt and build commit message
commitMsg, _ := promptForCommit(commitTypes)
// Create a temporary file
f, err := os.CreateTemp("", "commitMessage")
if err != nil {
pterm.Fatal.Println(err)
}
defer os.Remove(f.Name()) // clean up
if _, err := f.WriteString(commitMsg); err != nil {
pterm.Fatal.Println(err)
}
if err := f.Close(); err != nil {
pterm.Fatal.Println(err)
}
pterm.Debug.Println(commitMsg)
pterm.Debug.Println("temp file: " + f.Name())
// run git commit passing commit message, this ensures pre-commit hooks are run
cmd := exec.Command("git", "commit", "-F", f.Name())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Run the command
err = cmd.Run()
if err != nil {
pterm.Error.Println(err)
os.Exit(3)
}
}