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: ``` 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..8a2ab92 100644 --- a/internal/git/options.go +++ b/internal/git/options.go @@ -1,28 +1,36 @@ package git -import "github.com/spf13/pflag" +import ( + "strings" + + "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 +43,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 +71,30 @@ 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 { + result := deDuplicateFlag(o.ExtraGitFlags, "-F", "--file") + combination = append(combination, result...) + } return combination } + +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 +}