From 551b9a52071297e78bc72e33d2c7b5df61cf7e12 Mon Sep 17 00:00:00 2001 From: Aneesh Date: Wed, 8 Oct 2025 22:54:22 +0530 Subject: [PATCH 1/6] Added --auto flag in root.go --- cmd/cli/root.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/cli/root.go b/cmd/cli/root.go index fe6b258..281416c 100644 --- a/cmd/cli/root.go +++ b/cmd/cli/root.go @@ -76,6 +76,9 @@ func init() { // Add --dry-run flag to the commit command creatCommitMsg.Flags().Bool("dry-run", false, "Preview the prompt that would be sent to the LLM without making an API call") + // Add --auto flag to the commid command + creatCommitMsg.Flags().Bool("auto", false, "Automatically commit with the generated message") + rootCmd.AddCommand(creatCommitMsg) rootCmd.AddCommand(llmCmd) llmCmd.AddCommand(llmSetupCmd) From 86317416dc935944e514ce8893a04fcdbf57401f Mon Sep 17 00:00:00 2001 From: Aneesh Date: Wed, 8 Oct 2025 23:01:27 +0530 Subject: [PATCH 2/6] Updated command handler --- cmd/cli/root.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/cli/root.go b/cmd/cli/root.go index 281416c..8fbadfe 100644 --- a/cmd/cli/root.go +++ b/cmd/cli/root.go @@ -57,7 +57,12 @@ var creatCommitMsg = &cobra.Command{ if err != nil { return err } - CreateCommitMsg(dryRun) + + autoCommit, err := cmd.Flags().GetBool("auto") + if err != nil { + return err + } + CreateCommitMsg(dryRun, autoCommit) return nil }, } From fdb4a15df4495c85ed17710caae86382dc536aeb Mon Sep 17 00:00:00 2001 From: Aneesh Date: Wed, 8 Oct 2025 23:04:59 +0530 Subject: [PATCH 3/6] Feat: Add auto-commit flag to commit changes - Implements an `autoCommit` flag to automatically commit - changes after message generation. --- cmd/cli/createMsg.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cmd/cli/createMsg.go b/cmd/cli/createMsg.go index d32713e..c70a7a7 100644 --- a/cmd/cli/createMsg.go +++ b/cmd/cli/createMsg.go @@ -27,7 +27,7 @@ import ( // CreateCommitMsg launches the interactive flow for reviewing, regenerating, // editing, and accepting AI-generated commit messages in the current repo. // If dryRun is true, it displays the prompt without making an API call. -func CreateCommitMsg(dryRun bool) { +func CreateCommitMsg(dryRun bool, autoCommit bool) { // Validate COMMIT_LLM and required API keys useLLM, err := store.DefaultLLMKey() if err != nil { @@ -208,6 +208,23 @@ interactionLoop: pterm.Println() display.ShowChangesPreview(fileStats) + + // After message generation succeeds: + if autoCommit && !dryRun { + fmt.Println("\n🚀 Automatically committing with generated message...") + + cmd := exec.Command("git", "commit", "-m", finalMessage) + cmd.Dir = currentDir // set to your git repo path + + output, err := cmd.CombinedOutput() + if err != nil { + fmt.Printf("❌ Failed to commit: %v\n%s\n", err, output) + return + } + + fmt.Println("✅ Committed successfully!") + fmt.Println(string(output)) + } } type styleOption struct { @@ -234,6 +251,7 @@ var ( } errSelectionCancelled = errors.New("selection cancelled") ) + // resolveOllamaConfig returns the URL and model for Ollama, using environment variables as fallbacks func resolveOllamaConfig(apiKey string) (url, model string) { url = apiKey @@ -250,7 +268,6 @@ func resolveOllamaConfig(apiKey string) (url, model string) { return url, model } - func generateMessage(provider types.LLMProvider, config *types.Config, changes string, apiKey string, opts *types.GenerationOptions) (string, error) { switch provider { case types.ProviderGemini: From ab5c9178360ac6b032f21689b5f76db0cf4d0953 Mon Sep 17 00:00:00 2001 From: Aneesh Date: Wed, 8 Oct 2025 23:23:43 +0530 Subject: [PATCH 4/6] Command Works Cross-Platform now --- cmd/cli/createMsg.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/cmd/cli/createMsg.go b/cmd/cli/createMsg.go index c70a7a7..88a81fb 100644 --- a/cmd/cli/createMsg.go +++ b/cmd/cli/createMsg.go @@ -209,21 +209,36 @@ interactionLoop: pterm.Println() display.ShowChangesPreview(fileStats) - // After message generation succeeds: + // Auto-commit if flag is set (cross-platform compatible) if autoCommit && !dryRun { - fmt.Println("\n🚀 Automatically committing with generated message...") + pterm.Println() + spinner, err := pterm.DefaultSpinner. + WithSequence("⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"). + Start("Automatically committing with generated message...") + if err != nil { + pterm.Error.Printf("Failed to start spinner: %v\n", err) + return + } cmd := exec.Command("git", "commit", "-m", finalMessage) - cmd.Dir = currentDir // set to your git repo path + cmd.Dir = currentDir + // Ensure git command works across all platforms + cmd.Env = os.Environ() output, err := cmd.CombinedOutput() if err != nil { - fmt.Printf("❌ Failed to commit: %v\n%s\n", err, output) + spinner.Fail("Commit failed") + pterm.Error.Printf("Failed to commit: %v\n", err) + if len(output) > 0 { + pterm.Error.Println(string(output)) + } return } - fmt.Println("✅ Committed successfully!") - fmt.Println(string(output)) + spinner.Success("Committed successfully!") + if len(output) > 0 { + pterm.Info.Println(strings.TrimSpace(string(output))) + } } } From 81227b753732a7a7d6daddc900e3f78a88cdfafa Mon Sep 17 00:00:00 2001 From: Aneesh Date: Wed, 8 Oct 2025 23:24:10 +0530 Subject: [PATCH 5/6] Documentation Added for --auto flag --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 08b4ff9..74ae5a9 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,36 @@ Perfect for: - 🔒 **Privacy** - Verify what data would be shared with external APIs - 🧪 **Development** - Test prompt changes without API calls +### Auto Commit Mode + +Automatically commit with the generated message without manual confirmation: + +```bash +commit . --auto +``` + +This will: +- Generate the commit message using your configured LLM +- Automatically execute `git commit` with the generated message +- Skip the interactive review and manual confirmation step + +**Note**: The `--auto` flag cannot be combined with `--dry-run`. Dry run mode takes precedence and will only preview without committing. + +**Platform Support**: Works on Linux, macOS, and Windows. + +### Combining Flags + +```bash +# Preview only (no commit, no API call) +commit . --dry-run + +# Generate and auto-commit +commit . --auto + +# Generate with interactive review (default behavior) +commit . +``` + ### Setup LLM and API Key ```bash From 8a1b5929ec6fedf6cc4c8bb849af9cd21e024519 Mon Sep 17 00:00:00 2001 From: Aneesh Date: Wed, 8 Oct 2025 23:24:49 +0530 Subject: [PATCH 6/6] Fixed Groq Tests () --- internal/groq/groq.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/groq/groq.go b/internal/groq/groq.go index a8dc1be..013dd0e 100644 --- a/internal/groq/groq.go +++ b/internal/groq/groq.go @@ -8,7 +8,7 @@ import ( "net/http" "os" - httpClient "github.com/dfanso/commit-msg/internal/http" + internalHTTP "github.com/dfanso/commit-msg/internal/http" "github.com/dfanso/commit-msg/pkg/types" ) @@ -39,8 +39,14 @@ const defaultModel = "llama-3.3-70b-versatile" var ( // allow overrides in tests baseURL = "https://api.groq.com/openai/v1/chat/completions" + // httpClient can be overridden in tests; defaults to the internal http client + httpClient *http.Client ) +func init() { + httpClient = internalHTTP.GetClient() +} + // GenerateCommitMessage calls Groq's OpenAI-compatible chat completions API. func GenerateCommitMessage(_ *types.Config, changes string, apiKey string, opts *types.GenerationOptions) (string, error) { if changes == "" { @@ -82,7 +88,7 @@ func GenerateCommitMessage(_ *types.Config, changes string, apiKey string, opts req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey)) - resp, err := httpClient.GetClient().Do(req) + resp, err := httpClient.Do(req) if err != nil { return "", fmt.Errorf("failed to call Groq API: %w", err) }