feat(FN-4499): complete Step 3 — add reanchor primitive

Fusion-Task-Id: FN-4499
Fusion-Task-Lineage: 3e9fee75-5c3d-4ce5-aa8b-3f2bb94e48eb
This commit is contained in:
Fusion
2026-05-14 13:15:44 -07:00
committed by gsxdsm
parent 9bcff54143
commit 4f8f889d5d
2 changed files with 69 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import { exec } from "node:child_process";
import { promisify } from "node:util";
import {
autoRecoverCrossContamination,
reanchorBranchToBase,
classifyBootstrapMisbinding,
classifyForeignCommits,
type BranchCrossContaminationCommit,
@@ -183,6 +184,27 @@ describe("branch contamination recovery classification", () => {
});
});
it("reanchors branch to base and clears bootstrap foreign history", async () => {
const { repoDir, baseSha } = await setupRepo();
const foreign = await makeCommit(repoDir, "foreign-reanchor", "feat(FN-4367): dependency change", "FN-4367");
const before = await run("git rev-parse feature", repoDir);
const result = await reanchorBranchToBase({
repoDir,
worktreePath: repoDir,
branchName: "feature",
baseSha,
taskId: "FN-4488",
});
const after = await run("git rev-parse feature", repoDir);
const range = await run(`git rev-list --count ${baseSha}..feature`, repoDir);
expect(result.previousTipSha).toBe(before);
expect(result.newTipSha).toBe(after);
expect(result.previousTipSha).toBe(foreign.sha);
expect(range).toBe("0");
});
it("auto-recovers by dropping already-upstream foreign commits and preserving remaining branch work", async () => {
const { repoDir, baseSha } = await setupRepo();
await writeFile(path.join(repoDir, "foreign.txt"), "", "utf-8");
@@ -212,3 +234,4 @@ describe("branch contamination recovery classification", () => {
expect(result.newTipSha).not.toEqual(originalTip);
});
});

View File

@@ -507,6 +507,52 @@ export async function classifyForeignCommits(
}
}
export interface ReanchorBranchToBaseInput {
repoDir: string;
worktreePath: string;
branchName: string;
baseSha: string;
taskId: string;
}
export interface ReanchorBranchToBaseResult {
previousTipSha: string;
newTipSha: string;
}
export async function reanchorBranchToBase(
input: ReanchorBranchToBaseInput,
): Promise<ReanchorBranchToBaseResult> {
const { repoDir, worktreePath, branchName, baseSha, taskId } = input;
const previousTipSha = await revParse(repoDir, branchName);
try {
await execAsync("git checkout -- .", {
cwd: worktreePath,
encoding: "utf-8",
timeout: GIT_TIMEOUT_MS,
maxBuffer: GIT_MAX_BUFFER,
});
} catch {
// best-effort: worktree may already be clean
}
await execAsync("git clean -fd", {
cwd: worktreePath,
encoding: "utf-8",
timeout: GIT_TIMEOUT_MS,
maxBuffer: GIT_MAX_BUFFER,
});
await runGit(worktreePath, `git checkout --detach ${quoteShellArg(baseSha)}`);
await runGit(worktreePath, `git checkout -B ${quoteShellArg(branchName)} ${quoteShellArg(baseSha)}`);
await assertCleanBranchAtBase(repoDir, branchName, baseSha, taskId);
return {
previousTipSha,
newTipSha: await revParse(repoDir, branchName),
};
}
export interface AutoRecoverCrossContaminationInput {
repoDir: string;
branchName: string;