mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-13 05:12:55 -07:00
Limit recursion when looking for .gitignore files (#8103)
Co-authored-by: cornmander <shikhman@google.com>
This commit is contained in:
@@ -18,6 +18,7 @@ export class GitIgnoreParser implements GitIgnoreFilter {
|
||||
private projectRoot: string;
|
||||
private ig: Ignore = ignore();
|
||||
private patterns: string[] = [];
|
||||
private readonly maxScannedDirs = 200;
|
||||
|
||||
constructor(projectRoot: string) {
|
||||
this.projectRoot = path.resolve(projectRoot);
|
||||
@@ -33,13 +34,22 @@ export class GitIgnoreParser implements GitIgnoreFilter {
|
||||
this.findAndLoadGitignoreFiles(this.projectRoot);
|
||||
}
|
||||
|
||||
private findAndLoadGitignoreFiles(dir: string): void {
|
||||
private findAndLoadGitignoreFiles(startDir: string): void {
|
||||
const queue: string[] = [startDir];
|
||||
let scannedDirs = 0;
|
||||
let queueHead = 0;
|
||||
|
||||
while (queueHead < queue.length && scannedDirs < this.maxScannedDirs) {
|
||||
const dir = queue[queueHead];
|
||||
queueHead++;
|
||||
scannedDirs++;
|
||||
|
||||
const relativeDir = path.relative(this.projectRoot, dir);
|
||||
|
||||
// For sub-directories, check if they are ignored before proceeding.
|
||||
// The root directory (relativeDir === '') should not be checked.
|
||||
if (relativeDir && this.isIgnored(relativeDir)) {
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Load patterns from .gitignore in the current directory
|
||||
@@ -56,13 +66,14 @@ export class GitIgnoreParser implements GitIgnoreFilter {
|
||||
continue;
|
||||
}
|
||||
if (entry.isDirectory()) {
|
||||
this.findAndLoadGitignoreFiles(path.join(dir, entry.name));
|
||||
queue.push(path.join(dir, entry.name));
|
||||
}
|
||||
}
|
||||
} catch (_error) {
|
||||
// ignore readdir errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loadPatterns(patternsFileName: string): void {
|
||||
const patternsFilePath = path.join(this.projectRoot, patternsFileName);
|
||||
|
||||
Reference in New Issue
Block a user