Skip to content

Commit 7b3e062

Browse files
authored
feat(options): add more options of 'git commit' command (#44)
* feat(options): add more options of 'git commit' command add more options of 'git commit', rename '--add' to '--all' BREAKING CHANGE: rename '--add' to 'all' Closes #43 * WIP(options): rename combine
1 parent 2980d77 commit 7b3e062

File tree

4 files changed

+59
-18
lines changed

4 files changed

+59
-18
lines changed

cmd/cz/cz.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ func New() *cobra.Command {
3939

4040
if o.DryRun {
4141
fmt.Println(convutil.B2S(msg))
42-
return nil
42+
fmt.Println("")
43+
// inherits the --dry-run argument from the parent command
44+
o.GitOptions.DryRun = o.DryRun
4345
}
4446
output, err := git.Commit(msg, o.GitOptions)
4547
if err != nil {

internal/git/git.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,8 @@ func Commit(msg []byte, opts *Options) (string, error) {
6262
return "", err
6363
}
6464

65-
tokens := []string{
66-
"commit",
67-
"-F",
68-
temp.Name(),
69-
}
70-
if opts.Add {
71-
tokens = append(tokens, "-a")
72-
}
73-
if opts.SignOff {
74-
tokens = append(tokens, "-s")
75-
}
76-
stdout, err := cliutil.ExecContext(context.TODO(), "git", tokens...)
65+
args := opts.Combine(temp.Name())
66+
stdout, err := cliutil.ExecContext(context.TODO(), "git", args...)
7767
if err != nil {
7868
return "", err
7969
}

internal/git/options.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,67 @@ package git
33
import "github.com/spf13/pflag"
44

55
type Options struct {
6+
Quiet bool
7+
Verbose bool
68
SignOff bool
7-
Add bool
9+
All bool
10+
Amend bool
11+
DryRun bool
12+
Author string
13+
Date string
814
}
915

1016
func NewOptions() *Options {
1117
return &Options{
18+
Quiet: false,
19+
Verbose: false,
1220
SignOff: false,
13-
Add: false,
21+
All: false,
22+
Amend: false,
23+
DryRun: false,
24+
Author: "",
25+
Date: "",
1426
}
1527
}
1628

1729
func (o *Options) AddFlags(f *pflag.FlagSet) {
18-
f.BoolVarP(&o.Add, "add", "a", o.Add, "tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.")
19-
f.BoolVarP(&o.SignOff, "signoff", "s", o.SignOff, "add a Signed-off-by trailer by the committer at the end of the commit log message.")
30+
// inherits the --dry-run argument from the parent command
31+
f.BoolVarP(&o.Quiet, "quiet", "q", o.Quiet, "suppress summary after successful commit")
32+
f.BoolVarP(&o.Verbose, "verbose", "v", o.Verbose, "show diff in commit message template")
33+
f.StringVar(&o.Author, "author", o.Author, "override author for commit")
34+
f.StringVar(&o.Date, "date", o.Date, "override date for commit")
35+
f.BoolVarP(&o.All, "all", "a", o.All, "commit all changed files.")
36+
f.BoolVarP(&o.SignOff, "signoff", "s", o.SignOff, "add a Signed-off-by trailer.")
37+
f.BoolVar(&o.Amend, "amend", o.Amend, "amend previous commit")
38+
}
39+
40+
func (o *Options) Combine(filename string) []string {
41+
combination := []string{
42+
"commit",
43+
"-F",
44+
filename,
45+
}
46+
if o.Quiet {
47+
combination = append(combination, "-q")
48+
}
49+
if o.Verbose {
50+
combination = append(combination, "-v")
51+
}
52+
if o.Author != "" {
53+
combination = append(combination, "--author", o.Author)
54+
}
55+
if o.Date != "" {
56+
combination = append(combination, "--date", o.Date)
57+
}
58+
if o.All {
59+
combination = append(combination, "-a")
60+
}
61+
if o.Amend {
62+
combination = append(combination, "--amend")
63+
}
64+
if o.DryRun {
65+
combination = append(combination, "--dry-run")
66+
}
67+
68+
return combination
2069
}

internal/options/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func New() *Options {
2424
func (o *Options) AddFlags(f *pflag.FlagSet) {
2525
o.GitOptions.AddFlags(f)
2626

27-
f.BoolVar(&o.DryRun, "dry-run", o.DryRun, "you can use the --dry-run flag to preview the message that would be committed, without really submitting it.")
27+
f.BoolVar(&o.DryRun, "dry-run", o.DryRun, "do not create a commit, but show the message and list of paths \nthat are to be committed.")
2828
f.StringVarP(&o.Template, "template", "t", o.Template, "template name to use when multiple templates exist.")
2929
f.BoolVarP(&o.Default, "default", "d", o.Default, "use the default template, '--default' has a higher priority than '--template'.")
3030
f.BoolVar(&o.NoTTY, "no-tty", o.NoTTY, "make sure that the TTY (terminal) is never used for any output.")

0 commit comments

Comments
 (0)