import { resolve } from 'path' import type { Store } from '../persistence' import { getLocalProjectWorktreeGitOptions, type LocalProjectWorktreeGitOptions } from '../project-runtime-git-options' import { splitWorktreeId } from '../../shared/worktree-id' function comparableLocalPath(value: string): string { const normalized = resolve(value) return process.platform === 'win32' ? normalized.toLowerCase() : normalized } function getCandidateLocalWorktreePaths( worktreePath: string, resolvedWorktreePath: string ): Set { return new Set([worktreePath, resolvedWorktreePath].map(comparableLocalPath)) } function hasRegisteredWorktreeMetaForRepo( store: Store, repoId: string, candidatePaths: Set ): boolean { const worktreeMeta = typeof store.getAllWorktreeMeta === 'function' ? store.getAllWorktreeMeta() : {} for (const worktreeId of Object.keys(worktreeMeta)) { const parsed = splitWorktreeId(worktreeId) if (parsed?.repoId === repoId && candidatePaths.has(comparableLocalPath(parsed.worktreePath))) { return true } } return false } export function getLocalGitOptionsForRegisteredWorktree( store: Store, worktreePath: string, resolvedWorktreePath: string ): LocalProjectWorktreeGitOptions { if (typeof store.getProjects !== 'function' || typeof store.getSettings !== 'function') { return {} } const candidatePaths = getCandidateLocalWorktreePaths(worktreePath, resolvedWorktreePath) for (const repo of store.getRepos()) { if (repo.connectionId) { continue } if ( candidatePaths.has(comparableLocalPath(repo.path)) || hasRegisteredWorktreeMetaForRepo(store, repo.id, candidatePaths) ) { // Why: file discovery must use the same resolved runtime as project git, // terminals, and agents even when the worktree path is a Windows path. return getLocalProjectWorktreeGitOptions(store, repo) } } return {} }