Files
orca/src/main/runtime/runtime-relative-paths.ts
T
Neil 58bd15fa3f Fix ripgrep result completeness, filename handling, and search errors (#25156)
* Preserve ripgrep search results, filename identity, and failure diagnostics

* Fix adversarial Unicode and Explorer filename findings

* Register search failure localization fallback

* Preserve host filename identity through document and watcher consumers
2026-10-04 01:27:30 -07:00

36 lines
1.3 KiB
TypeScript

import { posix, win32 } from 'node:path'
import { isWindowsAbsolutePathLike } from '../../shared/cross-platform-path'
export function joinWorktreeRelativePath(rootPath: string, relativePath: string): string {
const normalizedRelativePath = isWindowsAbsolutePathLike(rootPath)
? relativePath.replace(/\\/g, '/')
: relativePath
if (isWindowsAbsolutePathLike(rootPath)) {
return win32.join(rootPath.replace(/\//g, '\\'), ...normalizedRelativePath.split('/'))
}
return posix.join(rootPath, ...normalizedRelativePath.split('/'))
}
export function normalizeRuntimeRelativePath(relativePath: string, rootPath?: string): string {
const path =
rootPath !== undefined && !isWindowsAbsolutePathLike(rootPath)
? relativePath
: relativePath.replace(/\\/g, '/')
const normalized = path.replace(/\/+$/, '')
if (normalized === '') {
return ''
}
if (!isSafeRuntimeRelativePath(normalized)) {
throw new Error('invalid_relative_path')
}
return normalized
}
function isSafeRuntimeRelativePath(relativePath: string): boolean {
if (relativePath.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(relativePath)) {
return false
}
const parts = relativePath.split('/')
return parts.every((part) => part !== '' && part !== '.' && part !== '..')
}