Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/commands/prepare-commit-msg-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ export const prepareCommitMessageHook = async (
const spin = spinner();
spin.start('Generating commit message');

const commitMessage = await generateCommitMessageByDiff(
await getDiff({ files: staged })
);
let commitMessage: string;
try {
commitMessage = await generateCommitMessageByDiff(
await getDiff({ files: staged })
);
} catch (error) {
spin.stop('Done');
throw error;
}

spin.stop('Done');

const fileContent = await fs.readFile(messageFilePath);
Expand All @@ -65,7 +72,17 @@ export const prepareCommitMessageHook = async (

await fs.writeFile(messageFilePath, message);
} catch (error) {
outro(`${chalk.red('✖')} ${error}`);
process.exit(1);
try {
outro(`${chalk.red('✖')} ${error}`);
const fileContent = await fs.readFile(messageFilePath);

const commentedError = String(error).replace(new RegExp('^', 'gm'), '# ');
const message = `\n\n# ---------- [OpenCommit] ---------- #\n# Failed to generate the commit message.\n# To cancel the commit, just close this window without making any changes.\n\n${commentedError}\n\n${fileContent.toString()}`

await fs.writeFile(messageFilePath, message);
} catch (error) {
outro(`${chalk.red('✖')} ${error}`);
process.exit(1);
}
}
};
6 changes: 1 addition & 5 deletions src/generateCommitMessageFromGitDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ export const generateCommitMessageByDiff = async (
context
);

const commitMessages = [] as string[];
for (const promise of commitMessagePromises) {
commitMessages.push((await promise) as string);
await delay(2000);
}
const commitMessages = await Promise.all(commitMessagePromises);

return commitMessages.join('\n\n');
}
Expand Down
Loading