Skip to content

Commit 9810bd5

Browse files
authored
Merge pull request #108 from shipengqi/feat/support-more-flags
feat(flags): support more git flags
2 parents f67b946 + d5de0ac commit 9810bd5

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Git Commit flags:
4040
override author for commit
4141
--date string
4242
override date for commit
43+
--git-flag strings
44+
git flags, e.g. --git-flag="--branch"
45+
-n, --no-verify
46+
bypass pre-commit and commit-msg hooks.
4347
-q, --quiet
4448
suppress summary after successful commit
4549
-s, --signoff
@@ -59,6 +63,8 @@ Commitizen flags:
5963
Use "commitizen [command] --help" for more information about a command.
6064
```
6165

66+
> To use more Git flags, you can use the '--git-flag' flag. Please do not conflict with other Git commit flags.
67+
6268
Commit with commitizen:
6369

6470
```

internal/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ const (
2424
FieldKeyTemplateSelect = "template-select"
2525
)
2626

27+
// Config represents a configuration object.
2728
type Config struct {
2829
defaultTmpl *templates.Template
2930
more []*templates.Template
3031
}
3132

33+
// New creates a new Config object.
3234
func New() *Config {
3335
return &Config{}
3436
}
@@ -154,6 +156,7 @@ func (c *Config) createTemplatesSelect(label string) *huh.Form {
154156
)
155157
}
156158

159+
// LoadTemplates reads a list of templates from the provided file.
157160
func LoadTemplates(file string) ([]*templates.Template, error) {
158161
if len(file) == 0 {
159162
return nil, nil
@@ -166,10 +169,12 @@ func LoadTemplates(file string) ([]*templates.Template, error) {
166169
return load(fd)
167170
}
168171

172+
// Load reads a list of templates from the provided byte slice.
169173
func Load(data []byte) ([]*templates.Template, error) {
170174
return load(bytes.NewReader(data))
171175
}
172176

177+
// load reads a list of templates from the provided io.Reader.
173178
func load(reader io.Reader) ([]*templates.Template, error) {
174179
var tmpls []*templates.Template
175180
d := yaml.NewDecoder(reader)

internal/config/default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package config
22

3+
// DefaultCommitTemplate is the default commit template.
34
const DefaultCommitTemplate = `---
45
name: default
56
default: true

internal/git/options.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
package git
22

3-
import "github.com/spf13/pflag"
3+
import (
4+
"strings"
5+
6+
"github.com/spf13/pflag"
7+
)
48

59
type Options struct {
6-
Quiet bool
7-
Verbose bool
8-
SignOff bool
9-
All bool
10-
Amend bool
11-
DryRun bool
12-
Author string
13-
Date string
10+
Quiet bool
11+
Verbose bool
12+
SignOff bool
13+
All bool
14+
Amend bool
15+
DryRun bool
16+
NoVerify bool
17+
Author string
18+
Date string
19+
ExtraGitFlags []string
1420
}
1521

1622
func NewOptions() *Options {
1723
return &Options{
18-
Quiet: false,
19-
Verbose: false,
20-
SignOff: false,
21-
All: false,
22-
Amend: false,
23-
DryRun: false,
24-
Author: "",
25-
Date: "",
24+
Quiet: false,
25+
Verbose: false,
26+
SignOff: false,
27+
All: false,
28+
Amend: false,
29+
NoVerify: false,
30+
DryRun: false,
31+
Author: "",
32+
Date: "",
33+
ExtraGitFlags: []string{},
2634
}
2735
}
2836

@@ -35,6 +43,8 @@ func (o *Options) AddFlags(f *pflag.FlagSet) {
3543
f.BoolVarP(&o.All, "all", "a", o.All, "commit all changed files.")
3644
f.BoolVarP(&o.SignOff, "signoff", "s", o.SignOff, "add a Signed-off-by trailer.")
3745
f.BoolVar(&o.Amend, "amend", o.Amend, "amend previous commit")
46+
f.BoolVarP(&o.NoVerify, "no-verify", "n", o.NoVerify, "bypass pre-commit and commit-msg hooks.")
47+
f.StringSliceVar(&o.ExtraGitFlags, "git-flag", o.ExtraGitFlags, "git flags, e.g. --git-flag=\"--branch\"")
3848
}
3949

4050
func (o *Options) Combine(filename string) []string {
@@ -61,9 +71,30 @@ func (o *Options) Combine(filename string) []string {
6171
if o.Amend {
6272
combination = append(combination, "--amend")
6373
}
74+
if o.NoVerify {
75+
combination = append(combination, "--no-verify")
76+
}
6477
if o.DryRun {
6578
combination = append(combination, "--dry-run")
6679
}
80+
if len(o.ExtraGitFlags) > 0 {
81+
result := deDuplicateFlag(o.ExtraGitFlags, "-F", "--file")
82+
combination = append(combination, result...)
83+
}
6784

6885
return combination
6986
}
87+
88+
func deDuplicateFlag(sli []string, short, long string) []string {
89+
var result []string
90+
for _, s := range sli {
91+
if strings.HasPrefix(s, short) {
92+
continue
93+
}
94+
if strings.HasPrefix(s, long) {
95+
continue
96+
}
97+
result = append(result, s)
98+
}
99+
return result
100+
}

0 commit comments

Comments
 (0)