Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion src/main/ipc/repos-remote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { tmpdir } from 'os'
import { join } from 'path'
import type * as RepoModule from '../git/repo'
import { DEFAULT_REPO_BADGE_COLOR } from '../../shared/constants'
import { isGitRepo } from '../git/repo'
import { getBaseRefDefault, isGitRepo } from '../git/repo'

const {
handleMock,
Expand Down Expand Up @@ -2226,6 +2226,30 @@ describe('repos:getBaseRefDefault envelope', () => {
expect(result.remoteCount).toBe(1)
})

it('uses repoPath to disambiguate duplicate repo ids for local defaults', async () => {
const selectedRepo = {
id: 'r1',
path: '/selected/repo',
kind: 'git'
}
mockStore.getRepo.mockReturnValue({
id: 'r1',
path: '/wrong/repo',
kind: 'git'
})
mockStore.getRepos.mockReturnValue([
{ id: 'r1', path: '/wrong/repo', kind: 'git' },
selectedRepo
])

await handlers.get('repos:getBaseRefDefault')!(null, {
repoId: 'r1',
repoPath: selectedRepo.path
})

expect(getBaseRefDefault).toHaveBeenLastCalledWith(selectedRepo.path)
})

// Why: the SSH handler resolves default-ref and remote-count in parallel
// (Promise.all) so the order of calls into provider.exec is not stable.
// Dispatch on argv instead of `mockResolvedValueOnce` chains so tests remain
Expand Down Expand Up @@ -2283,6 +2307,39 @@ describe('repos:getBaseRefDefault envelope', () => {
expect(result.remoteCount).toBe(2)
})

it('uses repoPath to disambiguate duplicate repo ids for SSH defaults', async () => {
mockGitProvider.exec = vi.fn().mockImplementation(
dispatchExec([
{
match: isSymbolicRef,
respond: () => Promise.resolve({ stdout: 'refs/remotes/origin/main\n', stderr: '' })
},
{
match: isRemoteList,
respond: () => Promise.resolve({ stdout: 'origin\n', stderr: '' })
}
])
)
mockStore.getRepo.mockReturnValue({
id: 'r1',
path: '/wrong/repo',
kind: 'git'
})
mockStore.getRepos.mockReturnValue([
{ id: 'r1', path: '/wrong/repo', kind: 'git' },
{ id: 'r1', path: '/remote/selected', connectionId: 'wrong-conn', kind: 'git' },
{ id: 'r1', path: '/remote/selected', connectionId: 'conn-1', kind: 'git' }
])

await handlers.get('repos:getBaseRefDefault')!(null, {
repoId: 'r1',
repoPath: '/remote/selected',
executionHostId: 'ssh:conn-1'
})

expect(mockGitProvider.exec).toHaveBeenCalledWith(expect.any(Array), '/remote/selected')
})

it('returns defaultBaseRef even when remote-count lookup fails', async () => {
mockGitProvider.exec = vi.fn().mockImplementation(
dispatchExec([
Expand Down Expand Up @@ -2427,6 +2484,39 @@ describe('repos:searchBaseRefs SSH relay', () => {
expect(argv).toContain('refs/remotes/**/**/**')
})

it('uses repoPath to disambiguate duplicate repo ids for SSH searches', async () => {
mockGitProvider.exec = vi.fn().mockImplementation((argv: string[]) => {
if (argv[0] === 'remote') {
return Promise.resolve({ stdout: 'origin\n', stderr: '' })
}
return Promise.resolve({
stdout: 'refs/remotes/origin/main\0origin/main',
stderr: ''
})
})
mockStore.getRepo.mockReturnValue({
id: 'r1',
path: '/wrong/repo',
kind: 'git'
})
mockStore.getRepos.mockReturnValue([
{ id: 'r1', path: '/wrong/repo', kind: 'git' },
{ id: 'r1', path: '/remote/selected', connectionId: 'wrong-conn', kind: 'git' },
{ id: 'r1', path: '/remote/selected', connectionId: 'conn-1', kind: 'git' }
])

const result = await handlers.get('repos:searchBaseRefs')!(null, {
repoId: 'r1',
repoPath: '/remote/selected',
executionHostId: 'ssh:conn-1',
query: 'main'
})

expect(result).toEqual(['origin/main'])
expect(mockGitProvider.exec).toHaveBeenCalledWith(['remote'], '/remote/selected')
expect(mockGitProvider.exec).toHaveBeenCalledWith(expect.any(Array), '/remote/selected')
})

it('sanitizes glob metacharacter-only queries into the empty-query branch list', async () => {
mockGitProvider.exec = vi.fn().mockResolvedValue({ stdout: '', stderr: '' })
mockStore.getRepo.mockReturnValue({
Expand Down
44 changes: 39 additions & 5 deletions src/main/ipc/repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ import type { RepoMethod } from '../../shared/telemetry-events'
import { detectRepoIconAndUpstream } from '../repo-icon-autodetect'
import { enrichMissingRepoGitRemoteIdentities } from '../repo-git-remote-identity-enrichment'
import { getProjectHostSetupForRepo } from '../../shared/project-host-setup-projection'
import { normalizeExecutionHostId, parseExecutionHostId } from '../../shared/execution-host'
import {
getRepoExecutionHostId,
normalizeExecutionHostId,
parseExecutionHostId
} from '../../shared/execution-host'
import { joinRemotePath } from '../ssh/ssh-remote-platform'
import {
assertFolderWorkspacePathUsable,
Expand Down Expand Up @@ -2325,8 +2329,8 @@ export function registerRepoHandlers(mainWindow: BrowserWindow, store: Store): v

ipcMain.handle(
'repos:getBaseRefDefault',
async (_event, args: { repoId: string }): Promise<BaseRefDefaultResult> => {
const repo = store.getRepo(args.repoId)
async (_event, args: BaseRefRepoLookupArgs): Promise<BaseRefDefaultResult> => {
const repo = resolveBaseRefRepo(store, args)
if (!repo || isFolderRepo(repo)) {
// Why: folder-mode repos have no git state to resolve a base ref from.
// Return null + 0 so the renderer can decline to use a fabricated default
Expand Down Expand Up @@ -2417,9 +2421,9 @@ export function registerRepoHandlers(mainWindow: BrowserWindow, store: Store): v

async function searchBaseRefDetailsForRepo(
store: Store,
args: { repoId: string; query: string; limit?: number }
args: BaseRefSearchArgs
): Promise<BaseRefSearchResult[]> {
const repo = store.getRepo(args.repoId)
const repo = resolveBaseRefRepo(store, args)
if (!repo || isFolderRepo(repo)) {
return []
}
Expand Down Expand Up @@ -2496,6 +2500,36 @@ async function searchBaseRefDetailsForRepo(
return searchBaseRefDetails(repo.path, args.query, limit)
}

type BaseRefRepoLookupArgs = {
repoId: string
repoPath?: string
executionHostId?: string | null
}

type BaseRefSearchArgs = BaseRefRepoLookupArgs & {
query: string
limit?: number
}

function resolveBaseRefRepo(store: Store, args: BaseRefRepoLookupArgs): Repo | undefined {
const repoPath = args.repoPath?.trim()
const executionHostId = args.executionHostId?.trim()
if (!repoPath && !executionHostId) {
return store.getRepo(args.repoId)
}
// Why: folder-workspace Source Control can render duplicate repo ids across
// hosts, sometimes with identical checkout paths. Match every supplied
// discriminator so local/SSH IPC base-ref lookups stay on the embedded child.
return store
.getRepos()
.find(
(repo) =>
repo.id === args.repoId &&
(!repoPath || repo.path === repoPath) &&
(!executionHostId || getRepoExecutionHostId(repo) === executionHostId)
)
}

function notifyReposChanged(mainWindow: BrowserWindow): void {
if (!mainWindow.isDestroyed()) {
mainWindow.webContents.send('repos:changed')
Expand Down
16 changes: 14 additions & 2 deletions src/preload/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,10 +823,22 @@ export type PreloadApi = {
getDefaultCreateProjectParent: () => Promise<string>
onCloneProgress: (callback: (data: { phase: string; percent: number }) => void) => () => void
getGitUsername: (args: { repoId: string }) => Promise<string>
getBaseRefDefault: (args: { repoId: string }) => Promise<BaseRefDefaultResult>
searchBaseRefs: (args: { repoId: string; query: string; limit?: number }) => Promise<string[]>
getBaseRefDefault: (args: {
repoId: string
repoPath?: string
executionHostId?: string | null
}) => Promise<BaseRefDefaultResult>
searchBaseRefs: (args: {
repoId: string
repoPath?: string
executionHostId?: string | null
query: string
limit?: number
}) => Promise<string[]>
searchBaseRefDetails: (args: {
repoId: string
repoPath?: string
executionHostId?: string | null
query: string
limit?: number
}) => Promise<BaseRefSearchResult[]>
Expand Down
18 changes: 14 additions & 4 deletions src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,24 @@ const api = {
getGitUsername: (args: { repoId: string }): Promise<string> =>
ipcRenderer.invoke('repos:getGitUsername', args),

getBaseRefDefault: (args: { repoId: string }): Promise<BaseRefDefaultResult> =>
ipcRenderer.invoke('repos:getBaseRefDefault', args),
getBaseRefDefault: (args: {
repoId: string
repoPath?: string
executionHostId?: string | null
}): Promise<BaseRefDefaultResult> => ipcRenderer.invoke('repos:getBaseRefDefault', args),

searchBaseRefs: (args: { repoId: string; query: string; limit?: number }): Promise<string[]> =>
ipcRenderer.invoke('repos:searchBaseRefs', args),
searchBaseRefs: (args: {
repoId: string
repoPath?: string
executionHostId?: string | null
query: string
limit?: number
}): Promise<string[]> => ipcRenderer.invoke('repos:searchBaseRefs', args),

searchBaseRefDetails: (args: {
repoId: string
repoPath?: string
executionHostId?: string | null
query: string
limit?: number
}): Promise<BaseRefSearchResult[]> => ipcRenderer.invoke('repos:searchBaseRefDetails', args),
Expand Down
Loading
Loading