From 3f7025d50afe4d2f3b5e78212988bf851ecf288a Mon Sep 17 00:00:00 2001 From: Stuart van Beek Date: Sun, 19 Mar 2023 09:01:57 +0100 Subject: [PATCH] feat: add support for .opencommitignore file (#22) * feat: add support for .opencommitignore --- .gitignore | 3 ++- README.md | 12 ++++++++++++ package-lock.json | 6 +++--- package.json | 3 ++- src/utils/git.ts | 24 +++++++++++++++++++++--- 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 9a79675..89dcc5c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ application.log logfile.log uncaughtExceptions.log .vscode -src/*.json \ No newline at end of file +src/*.json +.idea \ No newline at end of file diff --git a/README.md b/README.md index 498ac0b..cc3ffc5 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,18 @@ is translated to : 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. diff --git a/package-lock.json b/package-lock.json index e6ec109..47eac80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opencommit", - "version": "1.1.11", + "version": "1.1.16", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opencommit", - "version": "1.1.11", + "version": "1.1.16", "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" } diff --git a/package.json b/package.json index 7c238ac..22522d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencommit", - "version": "1.1.11", + "version": "1.1.16", "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" diff --git a/src/utils/git.ts b/src/utils/git.ts index 3f3dfc8..e492473 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -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,16 +15,32 @@ 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 => { const { stdout: files } = await execa('git', [ 'diff', '--name-only', - '--cached' + '--cached', ]); - if (!files) return []; + const filesList = files.split('\n'); - return files.split('\n').sort(); + + const ig = getOpenCommitIgnore(); + const allowedFiles = filesList.filter(file => !ig.ignores(file)); + + if (!allowedFiles) return []; + + return allowedFiles.sort(); }; export const getChangedFiles = async (): Promise => {