From 7d09dbf1aa8d44897d23c1b3bb60965b185b77d8 Mon Sep 17 00:00:00 2001 From: Amolith Date: Thu, 23 Oct 2025 07:58:25 -0600 Subject: [PATCH] feat: support amending commits Implements: bug-21bcc08 Co-authored-by: Crush --- AGENTS.md | 3 ++- README.md | 4 +++- main.go | 9 ++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 4a109b2a69e0963e92b089a3e4fdb672f185e793..f431f135119570bc99e18843bf3d0fb53ed6dfab 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -109,9 +109,10 @@ Co-authored-by: This is a very long value, with spaces and - `-B` / `--breaking`: Boolean flag for breaking changes (adds `!` to subject) - `-b` / `--body`: String flag for commit body text (can use heredoc for multiline) - `-T` / `--trailer`: Repeatable flag accepting full trailer strings in `Key: value` format (not separate key/value args) +- `-a` / `--amend`: Boolean flag to amend the previous commit instead of creating a new one Trailer format detail: Each `-T` flag takes a complete trailer string like `-T "Co-authored-by: Name "`, NOT separate key and value arguments. ### Final Output -The formatted commit message must be piped to `git commit -F -` to read from stdin. +The formatted commit message must be piped to `git commit -F -` to read from stdin. When the `-a`/`--amend` flag is used, it pipes to `git commit --amend -F -` instead. diff --git a/README.md b/README.md index 219f1eeecaaaa73d906139bdf0ef7caba72156e4..9facdab192942801d307a1746b5e7fd458e9b53e 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Crush, that might be `~/.config/crush/CRUSH.md` or `./CRUSH.md` in a repo. For this in a section like `## Creating git commits` or something. ```markdown -Create commits exclusively using `formatted-commit`. Try to use it normally, but if it's not in my PATH, ask me to `go install git.secluded.site/formatted-commit@latest`. It has no sub-commands and the following options: +Create/amend commits exclusively using `formatted-commit`. Try to use it normally, but if it's not in my PATH, ask me to `go install git.secluded.site/formatted-commit@latest`. It has no sub-commands and the following options: -t --type Commit type (required) -s --scope Commit scope (optional) @@ -51,6 +51,7 @@ Create commits exclusively using `formatted-commit`. Try to use it normally, but -m --message Commit message (required) -b --body Commit body (optional) -T --trailer Trailer in 'Sentence-case-key: value' format (optional, repeatable) +-a --amend Amend the previous commit (optional) -h --help @@ -136,6 +137,7 @@ $ formatted-commit -h FLAGS + -a --amend Amend the previous commit (optional) -b --body Commit body (optional) -B --breaking Mark as breaking change (optional) -h --help Help for formatted-commit diff --git a/main.go b/main.go index 283913d7bb62794ac003573a414dccd2d4f7ecc1..f453757b30642a561fb59135935685454a9848d6 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ var ( body string scope string breakingChange bool + amend bool ) var rootCmd = &cobra.Command{ @@ -76,7 +77,12 @@ formatted-commit -t refactor -s "web/git-bug" -m "fancy shmancy" \ commitMsg.WriteString(trailersBlock) } - gitCmd := exec.Command("git", "commit", "-F", "-") + gitArgs := []string{"commit"} + if amend { + gitArgs = append(gitArgs, "--amend") + } + gitArgs = append(gitArgs, "-F", "-") + gitCmd := exec.Command("git", gitArgs...) gitCmd.Stdout = os.Stdout gitCmd.Stderr = os.Stderr @@ -112,6 +118,7 @@ func init() { rootCmd.Flags().StringVarP(&body, "body", "b", "", "commit body (optional)") rootCmd.Flags().StringVarP(&scope, "scope", "s", "", "commit scope (optional)") rootCmd.Flags().BoolVarP(&breakingChange, "breaking", "B", false, "mark as breaking change (optional)") + rootCmd.Flags().BoolVarP(&amend, "amend", "a", false, "amend the previous commit (optional)") if err := rootCmd.MarkFlagRequired("type"); err != nil { panic(err)