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 fileTake it
Merge build/1003oscabrielbefore adoptione8561dc
line 1why, one step back1import { inRepoSession, Revision, RepoHost } from "@repo/artifacts/repo-host";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 2–12why, one step back2import 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 back13import { Array, DateTime, Effect, Equal, Option } from "effect";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 14–15why, one step back1415import type { CodeProject } from "../code.ts";Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 16why, one step back16import { 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 back17import { versionAt, walkableAt } from "./file-walk.ts";Merge build/1003oscabrielbefore adoptione8561dc
lines 18–20why, one step back18import {19 INCREMENT_REACH,20 keyOf,Merge redesign into mainoscabrielbefore adoptioncaedd21
line 21why, one step back21 keptOnlyIfCanonical,Merge build/1003oscabrielbefore adoptione8561dc
lines 22–26why, one step back22 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 back27import { logKept, logKey, throughStore } from "./objects.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 28why, one step back28import { commitAt, resolveAt } from "./revision.ts";Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 29why, one step back29import 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 back30import { entryAt, READS_AT_ONCE } from "./tree.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 31–87why, one step back3132export 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 );87Merge build/1003oscabrielbefore adoptione8561dc
line 88why, one step back88export const pageOf = (Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 89–93why, one step back89 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 back94 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 back102103const 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 });122Walk 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 back123type IdOf = (124 commit: Commit125) => Effect.Effect<Option.Option<string>, RepoNotFound, RepoHost>;126Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 127why, one step back127// 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 back128const idOfFrom = (Merge build/1003oscabrielbefore adoptione8561dc
lines 129–130why, one step back129 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 back131 start: Commit132): 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 back151 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 back153 Effect.forEach(commits, idOf, { concurrency: READS_AT_ONCE });Merge build/1003oscabrielbefore adoptione8561dc
lines 154–156why, one step back154155const 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 back157 idOf: IdOf,Merge build/1003oscabrielbefore adoptione8561dc
lines 158–162why, one step back158 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 back163 const ids = yield* idsOf(idOf, looked);Merge build/1003oscabrielbefore adoptione8561dc
lines 164–180why, one step back164165 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 back181 onSome: idOf,Merge build/1003oscabrielbefore adoptione8561dc
lines 182–221why, one step back182 })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 back222 const [start] = scan;Merge build/1003oscabrielbefore adoptione8561dc
line 223why, one step back223Walk 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 back224 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 );232Merge build/1003oscabrielbefore adoptione8561dc
lines 233–235why, one step back233 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 back236 shas: yield* changedIn(repo, idOf, scan, scanned.length),Merge build/1003oscabrielbefore adoptione8561dc
lines 237–241why, one step back237 };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 back242 const between = yield* changedIn(repo, idOf, scan, back);Merge build/1003oscabrielbefore adoptione8561dc
lines 243–252why, one step back243244 return {245 reachedRoot,246 shas: [247 ...between,248 ...kept.value.kept.shas.filter((sha) => inScan.has(sha)),249 ],250 };251 });252Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 253why, one step back253const pageFrom = (Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 254why, one step back254 project: CodeProject,Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 255why, one step back255 resolved: Resolved,Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 256–260why, one step back256 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 back261): Effect.Effect<LogPage, never, RepoHost> =>Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 262why, one step back262 Effect.gen(function* () {Merge redesign into mainoscabrielbefore adoptioncaedd21
line 263why, one step back263 const { commit: sha, repo } = resolved;Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 264–284why, one step back264 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 );284Merge build/1003oscabrielbefore adoptione8561dc
line 285why, one step back285 const [start] = scan;Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 286why, one step back286Merge build/1003oscabrielbefore adoptione8561dc
lines 287–289why, one step back287 if (start === undefined) {288 return pageWith([], Option.none());289 }Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 290why, one step back290Merge build/1003oscabrielbefore adoptione8561dc
lines 291–296why, one step back291 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 back297Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 298–300why, one step back298 const { shas } = yield* keptOnlyIfCanonical(299 resolved,300 throughHistory(pathLogKept, key, asOneView(historyOf(repo, path, scan)))Merge build/1003oscabrielbefore adoptione8561dc
line 301why, one step back301 ).pipe(Effect.orDie);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 302why, one step back302Merge build/1003oscabrielbefore adoptione8561dc
line 303why, one step back303 const at = new Map(scan.map((commit, index) => [commit.sha, index]));Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 304why, one step back304Merge build/1003oscabrielbefore adoptione8561dc
lines 305–306why, one step back305 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 back307Merge build/1003oscabrielbefore adoptione8561dc
lines 308–309why, one step back308 return commit === undefined ? [] : [commit];309 });Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 310why, one step back310Merge build/1003oscabrielbefore adoptione8561dc
lines 311–313why, one step back311 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 back314Merge build/1003oscabrielbefore adoptione8561dc
lines 315–320why, one step back315 // 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 back321 }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 back326 );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 back344 );Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 345why, one step back345 }).pipe(inRepoSession);