Skip to content

Commit 88cd482

Browse files
committed
imagine using custom comment for ur autoCommitGenerator thingy
1 parent fa3fcbc commit 88cd482

7 files changed

Lines changed: 102 additions & 70 deletions

File tree

cmd/config.go

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
// File: cmd/config.go
21
package cmd
32

43
import (
54
"fmt"
65
"os"
76
"os/exec"
8-
"path/filepath"
9-
"strings"
107

118
"smartcommit/config"
129

@@ -16,68 +13,85 @@ import (
1613
var ConfigCmd = &cobra.Command{
1714
Use: "config",
1815
Short: "View or change smartcommit configuration",
19-
}
16+
Long: `Manage smartcommit configuration
2017
21-
var setCmd = &cobra.Command{
22-
Use: "set <key> <value>",
23-
Short: "Set a configuration value",
24-
Args: cobra.ExactArgs(2),
25-
Run: func(cmd *cobra.Command, args []string) {
26-
key, value := args[0], args[1]
27-
cfg := config.LoadOrDefault()
28-
cfg.Set(key, value)
29-
if err := config.Save(cfg); err != nil {
30-
fmt.Println("❌ Failed to save config:", err)
31-
return
32-
}
33-
fmt.Println("✅ Config updated")
34-
},
18+
Examples:
19+
smartcommit config edit # Edit the system prompt (tone/style)
20+
smartcommit config show # View current configuration`,
3521
}
3622

37-
var showCmd = &cobra.Command{
38-
Use: "show",
39-
Short: "Display current configuration",
23+
var EditConfigCmd = &cobra.Command{
24+
Use: "edit",
25+
Short: "Edit system prompt in your preferred editor (e.g., vim, nano, code)",
4026
Run: func(cmd *cobra.Command, args []string) {
4127
cfg := config.LoadOrDefault()
42-
cfg.PrettyPrint()
43-
},
44-
}
4528

46-
var editCmd = &cobra.Command{
47-
Use: "edit system_prompt",
48-
Short: "Edit the system prompt using your default editor",
49-
Args: cobra.ExactArgs(1),
50-
Run: func(cmd *cobra.Command, args []string) {
51-
if args[0] != "system_prompt" {
52-
fmt.Println("❌ Only 'system_prompt' can be edited via editor for now.")
29+
tmpFile, err := os.CreateTemp("", "smartcommit-prompt-*.txt")
30+
if err != nil {
31+
fmt.Println("❌ Failed to create temp file:", err)
5332
return
5433
}
34+
defer os.Remove(tmpFile.Name())
5535

56-
cfg := config.LoadOrDefault()
57-
tmpfile := filepath.Join(os.TempDir(), "smartcommit_system_prompt.txt")
58-
os.WriteFile(tmpfile, []byte(cfg.SystemPrompt), 0644)
36+
// Write current prompt to temp file
37+
_, _ = tmpFile.WriteString(cfg.SystemPrompt)
38+
tmpFile.Close()
5939

40+
// Open in default editor
6041
editor := os.Getenv("EDITOR")
6142
if editor == "" {
6243
editor = "vim"
6344
}
45+
cmdEdit := exec.Command(editor, tmpFile.Name())
46+
cmdEdit.Stdin = os.Stdin
47+
cmdEdit.Stdout = os.Stdout
48+
cmdEdit.Stderr = os.Stderr
6449

65-
execCmd := exec.Command(editor, tmpfile)
66-
execCmd.Stdin = os.Stdin
67-
execCmd.Stdout = os.Stdout
68-
execCmd.Stderr = os.Stderr
69-
execCmd.Run()
50+
if err := cmdEdit.Run(); err != nil {
51+
fmt.Println("❌ Failed to open editor:", err)
52+
return
53+
}
7054

71-
updated, _ := os.ReadFile(tmpfile)
72-
cfg.SystemPrompt = strings.TrimSpace(string(updated))
73-
if err := config.Save(cfg); err != nil {
74-
fmt.Println("❌ Failed to save:", err)
55+
// Read edited prompt
56+
editedBytes, err := os.ReadFile(tmpFile.Name())
57+
if err != nil {
58+
fmt.Println("❌ Failed to read edited prompt:", err)
7559
return
7660
}
77-
fmt.Println("✅ Updated system prompt")
61+
62+
edited := string(editedBytes)
63+
if edited == "" {
64+
fmt.Println("⚠️ Prompt left empty. Aborting.")
65+
return
66+
}
67+
68+
cfg.SystemPrompt = edited
69+
if err := config.Save(cfg); err != nil {
70+
fmt.Println("❌ Failed to save config:", err)
71+
} else {
72+
fmt.Println("✅ System prompt updated successfully.")
73+
}
7874
},
7975
}
8076

81-
func init() {
82-
ConfigCmd.AddCommand(setCmd, showCmd, editCmd)
77+
78+
var ShowConfigCmd = &cobra.Command{
79+
Use: "show",
80+
Short: "Show current config",
81+
Run: func(cmd *cobra.Command, args []string) {
82+
cfg := config.LoadOrDefault()
83+
fmt.Printf("\nSystem Prompt: %s\n", cfg.SystemPrompt)
84+
fmt.Printf("Provider: %s\n", cfg.Provider)
85+
fmt.Printf("Model: %s\n", cfg.Model)
86+
if cfg.APIKey != "" {
87+
fmt.Println("API Key: [set]")
88+
} else {
89+
fmt.Println("API Key: [not set]")
90+
}
91+
},
8392
}
93+
94+
func init() {
95+
ConfigCmd.AddCommand(EditConfigCmd)
96+
ConfigCmd.AddCommand(ShowConfigCmd)
97+
}

cmd/generate.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package cmd
22

33
import (
4-
"bufio"
54
"fmt"
65
"os"
76
"os/exec"
87
"smartcommit/config"
98
"smartcommit/diff"
109
"smartcommit/llm"
1110
"smartcommit/prompt"
11+
"time"
1212

1313
promptui "github.com/manifoldco/promptui"
1414
"github.com/spf13/cobra"
@@ -19,7 +19,6 @@ var GenerateCmd = &cobra.Command{
1919
Short: "Generate a commit message using AI",
2020
Run: func(cmd *cobra.Command, args []string) {
2121
cfg := config.LoadOrDefault()
22-
2322

2423
diffText, err := diff.GetStagedDiff()
2524
if err != nil || len(diffText) == 0 {
@@ -41,25 +40,33 @@ var GenerateCmd = &cobra.Command{
4140
return
4241
}
4342

44-
reader := bufio.NewReader(os.Stdin)
45-
4643
for {
4744
fmt.Println("\n💡 Generated Commit Message:")
4845
fmt.Println("----------------------------------")
4946
fmt.Println(message)
5047
fmt.Println("----------------------------------")
51-
fmt.Print("Choose: [c]ommit, [e]dit prompt, [r]egenerate, [q]uit: ")
48+
fmt.Print("Choose [c]ommit, [e]dit message, [r]egenerate, [q]uit: ")
49+
50+
var choice string
51+
fmt.Scanln(&choice)
5252

53-
choice, _ := reader.ReadString('\n')
54-
switch choice[:1] {
53+
switch choice {
5554
case "c":
5655
runGitCommit(message)
5756
return
5857
case "e":
59-
promptText = editPrompt(promptText)
60-
message, _ = provider.Generate(promptText)
58+
message = editMessage(message)
6159
case "r":
62-
message, _ = provider.Generate(promptText)
60+
fmt.Print("\n🔄 Regenerating")
61+
for i := 0; i < 3; i++ {
62+
fmt.Print(".")
63+
time.Sleep(200 * time.Millisecond)
64+
}
65+
message, err = provider.Generate(promptText)
66+
if err != nil {
67+
fmt.Println("❌ Regeneration failed:", err)
68+
continue
69+
}
6370
case "q":
6471
return
6572
default:
@@ -84,15 +91,16 @@ func executeCommand(name string, args ...string) error {
8491
return c.Run()
8592
}
8693

87-
func editPrompt(defaultPrompt string) string {
94+
func editMessage(current string) string {
8895
prompt := promptui.Prompt{
89-
Label: "Edit Prompt",
90-
Default: defaultPrompt,
96+
Label: "Edit Commit Message",
97+
Default: current,
9198
AllowEdit: true,
9299
}
93100
result, err := prompt.Run()
94101
if err != nil {
95-
return defaultPrompt
102+
fmt.Println("⚠️ Edit cancelled.")
103+
return current
96104
}
97105
return result
98106
}

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func defaultConfig() *Config {
2121
return &Config{
2222
Provider: "ollama",
2323
Model: "llama3",
24-
SystemPrompt: "You are a terse, developer-friendly AI who writes conventional commit messages. Prefer single-line messages like 'feat: add ...' or 'fix: resolve ...'. Avoid PR-style summaries, bullet points, also this is official commit message. JUST SHOW THE COMMIT MESSAGE, NO OTHER BS.",
24+
SystemPrompt: "You are a terse, developer-friendly AI who writes conventional commit messages. Prefer single-line messages like 'feat: add ...' or 'fix: resolve ...'. Avoid PR-style summaries, bullet points, also this is official commit message. JUST SHOW THE COMMIT MESSAGE, NO OTHER BS. DONT GIVE ANY OTHER SUGGESTIONS OR BRACKETS OR ANYTHING, THIS ARE LIKE LITERAL COMMITS! DONT MESS THEM UP JUST GIVE THE COMMIT!",
2525
}
2626
}
2727

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ require (
1212
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
1313
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1414
github.com/spf13/pflag v1.0.6 // indirect
15-
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect
15+
golang.org/x/sys v0.33.0 // indirect
1616
)

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
1414
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
1515
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
1616
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
17-
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4=
1817
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
18+
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
19+
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
1920
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2021
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2122
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

main.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Entry point: main.go
21
package main
32

43
import (
@@ -13,19 +12,29 @@ import (
1312
func main() {
1413
rootCmd := &cobra.Command{
1514
Use: "smartcommit",
16-
Short: "Generate Git commit messages using AI",
17-
Long: `smartcommit is a CLI tool that uses local or remote LLMs (e.g. Ollama, OpenAI) to generate commit messages
18-
based on staged Git changes. It supports interactive flow, multiple backends, and prompt customization.`,
15+
Short: "AI-powered Git commit message generator",
16+
Long: `smartcommit is a CLI tool to generate AI-based Git commit messages
17+
from staged changes using local or remote LLMs (e.g., Ollama, OpenAI).
18+
19+
Examples:
20+
smartcommit generate Generate a commit message from staged changes
21+
smartcommit config edit Edit the system prompt (tone/style)
22+
smartcommit config show View current configuration
23+
24+
Run 'smartcommit [command] --help' for detailed command help.`,
1925
}
26+
27+
// Override Cobra's help output with our Long text
28+
rootCmd.SetHelpFunc(func(cmd *cobra.Command, _ []string) {
29+
fmt.Println(cmd.Long)
30+
})
2031

32+
// Register subcommands
2133
rootCmd.AddCommand(cmd.GenerateCmd)
2234
rootCmd.AddCommand(cmd.ConfigCmd)
2335

24-
rootCmd.PersistentFlags().BoolP("help", "h", false, "Show help message")
25-
2636
if err := rootCmd.Execute(); err != nil {
2737
fmt.Println("❌", err)
2838
os.Exit(1)
2939
}
3040
}
31-

smartcommit

2 KB
Binary file not shown.

0 commit comments

Comments
 (0)