tree.ts
This file
Path
packages/project/src/code/tree.ts
286 lines · 7.7 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake 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/903-code-reads'oscabrielbefore adoption7608aca
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/903-code-reads'oscabrielbefore adoption7608aca
lines 9–11why, one step back910import type { CodeProject } from "../code.ts";11import { CodeCache } from "./cache.ts";Merge redesign into mainoscabrielbefore adoptioncaedd21
line 12why, one step back12import { commitAt, resolveAt } from "./revision.ts";Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 13why, one step back13Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 14why, one step back14/** 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 15why, one step back15export 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 16why, one step back16Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 17–25why, one step back17export const entriesOf = (18 repo: RepoName,19 id: string20): Effect.Effect<readonly TreeEntry[], RepoNotFound, RepoHost> =>21 Effect.gen(function* () {22 const cache = yield* CodeCache;2324 return yield* cache.through(25 `${repo}:tree:${id}`,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 26–31why, one step back26 RepoHost.use((host) => host.readTree(repo, ObjectId.make(id))).pipe(27 Effect.flatMap(28 Option.match({29 onNone: () => Effect.die(`${repo} has no tree ${id}`),30 onSome: Effect.succeed,31 })Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 32–98why, one step back32 )33 )34 );35 });3637export 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/903-code-reads'oscabrielbefore adoption7608aca
line 107why, one step back107Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
lines 108–142why, 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;124125const linkTextOf = (repo: RepoName, link: string) =>126 Effect.gen(function* () {127 const cache = yield* CodeCache;128129 return yield* cache.through(130 `${repo}:link:${link}`,131 RepoHost.use((host) => host.readBlob(repo, ObjectId.make(link))).pipe(132 Effect.map(133 Option.flatMap((bytes) =>134 bytes.length > LINK_TARGET_MAX135 ? Option.none()136 : Option.some(new TextDecoder().decode(bytes))137 )138 )139 )140 );141 });142Merge build/comments-project into mainoscabrielbefore adoption49efd2a
line 143why, one step back143/** 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 144–205why, one step back144const linkedFile = (145 repo: RepoName,146 root: Effect.Effect<string, never, RepoHost>,147 folder: string,148 link: string149) =>150 Effect.gen(function* () {151 const path = Option.flatMap(152 yield* Effect.orDie(linkTextOf(repo, link)),153 (target) => linkTargetOf(folder, target)154 );155156 if (Option.isNone(path)) {157 return Option.none();158 }159160 const entry = yield* entryAt(repo, yield* root, path.value).pipe(161 Effect.orDie162 );163164 return Option.flatMap(entry, (found) =>165 kindOf(found) === "blob"166 ? Option.some({167 path: RepoPath.make(path.value),168 sha: BlobSha.make(found.sha),169 })170 : Option.none()171 );172 });173174const readmeOf = (175 repo: RepoName,176 root: Effect.Effect<string, never, RepoHost>,177 folder: string,178 entries: readonly CodeEntry[]179) =>180 Effect.gen(function* () {181 // Every entry by each name, so a dangling `README.md` link still lets182 // a `readme.md` file beside it be the README.183 const candidates = README.flatMap((name) =>184 entries.filter(185 (entry) =>186 (entry.type === "blob" || entry.type === "symlink") &&187 entry.name.toLowerCase() === name188 )189 );190191 for (const found of candidates) {192 if (found.type === "blob") {193 return Option.some({ path: found.path, sha: BlobSha.make(found.sha) });194 }195196 const linked = yield* linkedFile(repo, root, folder, found.sha);197198 if (Option.isSome(linked)) {199 return linked;200 }201 }202203 return Option.none();204 });205Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 206–239why, one step back206export const folderAt = (207 repo: RepoName,208 commit: CommitSha,209 path: Option.Option<RepoPath>210): Effect.Effect<string, UnknownPath, RepoHost> =>211 Effect.gen(function* () {212 const found = yield* commitAt(repo, commit).pipe(213 Effect.flatMap(Effect.fromOption)214 );215216 if (Option.isNone(path)) {217 return found.tree;218 }219220 const entry = yield* entryAt(repo, found.tree, path.value);221222 if (Option.isNone(entry) || entry.value.type !== "tree") {223 return yield* new UnknownPath({ commit, path: path.value });224 }225226 return entry.value.sha;227 }).pipe(228 Effect.catchTags({229 NoSuchElementError: Effect.die,230 RepoNotFound: Effect.die,231 })232 );233234export const listing = (235 project: CodeProject,236 at: Option.Option<Rev>,237 path: Option.Option<RepoPath>238): Effect.Effect<TreeListing, UnknownRevision | UnknownPath, RepoHost> =>239 Effect.gen(function* () {Merge redesign into mainoscabrielbefore adoptioncaedd21
lines 240–241why, one step back240 const { commit, repo } = yield* resolveAt(project, at);241 const tree = yield* folderAt(repo, commit, path);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 242–247why, one step back242243 const prefix = Option.match(path, {244 onNone: () => "",245 onSome: (folder) => `${folder}/`,246 });247Merge redesign into mainoscabrielbefore adoptioncaedd21
line 248why, one step back248 const read = yield* entriesOf(repo, tree).pipe(Effect.orDie);Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 249–260why, one step back249250 const entries = Array.sort(251 read.map((entry): CodeEntry => ({252 mode: entry.mode,253 name: entry.name,254 path: RepoPath.make(`${prefix}${entry.name}`),255 sha: entry.sha,256 type: kindOf(entry),257 })),258 byKindThenName259 );260Merge redesign into mainoscabrielbefore adoptioncaedd21
line 261why, one step back261 const root = commitAt(repo, commit).pipe(Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
lines 262–267why, one step back262 Effect.flatMap(Effect.fromOption),263 Effect.map(({ tree: id }) => id),264 Effect.orDie265 );266267 const readme = yield* readmeOf(Merge redesign into mainoscabrielbefore adoptioncaedd21
line 268why, one step back268 repo,Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
lines 269–273why, one step back269 root,270 Option.getOrElse(path, () => ""),271 entries272 );273Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 274–280why, one step back274 return {275 at: commit,276 entries,277 ...Option.match(path, {278 onNone: () => ({}),279 onSome: (folder) => ({ path: folder }),280 }),Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
line 281why, one step back281 ...Option.match(readme, {Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
line 282why, one step back282 onNone: () => ({}),Merge build/1317-readme-symlink into mainoscabrielbefore adoption643ab22
line 283why, one step back283 onSome: (found) => ({ readme: found }),Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca
lines 284–286why, one step back284 }),285 };286 });