|
| 1 | +import * as path from 'path'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | +import type { GitignoreMatcher, GitignoreRule } from '../types/gitignore'; |
| 4 | + |
| 5 | +export class GitignoreService { |
| 6 | + private readonly cache = new Map<string, GitignoreMatcher>(); |
| 7 | + |
| 8 | + async getMatcher(rootPath: string): Promise<GitignoreMatcher> { |
| 9 | + const cached = this.cache.get(rootPath); |
| 10 | + if (cached) { |
| 11 | + return cached; |
| 12 | + } |
| 13 | + |
| 14 | + const matcher = await this.loadGitignore(rootPath); |
| 15 | + this.cache.set(rootPath, matcher); |
| 16 | + return matcher; |
| 17 | + } |
| 18 | + |
| 19 | + private async loadGitignore(rootPath: string): Promise<GitignoreMatcher> { |
| 20 | + const gitignoreUri = vscode.Uri.file(path.join(rootPath, '.gitignore')); |
| 21 | + let content = ''; |
| 22 | + try { |
| 23 | + const data = await vscode.workspace.fs.readFile(gitignoreUri); |
| 24 | + content = data.toString(); |
| 25 | + } catch { |
| 26 | + // .gitignore not found or not readable; ignore silently |
| 27 | + } |
| 28 | + |
| 29 | + const rules = this.parseGitignore(content); |
| 30 | + return { |
| 31 | + ignores: (relativePath: string) => this.matchGitignore(rules, relativePath) |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + private parseGitignore(content: string): GitignoreRule[] { |
| 36 | + const rules: GitignoreRule[] = []; |
| 37 | + const lines = content.split(/\r?\n/); |
| 38 | + for (const rawLine of lines) { |
| 39 | + if (!rawLine || rawLine.trim().length === 0) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + let line = rawLine; |
| 44 | + let negate = false; |
| 45 | + if (line.startsWith('\\#')) { |
| 46 | + line = line.slice(1); |
| 47 | + } else if (line.startsWith('#')) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + if (line.startsWith('!')) { |
| 52 | + negate = true; |
| 53 | + line = line.slice(1); |
| 54 | + } |
| 55 | + |
| 56 | + if (line.trim().length === 0) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + const isDirectory = line.endsWith('/'); |
| 61 | + const pattern = isDirectory ? line.slice(0, -1) : line; |
| 62 | + const regex = this.gitignorePatternToRegex(pattern); |
| 63 | + rules.push({ negate, regex, isDirectory }); |
| 64 | + } |
| 65 | + |
| 66 | + return rules; |
| 67 | + } |
| 68 | + |
| 69 | + private matchGitignore(rules: GitignoreRule[], relativePath: string): boolean { |
| 70 | + const normalized = relativePath.replace(/\\/g, '/'); |
| 71 | + let ignored = false; |
| 72 | + for (const rule of rules) { |
| 73 | + const matches = rule.isDirectory |
| 74 | + ? rule.regex.test(normalized + '/') |
| 75 | + : rule.regex.test(normalized); |
| 76 | + if (matches) { |
| 77 | + ignored = !rule.negate; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return ignored; |
| 82 | + } |
| 83 | + |
| 84 | + private gitignorePatternToRegex(pattern: string): RegExp { |
| 85 | + let pat = pattern.replace(/\\/g, '/'); |
| 86 | + let anchored = false; |
| 87 | + if (pat.startsWith('/')) { |
| 88 | + anchored = true; |
| 89 | + pat = pat.slice(1); |
| 90 | + } |
| 91 | + |
| 92 | + const escaped = pat |
| 93 | + .split(/(\*\*|\*|\?)/g) |
| 94 | + .map((part) => { |
| 95 | + if (part === '**') { |
| 96 | + return '.*'; |
| 97 | + } |
| 98 | + if (part === '*') { |
| 99 | + return '[^/]*'; |
| 100 | + } |
| 101 | + if (part === '?') { |
| 102 | + return '[^/]'; |
| 103 | + } |
| 104 | + return part.replace(/[.+^${}()|[\]\\]/g, '\\$&'); |
| 105 | + }) |
| 106 | + .join(''); |
| 107 | + |
| 108 | + const prefix = anchored ? '^' : '(^|.*/)'; |
| 109 | + return new RegExp(`${prefix}${escaped}(/|$)`); |
| 110 | + } |
| 111 | +} |
0 commit comments