Skip to content

Commit 48b8d9d

Browse files
authored
Merge pull request #494 from PhantasWeng/commit-hook-default
feat(config): add OCO_HOOK_AUTO_UNCOMMENT config key and update commit message hook behavior to conditionally uncomment the message
2 parents 9971b3c + 24adc16 commit 48b8d9d

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/commands/config.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export enum CONFIG_KEYS {
2727
OCO_API_URL = 'OCO_API_URL',
2828
OCO_API_CUSTOM_HEADERS = 'OCO_API_CUSTOM_HEADERS',
2929
OCO_OMIT_SCOPE = 'OCO_OMIT_SCOPE',
30-
OCO_GITPUSH = 'OCO_GITPUSH' // todo: deprecate
30+
OCO_GITPUSH = 'OCO_GITPUSH', // todo: deprecate
31+
OCO_HOOK_AUTO_UNCOMMENT = 'OCO_HOOK_AUTO_UNCOMMENT'
3132
}
3233

3334
export enum CONFIG_MODES {
@@ -711,6 +712,14 @@ export const configValidators = {
711712
'Must be true or false'
712713
);
713714
return value;
715+
},
716+
717+
[CONFIG_KEYS.OCO_HOOK_AUTO_UNCOMMENT](value: any) {
718+
validateConfig(
719+
CONFIG_KEYS.OCO_HOOK_AUTO_UNCOMMENT,
720+
typeof value === 'boolean',
721+
'Must be true or false'
722+
);
714723
}
715724
};
716725

@@ -747,6 +756,7 @@ export type ConfigType = {
747756
[CONFIG_KEYS.OCO_ONE_LINE_COMMIT]: boolean;
748757
[CONFIG_KEYS.OCO_OMIT_SCOPE]: boolean;
749758
[CONFIG_KEYS.OCO_TEST_MOCK_TYPE]: string;
759+
[CONFIG_KEYS.OCO_HOOK_AUTO_UNCOMMENT]: boolean;
750760
};
751761

752762
export const defaultConfigPath = pathJoin(homedir(), '.opencommit');
@@ -794,7 +804,8 @@ export const DEFAULT_CONFIG = {
794804
OCO_TEST_MOCK_TYPE: 'commit-message',
795805
OCO_WHY: false,
796806
OCO_OMIT_SCOPE: false,
797-
OCO_GITPUSH: true // todo: deprecate
807+
OCO_GITPUSH: true, // todo: deprecate
808+
OCO_HOOK_AUTO_UNCOMMENT: false
798809
};
799810

800811
const initGlobalConfig = (configPath: string = defaultConfigPath) => {
@@ -1046,6 +1057,11 @@ function getConfigKeyDetails(key) {
10461057
description: 'Message template placeholder',
10471058
values: ['String (must start with $)']
10481059
};
1060+
case CONFIG_KEYS.OCO_HOOK_AUTO_UNCOMMENT:
1061+
return {
1062+
description: 'Automatically uncomment the commit message in the hook',
1063+
values: ['true', 'false']
1064+
};
10491065
default:
10501066
return {
10511067
description: 'String value',

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ export const prepareCommitMessageHook = async (
5656

5757
const fileContent = await fs.readFile(messageFilePath);
5858

59-
const divider = '# ---------- [OpenCommit] ---------- #';
59+
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()}`;
60+
const messageWithoutComment = `${commitMessage}\n\n${fileContent.toString()}`;
6061

61-
await fs.writeFile(
62-
messageFilePath,
63-
`# ${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()}`
64-
);
62+
const message = config.OCO_HOOK_AUTO_UNCOMMENT
63+
? messageWithoutComment
64+
: messageWithComment;
65+
66+
await fs.writeFile(messageFilePath, message);
6567
} catch (error) {
6668
outro(`${chalk.red('✖')} ${error}`);
6769
process.exit(1);

src/migrations/_run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const runMigrations = async () => {
4343
OCO_AI_PROVIDER_ENUM.GROQ,
4444
OCO_AI_PROVIDER_ENUM.MISTRAL,
4545
OCO_AI_PROVIDER_ENUM.MLX,
46-
OCO_AI_PROVIDER_ENUM.OPENROUTER,
46+
OCO_AI_PROVIDER_ENUM.OPENROUTER
4747
].includes(config.OCO_AI_PROVIDER)
4848
) {
4949
return;

0 commit comments

Comments
 (0)