Skip to content
41 linesCodeBlameRaw
1/**
2 * A repository's branches as its pages show them: each one's head commit,
3 * how far it has moved from the default branch, the pull request open on
4 * it with its checks, and its preview. The overview shows the newest few;
5 * the Branches page shows them all.
6 */
7import type { Branch, BranchDrifts, Pull, RepoPath, Viewer } from "@g1t/contracts";
8
9import type { ActiveBranch } from "../components/branches";
10import { activeBranches, branchesToRead, summary } from "./branches";
11import { repos } from "./services.server";
12
13type Preview = { branch?: string | null; number?: number | null; url: string };
14
15/**
16 * The branches other than the default, at most `read` of them read (those
17 * with an open pull request first), newest commit first, and the default
18 * branch's head commit with its first line.
19 *
20 * One call to repos (`branch_drift`, services/repos/src/drift.rs) measures
21 * every branch. It keeps each answer by the pair of head commits, so only
22 * branches that moved, or every branch once the default branch moved, cost
23 * a walk, and that walk reads the default branch's history once for all of
24 * them. Before 2026-10-08 this was a `log` call per branch per depth from
25 * here, up to twenty on an overview.
26 */
27export async function readBranches(
28 path: RepoPath,
29 viewer: Viewer,
30 input: { defaultBranch: string; branches: Branch[]; pulls: Pull[]; previews: Preview[] },
31 read: number,
32): Promise<{ main: string; total: number; shown: ActiveBranch[]; head: ActiveBranch["commit"] }> {
33 const { reading, total, mainHead } = branchesToRead(input, read);
34 const measured: BranchDrifts | null = mainHead
35 ? await repos
36 .branchDrift(path, viewer, mainHead, reading.map((branch) => branch.hash).filter(Boolean))
37 .then((found) => (found.ok ? found.value : null))
38 .catch(() => null)
39 : null;
40 return { main: input.defaultBranch, total, shown: activeBranches(reading, measured, input), head: summary(measured?.base) };
41}