answer-base.ts
This file
Path
packages/git/src/answer-base.ts
119 lines · 3.8 KiB · TypeScript
Last changed by
Merge project/claim-base-moves into mainoscabrielbefore adoptiondb02276
History of this fileTake it
Merge project/claim-base-moves into mainoscabrielbefore adoptiondb02276
lines 1–119why, one step back1import { RepoHost, Revision } from "@repo/artifacts/repo-host";2import type { RepoNotFound } from "@repo/artifacts/repo-host";3import type { CommitSha, LandedSha, RepoName } from "@repo/protocol/ids";4import { Effect, Option } from "effect";56/** How far back from `landed` main's landings are read for the base, in one log. */7const LANDINGS_READ = 256;89/** How many of the head's own commits are read before the walk gives up. */10const COMMITS_READ = 512;1112const READS_AT_ONCE = 8;1314export interface AnswerBaseInput {15 /** The repo `landed` and main's history live in. */16 readonly canonical: RepoName;17 /** The repo `head` lives in: the answer's fork, or the canonical repo itself. */18 readonly repo: RepoName;19 /** Where the answer's history left main's, as the claim's grant recorded it. */20 readonly base: CommitSha;21 readonly head: CommitSha;22 readonly landed: LandedSha;23}2425/**26 * The base an answer is judged from: the newest of main's landings its head27 * contains, from `landed` back to the claim's `base`. An answer that merged28 * or rebased onto a main that moved under it is diffed from that main, so a29 * sibling's landed paths are not its own; one that stayed on its base is30 * diffed from the base, as before. The gateway, a submit and the local check31 * choose the base here, so the same change keeps the same patch id wherever32 * it is judged. A head that contains none of the landings, or whose history33 * is too long to walk, keeps the claim's base, as it did before.34 */35export const answerBase = (36 input: AnswerBaseInput37): Effect.Effect<CommitSha, RepoNotFound, RepoHost> =>38 Effect.gen(function* () {39 if (input.base === input.landed) {40 return input.base;41 }4243 const host = yield* RepoHost;4445 const chain = yield* host.log(46 input.canonical,47 Revision.Commit({ sha: input.landed }),48 LANDINGS_READ49 );5051 // Newest first, cut before the claim's base: a chain the base is not on52 // within the limit offers nothing, so the base stands.53 const since = chain.findIndex((commit) => commit.sha === input.base);54 const landings = since === -1 ? [] : chain.slice(0, since);55 const rank = new Map(landings.map((commit, index) => [commit.sha, index]));5657 if (landings.length === 0) {58 return input.base;59 }6061 const seen = new Set<CommitSha>([input.head]);62 let frontier: readonly CommitSha[] = [input.head];63 let best = Option.none<number>();64 let reads = 0;6566 // Breadth first over every parent, since a merge of main is a second67 // parent; a landing or the base ends its own branch of the walk.68 while (frontier.length > 0 && reads < COMMITS_READ) {69 const unread: CommitSha[] = [];7071 for (const sha of frontier) {72 const found = rank.get(sha);7374 if (found !== undefined) {75 best = Option.some(76 Option.match(best, {77 onNone: () => found,78 onSome: (held) => Math.min(held, found),79 })80 );81 } else if (sha !== input.base) {82 unread.push(sha);83 }84 }8586 if (Option.contains(best, 0)) {87 return input.landed;88 }8990 const read = yield* Effect.forEach(91 unread,92 (sha) => host.readCommit(input.repo, sha),93 { concurrency: READS_AT_ONCE }94 );9596 reads += unread.length;9798 frontier = read.flatMap((commit) =>99 Option.match(commit, {100 onNone: () => [],101 onSome: ({ parents }) =>102 parents.filter((parent) => {103 if (seen.has(parent)) {104 return false;105 }106107 seen.add(parent);108109 return true;110 }),111 })112 );113 }114115 return Option.match(best, {116 onNone: () => input.base,117 onSome: (index) => landings[index]?.sha ?? input.base,118 });119 });