diff options
| author | Keith Ballinger <[email protected]> | 2025-06-03 21:40:46 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-04 04:40:46 +0000 |
| commit | c313762ba06ab1324dccd4c7663038cb56d24e53 (patch) | |
| tree | dbec46c12a06047ec1b79bfdfe6c8a5dd90a97be /packages/core/src/tools/read-many-files.ts | |
| parent | d85f09ac5129227932d3d6cf76b6dac36a325655 (diff) | |
Ignore folders files (#651)
# Add .gitignore-Aware File Filtering to gemini-cli
This pull request introduces .gitignore-based file filtering to the gemini-cli, ensuring that git-ignored files are automatically excluded from file-related operations and suggestions throughout the CLI. The update enhances usability, reduces noise from build artifacts and dependencies, and provides new configuration options for fine-tuning file discovery.
Key Improvements
.gitignore File Filtering
All @ (at) commands, file completions, and core discovery tools now honor .gitignore patterns by default.
Git-ignored files (such as node_modules/, dist/, .env, and .git) are excluded from results unless explicitly overridden.
The behavior can be customized via a new fileFiltering section in settings.json, including options for:
Turning .gitignore respect on/off.
Adding custom ignore patterns.
Allowing or excluding build artifacts.
Configuration & Documentation Updates
settings.json schema extended with fileFiltering options.
Documentation updated to explain new filtering controls and usage patterns.
Testing
New and updated integration/unit tests for file filtering logic, configuration merging, and edge cases.
Test coverage ensures .gitignore filtering works as intended across different workflows.
Internal Refactoring
Core file discovery logic refactored for maintainability and extensibility.
Underlying tools (ls, glob, read-many-files) now support git-aware filtering out of the box.
Co-authored-by: N. Taylor Mullen <[email protected]>
Diffstat (limited to 'packages/core/src/tools/read-many-files.ts')
| -rw-r--r-- | packages/core/src/tools/read-many-files.ts | 61 |
1 files changed, 59 insertions, 2 deletions
diff --git a/packages/core/src/tools/read-many-files.ts b/packages/core/src/tools/read-many-files.ts index 4ba09ef0..30f70c91 100644 --- a/packages/core/src/tools/read-many-files.ts +++ b/packages/core/src/tools/read-many-files.ts @@ -16,6 +16,7 @@ import { DEFAULT_ENCODING, } from '../utils/fileUtils.js'; import { PartListUnion } from '@google/genai'; +import { Config } from '../config/config.js'; /** * Parameters for the ReadManyFilesTool. @@ -54,6 +55,11 @@ export interface ReadManyFilesParams { * Optional. Apply default exclusion patterns. Defaults to true. */ useDefaultExcludes?: boolean; + + /** + * Optional. Whether to respect .gitignore patterns. Defaults to true. + */ + respect_git_ignore?: boolean; } /** @@ -119,7 +125,10 @@ export class ReadManyFilesTool extends BaseTool< * @param targetDir The absolute root directory within which this tool is allowed to operate. * All paths provided in `params` will be resolved relative to this directory. */ - constructor(readonly targetDir: string) { + constructor( + readonly targetDir: string, + private config: Config, + ) { const parameterSchema: Record<string, unknown> = { type: 'object', properties: { @@ -155,6 +164,12 @@ export class ReadManyFilesTool extends BaseTool< 'Optional. Whether to apply a list of default exclusion patterns (e.g., node_modules, .git, binary files). Defaults to true.', default: true, }, + respect_git_ignore: { + type: 'boolean', + description: + 'Optional. Whether to respect .gitignore patterns when discovering files. Only available in git repositories. Defaults to true.', + default: true, + }, }, required: ['paths'], }; @@ -254,8 +269,15 @@ Use this tool when the user's query implies needing the content of several files include = [], exclude = [], useDefaultExcludes = true, + respect_git_ignore = true, } = params; + const respectGitIgnore = + respect_git_ignore ?? this.config.getFileFilteringRespectGitIgnore(); + + // Get centralized file discovery service + const fileDiscovery = await this.config.getFileService(); + const toolBaseDir = this.targetDir; const filesToConsider = new Set<string>(); const skippedFiles: Array<{ path: string; reason: string }> = []; @@ -290,9 +312,22 @@ Use this tool when the user's query implies needing the content of several files caseSensitiveMatch: false, }); + // Apply git-aware filtering if enabled and in git repository + const filteredEntries = + respectGitIgnore && fileDiscovery.isGitRepository() + ? fileDiscovery + .filterFiles( + entries.map((p) => path.relative(toolBaseDir, p)), + { + respectGitIgnore, + }, + ) + .map((p) => path.resolve(toolBaseDir, p)) + : entries; + + let gitIgnoredCount = 0; for (const absoluteFilePath of entries) { // Security check: ensure the glob library didn't return something outside targetDir. - // This should be guaranteed by `cwd` and the library's sandboxing, but an extra check is good practice. if (!absoluteFilePath.startsWith(toolBaseDir)) { skippedFiles.push({ path: absoluteFilePath, @@ -300,8 +335,30 @@ Use this tool when the user's query implies needing the content of several files }); continue; } + + // Check if this file was filtered out by git ignore + if ( + respectGitIgnore && + fileDiscovery.isGitRepository() && + !filteredEntries.includes(absoluteFilePath) + ) { + gitIgnoredCount++; + continue; + } + filesToConsider.add(absoluteFilePath); } + + // Add info about git-ignored files if any were filtered + if (gitIgnoredCount > 0) { + const reason = respectGitIgnore + ? 'git-ignored' + : 'filtered by custom ignore patterns'; + skippedFiles.push({ + path: `${gitIgnoredCount} file(s)`, + reason, + }); + } } catch (error) { return { llmContent: `Error during file search: ${getErrorMessage(error)}`, |
