Merge build/1317-readme-symlink into main
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by oscabriel · committed by oscabriel · parents 2f3d065, d352234 · Browse files · 3 files·+223 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+223 added−9 removed
- tests:+86 added−0 removed
- other:+137 added−9 removed
Provenance
- Commit
643ab22- Tree
0b45693- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent 1
2f3d065- Parent 2
d352234
3 files changed+223 added−9 removed
modified:packages/project/src/code/tree.ts+137 added-9 removed
115 unmodified lines116 "readme.txt",117 "readme.rst",118 ];119 120-const readmeOf = (entries: readonly CodeEntry[]) =>121- Array.findFirst(README, (name) =>122- Array.findFirst(123- entries,124- (entry) => entry.type === "blob" && entry.name.toLowerCase() === name125- )126- ).pipe(Option.map(({ path, sha }) => ({ path, sha: BlobSha.make(sha) })));120+/**121+ * Where a symlink in `folder` points, as a path from the repo's root: its122+ * target read against the folder, `.` and `..` resolved. None when the123+ * target is absolute or climbs out of the repo.124+ */125+const linkTargetOf = (126+ folder: string,127+ target: string128+): Option.Option<string> => {129+ if (target.startsWith("/")) {130+ return Option.none();131+ }132 133+ const resolved: string[] = folder === "" ? [] : folder.split("/");134+135+ for (const segment of target.split("/")) {136+ if (segment === "..") {137+ if (resolved.pop() === undefined) {138+ return Option.none();139+ }140+ } else if (segment !== "" && segment !== ".") {141+ resolved.push(segment);142+ }143+ }144+145+ return Option.liftPredicate(resolved.join("/"), (path) => path !== "");146+};147+148+/** The longest path a symlink's target may be: git's own `PATH_MAX`. */149+const LINK_TARGET_MAX = 4096;150+151+/** A symlink's target text, kept by its blob id; none when it is too long to be a path. */152+const linkTextOf = (repo: RepoName, link: string) =>153+ Effect.gen(function* () {154+ const cache = yield* CodeCache;155+156+ return yield* cache.through(157+ `${repo}:link:${link}`,158+ RepoHost.use((host) => host.readBlob(repo, ObjectId.make(link))).pipe(159+ Effect.map(160+ Option.flatMap((bytes) =>161+ bytes.length > LINK_TARGET_MAX162+ ? Option.none()163+ : Option.some(new TextDecoder().decode(bytes))164+ )165+ )166+ )167+ );168+ });169+170+/**171+ * The file the symlink blob `link` in `folder` names, if it is a file in172+ * the repo: one link followed, never a chain (a link's entry is a blob too,173+ * so its kind is asked, not its type).174+ */175+const linkedFile = (176+ repo: RepoName,177+ root: Effect.Effect<string, never, RepoHost>,178+ folder: string,179+ link: string180+) =>181+ Effect.gen(function* () {182+ const path = Option.flatMap(183+ yield* Effect.orDie(linkTextOf(repo, link)),184+ (target) => linkTargetOf(folder, target)185+ );186+187+ if (Option.isNone(path)) {188+ return Option.none();189+ }190+191+ const entry = yield* entryAt(repo, yield* root, path.value).pipe(192+ Effect.orDie193+ );194+195+ return Option.flatMap(entry, (found) =>196+ kindOf(found) === "blob"197+ ? Option.some({198+ path: RepoPath.make(path.value),199+ sha: BlobSha.make(found.sha),200+ })201+ : Option.none()202+ );203+ });204+205+/**206+ * The folder's README by GitLab's precedence, any case: a file, or a207+ * symlink to a file elsewhere in the repo (axum's root README.md links to208+ * its crate's), read where it points, so its relative links resolve there.209+ * A link out of the repo, to nothing or to a folder is no README.210+ */211+const readmeOf = (212+ repo: RepoName,213+ root: Effect.Effect<string, never, RepoHost>,214+ folder: string,215+ entries: readonly CodeEntry[]216+) =>217+ Effect.gen(function* () {218+ // Every entry by each name, so a dangling `README.md` link still lets219+ // a `readme.md` file beside it be the README.220+ const candidates = README.flatMap((name) =>221+ entries.filter(222+ (entry) =>223+ (entry.type === "blob" || entry.type === "symlink") &&224+ entry.name.toLowerCase() === name225+ )226+ );227+228+ for (const found of candidates) {229+ if (found.type === "blob") {230+ return Option.some({ path: found.path, sha: BlobSha.make(found.sha) });231+ }232+233+ const linked = yield* linkedFile(repo, root, folder, found.sha);234+235+ if (Option.isSome(linked)) {236+ return linked;237+ }238+ }239+240+ return Option.none();241+ });242+243 /** The tree `path` names at `commit`, or `UnknownPath`. */244 export const folderAt = (245 repo: RepoName,246 commit: CommitSha,49 unmodified lines296 })),297 byKindThenName298 );299 300+ const root = commitAt(project.repo, commit).pipe(301+ Effect.flatMap(Effect.fromOption),302+ Effect.map(({ tree: id }) => id),303+ Effect.orDie304+ );305+306+ const readme = yield* readmeOf(307+ project.repo,308+ root,309+ Option.getOrElse(path, () => ""),310+ entries311+ );312+313 return {314 at: commit,315 entries,316 ...Option.match(path, {317 onNone: () => ({}),318 onSome: (folder) => ({ path: folder }),319 }),192- ...Option.match(readmeOf(entries), {320+ ...Option.match(readme, {321 onNone: () => ({}),194- onSome: (readme) => ({ readme }),322+ onSome: (found) => ({ readme: found }),323 }),324 };325 });modified:packages/project/test/code-fixtures.ts+35 added
437 unmodified lines438 return { code: recordOf(name, sha, []), sha };439 });440 441 /**442+ * A workspace whose root README is a symlink to its crate's, as axum's is,443+ * with links elsewhere that a README must not follow: one up out of the444+ * repo, one to nothing, one to a folder, one to a link (the root's) and445+ * one absolute. `docs` links up one level to the crate's README, and446+ * `both` holds a dangling `README.md` link beside a `readme.md` file.447+ */448+export const linkedReadmes = Effect.gen(function* () {449+ const git = yield* writer;450+ const name = RepoName.make("linkkit");451+452+ const sha = yield* git.commit(453+ "Link the READMEs",454+ [455+ ["README.md", Content.Link({ target: "crate/README.md" })],456+ ["crate/README.md", text("# the crate\n")],457+ ["crate/src/lib.rs", text("pub fn crate() {}\n")],458+ ["docs/README.md", Content.Link({ target: "../crate/README.md" })],459+ ["out/README.md", Content.Link({ target: "../../README.md" })],460+ ["gone/README.md", Content.Link({ target: "missing.md" })],461+ ["folder/README.md", Content.Link({ target: "../crate" })],462+ ["chain/README.md", Content.Link({ target: "../README.md" })],463+ ["rooted/README.md", Content.Link({ target: "/crate/README.md" })],464+ ["both/README.md", Content.Link({ target: "missing.md" })],465+ ["both/readme.md", text("# beside the link\n")],466+ ],467+ [],468+ 1469+ );470+471+ yield* git.push(name, [["refs/heads/main", sha]]);472+473+ return { code: recordOf(name, sha, []) };474+});475+476+/**477 * The memory store, each `readTree` (or each `readBlob`, for `"blob"`)478 * taking 10 ms once `reading` is set (a history is written fast, then479 * read), with the most reads ever under way at once kept in `peak`: a live480 * store answers a read in tens of milliseconds, so a walk that reads one44 unmodified linesmodified:packages/project/test/code.test.ts+51 added
47 unmodified lines48 big,49 Content,50 datekit,51 fork,52+ linkedReadmes,53 longHistory,54 project,55 overlappingTrees,56 wide,108 unmodified lines165 expect(listing.readme?.path).toBe("lib/readme.md");166 }).pipe(Effect.provide(RepoHostMemory))167 );168 169+ it.effect(170+ "follows a README that is a symlink to the file it names in the repo, and reads it there",171+ () =>172+ Effect.gen(function* () {173+ const { code } = yield* linkedReadmes;174+ const root = yield* treeOf(code, main, Option.none());175+ const crate = yield* treeOf(code, main, path("crate"));176+ const docs = yield* treeOf(code, main, path("docs"));177+ const target = crate.entries.find(({ name }) => name === "README.md");178+179+ expect(180+ root.entries.find(({ name }) => name === "README.md")?.type181+ ).toBe("symlink");182+ expect([root.readme, docs.readme]).toStrictEqual([183+ { path: "crate/README.md", sha: target?.sha },184+ { path: "crate/README.md", sha: target?.sha },185+ ]);186+ }).pipe(Effect.provide(RepoHostMemory))187+ );188+189+ it.effect(190+ "passes over a dangling README link to a readme file beside it",191+ () =>192+ Effect.gen(function* () {193+ const { code } = yield* linkedReadmes;194+ const listing = yield* treeOf(code, main, path("both"));195+196+ expect(listing.readme?.path).toBe("both/readme.md");197+ }).pipe(Effect.provide(RepoHostMemory))198+ );199+200+ it.effect(201+ "has no README where its symlink leaves the repo, names nothing, names a folder, names another link, or is absolute",202+ () =>203+ Effect.gen(function* () {204+ const { code } = yield* linkedReadmes;205+206+ const readmes = yield* Effect.forEach(207+ ["out", "gone", "folder", "chain", "rooted"],208+ (at) =>209+ Effect.map(treeOf(code, main, path(at)), ({ readme }) =>210+ Option.fromUndefinedOr(readme)211+ )212+ );213+214+ expect(readmes.every(Option.isNone)).toBeTruthy();215+ expect(readmes).toHaveLength(5);216+ }).pipe(Effect.provide(RepoHostMemory))217+ );218+219 it.effect("is UnknownPath when missing, or a file", () =>220 Effect.gen(function* () {221 const { c4, code } = yield* datekit;222 694 unmodified lines