mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-29 06:25:16 -07:00
refactor: simplify FilterReport and remove unused code (#11681)
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
@@ -136,6 +136,43 @@ describe('FileDiscoveryService', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('filterFilesWithReport', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
await fs.mkdir(path.join(projectRoot, '.git'));
|
||||||
|
await createTestFile('.gitignore', 'node_modules/');
|
||||||
|
await createTestFile('.geminiignore', '*.log');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return filtered paths and correct ignored count', () => {
|
||||||
|
const files = [
|
||||||
|
'src/index.ts',
|
||||||
|
'node_modules/package/index.js',
|
||||||
|
'debug.log',
|
||||||
|
'README.md',
|
||||||
|
].map((f) => path.join(projectRoot, f));
|
||||||
|
|
||||||
|
const service = new FileDiscoveryService(projectRoot);
|
||||||
|
const report = service.filterFilesWithReport(files);
|
||||||
|
|
||||||
|
expect(report.filteredPaths).toEqual(
|
||||||
|
['src/index.ts', 'README.md'].map((f) => path.join(projectRoot, f)),
|
||||||
|
);
|
||||||
|
expect(report.ignoredCount).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle no ignored files', () => {
|
||||||
|
const files = ['src/index.ts', 'README.md'].map((f) =>
|
||||||
|
path.join(projectRoot, f),
|
||||||
|
);
|
||||||
|
|
||||||
|
const service = new FileDiscoveryService(projectRoot);
|
||||||
|
const report = service.filterFilesWithReport(files);
|
||||||
|
|
||||||
|
expect(report.filteredPaths).toEqual(files);
|
||||||
|
expect(report.ignoredCount).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('shouldGitIgnoreFile & shouldGeminiIgnoreFile', () => {
|
describe('shouldGitIgnoreFile & shouldGeminiIgnoreFile', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await fs.mkdir(path.join(projectRoot, '.git'));
|
await fs.mkdir(path.join(projectRoot, '.git'));
|
||||||
|
|||||||
@@ -18,8 +18,7 @@ export interface FilterFilesOptions {
|
|||||||
|
|
||||||
export interface FilterReport {
|
export interface FilterReport {
|
||||||
filteredPaths: string[];
|
filteredPaths: string[];
|
||||||
gitIgnoredCount: number;
|
ignoredCount: number;
|
||||||
geminiIgnoredCount: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FileDiscoveryService {
|
export class FileDiscoveryService {
|
||||||
@@ -70,28 +69,12 @@ export class FileDiscoveryService {
|
|||||||
respectGeminiIgnore: true,
|
respectGeminiIgnore: true,
|
||||||
},
|
},
|
||||||
): FilterReport {
|
): FilterReport {
|
||||||
const filteredPaths: string[] = [];
|
const filteredPaths = this.filterFiles(filePaths, opts);
|
||||||
let gitIgnoredCount = 0;
|
const ignoredCount = filePaths.length - filteredPaths.length;
|
||||||
let geminiIgnoredCount = 0;
|
|
||||||
|
|
||||||
for (const filePath of filePaths) {
|
|
||||||
if (opts.respectGitIgnore && this.shouldGitIgnoreFile(filePath)) {
|
|
||||||
gitIgnoredCount++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.respectGeminiIgnore && this.shouldGeminiIgnoreFile(filePath)) {
|
|
||||||
geminiIgnoredCount++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
filteredPaths.push(filePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
filteredPaths,
|
filteredPaths,
|
||||||
gitIgnoredCount,
|
ignoredCount,
|
||||||
geminiIgnoredCount,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ class GlobToolInvocation extends BaseToolInvocation<
|
|||||||
path.relative(this.config.getTargetDir(), p.fullpath()),
|
path.relative(this.config.getTargetDir(), p.fullpath()),
|
||||||
);
|
);
|
||||||
|
|
||||||
const { filteredPaths, gitIgnoredCount, geminiIgnoredCount } =
|
const { filteredPaths, ignoredCount } =
|
||||||
fileDiscovery.filterFilesWithReport(relativePaths, {
|
fileDiscovery.filterFilesWithReport(relativePaths, {
|
||||||
respectGitIgnore:
|
respectGitIgnore:
|
||||||
this.params?.respect_git_ignore ??
|
this.params?.respect_git_ignore ??
|
||||||
@@ -196,11 +196,8 @@ class GlobToolInvocation extends BaseToolInvocation<
|
|||||||
} else {
|
} else {
|
||||||
message += ` within ${searchDirectories.length} workspace directories`;
|
message += ` within ${searchDirectories.length} workspace directories`;
|
||||||
}
|
}
|
||||||
if (gitIgnoredCount > 0) {
|
if (ignoredCount > 0) {
|
||||||
message += ` (${gitIgnoredCount} files were git-ignored)`;
|
message += ` (${ignoredCount} files were ignored)`;
|
||||||
}
|
|
||||||
if (geminiIgnoredCount > 0) {
|
|
||||||
message += ` (${geminiIgnoredCount} files were gemini-ignored)`;
|
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
llmContent: message,
|
llmContent: message,
|
||||||
@@ -231,11 +228,8 @@ class GlobToolInvocation extends BaseToolInvocation<
|
|||||||
} else {
|
} else {
|
||||||
resultMessage += ` across ${searchDirectories.length} workspace directories`;
|
resultMessage += ` across ${searchDirectories.length} workspace directories`;
|
||||||
}
|
}
|
||||||
if (gitIgnoredCount > 0) {
|
if (ignoredCount > 0) {
|
||||||
resultMessage += ` (${gitIgnoredCount} additional files were git-ignored)`;
|
resultMessage += ` (${ignoredCount} additional files were ignored)`;
|
||||||
}
|
|
||||||
if (geminiIgnoredCount > 0) {
|
|
||||||
resultMessage += ` (${geminiIgnoredCount} additional files were gemini-ignored)`;
|
|
||||||
}
|
}
|
||||||
resultMessage += `, sorted by modification time (newest first):\n${fileListDescription}`;
|
resultMessage += `, sorted by modification time (newest first):\n${fileListDescription}`;
|
||||||
|
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ describe('LSTool', () => {
|
|||||||
expect(result.llmContent).toContain('file1.txt');
|
expect(result.llmContent).toContain('file1.txt');
|
||||||
expect(result.llmContent).not.toContain('file2.log');
|
expect(result.llmContent).not.toContain('file2.log');
|
||||||
// .git is always ignored by default.
|
// .git is always ignored by default.
|
||||||
expect(result.returnDisplay).toBe('Listed 2 item(s). (2 git-ignored)');
|
expect(result.returnDisplay).toBe('Listed 2 item(s). (2 ignored)');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respect geminiignore patterns', async () => {
|
it('should respect geminiignore patterns', async () => {
|
||||||
@@ -161,7 +161,7 @@ describe('LSTool', () => {
|
|||||||
|
|
||||||
expect(result.llmContent).toContain('file1.txt');
|
expect(result.llmContent).toContain('file1.txt');
|
||||||
expect(result.llmContent).not.toContain('file2.log');
|
expect(result.llmContent).not.toContain('file2.log');
|
||||||
expect(result.returnDisplay).toBe('Listed 2 item(s). (1 gemini-ignored)');
|
expect(result.returnDisplay).toBe('Listed 2 item(s). (1 ignored)');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle non-directory paths', async () => {
|
it('should handle non-directory paths', async () => {
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ class LSToolInvocation extends BaseToolInvocation<LSToolParams, ToolResult> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const fileDiscovery = this.config.getFileService();
|
const fileDiscovery = this.config.getFileService();
|
||||||
const { filteredPaths, gitIgnoredCount, geminiIgnoredCount } =
|
const { filteredPaths, ignoredCount } =
|
||||||
fileDiscovery.filterFilesWithReport(relativePaths, {
|
fileDiscovery.filterFilesWithReport(relativePaths, {
|
||||||
respectGitIgnore:
|
respectGitIgnore:
|
||||||
this.params.file_filtering_options?.respect_git_ignore ??
|
this.params.file_filtering_options?.respect_git_ignore ??
|
||||||
@@ -222,20 +222,13 @@ class LSToolInvocation extends BaseToolInvocation<LSToolParams, ToolResult> {
|
|||||||
.join('\n');
|
.join('\n');
|
||||||
|
|
||||||
let resultMessage = `Directory listing for ${this.params.path}:\n${directoryContent}`;
|
let resultMessage = `Directory listing for ${this.params.path}:\n${directoryContent}`;
|
||||||
const ignoredMessages = [];
|
if (ignoredCount > 0) {
|
||||||
if (gitIgnoredCount > 0) {
|
resultMessage += `\n\n(${ignoredCount} ignored)`;
|
||||||
ignoredMessages.push(`${gitIgnoredCount} git-ignored`);
|
|
||||||
}
|
|
||||||
if (geminiIgnoredCount > 0) {
|
|
||||||
ignoredMessages.push(`${geminiIgnoredCount} gemini-ignored`);
|
|
||||||
}
|
|
||||||
if (ignoredMessages.length > 0) {
|
|
||||||
resultMessage += `\n\n(${ignoredMessages.join(', ')})`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let displayMessage = `Listed ${entries.length} item(s).`;
|
let displayMessage = `Listed ${entries.length} item(s).`;
|
||||||
if (ignoredMessages.length > 0) {
|
if (ignoredCount > 0) {
|
||||||
displayMessage += ` (${ignoredMessages.join(', ')})`;
|
displayMessage += ` (${ignoredCount} ignored)`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ ${finalExclusionPatternsForDescription
|
|||||||
);
|
);
|
||||||
|
|
||||||
const fileDiscovery = this.config.getFileService();
|
const fileDiscovery = this.config.getFileService();
|
||||||
const { filteredPaths, gitIgnoredCount, geminiIgnoredCount } =
|
const { filteredPaths, ignoredCount } =
|
||||||
fileDiscovery.filterFilesWithReport(relativeEntries, {
|
fileDiscovery.filterFilesWithReport(relativeEntries, {
|
||||||
respectGitIgnore:
|
respectGitIgnore:
|
||||||
this.params.file_filtering_options?.respect_git_ignore ??
|
this.params.file_filtering_options?.respect_git_ignore ??
|
||||||
@@ -253,19 +253,11 @@ ${finalExclusionPatternsForDescription
|
|||||||
filesToConsider.add(fullPath);
|
filesToConsider.add(fullPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add info about git-ignored files if any were filtered
|
// Add info about ignored files if any were filtered
|
||||||
if (gitIgnoredCount > 0) {
|
if (ignoredCount > 0) {
|
||||||
skippedFiles.push({
|
skippedFiles.push({
|
||||||
path: `${gitIgnoredCount} file(s)`,
|
path: `${ignoredCount} file(s)`,
|
||||||
reason: 'git ignored',
|
reason: 'ignored by project ignore files',
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add info about gemini-ignored files if any were filtered
|
|
||||||
if (geminiIgnoredCount > 0) {
|
|
||||||
skippedFiles.push({
|
|
||||||
path: `${geminiIgnoredCount} file(s)`,
|
|
||||||
reason: 'gemini ignored',
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user