feat: support amending commits

Amolith and Crush created

Implements: bug-21bcc08
Co-authored-by: Crush <crush@charm.land>

Change summary

AGENTS.md | 3 ++-
README.md | 4 +++-
main.go   | 9 ++++++++-
3 files changed, 13 insertions(+), 3 deletions(-)

Detailed changes

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 <email>"`, 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.

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:
 <formatted-commit_flags>
 -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
 </formatted-commit_flags>
 <formatted-commit_example>
@@ -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

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)