Skip to content
Myr

files.ts

This file

Path

apps/web/src/features/finish/files.ts

167 lines · 4.3 KiB · TypeScript

Last changed by

Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72

History of this file

Take it

Merge build/1224-decider-pagesoscabrielbefore adoptiond22b7a2

lines 1–167why, one step back
1import { parsePatchFiles } from "@pierre/diffs";
2import type { FileDiffMetadata } from "@pierre/diffs";3import type { ChangedFile } from "@repo/protocol/domain/code";4import { RepoPath } from "@repo/protocol/ids";5import { Option, Schema } from "effect";67import {8 countsOf,9 globTest,10 isTestPath,11 LONG_FILE,12 treeOrder,13} from "../code/diff-model.ts";14import type { DrawnFile } from "../code/diff-model.ts";15import { fileBlocks, patchIdOf } from "./patch.ts";1617export type Part = "territory" | "tests" | "other";1819export interface PatchFile extends DrawnFile {20 readonly part: Part;21 readonly patchId: string;22}2324const STATUS = {25 change: "modified",26 deleted: "deleted",27 new: "added",28 "rename-changed": "modified",29 "rename-pure": "modified",30} as const satisfies Record<FileDiffMetadata["type"], ChangedFile["status"]>;3132const PART_ORDER = {33 other: 2,34 territory: 0,35 tests: 1,36} as const satisfies Record<Part, number>;3738const decodePath = Schema.decodeUnknownOption(RepoPath);3940export const partOf =41 (territory: readonly string[]) =>42 (path: string): Part => {43 if (isTestPath(path)) {44 return "tests";45 }4647 return territory.some((glob) => globTest(glob)(path))48 ? "territory"49 : "other";50 };5152const isBinary = (block: string) =>53 /^(?:Binary files .* differ|GIT binary patch)$/mu.test(block);5455export const patchFiles = (56 patch: string,57 territory: readonly string[],58 key: string59): readonly PatchFile[] => {60 const blocks = fileBlocks(patch);61 const part = partOf(territory);6263 const parsed = parsePatchFiles(patch).flatMap(({ files }) => files);6465 return parsed66 .flatMap((diff): readonly PatchFile[] => {67 const path = decodePath(diff.name);6869 if (Option.isNone(path)) {70 return [];71 }7273 const block = blocks.get(diff.name) ?? "";74 const binary = isBinary(block);75 const patchId = patchIdOf(block);7677 diff.cacheKey = `finish:${key}:${diff.name}:${patchId}`;7879 const shown = binary ? Option.none() : Option.some(diff);80 const counts = countsOf(shown);81 const status = STATUS[diff.type];8283 return [84 {85 counts,86 diff: shown,87 file: { binary, path: path.value, status },88 folded:89 status !== "modified" &&90 counts.additions + counts.deletions > LONG_FILE,91 part: part(diff.name),92 patchId,93 },94 ];95 })96 .toSorted(97 (a, b) =>98 PART_ORDER[a.part] - PART_ORDER[b.part] ||99 treeOrder(a.file.path, b.file.path)100 );101};102103export interface PatchLine {104 readonly kind: "addition" | "context" | "deletion";105 readonly number: number;106 readonly text: string;107}108109export type PatchRow =110 | { readonly kind: "separator"; readonly lines: number }111 | PatchLine;112113const bare = (line: string | undefined) => (line ?? "").replace(/\r?\n$/u, "");114115export const patchRows = (diff: FileDiffMetadata): readonly PatchRow[] => {116 const rows: PatchRow[] = [];117118 for (const hunk of diff.hunks) {119 if (hunk.collapsedBefore > 0) {120 rows.push({ kind: "separator", lines: hunk.collapsedBefore });121 }122123 let next = hunk.additionStart;124 let old = hunk.deletionStart;125126 for (const content of hunk.hunkContent) {127 if (content.type === "context") {128 for (let line = 0; line < content.lines; line += 1) {129 rows.push({130 kind: "context",131 number: next,132 text: bare(diff.additionLines[content.additionLineIndex + line]),133 });134 next += 1;135 old += 1;136 }137 } else {138 for (let line = 0; line < content.deletions; line += 1) {139 rows.push({140 kind: "deletion",141 number: old,142 text: bare(diff.deletionLines[content.deletionLineIndex + line]),143 });144 old += 1;145 }146147 for (let line = 0; line < content.additions; line += 1) {148 rows.push({149 kind: "addition",150 number: next,151 text: bare(diff.additionLines[content.additionLineIndex + line]),152 });153 next += 1;154 }155 }156 }157 }158159 return rows;160};161162export const drawnLines = (diff: FileDiffMetadata): ReadonlySet<number> =>163 new Set(164 patchRows(diff).flatMap((row) =>165 row.kind === "addition" || row.kind === "context" ? [row.number] : []166 )167 );