Files
orca/src/main/runtime/runtime-git-diff-commands.ts
T
Sojiro.OandBrennan Benson 9802866ed5 fix(editor): Copy Remote URL links to the code the workspace is on, with the selected line range (#33597)
* fix(editor): Copy Remote URL links to the code the workspace is on, with the selected line range

The gutter menu's Copy Remote URL always built the link on the default
branch and dropped any multi-line selection. Prefer the HEAD commit when
origin already has it, then the origin branch the current branch tracks,
then the default branch; carry an optional end line through IPC, RPC and
the git provider so a selection becomes a line range.

* fix: pair remote file links with published repository refs

* fix: prepare exact remote file fallback before publication scan

* test: consolidate remote file URL integration coverage

* test: document real-clock cancellation deadline proof

* test: handle optional cancellation signal in assertion

* test: reuse spawned child fixture across runtimes

---------

Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
2026-10-27 13:00:23 -07:00

133 lines
4.4 KiB
TypeScript

import type {
GitBranchCompareResult,
GitCommitCompareResult,
GitDiffResult
} from '../../shared/git-diff-compare-types'
import { assertGitDiffWithinTransportBudget } from '../../shared/git-diff-transport-budget'
import { getBranchDiff } from '../git/status'
import type { GitAdmissionTier } from '../git/command-runner/git-exec-options'
import { normalizeRuntimeRelativePath } from './runtime-relative-paths'
import {
localGitOptionsForTarget,
normalizeRuntimeGitRelativePath,
requireLocalRuntimeGitRoute,
requireRuntimeGitProvider,
type RuntimeGitCommandHost
} from './runtime-git-command-target'
export class RuntimeGitDiffCommands {
constructor(private readonly host: RuntimeGitCommandHost) {}
// Why: cap the payload after execution-host dispatch.
async getRuntimeGitDiff(
worktreeSelector: string,
filePath: string,
staged: boolean,
compareAgainstHead?: boolean,
maxContentBytes?: number
): Promise<GitDiffResult> {
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
const relativePath = normalizeRuntimeGitRelativePath(filePath)
return assertGitDiffWithinTransportBudget(
await requireRuntimeGitProvider(target).getDiff(
target.worktree.path,
relativePath,
staged,
compareAgainstHead
),
maxContentBytes
)
}
async getRuntimeGitBranchCompare(
worktreeSelector: string,
baseRef: string,
admissionTier: GitAdmissionTier = 'interactive'
): Promise<GitBranchCompareResult> {
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
return requireRuntimeGitProvider(target).getBranchCompare(target.worktree.path, baseRef, {
admissionTier
})
}
async getRuntimeGitCommitCompare(
worktreeSelector: string,
commitId: string
): Promise<GitCommitCompareResult> {
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
return requireRuntimeGitProvider(target).getCommitCompare(target.worktree.path, commitId)
}
async getRuntimeGitBranchDiff(
worktreeSelector: string,
compare: { mergeBase: string; headOid: string },
filePath: string,
oldPath?: string,
maxContentBytes?: number
): Promise<GitDiffResult> {
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
const relativePath = normalizeRuntimeGitRelativePath(filePath)
const oldRelativePath = oldPath ? normalizeRuntimeGitRelativePath(oldPath) : undefined
requireLocalRuntimeGitRoute(target)
return assertGitDiffWithinTransportBudget(
await getBranchDiff(
target.worktree.path,
{
mergeBase: compare.mergeBase,
headOid: compare.headOid,
filePath: relativePath,
oldPath: oldRelativePath
},
{
...localGitOptionsForTarget(target),
admissionTier: 'interactive'
}
),
maxContentBytes
)
}
async getRuntimeGitCommitDiff(
worktreeSelector: string,
args: { commitOid: string; parentOid?: string | null; filePath: string; oldPath?: string },
maxContentBytes?: number
): Promise<GitDiffResult> {
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
const relativePath = normalizeRuntimeRelativePath(args.filePath)
const oldRelativePath = args.oldPath ? normalizeRuntimeRelativePath(args.oldPath) : undefined
return assertGitDiffWithinTransportBudget(
await requireRuntimeGitProvider(target).getCommitDiff(target.worktree.path, {
commitOid: args.commitOid,
parentOid: args.parentOid,
filePath: relativePath,
oldPath: oldRelativePath
}),
maxContentBytes
)
}
async getRuntimeGitRemoteFileUrl(
worktreeSelector: string,
relativePath: string,
line: number,
endLine?: number
): Promise<string | null> {
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
const normalizedRelativePath = normalizeRuntimeGitRelativePath(relativePath)
return requireRuntimeGitProvider(target).getRemoteFileUrl(
target.worktree.path,
normalizedRelativePath,
line,
endLine
)
}
async getRuntimeGitRemoteCommitUrl(
worktreeSelector: string,
sha: string
): Promise<string | null> {
const target = await this.host.resolveRuntimeGitTarget(worktreeSelector)
return requireRuntimeGitProvider(target).getRemoteCommitUrl(target.worktree.path, sha)
}
}