1+ package cmd
2+
3+ import (
4+ "log"
5+ "os"
6+
7+ "github.com/atotto/clipboard"
8+ "github.com/dfanso/commit-msg/cmd/cli/store"
9+ "github.com/dfanso/commit-msg/internal/chatgpt"
10+ "github.com/dfanso/commit-msg/internal/claude"
11+ "github.com/dfanso/commit-msg/internal/display"
12+ "github.com/dfanso/commit-msg/internal/gemini"
13+ "github.com/dfanso/commit-msg/internal/git"
14+ "github.com/dfanso/commit-msg/internal/grok"
15+ "github.com/dfanso/commit-msg/internal/stats"
16+ "github.com/dfanso/commit-msg/pkg/types"
17+ "github.com/pterm/pterm"
18+ )
19+
20+
21+ func CreateCommitMsg () {
22+
23+ // Validate COMMIT_LLM and required API keys
24+ useLLM ,err := store .DefaultLLMKey ()
25+ if err != nil {
26+ log .Fatal (err )
27+ }
28+
29+ commitLLM := useLLM .LLM
30+ apiKey := useLLM .APIKey
31+
32+
33+ // Get current directory
34+ currentDir , err := os .Getwd ()
35+ if err != nil {
36+ log .Fatalf ("Failed to get current directory: %v" , err )
37+ }
38+
39+ // Check if current directory is a git repository
40+ if ! git .IsRepository (currentDir ) {
41+ log .Fatalf ("Current directory is not a Git repository: %s" , currentDir )
42+ }
43+
44+ // Create a minimal config for the API
45+ config := & types.Config {
46+ GrokAPI : "https://api.x.ai/v1/chat/completions" ,
47+ }
48+
49+ // Create a repo config for the current directory
50+ repoConfig := types.RepoConfig {
51+ Path : currentDir ,
52+ }
53+
54+ // Get file statistics before fetching changes
55+ fileStats , err := stats .GetFileStatistics (& repoConfig )
56+ if err != nil {
57+ log .Fatalf ("Failed to get file statistics: %v" , err )
58+ }
59+
60+ // Display header
61+ pterm .DefaultHeader .WithFullWidth ().
62+ WithBackgroundStyle (pterm .NewStyle (pterm .BgDarkGray )).
63+ WithTextStyle (pterm .NewStyle (pterm .FgLightWhite )).
64+ Println ("🚀 Commit Message Generator" )
65+
66+ pterm .Println ()
67+
68+ // Display file statistics with icons
69+ display .ShowFileStatistics (fileStats )
70+
71+ if fileStats .TotalFiles == 0 {
72+ pterm .Warning .Println ("No changes detected in the Git repository." )
73+ return
74+ }
75+
76+ // Get the changes
77+ changes , err := git .GetChanges (& repoConfig )
78+ if err != nil {
79+ log .Fatalf ("Failed to get Git changes: %v" , err )
80+ }
81+
82+ if len (changes ) == 0 {
83+ pterm .Warning .Println ("No changes detected in the Git repository." )
84+ return
85+ }
86+
87+ pterm .Println ()
88+
89+ // Show generating spinner
90+ spinnerGenerating , err := pterm .DefaultSpinner .
91+ WithSequence ("⠋" , "⠙" , "⠹" , "⠸" , "⠼" , "⠴" , "⠦" , "⠧" , "⠇" , "⠏" ).
92+ Start ("🤖 Generating commit message..." )
93+ if err != nil {
94+ log .Fatalf ("Failed to start spinner: %v" , err )
95+ }
96+
97+ var commitMsg string
98+
99+ switch commitLLM {
100+
101+ case "Gemini" :
102+ commitMsg , err = gemini .GenerateCommitMessage (config , changes , apiKey )
103+
104+ case "OpenAI" :
105+ commitMsg , err = chatgpt .GenerateCommitMessage (config , changes , apiKey )
106+
107+ case "Claude" :
108+ commitMsg , err = claude .GenerateCommitMessage (config , changes , apiKey )
109+
110+ default :
111+ commitMsg , err = grok .GenerateCommitMessage (config , changes , apiKey )
112+ }
113+
114+
115+ if err != nil {
116+ spinnerGenerating .Fail ("Failed to generate commit message" )
117+ log .Fatalf ("Error: %v" , err )
118+ }
119+
120+ spinnerGenerating .Success ("✅ Commit message generated successfully!" )
121+
122+ pterm .Println ()
123+
124+ // Display the commit message in a styled panel
125+ display .ShowCommitMessage (commitMsg )
126+
127+ // Copy to clipboard
128+ err = clipboard .WriteAll (commitMsg )
129+ if err != nil {
130+ pterm .Warning .Printf ("⚠️ Could not copy to clipboard: %v\n " , err )
131+ } else {
132+ pterm .Success .Println ("📋 Commit message copied to clipboard!" )
133+ }
134+
135+ pterm .Println ()
136+
137+ // Display changes preview
138+ display .ShowChangesPreview (fileStats )
139+
140+ }
0 commit comments