Skip to content
Myr

log.ts

This file

Path

packages/project/src/code/log.ts

345 lines · 9.7 KiB · TypeScript

Last changed by

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

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 perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 16why, one step back
16import { asOneView } 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 perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 27why, one step back
27import { logKept, logKey, throughStore } from "./objects.ts";

Merge redesign into mainoscabrielbefore adoptioncaedd21

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

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 29why, one step back
29import type { Resolved } 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 30why, one step back
30import { entryAt, READS_AT_ONCE } from "./tree.ts";

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

lines 31–87why, one step back
31
32export const LOG_LIMIT = 100;3334export const PATH_SCAN = 200;3536export const commitView = (37 commit: Commit,38 landing: Option.Option<LandingView>39): CommitView => {40 const [subject = "", ...rest] = commit.message.split("\n");4142 return {43 author: {44 at: DateTime.fromEpochSeconds(commit.authoredAt),45 email: commit.author.email,46 name: commit.author.name,47 },48 body: rest.join("\n").replace(/^\n+/u, ""),49 committer: {50 at: DateTime.fromEpochSeconds(commit.committedAt),51 email: commit.committer.email,52 name: commit.committer.name,53 },54 parents: commit.parents,55 sha: commit.sha,56 subject,57 trailers: parseTrailers(commit.message).map(({ key, value }) => ({58 key,59 value,60 })),61 tree: TreeSha.make(commit.tree),62 ...Option.match(landing, {63 onNone: () => ({}),64 onSome: (found) => ({ landing: found }),65 }),66 };67};6869export const commitViews = (70 project: CodeProject,71 commits: readonly Commit[]72): Effect.Effect<readonly CommitView[]> =>73 Effect.map(74 commits.length === 075 ? Effect.succeed([])76 : project.landings(commits.map(({ sha }) => sha)),77 (landings) => {78 const bySha = new Map<string, LandingView>(79 landings.map((landing) => [landing.sha, landing])80 );8182 return commits.map((commit) =>83 commitView(commit, Option.fromUndefinedOr(bySha.get(commit.sha)))84 );85 }86 );87

Merge build/1003oscabrielbefore adoptione8561dc

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

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

lines 89–93why, one step back
89 repo: RepoName,
90 sha: CommitSha,91 from: number,92 count: number93): Effect.Effect<readonly Commit[], RepoNotFound, RepoHost> =>

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 94–101why, one step back
94 throughStore(
95 logKept,96 logKey(repo, sha, from, count),97 RepoHost.use((host) =>98 host.log(repo, Revision.Commit({ sha }), count, from)99 ).pipe(Effect.asSome),100 { heavy: true }101 ).pipe(Effect.map(Option.getOrElse(() => [])));

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

lines 102–122why, one step back
102
103const idAt = (repo: RepoName, tree: string, path: RepoPath) =>104 Effect.map(105 entryAt(repo, tree, path),106 Option.map(({ mode, sha }: TreeEntry) => `${mode} ${sha}`)107 );108109export interface LogPage {110 readonly commits: readonly CommitView[];111 readonly next?: number;112}113114const pageWith = (115 commits: readonly CommitView[],116 next: Option.Option<number>117): LogPage =>118 Option.match(next, {119 onNone: () => ({ commits }),120 onSome: (at) => ({ commits, next: at }),121 });122

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

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

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 127why, one step back
127// 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 128why, one step back
128const idOfFrom = (

Merge build/1003oscabrielbefore adoptione8561dc

lines 129–130why, one step back
129 repo: RepoName,
130 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 131–150why, one step back
131 start: Commit
132): Effect.Effect<IdOf, RepoNotFound, RepoHost> =>133 Effect.map(walkableAt(repo, start.sha, path), (whole) =>134 Option.match(whole, {135 onNone: () => (commit: Commit) => idAt(repo, commit.tree, path),136 // The start's file is read already: each commit is read once.137 onSome:138 ({ blob: first }) =>139 (commit: Commit) =>140 commit.sha === start.sha141 ? Effect.succeedSome(`blob ${first}`)142 : Effect.map(143 versionAt(repo, commit.sha, path),144 Option.map(({ blob }) => `blob ${blob}`)145 ),146 })147 );148149const idsOf = (150 idOf: IdOf,

Merge build/1003oscabrielbefore adoptione8561dc

lines 151–152why, one step back
151 commits: readonly Commit[]
152): 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 153why, one step back
153 Effect.forEach(commits, idOf, { concurrency: READS_AT_ONCE });

Merge build/1003oscabrielbefore adoptione8561dc

lines 154–156why, one step back
154
155const changedIn = (156 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 157why, one step back
157 idOf: IdOf,

Merge build/1003oscabrielbefore adoptione8561dc

lines 158–162why, one step back
158 scan: readonly Commit[],
159 count: number160): Effect.Effect<readonly CommitSha[], RepoNotFound, RepoHost> =>161 Effect.gen(function* () {162 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 163why, one step back
163 const ids = yield* idsOf(idOf, looked);

Merge build/1003oscabrielbefore adoptione8561dc

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

Merge build/1003oscabrielbefore adoptione8561dc

lines 182–221why, one step back
182 })
183 )184 ),185 });186187 return Equal.equals(ids[at] ?? Option.none(), before)188 ? Option.none()189 : Option.some(commit.sha);190 }),191 { concurrency: READS_AT_ONCE }192 );193194 return Array.getSomes(changed);195 });196197const nearestKept = (repo: RepoName, path: RepoPath, scan: readonly Commit[]) =>198 Effect.map(199 Effect.forEach(200 scan.slice(1, INCREMENT_REACH + 1),201 ({ sha }) =>202 Effect.flatMap(203 keyOf(pathLogKept, { commit: sha, path, repo, scan: PATH_SCAN }),204 (key) => recall(pathLogKept, key)205 ),206 { concurrency: INCREMENT_REACH }207 ),208 (found) =>209 Array.findFirst(found, (kept, at) =>210 Option.map(kept, (value) => ({ back: at + 1, kept: value }))211 )212 );213214const historyOf = (215 repo: RepoName,216 path: RepoPath,217 scan: readonly Commit[]218): Effect.Effect<StoredPathLog, RepoNotFound, RepoHost> =>219 Effect.gen(function* () {220 const scanned = scan.slice(0, PATH_SCAN);221 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 222why, one step back
222 const [start] = scan;

Merge build/1003oscabrielbefore adoptione8561dc

line 223why, one step back
223

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

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

Merge build/1003oscabrielbefore adoptione8561dc

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

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

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

Merge build/1003oscabrielbefore adoptione8561dc

lines 237–241why, one step back
237 };
238 }239240 const { back } = kept.value;241 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 242why, one step back
242 const between = yield* changedIn(repo, idOf, scan, back);

Merge build/1003oscabrielbefore adoptione8561dc

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

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 253why, one step back
253const pageFrom = (

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

line 254why, one step back
254 project: CodeProject,

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 255why, one step back
255 resolved: Resolved,

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

lines 256–260why, one step back
256 input: {
257 readonly path: Option.Option<RepoPath>;258 readonly from: number;259 readonly limit: number;260 }

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

line 261why, one step back
261): Effect.Effect<LogPage, never, RepoHost> =>

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

line 262why, one step back
262 Effect.gen(function* () {

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 263why, one step back
263 const { commit: sha, repo } = resolved;

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

lines 264–284why, one step back
264 const from = Math.max(0, Math.floor(input.from));
265 const limit = Math.min(LOG_LIMIT, Math.max(1, Math.floor(input.limit)));266267 if (Option.isNone(input.path)) {268 const read = yield* pageOf(repo, sha, from, limit + 1).pipe(Effect.orDie);269 const commits = yield* commitViews(project, read.slice(0, limit));270271 return pageWith(272 commits,273 read.length > limit ? Option.some(from + limit) : Option.none()274 );275 }276277 const path = input.path.value;278279 // One past the scan, so the last scanned commit's parent is at hand280 // and whether history goes on is known.281 const scan = yield* pageOf(repo, sha, from, PATH_SCAN + 1).pipe(282 Effect.orDie283 );284

Merge build/1003oscabrielbefore adoptione8561dc

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

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

line 286why, one step back
286

Merge build/1003oscabrielbefore adoptione8561dc

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

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

line 290why, one step back
290

Merge build/1003oscabrielbefore adoptione8561dc

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

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

line 297why, one step back
297

Merge redesign into mainoscabrielbefore adoptioncaedd21

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

Merge build/1003oscabrielbefore adoptione8561dc

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

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

line 302why, one step back
302

Merge build/1003oscabrielbefore adoptione8561dc

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

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

line 304why, one step back
304

Merge build/1003oscabrielbefore adoptione8561dc

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

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

line 307why, one step back
307

Merge build/1003oscabrielbefore adoptione8561dc

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

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

line 310why, one step back
310

Merge build/1003oscabrielbefore adoptione8561dc

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

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

line 314why, one step back
314

Merge build/1003oscabrielbefore adoptione8561dc

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

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

lines 321–325why, one step back
321 }
322323 return pageWith(324 commits,325 scan.length > PATH_SCAN ? Option.some(from + PATH_SCAN) : Option.none()

Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd

lines 326–343why, one step back
326 );
327 });328329export const logPage = (330 project: CodeProject,331 input: {332 readonly at: Option.Option<Rev>;333 readonly path: Option.Option<RepoPath>;334 readonly from: number;335 readonly limit: number;336 }337): Effect.Effect<LogPage, UnknownRevision, RepoHost> =>338 Effect.gen(function* () {339 const resolved = yield* resolveAt(project, input.at);340341 return yield* keptOnlyIfCanonical(342 resolved,343 pageFrom(project, resolved, input)

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

line 344why, one step back
344 );

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

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