|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { GitIgnoreParser, GitIgnoreFilter } from '../utils/gitIgnoreParser.js'; |
|
|
import { isGitRepository } from '../utils/gitUtils.js'; |
|
|
import * as path from 'path'; |
|
|
|
|
|
const GEMINI_IGNORE_FILE_NAME = '.geminiignore'; |
|
|
|
|
|
export interface FilterFilesOptions { |
|
|
respectGitIgnore?: boolean; |
|
|
respectGeminiIgnore?: boolean; |
|
|
} |
|
|
|
|
|
export class FileDiscoveryService { |
|
|
private gitIgnoreFilter: GitIgnoreFilter | null = null; |
|
|
private geminiIgnoreFilter: GitIgnoreFilter | null = null; |
|
|
private projectRoot: string; |
|
|
|
|
|
constructor(projectRoot: string) { |
|
|
this.projectRoot = path.resolve(projectRoot); |
|
|
if (isGitRepository(this.projectRoot)) { |
|
|
const parser = new GitIgnoreParser(this.projectRoot); |
|
|
try { |
|
|
parser.loadGitRepoPatterns(); |
|
|
} catch (_error) { |
|
|
|
|
|
} |
|
|
this.gitIgnoreFilter = parser; |
|
|
} |
|
|
const gParser = new GitIgnoreParser(this.projectRoot); |
|
|
try { |
|
|
gParser.loadPatterns(GEMINI_IGNORE_FILE_NAME); |
|
|
} catch (_error) { |
|
|
|
|
|
} |
|
|
this.geminiIgnoreFilter = gParser; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filterFiles( |
|
|
filePaths: string[], |
|
|
options: FilterFilesOptions = { |
|
|
respectGitIgnore: true, |
|
|
respectGeminiIgnore: true, |
|
|
}, |
|
|
): string[] { |
|
|
return filePaths.filter((filePath) => { |
|
|
if (options.respectGitIgnore && this.shouldGitIgnoreFile(filePath)) { |
|
|
return false; |
|
|
} |
|
|
if ( |
|
|
options.respectGeminiIgnore && |
|
|
this.shouldGeminiIgnoreFile(filePath) |
|
|
) { |
|
|
return false; |
|
|
} |
|
|
return true; |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shouldGitIgnoreFile(filePath: string): boolean { |
|
|
if (this.gitIgnoreFilter) { |
|
|
return this.gitIgnoreFilter.isIgnored(filePath); |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shouldGeminiIgnoreFile(filePath: string): boolean { |
|
|
if (this.geminiIgnoreFilter) { |
|
|
return this.geminiIgnoreFilter.isIgnored(filePath); |
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getGeminiIgnorePatterns(): string[] { |
|
|
return this.geminiIgnoreFilter?.getPatterns() ?? []; |
|
|
} |
|
|
} |
|
|
|