@@ -48,48 +48,79 @@ class GitSmart {
4848 }
4949
5050 async validateEnvironment ( ) {
51+ // Check if we're in a git repository
5152 if ( ! GitUtils . isGitRepository ( ) ) {
5253 throw new Error ( 'Not a git repository. Please run this command from within a git repository.' )
5354 }
55+
56+ // Check for staged changes
5457 if ( ! GitUtils . hasStagedChanges ( ) ) {
5558 throw new Error ( 'No staged changes found. Please stage your changes with "git add" first.' )
5659 }
57- // Verbose logging removed for linting compliance
60+
61+ // Additional validation: check if git is accessible
62+ try {
63+ GitUtils . getStagedFiles ( )
64+ } catch ( error ) {
65+ throw new Error ( `Git repository validation failed: ${ error . message } ` )
66+ }
5867 }
5968
6069 async analyzeChanges ( ) {
61- // Verbose logging removed for linting compliance
62- const diff = GitUtils . getStagedDiff ( )
63- const stagedFiles = GitUtils . getStagedFiles ( )
64- const stats = GitUtils . getDiffStats ( )
65- const analysis = this . diffAnalyzer . analyze ( diff , stagedFiles )
66- analysis . stats = stats
67- // Verbose logging removed for linting compliance
68- return analysis
70+ try {
71+ const diff = GitUtils . getStagedDiff ( )
72+ const stagedFiles = GitUtils . getStagedFiles ( )
73+ const stats = GitUtils . getDiffStats ( )
74+
75+ // Validate that we have some changes to analyze
76+ if ( ! diff && stagedFiles . length === 0 ) {
77+ throw new Error ( 'No changes detected to analyze' )
78+ }
79+
80+ const analysis = this . diffAnalyzer . analyze ( diff , stagedFiles )
81+ analysis . stats = stats
82+
83+ return analysis
84+ } catch ( error ) {
85+ throw new Error ( `Failed to analyze changes: ${ error . message } ` )
86+ }
6987 }
7088
7189 async getStyleGuide ( ) {
72- // Verbose logging removed for linting compliance
73- const commits = GitUtils . getRecentCommits ( 50 )
74- const historyAnalysis = this . historyAnalyzer . analyzeCommitHistory ( commits )
75-
76- // v1.1.0: Use enhanced style guide generation
77- const styleGuide = this . options . enhanced
78- ? this . historyAnalyzer . generateEnhancedStyleGuide ( historyAnalysis )
79- : this . historyAnalyzer . generateStyleGuide ( historyAnalysis )
80-
81- // Verbose logging removed for linting compliance
82- // Add the adaptation method
83- styleGuide . adaptMessageToStyle = ( message , guide ) => {
84- return this . historyAnalyzer . adaptMessageToStyle ( message , guide )
85- }
86-
87- // v1.1.0: Store enhanced analysis for potential CLI options
88- if ( this . options . enhanced ) {
89- styleGuide . enhancedAnalysis = historyAnalysis
90+ try {
91+ const commits = GitUtils . getRecentCommits ( 50 )
92+
93+ // Validate commit history
94+ if ( ! Array . isArray ( commits ) ) {
95+ throw new Error ( 'Invalid commit history format' )
96+ }
97+
98+ const historyAnalysis = this . historyAnalyzer . analyzeCommitHistory ( commits )
99+
100+ // v1.1.0: Use enhanced style guide generation
101+ const styleGuide = this . options . enhanced
102+ ? this . historyAnalyzer . generateEnhancedStyleGuide ( historyAnalysis )
103+ : this . historyAnalyzer . generateStyleGuide ( historyAnalysis )
104+
105+ // Validate style guide
106+ if ( ! styleGuide || typeof styleGuide !== 'object' ) {
107+ throw new Error ( 'Failed to generate style guide' )
108+ }
109+
110+ // Add the adaptation method
111+ styleGuide . adaptMessageToStyle = ( message , guide ) => {
112+ return this . historyAnalyzer . adaptMessageToStyle ( message , guide )
113+ }
114+
115+ // v1.1.0: Store enhanced analysis for potential CLI options
116+ if ( this . options . enhanced ) {
117+ styleGuide . enhancedAnalysis = historyAnalysis
118+ }
119+
120+ return styleGuide
121+ } catch ( error ) {
122+ throw new Error ( `Failed to analyze commit history: ${ error . message } ` )
90123 }
91-
92- return styleGuide
93124 }
94125
95126 generateCommitMessages ( analysis , styleGuide ) {
@@ -136,23 +167,44 @@ class GitSmart {
136167 }
137168
138169 async executeCommit ( message ) {
139- if ( ! ( message ) ) {
140- // Info message removed for linting compliance
170+ if ( ! message ) {
141171 return
142172 }
173+
143174 if ( this . options . dryRun ) {
144- // Dry run output removed for linting compliance
145175 return
146176 }
177+
178+ // Additional validation before committing
179+ if ( typeof message !== 'string' || message . trim ( ) . length === 0 ) {
180+ throw new Error ( 'Invalid commit message: must be a non-empty string' )
181+ }
182+
183+ // Check message length (git has practical limits)
184+ if ( message . length > 3600 ) {
185+ throw new Error ( 'Commit message too long (maximum 3600 characters)' )
186+ }
187+
147188 try {
148- GitUtils . commit ( message )
189+ const result = GitUtils . commit ( message . trim ( ) )
190+
149191 if ( this . interactive ) {
150192 this . interactive . displaySuccess ( `Committed with message: "${ message } "` )
151- } else {
152- // Success message handled by interactive prompt
153193 }
154- // Verbose logging removed for linting compliance
194+
195+ return result
155196 } catch ( error ) {
197+ // Provide more helpful error messages for common issues
198+ if ( error . message . includes ( 'nothing to commit' ) ) {
199+ throw new Error ( 'No changes to commit. Please stage your changes first.' )
200+ }
201+ if ( error . message . includes ( 'not a git repository' ) ) {
202+ throw new Error ( 'Not in a git repository. Please run from within a git repository.' )
203+ }
204+ if ( error . message . includes ( 'Author identity unknown' ) ) {
205+ throw new Error ( 'Git user not configured. Please set user.name and user.email with git config.' )
206+ }
207+
156208 throw new Error ( `Failed to commit: ${ error . message } ` )
157209 }
158210 }
0 commit comments