From 69475782eba1832f5a124e1261a002ccec3e6f94 Mon Sep 17 00:00:00 2001 From: abhishekbhakat Date: Thu, 13 Mar 2025 08:12:06 +0000 Subject: [PATCH] Enhance file selection by loading ignore patterns from .gitignore in parent directories --- src/providers/fileSelectionManager.ts | 64 ++++++++++++++++++--------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/src/providers/fileSelectionManager.ts b/src/providers/fileSelectionManager.ts index 3fc0e92..bad80bd 100644 --- a/src/providers/fileSelectionManager.ts +++ b/src/providers/fileSelectionManager.ts @@ -70,22 +70,52 @@ export class FileSelectionManager { return false; } + /** + * Load ignore patterns from .gitignore files in the given directory and its parent directories + * @param directoryPath The directory path to start searching from + * @param workspaceRoot The workspace root path + * @returns An ignore instance with loaded patterns + */ + private loadIgnorePatternsFromDirectory(directoryPath: string, workspaceRoot: string): any { + const ig = ignoreFunc(); + let currentDir = directoryPath; + + // Check for .gitignore in the current directory and all parent directories up to workspace root + while (currentDir.startsWith(workspaceRoot)) { + try { + const gitignorePath = path.join(currentDir, '.gitignore'); + if (fs.existsSync(gitignorePath)) { + const gitignoreContent = fs.readFileSync(gitignorePath, 'utf8'); + ig.add(gitignoreContent); + console.log(`Loaded .gitignore patterns from ${gitignorePath}`); + } + } catch (error) { + console.error(`Error loading .gitignore from ${currentDir}:`, error); + } + + // Stop if we've reached the workspace root + if (currentDir === workspaceRoot) { + break; + } + + // Move up to parent directory + const parentDir = path.dirname(currentDir); + if (parentDir === currentDir) { // Avoid infinite loop if we've reached the root + break; + } + currentDir = parentDir; + } + + return ig; + } + /** * Add a file to the selection if it's not ignored */ addFile(filePath: string): void { const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath || ''; - const ig = ignoreFunc(); - try { - const gitignorePath = path.join(workspaceRoot, '.gitignore'); - if (fs.existsSync(gitignorePath)) { - const gitignoreContent = fs.readFileSync(gitignorePath, 'utf8'); - ig.add(gitignoreContent); - console.log('Loaded .gitignore patterns for file selection'); - } - } catch (error) { - console.error('Error loading .gitignore for file selection:', error); - } + const dirPath = path.dirname(filePath); + const ig = this.loadIgnorePatternsFromDirectory(dirPath, workspaceRoot); if (this.isPathIgnored(filePath, ig, workspaceRoot)) { console.log(`Ignoring file ${filePath} because it or a parent directory is ignored`); @@ -100,17 +130,7 @@ export class FileSelectionManager { */ async addDirectory(dirPath: string): Promise { const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath || ''; - const ig = ignoreFunc(); - try { - const gitignorePath = path.join(workspaceRoot, '.gitignore'); - if (fs.existsSync(gitignorePath)) { - const gitignoreContent = fs.readFileSync(gitignorePath, 'utf8'); - ig.add(gitignoreContent); - console.log('Loaded .gitignore patterns:', gitignoreContent.split('\n').filter(Boolean)); - } - } catch (error) { - console.error('Error loading .gitignore:', error); - } + const ig = this.loadIgnorePatternsFromDirectory(dirPath, workspaceRoot); const relativeDirPath = this.getRelativePath(dirPath, workspaceRoot); const normalizedDirPath = relativeDirPath.split(path.sep).join('/');