Skip to content

Commit e684baf

Browse files
refactor: add use-as-is flag to lazycommit and update prepare-commit-msg hook
1 parent 4ab7f96 commit e684baf

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ For large commits with many files, lazycommit automatically stays within API lim
138138

139139
### Git hook
140140

141-
You can also integrate _lazycommit_ with Git via the [`prepare-commit-msg`](https://git-scm.com/docs/githooks#_prepare_commit_msg) hook. This lets you use Git like you normally would, and edit the commit message before committing.
141+
You can also integrate _lazycommit_ with Git via the [`prepare-commit-msg`](https://git-scm.com/docs/githooks#_prepare_commit_msg) hook. This lets you use Git like you normally would, and edit the commit message before committing. The hook uses the same enhanced analysis and quality improvements as the CLI mode.
142142

143143
#### Install
144144

@@ -167,7 +167,7 @@ lazycommit hook uninstall
167167

168168
> If you ever want to write your own message instead of generating one, you can simply pass one in: `git commit -m "My message"`
169169
170-
2. Lazycommit will generate the commit message for you and pass it back to Git. Git will open it with the [configured editor](https://docs.github.com/en/get-started/getting-started-with-git/associating-text-editors-with-git) for you to review/edit it.
170+
2. Lazycommit will generate a high-quality commit message using the same enhanced analysis as the CLI mode and pass it back to Git. Git will open it with the [configured editor](https://docs.github.com/en/get-started/getting-started-with-git/associating-text-editors-with-git) for you to review/edit it.
171171

172172
3. Save and close the editor to commit!
173173

src/commands/lazycommit.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const buildDiffSnippets = async (
6666
}
6767
};
6868

69-
const buildSingleCommitPrompt = async (
69+
export const buildSingleCommitPrompt = async (
7070
files: string[],
7171
compactSummary: string,
7272
maxLength: number
@@ -238,6 +238,7 @@ export default async (
238238

239239
let message: string;
240240
let editedAlready = false;
241+
let useAsIs = false;
241242
if (messages.length === 1) {
242243
[message] = messages;
243244
const choice = await select({
@@ -255,7 +256,7 @@ export default async (
255256
}
256257

257258
if (choice === 'use') {
258-
editedAlready = true;
259+
useAsIs = true;
259260
} else if (choice === 'edit') {
260261
const edited = await text({
261262
message: 'Edit commit message:',
@@ -281,12 +282,11 @@ export default async (
281282
}
282283

283284
message = selected as string;
284-
// User selected a message, no need for further editing
285-
editedAlready = true;
285+
useAsIs = true;
286286
}
287287

288-
// Offer editing of the final commit message (skip if already edited)
289-
if (!editedAlready) {
288+
// Offer editing of the final commit message (skip if user chose 'Use as-is' or already edited)
289+
if (!useAsIs && !editedAlready) {
290290
const wantsEdit = await confirm({ message: 'Edit the commit message before committing?' });
291291
if (wantsEdit && !isCancel(wantsEdit)) {
292292
const edited = await text({
@@ -299,16 +299,19 @@ export default async (
299299
return;
300300
}
301301
message = String(edited).trim();
302+
editedAlready = true;
302303
}
303304
}
304305

305-
// Final proceed confirmation displaying the message
306-
const proceed = await confirm({
307-
message: `Proceed with this commit message?\n\n ${message}\n`,
308-
});
309-
if (!proceed || isCancel(proceed)) {
310-
outro('Commit cancelled');
311-
return;
306+
// Final proceed confirmation displaying the message (skip if user chose 'Use as-is')
307+
if (!useAsIs) {
308+
const proceed = await confirm({
309+
message: `Proceed with this commit message?\n\n ${message}\n`,
310+
});
311+
if (!proceed || isCancel(proceed)) {
312+
outro('Commit cancelled');
313+
return;
314+
}
312315
}
313316

314317
await execa('git', ['commit', '-m', message, ...rawArgv]);

src/commands/prepare-commit-msg-hook.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs/promises';
22
import { intro, outro, spinner } from '@clack/prompts';
33
import { black, green, red, bgCyan } from 'kolorist';
44
import { getStagedDiff, buildCompactSummary } from '../utils/git.js';
5+
import { buildSingleCommitPrompt } from './lazycommit.js';
56
import { getConfig } from '../utils/config.js';
67
import { generateCommitMessageFromSummary } from '../utils/groq.js';
78
import { KnownError, handleCliError } from '../utils/error.js';
@@ -42,11 +43,12 @@ export default () =>
4243
try {
4344
const compact = await buildCompactSummary();
4445
if (compact) {
46+
const enhanced = await buildSingleCommitPrompt(staged.files, compact, config['max-length']);
4547
messages = await generateCommitMessageFromSummary(
4648
config.GROQ_API_KEY,
4749
config.model,
4850
config.locale,
49-
compact,
51+
enhanced,
5052
config.generate,
5153
config['max-length'],
5254
config.type,
@@ -56,7 +58,7 @@ export default () =>
5658
} else {
5759
// Fallback to simple file list if summary fails
5860
const fileList = staged!.files.join(', ');
59-
const fallbackPrompt = `Generate a commit message for these files: ${fileList}`;
61+
const fallbackPrompt = await buildSingleCommitPrompt(staged.files, `Files: ${fileList}`, config['max-length']);
6062
messages = await generateCommitMessageFromSummary(
6163
config.GROQ_API_KEY,
6264
config.model,

0 commit comments

Comments
 (0)