Skip to content

Commit 4c590dc

Browse files
committed
Add support for amending commits with the --amend flag
1 parent 232f294 commit 4c590dc

File tree

10 files changed

+509
-565
lines changed

10 files changed

+509
-565
lines changed

.pre-commit-config.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
repos:
4+
- repo: https://github.com/pre-commit/pre-commit-hooks
5+
rev: v5.0.0
6+
hooks:
7+
- id: trailing-whitespace
8+
- id: end-of-file-fixer
9+
- id: check-yaml
10+
- id: check-added-large-files
11+
- repo: local
12+
hooks:
13+
- id: gofmt
14+
name: gofmt
15+
entry: gofmt -s -w
16+
language: system
17+
files: \.go$

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
- Support for cascading configuration: CLI, current directory, home directory, environment variables, defaults
99
- Added `--config` flag to specify explicit config file path
1010
- Initial support for generating commit messages
11+
- Support for amending existing commits with the `--amend` or `-a` flag

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ git ai commit --conventional
119119
# Avoid conventional format
120120
git ai commit --no-conventional
121121

122+
# Amend previous commit
123+
git ai commit --amend
124+
122125
# Use specific config file
123126
git ai --config /path/to/config.yaml commit
124127
```

cmd/commit/commit.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
conventionalCommits bool
1717
noConventionalCommits bool
1818
commitsWithDescriptions bool
19+
amendCommit bool
1920
)
2021

2122
// Cmd represents the commit command
@@ -33,6 +34,7 @@ func init() {
3334
Cmd.Flags().BoolVar(&conventionalCommits, "conventional", false, "Use conventional commit format (type(scope): description)")
3435
Cmd.Flags().BoolVar(&noConventionalCommits, "no-conventional", false, "Don't use conventional commit format")
3536
Cmd.Flags().BoolVar(&commitsWithDescriptions, "with-descriptions", false, "Generate commit messages with detailed descriptions")
37+
Cmd.Flags().BoolVarP(&amendCommit, "amend", "a", false, "Amend the previous commit instead of creating a new one")
3638
}
3739

3840
func executeCommit() {
@@ -43,8 +45,12 @@ func executeCommit() {
4345

4446
// Check if there are staged changes
4547
if !git.HasStagedChanges() {
46-
logger.Error("No staged changes found. Please stage your changes with 'git add' first.")
47-
os.Exit(1)
48+
if amendCommit {
49+
logger.PrintMessage("No staged changes found. Will amend the previous commit message only.")
50+
} else {
51+
logger.Error("No staged changes found. Please stage your changes with 'git add' first.")
52+
os.Exit(1)
53+
}
4854
}
4955

5056
// If the --with-descriptions flag wasn't explicitly set, check git config
@@ -68,10 +74,18 @@ func executeCommit() {
6874
// Determine whether to use conventional commits format
6975
useConventionalCommits := shouldUseConventionalCommits()
7076

71-
// Generate commit message based on staged changes and history
72-
logger.PrintMessage("Generating commit message with LLM...")
77+
// Generate commit message based on staged changes and history - with spinner
78+
spinner, err := ui.ShowSpinner("Generating commit message with LLM...")
79+
if err != nil {
80+
logger.Error("Failed to start spinner: %v", err)
81+
}
82+
7383
message := llm.GenerateCommitMessage(cfg, diff, recentCommits, useConventionalCommits, commitsWithDescriptions)
7484

85+
if spinner != nil {
86+
spinner.Success("Commit message generated!")
87+
}
88+
7589
// If auto-approve flag is not set, ask user to confirm or edit
7690
var proceed bool
7791
if !autoApprove {
@@ -92,12 +106,16 @@ func executeCommit() {
92106
}
93107

94108
// Create the commit with the message
95-
err = git.CreateCommit(message)
109+
err = git.CreateCommit(message, amendCommit)
96110
if err != nil {
97111
logger.Fatal("Failed to create commit: %v", err)
98112
}
99113

100-
logger.PrintMessage("Commit created successfully!")
114+
if amendCommit {
115+
logger.PrintMessage("Commit amended successfully!")
116+
} else {
117+
logger.PrintMessage("Commit created successfully!")
118+
}
101119
}
102120

103121
// shouldUseConventionalCommits determines whether to use conventional commit format

0 commit comments

Comments
 (0)