Skip to content
99 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Merge branch 'main' into worktree-agent-a69aeabc4b0deeb971/**
2 * What the Files page's About, the Contributors, Releases and Activity
3 * pages say, worked out without the server: labels, the language bar,
4 * and an activity line for each event worth one.
5 */
Commits show their accounts, and every name opens a card (repos 0016)6import type { G1tEvent, LanguageShare, License, Project, WeekCommits } from "@g1t/contracts";
Merge branch 'main' into worktree-agent-a69aeabc4b0deeb977
8/**
9 * The address the About links to: the project's homepage (its own, or its
10 * repository's website it follows), else production when it runs
11 * elsewhere. Null leaves the repository's website.
12 */
13export function projectHomepage(project: Pick<Project, "links" | "runs" | "productionUrl"> | null): string | null {
14 if (!project) return null;
15 return project.links?.homepage ?? (project.runs === "elsewhere" ? project.productionUrl : null) ?? null;
16}
17
18/** "MIT license", as the About names it; "View license" for one not recognized. */
19export function licenseLabel(license: License): string {
20 return license.spdxId ? `${license.spdxId} license` : "View license";
21}
22
23/** "12", "1.2k", "15k". */
24export function compact(n: number): string {
25 return n >= 10_000 ? `${Math.round(n / 1000)}k` : n >= 1000 ? `${(n / 1000).toFixed(1).replace(/\.0$/, "")}k` : String(n);
26}
27
28/** "1 star", "1.2k stars". */
29export function count(n: number, one: string, many = `${one}s`): string {
30 return `${compact(n)} ${n === 1 ? one : many}`;
31}
32
33/** The language bar's pieces: the largest, then the rest under 1% as Other. */
34export function languageBar(languages: LanguageShare[], least = 1): LanguageShare[] {
35 const shown = languages.filter((language) => language.percent >= least);
36 const rest = languages.filter((language) => language.percent < least);
37 if (rest.length === 0) return shown;
38 const bytes = rest.reduce((sum, language) => sum + language.bytes, 0);
39 const percent = Math.round(rest.reduce((sum, language) => sum + language.percent, 0) * 10) / 10;
40 if (percent === 0 && shown.length > 0) return shown;
41 return [...shown, { name: "Other", color: null, bytes, percent }];
42}
43
44
45/** The tallest week, for a chart's scale: never zero. */
46export function peak(weeks: WeekCommits[]): number {
47 return Math.max(1, ...weeks.map((week) => week.commits));
48}
49
50/** A contributor's weeks laid over the repository's, missing weeks as none. */
51export function alignWeeks(all: WeekCommits[], own: WeekCommits[]): number[] {
52 const byWeek = new Map(own.map((week) => [week.week, week.commits]));
53 return all.map((week) => byWeek.get(week.week) ?? 0);
54}
55
56/** An Activity line: what happened, and the branch, tag or pull request it was to. */
57export type ActivityLine =
58 | { kind: "push"; branch: string; commit: string; before: string | null; created: boolean; defaultBranch: boolean }
59 | { kind: "tag"; tag: string; commit: string }
60 | { kind: "merge"; number: number; commit: string }
61 | { kind: "renamed"; from: string; to: string }
62 | { kind: "default"; from: string; to: string };
63
64/** The events the Activity page lists. */
65export const ACTIVITY_TYPES = ["git.push", "pull.merged", "branch.renamed", "repo.default_branch_changed"] as const;
66
67/** One event as an Activity line, or null for one it does not list. */
68export function activityLine(event: G1tEvent): ActivityLine | null {
69 switch (event.type) {
70 case "git.push": {
71 const data = event.data as { ref: string; before?: string | null; after: string; defaultBranch: boolean };
72 if (data.ref.startsWith("refs/tags/")) return { kind: "tag", tag: data.ref.slice("refs/tags/".length), commit: data.after };
73 if (!data.ref.startsWith("refs/heads/")) return null;
74 return {
75 kind: "push",
76 branch: data.ref.slice("refs/heads/".length),
77 commit: data.after,
78 before: data.before ?? null,
79 created: !data.before,
80 defaultBranch: data.defaultBranch,
81 };
82 }
83 case "pull.merged": {
84 const data = event.data as { number: number; commit: string };
85 return { kind: "merge", number: data.number, commit: data.commit };
86 }
87 case "branch.renamed": {
88 const data = event.data as { from: string; to: string };
89 return { kind: "renamed", from: data.from, to: data.to };
90 }
91 case "repo.default_branch_changed": {
92 const data = event.data as { from: string; to: string; renamed: boolean };
93 // A rename of the default branch says so once, as a rename.
94 return data.renamed ? null : { kind: "default", from: data.from, to: data.to };
95 }
96 default:
97 return null;
98 }
99}

This file's history is long; its oldest lines are credited to the oldest commit read.