diff --git a/script/upstream/utils/git.test.ts b/script/upstream/utils/git.test.ts index 999510bb42b..f841d74bf59 100644 --- a/script/upstream/utils/git.test.ts +++ b/script/upstream/utils/git.test.ts @@ -33,8 +33,10 @@ test("finds previous compatibility commit for transformed base", async () => { await Bun.write("brand.txt", "opencode B\n") const target = await commit("release: v1.0.1") + await $`git tag v1.0.1 ${target}`.quiet() await $`git checkout -b main ${old}`.quiet() + await $`git tag v1.0.0 ${old}`.quiet() await Bun.write("brand.txt", "kilo A\n") const prior = await commit("refactor: kilo compat for v1.0.0") diff --git a/script/upstream/utils/git.ts b/script/upstream/utils/git.ts index b25212de02a..d8730d2d378 100644 --- a/script/upstream/utils/git.ts +++ b/script/upstream/utils/git.ts @@ -205,6 +205,19 @@ export async function updateBranch(name: string, commit: string): Promise await $`git update-ref refs/heads/${name} ${commit}` } +async function compatUpstream(message: string): Promise { + const prefix = "refactor: kilo compat for " + if (!message.startsWith(prefix)) return null + + const tag = message.slice(prefix.length).trim().split(/\s+/)[0] + if (!tag) return null + + const ref = `${tag}^{commit}` + const result = await $`git rev-parse ${ref}`.quiet().nothrow() + if (result.exitCode !== 0) return null + return result.stdout.toString().trim() +} + export async function findLatestCompatCommit(base: string, target: string): Promise { const grep = "^refactor: kilo compat for " const result = await $`git log --format=%H%x00%s --grep=${grep} ${base}`.quiet().nothrow() @@ -222,6 +235,9 @@ export async function findLatestCompatCommit(base: string, target: string): Prom const [commit, message = ""] = line.split("\0") if (!commit) continue + const upstream = await compatUpstream(message) + if (upstream && (await isAncestor(upstream, target))) return { commit, upstream, message } + const parents = await getCommitParents(commit) for (const parent of parents) { if (await isAncestor(parent, target)) return { commit, upstream: parent, message }