Skip to content
Myr

log.ts

This file

Path

packages/project/src/code/log.ts

329 lines · 9.3 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge build/1003oscabrielbefore adoptione8561dc

line 1why, one step back
1import { inRepoSession, Revision, RepoHost } from "@repo/artifacts/repo-host";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 2–12why, one step back
2import type {
3 Commit,4 RepoNotFound,5 TreeEntry,6} from "@repo/artifacts/repo-host";7import { parseTrailers } from "@repo/git/trailers";8import type { CommitView, Rev } from "@repo/protocol/domain/code";9import type { LandingView } from "@repo/protocol/domain/landing";10import { TreeSha } from "@repo/protocol/ids";11import type { CommitSha, RepoName, RepoPath } from "@repo/protocol/ids";12import type { UnknownRevision } from "@repo/protocol/refusal";

Read a path's history sixteen commits at a time, so why and a file's log answer in secondsoscabrielbefore adoption73adbab

line 13why, one step back
13import { Array, DateTime, Effect, Equal, Option } from "effect";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 14–15why, one step back
14
15import type { CodeProject } from "../code.ts";

Merge build/1003oscabrielbefore adoptione8561dc

line 16why, one step back
16import { asOneView, CodeCache } from "./cache.ts";

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 17why, one step back
17import { versionAt, walkableAt } from "./file-walk.ts";

Merge build/1003oscabrielbefore adoptione8561dc

lines 18–20why, one step back
18import {
19 INCREMENT_REACH,20 keyOf,

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 21why, one step back
21 keptOnlyIfCanonical,

Merge build/1003oscabrielbefore adoptione8561dc

lines 22–26why, one step back
22 pathLogKept,
23 recall,24 throughHistory,25} from "./history.ts";26import type { StoredPathLog } from "./history.ts";

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 27why, one step back
27import { commitAt, resolveAt } from "./revision.ts";

Read a path's history sixteen commits at a time, so why and a file's log answer in secondsoscabrielbefore adoption73adbab

line 28why, one step back
28import { entryAt, READS_AT_ONCE } from "./tree.ts";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 29–85why, one step back
29
30export const LOG_LIMIT = 100;3132export const PATH_SCAN = 200;3334export const commitView = (35 commit: Commit,36 landing: Option.Option<LandingView>37): CommitView => {38 const [subject = "", ...rest] = commit.message.split("\n");3940 return {41 author: {42 at: DateTime.fromEpochSeconds(commit.authoredAt),43 email: commit.author.email,44 name: commit.author.name,45 },46 body: rest.join("\n").replace(/^\n+/u, ""),47 committer: {48 at: DateTime.fromEpochSeconds(commit.committedAt),49 email: commit.committer.email,50 name: commit.committer.name,51 },52 parents: commit.parents,53 sha: commit.sha,54 subject,55 trailers: parseTrailers(commit.message).map(({ key, value }) => ({56 key,57 value,58 })),59 tree: TreeSha.make(commit.tree),60 ...Option.match(landing, {61 onNone: () => ({}),62 onSome: (found) => ({ landing: found }),63 }),64 };65};6667export const commitViews = (68 project: CodeProject,69 commits: readonly Commit[]70): Effect.Effect<readonly CommitView[]> =>71 Effect.map(72 commits.length === 073 ? Effect.succeed([])74 : project.landings(commits.map(({ sha }) => sha)),75 (landings) => {76 const bySha = new Map<string, LandingView>(77 landings.map((landing) => [landing.sha, landing])78 );7980 return commits.map((commit) =>81 commitView(commit, Option.fromUndefinedOr(bySha.get(commit.sha)))82 );83 }84 );85

Merge build/1003oscabrielbefore adoptione8561dc

line 86why, one step back
86export const pageOf = (

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 87–123why, one step back
87 repo: RepoName,
88 sha: CommitSha,89 from: number,90 count: number91): Effect.Effect<readonly Commit[], RepoNotFound, RepoHost> =>92 Effect.gen(function* () {93 const cache = yield* CodeCache;9495 return yield* cache.through(96 `${repo}:log:${sha}:${from}:${count}`,97 RepoHost.use((host) =>98 host.log(repo, Revision.Commit({ sha }), count, from)99 ),100 { heavy: true }101 );102 });103104const idAt = (repo: RepoName, tree: string, path: RepoPath) =>105 Effect.map(106 entryAt(repo, tree, path),107 Option.map(({ mode, sha }: TreeEntry) => `${mode} ${sha}`)108 );109110export interface LogPage {111 readonly commits: readonly CommitView[];112 readonly next?: number;113}114115const pageWith = (116 commits: readonly CommitView[],117 next: Option.Option<number>118): LogPage =>119 Option.match(next, {120 onNone: () => ({ commits }),121 onSome: (at) => ({ commits, next: at }),122 });123

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

lines 124–127why, one step back
124type IdOf = (
125 commit: Commit126) => Effect.Effect<Option.Option<string>, RepoNotFound, RepoHost>;127

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 128why, one step back
128// A small file is named by its blob id, so a change of its mode alone is not seen.

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 129why, one step back
129const idOfFrom = (

Merge build/1003oscabrielbefore adoptione8561dc

lines 130–131why, one step back
130 repo: RepoName,
131 path: RepoPath,

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

lines 132–151why, one step back
132 start: Commit
133): Effect.Effect<IdOf, RepoNotFound, RepoHost> =>134 Effect.map(walkableAt(repo, start.sha, path), (whole) =>135 Option.match(whole, {136 onNone: () => (commit: Commit) => idAt(repo, commit.tree, path),137 // The start's file is read already: each commit is read once.138 onSome:139 ({ blob: first }) =>140 (commit: Commit) =>141 commit.sha === start.sha142 ? Effect.succeedSome(`blob ${first}`)143 : Effect.map(144 versionAt(repo, commit.sha, path),145 Option.map(({ blob }) => `blob ${blob}`)146 ),147 })148 );149150const idsOf = (151 idOf: IdOf,

Merge build/1003oscabrielbefore adoptione8561dc

lines 152–153why, one step back
152 commits: readonly Commit[]
153): Effect.Effect<readonly Option.Option<string>[], RepoNotFound, RepoHost> =>

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 154why, one step back
154 Effect.forEach(commits, idOf, { concurrency: READS_AT_ONCE });

Merge build/1003oscabrielbefore adoptione8561dc

lines 155–157why, one step back
155
156const changedIn = (157 repo: RepoName,

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 158why, one step back
158 idOf: IdOf,

Merge build/1003oscabrielbefore adoptione8561dc

lines 159–163why, one step back
159 scan: readonly Commit[],
160 count: number161): Effect.Effect<readonly CommitSha[], RepoNotFound, RepoHost> =>162 Effect.gen(function* () {163 const looked = scan.slice(0, count + 1);

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 164why, one step back
164 const ids = yield* idsOf(idOf, looked);

Merge build/1003oscabrielbefore adoptione8561dc

lines 165–181why, one step back
165
166 const changed = yield* Effect.forEach(167 looked.slice(0, count),168 (commit, at) =>169 Effect.gen(function* () {170 const parent = Option.fromUndefinedOr(commit.parents[0]);171 const after = looked[at + 1];172173 const before = yield* Option.match(parent, {174 onNone: () => Effect.succeedNone,175 onSome: (first) =>176 after !== undefined && after.sha === first177 ? Effect.succeed(ids[at + 1] ?? Option.none())178 : commitAt(repo, first).pipe(179 Effect.flatMap(180 Option.match({181 onNone: () => Effect.succeedNone,

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 182why, one step back
182 onSome: idOf,

Merge build/1003oscabrielbefore adoptione8561dc

lines 183–222why, one step back
183 })
184 )185 ),186 });187188 return Equal.equals(ids[at] ?? Option.none(), before)189 ? Option.none()190 : Option.some(commit.sha);191 }),192 { concurrency: READS_AT_ONCE }193 );194195 return Array.getSomes(changed);196 });197198const nearestKept = (repo: RepoName, path: RepoPath, scan: readonly Commit[]) =>199 Effect.map(200 Effect.forEach(201 scan.slice(1, INCREMENT_REACH + 1),202 ({ sha }) =>203 Effect.flatMap(204 keyOf(pathLogKept, { commit: sha, path, repo, scan: PATH_SCAN }),205 (key) => recall(pathLogKept, key)206 ),207 { concurrency: INCREMENT_REACH }208 ),209 (found) =>210 Array.findFirst(found, (kept, at) =>211 Option.map(kept, (value) => ({ back: at + 1, kept: value }))212 )213 );214215const historyOf = (216 repo: RepoName,217 path: RepoPath,218 scan: readonly Commit[]219): Effect.Effect<StoredPathLog, RepoNotFound, RepoHost> =>220 Effect.gen(function* () {221 const scanned = scan.slice(0, PATH_SCAN);222 const reachedRoot = scan.length <= PATH_SCAN;

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 223why, one step back
223 const [start] = scan;

Merge build/1003oscabrielbefore adoptione8561dc

line 224why, one step back
224

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

lines 225–233why, one step back
225 if (start === undefined) {
226 return { reachedRoot, shas: [] };227 }228229 const [kept, idOf] = yield* Effect.all(230 [nearestKept(repo, path, scan), idOfFrom(repo, path, start)],231 { concurrency: 2 }232 );233

Merge build/1003oscabrielbefore adoptione8561dc

lines 234–236why, one step back
234 if (Option.isNone(kept)) {
235 return {236 reachedRoot,

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 237why, one step back
237 shas: yield* changedIn(repo, idOf, scan, scanned.length),

Merge build/1003oscabrielbefore adoptione8561dc

lines 238–242why, one step back
238 };
239 }240241 const { back } = kept.value;242 const inScan = new Set<string>(scanned.map(({ sha }) => sha));

Walk a small file's history by reading it whole at each commit: one call a commit instead of its foldersoscabrielbefore adoption8310fed

line 243why, one step back
243 const between = yield* changedIn(repo, idOf, scan, back);

Merge build/1003oscabrielbefore adoptione8561dc

lines 244–253why, one step back
244
245 return {246 reachedRoot,247 shas: [248 ...between,249 ...kept.value.kept.shas.filter((sha) => inScan.has(sha)),250 ],251 };252 });253

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 254–263why, one step back
254export const logPage = (
255 project: CodeProject,256 input: {257 readonly at: Option.Option<Rev>;258 readonly path: Option.Option<RepoPath>;259 readonly from: number;260 readonly limit: number;261 }262): Effect.Effect<LogPage, UnknownRevision, RepoHost> =>263 Effect.gen(function* () {

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 264–265why, one step back
264 const resolved = yield* resolveAt(project, input.at);
265 const { commit: sha, repo } = resolved;

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 266–286why, one step back
266 const from = Math.max(0, Math.floor(input.from));
267 const limit = Math.min(LOG_LIMIT, Math.max(1, Math.floor(input.limit)));268269 if (Option.isNone(input.path)) {270 const read = yield* pageOf(repo, sha, from, limit + 1).pipe(Effect.orDie);271 const commits = yield* commitViews(project, read.slice(0, limit));272273 return pageWith(274 commits,275 read.length > limit ? Option.some(from + limit) : Option.none()276 );277 }278279 const path = input.path.value;280281 // One past the scan, so the last scanned commit's parent is at hand282 // and whether history goes on is known.283 const scan = yield* pageOf(repo, sha, from, PATH_SCAN + 1).pipe(284 Effect.orDie285 );286

Merge build/1003oscabrielbefore adoptione8561dc

line 287why, one step back
287 const [start] = scan;

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 288why, one step back
288

Merge build/1003oscabrielbefore adoptione8561dc

lines 289–291why, one step back
289 if (start === undefined) {
290 return pageWith([], Option.none());291 }

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 292why, one step back
292

Merge build/1003oscabrielbefore adoptione8561dc

lines 293–298why, one step back
293 const key = yield* keyOf(pathLogKept, {
294 commit: start.sha,295 path,296 repo,297 scan: PATH_SCAN,298 });

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 299why, one step back
299

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 300–302why, one step back
300 const { shas } = yield* keptOnlyIfCanonical(
301 resolved,302 throughHistory(pathLogKept, key, asOneView(historyOf(repo, path, scan)))

Merge build/1003oscabrielbefore adoptione8561dc

line 303why, one step back
303 ).pipe(Effect.orDie);

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 304why, one step back
304

Merge build/1003oscabrielbefore adoptione8561dc

line 305why, one step back
305 const at = new Map(scan.map((commit, index) => [commit.sha, index]));

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 306why, one step back
306

Merge build/1003oscabrielbefore adoptione8561dc

lines 307–308why, one step back
307 const touched = shas.slice(0, limit).flatMap((changed) => {
308 const commit = scan[at.get(changed) ?? scan.length];

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 309why, one step back
309

Merge build/1003oscabrielbefore adoptione8561dc

lines 310–311why, one step back
310 return commit === undefined ? [] : [commit];
311 });

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 312why, one step back
312

Merge build/1003oscabrielbefore adoptione8561dc

lines 313–315why, one step back
313 const commits = yield* commitViews(project, touched);
314 const newest = touched.at(-1);315 const last = newest === undefined ? undefined : at.get(newest.sha);

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 316why, one step back
316

Merge build/1003oscabrielbefore adoptione8561dc

lines 317–322why, one step back
317 // A page that fills stops the scan at its last commit.
318 if (touched.length === limit && last !== undefined) {319 return pageWith(320 commits,321 last + 1 < scan.length ? Option.some(from + last + 1) : Option.none()322 );

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 323–328why, one step back
323 }
324325 return pageWith(326 commits,327 scan.length > PATH_SCAN ? Option.some(from + PATH_SCAN) : Option.none()328 );

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 329why, one step back
329 }).pipe(inRepoSession);