summaryrefslogtreecommitdiff
path: root/packages/core/src/tools/ls.ts
diff options
context:
space:
mode:
authorKeith Ballinger <[email protected]>2025-06-03 21:40:46 -0700
committerGitHub <[email protected]>2025-06-04 04:40:46 +0000
commitc313762ba06ab1324dccd4c7663038cb56d24e53 (patch)
treedbec46c12a06047ec1b79bfdfe6c8a5dd90a97be /packages/core/src/tools/ls.ts
parentd85f09ac5129227932d3d6cf76b6dac36a325655 (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/ls.ts')
-rw-r--r--packages/core/src/tools/ls.ts54
1 files changed, 49 insertions, 5 deletions
diff --git a/packages/core/src/tools/ls.ts b/packages/core/src/tools/ls.ts
index fea95187..56a016aa 100644
--- a/packages/core/src/tools/ls.ts
+++ b/packages/core/src/tools/ls.ts
@@ -9,6 +9,7 @@ import path from 'path';
import { BaseTool, ToolResult } from './tools.js';
import { SchemaValidator } from '../utils/schemaValidator.js';
import { makeRelative, shortenPath } from '../utils/paths.js';
+import { Config } from '../config/config.js';
/**
* Parameters for the LS tool
@@ -20,9 +21,14 @@ export interface LSToolParams {
path: string;
/**
- * List of glob patterns to ignore
+ * Array of glob patterns to ignore (optional)
*/
ignore?: string[];
+
+ /**
+ * Whether to respect .gitignore patterns (optional, defaults to true)
+ */
+ respect_git_ignore?: boolean;
}
/**
@@ -65,7 +71,10 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> {
* Creates a new instance of the LSLogic
* @param rootDirectory Root directory to ground this tool in. All operations will be restricted to this directory.
*/
- constructor(private rootDirectory: string) {
+ constructor(
+ private rootDirectory: string,
+ private config: Config,
+ ) {
super(
LSTool.Name,
'ReadFolder',
@@ -84,6 +93,11 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> {
},
type: 'array',
},
+ respect_git_ignore: {
+ description:
+ 'Optional: Whether to respect .gitignore patterns when listing files. Only available in git repositories. Defaults to true.',
+ type: 'boolean',
+ },
},
required: ['path'],
type: 'object',
@@ -214,7 +228,16 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> {
}
const files = fs.readdirSync(params.path);
+
+ // Get centralized file discovery service
+ const respectGitIgnore =
+ params.respect_git_ignore ??
+ this.config.getFileFilteringRespectGitIgnore();
+ const fileDiscovery = await this.config.getFileService();
+
const entries: FileEntry[] = [];
+ let gitIgnoredCount = 0;
+
if (files.length === 0) {
// Changed error message to be more neutral for LLM
return {
@@ -229,6 +252,18 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> {
}
const fullPath = path.join(params.path, file);
+ const relativePath = path.relative(this.rootDirectory, fullPath);
+
+ // Check if this file should be git-ignored (only in git repositories)
+ if (
+ respectGitIgnore &&
+ fileDiscovery.isGitRepository() &&
+ fileDiscovery.shouldIgnoreFile(relativePath)
+ ) {
+ gitIgnoredCount++;
+ continue;
+ }
+
try {
const stats = fs.statSync(fullPath);
const isDir = stats.isDirectory();
@@ -257,10 +292,19 @@ export class LSTool extends BaseTool<LSToolParams, ToolResult> {
.map((entry) => `${entry.isDirectory ? '[DIR] ' : ''}${entry.name}`)
.join('\n');
+ let resultMessage = `Directory listing for ${params.path}:\n${directoryContent}`;
+ if (gitIgnoredCount > 0) {
+ resultMessage += `\n\n(${gitIgnoredCount} items were git-ignored)`;
+ }
+
+ let displayMessage = `Listed ${entries.length} item(s).`;
+ if (gitIgnoredCount > 0) {
+ displayMessage += ` (${gitIgnoredCount} git-ignored)`;
+ }
+
return {
- llmContent: `Directory listing for ${params.path}:\n${directoryContent}`,
- // Simplified display, CLI wrapper can enhance
- returnDisplay: `Listed ${entries.length} item(s).`,
+ llmContent: resultMessage,
+ returnDisplay: displayMessage,
};
} catch (error) {
const errorMsg = `Error listing directory: ${error instanceof Error ? error.message : String(error)}`;