patch.ts
This file
Path
apps/web/src/features/finish/patch.ts
112 lines · 3.3 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–26why, one step back1import { DecisionSubject } from "@repo/protocol/domain/decision";2import type { Decision } from "@repo/protocol/domain/decision";3import { Array as Arr, Option } from "effect";45export const PATCH_LABEL = "patch";67export const FORK_LABEL = "fork";89export type FinishDecision = Decision & {10 readonly subject: Extract<DecisionSubject, { readonly _tag: "Finish" }>;11};1213const finishes = DecisionSubject.isAnyOf(["Finish"]);1415export const isFinishDecision = (16 decision: Decision17): decision is FinishDecision => finishes(decision.subject);1819export const patchOf = (decision: Decision): Option.Option<string> =>20 isFinishDecision(decision)21 ? Option.map(22 Arr.findFirst(decision.evidence, ({ label }) => label === PATCH_LABEL),23 ({ value }) => value24 ).pipe(Option.filter((patch) => patch.trim().length > 0))25 : Option.none();26Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 27–28why, one step back27// git ends a `---`/`+++` line with a tab when its path holds a space, and `diff28// -u` puts its timestamp after one: the path stops at the tab.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 29–57why, one step back29const NEW_FILE = /^\+\+\+ (?:b\/)?(?<path>[^\t]+)(?:\t.*)?$/u;3031const OLD_FILE = /^--- (?:a\/)?(?<path>[^\t]+)(?:\t.*)?$/u;3233const RENAMED = /^rename to (?<path>.+)$/u;3435const HEADER = /^diff --git a\/.+ b\/(?<path>.+)$/u;3637const pathOf = (block: readonly string[]): Option.Option<string> => {38 const first = (pattern: RegExp) =>39 Arr.findFirst(block, (line) => pattern.test(line)).pipe(40 Option.flatMap((line) =>41 Option.fromNullishOr(pattern.exec(line)?.groups?.path)42 )43 );4445 const added = first(NEW_FILE).pipe(46 Option.filter((path) => path !== "/dev/null")47 );4849 return added.pipe(50 Option.orElse(() =>51 first(OLD_FILE).pipe(Option.filter((path) => path !== "/dev/null"))52 ),53 Option.orElse(() => first(RENAMED)),54 Option.orElse(() => first(HEADER))55 );56};57Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
line 58why, one step back58export const fileBlocks = (patch: string): ReadonlyMap<string, string> => {Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 59–76why, one step back59 const lines = patch.split("\n");60 const gitHeaders = lines.some((line) => HEADER.test(line));6162 const starts = (line: string, next: string | undefined) =>63 gitHeaders64 ? line.startsWith("diff --git ")65 : OLD_FILE.test(line) && next !== undefined && NEW_FILE.test(next);6667 const blocks: string[][] = [];6869 for (const [index, line] of lines.entries()) {70 if (starts(line, lines[index + 1])) {71 blocks.push([line]);72 } else {73 blocks.at(-1)?.push(line);74 }75 }76Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
lines 77–83why, one step back77 return new Map(78 blocks.flatMap((block) =>79 Option.toArray(80 Option.map(pathOf(block), (path) => [path, block.join("\n")] as const)81 )82 )83 );Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
line 84why, one step back84};Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
lines 85–90why, one step back8586export const changedFiles = (patch: string): readonly string[] => [87 ...fileBlocks(patch).keys(),88];8990/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 91–92why, one step back91 * A new head that changes the file changes its id, so a mark kept against the92 * old one no longer matches.Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2
lines 93–112why, one step back93 */94export const patchIdOf = (text: string): string => {95 let hash = 7;9697 for (let index = 0; index < text.length; index += 1) {98 hash = (hash * 31 + (text.codePointAt(index) ?? 0)) % 4_294_967_291;99 }100101 return hash.toString(16).padStart(8, "0");102};103104const waives = DecisionSubject.isAnyOf(["Waiver"]);105106export const waiverPatchOf = (decision: Decision): Option.Option<string> =>107 waives(decision.subject)108 ? Option.map(109 Arr.findFirst(decision.evidence, ({ label }) => label === PATCH_LABEL),110 ({ value }) => value111 ).pipe(Option.filter((patch) => patch.trim().length > 0))112 : Option.none();