fix: skip migrations and version check when called as git hook

Move isHookCalled() check before runMigrations() and
checkIsLatestVersion() so that during git rebase, each pick commit
exits immediately without expensive I/O and network calls.

Also adds missing await on prepareCommitMessageHook() to properly
handle async errors.

Closes #493

Signed-off-by: majiayu000 <1835304752@qq.com>
This commit is contained in:
majiayu000
2026-03-21 10:55:47 +08:00
parent 40182f26b3
commit bc608e97bd
3 changed files with 47 additions and 34 deletions

View File

@@ -24,7 +24,13 @@ cli(
{
version: packageJSON.version,
name: 'opencommit',
commands: [configCommand, hookCommand, commitlintConfigCommand, setupCommand, modelsCommand],
commands: [
configCommand,
hookCommand,
commitlintConfigCommand,
setupCommand,
modelsCommand
],
flags: {
fgm: {
type: Boolean,
@@ -48,28 +54,29 @@ cli(
help: { description: packageJSON.description }
},
async ({ flags }) => {
if (await isHookCalled()) {
await prepareCommitMessageHook();
return;
}
await runMigrations();
await checkIsLatestVersion();
if (await isHookCalled()) {
prepareCommitMessageHook();
} else {
// Check for first run and trigger setup wizard
if (isFirstRun()) {
const setupComplete = await runSetup();
if (!setupComplete) {
process.exit(1);
}
}
// Check for missing API key and prompt if needed
const hasApiKey = await promptForMissingApiKey();
if (!hasApiKey) {
// Check for first run and trigger setup wizard
if (isFirstRun()) {
const setupComplete = await runSetup();
if (!setupComplete) {
process.exit(1);
}
commit(extraArgs, flags.context, false, flags.fgm, flags.yes);
}
// Check for missing API key and prompt if needed
const hasApiKey = await promptForMissingApiKey();
if (!hasApiKey) {
process.exit(1);
}
commit(extraArgs, flags.context, false, flags.fgm, flags.yes);
},
extraArgs
);