grokkit/cmd/commit.go

57 lines
1.5 KiB
Go
Raw Permalink Normal View History

2026-02-28 18:03:12 +00:00
package cmd
2026-02-28 18:28:27 +00:00
2026-02-28 19:56:23 +00:00
import (
"fmt"
"os/exec"
"github.com/fatih/color"
"github.com/spf13/cobra"
"gmgauthier.com/grokkit/config"
2026-02-28 19:56:23 +00:00
"gmgauthier.com/grokkit/internal/git"
"gmgauthier.com/grokkit/internal/grok"
)
2026-02-28 18:28:27 +00:00
var commitCmd = &cobra.Command{
Use: "commit",
Short: "Generate message and commit staged changes",
Run: func(cmd *cobra.Command, args []string) {
diff, err := git.Run([]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!")
2026-02-28 19:56:23 +00:00
return
}
modelFlag, _ := cmd.Flags().GetString("model")
model := config.GetModel(modelFlag)
client := grok.NewClient()
2026-02-28 19:56:23 +00:00
messages := []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)},
2026-02-28 19:56:23 +00:00
}
color.Yellow("Generating commit message...")
msg := client.Stream(messages, model)
2026-02-28 19:56:23 +00:00
color.Cyan("\nProposed commit message:\n%s", msg)
2026-02-28 19:56:23 +00:00
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" {
2026-02-28 19:56:23 +00:00
color.Yellow("Aborted.")
return
}
if err := exec.Command("git", "commit", "-m", msg).Run(); err != nil {
color.Red("Git commit failed")
2026-02-28 19:56:23 +00:00
} else {
color.Green("✅ Committed successfully!")
2026-02-28 19:56:23 +00:00
}
2026-02-28 18:28:27 +00:00
},
}