From 1eac0c260529b440d722a2f796f267b840598985 Mon Sep 17 00:00:00 2001 From: shipengqi Date: Fri, 29 Nov 2024 09:43:32 +0800 Subject: [PATCH 1/3] feat(flags): support more git flags --- internal/config/config.go | 5 ++++ internal/config/default.go | 1 + internal/git/options.go | 50 ++++++++++++++++++++++++-------------- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 354ac2f..bd581ba 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -24,11 +24,13 @@ const ( FieldKeyTemplateSelect = "template-select" ) +// Config represents a configuration object. type Config struct { defaultTmpl *templates.Template more []*templates.Template } +// New creates a new Config object. func New() *Config { return &Config{} } @@ -154,6 +156,7 @@ func (c *Config) createTemplatesSelect(label string) *huh.Form { ) } +// LoadTemplates reads a list of templates from the provided file. func LoadTemplates(file string) ([]*templates.Template, error) { if len(file) == 0 { return nil, nil @@ -166,10 +169,12 @@ func LoadTemplates(file string) ([]*templates.Template, error) { return load(fd) } +// Load reads a list of templates from the provided byte slice. func Load(data []byte) ([]*templates.Template, error) { return load(bytes.NewReader(data)) } +// load reads a list of templates from the provided io.Reader. func load(reader io.Reader) ([]*templates.Template, error) { var tmpls []*templates.Template d := yaml.NewDecoder(reader) diff --git a/internal/config/default.go b/internal/config/default.go index 1d8bb8b..d4aa6cc 100644 --- a/internal/config/default.go +++ b/internal/config/default.go @@ -1,5 +1,6 @@ package config +// DefaultCommitTemplate is the default commit template. const DefaultCommitTemplate = `--- name: default default: true diff --git a/internal/git/options.go b/internal/git/options.go index d9e53ba..71bac93 100644 --- a/internal/git/options.go +++ b/internal/git/options.go @@ -1,28 +1,34 @@ package git -import "github.com/spf13/pflag" +import ( + "github.com/spf13/pflag" +) type Options struct { - Quiet bool - Verbose bool - SignOff bool - All bool - Amend bool - DryRun bool - Author string - Date string + Quiet bool + Verbose bool + SignOff bool + All bool + Amend bool + DryRun bool + NoVerify bool + Author string + Date string + ExtraGitFlags []string } func NewOptions() *Options { return &Options{ - Quiet: false, - Verbose: false, - SignOff: false, - All: false, - Amend: false, - DryRun: false, - Author: "", - Date: "", + Quiet: false, + Verbose: false, + SignOff: false, + All: false, + Amend: false, + NoVerify: false, + DryRun: false, + Author: "", + Date: "", + ExtraGitFlags: []string{}, } } @@ -35,6 +41,8 @@ func (o *Options) AddFlags(f *pflag.FlagSet) { f.BoolVarP(&o.All, "all", "a", o.All, "commit all changed files.") f.BoolVarP(&o.SignOff, "signoff", "s", o.SignOff, "add a Signed-off-by trailer.") f.BoolVar(&o.Amend, "amend", o.Amend, "amend previous commit") + f.BoolVarP(&o.NoVerify, "no-verify", "n", o.NoVerify, "bypass pre-commit and commit-msg hooks.") + f.StringSliceVar(&o.ExtraGitFlags, "git-flag", o.ExtraGitFlags, "git flags, e.g. --git-flag=\"--branch\"") } func (o *Options) Combine(filename string) []string { @@ -61,9 +69,15 @@ func (o *Options) Combine(filename string) []string { if o.Amend { combination = append(combination, "--amend") } + if o.NoVerify { + combination = append(combination, "--no-verify") + } if o.DryRun { combination = append(combination, "--dry-run") } + if len(o.ExtraGitFlags) > 0 { + combination = append(combination, o.ExtraGitFlags...) + } return combination -} +} \ No newline at end of file From b91b8bf048e6fedd8d2b21481a4839bac680f031 Mon Sep 17 00:00:00 2001 From: shipengqi Date: Fri, 29 Nov 2024 10:15:52 +0800 Subject: [PATCH 2/3] docs(flags): usage of '--git-flag' --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index ecb9e1d..0a13bb5 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ Git Commit flags: override author for commit --date string override date for commit + --git-flag strings + git flags, e.g. --git-flag="--branch" + -n, --no-verify + bypass pre-commit and commit-msg hooks. -q, --quiet suppress summary after successful commit -s, --signoff @@ -59,6 +63,8 @@ Commitizen flags: Use "commitizen [command] --help" for more information about a command. ``` +> To use more Git flags, you can use the '--git-flag' flag. Please do not conflict with other Git commit flags. + Commit with commitizen: ``` From d5de0ac19bc28beabd2a70f019ee901755e2cc88 Mon Sep 17 00:00:00 2001 From: shipengqi Date: Fri, 29 Nov 2024 10:30:18 +0800 Subject: [PATCH 3/3] fix(flags): "-F/--file" provided by the "--git-flag" should be ignored --- internal/git/options.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/git/options.go b/internal/git/options.go index 71bac93..8a2ab92 100644 --- a/internal/git/options.go +++ b/internal/git/options.go @@ -1,6 +1,8 @@ package git import ( + "strings" + "github.com/spf13/pflag" ) @@ -76,8 +78,23 @@ func (o *Options) Combine(filename string) []string { combination = append(combination, "--dry-run") } if len(o.ExtraGitFlags) > 0 { - combination = append(combination, o.ExtraGitFlags...) + result := deDuplicateFlag(o.ExtraGitFlags, "-F", "--file") + combination = append(combination, result...) } return combination -} \ No newline at end of file +} + +func deDuplicateFlag(sli []string, short, long string) []string { + var result []string + for _, s := range sli { + if strings.HasPrefix(s, short) { + continue + } + if strings.HasPrefix(s, long) { + continue + } + result = append(result, s) + } + return result +}