mirror of
https://github.com/stablyai/orca.git
synced 2026-09-28 08:02:43 +00:00
* perf(git): normalize tracked discard paths once per operation * chore(git): drop the now-dead tracked-pathspec re-export
38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
function normalizeGitPathForCompare(filePath: string): string {
|
|
return filePath.replace(/\\/g, '/').replace(/\/+$/, '')
|
|
}
|
|
|
|
function createTrackedPathSpecMatcher(
|
|
trackedPaths: readonly string[]
|
|
): (filePath: string) => boolean {
|
|
// Normalize lazily so selecting a directory still stops at its first tracked descendant.
|
|
const normalizedTrackedPaths: string[] = []
|
|
return (filePath) => {
|
|
const normalized = normalizeGitPathForCompare(filePath)
|
|
const descendantPrefix = `${normalized}/`
|
|
return trackedPaths.some((trackedPath, index) => {
|
|
const normalizedTracked = (normalizedTrackedPaths[index] ??=
|
|
normalizeGitPathForCompare(trackedPath))
|
|
return normalizedTracked === normalized || normalizedTracked.startsWith(descendantPrefix)
|
|
})
|
|
}
|
|
}
|
|
|
|
export function isTrackedPathSpec(filePath: string, trackedPaths: readonly string[]): boolean {
|
|
return createTrackedPathSpecMatcher(trackedPaths)(filePath)
|
|
}
|
|
|
|
export function partitionTrackedPathSpecs(
|
|
filePaths: readonly string[],
|
|
trackedPathSpecs: readonly string[]
|
|
): { trackedPaths: string[]; untrackedPaths: string[] } {
|
|
const isTracked = createTrackedPathSpecMatcher(trackedPathSpecs)
|
|
const trackedPaths: string[] = []
|
|
const untrackedPaths: string[] = []
|
|
// Keep original spellings, duplicates and order: these arrays select restore versus clean.
|
|
for (const filePath of filePaths) {
|
|
;(isTracked(filePath) ? trackedPaths : untrackedPaths).push(filePath)
|
|
}
|
|
return { trackedPaths, untrackedPaths }
|
|
}
|