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/services/fileDiscoveryService.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/services/fileDiscoveryService.ts')
| -rw-r--r-- | packages/core/src/services/fileDiscoveryService.ts | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/packages/core/src/services/fileDiscoveryService.ts b/packages/core/src/services/fileDiscoveryService.ts new file mode 100644 index 00000000..5329507e --- /dev/null +++ b/packages/core/src/services/fileDiscoveryService.ts @@ -0,0 +1,87 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { GitIgnoreParser, GitIgnoreFilter } from '../utils/gitIgnoreParser.js'; +import { isGitRepository } from '../utils/gitUtils.js'; +import * as path from 'path'; + +export interface FileDiscoveryOptions { + respectGitIgnore?: boolean; + includeBuildArtifacts?: boolean; + isGitRepo?: boolean; +} + +export class FileDiscoveryService { + private gitIgnoreFilter: GitIgnoreFilter | null = null; + private projectRoot: string; + + constructor(projectRoot: string) { + this.projectRoot = path.resolve(projectRoot); + } + + async initialize(options: FileDiscoveryOptions = {}): Promise<void> { + const isGitRepo = options.isGitRepo ?? isGitRepository(this.projectRoot); + + if (options.respectGitIgnore !== false && isGitRepo) { + const parser = new GitIgnoreParser(this.projectRoot); + await parser.initialize(); + this.gitIgnoreFilter = parser; + } + } + + /** + * Filters a list of file paths based on git ignore rules + */ + filterFiles( + filePaths: string[], + options: FileDiscoveryOptions = {}, + ): string[] { + return filePaths.filter((filePath) => { + // Always respect git ignore unless explicitly disabled + if (options.respectGitIgnore !== false && this.gitIgnoreFilter) { + if (this.gitIgnoreFilter.isIgnored(filePath)) { + return false; + } + } + + return true; + }); + } + + /** + * Gets patterns that would be ignored for debugging/transparency + */ + getIgnoreInfo(): { gitIgnored: string[] } { + return { + gitIgnored: this.gitIgnoreFilter?.getIgnoredPatterns() || [], + }; + } + + /** + * Checks if a single file should be ignored + */ + shouldIgnoreFile( + filePath: string, + options: FileDiscoveryOptions = {}, + ): boolean { + const isGitRepo = options.isGitRepo ?? isGitRepository(this.projectRoot); + if ( + options.respectGitIgnore !== false && + isGitRepo && + this.gitIgnoreFilter + ) { + return this.gitIgnoreFilter.isIgnored(filePath); + } + return false; + } + + /** + * Returns whether the project is a git repository + */ + isGitRepository(options: FileDiscoveryOptions = {}): boolean { + return options.isGitRepo ?? isGitRepository(this.projectRoot); + } +} |
