Refine hidden imported worktrees row (#4390)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Brennan Benson
2026-06-01 05:13:09 -07:00
committed by GitHub
co-authored by Orca
parent d3e2ff8b3c
commit 9b5bfbafb7
7 changed files with 243 additions and 99 deletions
@@ -0,0 +1,44 @@
import { normalizeRuntimePathSeparators } from './cross-platform-path'
export const UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH = 'Unknown location'
function trimRuntimePathTrailingSlash(value: string): string {
if (value === '/' || /^[A-Za-z]:\/$/.test(value)) {
return value
}
return value.replace(/\/+$/, '')
}
export function getExternalWorktreeParentPath(worktreePath: string | undefined): string {
if (!worktreePath) {
return UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH
}
const normalized = trimRuntimePathTrailingSlash(normalizeRuntimePathSeparators(worktreePath))
if (!normalized) {
return UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH
}
if (normalized.startsWith('//')) {
const parts = normalized.slice(2).split('/').filter(Boolean)
if (parts.length < 2) {
return UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH
}
if (parts.length === 2) {
return `//${parts[0]}/${parts[1]}`
}
return `//${parts.slice(0, -1).join('/')}`
}
const lastSeparatorIndex = normalized.lastIndexOf('/')
if (lastSeparatorIndex < 0) {
return UNKNOWN_EXTERNAL_WORKTREE_PARENT_PATH
}
if (lastSeparatorIndex === 0) {
return '/'
}
if (/^[A-Za-z]:\/$/.test(normalized)) {
return normalized
}
if (/^[A-Za-z]:\/[^/]+$/.test(normalized)) {
return `${normalized.slice(0, 2)}/`
}
return normalized.slice(0, lastSeparatorIndex)
}