mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-04-20 03:02:51 -04:00
Merge pull request #549 from SOV710/fix-cli-flags
fix: make -c/--context, -y/--yes, and --fgm flags actually work
This commit is contained in:
31
src/cli.ts
31
src/cli.ts
@@ -22,7 +22,34 @@ import { runMigrations } from './migrations/_run.js';
|
||||
const config = getConfig();
|
||||
setupProxy(config.OCO_PROXY);
|
||||
|
||||
const extraArgs = process.argv.slice(2);
|
||||
const OCO_FLAGS_WITH_VALUE = new Set(['-c', '--context']);
|
||||
const OCO_BOOLEAN_FLAGS = new Set(['-y', '--yes', '--fgm']);
|
||||
const OCO_EQUALS_PREFIXES = ['-c=', '--context=', '-y=', '--yes=', '--fgm='];
|
||||
|
||||
const stripOcoFlags = (argv: string[]): string[] => {
|
||||
const out: string[] = [];
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
// String flags with a separate value token: -c <val>, --context <val>
|
||||
if (OCO_FLAGS_WITH_VALUE.has(a)) {
|
||||
i++; // skip the value token too
|
||||
continue;
|
||||
}
|
||||
// Boolean flags: -y, --yes, --fgm
|
||||
if (OCO_BOOLEAN_FLAGS.has(a)) {
|
||||
continue;
|
||||
}
|
||||
// Equals form: -c=…, --context=…, -y=…, --yes=…, --fgm=…
|
||||
if (OCO_EQUALS_PREFIXES.some((prefix) => a.startsWith(prefix))) {
|
||||
continue;
|
||||
}
|
||||
out.push(a);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const rawArgv = process.argv.slice(2);
|
||||
const extraArgs = stripOcoFlags(rawArgv);
|
||||
|
||||
cli(
|
||||
{
|
||||
@@ -82,5 +109,5 @@ cli(
|
||||
|
||||
commit(extraArgs, flags.context, false, flags.fgm, flags.yes);
|
||||
},
|
||||
extraArgs
|
||||
rawArgv
|
||||
);
|
||||
|
||||
@@ -236,7 +236,9 @@ ${chalk.grey('——————————————————')}`
|
||||
await generateCommitMessageFromGitDiff({
|
||||
diff,
|
||||
extraArgs,
|
||||
fullGitMojiSpec
|
||||
context,
|
||||
fullGitMojiSpec,
|
||||
skipCommitConfirmation
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +130,7 @@ async function handleModelNotFoundError(
|
||||
...existingConfig,
|
||||
OCO_MODEL: newModel
|
||||
} as any);
|
||||
console.log(chalk.green('✔') + ' Model saved as default\n');
|
||||
console.log(chalk.green('√') + ' Model saved as default\n');
|
||||
}
|
||||
|
||||
return newModel;
|
||||
@@ -168,7 +168,8 @@ export const generateCommitMessageByDiff = async (
|
||||
const commitMessagePromises = await getCommitMsgsPromisesFromFileDiffs(
|
||||
diff,
|
||||
MAX_REQUEST_TOKENS,
|
||||
fullGitMojiSpec
|
||||
fullGitMojiSpec,
|
||||
context
|
||||
);
|
||||
|
||||
const commitMessages = [] as string[];
|
||||
@@ -228,7 +229,8 @@ function getMessagesPromisesByChangesInFile(
|
||||
fileDiff: string,
|
||||
separator: string,
|
||||
maxChangeLength: number,
|
||||
fullGitMojiSpec: boolean
|
||||
fullGitMojiSpec: boolean,
|
||||
context: string
|
||||
) {
|
||||
const hunkHeaderSeparator = '@@ ';
|
||||
const [fileHeader, ...fileDiffByLines] = fileDiff.split(hunkHeaderSeparator);
|
||||
@@ -256,7 +258,8 @@ function getMessagesPromisesByChangesInFile(
|
||||
async (lineDiff) => {
|
||||
const messages = await generateCommitMessageChatCompletionPrompt(
|
||||
separator + lineDiff,
|
||||
fullGitMojiSpec
|
||||
fullGitMojiSpec,
|
||||
context
|
||||
);
|
||||
|
||||
return engine.generateCommitMessage(messages);
|
||||
@@ -305,7 +308,8 @@ function splitDiff(diff: string, maxChangeLength: number) {
|
||||
export const getCommitMsgsPromisesFromFileDiffs = async (
|
||||
diff: string,
|
||||
maxDiffLength: number,
|
||||
fullGitMojiSpec: boolean
|
||||
fullGitMojiSpec: boolean,
|
||||
context: string
|
||||
) => {
|
||||
const separator = 'diff --git ';
|
||||
|
||||
@@ -323,14 +327,16 @@ export const getCommitMsgsPromisesFromFileDiffs = async (
|
||||
fileDiff,
|
||||
separator,
|
||||
maxDiffLength,
|
||||
fullGitMojiSpec
|
||||
fullGitMojiSpec,
|
||||
context
|
||||
);
|
||||
|
||||
commitMessagePromises.push(...messagesPromises);
|
||||
} else {
|
||||
const messages = await generateCommitMessageChatCompletionPrompt(
|
||||
separator + fileDiff,
|
||||
fullGitMojiSpec
|
||||
fullGitMojiSpec,
|
||||
context
|
||||
);
|
||||
|
||||
const engine = getEngine();
|
||||
|
||||
@@ -95,11 +95,11 @@ const CONVENTIONAL_COMMIT_KEYWORDS =
|
||||
'Do not preface the commit with anything, except for the conventional commit keywords: fix, feat, build, chore, ci, docs, style, refactor, perf, test.';
|
||||
|
||||
const getCommitConvention = (fullGitMojiSpec: boolean) =>
|
||||
config.OCO_EMOJI
|
||||
? fullGitMojiSpec
|
||||
? FULL_GITMOJI_SPEC
|
||||
: GITMOJI_HELP
|
||||
: CONVENTIONAL_COMMIT_KEYWORDS;
|
||||
fullGitMojiSpec
|
||||
? FULL_GITMOJI_SPEC
|
||||
: config.OCO_EMOJI
|
||||
? GITMOJI_HELP
|
||||
: CONVENTIONAL_COMMIT_KEYWORDS;
|
||||
|
||||
const getDescriptionInstruction = () =>
|
||||
config.OCO_DESCRIPTION
|
||||
@@ -123,36 +123,36 @@ const getScopeInstruction = () =>
|
||||
* $ oco -- This is a context used to generate the commit message
|
||||
* @returns - The context of the user input
|
||||
*/
|
||||
const userInputCodeContext = (context: string) => {
|
||||
if (context !== '' && context !== ' ') {
|
||||
return `Additional context provided by the user: <context>${context}</context>\nConsider this context when generating the commit message, incorporating relevant information when appropriate.`;
|
||||
const userInputCodeContext = (context: string | undefined | null) => {
|
||||
const trimmed = (context ?? '').trim();
|
||||
if (trimmed === '') {
|
||||
return '';
|
||||
}
|
||||
return '';
|
||||
return `Additional context provided by the user: <context>${trimmed}</context>\nConsider this context when generating the commit message, incorporating relevant information when appropriate.`;
|
||||
};
|
||||
|
||||
const INIT_MAIN_PROMPT = (
|
||||
language: string,
|
||||
fullGitMojiSpec: boolean,
|
||||
context: string
|
||||
): OpenAI.Chat.Completions.ChatCompletionMessageParam => ({
|
||||
role: 'system',
|
||||
content: (() => {
|
||||
const commitConvention = fullGitMojiSpec
|
||||
? 'GitMoji specification'
|
||||
: 'Conventional Commit Convention';
|
||||
const missionStatement = `${IDENTITY} Your mission is to create clean and comprehensive commit messages as per the ${commitConvention} and explain WHAT were the changes and mainly WHY the changes were done.`;
|
||||
const diffInstruction =
|
||||
"I'll send you an output of 'git diff --staged' command, and you are to convert it into a commit message.";
|
||||
const conventionGuidelines = getCommitConvention(fullGitMojiSpec);
|
||||
const descriptionGuideline = getDescriptionInstruction();
|
||||
const oneLineCommitGuideline = getOneLineCommitInstruction();
|
||||
const scopeInstruction = getScopeInstruction();
|
||||
const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`;
|
||||
const userInputContext = userInputCodeContext(context);
|
||||
): OpenAI.Chat.Completions.ChatCompletionMessageParam => {
|
||||
const commitConvention = fullGitMojiSpec
|
||||
? 'GitMoji specification'
|
||||
: 'Conventional Commit Convention';
|
||||
const missionStatement = `${IDENTITY} Your mission is to create clean and comprehensive commit messages as per the ${commitConvention} and explain WHAT were the changes and mainly WHY the changes were done.`;
|
||||
const diffInstruction =
|
||||
"I'll send you an output of 'git diff --staged' command, and you are to convert it into a commit message.";
|
||||
const conventionGuidelines = getCommitConvention(fullGitMojiSpec);
|
||||
const descriptionGuideline = getDescriptionInstruction();
|
||||
const oneLineCommitGuideline = getOneLineCommitInstruction();
|
||||
const scopeInstruction = getScopeInstruction();
|
||||
const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`;
|
||||
const userInputContext = userInputCodeContext(context);
|
||||
|
||||
return `${missionStatement}\n${diffInstruction}\n${conventionGuidelines}\n${descriptionGuideline}\n${oneLineCommitGuideline}\n${scopeInstruction}\n${generalGuidelines}\n${userInputContext}`;
|
||||
})()
|
||||
});
|
||||
const content = `${missionStatement}\n${diffInstruction}\n${conventionGuidelines}\n${descriptionGuideline}\n${oneLineCommitGuideline}\n${scopeInstruction}\n${generalGuidelines}\n${userInputContext}`;
|
||||
|
||||
return { role: 'system', content };
|
||||
};
|
||||
|
||||
export const INIT_DIFF_PROMPT: OpenAI.Chat.Completions.ChatCompletionMessageParam =
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user