From 4b1c1930c2fd07a4dec556f138217e4953685594 Mon Sep 17 00:00:00 2001 From: "kiloconnect[bot]" <240665456+kiloconnect[bot]@users.noreply.github.com> Date: Mon, 4 May 2026 15:54:19 +0000 Subject: [PATCH] fix: preserve upstream compat merge bases --- script/upstream/merge.ts | 2 +- script/upstream/utils/git.test.ts | 55 +++++++++++++++++++++++++++++++ script/upstream/utils/git.ts | 5 ++- 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 script/upstream/utils/git.test.ts diff --git a/script/upstream/merge.ts b/script/upstream/merge.ts index 967ea1d9c78..db3245c1c1d 100644 --- a/script/upstream/merge.ts +++ b/script/upstream/merge.ts @@ -536,7 +536,7 @@ async function main() { const compatMessage = `refactor: kilo compat for ${targetVersion.tag}` if (prior) { const tree = await git.writeTree() - const commit = await git.commitTree(tree, compatMessage, [targetVersion.commit, prior.commit]) + const commit = await git.createMergeCommit(tree, compatMessage, targetVersion.commit, prior.commit) await git.updateBranch(opencodeBranch, commit) await git.checkout(opencodeBranch) } else { diff --git a/script/upstream/utils/git.test.ts b/script/upstream/utils/git.test.ts new file mode 100644 index 00000000000..5636241a2f7 --- /dev/null +++ b/script/upstream/utils/git.test.ts @@ -0,0 +1,55 @@ +import { afterEach, beforeEach, expect, test } from "bun:test" +import { mkdtemp, rm } from "node:fs/promises" +import { join } from "node:path" +import { tmpdir } from "node:os" +import { $ } from "bun" +import { createMergeCommit, findLatestCompatCommit, getCommitHash, updateBranch, writeTree } from "./git" + +const cwd = process.cwd() +let dir = "" + +async function commit(message: string) { + await $`git add -A`.quiet() + await $`git -c user.name=Test -c user.email=test@example.com commit -m ${message}`.quiet() + return getCommitHash("HEAD") +} + +beforeEach(async () => { + dir = await mkdtemp(join(tmpdir(), "kilo-upstream-git-")) + process.chdir(dir) + await $`git init -b upstream`.quiet() + await $`git config user.name Test`.quiet() + await $`git config user.email test@example.com`.quiet() +}) + +afterEach(async () => { + process.chdir(cwd) + await rm(dir, { recursive: true, force: true }) +}) + +test("finds previous compatibility commit for transformed merge base", async () => { + await Bun.write("brand.txt", "opencode A\n") + const old = await commit("release: v1.0.0") + + await Bun.write("brand.txt", "opencode B\n") + const target = await commit("release: v1.0.1") + + await $`git checkout -b main ${old}`.quiet() + await Bun.write("brand.txt", "kilo A\n") + const prior = await commit("refactor: kilo compat for v1.0.0") + + const found = await findLatestCompatCommit("main", target) + expect(found?.commit).toBe(prior) + expect(found?.upstream).toBe(old) + + await $`git checkout ${target}`.quiet() + await $`git checkout -b opencode-v1.0.1`.quiet() + await Bun.write("brand.txt", "kilo B\n") + await $`git add -A`.quiet() + const tree = await writeTree() + const next = await createMergeCommit(tree, "refactor: kilo compat for v1.0.1", target, 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) +}) diff --git a/script/upstream/utils/git.ts b/script/upstream/utils/git.ts index d30cb2b99ea..4e9ccea8197 100644 --- a/script/upstream/utils/git.ts +++ b/script/upstream/utils/git.ts @@ -196,9 +196,8 @@ export async function writeTree(): Promise { return result.trim() } -export async function commitTree(tree: string, message: string, parents: string[]): Promise { - const args = parents.flatMap((parent) => ["-p", parent]) - const result = await $`git commit-tree ${tree} ${args} -m ${message}`.text() +export async function createMergeCommit(tree: string, message: string, first: string, second: string): Promise { + const result = await $`git commit-tree ${tree} -p ${first} -p ${second} -m ${message}`.text() return result.trim() }