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

View File

@ -4,11 +4,10 @@
* 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 path from 'path';
import { Dirent } from 'fs';
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
// Simple console logger for now.
// TODO: Integrate with a more robust server-side logger.
@ -22,8 +21,7 @@ interface BfsFileSearchOptions {
ignoreDirs?: string[];
maxDirs?: number;
debug?: boolean;
respectGitIgnore?: boolean;
projectRoot?: string;
fileService?: FileDiscoveryService;
}
/**
@ -42,21 +40,13 @@ export async function bfsFileSearch(
ignoreDirs = [],
maxDirs = Infinity,
debug = false,
respectGitIgnore = true,
projectRoot = rootDir,
fileService,
} = options;
const foundFiles: string[] = [];
const queue: string[] = [rootDir];
const visited = new Set<string>();
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) {
const currentDir = queue.shift()!;
if (visited.has(currentDir)) {
@ -79,7 +69,7 @@ export async function bfsFileSearch(
for (const entry of entries) {
const fullPath = path.join(currentDir, entry.name);
if (gitIgnoreFilter?.isIgnored(fullPath)) {
if (fileService?.shouldIgnoreFile(fullPath)) {
continue;
}

View File

@ -13,6 +13,7 @@ import {
GEMINI_CONFIG_DIR,
getAllGeminiMdFilenames,
} from '../tools/memoryTool.js';
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
// 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.
@ -178,12 +179,13 @@ async function getGeminiMdFilePathsInternal(
}
upwardPaths.forEach((p) => allPaths.add(p));
const fileService = new FileDiscoveryService(projectRoot || resolvedCwd);
await fileService.initialize();
const downwardPaths = await bfsFileSearch(resolvedCwd, {
fileName: geminiMdFilename,
maxDirs: MAX_DIRECTORIES_TO_SCAN_FOR_MEMORY,
debug: debugMode,
respectGitIgnore: true,
projectRoot: projectRoot || resolvedCwd,
fileService,
});
downwardPaths.sort(); // Sort for consistent ordering, though hierarchy might be more complex
if (debugMode && downwardPaths.length > 0)