Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remove ignored files from import #783

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/commit.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "commit": "e064803955604198c6aac7b257efd0ad8503cb73", "version": "0.0.3" }
{ "commit": "cad2b6d1f2b8405e2230277f929d59257fa68bd6" }
55 changes: 54 additions & 1 deletion app/components/chat/ImportFolderButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,65 @@ interface ImportFolderButtonProps {
className?: string;
importChat?: (description: string, messages: Message[]) => Promise<void>;
}
interface FileWithPath extends File {
webkitRelativePath: string;
}

export const ImportFolderButton: React.FC<ImportFolderButtonProps> = ({ className, importChat }) => {
const [isLoading, setIsLoading] = useState(false);

const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
const allFiles = Array.from(e.target.files || []);
let allFiles = Array.from(e.target.files || []) as FileWithPath[];

const gitignoreFiles = allFiles.filter((file) => file.webkitRelativePath.endsWith('.gitignore'));
const gitignorePatterns: string[] = ['.git/**', '.git/*', 'node_modules/**', '.env*'];

// 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}`);
Expand Down
Loading