Skip to content

Commit 6d9e658

Browse files
committed
feat(prompt): added support optional scopes or explicit scope list in config
1 parent cd1c446 commit 6d9e658

File tree

4 files changed

+92
-74
lines changed

4 files changed

+92
-74
lines changed

.git-cc.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
use_defaults: true
2+
scopes:
3+
- config
4+
- manpage
5+
- prompt
6+
- readme
7+
- scripts

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,30 @@ To invoke simply run `git cc`
3636

3737
## Configuration
3838

39-
`git-cc` supports a simple yaml based configuration to customize the prompt behavoir on a repo basis. Simply add a `.git-cc.yaml` into the root of your repository. See [.git-cc.example.yaml](.git-cc.example.yaml)
39+
`git-cc` supports a simple yaml based configuration to customize the prompt behavoir on a repo basis. Simply add a `.git-cc.yaml` into the root of the repository. See [.git-cc.example.yaml](.git-cc.example.yaml)
4040

4141
```yaml
4242
# .git-cc.yaml
4343
use_defaults: true
44-
custom_commit_types: [
45-
build,
46-
chore,
47-
ci,
48-
docs,
49-
style,
50-
refactor,
51-
perf,
52-
test
53-
]
44+
custom_commit_types:
45+
- build
46+
- chore
47+
- ci
48+
- docs
49+
- style
50+
- refactor
51+
- perf
52+
- test
53+
scopes:
54+
- config
55+
- manpage
56+
- prompt
57+
- readme
58+
- scripts
5459
```
5560
5661
| property | options |
5762
| :-----------------: | :-----------------------------------------------------------------------------------------: |
5863
| use_defaults | If true use default commit types (default: true) |
5964
| custom_commit_types | Custom commit types to include when prompting, appended to defaults if `use_defaults: true` |
65+
| scopes | List of available scopes |

main.go

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ import (
3535

3636
// Build Info Vars
3737
var (
38-
version = "dev"
39-
commit = "none"
40-
date = "unknown"
38+
version = "dev"
39+
commit = "none"
40+
date = "unknown"
4141
)
4242

4343
// Global Vars
4444
var (
4545
commitTypes []string
46-
scopes []string
47-
gitRoot string
46+
scopes []string
47+
gitRoot string
4848
)
4949

5050
func gitStatus() {
@@ -53,33 +53,33 @@ func gitStatus() {
5353
pterm.Error.Println(err)
5454
os.Exit(1)
5555
}
56-
56+
5757
Worktree, err := repo.Worktree()
5858
if err != nil {
5959
pterm.Fatal.Println("Error opening Git repository:", err)
6060
}
61-
61+
6262
gitRoot = Worktree.Filesystem.Root()
6363
pterm.Debug.Println("Root directory of Git repository:", gitRoot)
64-
64+
6565
status, err := Worktree.Status()
6666
if err != nil {
6767
fmt.Println("Failed to get status:", err)
6868
os.Exit(1)
6969
}
70-
70+
7171
// Check if there are staged changes
7272
hasStagedChanges := false
7373
hasUntracked := false
7474
for _, entry := range status {
7575
if entry.Staging != git.Untracked && entry.Staging != git.Unmodified {
7676
hasStagedChanges = true
7777
break
78-
} else if entry.Staging == git.Untracked {
79-
hasUntracked = true
80-
}
78+
} else if entry.Staging == git.Untracked {
79+
hasUntracked = true
8180
}
82-
81+
}
82+
8383
// Error out if nothing is staged
8484
if !hasStagedChanges && hasUntracked {
8585
pterm.Error.Println("nothing added to commit but untracked files present (use \"git add\" to track)")
@@ -91,36 +91,40 @@ func gitStatus() {
9191
}
9292

9393
func loadConfig() {
94-
// Set the file name of the configuration file
95-
viper.SetConfigName(".git-cc.yaml")
96-
// config file format
97-
viper.SetConfigType("yaml")
98-
// Add the path to look for the config file
99-
viper.AddConfigPath(gitRoot)
100-
// Optional. If you want to support environment variables, use this
101-
viper.AutomaticEnv()
102-
103-
// Set Default Config Values
104-
viper.SetDefault("use_defaults", true)
105-
viper.SetDefault("custom_commit_types", []string{})
106-
viper.SetDefault("scopes", []string{})
107-
108-
default_commit_types := []string{"feat", "fix", "build", "chore", "ci", "docs", "refactor", "test"}
109-
110-
// Read the configuration file
111-
if err := viper.ReadInConfig(); err != nil {
112-
pterm.Debug.Printfln("Error reading config file: %s \n", err)
113-
}
114-
115-
if viper.GetBool("use_defaults") {
116-
commitTypes = append(default_commit_types, viper.GetStringSlice("custom_commit_types")...)
117-
} else {
118-
commitTypes = viper.GetStringSlice("custom_commit_types")
94+
// Set the file name of the configuration file
95+
viper.SetConfigName(".git-cc.yaml")
96+
// config file format
97+
viper.SetConfigType("yaml")
98+
// Add the path to look for the config file
99+
viper.AddConfigPath(gitRoot)
100+
// Optional. If you want to support environment variables, use this
101+
viper.AutomaticEnv()
102+
103+
// Set Default Config Values
104+
viper.SetDefault("use_defaults", true)
105+
viper.SetDefault("custom_commit_types", []string{})
106+
viper.SetDefault("scopes", []string{})
107+
108+
default_commit_types := []string{"feat", "fix", "build", "chore", "ci", "docs", "refactor", "test"}
109+
110+
// Read the configuration file
111+
if err := viper.ReadInConfig(); err != nil {
112+
pterm.Debug.Printfln("Error reading config file: %s \n", err)
113+
}
114+
115+
use_defaults := viper.GetBool("use_defaults")
116+
if use_defaults {
117+
commitTypes = append(default_commit_types, viper.GetStringSlice("custom_commit_types")...)
118+
if len(viper.GetStringSlice("scopes")) > 0 {
119+
scopes = append([]string{"none"}, viper.GetStringSlice("scopes")...)
119120
}
120-
121-
// dedup slices just in case
122-
commitTypes = removeDuplicateStr(commitTypes)
123-
scopes = removeDuplicateStr(viper.GetStringSlice("scopes"))
121+
} else {
122+
commitTypes = viper.GetStringSlice("custom_commit_types")
123+
scopes = viper.GetStringSlice("scopes")
124+
}
125+
// dedup slices just in case
126+
commitTypes = removeDuplicateStr(commitTypes)
127+
scopes = removeDuplicateStr(scopes)
124128
}
125129

126130
func openGitRepo() (*git.Repository, error) {
@@ -143,7 +147,7 @@ func parseFlags() {
143147
var showVersion bool
144148

145149
// Define a flag for version
146-
flag.BoolVar(&showVersion,"version", false, "Show version information")
150+
flag.BoolVar(&showVersion, "version", false, "Show version information")
147151

148152
// Parse command-line arguments
149153
flag.Parse()
@@ -163,7 +167,7 @@ func promptForCommit(commitTypes []string) (string, error) {
163167
commitType, _ := pterm.DefaultInteractiveSelect.WithOptions(commitTypes).WithDefaultText("Commit Type").WithMaxHeight(20).Show()
164168

165169
if len(scopes) > 0 {
166-
scope, _ = pterm.DefaultInteractiveSelect.WithOptions(scopes).WithDefaultText("Scope (optional)").WithMaxHeight(10).Show()
170+
scope, _ = pterm.DefaultInteractiveSelect.WithOptions(scopes).WithDefaultText("Scope").WithMaxHeight(10).WithDefaultOption("none").Show()
167171
} else {
168172
scope, _ = pterm.DefaultInteractiveTextInput.WithDefaultText("Scope (optional)").Show()
169173
}
@@ -184,7 +188,7 @@ func promptForCommit(commitTypes []string) (string, error) {
184188
// build commit message
185189
commitMessage.WriteString(commitType)
186190

187-
if len(scope) > 0 {
191+
if len(scope) > 0 && scope != "none" {
188192
commitMessage.WriteString("(" + scope + ")")
189193
}
190194

@@ -193,7 +197,7 @@ func promptForCommit(commitTypes []string) (string, error) {
193197
if breakingChange {
194198
// Prompt for breaking change message
195199
breakingChangeMessage, _ = pterm.DefaultInteractiveTextInput.WithDefaultText("Breaking Change Note").Show()
196-
200+
197201
commitMessage.WriteString("!: " + shortDescription)
198202
} else {
199203
commitMessage.WriteString(": " + shortDescription)
@@ -222,8 +226,6 @@ func removeDuplicateStr(strSlice []string) []string {
222226
return list
223227
}
224228

225-
226-
227229
func init() {
228230
if strings.ToLower(os.Getenv("DEBUG")) == "true" {
229231
// Enable debug messages in PTerm.
@@ -235,12 +237,11 @@ func init() {
235237

236238
// Validate we are running in a git repo and get status
237239
gitStatus()
238-
240+
239241
// load optional config file
240242
loadConfig()
241-
242-
}
243243

244+
}
244245

245246
func main() {
246247
// Prompt and build commit message
@@ -271,4 +272,4 @@ func main() {
271272
pterm.Error.Println(err)
272273
os.Exit(3)
273274
}
274-
}
275+
}

share/man/git-cc.1.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,26 @@ git-cc is interactive git sub-command that will help you craft beautify and info
1212

1313
## Configuration
1414

15-
`git-cc` supports a simple yaml based configuration to customize the prompt behavoir on a repo basis. Simply add a `.git-cc.yaml` into the root of your repository. See [.git-cc.example.yaml](.git-cc.example.yaml)
15+
`git-cc` supports a simple yaml based configuration to customize the prompt behavoir on a repo basis. Simply add a `.git-cc.yaml` into the root of the repository.
1616

1717
```yaml
1818
# .git-cc.yaml
1919
use_defaults: true
20-
custom_commit_types: [
21-
build,
22-
chore,
23-
ci,
24-
docs,
25-
style,
26-
refactor,
27-
perf,
28-
test
29-
]
20+
custom_commit_types:
21+
- build
22+
- chore
23+
- ci
24+
- docs
25+
- style
26+
- refactor
27+
- perf
28+
- test
29+
scopes:
30+
- config
31+
- manpage
32+
- prompt
33+
- readme
34+
- scripts
3035
```
3136
3237
### Properties

0 commit comments

Comments
 (0)