|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Represents a parsed gitignore pattern |
| 6 | + */ |
| 7 | +export interface GitignorePattern { |
| 8 | + pattern: string; |
| 9 | + isNegated: boolean; |
| 10 | + isDirectory: boolean; |
| 11 | + isAbsolute: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Parses a .gitignore file and returns an array of patterns |
| 16 | + * @param gitignorePath Path to the .gitignore file |
| 17 | + * @returns Array of parsed gitignore patterns |
| 18 | + */ |
| 19 | +export function parseGitignoreFile(gitignorePath: string): GitignorePattern[] { |
| 20 | + if (!fs.existsSync(gitignorePath)) { |
| 21 | + return []; |
| 22 | + } |
| 23 | + |
| 24 | + const content = fs.readFileSync(gitignorePath, 'utf8'); |
| 25 | + return parseGitignoreContent(content); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Parses gitignore content and returns an array of patterns |
| 30 | + * @param content Content of the .gitignore file |
| 31 | + * @returns Array of parsed gitignore patterns |
| 32 | + */ |
| 33 | +export function parseGitignoreContent(content: string): GitignorePattern[] { |
| 34 | + return content |
| 35 | + .split('\n') |
| 36 | + .map(line => line.trim()) |
| 37 | + .filter(line => line && !line.startsWith('#')) // Remove empty lines and comments |
| 38 | + .map(line => { |
| 39 | + const isNegated = line.startsWith('!'); |
| 40 | + const pattern = isNegated ? line.substring(1) : line; |
| 41 | + const isDirectory = pattern.endsWith('/'); |
| 42 | + const isAbsolute = pattern.startsWith('/') || pattern.startsWith('./'); |
| 43 | + |
| 44 | + return { |
| 45 | + pattern: isAbsolute ? pattern.substring(pattern.startsWith('./') ? 2 : 1) : pattern, |
| 46 | + isNegated, |
| 47 | + isDirectory, |
| 48 | + isAbsolute |
| 49 | + }; |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Checks if a file or directory matches any of the gitignore patterns |
| 55 | + * @param filePath Path to the file or directory (relative to the directory containing the .gitignore) |
| 56 | + * @param patterns Array of gitignore patterns |
| 57 | + * @param isDirectory Whether the path is a directory |
| 58 | + * @returns True if the file or directory should be ignored |
| 59 | + */ |
| 60 | +export function matchesGitignorePatterns( |
| 61 | + filePath: string, |
| 62 | + patterns: GitignorePattern[], |
| 63 | + isDirectory: boolean |
| 64 | +): boolean { |
| 65 | + // Normalize path for matching |
| 66 | + const normalizedPath = filePath.replace(/\\/g, '/'); |
| 67 | + |
| 68 | + // Start with not ignored, then apply patterns in order |
| 69 | + let ignored = false; |
| 70 | + |
| 71 | + for (const pattern of patterns) { |
| 72 | + // Skip directory-only patterns if this is a file |
| 73 | + if (pattern.isDirectory && !isDirectory) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if (matchesPattern(normalizedPath, pattern, isDirectory)) { |
| 78 | + // If pattern matches, set ignored based on whether it's negated |
| 79 | + ignored = !pattern.isNegated; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return ignored; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Checks if a path matches a gitignore pattern |
| 88 | + * @param normalizedPath Normalized path to check |
| 89 | + * @param pattern Gitignore pattern |
| 90 | + * @param isDirectory Whether the path is a directory |
| 91 | + * @returns True if the path matches the pattern |
| 92 | + */ |
| 93 | +function matchesPattern( |
| 94 | + normalizedPath: string, |
| 95 | + pattern: GitignorePattern, |
| 96 | + isDirectory: boolean |
| 97 | +): boolean { |
| 98 | + const patternStr = pattern.pattern.replace(/\\/g, '/'); |
| 99 | + |
| 100 | + // Handle exact matches |
| 101 | + if (!patternStr.includes('*')) { |
| 102 | + if (pattern.isAbsolute) { |
| 103 | + // For absolute patterns, match from the beginning |
| 104 | + return normalizedPath === patternStr || |
| 105 | + (isDirectory && normalizedPath.startsWith(patternStr + '/')); |
| 106 | + } else { |
| 107 | + // For relative patterns, match anywhere in the path |
| 108 | + return normalizedPath === patternStr || |
| 109 | + normalizedPath.endsWith('/' + patternStr) || |
| 110 | + normalizedPath.includes('/' + patternStr + '/') || |
| 111 | + (isDirectory && ( |
| 112 | + normalizedPath.endsWith('/' + patternStr) || |
| 113 | + normalizedPath.includes('/' + patternStr + '/') |
| 114 | + )); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Handle wildcard patterns |
| 119 | + const regexPattern = patternStr |
| 120 | + .replace(/\./g, '\\.') // Escape dots |
| 121 | + .replace(/\*/g, '.*') // Convert * to .* |
| 122 | + .replace(/\?/g, '.'); // Convert ? to . |
| 123 | + |
| 124 | + const regex = pattern.isAbsolute |
| 125 | + ? new RegExp(`^${regexPattern}$`) |
| 126 | + : new RegExp(`(^|/)${regexPattern}$`); |
| 127 | + |
| 128 | + return regex.test(normalizedPath); |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Collects gitignore patterns from a specific directory |
| 133 | + * @param dirPath Path to the directory |
| 134 | + * @returns Array of gitignore patterns from this directory |
| 135 | + */ |
| 136 | +export function collectDirectoryGitignorePatterns(dirPath: string): GitignorePattern[] { |
| 137 | + const gitignorePath = path.join(dirPath, '.gitignore'); |
| 138 | + if (fs.existsSync(gitignorePath)) { |
| 139 | + return parseGitignoreFile(gitignorePath); |
| 140 | + } |
| 141 | + return []; |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Collects all gitignore patterns that apply to a given directory |
| 146 | + * @param dirPath Path to the directory |
| 147 | + * @returns Array of gitignore patterns that apply to the directory |
| 148 | + */ |
| 149 | +export function collectGitignorePatterns(dirPath: string): GitignorePattern[] { |
| 150 | + const patterns: GitignorePattern[] = []; |
| 151 | + let currentDir = dirPath; |
| 152 | + |
| 153 | + // Collect patterns from all parent directories up to the root |
| 154 | + while (true) { |
| 155 | + const gitignorePath = path.join(currentDir, '.gitignore'); |
| 156 | + if (fs.existsSync(gitignorePath)) { |
| 157 | + const dirPatterns = parseGitignoreFile(gitignorePath); |
| 158 | + patterns.push(...dirPatterns); |
| 159 | + } |
| 160 | + |
| 161 | + const parentDir = path.dirname(currentDir); |
| 162 | + if (parentDir === currentDir) { |
| 163 | + break; // Reached the root |
| 164 | + } |
| 165 | + currentDir = parentDir; |
| 166 | + } |
| 167 | + |
| 168 | + return patterns; |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * Determines if a file or directory should be excluded based on gitignore patterns |
| 173 | + * @param fullPath Full path to the file or directory |
| 174 | + * @param baseDir Base directory for relative path calculation |
| 175 | + * @param patterns Gitignore patterns to check against |
| 176 | + * @param isDirectory Whether the path is a directory |
| 177 | + * @returns True if the file or directory should be excluded |
| 178 | + */ |
| 179 | +export function shouldExcludeByGitignore( |
| 180 | + fullPath: string, |
| 181 | + baseDir: string, |
| 182 | + patterns: GitignorePattern[], |
| 183 | + isDirectory: boolean |
| 184 | +): boolean { |
| 185 | + // Calculate path relative to the base directory |
| 186 | + const relativePath = path.relative(baseDir, fullPath).replace(/\\/g, '/'); |
| 187 | + |
| 188 | + // Check if the path matches any gitignore pattern |
| 189 | + return matchesGitignorePatterns(relativePath, patterns, isDirectory); |
| 190 | +} |
0 commit comments