tree.ts
This file
Path
packages/project/src/code/tree.ts
286 lines · 7.8 KiB · TypeScript
Last changed by
Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
History of this fileTake it
The walk stopped after 200 commits: lines older than 958650a point at it.
Revert "Keep each tree a code read takes from a repo in the code store, so a repo's walks share them across isolates"oscabrielbefore adoption2421226
lines 1–2why, one step back1import { ObjectId, RepoHost } from "@repo/artifacts/repo-host";2import type { RepoNotFound, TreeEntry } from "@repo/artifacts/repo-host";Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 3–7why, one step back3import type { CodeEntry, Rev, TreeListing } from "@repo/protocol/domain/code";4import { BlobSha, RepoPath } from "@repo/protocol/ids";5import type { CommitSha, RepoName } from "@repo/protocol/ids";6import { UnknownPath } from "@repo/protocol/refusal";7import type { UnknownRevision } from "@repo/protocol/refusal";Revert "Keep each tree a code read takes from a repo in the code store, so a repo's walks share them across isolates"oscabrielbefore adoption2421226
line 8why, one step back8import { Array, Effect, Match, Option, Order } from "effect";Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 9–10why, one step back910import type { CodeProject } from "../code.ts";Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 11–13why, one step back11import { contentsOf } from "./blob.ts";12import { keptOnlyIfCanonical } from "./history.ts";13import { objectKey, throughStore, treeKept } from "./objects.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 14why, one step back14import { commitAt, resolveAt } from "./revision.ts";Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 15why, one step back15import type { Resolved } from "./revision.ts";Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
line 16why, one step back16Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 17why, one step back17/** Live store reads take 0.1-0.5 s and chain per path segment; 48 at once keeps a walk fast and under the store's 2,000 requests per 10 s per repo. */Merge build/1003oscabrielbefore adoptione8561dc
line 18why, one step back18export const READS_AT_ONCE = 48;Read a path's history sixteen commits at a time, so why and a file's log answer in secondsoscabrielbefore adoption73adbab
line 19why, one step back19Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 20–23why, one step back20export const entriesOf = (21 repo: RepoName,22 id: string23): Effect.Effect<readonly TreeEntry[], RepoNotFound, RepoHost> =>Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 24–35why, one step back24 throughStore(25 treeKept,26 objectKey(treeKept, repo, id),27 RepoHost.use((host) => host.readTree(repo, ObjectId.make(id)))28 ).pipe(29 Effect.flatMap(30 Option.match({31 onNone: () => Effect.die(`${repo} has no tree ${id}`),32 onSome: Effect.succeed,33 })34 )35 );Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 36–98why, one step back3637export const segmentsOf = (path: string): Option.Option<readonly string[]> => {38 const segments = path.split("/");3940 return segments.every(41 (segment) => segment !== "" && segment !== "." && segment !== ".."42 )43 ? Option.some(segments)44 : Option.none();45};4647export const entryAt = (48 repo: RepoName,49 root: string,50 path: string51): Effect.Effect<Option.Option<TreeEntry>, RepoNotFound, RepoHost> =>52 Option.match(segmentsOf(path), {53 onNone: () => Effect.succeedNone,54 onSome: (segments) =>55 Effect.gen(function* () {56 let tree = root;57 let found: Option.Option<TreeEntry> = Option.none();5859 for (const [at, segment] of segments.entries()) {60 if (at > 0) {61 if (Option.isNone(found) || found.value.type !== "tree") {62 return Option.none();63 }6465 tree = found.value.sha;66 }6768 const entries = yield* entriesOf(repo, tree);6970 found = Array.findFirst(entries, ({ name }) => name === segment);71 }7273 return found;74 }),75 });7677export const kindOf = (entry: TreeEntry): CodeEntry["type"] =>78 Match.value(entry).pipe(79 Match.when({ type: "tree" }, () => "tree" as const),80 Match.when({ type: "commit" }, () => "submodule" as const),81 Match.when({ mode: "120000" }, () => "symlink" as const),82 Match.orElse(() => "blob" as const)83 );8485const byKindThenName = Order.combine(86 Order.mapInput(Order.Boolean, (entry: CodeEntry) => entry.type !== "tree"),87 Order.mapInput(Order.String, (entry: CodeEntry) => entry.name)88);8990const README = [91 "readme.md",92 "readme.markdown",93 "readme.mdown",94 "readme",95 "readme.txt",96 "readme.rst",97];98Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 99why, one step back99/** None when the target is absolute or climbs out of the repo. */Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
lines 100–106why, one step back100const linkTargetOf = (101 folder: string,102 target: string103): Option.Option<string> => {104 if (target.startsWith("/")) {105 return Option.none();106 }Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
line 107why, one step back107Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
lines 108–124why, one step back108 const resolved: string[] = folder === "" ? [] : folder.split("/");109110 for (const segment of target.split("/")) {111 if (segment === "..") {112 if (resolved.pop() === undefined) {113 return Option.none();114 }115 } else if (segment !== "" && segment !== ".") {116 resolved.push(segment);117 }118 }119120 return Option.liftPredicate(resolved.join("/"), (path) => path !== "");121};122123const LINK_TARGET_MAX = 4096;124Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 125why, one step back125/** A link's blob is kept as any blob is, by its id. */Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
line 126why, one step back126const linkTextOf = (repo: RepoName, link: string) =>Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 127–132why, one step back127 Effect.map(128 contentsOf(repo, BlobSha.make(link)),129 Option.flatMap(({ size, text }) =>130 size > LINK_TARGET_MAX ? Option.none() : text131 )132 );Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
line 133why, one step back133Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 134why, one step back134/** One link followed, never a chain; a link's entry is a blob too, so its kind is asked, not its type. */Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
lines 135–196why, one step back135const linkedFile = (136 repo: RepoName,137 root: Effect.Effect<string, never, RepoHost>,138 folder: string,139 link: string140) =>141 Effect.gen(function* () {142 const path = Option.flatMap(143 yield* Effect.orDie(linkTextOf(repo, link)),144 (target) => linkTargetOf(folder, target)145 );146147 if (Option.isNone(path)) {148 return Option.none();149 }150151 const entry = yield* entryAt(repo, yield* root, path.value).pipe(152 Effect.orDie153 );154155 return Option.flatMap(entry, (found) =>156 kindOf(found) === "blob"157 ? Option.some({158 path: RepoPath.make(path.value),159 sha: BlobSha.make(found.sha),160 })161 : Option.none()162 );163 });164165const readmeOf = (166 repo: RepoName,167 root: Effect.Effect<string, never, RepoHost>,168 folder: string,169 entries: readonly CodeEntry[]170) =>171 Effect.gen(function* () {172 // Every entry by each name, so a dangling `README.md` link still lets173 // a `readme.md` file beside it be the README.174 const candidates = README.flatMap((name) =>175 entries.filter(176 (entry) =>177 (entry.type === "blob" || entry.type === "symlink") &&178 entry.name.toLowerCase() === name179 )180 );181182 for (const found of candidates) {183 if (found.type === "blob") {184 return Option.some({ path: found.path, sha: BlobSha.make(found.sha) });185 }186187 const linked = yield* linkedFile(repo, root, folder, found.sha);188189 if (Option.isSome(linked)) {190 return linked;191 }192 }193194 return Option.none();195 });196Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 197–224why, one step back197export const folderAt = (198 repo: RepoName,199 commit: CommitSha,200 path: Option.Option<RepoPath>201): Effect.Effect<string, UnknownPath, RepoHost> =>202 Effect.gen(function* () {203 const found = yield* commitAt(repo, commit).pipe(204 Effect.flatMap(Effect.fromOption)205 );206207 if (Option.isNone(path)) {208 return found.tree;209 }210211 const entry = yield* entryAt(repo, found.tree, path.value);212213 if (Option.isNone(entry) || entry.value.type !== "tree") {214 return yield* new UnknownPath({ commit, path: path.value });215 }216217 return entry.value.sha;218 }).pipe(219 Effect.catchTags({220 NoSuchElementError: Effect.die,221 RepoNotFound: Effect.die,222 })223 );224Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 225–226why, one step back225const folderListing = (226 { commit, repo }: Resolved,Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
line 227why, one step back227 path: Option.Option<RepoPath>Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
line 228why, one step back228): Effect.Effect<TreeListing, UnknownPath, RepoHost> =>Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
line 229why, one step back229 Effect.gen(function* () {Merge redesign into mainoscabrielbefore adoptioncaedd21
line 230why, one step back230 const tree = yield* folderAt(repo, commit, path);Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 231–236why, one step back231232 const prefix = Option.match(path, {233 onNone: () => "",234 onSome: (folder) => `${folder}/`,235 });236Merge redesign into mainoscabrielbefore adoptioncaedd21
line 237why, one step back237 const read = yield* entriesOf(repo, tree).pipe(Effect.orDie);Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 238–249why, one step back238239 const entries = Array.sort(240 read.map((entry): CodeEntry => ({241 mode: entry.mode,242 name: entry.name,243 path: RepoPath.make(`${prefix}${entry.name}`),244 sha: entry.sha,245 type: kindOf(entry),246 })),247 byKindThenName248 );249Merge redesign into mainoscabrielbefore adoptioncaedd21
line 250why, one step back250 const root = commitAt(repo, commit).pipe(Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
lines 251–256why, one step back251 Effect.flatMap(Effect.fromOption),252 Effect.map(({ tree: id }) => id),253 Effect.orDie254 );255256 const readme = yield* readmeOf(Merge redesign into mainoscabrielbefore adoptioncaedd21
line 257why, one step back257 repo,Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
lines 258–262why, one step back258 root,259 Option.getOrElse(path, () => ""),260 entries261 );262Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 263–269why, one step back263 return {264 at: commit,265 entries,266 ...Option.match(path, {267 onNone: () => ({}),268 onSome: (folder) => ({ path: folder }),269 }),Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
line 270why, one step back270 ...Option.match(readme, {Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
line 271why, one step back271 onNone: () => ({}),Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
line 272why, one step back272 onSome: (found) => ({ readme: found }),Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
lines 273–274why, one step back273 }),274 };Merge perf/code-store into mainoscabrielbefore adoptionef0ddbd
lines 275–285why, one step back275 });276277export const listing = (278 project: CodeProject,279 at: Option.Option<Rev>,280 path: Option.Option<RepoPath>281): Effect.Effect<TreeListing, UnknownRevision | UnknownPath, RepoHost> =>282 Effect.gen(function* () {283 const resolved = yield* resolveAt(project, at);284285 return yield* keptOnlyIfCanonical(resolved, folderListing(resolved, path));Merge branch 'build/905-readme-markdown'oscabrielbefore adoption958650a
line 286why, one step back286 });