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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221/** * `main` resolves from the project's record, never a ref; a tag name is * matched among the repo's tags, never joined into a ref path, so * `../heads/x` names nothing. A fork address (ADR 0043) is found among the * project's own listed forks, and its branch matched among that fork's * branches, so neither a layer's repo nor any repo the project does not list * is ever read. */import { RepoHost } from "@repo/artifacts/repo-host";import type { Commit, RepoNotFound } from "@repo/artifacts/repo-host";import { forkAddressOf, ForkPoint } from "@repo/protocol/domain/code";import type { ForkAddress, Rev } from "@repo/protocol/domain/code";import { claimForkName, CommitSha, sameHandle } from "@repo/protocol/ids";import type { RepoName } from "@repo/protocol/ids";import { UnknownRevision } from "@repo/protocol/refusal";import { Array, Effect, Option } from "effect";
import type { CodeProject } from "../code.ts";import { CodeCache } from "./cache.ts";
const FULL_SHA = /^[0-9a-f]{40}$/u;
export const isFullSha = (rev: string) => FULL_SHA.test(rev);
const TAGS = "refs/tags/";
const BRANCHES = "refs/heads/";
const nameUnder = (prefix: string) => (ref: string) => ref.startsWith(prefix) ? Option.some(ref.slice(prefix.length)) : Option.none();
export const tagNameOf: (ref: string) => Option.Option<string> = nameUnder(TAGS);
const branchNameOf = nameUnder(BRANCHES);
/** A miss is not kept: the commit may yet be pushed. */export const commitAt = ( repo: RepoName, sha: CommitSha): Effect.Effect<Option.Option<Commit>, RepoNotFound, RepoHost> => Effect.gen(function* () { const cache = yield* CodeCache;
return yield* cache .through( `${repo}:commit:${sha}`, RepoHost.use((host) => host.readCommit(repo, sha)).pipe( Effect.flatMap(Effect.fromOption) ) ) .pipe( Effect.asSome, Effect.catchTag("NoSuchElementError", () => Effect.succeedNone) ); });
/** * Where a revision resolved: the commit, and the repo that holds it. Only the * canonical repo's reads are kept in the code store; a fork's stay out of it. */export interface Resolved { readonly canonical: boolean; readonly commit: CommitSha; readonly repo: RepoName;}
/** The repo of a fork the project lists, only ever its claim fork's own name. */const listedFork = ( project: CodeProject, address: ForkAddress): Effect.Effect<Option.Option<RepoName>> => Effect.map(project.forks, (forks) => Option.map( Array.findFirst( forks, (fork) => fork.kind === "claim" && fork.ask === address.ask && sameHandle(fork.handle, address.handle) && fork.repo === claimForkName(project.project, address.ask, address.handle) ), ({ repo }) => repo ) );
const pointIn = ( repo: RepoName, address: ForkAddress): Effect.Effect<Option.Option<CommitSha>, never, RepoHost> => ForkPoint.$match(address.point, { Branch: ({ name }) => RepoHost.use((host) => host.refs(repo)).pipe( Effect.map((refs) => Option.map( Array.findFirst([...refs], ([ref]) => Option.contains(branchNameOf(ref), name) ), ([, sha]) => sha ) ), Effect.catchTag("RepoNotFound", () => Effect.succeedNone) ), Commit: ({ sha }) => commitAt(repo, sha).pipe( Effect.map(Option.map((found) => found.sha)), Effect.catchTag("RepoNotFound", () => Effect.succeedNone) ), });
const forkAt = ( project: CodeProject, address: ForkAddress): Effect.Effect<Option.Option<Resolved>, never, RepoHost> => Effect.flatMap(listedFork(project, address), (listed) => Option.match(listed, { onNone: () => Effect.succeedNone, onSome: (repo) => Effect.map( pointIn(repo, address), Option.map((commit): Resolved => ({ canonical: false, commit, repo })) ), }) );
const tagAt = ( project: CodeProject, rev: string): Effect.Effect<Option.Option<CommitSha>, never, RepoHost> => // No tag holds a `:`, which git refuses in a ref name. rev.includes(":") ? Effect.succeedNone : RepoHost.use((host) => host.tags(project.repo)).pipe( Effect.catchTag("RepoNotFound", () => Effect.succeed([])), Effect.map((tags) => Option.map( Array.findFirst(tags, ({ name }) => Option.contains(tagNameOf(name), rev) ), ({ sha }) => sha ) ) );
/** `main`, a full commit id, a tag by its exact name, then a fork address. */export const resolveAt = ( project: CodeProject, at: Option.Option<Rev>): Effect.Effect<Resolved, UnknownRevision, RepoHost> => { const rev = Option.getOrElse(at, () => "main"); const unknown = new UnknownRevision({ rev });
const canonical = (commit: CommitSha): Resolved => ({ canonical: true, commit, repo: project.repo, });
if (rev === "main") { return Effect.flatMap(project.head, (landed) => Option.match(landed, { onNone: () => Effect.fail(unknown), onSome: (sha) => Effect.succeed(canonical(sha)), }) ); }
if (isFullSha(rev)) { const sha = CommitSha.make(rev);
return commitAt(project.repo, sha).pipe( Effect.catchTag("RepoNotFound", () => Effect.succeedNone), Effect.flatMap((found) => Option.isSome(found) ? Effect.succeed(canonical(sha)) : Effect.fail(unknown) ) ); }
return Effect.flatMap(tagAt(project, rev), (tag) => { if (Option.isSome(tag)) { return Effect.succeed(canonical(tag.value)); }
return Option.match(forkAddressOf(rev), { onNone: () => Effect.fail(unknown), onSome: (address) => forkAt(project, address).pipe( Effect.flatMap(Effect.fromOption), Effect.mapError(() => unknown) ), }); });};
/** * The repo a commit is read in. With no `at`, the canonical repo; otherwise * `at` is resolved as every other read resolves it, so `main`, a full commit * id and a tag read the canonical repo, a listed fork's address reads that * fork, and any other text is `UnknownRevision`. */export const repoAt = ( project: CodeProject, at: Option.Option<Rev>): Effect.Effect<Omit<Resolved, "commit">, UnknownRevision, RepoHost> => Option.match(at, { onNone: () => Effect.succeed({ canonical: true, repo: project.repo }), onSome: (rev) => Effect.map( resolveAt(project, Option.some(rev)), ({ canonical, repo }) => ({ canonical, repo, }) ), });