Files
gemini-cli/packages/core/src/services/fileDiscoveryService.ts
T

101 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-06-03 21:40:46 -07:00
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { GitIgnoreFilter } from '../utils/gitIgnoreParser.js';
2025-09-10 12:48:07 -07:00
import type { GeminiIgnoreFilter } from '../utils/geminiIgnoreParser.js';
import { GitIgnoreParser } from '../utils/gitIgnoreParser.js';
2025-09-10 12:48:07 -07:00
import { GeminiIgnoreParser } from '../utils/geminiIgnoreParser.js';
2025-06-03 21:40:46 -07:00
import { isGitRepository } from '../utils/gitUtils.js';
import * as path from 'node:path';
2025-06-03 21:40:46 -07:00
export interface FilterFilesOptions {
2025-06-03 21:40:46 -07:00
respectGitIgnore?: boolean;
respectGeminiIgnore?: boolean;
2025-06-03 21:40:46 -07:00
}
export interface FilterReport {
filteredPaths: string[];
ignoredCount: number;
}
2025-06-03 21:40:46 -07:00
export class FileDiscoveryService {
private gitIgnoreFilter: GitIgnoreFilter | null = null;
2025-09-10 12:48:07 -07:00
private geminiIgnoreFilter: GeminiIgnoreFilter | null = null;
private combinedIgnoreFilter: GitIgnoreFilter | null = null;
2025-06-03 21:40:46 -07:00
private projectRoot: string;
constructor(projectRoot: string) {
this.projectRoot = path.resolve(projectRoot);
if (isGitRepository(this.projectRoot)) {
2025-09-10 12:48:07 -07:00
this.gitIgnoreFilter = new GitIgnoreParser(this.projectRoot);
}
2025-09-10 12:48:07 -07:00
this.geminiIgnoreFilter = new GeminiIgnoreParser(this.projectRoot);
if (this.gitIgnoreFilter) {
const geminiPatterns = this.geminiIgnoreFilter.getPatterns();
// Create combined parser: .gitignore + .geminiignore
this.combinedIgnoreFilter = new GitIgnoreParser(
this.projectRoot,
geminiPatterns,
);
}
}
2025-06-03 21:40:46 -07:00
/**
* Filters a list of file paths based on git ignore rules
*/
filterFiles(filePaths: string[], options: FilterFilesOptions = {}): string[] {
const { respectGitIgnore = true, respectGeminiIgnore = true } = options;
2025-06-03 21:40:46 -07:00
return filePaths.filter((filePath) => {
if (
respectGitIgnore &&
respectGeminiIgnore &&
this.combinedIgnoreFilter
) {
return !this.combinedIgnoreFilter.isIgnored(filePath);
}
if (respectGitIgnore && this.gitIgnoreFilter?.isIgnored(filePath)) {
return false;
}
if (respectGeminiIgnore && this.geminiIgnoreFilter?.isIgnored(filePath)) {
return false;
2025-06-03 21:40:46 -07:00
}
return true;
});
}
/**
* Filters a list of file paths based on git ignore rules and returns a report
* with counts of ignored files.
*/
filterFilesWithReport(
filePaths: string[],
opts: FilterFilesOptions = {
respectGitIgnore: true,
respectGeminiIgnore: true,
},
): FilterReport {
const filteredPaths = this.filterFiles(filePaths, opts);
const ignoredCount = filePaths.length - filteredPaths.length;
return {
filteredPaths,
ignoredCount,
};
}
/**
* Unified method to check if a file should be ignored based on filtering options
*/
shouldIgnoreFile(
filePath: string,
options: FilterFilesOptions = {},
): boolean {
return this.filterFiles([filePath], options).length === 0;
}
2025-06-03 21:40:46 -07:00
}