grokkit/cmd/commit.go
Greg Gauthier 99ef10b16b
Some checks failed
CI / Test (push) Successful in 34s
CI / Lint (push) Failing after 19s
CI / Build (push) Successful in 20s
refactor(cmd): extract run funcs and add injectable deps for testability
- Introduce newGrokClient and gitRun vars to allow mocking in tests.
- Refactor commit, commitmsg, history, prdescribe, and review cmds into separate run funcs.
- Update docs, lint, and review to use newGrokClient.
- Add comprehensive unit tests in run_test.go covering happy paths, errors, and edge cases.
- Expand grok client tests with SSE server mocks for Stream* methods.
2026-03-02 20:47:16 +00:00

61 lines
1.5 KiB
Go

package cmd
import (
"fmt"
"os/exec"
"github.com/fatih/color"
"github.com/spf13/cobra"
"gmgauthier.com/grokkit/config"
)
var commitCmd = &cobra.Command{
Use: "commit",
Short: "Generate message and commit staged changes",
Run: runCommit,
}
func runCommit(cmd *cobra.Command, args []string) {
diff, err := gitRun([]string{"diff", "--cached", "--no-color"})
if err != nil {
color.Red("Failed to get staged changes: %v", err)
return
}
if diff == "" {
color.Yellow("No staged changes!")
return
}
modelFlag, _ := cmd.Flags().GetString("model")
model := config.GetModel("commit", modelFlag)
client := newGrokClient()
messages := buildCommitMessages(diff)
color.Yellow("Generating commit message...")
msg := client.Stream(messages, model)
color.Cyan("\nProposed commit message:\n%s", msg)
var confirm string
color.Yellow("Commit with this message? (y/n): ")
if _, err := fmt.Scanln(&confirm); err != nil {
color.Red("Failed to read input: %v", err)
return
}
if confirm != "y" && confirm != "Y" {
color.Yellow("Aborted.")
return
}
if err := exec.Command("git", "commit", "-m", msg).Run(); err != nil {
color.Red("Git commit failed")
} else {
color.Green("✅ Committed successfully!")
}
}
func buildCommitMessages(diff string) []map[string]string {
return []map[string]string{
{"role": "system", "content": "Return ONLY a conventional commit message (type(scope): subject\n\nbody)."},
{"role": "user", "content": fmt.Sprintf("Staged changes:\n%s", diff)},
}
}