From cad2b6d1f2b8405e2230277f929d59257fa68bd6 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Mon, 16 Dec 2024 23:35:05 +0100 Subject: [PATCH 1/2] feat: Enhance ImportFolderButton to support .gitignore file processing - Added functionality to read and process .gitignore files from selected folders. - Implemented logic to filter out files based on patterns defined in .gitignore. - Improved file selection by ignoring specified directories and files during import. --- app/commit.json | 2 +- app/components/chat/ImportFolderButton.tsx | 61 +++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/app/commit.json b/app/commit.json index 832678f8d..2d7a657da 100644 --- a/app/commit.json +++ b/app/commit.json @@ -1 +1 @@ -{ "commit": "1e72d52278730f7d22448be9d5cf2daf12559486", "version": "0.0.2" } +{ "commit": "0ee373629789f01fb9f54f6747735b51a94a5562" } diff --git a/app/components/chat/ImportFolderButton.tsx b/app/components/chat/ImportFolderButton.tsx index c75200e9b..0c0685034 100644 --- a/app/components/chat/ImportFolderButton.tsx +++ b/app/components/chat/ImportFolderButton.tsx @@ -9,12 +9,71 @@ interface ImportFolderButtonProps { className?: string; importChat?: (description: string, messages: Message[]) => Promise; } +interface FileWithPath extends File { + webkitRelativePath: string; +} export const ImportFolderButton: React.FC = ({ className, importChat }) => { const [isLoading, setIsLoading] = useState(false); const handleFileChange = async (e: React.ChangeEvent) => { - const allFiles = Array.from(e.target.files || []); + let allFiles = Array.from(e.target.files || []) as FileWithPath[]; + + // Get all .gitignore files in any folder + const gitignoreFiles = allFiles.filter((file) => file.webkitRelativePath.endsWith('.gitignore')); + const gitignorePatterns: string[] = [ + '.git/**', + '.git/*', + 'node_modules/**', // Example: also ignore node_modules + '.env*', // Example: ignore environment files + ]; + + // Process all .gitignore files + for (const gitignoreFile of gitignoreFiles) { + const gitignoreContent = await gitignoreFile.text(); + const gitignorePath = gitignoreFile.webkitRelativePath; + const gitignoreDir = gitignorePath.substring(0, gitignorePath.lastIndexOf('/')); + + const patterns = gitignoreContent + .split('\n') + .filter((line: string) => line.trim() !== '' && !line.startsWith('#')) + .map((pattern: string) => { + // Make pattern relative to the .gitignore location + if (gitignoreDir) { + return `${gitignoreDir}/${pattern}`; + } + + return pattern; + }); + + gitignorePatterns.push(...patterns); + } + + const isIgnored = (filePath: string) => { + return gitignorePatterns.some((pattern) => { + // Convert glob pattern to regex + const regexPattern = pattern + .replace(/\./g, '\\.') + .replace(/\*\*/g, '.*') + .replace(/\*/g, '[^/]*') + .replace(/\?/g, '.'); + const regex = new RegExp(`^${regexPattern}$`); + + // Test both the full path and path segments + const pathSegments = filePath.split('/'); + + return ( + regex.test(filePath) || + pathSegments.some((_, index) => { + const partialPath = pathSegments.slice(0, index + 1).join('/'); + return regex.test(partialPath); + }) + ); + }); + }; + + const nonIgnoredFiles = allFiles.filter((file) => !isIgnored(file.webkitRelativePath)); + allFiles = nonIgnoredFiles; if (allFiles.length > MAX_FILES) { const error = new Error(`Too many files: ${allFiles.length}`); From 856e50529f29038fb53b8c6ff54f6f3a30d0dbab Mon Sep 17 00:00:00 2001 From: Jeremy Date: Mon, 16 Dec 2024 23:36:03 +0100 Subject: [PATCH 2/2] fix:format --- app/commit.json | 2 +- app/components/chat/ImportFolderButton.tsx | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/app/commit.json b/app/commit.json index 2d7a657da..abb7ef5eb 100644 --- a/app/commit.json +++ b/app/commit.json @@ -1 +1 @@ -{ "commit": "0ee373629789f01fb9f54f6747735b51a94a5562" } +{ "commit": "cad2b6d1f2b8405e2230277f929d59257fa68bd6" } diff --git a/app/components/chat/ImportFolderButton.tsx b/app/components/chat/ImportFolderButton.tsx index 0c0685034..af6369cdb 100644 --- a/app/components/chat/ImportFolderButton.tsx +++ b/app/components/chat/ImportFolderButton.tsx @@ -19,14 +19,8 @@ export const ImportFolderButton: React.FC = ({ classNam const handleFileChange = async (e: React.ChangeEvent) => { let allFiles = Array.from(e.target.files || []) as FileWithPath[]; - // Get all .gitignore files in any folder const gitignoreFiles = allFiles.filter((file) => file.webkitRelativePath.endsWith('.gitignore')); - const gitignorePatterns: string[] = [ - '.git/**', - '.git/*', - 'node_modules/**', // Example: also ignore node_modules - '.env*', // Example: ignore environment files - ]; + const gitignorePatterns: string[] = ['.git/**', '.git/*', 'node_modules/**', '.env*']; // Process all .gitignore files for (const gitignoreFile of gitignoreFiles) {