diff --git a/script/upstream/merge.ts b/script/upstream/merge.ts index e4ac18f7441..77f3faa4713 100644 --- a/script/upstream/merge.ts +++ b/script/upstream/merge.ts @@ -548,6 +548,10 @@ async function main() { logger.step(7, 8, "Merging into Kilo branch...") await git.checkout(kiloBranch) + if (prior) { + const linked = await git.recordAncestor(targetVersion.commit, `merge: record upstream ${targetVersion.tag}`) + if (linked) logger.info(`Recorded upstream ${targetVersion.tag} as Kilo branch ancestry`) + } const mergeResult = await git.merge(opencodeBranch) if (!mergeResult.success) { diff --git a/script/upstream/utils/git.test.ts b/script/upstream/utils/git.test.ts index f841d74bf59..7d1f5e3c904 100644 --- a/script/upstream/utils/git.test.ts +++ b/script/upstream/utils/git.test.ts @@ -3,7 +3,16 @@ import { mkdtemp, rm } from "node:fs/promises" import { join } from "node:path" import { tmpdir } from "node:os" import { $ } from "bun" -import { createCommit, findLatestCompatCommit, getCommitHash, getCommitParents, updateBranch, writeTree } from "./git" +import { + createCommit, + findLatestCompatCommit, + getCommitHash, + getCommitParents, + isAncestor, + recordAncestor, + updateBranch, + writeTree, +} from "./git" const cwd = process.cwd() let dir = "" @@ -51,8 +60,21 @@ test("finds previous compatibility commit for transformed base", async () => { const tree = await writeTree() const next = await createCommit(tree, "refactor: kilo compat for v1.0.1", prior) await updateBranch("opencode-v1.0.1", next) - const base = (await $`git merge-base main opencode-v1.0.1`.text()).trim() expect(base).toBe(prior) expect(await getCommitParents(next)).toEqual([prior]) + + await $`git checkout main`.quiet() + expect(await recordAncestor(target, "merge: record upstream v1.0.1")).toBe(true) + const link = await getCommitHash("HEAD") + expect(await getCommitParents(link)).toEqual([prior, target]) + expect(await isAncestor(target, link)).toBe(true) + + const linked = (await $`git merge-base main opencode-v1.0.1`.text()).trim() + expect(linked).toBe(prior) + + await $`git merge opencode-v1.0.1`.quiet() + const head = await getCommitHash("HEAD") + expect(await getCommitParents(head)).toEqual([link, next]) + expect(await isAncestor(target, head)).toBe(true) }) diff --git a/script/upstream/utils/git.ts b/script/upstream/utils/git.ts index d8730d2d378..35588124e00 100644 --- a/script/upstream/utils/git.ts +++ b/script/upstream/utils/git.ts @@ -205,6 +205,16 @@ export async function updateBranch(name: string, commit: string): Promise await $`git update-ref refs/heads/${name} ${commit}` } +export async function recordAncestor(ref: string, message: string): Promise { + if (await isAncestor(ref, "HEAD")) return false + + const result = await $`git merge -s ours --no-ff ${ref} -m ${message}`.nothrow() + if (result.exitCode !== 0) { + throw new Error(`Failed to record ancestor ${ref}: ${result.stderr.toString()}`) + } + return true +} + async function compatUpstream(message: string): Promise { const prefix = "refactor: kilo compat for " if (!message.startsWith(prefix)) return null