MCPcopy Create free account
hub / github.com/Noumena-Network/code / normalizeFilePath

Function normalizeFilePath

src/utils/commitAttribution.ts:193–232  ·  view source on GitHub ↗
(filePath: string)

Source from the content-addressed store, hash-verified

191 * Resolves symlinks to handle /tmp vs /private/tmp on macOS.
192 */
193export function normalizeFilePath(filePath: string): string {
194 const fs = getFsImplementation()
195 const cwd = getAttributionRepoRoot()
196
197 if (!isAbsolute(filePath)) {
198 return filePath
199 }
200
201 // Resolve symlinks in both paths for consistent comparison
202 // (e.g., /tmp -> /private/tmp on macOS)
203 let resolvedPath = filePath
204 let resolvedCwd = cwd
205
206 try {
207 resolvedPath = fs.realpathSync(filePath)
208 } catch {
209 // File may not exist yet, use original path
210 }
211
212 try {
213 resolvedCwd = fs.realpathSync(cwd)
214 } catch {
215 // Keep original cwd
216 }
217
218 if (
219 resolvedPath.startsWith(resolvedCwd + sep) ||
220 resolvedPath === resolvedCwd
221 ) {
222 // Normalize to forward slashes so keys match git diff output on Windows
223 return relative(resolvedCwd, resolvedPath).replaceAll(sep, '/')
224 }
225
226 // Fallback: try original comparison
227 if (filePath.startsWith(cwd + sep) || filePath === cwd) {
228 return relative(cwd, filePath).replaceAll(sep, '/')
229 }
230
231 return filePath
232}
233
234/**
235 * Expand a relative path to absolute path.

Callers 5

getFileMtimeFunction · 0.85
trackFileModificationFunction · 0.85
trackFileDeletionFunction · 0.85
trackBulkFileChangesFunction · 0.85

Calls 2

getFsImplementationFunction · 0.85
getAttributionRepoRootFunction · 0.85

Tested by

no test coverage detected