Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion scripts/ci-changes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 29 additions & 1 deletion scripts/ci-changes.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
);
});
11 changes: 8 additions & 3 deletions scripts/ci-rust-scope.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading