package cmd import ( "fmt" "os" "os/exec" "strings" "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/viper" "gmgauthier.com/grokkit/internal/git" "gmgauthier.com/grokkit/internal/grok" ) var commitCmd = &cobra.Command{ Use: "commit", Short: "Generate message and commit staged changes", Run: func(cmd *cobra.Command, args []string) { model := viper.GetString("model") client := grok.NewClient() if !git.IsRepo() { color.Red("Error: Not inside a git repository") os.Exit(1) } diffOutput := git.Run([]string{"diff", "--cached"}) if diffOutput == "" { color.Yellow("No staged changes. Stage files with `git add` first.") return } systemPrompt := `You are an expert at writing conventional commit messages. Analyze the code diff below and generate a concise commit message: - Prefix: feat|fix|docs|style|refactor|perf|test|ci|chore - Imperative mood (e.g., 'Add', 'Fix'), capitalize first letter - Max 72 chars for subject line - Optional body lines wrapped at 72 chars - No trailing period on subject` messages := []map[string]string{ {"role": "system", "content": systemPrompt}, {"role": "user", "content": diffOutput}, } commitMsg := client.Stream(messages, model) fmt.Println(color.YellowString("Suggested commit message:")) fmt.Println() fmt.Print(color.CyanString(commitMsg)) fmt.Println() fmt.Print(color.YellowString("Commit now? (y/N): ")) var confirm string fmt.Scanln(&confirm) if strings.ToLower(strings.TrimSpace(confirm)) != "y" { color.Yellow("Aborted.") return } cmdOut, err := exec.Command("git", "commit", "-m", commitMsg).CombinedOutput() if err != nil { color.Red("Commit failed: %%v\\nOutput: %%s", err, cmdOut) os.Exit(1) } outputStr := strings.TrimSpace(string(cmdOut)) if len(outputStr) > 0 { fmt.Println(color.GreenString("Commit output: ") + outputStr) } else { color.Green("Committed successfully!") } }, }