Skip to content

Commit 3947d23

Browse files
authored
feat: add amend functionality to commit command (#35)
- Add a `--amend` boolean flag to `commitCmd` in `cmd/commit.go` - Add an `--amend` boolean flag to `Command` in `git/git.go` - Add `WithEnableAmend` option to `WithAmend` in `git/options.go` fix #32
1 parent d69f877 commit 3947d23

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

cmd/commit.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var (
2828
socksProxy string
2929
templateFile string
3030
templateString string
31+
commitAmend bool
3132
)
3233

3334
func init() {
@@ -41,6 +42,7 @@ func init() {
4142
commitCmd.PersistentFlags().StringVar(&socksProxy, "socks", "", "socks proxy")
4243
commitCmd.PersistentFlags().StringVar(&templateFile, "template_file", "", "git commit message file")
4344
commitCmd.PersistentFlags().StringVar(&templateString, "template_string", "", "git commit message string")
45+
commitCmd.PersistentFlags().BoolVar(&commitAmend, "amend", false, "replace the tip of the current branch by creating a new commit.")
4446
_ = viper.BindPFlag("output.file", commitCmd.PersistentFlags().Lookup("file"))
4547
}
4648

@@ -78,6 +80,7 @@ var commitCmd = &cobra.Command{
7880
g := git.New(
7981
git.WithDiffUnified(viper.GetInt("git.diff_unified")),
8082
git.WithExcludeList(viper.GetStringSlice("git.exclude_list")),
83+
git.WithEnableAmend(commitAmend),
8184
)
8285
diff, err := g.DiffFiles()
8386
if err != nil {

git/git.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Command struct {
2525
// Generate diffs with <n> lines of context instead of the usual three
2626
diffUnified int
2727
excludeList []string
28+
isAmend bool
2829
}
2930

3031
func (c *Command) excludeFiles() []string {
@@ -39,10 +40,15 @@ func (c *Command) excludeFiles() []string {
3940
func (c *Command) diffNames() *exec.Cmd {
4041
args := []string{
4142
"diff",
42-
"--staged",
4343
"--name-only",
4444
}
4545

46+
if c.isAmend {
47+
args = append(args, "HEAD^", "HEAD")
48+
} else {
49+
args = append(args, "--staged")
50+
}
51+
4652
args = append(args, c.excludeFiles()...)
4753

4854
return exec.Command(
@@ -54,12 +60,17 @@ func (c *Command) diffNames() *exec.Cmd {
5460
func (c *Command) diffFiles() *exec.Cmd {
5561
args := []string{
5662
"diff",
57-
"--staged",
5863
"--ignore-all-space",
5964
"--diff-algorithm=minimal",
6065
"--unified=" + strconv.Itoa(c.diffUnified),
6166
}
6267

68+
if c.isAmend {
69+
args = append(args, "HEAD^", "HEAD")
70+
} else {
71+
args = append(args, "--staged")
72+
}
73+
6374
args = append(args, c.excludeFiles()...)
6475

6576
return exec.Command(
@@ -89,6 +100,10 @@ func (c *Command) commit(val string) *exec.Cmd {
89100
fmt.Sprintf("--message=%s", val),
90101
}
91102

103+
if c.isAmend {
104+
args = append(args, "--amend")
105+
}
106+
92107
return exec.Command(
93108
"git",
94109
args...,
@@ -168,5 +183,6 @@ func New(opts ...Option) *Command {
168183
return &Command{
169184
diffUnified: cfg.diffUnified,
170185
excludeList: append(excludeFromDiff, cfg.excludeList...),
186+
isAmend: cfg.isAmend,
171187
}
172188
}

git/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ func WithExcludeList(val []string) Option {
3535
})
3636
}
3737

38+
func WithEnableAmend(val bool) Option {
39+
return optionFunc(func(c *config) {
40+
c.isAmend = val
41+
})
42+
}
43+
3844
// config is a struct that stores configuration options for the instrumentation.
3945
type config struct {
4046
diffUnified int
4147
excludeList []string
48+
isAmend bool
4249
}

0 commit comments

Comments
 (0)