@@ -18,11 +18,10 @@ import (
1818 "github.com/pterm/pterm"
1919)
2020
21+ func CreateCommitMsg () {
2122
22- func CreateCommitMsg () {
23-
24- // Validate COMMIT_LLM and required API keys
25- useLLM ,err := store .DefaultLLMKey ()
23+ // Validate COMMIT_LLM and required API keys
24+ useLLM , err := store .DefaultLLMKey ()
2625 if err != nil {
2726 pterm .Error .Printf ("No LLM configured. Run: commit llm setup\n " )
2827 os .Exit (1 )
@@ -31,144 +30,142 @@ func CreateCommitMsg () {
3130 commitLLM := useLLM .LLM
3231 apiKey := useLLM .APIKey
3332
33+ // Get current directory
34+ currentDir , err := os .Getwd ()
35+ if err != nil {
36+ pterm .Error .Printf ("Failed to get current directory: %v\n " , err )
37+ os .Exit (1 )
38+ }
3439
35- // Get current directory
36- currentDir , err := os .Getwd ()
37- if err != nil {
38- pterm .Error .Printf ("Failed to get current directory: %v\n " , err )
39- os .Exit (1 )
40- }
40+ // Check if current directory is a git repository
41+ if ! git .IsRepository (currentDir ) {
42+ pterm .Error .Printf ("Current directory is not a Git repository: %s\n " , currentDir )
43+ os .Exit (1 )
44+ }
4145
42- // Check if current directory is a git repository
43- if ! git .IsRepository (currentDir ) {
44- pterm .Error .Printf ("Current directory is not a Git repository: %s\n " , currentDir )
45- os .Exit (1 )
46- }
46+ // Create a minimal config for the API
47+ config := & types.Config {
48+ GrokAPI : "https://api.x.ai/v1/chat/completions" ,
49+ }
4750
48- // Create a minimal config for the API
49- config := & types.Config {
50- GrokAPI : "https://api.x.ai/v1/chat/completions" ,
51- }
51+ // Create a repo config for the current directory
52+ repoConfig := types.RepoConfig {
53+ Path : currentDir ,
54+ }
5255
53- // Create a repo config for the current directory
54- repoConfig := types.RepoConfig {
55- Path : currentDir ,
56- }
56+ // Get file statistics before fetching changes
57+ fileStats , err := stats .GetFileStatistics (& repoConfig )
58+ if err != nil {
59+ pterm .Error .Printf ("Failed to get file statistics: %v\n " , err )
60+ os .Exit (1 )
61+ }
5762
58- // Get file statistics before fetching changes
59- fileStats , err := stats .GetFileStatistics (& repoConfig )
60- if err != nil {
61- pterm .Error .Printf ("Failed to get file statistics: %v\n " , err )
62- os .Exit (1 )
63- }
63+ // Display header
64+ pterm .DefaultHeader .WithFullWidth ().
65+ WithBackgroundStyle (pterm .NewStyle (pterm .BgDarkGray )).
66+ WithTextStyle (pterm .NewStyle (pterm .FgLightWhite )).
67+ Println ("Commit Message Generator" )
6468
65- // Display header
66- pterm .DefaultHeader .WithFullWidth ().
67- WithBackgroundStyle (pterm .NewStyle (pterm .BgDarkGray )).
68- WithTextStyle (pterm .NewStyle (pterm .FgLightWhite )).
69- Println ("Commit Message Generator" )
69+ pterm .Println ()
7070
71- pterm .Println ()
71+ // Display file statistics with icons
72+ display .ShowFileStatistics (fileStats )
7273
73- // Display file statistics with icons
74- display .ShowFileStatistics (fileStats )
74+ if fileStats .TotalFiles == 0 {
75+ pterm .Warning .Println ("No changes detected in the Git repository." )
76+ pterm .Info .Println ("Tips:" )
77+ pterm .Info .Println (" - Stage your changes with: git add ." )
78+ pterm .Info .Println (" - Check repository status with: git status" )
79+ pterm .Info .Println (" - Make sure you're in the correct Git repository" )
80+ return
81+ }
7582
76- if fileStats .TotalFiles == 0 {
77- pterm .Warning .Println ("No changes detected in the Git repository." )
78- pterm .Info .Println ("Tips:" )
79- pterm .Info .Println (" - Stage your changes with: git add ." )
80- pterm .Info .Println (" - Check repository status with: git status" )
81- pterm .Info .Println (" - Make sure you're in the correct Git repository" )
82- return
83- }
83+ // Get the changes
84+ changes , err := git .GetChanges (& repoConfig )
85+ if err != nil {
86+ pterm .Error .Printf ("Failed to get Git changes: %v\n " , err )
87+ os .Exit (1 )
88+ }
8489
85- // Get the changes
86- changes , err := git .GetChanges (& repoConfig )
87- if err != nil {
88- pterm .Error .Printf ("Failed to get Git changes: %v\n " , err )
89- os .Exit (1 )
90- }
90+ if len (changes ) == 0 {
91+ pterm .Warning .Println ("No changes detected in the Git repository." )
92+ pterm .Info .Println ("Tips:" )
93+ pterm .Info .Println (" - Stage your changes with: git add ." )
94+ pterm .Info .Println (" - Check repository status with: git status" )
95+ pterm .Info .Println (" - Make sure you're in the correct Git repository" )
96+ return
97+ }
9198
92- if len (changes ) == 0 {
93- pterm .Warning .Println ("No changes detected in the Git repository." )
94- pterm .Info .Println ("Tips:" )
95- pterm .Info .Println (" - Stage your changes with: git add ." )
96- pterm .Info .Println (" - Check repository status with: git status" )
97- pterm .Info .Println (" - Make sure you're in the correct Git repository" )
98- return
99- }
99+ pterm .Println ()
100+
101+ // Show generating spinner
102+ spinnerGenerating , err := pterm .DefaultSpinner .
103+ WithSequence ("⠋" , "⠙" , "⠹" , "⠸" , "⠼" , "⠴" , "⠦" , "⠧" , "⠇" , "⠏" ).
104+ Start ("Generating commit message with " + commitLLM .String () + "..." )
105+ if err != nil {
106+ pterm .Error .Printf ("Failed to start spinner: %v\n " , err )
107+ os .Exit (1 )
108+ }
100109
101- pterm . Println ()
110+ var commitMsg string
102111
103- // Show generating spinner
104- spinnerGenerating , err := pterm .DefaultSpinner .
105- WithSequence ("⠋" , "⠙" , "⠹" , "⠸" , "⠼" , "⠴" , "⠦" , "⠧" , "⠇" , "⠏" ).
106- Start ("Generating commit message with " + commitLLM + "..." )
107- if err != nil {
108- pterm .Error .Printf ("Failed to start spinner: %v\n " , err )
109- os .Exit (1 )
110- }
112+ switch commitLLM {
113+
114+ case types .ProviderGemini :
115+ commitMsg , err = gemini .GenerateCommitMessage (config , changes , apiKey )
116+
117+ case types .ProviderOpenAI :
118+ commitMsg , err = chatgpt .GenerateCommitMessage (config , changes , apiKey )
111119
112- var commitMsg string
120+ case types .ProviderClaude :
121+ commitMsg , err = claude .GenerateCommitMessage (config , changes , apiKey )
122+ case types .ProviderGroq :
123+ commitMsg , err = groq .GenerateCommitMessage (config , changes , apiKey )
124+ case types .ProviderOllama :
125+ model := "llama3:latest"
113126
127+ commitMsg , err = ollama .GenerateCommitMessage (config , changes , apiKey , model )
128+ default :
129+ commitMsg , err = grok .GenerateCommitMessage (config , changes , apiKey )
130+ }
131+
132+ if err != nil {
133+ spinnerGenerating .Fail ("Failed to generate commit message" )
114134 switch commitLLM {
115-
116- case "Gemini" :
117- commitMsg , err = gemini .GenerateCommitMessage (config , changes , apiKey )
118-
119- case "OpenAI" :
120- commitMsg , err = chatgpt .GenerateCommitMessage (config , changes , apiKey )
121-
122- case "Claude" :
123- commitMsg , err = claude .GenerateCommitMessage (config , changes , apiKey )
124- case "Groq" :
125- commitMsg , err = groq .GenerateCommitMessage (config , changes , apiKey )
126- case "Ollama" :
127- model := "llama3:latest"
128-
129- commitMsg , err = ollama .GenerateCommitMessage (config , changes , apiKey , model )
135+ case types .ProviderGemini :
136+ pterm .Error .Printf ("Gemini API error. Check your GEMINI_API_KEY environment variable or run: commit llm setup\n " )
137+ case types .ProviderOpenAI :
138+ pterm .Error .Printf ("OpenAI API error. Check your OPENAI_API_KEY environment variable or run: commit llm setup\n " )
139+ case types .ProviderClaude :
140+ pterm .Error .Printf ("Claude API error. Check your CLAUDE_API_KEY environment variable or run: commit llm setup\n " )
141+ case types .ProviderGroq :
142+ pterm .Error .Printf ("Groq API error. Check your GROQ_API_KEY environment variable or run: commit llm setup\n " )
143+ case types .ProviderGrok :
144+ pterm .Error .Printf ("Grok API error. Check your GROK_API_KEY environment variable or run: commit llm setup\n " )
130145 default :
131- commitMsg , err = grok .GenerateCommitMessage (config , changes , apiKey )
132- }
133-
134-
135- if err != nil {
136- spinnerGenerating .Fail ("Failed to generate commit message" )
137- switch commitLLM {
138- case "Gemini" :
139- pterm .Error .Printf ("Gemini API error. Check your GEMINI_API_KEY environment variable or run: commit llm setup\n " )
140- case "OpenAI" :
141- pterm .Error .Printf ("OpenAI API error. Check your OPENAI_API_KEY environment variable or run: commit llm setup\n " )
142- case "Claude" :
143- pterm .Error .Printf ("Claude API error. Check your CLAUDE_API_KEY environment variable or run: commit llm setup\n " )
144- case "Groq" :
145- pterm .Error .Printf ("Groq API error. Check your GROQ_API_KEY environment variable or run: commit llm setup\n " )
146- case "Grok" :
147- pterm .Error .Printf ("Grok API error. Check your GROK_API_KEY environment variable or run: commit llm setup\n " )
148- default :
149- pterm .Error .Printf ("LLM API error: %v\n " , err )
150- }
151- os .Exit (1 )
146+ pterm .Error .Printf ("LLM API error: %v\n " , err )
152147 }
148+ os .Exit (1 )
149+ }
153150
154- spinnerGenerating .Success ("Commit message generated successfully!" )
151+ spinnerGenerating .Success ("Commit message generated successfully!" )
155152
156- pterm .Println ()
153+ pterm .Println ()
157154
158- // Display the commit message in a styled panel
159- display .ShowCommitMessage (commitMsg )
155+ // Display the commit message in a styled panel
156+ display .ShowCommitMessage (commitMsg )
160157
161- // Copy to clipboard
162- err = clipboard .WriteAll (commitMsg )
163- if err != nil {
164- pterm .Warning .Printf ("Could not copy to clipboard: %v\n " , err )
165- } else {
166- pterm .Success .Println ("Commit message copied to clipboard!" )
167- }
158+ // Copy to clipboard
159+ err = clipboard .WriteAll (commitMsg )
160+ if err != nil {
161+ pterm .Warning .Printf ("Could not copy to clipboard: %v\n " , err )
162+ } else {
163+ pterm .Success .Println ("Commit message copied to clipboard!" )
164+ }
168165
169- pterm .Println ()
166+ pterm .Println ()
170167
171- // Display changes preview
172- display .ShowChangesPreview (fileStats )
168+ // Display changes preview
169+ display .ShowChangesPreview (fileStats )
173170
174- }
171+ }
0 commit comments