Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e4485cf

Browse files
committed
refactor: extract standard mode logic
Extract inline standard mode handling logic into a separate handleStandardMode function to improve code organization and follow the same pattern as handleAgentMode. - Move standard mode logic from main function to handleStandardMode - Add default value for contextLines parameter (10) - Improve separation of concerns and code readability
1 parent 7406986 commit e4485cf

File tree

1 file changed

+83
-75
lines changed

1 file changed

+83
-75
lines changed

src/commands/aicommits.ts

Lines changed: 83 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -82,81 +82,7 @@ export const aiCommits = async ({
8282
return await handleAgentMode(aiAgentService, gitService, promptUI);
8383
}
8484

85-
const detectingFiles = promptUI.spinner();
86-
detectingFiles.start('Detecting staged files');
87-
const staged = await gitService.getStagedDiff(config.exclude, config.contextLines);
88-
89-
if (!staged) {
90-
detectingFiles.stop('Detecting staged files');
91-
throw new KnownError(
92-
trimLines(`
93-
No staged changes found. Stage your changes manually, or automatically stage all changes with the \`--stage-all\` flag.
94-
`),
95-
);
96-
}
97-
98-
detectingFiles.stop(
99-
`${gitService.getDetectedMessage(staged.files)}:\n${staged.files.map((file) => ` ${file}`).join('\n')}`,
100-
);
101-
102-
const analyzeSpinner = promptUI.spinner();
103-
analyzeSpinner.start('The AI is analyzing your changes');
104-
105-
let commitMessage = '';
106-
let commitBody = '';
107-
let messageBuffer = '';
108-
109-
await aiCommitMessageService.generateStreamingCommitMessage({
110-
diff: staged.diff,
111-
onMessageUpdate: (content) => {
112-
// Update spinner message with the growing message content
113-
messageBuffer += content;
114-
const previewContent =
115-
messageBuffer.length > 50 ? messageBuffer.substring(0, 47) + '...' : messageBuffer;
116-
analyzeSpinner.message(`Generating commit message: ${previewContent}`);
117-
},
118-
onBodyUpdate: () => {
119-
// Don't show body updates in real-time
120-
},
121-
onComplete: (message, body) => {
122-
commitMessage = message;
123-
commitBody = body;
124-
},
125-
});
126-
127-
analyzeSpinner.stop('Commit message generated');
128-
129-
// Display the full message after generation
130-
promptUI.log.step('Generated commit message:');
131-
promptUI.log.message(green(commitMessage));
132-
133-
if (commitBody) {
134-
promptUI.log.step('Commit body:');
135-
promptUI.log.message(commitBody);
136-
}
137-
138-
if (!commitMessage) {
139-
throw new KnownError('No commit message was generated. Try again.');
140-
}
141-
142-
const result = await streamingReviewAndRevise({
143-
aiCommitMessageService,
144-
promptUI,
145-
message: commitMessage,
146-
body: commitBody,
147-
diff: staged.diff,
148-
});
149-
if (!result?.accepted) {
150-
return;
151-
}
152-
153-
const message = result.message ?? '';
154-
const body = result.body ?? '';
155-
156-
const fullMessage = `${message}\n\n${body}`.trim();
157-
await gitService.commitChanges(fullMessage);
158-
159-
promptUI.outro(`${green('✔')} Successfully committed`);
85+
return await handleStandardMode(aiCommitMessageService, gitService, promptUI, config);
16086
} catch (error) {
16187
if (isError(error)) {
16288
promptUI.outro(`${red('✖')} ${error.message}`);
@@ -221,3 +147,85 @@ async function handleAgentMode(
221147
throw error;
222148
}
223149
}
150+
151+
const handleStandardMode = async (
152+
aiCommitMessageService: AICommitMessageService,
153+
gitService: GitService,
154+
promptUI: ClackPromptService,
155+
config: { exclude?: string[]; contextLines?: number },
156+
): Promise<void> => {
157+
const detectingFiles = promptUI.spinner();
158+
detectingFiles.start('Detecting staged files');
159+
const staged = await gitService.getStagedDiff(config.exclude, config.contextLines ?? 10);
160+
161+
if (!staged) {
162+
detectingFiles.stop('Detecting staged files');
163+
throw new KnownError(
164+
trimLines(`
165+
No staged changes found. Stage your changes manually, or automatically stage all changes with the \`--stage-all\` flag.
166+
`),
167+
);
168+
}
169+
170+
detectingFiles.stop(
171+
`${gitService.getDetectedMessage(staged.files)}:\n${staged.files.map((file) => ` ${file}`).join('\n')}`,
172+
);
173+
174+
const analyzeSpinner = promptUI.spinner();
175+
analyzeSpinner.start('The AI is analyzing your changes');
176+
177+
let commitMessage = '';
178+
let commitBody = '';
179+
let messageBuffer = '';
180+
181+
await aiCommitMessageService.generateStreamingCommitMessage({
182+
diff: staged.diff,
183+
onMessageUpdate: (content) => {
184+
// Update spinner message with the growing message content
185+
messageBuffer += content;
186+
const previewContent = messageBuffer.length > 50 ? messageBuffer.substring(0, 47) + '...' : messageBuffer;
187+
analyzeSpinner.message(`Generating commit message: ${previewContent}`);
188+
},
189+
onBodyUpdate: () => {
190+
// Don't show body updates in real-time
191+
},
192+
onComplete: (message, body) => {
193+
commitMessage = message;
194+
commitBody = body;
195+
},
196+
});
197+
198+
analyzeSpinner.stop('Commit message generated');
199+
200+
// Display the full message after generation
201+
promptUI.log.step('Generated commit message:');
202+
promptUI.log.message(green(commitMessage));
203+
204+
if (commitBody) {
205+
promptUI.log.step('Commit body:');
206+
promptUI.log.message(commitBody);
207+
}
208+
209+
if (!commitMessage) {
210+
throw new KnownError('No commit message was generated. Try again.');
211+
}
212+
213+
const result = await streamingReviewAndRevise({
214+
aiCommitMessageService,
215+
promptUI,
216+
message: commitMessage,
217+
body: commitBody,
218+
diff: staged.diff,
219+
});
220+
if (!result?.accepted) {
221+
return;
222+
}
223+
224+
const message = result.message ?? '';
225+
const body = result.body ?? '';
226+
227+
const fullMessage = `${message}\n\n${body}`.trim();
228+
await gitService.commitChanges(fullMessage);
229+
230+
promptUI.outro(`${green('✔')} Successfully committed`);
231+
};

0 commit comments

Comments
 (0)