Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

perf(git): batch per-remote exact-ref probes into one for-each-ref call (#36156)

Neilbb5f08e02c6e
diff --git a/src/main/git/exact-ref-probe.test.ts b/src/main/git/exact-ref-probe.test.ts
index 431ad918543..43c72c3ec1b 100644
--- a/src/main/git/exact-ref-probe.test.ts
+++ b/src/main/git/exact-ref-probe.test.ts
@@ -1,5 +1,10 @@
import { describe, expect, it, vi } from 'vitest'
-import { isShowRefNoMatchError, probeAnyExactRef, probeExactRefs } from './exact-ref-probe'
+import {
+ isShowRefNoMatchError,
+ probeAnyExactRef,
+ probeExactRefs,
+ probeExactRefsViaForEachRef
+} from './exact-ref-probe'
describe('isShowRefNoMatchError', () => {
it('accepts only the numeric Git no-match exit status', () => {
@@ -127,3 +132,47 @@ describe('probeAnyExactRef', () => {
expect(result.absentRefs).toEqual(['refs/remotes/origin/main'])
})
})
+
+describe('probeExactRefsViaForEachRef', () => {
+ const present = 'refs/remotes/origin/main'
+ const descendantOnly = 'refs/remotes/fork/main'
+ const invalid = 'refs/remotes/origin/bad*'
+
+ it('answers all candidates from one child and ignores descendant matches', async () => {
+ const runGit = vi.fn(async () => ({
+ stdout: `${present}\r\n${descendantOnly}/sub\n`
+ }))
+
+ await expect(
+ probeExactRefsViaForEachRef(runGit, [present, descendantOnly, invalid, present])
+ ).resolves.toEqual({
+ presentRefs: [present],
+ absentRefs: [descendantOnly],
+ unknownRefs: [invalid]
+ })
+ expect(runGit).toHaveBeenCalledTimes(1)
+ expect(runGit).toHaveBeenCalledWith([
+ 'for-each-ref',
+ '--format=%(refname)',
+ '--',
+ present,
+ descendantOnly
+ ])
+ })
+
+ it('falls back to per-ref probes when the batch fails', async () => {
+ const runGit = vi.fn(async (argv: string[]) => {
+ if (argv[0] === 'for-each-ref' || argv.at(-1) === descendantOnly) {
+ throw Object.assign(new Error('failed'), { code: 1 })
+ }
+ return { stdout: '' }
+ })
+
+ await expect(probeExactRefsViaForEachRef(runGit, [present, descendantOnly])).resolves.toEqual({
+ presentRefs: [present],
+ absentRefs: [descendantOnly],
+ unknownRefs: []
+ })
+ expect(runGit).toHaveBeenCalledTimes(3)
+ })
+})
diff --git a/src/main/git/exact-ref-probe.ts b/src/main/git/exact-ref-probe.ts
index 879e388113a..cf18d9d1370 100644
--- a/src/main/git/exact-ref-probe.ts
+++ b/src/main/git/exact-ref-probe.ts
@@ -86,6 +86,35 @@ export async function probeExactRefs(
}
}
+/** Answer every candidate from one stdin-free child; any failure falls back to per-ref probes. */
+export async function probeExactRefsViaForEachRef(
+ runGit: ExactRefProbeExec,
+ refs: readonly string[],
+ options: ExactRefProbeExecOptions = {}
+): Promise<ExactRefProbeSetResult> {
+ const uniqueRefs = [...new Set(refs)]
+ const safeRefs = uniqueRefs.filter((ref) => isSafeGitRefName(ref))
+ if (safeRefs.length === 0) {
+ return probeExactRefs(runGit, uniqueRefs, options)
+ }
+ let stdout: string
+ try {
+ const argv = ['for-each-ref', '--format=%(refname)', '--', ...safeRefs]
+ const forwardedOptions = commandOptions(options)
+ ;({ stdout } = await (forwardedOptions ? runGit(argv, forwardedOptions) : runGit(argv)))
+ } catch {
+ return probeExactRefs(runGit, uniqueRefs, options)
+ }
+ // Patterns also match descendants (refs/x/main/sub), so only exact lines count.
+ const listed = new Set(stdout.split('\n').map((line) => line.trim()))
+ const safe = new Set(safeRefs)
+ return {
+ presentRefs: safeRefs.filter((ref) => listed.has(ref)),
+ absentRefs: safeRefs.filter((ref) => !listed.has(ref)),
+ unknownRefs: uniqueRefs.filter((ref) => !safe.has(ref))
+ }
+}
+
/** Stop scheduling exact lookups once any requested ref is present. */
export async function probeAnyExactRef(
runGit: ExactRefProbeExec,
diff --git a/src/main/source-control/hosted-review-creation-eligibility.test.ts b/src/main/source-control/hosted-review-creation-eligibility.test.ts
index d5a7ba70380..89303217102 100644
--- a/src/main/source-control/hosted-review-creation-eligibility.test.ts
+++ b/src/main/source-control/hosted-review-creation-eligibility.test.ts
@@ -246,29 +246,31 @@ describe('getHostedReviewCreationEligibility', () => {
})
})
- it('uses exact remote-ref probes instead of a wildcard ref scan', async () => {
+ it('probes every remote candidate in one exact for-each-ref call', async () => {
gitExecFileAsyncMock.mockImplementation(async (args: string[]) => {
if (args[0] === 'remote') {
return { stdout: 'origin\nfork\n', stderr: '' }
}
- if (args[0] === 'show-ref') {
- return args.at(-1) === 'refs/remotes/fork/main'
- ? { stdout: '', stderr: '' }
- : Promise.reject(Object.assign(new Error('missing ref'), { code: 1 }))
+ if (args[0] === 'for-each-ref') {
+ return { stdout: 'refs/remotes/fork/main\n', stderr: '' }
}
throw Object.assign(new Error('missing ref'), { code: 1 })
})
await expect(baseRefExistsOnRemote('main', '/repo', 'local')).resolves.toBe(true)
- expect(gitExecFileAsyncMock.mock.calls.map(([args]) => args)).toContainEqual([
- 'show-ref',
- '--verify',
- '--quiet',
- '--',
- 'refs/remotes/fork/main'
+ expect(gitExecFileAsyncMock.mock.calls.map(([args]) => args)).toEqual([
+ ['remote'],
+ [
+ 'for-each-ref',
+ '--format=%(refname)',
+ '--',
+ 'refs/remotes/origin/main',
+ 'refs/remotes/upstream/main',
+ 'refs/remotes/fork/main'
+ ]
])
- expect(gitExecFileAsyncMock).toHaveBeenCalledWith(
- ['show-ref', '--verify', '--quiet', '--', 'refs/remotes/fork/main'],
+ expect(gitExecFileAsyncMock).toHaveBeenLastCalledWith(
+ expect.any(Array),
expect.objectContaining({ maxBuffer: 10 * 1024 * 1024 })
)
})
diff --git a/src/main/source-control/hosted-review-creation-git-state.ts b/src/main/source-control/hosted-review-creation-git-state.ts
index 3617144ac3c..82fd9adc537 100644
--- a/src/main/source-control/hosted-review-creation-git-state.ts
+++ b/src/main/source-control/hosted-review-creation-git-state.ts
@@ -6,7 +6,7 @@ import {
import { isSafeGitRefName } from '../../shared/git-status-upstream-ref'
import type { GitUpstreamStatus } from '../../shared/git-status-types'
import { gitExecFileAsync } from '../github/gh-utils'
-import { isShowRefNoMatchError, probeAnyExactRef } from '../git/exact-ref-probe'
+import { isShowRefNoMatchError, probeExactRefsViaForEachRef } from '../git/exact-ref-probe'
import { gitOptionalLocksDisabledEnv } from '../git/runner'
import { parsePorcelainV1Records, type PorcelainV1Record } from '../git/porcelain-v1-records'
import { resolveDefaultBaseRefViaExec } from '../git/repo'
@@ -176,10 +176,10 @@ export async function baseRefExistsOnRemote(
candidateRefs.add(ref)
}
}
- const exactResult = await probeAnyExactRef(run, [...candidateRefs], {
+ const exactResult = await probeExactRefsViaForEachRef(run, [...candidateRefs], {
maxBuffer: MAX_REMOTE_REF_OUTPUT_BYTES
})
- if (exactResult.found || exactResult.unknown) {
+ if (exactResult.presentRefs.length > 0 || exactResult.unknownRefs.length > 0) {
return true
}
// Preserve stale-remote suffix matching with a 10MiB bound after cheap exact probes.
diff --git a/src/main/text-generation/pull-request-context-errors.test.ts b/src/main/text-generation/pull-request-context-errors.test.ts
index bdb340d07a6..74627bd569e 100644
--- a/src/main/text-generation/pull-request-context-errors.test.ts
+++ b/src/main/text-generation/pull-request-context-errors.test.ts
@@ -131,6 +131,9 @@ describe('getPullRequestDraftContext error handling', () => {
if (args[0] === 'remote') {
return { stdout: '--upload-pack=x\n' }
}
+ if (args[0] === 'for-each-ref') {
+ return { stdout: '' }
+ }
if (args[0] === 'show-ref' && args.includes('--verify')) {
throw Object.assign(new Error('missing exact ref'), { code: 1 })
}
@@ -161,6 +164,9 @@ describe('getPullRequestDraftContext error handling', () => {
if (args[0] === 'remote') {
return { stdout: 'origin\n' }
}
+ if (args[0] === 'for-each-ref') {
+ return { stdout: '' }
+ }
if (args[0] === 'show-ref') {
throw Object.assign(new Error('missing exact ref'), { code: 1 })
}
@@ -194,6 +200,9 @@ describe('getPullRequestDraftContext error handling', () => {
if (args[0] === 'remote') {
return { stdout: 'fork\n' }
}
+ if (args[0] === 'for-each-ref') {
+ return { stdout: '' }
+ }
if (args[0] === 'show-ref' && args.some((arg) => arg.startsWith('refs/remotes/'))) {
throw Object.assign(new Error('missing exact ref'), { code: 1 })
}
diff --git a/src/main/text-generation/pull-request-context.test.ts b/src/main/text-generation/pull-request-context.test.ts
index d09fe820423..36c84ed051e 100644
--- a/src/main/text-generation/pull-request-context.test.ts
+++ b/src/main/text-generation/pull-request-context.test.ts
@@ -168,6 +168,9 @@ describe('getPullRequestDraftContext', () => {
if (args[0] === 'remote') {
return { stdout: 'contributor-a\ncontributor-b\n', stderr: '' }
}
+ if (args[0] === 'for-each-ref') {
+ return { stdout: '', stderr: '' }
+ }
if (args[0] === 'show-ref') {
throw Object.assign(new Error('missing exact ref'), { code: 1 })
}
@@ -189,19 +192,19 @@ describe('getPullRequestDraftContext', () => {
await getPullRequestDraftContext(execGit, createContextInput())
- expect(execGit.mock.calls.filter(([args]) => args[0] === 'for-each-ref')).toHaveLength(0)
const showRefCalls = execGit.mock.calls.filter(([args]) => args[0] === 'show-ref')
- const exactRefs = showRefCalls
- .filter(([args]) => args.includes('--verify'))
- .map(([args]) => args.at(-1))
- expect(exactRefs).toEqual(
- expect.arrayContaining([
- 'refs/remotes/origin/main',
- 'refs/remotes/upstream/main',
- 'refs/remotes/contributor-a/main',
- 'refs/remotes/contributor-b/main'
- ])
- )
+ expect(showRefCalls.some(([args]) => args.includes('--verify'))).toBe(false)
+ const exactProbes = execGit.mock.calls.filter(([args]) => args[0] === 'for-each-ref')
+ expect(exactProbes).toHaveLength(1)
+ expect(exactProbes[0][0]).toEqual([
+ 'for-each-ref',
+ '--format=%(refname)',
+ '--',
+ 'refs/remotes/origin/main',
+ 'refs/remotes/upstream/main',
+ 'refs/remotes/contributor-a/main',
+ 'refs/remotes/contributor-b/main'
+ ])
expect(showRefCalls.some(([args]) => args.join(' ') === 'show-ref -- main')).toBe(true)
expect(execGit).not.toHaveBeenCalledWith(
@@ -290,10 +293,8 @@ describe('getPullRequestDraftContext', () => {
if (args[0] === 'remote') {
return { stdout: 'foo/bar\n', stderr: '' }
}
- if (args[0] === 'show-ref') {
- return args.at(-1) === 'refs/remotes/foo/bar/feature/fix'
- ? { stdout: '' }
- : Promise.reject(Object.assign(new Error('missing exact ref'), { code: 1 }))
+ if (args[0] === 'for-each-ref') {
+ return { stdout: 'refs/remotes/foo/bar/feature/fix\n' }
}
if (args[0] === 'fetch') {
return { stdout: '', stderr: '' }
@@ -319,7 +320,6 @@ describe('getPullRequestDraftContext', () => {
['fetch', '--no-tags', 'foo/bar', '+refs/heads/feature/fix:refs/remotes/foo/bar/feature/fix'],
expect.any(Object)
)
- expect(execGit.mock.calls.filter(([args]) => args[0] === 'for-each-ref')).toHaveLength(0)
})
it('preserves Git-valid punctuation in configured remote names', async () => {
@@ -609,10 +609,8 @@ describe('getPullRequestDraftContext', () => {
if (args[0] === 'remote') {
return { stdout: 'fork\n', stderr: '' }
}
- if (args[0] === 'show-ref') {
- return args.at(-1) === 'refs/remotes/origin/main'
- ? { stdout: '', stderr: '' }
- : Promise.reject(Object.assign(new Error('missing exact ref'), { code: 1 }))
+ if (args[0] === 'for-each-ref') {
+ return { stdout: 'refs/remotes/origin/main\n', stderr: '' }
}
if (args[0] === 'fetch' || args[0] === 'branch') {
return { stdout: '', stderr: '' }
@@ -633,9 +631,6 @@ describe('getPullRequestDraftContext', () => {
['fetch', '--no-tags', 'origin', '+refs/heads/main:refs/remotes/origin/main'],
expect.any(Object)
)
- expect(
- execGit.mock.calls.filter(([args]) => args[0] === 'show-ref' && args.includes('--verify'))
- ).toHaveLength(3)
- expect(execGit.mock.calls.filter(([args]) => args[0] === 'for-each-ref')).toHaveLength(0)
+ expect(execGit.mock.calls.filter(([args]) => args[0] === 'for-each-ref')).toHaveLength(1)
})
})
diff --git a/src/main/text-generation/pull-request-remote-ref-probes.ts b/src/main/text-generation/pull-request-remote-ref-probes.ts
index 640597a6e54..a3da967db02 100644
--- a/src/main/text-generation/pull-request-remote-ref-probes.ts
+++ b/src/main/text-generation/pull-request-remote-ref-probes.ts
@@ -1,7 +1,7 @@
import { isSafeGitRefName } from '../../shared/git-status-upstream-ref'
import { isRemoteHeadRef } from '../../shared/hosted-review-refs'
import { isSafeReviewHeadFetchRemote } from '../../shared/review-head-tracking-ref'
-import { probeExactRefs, type ExactRefProbeExecOptions } from '../git/exact-ref-probe'
+import { probeExactRefsViaForEachRef, type ExactRefProbeExecOptions } from '../git/exact-ref-probe'
export type PullRequestGitExec = (
args: string[],
@@ -88,7 +88,7 @@ async function listExactRemoteBaseRefs(
])
const refs = [...exactRefNames].filter((ref) => isSafeGitRefName(`refs/remotes/${ref}`))
const qualifiedRefs = refs.map((ref) => `refs/remotes/${ref}`)
- const result = await probeExactRefs(execGit, qualifiedRefs, options)
+ const result = await probeExactRefsViaForEachRef(execGit, qualifiedRefs, options)
const present = new Set(result.presentRefs)
return {
refs: refs.filter((ref) => present.has(`refs/remotes/${ref}`)),
diff --git a/src/shared/git-binary-compatibility.test.ts b/src/shared/git-binary-compatibility.test.ts
index 87788208150..da3b3be22b0 100644
--- a/src/shared/git-binary-compatibility.test.ts
+++ b/src/shared/git-binary-compatibility.test.ts
@@ -625,6 +625,26 @@ describeBinaryCompatibility('real Git binary compatibility', () => {
).rejects.toMatchObject({ code: 1 })
})
+ it('lists exact and descendant refs from batched for-each-ref probes', async () => {
+ const head = (await runGit(['rev-parse', 'HEAD'])).stdout.trim()
+ const presentRef = 'refs/remotes/origin/compat-batch'
+ const nestedRef = 'refs/remotes/origin/compat-batch-parent/nested'
+ await runGit(['update-ref', presentRef, head])
+ await runGit(['update-ref', nestedRef, head])
+
+ // A miss still exits 0; callers keep only exact lines, so the parent stays absent.
+ await expect(
+ runGit([
+ 'for-each-ref',
+ '--format=%(refname)',
+ '--',
+ presentRef,
+ 'refs/remotes/origin/compat-batch-parent',
+ 'refs/remotes/missing/compat-batch'
+ ])
+ ).resolves.toMatchObject({ stdout: `${presentRef}\n${nestedRef}\n` })
+ })
+
it('packs loose refs and reads the maintenance opt-out at the baseline', async () => {
// Pin baseline Git 2.25 pack-refs --all --prune (not 2.45+ --auto) and maintenance.auto opt-out reads.
const head = (await runGit(['rev-parse', 'HEAD'])).stdout.trim()