Skip to content
Myr

revision.ts

This file

Path

packages/project/src/code/revision.ts

220 lines · 6.5 KiB · TypeScript

Last changed by

Merge redesign into mainoscabrielbefore adoptioncaedd21

History of this file

Take it

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 1why, one step back
1/**

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

lines 2–3why, one step back
2 * `main` resolves from the project's record, never a ref; a tag name is
3 * matched among the repo's tags, never joined into a ref path, so

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 4–7why, one step back
4 * `../heads/x` names nothing. A fork address (ADR 0043) is found among the
5 * project's own listed forks, and its branch matched among that fork's6 * branches, so neither a layer's repo nor any repo the project does not list7 * is ever read.

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 8–10why, one step back
8 */
9import { RepoHost } from "@repo/artifacts/repo-host";10import type { Commit, RepoNotFound } from "@repo/artifacts/repo-host";

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 11–13why, one step back
11import { forkAddressOf, ForkPoint } from "@repo/protocol/domain/code";
12import type { ForkAddress, Rev } from "@repo/protocol/domain/code";13import { claimForkName, CommitSha, sameHandle } from "@repo/protocol/ids";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 14–26why, one step back
14import type { RepoName } from "@repo/protocol/ids";
15import { UnknownRevision } from "@repo/protocol/refusal";16import { Array, Effect, Option } from "effect";1718import type { CodeProject } from "../code.ts";19import { CodeCache } from "./cache.ts";2021const FULL_SHA = /^[0-9a-f]{40}$/u;2223export const isFullSha = (rev: string) => FULL_SHA.test(rev);2425const TAGS = "refs/tags/";26

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 27why, one step back
27const BRANCHES = "refs/heads/";

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 28why, one step back
28

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 29–38why, one step back
29const nameUnder = (prefix: string) => (ref: string) =>
30 ref.startsWith(prefix)31 ? Option.some(ref.slice(prefix.length))32 : Option.none();3334export const tagNameOf: (ref: string) => Option.Option<string> =35 nameUnder(TAGS);3637const branchNameOf = nameUnder(BRANCHES);38

Merge build/comments-project into mainoscabrielbefore adoption49efd2a

line 39why, one step back
39/** A miss is not kept: the commit may yet be pushed. */

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 40–59why, one step back
40export const commitAt = (
41 repo: RepoName,42 sha: CommitSha43): Effect.Effect<Option.Option<Commit>, RepoNotFound, RepoHost> =>44 Effect.gen(function* () {45 const cache = yield* CodeCache;4647 return yield* cache48 .through(49 `${repo}:commit:${sha}`,50 RepoHost.use((host) => host.readCommit(repo, sha)).pipe(51 Effect.flatMap(Effect.fromOption)52 )53 )54 .pipe(55 Effect.asSome,56 Effect.catchTag("NoSuchElementError", () => Effect.succeedNone)57 );58 });59

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 60–71why, one step back
60/**
61 * Where a revision resolved: the commit, and the repo that holds it. Only the62 * canonical repo's reads are kept in the code store; a fork's stay out of it.63 */64export interface Resolved {65 readonly canonical: boolean;66 readonly commit: CommitSha;67 readonly repo: RepoName;68}6970/** The repo of a fork the project lists, only ever its claim fork's own name. */71const listedFork = (

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 72why, one step back
72 project: CodeProject,

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 73–150why, one step back
73 address: ForkAddress
74): Effect.Effect<Option.Option<RepoName>> =>75 Effect.map(project.forks, (forks) =>76 Option.map(77 Array.findFirst(78 forks,79 (fork) =>80 fork.kind === "claim" &&81 fork.ask === address.ask &&82 sameHandle(fork.handle, address.handle) &&83 fork.repo ===84 claimForkName(project.project, address.ask, address.handle)85 ),86 ({ repo }) => repo87 )88 );8990const pointIn = (91 repo: RepoName,92 address: ForkAddress93): Effect.Effect<Option.Option<CommitSha>, never, RepoHost> =>94 ForkPoint.$match(address.point, {95 Branch: ({ name }) =>96 RepoHost.use((host) => host.refs(repo)).pipe(97 Effect.map((refs) =>98 Option.map(99 Array.findFirst([...refs], ([ref]) =>100 Option.contains(branchNameOf(ref), name)101 ),102 ([, sha]) => sha103 )104 ),105 Effect.catchTag("RepoNotFound", () => Effect.succeedNone)106 ),107 Commit: ({ sha }) =>108 commitAt(repo, sha).pipe(109 Effect.map(Option.map((found) => found.sha)),110 Effect.catchTag("RepoNotFound", () => Effect.succeedNone)111 ),112 });113114const forkAt = (115 project: CodeProject,116 address: ForkAddress117): Effect.Effect<Option.Option<Resolved>, never, RepoHost> =>118 Effect.flatMap(listedFork(project, address), (listed) =>119 Option.match(listed, {120 onNone: () => Effect.succeedNone,121 onSome: (repo) =>122 Effect.map(123 pointIn(repo, address),124 Option.map((commit): Resolved => ({ canonical: false, commit, repo }))125 ),126 })127 );128129const tagAt = (130 project: CodeProject,131 rev: string132): Effect.Effect<Option.Option<CommitSha>, never, RepoHost> =>133 // No tag holds a `:`, which git refuses in a ref name.134 rev.includes(":")135 ? Effect.succeedNone136 : RepoHost.use((host) => host.tags(project.repo)).pipe(137 Effect.catchTag("RepoNotFound", () => Effect.succeed([])),138 Effect.map((tags) =>139 Option.map(140 Array.findFirst(tags, ({ name }) =>141 Option.contains(tagNameOf(name), rev)142 ),143 ({ sha }) => sha144 )145 )146 );147148/** `main`, a full commit id, a tag by its exact name, then a fork address. */149export const resolveAt = (150 project: CodeProject,

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 151why, one step back
151 at: Option.Option<Rev>

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 152why, one step back
152): Effect.Effect<Resolved, UnknownRevision, RepoHost> => {

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 153–155why, one step back
153 const rev = Option.getOrElse(at, () => "main");
154 const unknown = new UnknownRevision({ rev });155

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 156–161why, one step back
156 const canonical = (commit: CommitSha): Resolved => ({
157 canonical: true,158 commit,159 repo: project.repo,160 });161

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 162–165why, one step back
162 if (rev === "main") {
163 return Effect.flatMap(project.head, (landed) =>164 Option.match(landed, {165 onNone: () => Effect.fail(unknown),

Merge redesign into mainoscabrielbefore adoptioncaedd21

line 166why, one step back
166 onSome: (sha) => Effect.succeed(canonical(sha)),

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 167–176why, one step back
167 })
168 );169 }170171 if (isFullSha(rev)) {172 const sha = CommitSha.make(rev);173174 return commitAt(project.repo, sha).pipe(175 Effect.catchTag("RepoNotFound", () => Effect.succeedNone),176 Effect.flatMap((found) =>

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 177–179why, one step back
177 Option.isSome(found)
178 ? Effect.succeed(canonical(sha))179 : Effect.fail(unknown)

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

lines 180–183why, one step back
180 )
181 );182 }183

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 184–194why, one step back
184 return Effect.flatMap(tagAt(project, rev), (tag) => {
185 if (Option.isSome(tag)) {186 return Effect.succeed(canonical(tag.value));187 }188189 return Option.match(forkAddressOf(rev), {190 onNone: () => Effect.fail(unknown),191 onSome: (address) =>192 forkAt(project, address).pipe(193 Effect.flatMap(Effect.fromOption),194 Effect.mapError(() => unknown)

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 195why, one step back
195 ),

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 196–197why, one step back
196 });
197 });

Merge branch 'build/903-code-reads'oscabrielbefore adoption7608aca

line 198why, one step back
198};

Merge redesign into mainoscabrielbefore adoptioncaedd21

lines 199–220why, one step back
199
200/**201 * The repo a commit is read in. With no `at`, the canonical repo; otherwise202 * `at` is resolved as every other read resolves it, so `main`, a full commit203 * id and a tag read the canonical repo, a listed fork's address reads that204 * fork, and any other text is `UnknownRevision`.205 */206export const repoAt = (207 project: CodeProject,208 at: Option.Option<Rev>209): Effect.Effect<Omit<Resolved, "commit">, UnknownRevision, RepoHost> =>210 Option.match(at, {211 onNone: () => Effect.succeed({ canonical: true, repo: project.repo }),212 onSome: (rev) =>213 Effect.map(214 resolveAt(project, Option.some(rev)),215 ({ canonical, repo }) => ({216 canonical,217 repo,218 })219 ),220 });