Skip to content

Commit b470556

Browse files
committed
feat: enhance ignore file support with local and global configurations
1 parent 8ae2f7d commit b470556

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,39 @@ Once users have generated their desired commit message, they can proceed to comm
327327
328328
### Ignore files
329329
330-
You can remove files from being sent to OpenAI by creating a `.opencommitignore` file. For example:
330+
You can remove files from being sent to OpenAI by creating ignore files. OpenCommit supports both local and global ignore files:
331+
332+
#### Local ignore file
333+
334+
Create a `.opencommitignore` file in your project root:
331335
332336
```ignorelang
333337
path/to/large-asset.zip
334338
**/*.jpg
339+
docs/generated/
340+
*.generated.ts
335341
```
336342
343+
#### Global ignore file
344+
345+
Create a global `.opencommitignore` file in your home directory (`~/.opencommitignore`) to ignore files across all projects:
346+
347+
```ignorelang
348+
*.log
349+
*.tmp
350+
.DS_Store
351+
node_modules/
352+
.env
353+
.env.local
354+
dist/
355+
build/
356+
coverage/
357+
.vscode/
358+
.idea/
359+
```
360+
361+
**Priority:** Local ignore rules are applied after global rules. Both files use [gitignore syntax](https://git-scm.com/docs/gitignore#_pattern_format).
362+
337363
This helps prevent opencommit from uploading artifacts and large files.
338364
339365
By default, opencommit ignores files matching: `*-lock.*` and `*.lock`

src/utils/git.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { execa } from 'execa';
2-
import { readFileSync } from 'fs';
2+
import { readFileSync, existsSync } from 'fs';
33
import ignore, { Ignore } from 'ignore';
44
import { join } from 'path';
5+
import { homedir } from 'os';
56
import { outro, spinner } from '@clack/prompts';
67

78
export const assertGitRepo = async () => {
@@ -18,14 +19,27 @@ export const assertGitRepo = async () => {
1819

1920
export const getOpenCommitIgnore = async (): Promise<Ignore> => {
2021
const gitDir = await getGitDir();
21-
2222
const ig = ignore();
2323

24-
try {
25-
ig.add(
26-
readFileSync(join(gitDir, '.opencommitignore')).toString().split('\n')
27-
);
28-
} catch (e) {}
24+
const globalIgnorePath = join(homedir(), '.opencommitignore');
25+
if (existsSync(globalIgnorePath)) {
26+
try {
27+
const globalIgnoreContent = readFileSync(globalIgnorePath, 'utf8');
28+
ig.add(globalIgnoreContent.split('\n'));
29+
} catch (e) {
30+
// do nothing
31+
}
32+
}
33+
34+
const localIgnorePath = join(gitDir, '.opencommitignore');
35+
if (existsSync(localIgnorePath)) {
36+
try {
37+
const localIgnoreContent = readFileSync(localIgnorePath, 'utf8');
38+
ig.add(localIgnoreContent.split('\n'));
39+
} catch (e) {
40+
// do nothing
41+
}
42+
}
2943

3044
return ig;
3145
};

0 commit comments

Comments
 (0)