Files
orca/src/relay/git-handler-branch-diff-ops.ts
T
Neil 3713dd7376 perf(git): reuse pinned OIDs for SSH file diffs (#13586)
Forward the renderer's already-pinned {mergeBase, headOid} to the SSH relay so a single-file branch diff reads the two blobs directly instead of rediscovering live HEAD. Six sequential git processes become two concurrent reads, and a branch move mid-review no longer changes which revision is displayed.

Equivalence with the legacy route is proven against real Git across 14 change types; wire compatibility is proven over a real SSH socket against relay bundles built from main and from the pre-merge-base.
2026-08-11 02:18:29 -07:00

58 lines
1.8 KiB
TypeScript

import { buildDiffResult } from './git-diff-result'
import { readBlobAtOid, type GitBufferExec } from './git-handler-ops'
const FULL_GIT_OBJECT_ID_PATTERN = /^(?:[0-9a-fA-F]{40}|[0-9a-fA-F]{64})$/
function assertFullGitObjectId(value: unknown, label: string): asserts value is string {
if (typeof value !== 'string' || !FULL_GIT_OBJECT_ID_PATTERN.test(value)) {
throw new Error(`${label} must be a full git object id`)
}
}
export function isFullGitObjectId(value: unknown): value is string {
return typeof value === 'string' && FULL_GIT_OBJECT_ID_PATTERN.test(value)
}
export function parseOptionalBranchDiffHeadOid(
params: Record<string, unknown>
): string | undefined {
const { headOid } = params
// Why: GitBranchCompareSummary.headOid is `string | null`, so a mixed-version
// client can put an explicit null on the wire. Treat it as unpinned rather
// than rejecting a request the legacy path would have served.
if (headOid == null) {
return undefined
}
assertFullGitObjectId(headOid, 'headOid')
return headOid
}
export async function branchDiffEntryAtPinnedOids(
gitBuffer: GitBufferExec,
worktreePath: string,
baseOid: string,
headOid: string,
filePath: string,
oldPath?: string
) {
assertFullGitObjectId(baseOid, 'baseRef')
assertFullGitObjectId(headOid, 'headOid')
try {
const [left, right] = await Promise.all([
readBlobAtOid(gitBuffer, worktreePath, baseOid, oldPath ?? filePath),
readBlobAtOid(gitBuffer, worktreePath, headOid, filePath)
])
return [buildDiffResult(left.content, right.content, left.isBinary, right.isBinary, filePath)]
} catch {
return [
{
kind: 'text' as const,
originalContent: '',
modifiedContent: '',
originalIsBinary: false,
modifiedIsBinary: false
}
]
}
}