Repository navigation
fix(indexer): honor nested .gitignore files #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { promises as fs } from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import ignore, { type Ignore } from 'ignore'; | ||
|
|
||
| function scopePatterns(contents: string, directory: string): string[] { | ||
| // The directory is a literal path even when it contains glob metacharacters. | ||
| const prefix = directory.replace( | ||
| /[\\*?\[\]]/g, | ||
| (char) => `[\\x${char.charCodeAt(0).toString(16)}]` | ||
| ); | ||
| const patterns: string[] = []; | ||
| for (const line of contents.replace(/^\uFEFF/, '').split(/\r?\n/)) { | ||
| if (line.startsWith('#')) continue; | ||
| const negated = line.startsWith('!'); | ||
| const pattern = negated ? line.slice(1) : line; | ||
| const trimmed = pattern.trimEnd(); | ||
| if (!trimmed || trimmed === '/') continue; | ||
|
|
||
| // A final slash marks a directory, while other slashes anchor the rule. | ||
| const anchored = trimmed.startsWith('/') || trimmed.replace(/\/$/, '').includes('/'); | ||
| const base = anchored ? `/${prefix}/` : `/${prefix}/**/`; | ||
| const relative = pattern.replace(/^\//, ''); | ||
| // A directory-only globstar must match at least one directory below its base. | ||
| const scoped = `${base}${relative}`.replace(/\/\*\*\/(\s*)$/, '/**/*/$1'); | ||
| patterns.push(`${negated ? '!' : ''}${scoped}`); | ||
| } | ||
| return patterns; | ||
| } | ||
|
|
||
| /** Load the .gitignore rules that apply to each directory once per scan. */ | ||
| export function createGitignoreMatcher( | ||
| rootPath: string | ||
| ): (relativeFile: string) => Promise<boolean> { | ||
| const matchers = new Map<string, Ignore | null>(); | ||
|
|
||
| async function matcherFor(directory: string): Promise<Ignore | null> { | ||
| const cached = matchers.get(directory); | ||
| if (cached !== undefined) return cached; | ||
|
|
||
| const isRoot = directory === '.'; | ||
| const inherited = isRoot ? null : await matcherFor(path.posix.dirname(directory)); | ||
| let matcher = inherited; | ||
|
|
||
| // Excluded directories cannot re-include their contents through their own rules. | ||
| if (isRoot || !inherited?.ignores(`${directory}/`)) { | ||
| try { | ||
| const ignoreFile = path.join(rootPath, directory, '.gitignore'); | ||
| if ((await fs.lstat(ignoreFile)).isFile()) { | ||
| const contents = await fs.readFile(ignoreFile, 'utf8'); | ||
| const local = ignore.default(); | ||
| if (inherited) local.add(inherited); | ||
| matcher = local.add(isRoot ? contents : scopePatterns(contents, directory)); | ||
| } | ||
| } catch { | ||
| // Missing or unreadable ignore files leave inherited rules in effect. | ||
| } | ||
| } | ||
|
|
||
| matchers.set(directory, matcher); | ||
| return matcher; | ||
| } | ||
|
|
||
| return async (relativeFile) => { | ||
| const file = relativeFile.replaceAll(path.sep, '/'); | ||
| const matcher = await matcherFor(path.posix.dirname(file)); | ||
| return matcher?.ignores(file) ?? false; | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the root
.gitignoreis a readable symlink,lstat(...).isFile()returns false and none of its rules are loaded. Previously,scanFilesread the file directly and followed the link. A workspace linking to shared rules containingsecrets.tswill now include that source file in the preview and saved index despiterespectGitignore: true. Preserve the previous root behavior and add a regression test.How this was verified: The new file-type check skips linked ignore files, and the shared scan accepts supported source files when the matcher returns false.