Compare commits

...

10 Commits

Author SHA1 Message Date
di-sukharev
7fb46de105 1.1.17 2023-03-21 15:06:43 +08:00
di-sukharev
2f6e98dc30 style(commit.ts): format code with prettier
refactor(commit.ts): add types to commit function parameters
fix(git.ts): handle empty string returned from getStagedFiles function

refactor(mergeStrings.ts): remove unnecessary blank line and add missing semicolon
2023-03-21 15:06:30 +08:00
di-sukharev
2aa6582c52 Merge remote-tracking branch 'origin/dev' 2023-03-21 14:51:44 +08:00
openefit
2acf833cd0 fix(generateCommitMessageFromGitDiff.ts): remove unnecessary character at the end of the line (#36) 2023-03-19 16:04:06 +08:00
Stuart van Beek
3f7025d50a feat: add support for .opencommitignore file (#22)
* feat: add support for .opencommitignore
2023-03-19 16:01:57 +08:00
Nader Zouaoui
d793bf1340 feat: Add support for extra args to be passed to the git commit command (#17)
* feat: Add support for extra args to be passed to the git commit command
2023-03-19 15:58:21 +08:00
di-sukharev
0092e92061 1.1.16 2023-03-17 17:51:42 +08:00
di-sukharev
0f33b74942 1.1.15 2023-03-17 17:51:36 +08:00
di-sukharev
8f0a32275e 1.1.14 2023-03-17 17:50:39 +08:00
di-sukharev
b3509e34d0 1.1.13 2023-03-17 14:20:51 +08:00
9 changed files with 81 additions and 16 deletions

3
.gitignore vendored
View File

@@ -10,4 +10,5 @@ application.log
logfile.log
uncaughtExceptions.log
.vscode
src/*.json
src/*.json
.idea

View File

@@ -84,6 +84,32 @@ To remove description:
oc config set description=false
```
### Git flags
The `opencommit` or `oc` commands can be used in place of the `git commit -m "${generatedMessage}"` command. This means that any regular flags that are used with the `git commit` command will also be applied when using `opencommit` or `oc`.
```sh
oc --no-verify
```
is translated to :
```sh
git commit -m "${generatedMessage}" --no-verify
```
### Ignore files
You can ignore files from submission to OpenAI by creating a `.opencommitignore` file. For example:
```ignorelang
path/to/large-asset.zip
**/*.jpg
```
This is useful for preventing opencommit from uploading artifacts and large files.
By default, opencommit ignores files matching: `*-lock.*` and `*.lock`
## Git hook
You can set OpenCommit as Git [`prepare-commit-msg`](https://git-scm.com/docs/githooks#_prepare_commit_msg) hook. Hook integrates with you IDE Source Control and allows you edit the message before commit.

6
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "opencommit",
"version": "1.1.12",
"version": "1.1.17",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "opencommit",
"version": "1.1.12",
"version": "1.1.17",
"license": "ISC",
"dependencies": {
"@clack/prompts": "^0.6.1",
@@ -14,6 +14,7 @@
"chalk": "^5.2.0",
"cleye": "^1.3.2",
"execa": "^7.0.0",
"ignore": "^5.2.4",
"ini": "^3.0.1",
"inquirer": "^9.1.4",
"openai": "^3.2.1"
@@ -1880,7 +1881,6 @@
"version": "5.2.4",
"resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz",
"integrity": "sha512-MAb38BcSbH0eHNBxn7ql2NH/kX33OkB3lZ1BNdh7ENeRChHTYsTvWrMubiIAMNS2llXEEgZ1MUOBtXChP3kaFQ==",
"dev": true,
"engines": {
"node": ">= 4"
}

View File

@@ -1,6 +1,6 @@
{
"name": "opencommit",
"version": "1.1.12",
"version": "1.1.17",
"description": "GPT CLI to auto-generate impressive commits in 1 second. Killing lame commits with AI 🤯🔫",
"keywords": [
"git",
@@ -64,6 +64,7 @@
"chalk": "^5.2.0",
"cleye": "^1.3.2",
"execa": "^7.0.0",
"ignore": "^5.2.4",
"ini": "^3.0.1",
"inquirer": "^9.1.4",
"openai": "^3.2.1"

View File

@@ -8,7 +8,7 @@ import { hookCommand, isHookCalled } from './commands/githook.js';
import { prepareCommitMessageHook } from './commands/prepare-commit-msg-hook';
import { commit } from './commands/commit';
const rawArgv = process.argv.slice(2);
const extraArgs = process.argv.slice(2);
cli(
{
@@ -23,8 +23,8 @@ cli(
if (isHookCalled) {
prepareCommitMessageHook();
} else {
commit();
commit(extraArgs);
}
},
rawArgv
extraArgs
);

View File

@@ -22,7 +22,8 @@ import chalk from 'chalk';
import { trytm } from '../utils/trytm';
const generateCommitMessageFromGitDiff = async (
diff: string
diff: string,
extraArgs: string[]
): Promise<void> => {
await assertGitRepo();
@@ -59,7 +60,12 @@ ${chalk.grey('——————————————————')}`
});
if (isCommitConfirmedByUser && !isCancel(isCommitConfirmedByUser)) {
const { stdout } = await execa('git', ['commit', '-m', commitMessage]);
const { stdout } = await execa('git', [
'commit',
'-m',
commitMessage,
...extraArgs
]);
outro(`${chalk.green('✔')} successfully committed`);
@@ -74,6 +80,7 @@ ${chalk.grey('——————————————————')}`
pushSpinner.start('Running `git push`');
const { stdout } = await execa('git', ['push']);
pushSpinner.stop(`${chalk.green('✔')} successfully pushed all commits`);
if (stdout) outro(stdout);
@@ -81,9 +88,13 @@ ${chalk.grey('——————————————————')}`
} else outro(`${chalk.gray('✖')} process cancelled`);
};
export async function commit(isStageAllFlag = false) {
export async function commit(
extraArgs: string[] = [],
isStageAllFlag: Boolean = false
) {
if (isStageAllFlag) {
const changedFiles = await getChangedFiles();
if (changedFiles) await gitAdd({ files: changedFiles });
else {
outro('No changes detected, write some code and run `oc` again');
@@ -106,6 +117,7 @@ export async function commit(isStageAllFlag = false) {
}
const stagedFilesSpinner = spinner();
stagedFilesSpinner.start('Counting staged files');
if (!stagedFiles.length) {
@@ -118,7 +130,7 @@ export async function commit(isStageAllFlag = false) {
isStageAllAndCommitConfirmedByUser &&
!isCancel(isStageAllAndCommitConfirmedByUser)
) {
await commit(true);
await commit(extraArgs, true);
process.exit(1);
}
@@ -136,7 +148,7 @@ export async function commit(isStageAllFlag = false) {
await gitAdd({ files });
}
await commit(false);
await commit(extraArgs, false);
process.exit(1);
}
@@ -147,7 +159,10 @@ export async function commit(isStageAllFlag = false) {
);
const [, generateCommitError] = await trytm(
generateCommitMessageFromGitDiff(await getDiff({ files: stagedFiles }))
generateCommitMessageFromGitDiff(
await getDiff({ files: stagedFiles }),
extraArgs
)
);
if (generateCommitError) {

View File

@@ -21,7 +21,7 @@ const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
config?.description
? 'Add a short description of what commit is about after the commit message. Don\'t start it with "This commit", just describe the changes.'
: 'Don\'t add any descriptions to the commit, only commit message.'
} Use ${translation.localLanguage} to answer.}`
} Use ${translation.localLanguage} to answer.`
},
{
role: ChatCompletionRequestMessageRoleEnum.User,

View File

@@ -1,5 +1,7 @@
import { execa } from 'execa';
import { outro, spinner } from '@clack/prompts';
import { readFileSync } from 'fs';
import ignore, { Ignore } from 'ignore';
export const assertGitRepo = async () => {
try {
@@ -13,6 +15,16 @@ export const assertGitRepo = async () => {
// (file) => `:(exclude)${file}`
// );
export const getOpenCommitIgnore = (): Ignore => {
const ig = ignore();
try {
ig.add(readFileSync('.opencommitignore').toString().split('\n'));
} catch (e) {}
return ig;
};
export const getStagedFiles = async (): Promise<string[]> => {
const { stdout: files } = await execa('git', [
'diff',
@@ -22,7 +34,14 @@ export const getStagedFiles = async (): Promise<string[]> => {
if (!files) return [];
return files.split('\n').sort();
const filesList = files.split('\n');
const ig = getOpenCommitIgnore();
const allowedFiles = filesList.filter((file) => !ig.ignores(file));
if (!allowedFiles) return [];
return allowedFiles.sort();
};
export const getChangedFiles = async (): Promise<string[]> => {
@@ -67,6 +86,7 @@ export const getDiff = async ({ files }: { files: string[] }) => {
const { stdout: diff } = await execa('git', [
'diff',
'--staged',
'--',
...filesWithoutLocks
]);

View File

@@ -9,6 +9,8 @@ export function mergeStrings(arr: string[], maxStringLength: number): string[] {
currentItem = item;
}
}
mergedArr.push(currentItem);
return mergedArr;
}