diff --git a/README.md b/README.md index 1b27b530..35478afb 100644 --- a/README.md +++ b/README.md @@ -327,13 +327,39 @@ Once users have generated their desired commit message, they can proceed to comm ### Ignore files -You can remove files from being sent to OpenAI by creating a `.opencommitignore` file. For example: +You can remove files from being sent to OpenAI by creating ignore files. OpenCommit supports both local and global ignore files: + +#### Local ignore file + +Create a `.opencommitignore` file in your project root: ```ignorelang path/to/large-asset.zip **/*.jpg +docs/generated/ +*.generated.ts ``` +#### Global ignore file + +Create a global `.opencommitignore` file in your home directory (`~/.opencommitignore`) to ignore files across all projects: + +```ignorelang +*.log +*.tmp +.DS_Store +node_modules/ +.env +.env.local +dist/ +build/ +coverage/ +.vscode/ +.idea/ +``` + +**Priority:** Local ignore rules are applied after global rules. Both files use [gitignore syntax](https://git-scm.com/docs/gitignore#_pattern_format). + This helps prevent opencommit from uploading artifacts and large files. By default, opencommit ignores files matching: `*-lock.*` and `*.lock` diff --git a/src/utils/git.ts b/src/utils/git.ts index 902d61a8..df95f192 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -1,7 +1,8 @@ import { execa } from 'execa'; -import { readFileSync } from 'fs'; +import { readFileSync, existsSync } from 'fs'; import ignore, { Ignore } from 'ignore'; import { join } from 'path'; +import { homedir } from 'os'; import { outro, spinner } from '@clack/prompts'; export const assertGitRepo = async () => { @@ -18,14 +19,27 @@ export const assertGitRepo = async () => { export const getOpenCommitIgnore = async (): Promise => { const gitDir = await getGitDir(); - const ig = ignore(); - try { - ig.add( - readFileSync(join(gitDir, '.opencommitignore')).toString().split('\n') - ); - } catch (e) {} + const globalIgnorePath = join(homedir(), '.opencommitignore'); + if (existsSync(globalIgnorePath)) { + try { + const globalIgnoreContent = readFileSync(globalIgnorePath, 'utf8'); + ig.add(globalIgnoreContent.split('\n')); + } catch (e) { + // do nothing + } + } + + const localIgnorePath = join(gitDir, '.opencommitignore'); + if (existsSync(localIgnorePath)) { + try { + const localIgnoreContent = readFileSync(localIgnorePath, 'utf8'); + ig.add(localIgnoreContent.split('\n')); + } catch (e) { + // do nothing + } + } return ig; };