|
| 1 | +import fs from 'node:fs'; |
| 2 | +// oxlint-disable-next-line no-restricted-imports |
| 3 | +import path from 'node:path'; |
| 4 | +import { deserialize, serialize } from 'node:v8'; |
| 5 | +import { version } from '../version.ts'; |
| 6 | +import { debugLog } from './debug.ts'; |
| 7 | +import { isDirectory, isFile } from './fs.ts'; |
| 8 | +import { dirname } from './path.ts'; |
| 9 | + |
| 10 | +interface PerDirIgnores { |
| 11 | + ignores: string[]; |
| 12 | + unignores: string[]; |
| 13 | +} |
| 14 | + |
| 15 | +interface GitignoreCacheEntry { |
| 16 | + /** Relative paths (to cwd) — matches the contract of findAndParseGitignores's return */ |
| 17 | + gitignoreFiles: string[]; |
| 18 | + /** Parallel array: mtimeMs of each gitignoreFiles[i] at cache write */ |
| 19 | + mtimes: number[]; |
| 20 | + ignores: string[]; |
| 21 | + unignores: string[]; |
| 22 | + /** Absolute dir path → cached ignores/unignores for that dir */ |
| 23 | + perDirIgnores: Record<string, PerDirIgnores>; |
| 24 | + /** Sorted workspace dirs, joined with \0 — invalidates when workspace set changes */ |
| 25 | + workspaceDirsKey: string; |
| 26 | +} |
| 27 | + |
| 28 | +export interface CachedGitignoreResult { |
| 29 | + ignores: Set<string>; |
| 30 | + unignores: Set<string>; |
| 31 | + gitignoreFiles: string[]; |
| 32 | + perDirIgnores: Map<string, { ignores: Set<string>; unignores: Set<string> }>; |
| 33 | +} |
| 34 | + |
| 35 | +const CACHE_FILENAME = `gitignore-${version}.cache`; |
| 36 | + |
| 37 | +let cacheFilePath: string | undefined; |
| 38 | +let cache: Map<string, GitignoreCacheEntry> | undefined; |
| 39 | +let isDirty = false; |
| 40 | + |
| 41 | +export const initGitignoreCache = (cacheLocation: string) => { |
| 42 | + cacheFilePath = path.resolve(cacheLocation, CACHE_FILENAME); |
| 43 | + if (isFile(cacheFilePath)) { |
| 44 | + try { |
| 45 | + cache = deserialize(fs.readFileSync(cacheFilePath)); |
| 46 | + } catch { |
| 47 | + debugLog('*', `Error reading gitignore cache from ${cacheFilePath}`); |
| 48 | + cache = new Map(); |
| 49 | + } |
| 50 | + } else { |
| 51 | + cache = new Map(); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +export const isGitignoreCacheEnabled = () => cache !== undefined; |
| 56 | + |
| 57 | +const workspaceDirsKey = (workspaceDirs?: Set<string>): string => { |
| 58 | + if (!workspaceDirs || workspaceDirs.size === 0) return ''; |
| 59 | + return [...workspaceDirs].sort().join('\0'); |
| 60 | +}; |
| 61 | + |
| 62 | +const validateEntry = (entry: GitignoreCacheEntry, wsKey: string, cwd: string): boolean => { |
| 63 | + if (entry.workspaceDirsKey !== wsKey) return false; |
| 64 | + const files = entry.gitignoreFiles; |
| 65 | + const mtimes = entry.mtimes; |
| 66 | + for (let i = 0; i < files.length; i++) { |
| 67 | + const abs = path.isAbsolute(files[i]) ? files[i] : path.resolve(cwd, files[i]); |
| 68 | + try { |
| 69 | + if (fs.statSync(abs).mtimeMs !== mtimes[i]) return false; |
| 70 | + } catch { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + return true; |
| 75 | +}; |
| 76 | + |
| 77 | +export const getCachedGitignore = (cwd: string, workspaceDirs?: Set<string>): CachedGitignoreResult | undefined => { |
| 78 | + if (!cache) return undefined; |
| 79 | + const entry = cache.get(cwd); |
| 80 | + if (!entry) return undefined; |
| 81 | + const wsKey = workspaceDirsKey(workspaceDirs); |
| 82 | + if (!validateEntry(entry, wsKey, cwd)) { |
| 83 | + cache.delete(cwd); |
| 84 | + isDirty = true; |
| 85 | + return undefined; |
| 86 | + } |
| 87 | + const perDirIgnores = new Map<string, { ignores: Set<string>; unignores: Set<string> }>(); |
| 88 | + for (const dir in entry.perDirIgnores) { |
| 89 | + const data = entry.perDirIgnores[dir]; |
| 90 | + perDirIgnores.set(dir, { ignores: new Set(data.ignores), unignores: new Set(data.unignores) }); |
| 91 | + } |
| 92 | + return { |
| 93 | + ignores: new Set(entry.ignores), |
| 94 | + unignores: new Set(entry.unignores), |
| 95 | + gitignoreFiles: entry.gitignoreFiles, |
| 96 | + perDirIgnores, |
| 97 | + }; |
| 98 | +}; |
| 99 | + |
| 100 | +export const setCachedGitignore = ( |
| 101 | + cwd: string, |
| 102 | + workspaceDirs: Set<string> | undefined, |
| 103 | + gitignoreFiles: string[], |
| 104 | + ignores: Set<string>, |
| 105 | + unignores: Set<string>, |
| 106 | + perDirIgnores: Map<string, { ignores: Set<string>; unignores: Set<string> }> |
| 107 | +): void => { |
| 108 | + if (!cache) return; |
| 109 | + const mtimes: number[] = []; |
| 110 | + const validFiles: string[] = []; |
| 111 | + for (const file of gitignoreFiles) { |
| 112 | + const abs = path.isAbsolute(file) ? file : path.resolve(cwd, file); |
| 113 | + try { |
| 114 | + mtimes.push(fs.statSync(abs).mtimeMs); |
| 115 | + validFiles.push(file); |
| 116 | + } catch { |
| 117 | + // File was removed between read and stat; skip rather than poison cache |
| 118 | + } |
| 119 | + } |
| 120 | + const perDir: Record<string, PerDirIgnores> = {}; |
| 121 | + for (const [dir, data] of perDirIgnores) { |
| 122 | + perDir[dir] = { ignores: [...data.ignores], unignores: [...data.unignores] }; |
| 123 | + } |
| 124 | + cache.set(cwd, { |
| 125 | + gitignoreFiles: validFiles, |
| 126 | + mtimes, |
| 127 | + ignores: [...ignores], |
| 128 | + unignores: [...unignores], |
| 129 | + perDirIgnores: perDir, |
| 130 | + workspaceDirsKey: workspaceDirsKey(workspaceDirs), |
| 131 | + }); |
| 132 | + isDirty = true; |
| 133 | +}; |
| 134 | + |
| 135 | +export const flushGitignoreCache = (): void => { |
| 136 | + if (!cache || !cacheFilePath || !isDirty) return; |
| 137 | + try { |
| 138 | + const dir = dirname(cacheFilePath); |
| 139 | + if (!isDirectory(dir)) fs.mkdirSync(dir, { recursive: true }); |
| 140 | + fs.writeFileSync(cacheFilePath, serialize(cache)); |
| 141 | + isDirty = false; |
| 142 | + } catch { |
| 143 | + debugLog('*', `Error writing gitignore cache to ${cacheFilePath}`); |
| 144 | + } |
| 145 | +}; |
0 commit comments