feat-filter-ignore #1
@@ -70,22 +70,52 @@ export class FileSelectionManager {
|
|||||||
return false;
|
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
|
* Add a file to the selection if it's not ignored
|
||||||
*/
|
*/
|
||||||
addFile(filePath: string): void {
|
addFile(filePath: string): void {
|
||||||
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath || '';
|
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath || '';
|
||||||
const ig = ignoreFunc();
|
const dirPath = path.dirname(filePath);
|
||||||
try {
|
const ig = this.loadIgnorePatternsFromDirectory(dirPath, workspaceRoot);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.isPathIgnored(filePath, ig, workspaceRoot)) {
|
if (this.isPathIgnored(filePath, ig, workspaceRoot)) {
|
||||||
console.log(`Ignoring file ${filePath} because it or a parent directory is ignored`);
|
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<void> {
|
async addDirectory(dirPath: string): Promise<void> {
|
||||||
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath || '';
|
const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath || '';
|
||||||
const ig = ignoreFunc();
|
const ig = this.loadIgnorePatternsFromDirectory(dirPath, workspaceRoot);
|
||||||
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 relativeDirPath = this.getRelativePath(dirPath, workspaceRoot);
|
const relativeDirPath = this.getRelativePath(dirPath, workspaceRoot);
|
||||||
const normalizedDirPath = relativeDirPath.split(path.sep).join('/');
|
const normalizedDirPath = relativeDirPath.split(path.sep).join('/');
|
||||||
|
|||||||
Reference in New Issue
Block a user