Skip to content

Commit 3c0a271

Browse files
authoredJul 5, 2023
feat(config.ts): add OCO_MESSAGE_TEMPLATE_PLACEHOLDER configuration item to allow users to customize the message template placeholder (#208)
feat(commit.ts): add check for message templates in extraArgs and replace OCO_MESSAGE_TEMPLATE_PLACEHOLDER with generated commit message if found docs(README.md): add documentation for OCO_MESSAGE_TEMPLATE_PLACEHOLDER configuration item and how to use it in the command line (#205)
1 parent ccfd24a commit 3c0a271

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed
 

‎README.md

+9
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ OCO_DESCRIPTION=<postface a message with ~3 sentences description>
124124
OCO_EMOJI=<add GitMoji>
125125
OCO_MODEL=<either gpt-3.5-turbo or gpt-4>
126126
OCO_LANGUAGE=<locale, scroll to the bottom to see options>
127+
OCO_MESSAGE_TEMPLATE_PLACEHOLDER=<message template placeholder, example: '$msg'>
127128
```
128129

129130
### Global config for all repos
@@ -193,6 +194,14 @@ is translated to :
193194
git commit -m "${generatedMessage}" --no-verify
194195
```
195196

197+
To include a message in the generated message, you can utilize the template function! For instance:
198+
199+
```sh
200+
oco '$msg #205’
201+
```
202+
203+
> opencommit examines placeholders in the parameters, allowing you to append additional information before and after the placeholders, such as the relevant Issue or Pull Request. Similarly, you have the option to customize the OCO_MESSAGE_TEMPLATE_PLACEHOLDER configuration item, for example, simplifying it to $m!"
204+
196205
### Ignore files
197206

198207
You can ignore files from submission to OpenAI by creating a `.opencommitignore` file. For example:

‎src/commands/commit.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,42 @@ import {
1818
multiselect,
1919
select
2020
} from '@clack/prompts';
21+
import {
22+
getConfig
23+
} from '../commands/config';
2124
import chalk from 'chalk';
2225
import { trytm } from '../utils/trytm';
2326

27+
const config = getConfig();
28+
2429
const getGitRemotes = async () => {
2530
const { stdout } = await execa('git', ['remote']);
2631
return stdout.split('\n').filter((remote) => Boolean(remote.trim()));
2732
};
2833

34+
// Check for the presence of message templates
35+
const checkMessageTemplate = (extraArgs: string[]): string | false => {
36+
for(const key in extraArgs){
37+
if(extraArgs[key].includes(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER)) return extraArgs[key];
38+
}
39+
return false;
40+
};
41+
2942
const generateCommitMessageFromGitDiff = async (
3043
diff: string,
3144
extraArgs: string[]
3245
): Promise<void> => {
46+
const messageTemplate = checkMessageTemplate(extraArgs);
3347
await assertGitRepo();
3448

3549
const commitSpinner = spinner();
3650
commitSpinner.start('Generating the commit message');
3751
try {
38-
const commitMessage = await generateCommitMessageByDiff(diff);
52+
let commitMessage = await generateCommitMessageByDiff(diff);
3953

54+
if(typeof messageTemplate === 'string'){
55+
commitMessage = messageTemplate.replace(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER, commitMessage);
56+
}
4057
commitSpinner.stop('📝 Commit message generated');
4158

4259
outro(

‎src/commands/config.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export enum CONFIG_KEYS {
1919
OCO_DESCRIPTION = 'OCO_DESCRIPTION',
2020
OCO_EMOJI = 'OCO_EMOJI',
2121
OCO_MODEL = 'OCO_MODEL',
22-
OCO_LANGUAGE = 'OCO_LANGUAGE'
22+
OCO_LANGUAGE = 'OCO_LANGUAGE',
23+
OCO_MESSAGE_TEMPLATE_PLACEHOLDER = 'OCO_MESSAGE_TEMPLATE_PLACEHOLDER',
2324
}
2425

2526
export const DEFAULT_MODEL_TOKEN_LIMIT = 4096;
@@ -124,6 +125,14 @@ export const configValidators = {
124125
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
125126
);
126127
return value;
128+
},
129+
[CONFIG_KEYS.OCO_MESSAGE_TEMPLATE_PLACEHOLDER](value: any) {
130+
validateConfig(
131+
CONFIG_KEYS.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
132+
value.startsWith('$'),
133+
`${value} must start with $, for example: '$msg'`
134+
);
135+
return value;
127136
}
128137
};
129138

@@ -141,7 +150,8 @@ export const getConfig = (): ConfigType | null => {
141150
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false,
142151
OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false,
143152
OCO_MODEL: process.env.OCO_MODEL || 'gpt-3.5-turbo',
144-
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en'
153+
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en',
154+
OCO_MESSAGE_TEMPLATE_PLACEHOLDER: process.env.OCO_MESSAGE_TEMPLATE_PLACEHOLDER || '$msg'
145155
};
146156

147157
const configExists = existsSync(configPath);

0 commit comments

Comments
 (0)
Failed to load comments.