From 3a255a3ad92a7d20acd77a312283fa49004486c0 Mon Sep 17 00:00:00 2001 From: Phantas Weng Date: Tue, 8 Jul 2025 05:15:46 +0000 Subject: [PATCH 1/3] feat(config): add OCO_HOOK_AUTO_UNCOMMENT config key and update commit message hook behavior to conditionally uncomment the message --- src/commands/config.ts | 20 ++++++++++++++++++-- src/commands/prepare-commit-msg-hook.ts | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/commands/config.ts b/src/commands/config.ts index 4af9f52d..c5d01de0 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -27,7 +27,8 @@ export enum CONFIG_KEYS { OCO_API_URL = 'OCO_API_URL', OCO_API_CUSTOM_HEADERS = 'OCO_API_CUSTOM_HEADERS', OCO_OMIT_SCOPE = 'OCO_OMIT_SCOPE', - OCO_GITPUSH = 'OCO_GITPUSH' // todo: deprecate + OCO_GITPUSH = 'OCO_GITPUSH', // todo: deprecate + OCO_HOOK_AUTO_UNCOMMENT = 'OCO_HOOK_AUTO_UNCOMMENT' } export enum CONFIG_MODES { @@ -711,6 +712,14 @@ export const configValidators = { 'Must be true or false' ); return value; + }, + + [CONFIG_KEYS.OCO_HOOK_AUTO_UNCOMMENT](value: any) { + validateConfig( + CONFIG_KEYS.OCO_HOOK_AUTO_UNCOMMENT, + typeof value === 'boolean', + 'Must be true or false' + ); } }; @@ -747,6 +756,7 @@ export type ConfigType = { [CONFIG_KEYS.OCO_ONE_LINE_COMMIT]: boolean; [CONFIG_KEYS.OCO_OMIT_SCOPE]: boolean; [CONFIG_KEYS.OCO_TEST_MOCK_TYPE]: string; + [CONFIG_KEYS.OCO_HOOK_AUTO_UNCOMMENT]: boolean; }; export const defaultConfigPath = pathJoin(homedir(), '.opencommit'); @@ -794,7 +804,8 @@ export const DEFAULT_CONFIG = { OCO_TEST_MOCK_TYPE: 'commit-message', OCO_WHY: false, OCO_OMIT_SCOPE: false, - OCO_GITPUSH: true // todo: deprecate + OCO_GITPUSH: true, // todo: deprecate + OCO_HOOK_AUTO_UNCOMMENT: false }; const initGlobalConfig = (configPath: string = defaultConfigPath) => { @@ -1046,6 +1057,11 @@ function getConfigKeyDetails(key) { description: 'Message template placeholder', values: ['String (must start with $)'] }; + case CONFIG_KEYS.OCO_HOOK_AUTO_UNCOMMENT: + return { + description: 'Automatically uncomment the commit message in the hook', + values: ['true', 'false'] + }; default: return { description: 'String value', diff --git a/src/commands/prepare-commit-msg-hook.ts b/src/commands/prepare-commit-msg-hook.ts index 36e3f6d5..0174ea5d 100644 --- a/src/commands/prepare-commit-msg-hook.ts +++ b/src/commands/prepare-commit-msg-hook.ts @@ -60,7 +60,7 @@ export const prepareCommitMessageHook = async ( await fs.writeFile( messageFilePath, - `# ${commitMessage}\n\n${divider}\n# Remove the # above to use this generated commit message.\n# To cancel the commit, just close this window without making any changes.\n\n${fileContent.toString()}` + `${config.OCO_HOOK_AUTO_UNCOMMENT ? '' : '# '}${commitMessage}\n\n${divider}\n# Remove the # above to use this generated commit message.\n# To cancel the commit, just close this window without making any changes.\n\n${fileContent.toString()}` ); } catch (error) { outro(`${chalk.red('✖')} ${error}`); From 881f07eebee99073d6bd3e6226213954141c3458 Mon Sep 17 00:00:00 2001 From: Phantas Weng Date: Tue, 8 Jul 2025 05:31:44 +0000 Subject: [PATCH 2/3] fix(prepare-commit-msg-hook): simplify commit message generation logic for clarity and maintainability --- src/commands/prepare-commit-msg-hook.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/commands/prepare-commit-msg-hook.ts b/src/commands/prepare-commit-msg-hook.ts index 0174ea5d..776bf90a 100644 --- a/src/commands/prepare-commit-msg-hook.ts +++ b/src/commands/prepare-commit-msg-hook.ts @@ -56,12 +56,14 @@ export const prepareCommitMessageHook = async ( const fileContent = await fs.readFile(messageFilePath); - const divider = '# ---------- [OpenCommit] ---------- #'; + const messageWithComment = `# ${commitMessage}\n\n# ---------- [OpenCommit] ---------- #\n# Remove the # above to use this generated commit message.\n# To cancel the commit, just close this window without making any changes.\n\n${fileContent.toString()}`; + const messageWithoutComment = `${commitMessage}\n\n${fileContent.toString()}`; - await fs.writeFile( - messageFilePath, - `${config.OCO_HOOK_AUTO_UNCOMMENT ? '' : '# '}${commitMessage}\n\n${divider}\n# Remove the # above to use this generated commit message.\n# To cancel the commit, just close this window without making any changes.\n\n${fileContent.toString()}` - ); + const message = config.OCO_HOOK_AUTO_UNCOMMENT + ? messageWithoutComment + : messageWithComment; + + await fs.writeFile(messageFilePath, message); } catch (error) { outro(`${chalk.red('✖')} ${error}`); process.exit(1); From 24adc16adf19e017005324c1def9ca7009a63ea9 Mon Sep 17 00:00:00 2001 From: Phantas Weng Date: Tue, 8 Jul 2025 09:27:40 +0000 Subject: [PATCH 3/3] fix(run.ts): remove trailing comma from OCO_AI_PROVIDER_ENUM array to fix the prettier test --- src/migrations/_run.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/migrations/_run.ts b/src/migrations/_run.ts index 30d9fd34..989a71ac 100644 --- a/src/migrations/_run.ts +++ b/src/migrations/_run.ts @@ -43,7 +43,7 @@ export const runMigrations = async () => { OCO_AI_PROVIDER_ENUM.GROQ, OCO_AI_PROVIDER_ENUM.MISTRAL, OCO_AI_PROVIDER_ENUM.MLX, - OCO_AI_PROVIDER_ENUM.OPENROUTER, + OCO_AI_PROVIDER_ENUM.OPENROUTER ].includes(config.OCO_AI_PROVIDER) ) { return;