Skip to content

Commit 32f7d9d

Browse files
Merge branch 'DFanso:main' into main
2 parents f25b3ac + 2c50506 commit 32f7d9d

4 files changed

Lines changed: 81 additions & 5 deletions

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ Perfect for:
134134
- 🔒 **Privacy** - Verify what data would be shared with external APIs
135135
- 🧪 **Development** - Test prompt changes without API calls
136136

137+
### Auto Commit Mode
138+
139+
Automatically commit with the generated message without manual confirmation:
140+
141+
```bash
142+
commit . --auto
143+
```
144+
145+
This will:
146+
- Generate the commit message using your configured LLM
147+
- Automatically execute `git commit` with the generated message
148+
- Skip the interactive review and manual confirmation step
149+
150+
**Note**: The `--auto` flag cannot be combined with `--dry-run`. Dry run mode takes precedence and will only preview without committing.
151+
152+
**Platform Support**: Works on Linux, macOS, and Windows.
153+
154+
### Combining Flags
155+
156+
```bash
157+
# Preview only (no commit, no API call)
158+
commit . --dry-run
159+
160+
# Generate and auto-commit
161+
commit . --auto
162+
163+
# Generate with interactive review (default behavior)
164+
commit .
165+
```
166+
137167
### Setup LLM and API Key
138168

139169
```bash

cmd/cli/createMsg.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
// CreateCommitMsg launches the interactive flow for reviewing, regenerating,
2828
// editing, and accepting AI-generated commit messages in the current repo.
2929
// If dryRun is true, it displays the prompt without making an API call.
30-
func CreateCommitMsg(dryRun bool) {
30+
func CreateCommitMsg(dryRun bool, autoCommit bool) {
3131
// Validate COMMIT_LLM and required API keys
3232
useLLM, err := store.DefaultLLMKey()
3333
if err != nil {
@@ -208,6 +208,38 @@ interactionLoop:
208208

209209
pterm.Println()
210210
display.ShowChangesPreview(fileStats)
211+
212+
// Auto-commit if flag is set (cross-platform compatible)
213+
if autoCommit && !dryRun {
214+
pterm.Println()
215+
spinner, err := pterm.DefaultSpinner.
216+
WithSequence("⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏").
217+
Start("Automatically committing with generated message...")
218+
if err != nil {
219+
pterm.Error.Printf("Failed to start spinner: %v\n", err)
220+
return
221+
}
222+
223+
cmd := exec.Command("git", "commit", "-m", finalMessage)
224+
cmd.Dir = currentDir
225+
// Ensure git command works across all platforms
226+
cmd.Env = os.Environ()
227+
228+
output, err := cmd.CombinedOutput()
229+
if err != nil {
230+
spinner.Fail("Commit failed")
231+
pterm.Error.Printf("Failed to commit: %v\n", err)
232+
if len(output) > 0 {
233+
pterm.Error.Println(string(output))
234+
}
235+
return
236+
}
237+
238+
spinner.Success("Committed successfully!")
239+
if len(output) > 0 {
240+
pterm.Info.Println(strings.TrimSpace(string(output)))
241+
}
242+
}
211243
}
212244

213245
type styleOption struct {
@@ -234,6 +266,7 @@ var (
234266
}
235267
errSelectionCancelled = errors.New("selection cancelled")
236268
)
269+
237270
// resolveOllamaConfig returns the URL and model for Ollama, using environment variables as fallbacks
238271
func resolveOllamaConfig(apiKey string) (url, model string) {
239272
url = apiKey
@@ -250,7 +283,6 @@ func resolveOllamaConfig(apiKey string) (url, model string) {
250283
return url, model
251284
}
252285

253-
254286
func generateMessage(provider types.LLMProvider, config *types.Config, changes string, apiKey string, opts *types.GenerationOptions) (string, error) {
255287
switch provider {
256288
case types.ProviderGemini:

cmd/cli/root.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ var creatCommitMsg = &cobra.Command{
5757
if err != nil {
5858
return err
5959
}
60-
CreateCommitMsg(dryRun)
60+
61+
autoCommit, err := cmd.Flags().GetBool("auto")
62+
if err != nil {
63+
return err
64+
}
65+
CreateCommitMsg(dryRun, autoCommit)
6166
return nil
6267
},
6368
}
@@ -76,6 +81,9 @@ func init() {
7681
// Add --dry-run flag to the commit command
7782
creatCommitMsg.Flags().Bool("dry-run", false, "Preview the prompt that would be sent to the LLM without making an API call")
7883

84+
// Add --auto flag to the commid command
85+
creatCommitMsg.Flags().Bool("auto", false, "Automatically commit with the generated message")
86+
7987
rootCmd.AddCommand(creatCommitMsg)
8088
rootCmd.AddCommand(llmCmd)
8189
llmCmd.AddCommand(llmSetupCmd)

internal/groq/groq.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"net/http"
99
"os"
1010

11-
httpClient "github.com/dfanso/commit-msg/internal/http"
11+
internalHTTP "github.com/dfanso/commit-msg/internal/http"
1212
"github.com/dfanso/commit-msg/pkg/types"
1313
)
1414

@@ -39,8 +39,14 @@ const defaultModel = "llama-3.3-70b-versatile"
3939
var (
4040
// allow overrides in tests
4141
baseURL = "https://api.groq.com/openai/v1/chat/completions"
42+
// httpClient can be overridden in tests; defaults to the internal http client
43+
httpClient *http.Client
4244
)
4345

46+
func init() {
47+
httpClient = internalHTTP.GetClient()
48+
}
49+
4450
// GenerateCommitMessage calls Groq's OpenAI-compatible chat completions API.
4551
func GenerateCommitMessage(_ *types.Config, changes string, apiKey string, opts *types.GenerationOptions) (string, error) {
4652
if changes == "" {
@@ -82,7 +88,7 @@ func GenerateCommitMessage(_ *types.Config, changes string, apiKey string, opts
8288
req.Header.Set("Content-Type", "application/json")
8389
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
8490

85-
resp, err := httpClient.GetClient().Do(req)
91+
resp, err := httpClient.Do(req)
8692
if err != nil {
8793
return "", fmt.Errorf("failed to call Groq API: %w", err)
8894
}

0 commit comments

Comments
 (0)