Skip to content

Commit 30bad01

Browse files
gregbergeclaude
andcommitted
fix(core): read a commit's parents from the object, not the shallow graft
getCommitParents() read the parents with "git rev-list --parents", which honours the shallow graft: on the shallow clones CI uses, every commit at the boundary of the fetched history is reported as having no parent. That is exactly the commit we ask about - the test-merge commit HEAD sits on - so the answer came from a "git fetch --depth=2 origin <sha>" instead, once per build, over the network, for a commit the remote has to still be willing to serve. The commit object carries its parent SHAs whatever the graft says, so reading the object answers locally and cannot fail on the network, on lock contention, or on a remote that has stopped advertising the commit. Only the SHAs come back this way - the parent objects can still be absent - which is all the one caller needs, and is now written down. With the fetch gone the function is synchronous, and the deepening path it existed for is unreachable: getTestMergeBaseCommitSha only calls it after establishing that the commit is HEAD, so its object is always present. An explicit maxBuffer replaces the bound the old command got for free from printing nothing but SHAs. Reading the object and failing to read it are also reported apart now. Both used to log "is not a merge commit", which blames the commit's shape for a commit we never managed to look at. The existing tests build a full local repository, where the parents are readable whatever git is asked, so none of them covered the shallow checkout CI actually uses. The new one does, with the remote pointed at a path that does not exist. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent b2a9a9d commit 30bad01

3 files changed

Lines changed: 60 additions & 65 deletions

File tree

‎packages/core/src/ci-environment/git.test.ts‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -326,28 +326,24 @@ describe("#getCommitParents", () => {
326326
rmSync(root, { recursive: true, force: true });
327327
});
328328

329-
it("returns the parents of a merge commit, first parent first", async () => {
330-
await expect(getCommitParents(mergeSha)).resolves.toEqual([
331-
mainSha,
332-
featureSha,
333-
]);
329+
it("returns the parents of a merge commit, first parent first", () => {
330+
expect(getCommitParents(mergeSha)).toEqual([mainSha, featureSha]);
334331
});
335332

336-
it("returns an empty array for a root commit", async () => {
337-
await expect(getCommitParents(rootCommitSha)).resolves.toEqual([]);
333+
it("returns an empty array for a root commit", () => {
334+
expect(getCommitParents(rootCommitSha)).toEqual([]);
338335
});
339336

340-
it("returns null for an unknown commit", async () => {
341-
await expect(getCommitParents("f".repeat(40))).resolves.toBe(null);
337+
it("returns null for an unknown commit", () => {
338+
expect(getCommitParents("f".repeat(40))).toBe(null);
342339
});
343340

344-
it("deepens a shallow history to read the parents", async () => {
345-
// The state of a CI checkout: a shallow clone where git hides the parents
346-
// of the commits at the boundary of the history.
347-
// "--depth" implies "--single-branch", so the branch is named explicitly:
348-
// it would otherwise be read from the remote HEAD, which points to another
349-
// branch when git defaults to "master" for new repositories.
350-
const shallowDir = join(root, "shallow");
341+
it("reads the parents on a shallow clone without reaching the remote", async () => {
342+
// What CI has: a shallow checkout, where git grafts the boundary commit to
343+
// look parentless. The parent SHAs are in the commit object either way, so
344+
// no fetch is needed - and none may be relied on, since the remote can stop
345+
// advertising a commit GitHub generated for the pull request.
346+
const shallowDir = join(root, "shallow-offline");
351347
execFileSync("git", [
352348
"clone",
353349
"--depth=1",
@@ -360,18 +356,14 @@ describe("#getCommitParents", () => {
360356
execFileSync("git", ["-C", shallowDir, ...args])
361357
.toString()
362358
.trim();
359+
// Nothing to fetch from: the answer has to come from the local object.
360+
shallowGit("remote", "set-url", "origin", join(root, "does-not-exist.git"));
363361
process.chdir(shallowDir);
364362

365-
// The commit is there, but its parents are not…
366-
expect(shallowGit("rev-parse", "HEAD")).toBe(mergeSha);
367363
expect(shallowGit("rev-list", "--parents", "-n", "1", mergeSha, "--")).toBe(
368364
mergeSha,
369365
);
370366

371-
// … until they are fetched.
372-
await expect(getCommitParents(mergeSha)).resolves.toEqual([
373-
mainSha,
374-
featureSha,
375-
]);
367+
expect(getCommitParents(mergeSha)).toEqual([mainSha, featureSha]);
376368
});
377369
});

‎packages/core/src/ci-environment/git.ts‎

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -354,52 +354,44 @@ export async function listAncestorCommits(input: {
354354
}
355355

356356
/**
357-
* Read the parent commits of a commit, ordered as recorded in the commit — the
358-
* first parent first. Returns `null` when the commit is unknown, and an empty
359-
* array when it has no parent.
357+
* Read the parent commits recorded in a commit object, the first parent first.
358+
* Returns `null` when the object cannot be read — it is not in the repository,
359+
* or git refused to print it — and an empty array for a root commit.
360360
*
361-
* The history is deepened with a shallow fetch when the parents are not
362-
* available locally: in the shallow clones typically used in CI, git hides the
363-
* parents of the commits at the boundary of the history.
361+
* Read from the object rather than with `git rev-list --parents`, which honours
362+
* the shallow graft: on the shallow clones CI uses it reports every commit at
363+
* the boundary of the history as having no parent, which is most of what we ask
364+
* about. The object carries the parent SHAs whatever the graft says, so this
365+
* answers locally, and without the remote having to still advertise the commit:
366+
* GitHub stops advertising a test-merge commit as soon as it recomputes
367+
* `refs/pull/<n>/merge`, and fetching it then fails with "not our ref".
368+
*
369+
* Only the SHAs are recovered this way. The parent *objects* can be absent, so
370+
* a caller that goes on to walk from one has to be able to fetch it — see
371+
* {@link listAncestorCommits}, which deepens the history before listing.
364372
*/
365-
export async function getCommitParents(sha: string): Promise<string[] | null> {
366-
const localParents = readCommitParents(sha);
367-
if (localParents?.length) {
368-
return localParents;
369-
}
370-
371-
// Fetch the commit and its parents, so the parents stop being hidden by the
372-
// boundary of a shallow history.
373+
export function getCommitParents(sha: string): string[] | null {
373374
try {
374-
await runGitFetch(["--depth=2", "origin", sha]);
375+
const raw = execFileSync("git", ["cat-file", "commit", sha], {
376+
stdio: ["ignore", "pipe", "pipe"],
377+
// The whole object is printed, message included, where a bounded
378+
// `rev-list --parents` used to be enough. A generated commit message can
379+
// outgrow the 1MB default, and the overflow is reported as a failure to
380+
// read the commit at all.
381+
maxBuffer: 64 * 1024 * 1024,
382+
});
383+
// The header runs up to the first blank line, and holds one "parent <sha>"
384+
// line per parent, in order.
385+
const [header = ""] = raw.toString().split("\n\n", 1);
386+
return header
387+
.split("\n")
388+
.filter((line) => line.startsWith("parent "))
389+
.map((line) => line.slice("parent ".length).trim());
375390
} catch (error) {
376391
debug(
377-
`Failed to deepen history for ${sha}, using local history`,
392+
`Failed to read the commit object of ${sha}`,
378393
getGitErrorOutput(error),
379394
);
380-
return localParents;
381-
}
382-
383-
return readCommitParents(sha);
384-
}
385-
386-
/**
387-
* Read the parent commits of a commit from the local history. Returns `null`
388-
* when the commit is unknown, and an empty array when it has no parent — a root
389-
* commit, or a commit at the boundary of a shallow history, where git hides the
390-
* parents.
391-
*/
392-
function readCommitParents(sha: string): string[] | null {
393-
try {
394-
const raw = execFileSync(
395-
"git",
396-
["rev-list", "--parents", "-n", "1", sha, "--"],
397-
{ stdio: ["ignore", "pipe", "pipe"] },
398-
);
399-
const [, ...parents] = raw.toString().trim().split(" ");
400-
return parents;
401-
} catch (error) {
402-
debug(`Failed to read the parents of ${sha}`, getGitErrorOutput(error));
403395
return null;
404396
}
405397
}

‎packages/core/src/ci-environment/services/github-actions.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,22 @@ async function getTestMergeBaseCommitSha(
347347
return null;
348348
}
349349

350-
const parents = await getCommitParents(sha);
350+
const mergeRef = `refs/pull/${pullRequest.number}/merge`;
351+
const parents = getCommitParents(sha);
352+
353+
// Reported apart from the shape check below: a commit we could not read says
354+
// nothing about whether it is a test merge, and blaming its shape sends
355+
// anyone reading the debug output after the wrong thing.
356+
if (!parents) {
357+
debug(
358+
`Could not read the parents of ${sha}, ignoring the test-merge commit`,
359+
);
360+
return null;
361+
}
351362

352363
// A test-merge commit merges the pull request head (second parent) into the
353364
// tip of the base branch (first parent).
354-
if (parents?.length !== 2) {
365+
if (parents.length !== 2) {
355366
debug(`${sha} is not a merge commit, ignoring the test-merge commit`);
356367
return null;
357368
}
@@ -368,7 +379,7 @@ async function getTestMergeBaseCommitSha(
368379
// genuine test-merge build, and the fallback baselines it against the fork
369380
// point — reporting every change merged into the base branch since as a
370381
// change of the pull request.
371-
if (ctx.env.GITHUB_REF === `refs/pull/${pullRequest.number}/merge`) {
382+
if (ctx.env.GITHUB_REF === mergeRef) {
372383
return parents[0] ?? null;
373384
}
374385

0 commit comments

Comments
 (0)