diff --git a/src/main/ipc/filesystem-allowed-roots.test.ts b/src/main/ipc/filesystem-allowed-roots.test.ts index f94c99c5fdb..2ba6eaec367 100644 --- a/src/main/ipc/filesystem-allowed-roots.test.ts +++ b/src/main/ipc/filesystem-allowed-roots.test.ts @@ -8,6 +8,7 @@ import { listRepoWorktreeGraph } from '../repo-worktrees' import type * as ProjectGroupsModule from '../../shared/project-groups' import { buildProjectGroupChildIndex, getProjectGroupSubtreeIds } from '../../shared/project-groups' import { isPathInsideOrEqual } from '../../shared/cross-platform-path' +import type * as CrossPlatformPathModule from '../../shared/cross-platform-path' import { getWorktreeMirrorDistro } from '../project-runtime-git-options' import type { FolderWorkspace } from '../../shared/folder-workspace-types' import type { ProjectGroup } from '../../shared/project-group-types' @@ -32,6 +33,13 @@ vi.mock('../../shared/project-groups', async () => { } }) +vi.mock('../../shared/cross-platform-path', async () => { + const actual = await vi.importActual( + '../../shared/cross-platform-path' + ) + return { ...actual, isPathInsideOrEqual: vi.fn(actual.isPathInsideOrEqual) } +}) + type StoreFixture = { repos: Repo[] projects: Project[] @@ -257,6 +265,42 @@ beforeEach(() => { }) describe('getAllowedRoots', () => { + it('stops scanning repositories when a local candidate settles each folder scope', () => { + const fixture: StoreFixture = { + repos: Array.from({ length: 1_000 }, (_, index) => + makeRepo({ id: `repo-${index}`, path: `/folders/root/repo-${index}` }) + ), + projects: [], + projectGroups: [], + folderWorkspaces: Array.from({ length: 100 }, (_, index) => + makeWorkspace({ id: `folder-${index}`, folderPath: '/folders/root' }) + ) + } + const { store } = makeCountingStore(fixture) + vi.mocked(isPathInsideOrEqual).mockClear() + const actual = getAllowedRoots(store) + expect(isPathInsideOrEqual).toHaveBeenCalledTimes(100) + vi.mocked(isPathInsideOrEqual).mockClear() + expect(actual).toEqual(referenceAllowedRoots(store)) + expect(isPathInsideOrEqual).toHaveBeenCalledTimes(100_000) + }) + + it('preserves empty, remote-only, mixed and explicit remote folder scopes in any repo order', () => { + const fixture = makeMixedFixture() + fixture.repos.push( + makeRepo({ + id: 'local-in-remote-group', + path: '/local/mixed', + projectGroupId: 'group-remote' + }) + ) + for (let index = 0; index < fixture.repos.length; index += 1) { + fixture.repos.push(fixture.repos.shift()!) + const { store } = makeCountingStore(fixture) + expect(getAllowedRoots(store)).toEqual(referenceAllowedRoots(store)) + } + }) + it('produces the same roots as the pre-change implementation', () => { const { store } = makeCountingStore(makeMixedFixture()) diff --git a/src/main/ipc/filesystem-allowed-roots.ts b/src/main/ipc/filesystem-allowed-roots.ts index cef249430c6..fb4e9854c2e 100644 --- a/src/main/ipc/filesystem-allowed-roots.ts +++ b/src/main/ipc/filesystem-allowed-roots.ts @@ -27,20 +27,6 @@ export function getLocalRepos(store: Store) { return filterLocalRepos(store.getRepos()) } -function getFolderScopeCandidateRepos( - folderPath: string, - projectGroupId: string, - childGroupIndex: ProjectGroupChildIndex, - repos: readonly Repo[] -): Repo[] { - const groupIds = collectProjectGroupSubtreeIds(childGroupIndex, projectGroupId) - return repos.filter( - (repo) => - (typeof repo.projectGroupId === 'string' && groupIds.has(repo.projectGroupId)) || - isPathInsideOrEqual(folderPath, repo.path) - ) -} - function isRemoteOnlyFolderScope( folderPath: string, projectGroupId: string, @@ -51,13 +37,21 @@ function isRemoteOnlyFolderScope( if (connectionId) { return true } - const candidates = getFolderScopeCandidateRepos( - folderPath, - projectGroupId, - childGroupIndex, - repos - ) - return candidates.length > 0 && candidates.every((repo) => Boolean(repo.connectionId)) + const groupIds = collectProjectGroupSubtreeIds(childGroupIndex, projectGroupId) + let hasRemoteCandidate = false + for (const repo of repos) { + if ( + (typeof repo.projectGroupId === 'string' && groupIds.has(repo.projectGroupId)) || + isPathInsideOrEqual(folderPath, repo.path) + ) { + // One local candidate settles the scope without scanning the remaining repositories. + if (!repo.connectionId) { + return false + } + hasRemoteCandidate = true + } + } + return hasRemoteCandidate } function getFolderWorkspaceConnectionId(