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>
2025-06-03 21:40:46 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { GitIgnoreFilter } from '../utils/gitIgnoreParser.js';
|
2026-01-27 17:19:13 -08:00
|
|
|
import type { IgnoreFileFilter } from '../utils/ignoreFileParser.js';
|
2025-08-26 00:04:53 +02:00
|
|
|
import { GitIgnoreParser } from '../utils/gitIgnoreParser.js';
|
2026-01-27 17:19:13 -08:00
|
|
|
import { IgnoreFileParser } from '../utils/ignoreFileParser.js';
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
import { isGitRepository } from '../utils/gitUtils.js';
|
2026-01-27 17:19:13 -08:00
|
|
|
import { GEMINI_IGNORE_FILE_NAME } from '../config/constants.js';
|
|
|
|
|
import fs from 'node:fs';
|
2025-08-25 22:11:27 +02:00
|
|
|
import * as path from 'node:path';
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
|
2025-06-14 10:25:34 -04:00
|
|
|
export interface FilterFilesOptions {
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
respectGitIgnore?: boolean;
|
2025-06-14 10:25:34 -04:00
|
|
|
respectGeminiIgnore?: boolean;
|
2026-01-27 17:19:13 -08:00
|
|
|
customIgnoreFilePaths?: string[];
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
|
2025-09-10 09:54:50 -07:00
|
|
|
export interface FilterReport {
|
|
|
|
|
filteredPaths: string[];
|
2025-10-22 17:06:31 -07:00
|
|
|
ignoredCount: number;
|
2025-09-10 09:54:50 -07:00
|
|
|
}
|
|
|
|
|
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
export class FileDiscoveryService {
|
|
|
|
|
private gitIgnoreFilter: GitIgnoreFilter | null = null;
|
2026-01-27 17:19:13 -08:00
|
|
|
private geminiIgnoreFilter: IgnoreFileFilter | null = null;
|
|
|
|
|
private customIgnoreFilter: IgnoreFileFilter | null = null;
|
|
|
|
|
private combinedIgnoreFilter: GitIgnoreFilter | IgnoreFileFilter | null =
|
|
|
|
|
null;
|
|
|
|
|
private defaultFilterFileOptions: FilterFilesOptions = {
|
|
|
|
|
respectGitIgnore: true,
|
|
|
|
|
respectGeminiIgnore: true,
|
|
|
|
|
customIgnoreFilePaths: [],
|
|
|
|
|
};
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
private projectRoot: string;
|
|
|
|
|
|
2026-01-27 17:19:13 -08:00
|
|
|
constructor(projectRoot: string, options?: FilterFilesOptions) {
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
this.projectRoot = path.resolve(projectRoot);
|
2026-01-27 17:19:13 -08:00
|
|
|
this.applyFilterFilesOptions(options);
|
2025-06-14 10:25:34 -04:00
|
|
|
if (isGitRepository(this.projectRoot)) {
|
2025-09-10 12:48:07 -07:00
|
|
|
this.gitIgnoreFilter = new GitIgnoreParser(this.projectRoot);
|
2025-06-14 10:25:34 -04:00
|
|
|
}
|
2026-01-27 17:19:13 -08:00
|
|
|
this.geminiIgnoreFilter = new IgnoreFileParser(
|
|
|
|
|
this.projectRoot,
|
|
|
|
|
GEMINI_IGNORE_FILE_NAME,
|
|
|
|
|
);
|
|
|
|
|
if (this.defaultFilterFileOptions.customIgnoreFilePaths?.length) {
|
|
|
|
|
this.customIgnoreFilter = new IgnoreFileParser(
|
|
|
|
|
this.projectRoot,
|
|
|
|
|
this.defaultFilterFileOptions.customIgnoreFilePaths,
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-11-01 10:06:34 -07:00
|
|
|
|
|
|
|
|
if (this.gitIgnoreFilter) {
|
|
|
|
|
const geminiPatterns = this.geminiIgnoreFilter.getPatterns();
|
2026-01-27 17:19:13 -08:00
|
|
|
const customPatterns = this.customIgnoreFilter
|
|
|
|
|
? this.customIgnoreFilter.getPatterns()
|
|
|
|
|
: [];
|
|
|
|
|
// Create combined parser: .gitignore + .geminiignore + custom ignore
|
2025-11-01 10:06:34 -07:00
|
|
|
this.combinedIgnoreFilter = new GitIgnoreParser(
|
|
|
|
|
this.projectRoot,
|
2026-01-27 17:19:13 -08:00
|
|
|
// customPatterns should go the last to ensure overwriting of geminiPatterns
|
|
|
|
|
[...geminiPatterns, ...customPatterns],
|
2025-11-01 10:06:34 -07:00
|
|
|
);
|
2026-01-27 17:19:13 -08:00
|
|
|
} else {
|
|
|
|
|
// Create combined parser when not git repo
|
|
|
|
|
const geminiPatterns = this.geminiIgnoreFilter.getPatterns();
|
|
|
|
|
const customPatterns = this.customIgnoreFilter
|
|
|
|
|
? this.customIgnoreFilter.getPatterns()
|
|
|
|
|
: [];
|
|
|
|
|
this.combinedIgnoreFilter = new IgnoreFileParser(
|
|
|
|
|
this.projectRoot,
|
|
|
|
|
[...geminiPatterns, ...customPatterns],
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private applyFilterFilesOptions(options?: FilterFilesOptions): void {
|
|
|
|
|
if (!options) return;
|
|
|
|
|
|
|
|
|
|
if (options.respectGitIgnore !== undefined) {
|
|
|
|
|
this.defaultFilterFileOptions.respectGitIgnore = options.respectGitIgnore;
|
|
|
|
|
}
|
|
|
|
|
if (options.respectGeminiIgnore !== undefined) {
|
|
|
|
|
this.defaultFilterFileOptions.respectGeminiIgnore =
|
|
|
|
|
options.respectGeminiIgnore;
|
|
|
|
|
}
|
|
|
|
|
if (options.customIgnoreFilePaths) {
|
|
|
|
|
this.defaultFilterFileOptions.customIgnoreFilePaths =
|
|
|
|
|
options.customIgnoreFilePaths;
|
2025-11-01 10:06:34 -07:00
|
|
|
}
|
2025-06-12 07:09:38 -07:00
|
|
|
}
|
|
|
|
|
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
/**
|
2026-01-27 17:19:13 -08:00
|
|
|
* Filters a list of file paths based on ignore rules
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
*/
|
2025-10-24 18:55:12 -07:00
|
|
|
filterFiles(filePaths: string[], options: FilterFilesOptions = {}): string[] {
|
2026-01-27 17:19:13 -08:00
|
|
|
const {
|
|
|
|
|
respectGitIgnore = this.defaultFilterFileOptions.respectGitIgnore,
|
|
|
|
|
respectGeminiIgnore = this.defaultFilterFileOptions.respectGeminiIgnore,
|
|
|
|
|
} = options;
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
return filePaths.filter((filePath) => {
|
2025-11-01 10:06:34 -07:00
|
|
|
if (
|
|
|
|
|
respectGitIgnore &&
|
|
|
|
|
respectGeminiIgnore &&
|
|
|
|
|
this.combinedIgnoreFilter
|
|
|
|
|
) {
|
|
|
|
|
return !this.combinedIgnoreFilter.isIgnored(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-27 17:19:13 -08:00
|
|
|
// Always respect custom ignore filter if provided
|
|
|
|
|
if (this.customIgnoreFilter?.isIgnored(filePath)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 18:55:12 -07:00
|
|
|
if (respectGitIgnore && this.gitIgnoreFilter?.isIgnored(filePath)) {
|
2025-06-14 10:25:34 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2025-10-24 18:55:12 -07:00
|
|
|
if (respectGeminiIgnore && this.geminiIgnoreFilter?.isIgnored(filePath)) {
|
2025-06-14 10:25:34 -04:00
|
|
|
return false;
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 09:54:50 -07:00
|
|
|
/**
|
|
|
|
|
* 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 {
|
2025-10-22 17:06:31 -07:00
|
|
|
const filteredPaths = this.filterFiles(filePaths, opts);
|
|
|
|
|
const ignoredCount = filePaths.length - filteredPaths.length;
|
2025-09-10 09:54:50 -07:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
filteredPaths,
|
2025-10-22 17:06:31 -07:00
|
|
|
ignoredCount,
|
2025-09-10 09:54:50 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 13:48:39 +08:00
|
|
|
/**
|
|
|
|
|
* Unified method to check if a file should be ignored based on filtering options
|
|
|
|
|
*/
|
|
|
|
|
shouldIgnoreFile(
|
|
|
|
|
filePath: string,
|
|
|
|
|
options: FilterFilesOptions = {},
|
|
|
|
|
): boolean {
|
2025-10-24 18:55:12 -07:00
|
|
|
return this.filterFiles([filePath], options).length === 0;
|
2025-07-07 13:48:39 +08:00
|
|
|
}
|
2026-01-27 17:19:13 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the list of ignore files being used (e.g. .geminiignore) excluding .gitignore.
|
|
|
|
|
*/
|
|
|
|
|
getIgnoreFilePaths(): string[] {
|
|
|
|
|
const paths: string[] = [];
|
|
|
|
|
if (
|
|
|
|
|
this.geminiIgnoreFilter &&
|
|
|
|
|
this.defaultFilterFileOptions.respectGeminiIgnore
|
|
|
|
|
) {
|
|
|
|
|
paths.push(...this.geminiIgnoreFilter.getIgnoreFilePaths());
|
|
|
|
|
}
|
|
|
|
|
if (this.customIgnoreFilter) {
|
|
|
|
|
paths.push(...this.customIgnoreFilter.getIgnoreFilePaths());
|
|
|
|
|
}
|
|
|
|
|
return paths;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns all ignore files including .gitignore if applicable.
|
|
|
|
|
*/
|
|
|
|
|
getAllIgnoreFilePaths(): string[] {
|
|
|
|
|
const paths: string[] = [];
|
|
|
|
|
if (
|
|
|
|
|
this.gitIgnoreFilter &&
|
|
|
|
|
this.defaultFilterFileOptions.respectGitIgnore
|
|
|
|
|
) {
|
|
|
|
|
const gitIgnorePath = path.join(this.projectRoot, '.gitignore');
|
|
|
|
|
if (fs.existsSync(gitIgnorePath)) {
|
|
|
|
|
paths.push(gitIgnorePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return paths.concat(this.getIgnoreFilePaths());
|
|
|
|
|
}
|
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>
2025-06-03 21:40:46 -07:00
|
|
|
}
|