/**
* Git diff and change queries
*/
import type { SimpleGit } from 'simple-git';
import { debug } from '../logger.js';
/**
* Map (path -> (oldLine -> newLine)) from git diff base..head.
* WHY: After push, review comment line numbers refer to pre-push code; re-mapping
* reduces WRONG_LOCATION when the fixer looks up code at the comment's line.
* Only context lines (unchanged) are mapped; deleted/added lines are skipped.
*/
/**
* Pick a git ref for **`base..HEAD`** diffs (line map, PR changed-file list) when the PR base is **`develop`** etc.
* WHY: On fork clones **`origin/`** may not exist (fork never had that branch); **`upstream/`** was fetched from the base repo (**`baseRepoCloneUrl`**). Using the wrong remote breaks **`computeLineMapFromDiff`** with “ambiguous argument” / missing ref (output.log audits on eliza fork PRs).
*/
export async function resolveRemoteTrackingRefForPrBase(
git: SimpleGit,
prInfo: { baseBranch: string; baseRepoCloneUrl?: string }
): Promise {
const b = prInfo.baseBranch?.trim();
if (!b) return 'HEAD~1';
const preferUpstreamFirst = Boolean(prInfo.baseRepoCloneUrl?.trim());
const candidates = preferUpstreamFirst
? ([`upstream/${b}`, `origin/${b}`] as const)
: ([`origin/${b}`, `upstream/${b}`] as const);
for (const ref of candidates) {
try {
await git.revparse([ref]);
return ref;
} catch {
/* try next */
}
}
return candidates[0];
}
export async function computeLineMapFromDiff(
git: SimpleGit,
baseRef: string,
headRef: string
): Promise