mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-01-12 23:28:16 -05:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b747d70e69 | ||
|
|
caa64fbcf9 | ||
|
|
1a49c08409 | ||
|
|
3399d65a0c | ||
|
|
d561170519 | ||
|
|
bf29c260ca | ||
|
|
9e2a3d8988 | ||
|
|
1ea5fbc430 | ||
|
|
55e9adf73d | ||
|
|
60325f53b9 | ||
|
|
3966c4c53a | ||
|
|
2527c80f2f | ||
|
|
e89fc96732 | ||
|
|
b5d1057fd6 | ||
|
|
5f310970cc |
20
README.md
20
README.md
@@ -13,7 +13,7 @@
|
||||
|
||||
## Examples
|
||||
|
||||
Look into [the commits](https://github.com/di-sukharev/opencommit/commit) to see how OpenCommit works. Emoji and long commit description text is configurable.
|
||||
Look into [the commits](https://github.com/di-sukharev/opencommit/commit/eae7618d575ee8d2e9fff5de56da79d40c4bc5fc) to see how OpenCommit works. Emoji and long commit description text is configurable.
|
||||
|
||||
## Setup
|
||||
|
||||
@@ -60,27 +60,27 @@ oc
|
||||
To add emoji:
|
||||
|
||||
```sh
|
||||
opencommit config set emoji=true
|
||||
oc config set emoji=true
|
||||
```
|
||||
|
||||
To remove emoji:
|
||||
|
||||
```sh
|
||||
opencommit config set emoji=false
|
||||
oc config set emoji=false
|
||||
```
|
||||
|
||||
### Postface commits with descriptions of changes 🤠
|
||||
### Postface commits with descriptions of changes
|
||||
|
||||
To add descriptions:
|
||||
|
||||
```sh
|
||||
opencommit config set description=true
|
||||
oc config set description=true
|
||||
```
|
||||
|
||||
To remove description:
|
||||
|
||||
```sh
|
||||
opencommit config set description=false
|
||||
oc config set description=false
|
||||
```
|
||||
|
||||
## Git hook
|
||||
@@ -90,20 +90,20 @@ You can set opencommit as Git [`prepare-commit-msg`](https://git-scm.com/docs/gi
|
||||
To set the hook:
|
||||
|
||||
```sh
|
||||
opencommit hook set
|
||||
oc hook set
|
||||
```
|
||||
|
||||
To unset the hook:
|
||||
|
||||
```sh
|
||||
opencommit hook unset
|
||||
oc hook unset
|
||||
```
|
||||
|
||||
To use the hook:
|
||||
|
||||
```sh
|
||||
git add <files...>
|
||||
git commit
|
||||
git add <files...>
|
||||
git commit
|
||||
```
|
||||
|
||||
Or follow the process of your IDE Source Control feature, when it calls `git commit` command — OpenCommit will integrate into the flow.
|
||||
|
||||
2
TODO.md
2
TODO.md
@@ -1,7 +1,7 @@
|
||||
# TODOs
|
||||
|
||||
- [] [build for both mjs and cjs](https://snyk.io/blog/best-practices-create-modern-npm-package/)
|
||||
- [] make bundle smaller by properly configuring esbuild
|
||||
- [] [build for both mjs and cjs](https://snyk.io/blog/best-practices-create-modern-npm-package/)
|
||||
- [] do // TODOs in the code
|
||||
- [] batch small files in one request
|
||||
- [] add tests
|
||||
|
||||
120
di.txt
Normal file
120
di.txt
Normal file
@@ -0,0 +1,120 @@
|
||||
diff --git a/src/generateCommitMessageFromGitDiff.ts b/src/generateCommitMessageFromGitDiff.ts
|
||||
index c3fb638..82b8bde 100644
|
||||
--- a/src/generateCommitMessageFromGitDiff.ts
|
||||
+++ b/src/generateCommitMessageFromGitDiff.ts
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
} from 'openai';
|
||||
import { api } from './api';
|
||||
import { getConfig } from './commands/config';
|
||||
+import { mergeStrings } from './utils/mergeStrings';
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
@@ -88,37 +89,64 @@ export const generateCommitMessageWithChatCompletion = async (
|
||||
): Promise<string | GenerateCommitMessageError> => {
|
||||
try {
|
||||
if (diff.length >= MAX_REQ_TOKENS) {
|
||||
- const separator = 'diff --git ';
|
||||
-
|
||||
- const diffByFiles = diff.split(separator).slice(1);
|
||||
-
|
||||
- const commitMessagePromises = diffByFiles
|
||||
- .map((fileDiff) => {
|
||||
- // TODO: split by files
|
||||
- if (fileDiff.length >= MAX_REQ_TOKENS) return null;
|
||||
-
|
||||
- const messages = generateCommitMessageChatCompletionPrompt(
|
||||
- separator + fileDiff
|
||||
- );
|
||||
-
|
||||
- return api.generateCommitMessage(messages);
|
||||
- })
|
||||
- .filter(Boolean);
|
||||
+ const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs(diff);
|
||||
|
||||
const commitMessages = await Promise.all(commitMessagePromises);
|
||||
|
||||
return commitMessages.join('\n\n');
|
||||
- }
|
||||
-
|
||||
- const messages = generateCommitMessageChatCompletionPrompt(diff);
|
||||
+ } else {
|
||||
+ const messages = generateCommitMessageChatCompletionPrompt(diff);
|
||||
|
||||
- const commitMessage = await api.generateCommitMessage(messages);
|
||||
+ const commitMessage = await api.generateCommitMessage(messages);
|
||||
|
||||
- if (!commitMessage)
|
||||
- return { error: GenerateCommitMessageErrorEnum.emptyMessage };
|
||||
+ if (!commitMessage)
|
||||
+ return { error: GenerateCommitMessageErrorEnum.emptyMessage };
|
||||
|
||||
- return commitMessage;
|
||||
+ return commitMessage;
|
||||
+ }
|
||||
} catch (error) {
|
||||
return { error: GenerateCommitMessageErrorEnum.internalError };
|
||||
}
|
||||
};
|
||||
+
|
||||
+function getMessagesPromisesByLines(fileDiff: string, separator: string) {
|
||||
+ const [fileHeader, ...fileDiffByLines] = fileDiff.split('@@');
|
||||
+ const lineDiffsWithHeader = fileDiffByLines.map((d) => fileHeader + '@@' + d);
|
||||
+
|
||||
+ const mergedLines = mergeStrings(lineDiffsWithHeader, MAX_REQ_TOKENS);
|
||||
+
|
||||
+ const commitMsgsFromFileLineDiffs = mergedLines.map((d) => {
|
||||
+ const messages = generateCommitMessageChatCompletionPrompt(separator + d);
|
||||
+
|
||||
+ return api.generateCommitMessage(messages);
|
||||
+ });
|
||||
+
|
||||
+ return commitMsgsFromFileLineDiffs;
|
||||
+}
|
||||
+
|
||||
+function getCommitMsgsPromisesFromFileDiffs(diff: string) {
|
||||
+ const separator = 'diff --git ';
|
||||
+
|
||||
+ const diffByFiles = diff.split(separator).slice(1);
|
||||
+
|
||||
+ const mergedDiffs = mergeStrings(diffByFiles, MAX_REQ_TOKENS);
|
||||
+
|
||||
+ const commitMessagePromises = [];
|
||||
+
|
||||
+ for (const fileDiff of mergedDiffs) {
|
||||
+ if (fileDiff.length >= MAX_REQ_TOKENS) {
|
||||
+ // split fileDiff into lineDiff
|
||||
+ const messagesPromises = getMessagesPromisesByLines(fileDiff, separator);
|
||||
+
|
||||
+ commitMessagePromises.push(...messagesPromises);
|
||||
+ } else {
|
||||
+ // generate commits for files
|
||||
+ const messages = generateCommitMessageChatCompletionPrompt(
|
||||
+ separator + fileDiff
|
||||
+ );
|
||||
+
|
||||
+ commitMessagePromises.push(api.generateCommitMessage(messages));
|
||||
+ }
|
||||
+ }
|
||||
+ return commitMessagePromises;
|
||||
+}
|
||||
diff --git a/src/utils/mergeStrings.ts b/src/utils/mergeStrings.ts
|
||||
new file mode 100644
|
||||
index 0000000..a8beb37
|
||||
--- /dev/null
|
||||
+++ b/src/utils/mergeStrings.ts
|
||||
@@ -0,0 +1,14 @@
|
||||
+export function mergeStrings(arr: string[], maxStringLength: number): string[] {
|
||||
+ const mergedArr: string[] = [];
|
||||
+ let currentItem: string = arr[0];
|
||||
+ for (const item of arr.slice(1)) {
|
||||
+ if (currentItem.length + item.length <= maxStringLength) {
|
||||
+ currentItem += item;
|
||||
+ } else {
|
||||
+ mergedArr.push(currentItem);
|
||||
+ currentItem = item;
|
||||
+ }
|
||||
+ }
|
||||
+ mergedArr.push(currentItem);
|
||||
+ return mergedArr;
|
||||
+}
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "open-commit",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "open-commit",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.3",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@clack/prompts": "^0.6.1",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "opencommit",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.3",
|
||||
"description": "AI generates conventional commits with mind-blowing accuracy.",
|
||||
"keywords": [
|
||||
"git",
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { intro, outro } from '@clack/prompts';
|
||||
import {
|
||||
ChatCompletionRequestMessage,
|
||||
ChatCompletionResponseMessage,
|
||||
Configuration as OpenAiApiConfiguration,
|
||||
OpenAIApi
|
||||
} from 'openai';
|
||||
@@ -43,7 +42,7 @@ class OpenAi {
|
||||
|
||||
public generateCommitMessage = async (
|
||||
messages: Array<ChatCompletionRequestMessage>
|
||||
): Promise<ChatCompletionResponseMessage | undefined> => {
|
||||
): Promise<string | undefined> => {
|
||||
try {
|
||||
const { data } = await this.openAI.createChatCompletion({
|
||||
model: 'gpt-3.5-turbo',
|
||||
@@ -55,9 +54,9 @@ class OpenAi {
|
||||
|
||||
const message = data.choices[0].message;
|
||||
|
||||
return message;
|
||||
return message?.content;
|
||||
} catch (error) {
|
||||
console.error('openAI api error', { error });
|
||||
// console.error('openAI api error', { error });
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { cli } from 'cleye';
|
||||
import packageJSON from '../package.json';
|
||||
import packageJSON from '../package.json' assert { type: 'json' };
|
||||
|
||||
import { configCommand } from './commands/config';
|
||||
import { hookCommand, isHookCalled } from './commands/githook.js';
|
||||
|
||||
@@ -79,7 +79,7 @@ export async function commit(isStageAllFlag = false) {
|
||||
.bold('`oc`')} command.`
|
||||
);
|
||||
|
||||
stagedFilesSpinner.stop('Counting staged files');
|
||||
stagedFilesSpinner.stop('No files are staged');
|
||||
const isStageAllAndCommitConfirmedByUser = await confirm({
|
||||
message: 'Do you want to stage all files and generate commit message?'
|
||||
});
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { command } from 'cleye';
|
||||
import { assertGitRepo } from '../utils/git.js';
|
||||
import { existsSync } from 'fs';
|
||||
|
||||
@@ -37,7 +37,12 @@ export const prepareCommitMessageHook = async () => {
|
||||
|
||||
if (typeof commitMessage !== 'string') throw new Error(commitMessage.error);
|
||||
|
||||
await fs.appendFile(messageFilePath, commitMessage);
|
||||
const fileContent = await fs.readFile(messageFilePath);
|
||||
|
||||
await fs.writeFile(
|
||||
messageFilePath,
|
||||
commitMessage + '\n' + fileContent.toString()
|
||||
);
|
||||
|
||||
outro(`${chalk.green('✔')} commit done`);
|
||||
} catch (error) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
} from 'openai';
|
||||
import { api } from './api';
|
||||
import { getConfig } from './commands/config';
|
||||
import { mergeStrings } from './utils/mergeStrings';
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
@@ -81,47 +82,73 @@ const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
|
||||
(msg) => msg.content
|
||||
).join('').length;
|
||||
|
||||
const MAX_REQ_TOKENS = 3900 - INIT_MESSAGES_PROMPT_LENGTH;
|
||||
|
||||
export const generateCommitMessageWithChatCompletion = async (
|
||||
diff: string
|
||||
): Promise<string | GenerateCommitMessageError> => {
|
||||
try {
|
||||
const MAX_REQ_TOKENS = 3900;
|
||||
if (diff.length >= MAX_REQ_TOKENS) {
|
||||
const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs(diff);
|
||||
|
||||
if (INIT_MESSAGES_PROMPT_LENGTH + diff.length >= MAX_REQ_TOKENS) {
|
||||
const separator = 'diff --git ';
|
||||
|
||||
const diffByFiles = diff.split(separator).slice(1);
|
||||
|
||||
const commitMessages = [];
|
||||
|
||||
for (const diffFile of diffByFiles) {
|
||||
if (INIT_MESSAGES_PROMPT_LENGTH + diffFile.length >= MAX_REQ_TOKENS)
|
||||
continue;
|
||||
|
||||
const messages = generateCommitMessageChatCompletionPrompt(
|
||||
separator + diffFile
|
||||
);
|
||||
|
||||
const commitMessage = await api.generateCommitMessage(messages);
|
||||
|
||||
// TODO: handle this edge case
|
||||
if (!commitMessage?.content) continue;
|
||||
|
||||
commitMessages.push(commitMessage?.content);
|
||||
}
|
||||
const commitMessages = await Promise.all(commitMessagePromises);
|
||||
|
||||
return commitMessages.join('\n\n');
|
||||
} else {
|
||||
const messages = generateCommitMessageChatCompletionPrompt(diff);
|
||||
|
||||
const commitMessage = await api.generateCommitMessage(messages);
|
||||
|
||||
if (!commitMessage)
|
||||
return { error: GenerateCommitMessageErrorEnum.emptyMessage };
|
||||
|
||||
return commitMessage;
|
||||
}
|
||||
|
||||
const messages = generateCommitMessageChatCompletionPrompt(diff);
|
||||
|
||||
const commitMessage = await api.generateCommitMessage(messages);
|
||||
|
||||
if (!commitMessage)
|
||||
return { error: GenerateCommitMessageErrorEnum.emptyMessage };
|
||||
|
||||
return commitMessage.content;
|
||||
} catch (error) {
|
||||
return { error: GenerateCommitMessageErrorEnum.internalError };
|
||||
}
|
||||
};
|
||||
|
||||
function getMessagesPromisesByLines(fileDiff: string, separator: string) {
|
||||
const [fileHeader, ...fileDiffByLines] = fileDiff.split('\n@@');
|
||||
const lineDiffsWithHeader = fileDiffByLines.map(
|
||||
(d) => fileHeader + '\n@@' + d
|
||||
);
|
||||
|
||||
const mergedLines = mergeStrings(lineDiffsWithHeader, MAX_REQ_TOKENS);
|
||||
|
||||
const commitMsgsFromFileLineDiffs = mergedLines.map((d) => {
|
||||
const messages = generateCommitMessageChatCompletionPrompt(separator + d);
|
||||
|
||||
return api.generateCommitMessage(messages);
|
||||
});
|
||||
|
||||
return commitMsgsFromFileLineDiffs;
|
||||
}
|
||||
|
||||
function getCommitMsgsPromisesFromFileDiffs(diff: string) {
|
||||
const separator = 'diff --git ';
|
||||
|
||||
const diffByFiles = diff.split(separator).slice(1);
|
||||
|
||||
const mergedDiffs = mergeStrings(diffByFiles, MAX_REQ_TOKENS);
|
||||
|
||||
const commitMessagePromises = [];
|
||||
|
||||
for (const fileDiff of mergedDiffs) {
|
||||
if (fileDiff.length >= MAX_REQ_TOKENS) {
|
||||
// split fileDiff into lineDiff
|
||||
const messagesPromises = getMessagesPromisesByLines(fileDiff, separator);
|
||||
|
||||
commitMessagePromises.push(...messagesPromises);
|
||||
} else {
|
||||
// generate commits for files
|
||||
const messages = generateCommitMessageChatCompletionPrompt(
|
||||
separator + fileDiff
|
||||
);
|
||||
|
||||
commitMessagePromises.push(api.generateCommitMessage(messages));
|
||||
}
|
||||
}
|
||||
return commitMessagePromises;
|
||||
}
|
||||
|
||||
14
src/utils/mergeStrings.ts
Normal file
14
src/utils/mergeStrings.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export function mergeStrings(arr: string[], maxStringLength: number): string[] {
|
||||
const mergedArr: string[] = [];
|
||||
let currentItem: string = arr[0];
|
||||
for (const item of arr.slice(1)) {
|
||||
if (currentItem.length + item.length <= maxStringLength) {
|
||||
currentItem += item;
|
||||
} else {
|
||||
mergedArr.push(currentItem);
|
||||
currentItem = item;
|
||||
}
|
||||
}
|
||||
mergedArr.push(currentItem);
|
||||
return mergedArr;
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"lib": ["ES5"],
|
||||
"lib": ["ES5", "ES6"],
|
||||
|
||||
"module": "CommonJS",
|
||||
"module": "ESNext",
|
||||
// "rootDir": "./src",
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true,
|
||||
|
||||
Reference in New Issue
Block a user