From 8b981dcf36fafeab07089cfde327a6bb24e755de Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 30 Sep 2025 12:13:49 -0500 Subject: [PATCH] Add special handling when viewing projects/ and .git to text_editor_view tool --- src/lib/tools/TextEditorViewTool.test.ts | 45 ++++++++++++++++++++++++ src/lib/tools/TextEditorViewTool.ts | 27 +++++++++----- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/lib/tools/TextEditorViewTool.test.ts b/src/lib/tools/TextEditorViewTool.test.ts index 68441548..03e81b73 100644 --- a/src/lib/tools/TextEditorViewTool.test.ts +++ b/src/lib/tools/TextEditorViewTool.test.ts @@ -23,11 +23,15 @@ class MockFS implements JSRuntimeFS { this.files.set('/projects/project1/package.json', '{"name": "project1"}'); this.files.set('/projects/project1/src/index.ts', 'console.log("project1");'); + this.files.set('/projects/project1/.git/config', '[core]\nrepositoryformatversion = 0'); + this.files.set('/projects/project1/.git/HEAD', 'ref: refs/heads/main'); this.files.set('/projects/project2/package.json', '{"name": "project2"}'); this.files.set('/projects/project2/src/main.ts', 'console.log("project2");'); this.files.set('/project/package.json', '{"name": "test"}'); this.files.set('/project/src/index.ts', 'console.log("hello");'); this.files.set('/project/.gitignore', 'node_modules\n.git\n'); + this.files.set('/project/.git/config', '[core]\nrepositoryformatversion = 0'); + this.files.set('/project/.git/HEAD', 'ref: refs/heads/main'); this.files.set('/tmp/uploaded-file.txt', 'This is an uploaded file'); // Add binary files for testing @@ -284,4 +288,45 @@ describe('TextEditorViewTool', () => { expect(result).not.toContain('index.ts'); expect(result).not.toContain('main.ts'); }); + + it('should show .git directory but not traverse its contents when viewing project root', async () => { + const result = await tool.execute({ path: '/project' }); + + // Should show the .git directory + expect(result).toContain('.git/'); + + // Should show "..." under .git instead of its contents + expect(result).toContain('...'); + + // Should NOT show .git file contents + expect(result).not.toContain('config'); + expect(result).not.toContain('HEAD'); + expect(result).not.toContain('repositoryformatversion'); + + // Should still show other directory contents normally + expect(result).toContain('src/'); + expect(result).toContain('package.json'); + }); + + it('should show .git contents when explicitly viewing .git directory', async () => { + const result = await tool.execute({ path: '/project/.git' }); + + // Should show the .git file contents when explicitly requested + expect(result).toContain('config'); + expect(result).toContain('HEAD'); + + // Should NOT show "..." when explicitly viewing .git + expect(result).not.toContain('...'); + }); + + it('should show .git contents when explicitly viewing .git with relative path', async () => { + const result = await tool.execute({ path: '.git' }); + + // Should show the .git file contents when explicitly requested + expect(result).toContain('config'); + expect(result).toContain('HEAD'); + + // Should NOT show "..." when explicitly viewing .git + expect(result).not.toContain('...'); + }); }); \ No newline at end of file diff --git a/src/lib/tools/TextEditorViewTool.ts b/src/lib/tools/TextEditorViewTool.ts index aadcacca..e8ed1b64 100644 --- a/src/lib/tools/TextEditorViewTool.ts +++ b/src/lib/tools/TextEditorViewTool.ts @@ -59,9 +59,12 @@ export class TextEditorViewTool implements Tool { return tree; } + // Check if we're starting from a .git directory + const isGitRoot = absolutePath.endsWith('/.git') || absolutePath === '.git'; + // If it's a directory, generate a tree-like listing // Note: start_line and end_line are ignored for directories - const tree = await this.generateDirectoryTree(absolutePath); + const tree = await this.generateDirectoryTree(absolutePath, "", 3, 0, isGitRoot); return tree; } @@ -117,6 +120,7 @@ export class TextEditorViewTool implements Tool { prefix: string = "", maxDepth: number = 3, currentDepth: number = 0, + isGitRoot: boolean = false, ): Promise { if (currentDepth >= maxDepth) { return prefix + "...\n"; @@ -142,13 +146,20 @@ export class TextEditorViewTool implements Tool { if (entry.isDirectory()) { result += prefix + connector + entry.name + "/\n"; - const subPath = join(dirPath, entry.name); - result += await this.generateDirectoryTree( - subPath, - nextPrefix, - maxDepth, - currentDepth + 1, - ); + + // Special handling for .git directories - don't traverse unless we started from .git + if (entry.name === '.git' && !isGitRoot) { + result += nextPrefix + "...\n"; + } else { + const subPath = join(dirPath, entry.name); + result += await this.generateDirectoryTree( + subPath, + nextPrefix, + maxDepth, + currentDepth + 1, + isGitRoot, + ); + } } else { result += prefix + connector + entry.name + "\n"; }