From 5c19415eb8e0484fb210b40a31c88b213e9b182a Mon Sep 17 00:00:00 2001 From: tignear Date: Sat, 17 Oct 2026 09:28:47 +0900 Subject: [PATCH] ci: select PR checks from the merge base, not the base tip A PR's base.sha is the current base tip, so PR branches behind it reported unmerged base changes as their own. After #1382 changed lydite.yml, every older PR selected all Lydite lanes. --- scripts/ci-changes.mjs | 3 ++- scripts/ci-changes.test.mjs | 30 +++++++++++++++++++++++++++++- scripts/ci-rust-scope.mjs | 11 ++++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/scripts/ci-changes.mjs b/scripts/ci-changes.mjs index ff5349b3e4f..924e3bc5595 100644 --- a/scripts/ci-changes.mjs +++ b/scripts/ci-changes.mjs @@ -236,7 +236,8 @@ function changedFiles(base, head) { try { const output = execFileSync( "git", - ["diff", "--name-only", "-z", base, head], + // Diff from the merge base: a PR's base.sha is the current base tip. + ["diff", "--name-only", "-z", `${base}...${head}`], { encoding: "utf8" }, ); return output.split("\0").filter(Boolean); diff --git a/scripts/ci-changes.test.mjs b/scripts/ci-changes.test.mjs index 54f41654427..6873d679941 100644 --- a/scripts/ci-changes.test.mjs +++ b/scripts/ci-changes.test.mjs @@ -6,6 +6,7 @@ import { dirname, join, relative } from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; +import { changedFilesBetween } from "./ci-rust-scope.mjs"; import { affectsHeliodorArm64, classifyFiles, @@ -161,7 +162,7 @@ const MASTER_GROUP = { const classifier = fileURLToPath(new URL("./ci-changes.mjs", import.meta.url)); -// The classifier diffs base..head in the repository at cwd. +// The classifier diffs base...head in the repository at cwd. function runClassifier( base, head, @@ -506,3 +507,30 @@ test("queue configuration runs script checks without rebuilding the product", () }); assert.deepEqual(classifyFiles([".mergify.yml", "unknown/new-source"]), all); }); + +test("PR branches behind their base ignore unmerged base changes", (t) => { + const repository = mkdtempSync(join(tmpdir(), "celox-ci-changes-")); + t.after(() => rmSync(repository, { recursive: true, force: true })); + const git = (...args) => + execFileSync("git", args, { cwd: repository, encoding: "utf8" }).trim(); + const commit = (path) => { + writeFileSync(join(repository, path), `${path}\n`); + git("add", path); + git("commit", "--quiet", "-m", path); + return git("rev-parse", "HEAD"); + }; + git("init", "--quiet"); + git("config", "user.name", "test"); + git("config", "user.email", "test@example.com"); + git("config", "commit.gpgsign", "false"); + const fork = commit("README.md"); + git("checkout", "--quiet", "-b", "pr"); + const head = commit("CONTRIBUTING.md"); + git("checkout", "--quiet", fork); + const base = commit("unknown-base-change.xyz"); + assert.deepEqual(changedFilesBetween(base, head, repository), ["CONTRIBUTING.md"]); + assert.deepEqual( + runClassifier(base, head, { GITHUB_EVENT_NAME: "pull_request" }, repository), + { ...none, docs: true, heliodor_arm64: false, release: false }, + ); +}); diff --git a/scripts/ci-rust-scope.mjs b/scripts/ci-rust-scope.mjs index f81bab85071..75a18ea9ed0 100644 --- a/scripts/ci-rust-scope.mjs +++ b/scripts/ci-rust-scope.mjs @@ -206,12 +206,17 @@ export function rustTestScope(files, packages) { }; } -/** Changed files between two commits, or null when they cannot be compared. */ -export function changedFilesBetween(base, head) { +/** + * Files changed on head since it forked from base, or null when they cannot be + * compared. A PR's base.sha is the current base tip, so a two-point diff would + * also report base changes that the PR branch has not merged yet. + */ +export function changedFilesBetween(base, head, cwd = undefined) { const sha = /^[0-9a-f]{40,64}$/i; if (!sha.test(base) || !sha.test(head) || /^0+$/.test(base)) return null; try { - return execFileSync("git", ["diff", "--name-only", "-z", base, head], { + return execFileSync("git", ["diff", "--name-only", "-z", `${base}...${head}`], { + cwd, encoding: "utf8", }) .split("\0")