README links: ask the target's kind, so a chain is refused; keep the link's text by blob id; try every entry by each name
Before adoption: this commit came in with the history the project adopted, so no landing records it.
From review: a symlink's tree entry is a blob too, so a link to a link was taken as the README. The link's text is read through the code cache and refused past 4096 bytes. A dangling README.md link no longer hides a readme.md file beside it.
authored by oscabriel · committed by oscabriel · parent 6c754a5 · Browse files · 3 files·+72 added−26 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+72 added−26 removed
- tests:+27 added−6 removed
- other:+45 added−20 removed
Provenance
- Commit
d352234- Tree
d6092cc- Author
oscabriel <oscarhernandeziv@gmail.com>- Committer
oscabriel <oscarhernandeziv@gmail.com>- Parent
6c754a5
3 files changed+72 added−26 removed
modified:packages/project/src/code/tree.ts+45 added-20 removed
120 unmodified lines121 * 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-export const linkTargetOf = (125+const linkTargetOf = (126 folder: string,127 target: string128 ): Option.Option<string> => {129 if (target.startsWith("/")) {14 unmodified lines144 145 return Option.liftPredicate(resolved.join("/"), (path) => path !== "");146 };147 148-/** The file the symlink blob `link` in `folder` names, if it is one in the repo. */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* () {156- const bytes = yield* RepoHost.use((host) =>157- host.readBlob(repo, ObjectId.make(link))158- ).pipe(Effect.orDie);159-160- const path = Option.flatMap(bytes, (read) =>161- linkTargetOf(folder, new TextDecoder().decode(read))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();2 unmodified lines191 const entry = yield* entryAt(repo, yield* root, path.value).pipe(192 Effect.orDie193 );194 172- return Option.flatMap(entry, ({ sha, type }) =>173- type === "blob"195+ return Option.flatMap(entry, (found) =>196+ kindOf(found) === "blob"197 ? Option.some({198 path: RepoPath.make(path.value),176- sha: BlobSha.make(sha),199+ sha: BlobSha.make(found.sha),200 })201 : Option.none()202 );203 });10 unmodified lines214 folder: string,215 entries: readonly CodeEntry[]216 ) =>217 Effect.gen(function* () {195- for (const name of README) {196- const found = entries.find(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() === name200- );225+ )226+ );227 228+ for (const found of candidates) {202- if (found?.type === "blob") {229+ if (found.type === "blob") {230 return Option.some({ path: found.path, sha: BlobSha.make(found.sha) });231 }232 206- if (found?.type === "symlink") {207- const linked = yield* linkedFile(repo, root, folder, found.sha);233+ const linked = yield* linkedFile(repo, root, folder, found.sha);234 209- if (Option.isSome(linked)) {210- return linked;211- }235+ if (Option.isSome(linked)) {236+ return linked;237 }238 }239 240 return Option.none();85 unmodified linesmodified:packages/project/test/code-fixtures.ts+7 added-2 removed
439 unmodified lines440 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, and one to a folder. `docs` links up one level to445- * the crate's README.444+ * 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");7 unmodified lines458 ["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 );55 unmodified linesmodified:packages/project/test/code.test.ts+20 added-4 removed
185 unmodified lines186 }).pipe(Effect.provide(RepoHostMemory))187 );188 189 it.effect(190- "has no README where its symlink leaves the repo, names nothing, or names a folder",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 195- const readmes = yield* Effect.forEach(["out", "gone", "folder"], (at) =>196- Effect.map(treeOf(code, main, path(at)), ({ readme }) => readme)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 199- expect(readmes).toStrictEqual([undefined, undefined, undefined]);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", () =>697 unmodified lines