diff --git a/src/main/git/worktrees/catalog/worktree-path-comparison.ts b/src/main/git/worktrees/catalog/worktree-path-comparison.ts index f4e3ab1cb09..97bc5884b1a 100644 --- a/src/main/git/worktrees/catalog/worktree-path-comparison.ts +++ b/src/main/git/worktrees/catalog/worktree-path-comparison.ts @@ -66,6 +66,42 @@ export function areWorktreePathsEqual( return canonicalWorktreePath(leftPath, platform) === canonicalWorktreePath(rightPath, platform) } +export function createWorktreePathMembership( + paths: Iterable, + platform = process.platform +): (pathValue: string) => boolean { + const posixKeys = new Set() + const windowsKeys = new Set() + const relativeKeys = new Set() + const nonPosixWindowsKeys = new Set() + 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( worktrees: readonly T[], platform = process.platform diff --git a/src/main/workspaces/removal/worktree-removal-listing.ts b/src/main/workspaces/removal/worktree-removal-listing.ts index 6ff79174512..2cc35fb3acc 100644 --- a/src/main/workspaces/removal/worktree-removal-listing.ts +++ b/src/main/workspaces/removal/worktree-removal-listing.ts @@ -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) {