perf(worktrees): index removal listing path membership

This commit is contained in:
Neil
2026-11-10 21:54:22 -08:00
parent d30ea72469
commit 7ffdf9514d
2 changed files with 60 additions and 16 deletions
@@ -66,6 +66,42 @@ export function areWorktreePathsEqual(
return canonicalWorktreePath(leftPath, platform) === canonicalWorktreePath(rightPath, platform)
}
export function createWorktreePathMembership(
paths: Iterable<string>,
platform = process.platform
): (pathValue: string) => boolean {
const posixKeys = new Set<string>()
const windowsKeys = new Set<string>()
const relativeKeys = new Set<string>()
const nonPosixWindowsKeys = new Set<string>()
for (const pathValue of paths) {
if (looksLikePosixAbsolutePath(pathValue)) {
posixKeys.add(canonicalWorktreePath(pathValue, platform))
continue
}
const windowsKey = canonicalWorktreePath(pathValue, 'win32')
nonPosixWindowsKeys.add(windowsKey)
if (platform === 'win32' || isWindowsAbsolutePathLike(pathValue)) {
windowsKeys.add(windowsKey)
} else {
relativeKeys.add(canonicalWorktreePath(pathValue, platform))
}
}
// Relative paths compare in Windows syntax only when the other path requires it.
return (pathValue) => {
if (looksLikePosixAbsolutePath(pathValue)) {
return posixKeys.has(canonicalWorktreePath(pathValue, platform))
}
if (platform === 'win32' || isWindowsAbsolutePathLike(pathValue)) {
return nonPosixWindowsKeys.has(canonicalWorktreePath(pathValue, 'win32'))
}
return (
(relativeKeys.size > 0 && relativeKeys.has(canonicalWorktreePath(pathValue, platform))) ||
(windowsKeys.size > 0 && windowsKeys.has(canonicalWorktreePath(pathValue, 'win32')))
)
}
}
export function dedupeWorktreesByPath<T extends { path: string }>(
worktrees: readonly T[],
platform = process.platform
@@ -1,7 +1,7 @@
import { LOCAL_EXECUTION_HOST_ID, type ExecutionHostId } from '../../../shared/execution-host'
import type { GitWorktreeInfo } from '../../../shared/worktree/types'
import {
areWorktreePathsEqual,
createWorktreePathMembership,
canonicalWorktreePath
} from '../../git/worktrees/catalog/worktree-path-comparison'
import { isRecordedCheckout, isUnregisteredRemovalLeftover } from './worktree-removal-leftover'
@@ -30,22 +30,30 @@ export async function withUnregisteredRemovalCheckouts(
.filter((record) => record.repoId === repoId)
.map((record) => [canonicalWorktreePath(record.worktreePath), record] as const)
)
gitWorktrees = gitWorktrees.map((row) => {
const record = failures.get(canonicalWorktreePath(row.path))
if (!record) {
return row
}
if (isRecordedCheckout(row, record)) {
return { ...row, removalError: record.failure?.message }
}
failedWorktreeRemovals.delete(record.worktreeId)
droppedFailure = true
return row
})
if (
failures.size === 0 &&
![...pendingWorktreeRemovals.values()].some((record) => record.repoId === repoId)
) {
return gitWorktrees
}
gitWorktrees =
failures.size === 0
? gitWorktrees
: gitWorktrees.map((row) => {
const record = failures.get(canonicalWorktreePath(row.path))
if (!record) {
return row
}
if (isRecordedCheckout(row, record)) {
return { ...row, removalError: record.failure?.message }
}
failedWorktreeRemovals.delete(record.worktreeId)
droppedFailure = true
return row
})
const isListed = createWorktreePathMembership(gitWorktrees.map((worktree) => worktree.path))
const unlisted = [...pendingWorktreeRemovals.values(), ...failedWorktreeRemovals.values()].filter(
(record) =>
record.repoId === repoId &&
!gitWorktrees.some((worktree) => areWorktreePathsEqual(worktree.path, record.worktreePath))
(record) => record.repoId === repoId && !isListed(record.worktreePath)
)
const leftovers: GitWorktreeInfo[] = []
for (const record of unlisted) {