reuse filtering service in `bfsFileSearch` (#1018)

This commit is contained in:
Anas H. Sulaiman 2025-06-13 14:57:03 -04:00 committed by GitHub
parent 084b58a50e
commit 1cefe21d2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import { vi, describe, it, expect, beforeEach } from 'vitest';
import * as fs from 'fs/promises'; import * as fs from 'fs/promises';
import * as gitUtils from './gitUtils.js'; import * as gitUtils from './gitUtils.js';
import { bfsFileSearch } from './bfsFileSearch.js'; import { bfsFileSearch } from './bfsFileSearch.js';
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
vi.mock('fs/promises'); vi.mock('fs/promises');
vi.mock('./gitUtils.js'); vi.mock('./gitUtils.js');
@ -136,9 +137,11 @@ describe('bfsFileSearch', () => {
}); });
mockFs.readFile.mockResolvedValue('subdir2'); mockFs.readFile.mockResolvedValue('subdir2');
const fileService = new FileDiscoveryService('/test');
await fileService.initialize();
const result = await bfsFileSearch('/test', { const result = await bfsFileSearch('/test', {
fileName: 'file1.txt', fileName: 'file1.txt',
respectGitIgnore: true, fileService,
}); });
expect(result).toEqual(['/test/subdir1/file1.txt']); expect(result).toEqual(['/test/subdir1/file1.txt']);
}); });

View File

@ -4,11 +4,10 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
import { GitIgnoreParser, GitIgnoreFilter } from './gitIgnoreParser.js';
import { isGitRepository } from './gitUtils.js';
import * as fs from 'fs/promises'; import * as fs from 'fs/promises';
import * as path from 'path'; import * as path from 'path';
import { Dirent } from 'fs'; import { Dirent } from 'fs';
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
// Simple console logger for now. // Simple console logger for now.
// TODO: Integrate with a more robust server-side logger. // TODO: Integrate with a more robust server-side logger.
@ -22,8 +21,7 @@ interface BfsFileSearchOptions {
ignoreDirs?: string[]; ignoreDirs?: string[];
maxDirs?: number; maxDirs?: number;
debug?: boolean; debug?: boolean;
respectGitIgnore?: boolean; fileService?: FileDiscoveryService;
projectRoot?: string;
} }
/** /**
@ -42,21 +40,13 @@ export async function bfsFileSearch(
ignoreDirs = [], ignoreDirs = [],
maxDirs = Infinity, maxDirs = Infinity,
debug = false, debug = false,
respectGitIgnore = true, fileService,
projectRoot = rootDir,
} = options; } = options;
const foundFiles: string[] = []; const foundFiles: string[] = [];
const queue: string[] = [rootDir]; const queue: string[] = [rootDir];
const visited = new Set<string>(); const visited = new Set<string>();
let scannedDirCount = 0; let scannedDirCount = 0;
let gitIgnoreFilter: GitIgnoreFilter | null = null;
if (respectGitIgnore && isGitRepository(projectRoot)) {
const parser = new GitIgnoreParser(projectRoot);
await parser.initialize();
gitIgnoreFilter = parser;
}
while (queue.length > 0 && scannedDirCount < maxDirs) { while (queue.length > 0 && scannedDirCount < maxDirs) {
const currentDir = queue.shift()!; const currentDir = queue.shift()!;
if (visited.has(currentDir)) { if (visited.has(currentDir)) {
@ -79,7 +69,7 @@ export async function bfsFileSearch(
for (const entry of entries) { for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name); const fullPath = path.join(currentDir, entry.name);
if (gitIgnoreFilter?.isIgnored(fullPath)) { if (fileService?.shouldIgnoreFile(fullPath)) {
continue; continue;
} }

View File

@ -13,6 +13,7 @@ import {
GEMINI_CONFIG_DIR, GEMINI_CONFIG_DIR,
getAllGeminiMdFilenames, getAllGeminiMdFilenames,
} from '../tools/memoryTool.js'; } from '../tools/memoryTool.js';
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
// Simple console logger, similar to the one previously in CLI's config.ts // Simple console logger, similar to the one previously in CLI's config.ts
// TODO: Integrate with a more robust server-side logger if available/appropriate. // TODO: Integrate with a more robust server-side logger if available/appropriate.
@ -178,12 +179,13 @@ async function getGeminiMdFilePathsInternal(
} }
upwardPaths.forEach((p) => allPaths.add(p)); upwardPaths.forEach((p) => allPaths.add(p));
const fileService = new FileDiscoveryService(projectRoot || resolvedCwd);
await fileService.initialize();
const downwardPaths = await bfsFileSearch(resolvedCwd, { const downwardPaths = await bfsFileSearch(resolvedCwd, {
fileName: geminiMdFilename, fileName: geminiMdFilename,
maxDirs: MAX_DIRECTORIES_TO_SCAN_FOR_MEMORY, maxDirs: MAX_DIRECTORIES_TO_SCAN_FOR_MEMORY,
debug: debugMode, debug: debugMode,
respectGitIgnore: true, fileService,
projectRoot: projectRoot || resolvedCwd,
}); });
downwardPaths.sort(); // Sort for consistent ordering, though hierarchy might be more complex downwardPaths.sort(); // Sort for consistent ordering, though hierarchy might be more complex
if (debugMode && downwardPaths.length > 0) if (debugMode && downwardPaths.length > 0)