Skip to content

Commit

Permalink
fix: ignore the gitignored files in the scan to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 6, 2023
1 parent 1a9febd commit 77f676a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cspell-ignore-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ cacheable
crap
flagger
flaggy
globify
Posixified
posixify
shit
3 changes: 2 additions & 1 deletion packages/cspell-gitignore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
},
"dependencies": {
"cspell-glob": "workspace:*",
"find-up": "^5.0.0"
"find-up": "^5.0.0",
"globify-gitignore": "^0.2.1"
},
"devDependencies": {
"@types/node": "^18.11.18",
Expand Down
21 changes: 21 additions & 0 deletions packages/cspell-gitignore/src/GitIgnore.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { globifyGitIgnoreFile, posixifyPathNormalized } from 'globify-gitignore';
import * as path from 'path';
import { contains } from '.';
import { GitIgnoreHierarchy, IsIgnoredExResult, loadGitIgnore } from './GitIgnoreFile';
Expand Down Expand Up @@ -51,6 +52,26 @@ export class GitIgnore {
this.resolvedGitIgnoreHierarchies.set(directory, found);
return find;
}

async getGlobs(root: string) {
const rootPosixified = posixifyPathNormalized(root);
const globs = await globifyGitIgnoreFile(rootPosixified);

// globify-gitignore is compatible with fast-glob, but to make it work with glob,
// the patterns need to be separated and normalized.
const rootPosixifiedSlashed = `${rootPosixified}/`;
const ignored = [];
const included = [];
for (const g of globs) {
if (g.startsWith('!')) {
ignored.push(g.slice(1).replace(rootPosixifiedSlashed, ''));
} else {
included.push(g.replace(rootPosixifiedSlashed, ''));
}
}
return [ignored, included];
}

filterOutIgnored(files: string[]): Promise<string[]>;
filterOutIgnored(files: Iterable<string>): Promise<string[]>;
filterOutIgnored(files: AsyncIterable<string>): AsyncIterable<string>;
Expand Down
18 changes: 18 additions & 0 deletions packages/cspell/src/lint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,24 @@ async function determineFilesToCheck(
globOptions.dot = enableGlobDot;
}

if (gitIgnore) {
// Add the gitignore file in the root to the globs to improve performance.
const [gitIgnoredGlobs, gitIncludedGlobs] = await gitIgnore.getGlobs(root);

allGlobs.push(...gitIncludedGlobs);
fileGlobs.push(...gitIncludedGlobs);

let globOptionsIgnore: string[] = [];
if (Array.isArray(globOptions.ignore)) {
globOptionsIgnore = globOptions.ignore;
} else if (typeof globOptions.ignore === 'string') {
globOptionsIgnore = [globOptions.ignore];
}

globOptionsIgnore.push(...gitIgnoredGlobs);
globOptions.ignore = globOptionsIgnore;
}

const filterFiles = opFilter(filterFilesFn(globMatcher));
const foundFiles = await (hasFileLists
? useFileLists(fileLists, allGlobs, root, enableGlobDot)
Expand Down
36 changes: 35 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 77f676a

Please sign in to comment.