Skip to content
Closed
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
63 changes: 54 additions & 9 deletions packages/core/src/find-reference-commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,29 @@ describe("#resolveBaseline", () => {
expect(findBaseline).not.toHaveBeenCalled();
});

it("uses the baseline commit and sends no parent commits when one is found", async () => {
const ancestors = Array.from({ length: 200 }, (_, i) => `anc-${i}`);
it("hands the server the merge base and its ancestors when a baseline is reachable", async () => {
const ancestors = Array.from({ length: 500 }, (_, i) => `anc-${i}`);
const getMergeBase = vi.fn(async () => "merge-base");
const listCommits = createMergeBaseListCommits(ancestors);
const findBaseline = vi.fn(async () => createBaseline("anc-3"));
// The closest commit that happens to have a completed build right now.
const findBaseline = vi.fn(async () => createBaseline("anc-42"));

const result = await resolveBaseline({
getMergeBase,
listCommits,
findBaseline,
});

expect(result).toEqual({ referenceCommit: "anc-3", parentCommits: null });
// The merge base, not "anc-42". Naming a commit here would freeze the
// baseline to the builds that are complete at this instant, and the server
// stops at the reference commit as soon as it has a bucket for it.
expect(result.referenceCommit).toBe("merge-base");
expect(result.parentCommits).toEqual(
["merge-base", ...ancestors].slice(0, PARENT_COMMITS_LIMIT),
);
// A single request: the window the server searches on its own.
expect(findBaseline).toHaveBeenCalledTimes(1);
expect(findBaseline).toHaveBeenCalledWith(result.parentCommits);
});

it("offers the merge base itself as the first baseline candidate", async () => {
Expand All @@ -188,15 +198,35 @@ describe("#resolveBaseline", () => {
findBaseline,
});

expect(result).toEqual({
referenceCommit: "merge-base",
parentCommits: null,
});
expect(result.referenceCommit).toBe("merge-base");
// The merge base is the first candidate sent to the API.
expect(findBaseline.mock.calls[0]?.[0]?.[0]).toBe("merge-base");
});

it("falls back to the merge base with parent commits when no baseline is found", async () => {
it("names the commit itself when the baseline is out of the server's reach", async () => {
const ancestors = Array.from({ length: 1000 }, (_, i) => `anc-${i}`);
const getMergeBase = vi.fn(async () => "merge-base");
const listCommits = createMergeBaseListCommits(ancestors);
const findBaseline = vi
.fn<(commits: string[]) => Promise<Build | null>>()
// Nothing within the window the server searches…
.mockResolvedValueOnce(null)
// … but there is one further back, which the server cannot walk to.
.mockResolvedValueOnce(createBaseline("anc-700"));

const result = await resolveBaseline({
getMergeBase,
listCommits,
findBaseline,
});

expect(result.referenceCommit).toBe("anc-700");
expect(result.parentCommits).toEqual(
["anc-700", ...ancestors].slice(0, PARENT_COMMITS_LIMIT),
);
});

it("keeps the merge base when no baseline exists anywhere", async () => {
const ancestors = Array.from({ length: 500 }, (_, i) => `anc-${i}`);
const getMergeBase = vi.fn(async () => "merge-base");
const listCommits = createMergeBaseListCommits(ancestors);
Expand All @@ -215,4 +245,19 @@ describe("#resolveBaseline", () => {
);
expect(result.parentCommits).toHaveLength(PARENT_COMMITS_LIMIT);
});

it("never lists a shallower history than it has already fetched", async () => {
const ancestors = Array.from({ length: 1000 }, (_, i) => `anc-${i}`);
const getMergeBase = vi.fn(async () => "merge-base");
const listCommits = createMergeBaseListCommits(ancestors);
const findBaseline = vi.fn(async () => null);

await resolveBaseline({ getMergeBase, listCommits, findBaseline });

const limits = listCommits.mock.calls.map(([, limit]) => limit);
// Each listing deepens the shallow clone with a fetch, so a smaller limit
// afterwards would shorten the history back and hide commits we have.
expect(limits[0]).toBe(PARENT_COMMITS_LIMIT);
expect(limits).toEqual([...limits].sort((a, b) => a - b));
});
});
97 changes: 65 additions & 32 deletions packages/core/src/find-reference-commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export interface FindReferenceCommitParams {
* when none is found.
*/
findBaseline: (commits: string[]) => Promise<Build | null>;

/**
* Number of leading commits the caller has already inspected. Batches at or
* below that size are skipped, so the search never asks for a shallower
* history than the caller already fetched.
*/
inspectedCount?: number;
}

/**
Expand All @@ -65,17 +72,26 @@ export interface FindReferenceCommitParams {
export async function findReferenceCommit(
params: FindReferenceCommitParams,
): Promise<string | null> {
let inspectedCount = 0;
let inspectedCount = params.inspectedCount ?? 0;

for (const limit of getCumulativeBatchSizes()) {
// Already covered by the caller or by a previous batch. Asking anyway would
// re-fetch the history at a shallower depth than it already has.
if (limit <= inspectedCount) {
continue;
}

const commits = await params.listCommits(limit);

// Only send the commits not already inspected in previous batches. Earlier
// commits are closer ancestors and have already been ruled out, so within
// each batch the closest commit still wins overall.
const batch = commits.slice(inspectedCount);

// No new commits became available: the history is exhausted.
// The listing stopped growing, so the history is exhausted. This is the only
// reliable signal: a listing shorter than `limit` is normal on the shallow
// clones CI uses, where git stops at the boundary of the fetched history and
// a deeper fetch does bring more commits.
if (batch.length === 0) {
break;
}
Expand All @@ -88,24 +104,16 @@ export async function findReferenceCommit(
}

inspectedCount = commits.length;

// Fewer commits than requested: the history is exhausted, so asking for more
// would return the same list.
if (commits.length < limit) {
break;
}
}

debug("No eligible baseline found among ancestor commits");
return null;
}

/**
* Number of commits (the merge base plus its ancestors) sent as `parentCommits`
* when no baseline build is found yet. The baseline build may still be
* processing on the server; sending these lets the server resolve the baseline
* once it completes, when it processes this build. The server treats the first
* commit as the reference commit and searches the rest.
* Number of commits (the reference commit plus its ancestors) sent as
* `parentCommits`, which is the window the server searches on its own. The
* server treats the first commit as the reference commit and searches the rest.
*/
export const PARENT_COMMITS_LIMIT = 300;

Expand Down Expand Up @@ -137,7 +145,8 @@ export interface BaselineResolution {
referenceCommit: string | null;
/**
* Commits sent to the server so it can resolve the baseline itself — the
* reference commit followed by its ancestors — or `null` when not needed.
* reference commit followed by its ancestors — or `null` when there is no
* reference commit.
*/
parentCommits: string[] | null;
}
Expand All @@ -149,12 +158,21 @@ export interface BaselineResolution {
* 1. Find the base commit the build content is derived from — usually the merge
* base, the closest common ancestor of the build branch and its base branch.
* This is always the starting point.
* 2. From the merge base, ask the API to pick the closest commit (the merge base
* itself or one of its ancestors) that has an eligible baseline build, and use
* it as the reference commit.
* 3. If none is found, the baseline build may still be processing on the server.
* Fall back to the merge base as the reference commit and send its parent
* commits so the server can resolve the baseline once that build completes.
* 2. Send it as the reference commit, followed by the ancestors the server
* searches on its own, and let the server pick the baseline when it
* processes the build.
* 3. Only when no baseline is reachable from the merge base do we search deeper
* and name a commit ourselves, because past {@link PARENT_COMMITS_LIMIT} the
* server cannot walk back that far.
*
* Step 2 is deliberately not "resolve the baseline here and send that commit".
* `findBaseline` only sees builds that are already complete, so on a busy base
* branch the closest ones are routinely still running when this executes and
* the search walks straight past them. A commit named here is also final: the
* server stops at the reference commit as soon as it has a bucket, and for a
* project without remote content access there is no ancestor list to fall back
* on. Handing over the chain instead keeps the decision where the freshest
* information is.
*/
export async function resolveBaseline(
params: ResolveBaselineParams,
Expand All @@ -166,23 +184,38 @@ export async function resolveBaseline(
}
debug("Found merge base", mergeBase);

const parentCommits = await params.listCommits(
mergeBase,
PARENT_COMMITS_LIMIT,
);

// Whether a baseline exists within the server's reach — not which one, that
// is the server's call.
const reachable = await params.findBaseline(parentCommits);
if (reachable) {
debug("Baseline reachable from the merge base", mergeBase);
return { referenceCommit: mergeBase, parentCommits };
}

const referenceCommit = await findReferenceCommit({
listCommits: (limit) => params.listCommits(mergeBase, limit),
findBaseline: params.findBaseline,
inspectedCount: parentCommits.length,
});

if (referenceCommit) {
debug("Found reference commit from baseline", referenceCommit);
return { referenceCommit, parentCommits: null };
// Nothing anywhere in the history. Keep the merge base so the build is still
// compared against whatever lands on it later.
if (!referenceCommit) {
debug("No baseline found, using merge base with parent commits", mergeBase);
return { referenceCommit: mergeBase, parentCommits };
}

// No eligible baseline build yet — it may still be processing. Fall back to
// the merge base and let the server resolve the baseline from the parent
// commits once that build completes.
const parentCommits = await params.listCommits(
mergeBase,
PARENT_COMMITS_LIMIT,
);
debug("No baseline found, using merge base with parent commits", mergeBase);
return { referenceCommit: mergeBase, parentCommits };
debug("Baseline out of the server's reach, using", referenceCommit);
return {
referenceCommit,
parentCommits: await params.listCommits(
referenceCommit,
PARENT_COMMITS_LIMIT,
),
};
}
Loading