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 <ntaylormullen@google.com>
This commit is contained in:
Keith Ballinger
2025-06-03 21:40:46 -07:00
committed by GitHub
parent d85f09ac51
commit c313762ba0
29 changed files with 1934 additions and 45 deletions

View File

@@ -10,6 +10,7 @@ import fg from 'fast-glob';
import { SchemaValidator } from '../utils/schemaValidator.js';
import { BaseTool, ToolResult } from './tools.js';
import { shortenPath, makeRelative } from '../utils/paths.js';
import { Config } from '../config/config.js';
/**
* Parameters for the GlobTool
@@ -29,6 +30,11 @@ export interface GlobToolParams {
* Whether the search should be case-sensitive (optional, defaults to false)
*/
case_sensitive?: boolean;
/**
* Whether to respect .gitignore patterns (optional, defaults to true)
*/
respect_git_ignore?: boolean;
}
/**
@@ -40,7 +46,10 @@ export class GlobTool extends BaseTool<GlobToolParams, ToolResult> {
* Creates a new instance of the GlobLogic
* @param rootDirectory Root directory to ground this tool in.
*/
constructor(private rootDirectory: string) {
constructor(
private rootDirectory: string,
private config: Config,
) {
super(
GlobTool.Name,
'FindFiles',
@@ -62,6 +71,11 @@ export class GlobTool extends BaseTool<GlobToolParams, ToolResult> {
'Optional: Whether the search should be case-sensitive. Defaults to false.',
type: 'boolean',
},
respect_git_ignore: {
description:
'Optional: Whether to respect .gitignore patterns when finding files. Only available in git repositories. Defaults to true.',
type: 'boolean',
},
},
required: ['pattern'],
type: 'object',
@@ -167,6 +181,12 @@ export class GlobTool extends BaseTool<GlobToolParams, ToolResult> {
params.path || '.',
);
// Get centralized file discovery service
const respectGitIgnore =
params.respect_git_ignore ??
this.config.getFileFilteringRespectGitIgnore();
const fileDiscovery = await this.config.getFileService();
const entries = await fg(params.pattern, {
cwd: searchDirAbsolute,
absolute: true,
@@ -179,25 +199,57 @@ export class GlobTool extends BaseTool<GlobToolParams, ToolResult> {
suppressErrors: true,
});
if (!entries || entries.length === 0) {
// Apply git-aware filtering if enabled and in git repository
let filteredEntries = entries;
let gitIgnoredCount = 0;
if (respectGitIgnore && fileDiscovery.isGitRepository()) {
const allPaths = entries.map((entry) => entry.path);
const relativePaths = allPaths.map((p) =>
path.relative(this.rootDirectory, p),
);
const filteredRelativePaths = fileDiscovery.filterFiles(relativePaths, {
respectGitIgnore,
});
const filteredAbsolutePaths = new Set(
filteredRelativePaths.map((p) => path.resolve(this.rootDirectory, p)),
);
filteredEntries = entries.filter((entry) =>
filteredAbsolutePaths.has(entry.path),
);
gitIgnoredCount = entries.length - filteredEntries.length;
}
if (!filteredEntries || filteredEntries.length === 0) {
let message = `No files found matching pattern "${params.pattern}" within ${searchDirAbsolute}.`;
if (gitIgnoredCount > 0) {
message += ` (${gitIgnoredCount} files were git-ignored)`;
}
return {
llmContent: `No files found matching pattern "${params.pattern}" within ${searchDirAbsolute}.`,
llmContent: message,
returnDisplay: `No files found`,
};
}
entries.sort((a, b) => {
filteredEntries.sort((a, b) => {
const mtimeA = a.stats?.mtime?.getTime() ?? 0;
const mtimeB = b.stats?.mtime?.getTime() ?? 0;
return mtimeB - mtimeA;
});
const sortedAbsolutePaths = entries.map((entry) => entry.path);
const sortedAbsolutePaths = filteredEntries.map((entry) => entry.path);
const fileListDescription = sortedAbsolutePaths.join('\n');
const fileCount = sortedAbsolutePaths.length;
let resultMessage = `Found ${fileCount} file(s) matching "${params.pattern}" within ${searchDirAbsolute}`;
if (gitIgnoredCount > 0) {
resultMessage += ` (${gitIgnoredCount} additional files were git-ignored)`;
}
resultMessage += `, sorted by modification time (newest first):\n${fileListDescription}`;
return {
llmContent: `Found ${fileCount} file(s) matching "${params.pattern}" within ${searchDirAbsolute}, sorted by modification time (newest first):\n${fileListDescription}`,
llmContent: resultMessage,
returnDisplay: `Found ${fileCount} matching file(s)`,
};
} catch (error) {