- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/view-changes.ts?version=316&format=json
111 linesFirst import 56% · capacity 40% · codex-capacity-chair 5%Last change capacity in version 243, 3 versions changed it
You're reading version 316. The newest is 318, and no version since then changed this file.
1import { createHash } from "node:crypto";
2import type { ReadView } from "../../src/engine/index.ts";
3import { ProjectError } from "./project-error.ts";
4import type { FileChange, Mode, Modes } from "./project-types.ts";
6/** The listed files whose text differs between two views, before and after. */
7export function changesBetween(before: ReadView, after: ReadView, files: Iterable<string>): FileChange[] {
8 const out: FileChange[] = [];
9 for (const path of [...new Set(files)].toSorted()) {
10 const b = before.hasFile(path) ? before.text(path) : null;
11 const a = after.hasFile(path) ? after.text(path) : null;
12 const mode = modeChange(before, after, path);
13 const bb = before.attrs(path).blob;
14 const ab = after.attrs(path).blob;
15 const blob = bb !== ab ? { before: bb, after: ab } : undefined;
16 if (a !== b || mode || blob)
17 out.push({ path, before: b, after: a, ...(mode ? { mode } : {}), ...(blob ? { blob } : {}) });
18 }
19 return out;
20}
22/** A changed file's text or blob, summed up: its size in UTF-8 bytes and its SHA-256, or a blob's hash. */
23export type FileSummary = { size: number; sha256: string } | { blob: string };
25/**
26 * changesBetween without the texts (abso issue 680, compare's summary): each changed file before and after as its size
27 * and SHA-256, or its blob's hash for a binary file. Each text is read, summed up and let go, so a compare of thousands
28 * of files never holds them all.
29 */
30export function summariesBetween(
31 before: ReadView,
32 after: ReadView,
33 files: Iterable<string>,
34): {
35 path: string;
36 before: FileSummary | null;
37 after: FileSummary | null;
38 mode?: { before: Mode; after: Mode };
39 blob?: { before: string | null; after: string | null };
40}[] {
41 const sum = (view: ReadView, path: string): FileSummary | null => {
42 if (!view.hasFile(path)) return null;
43 const blob = view.attrs(path).blob;
44 if (blob !== null) return { blob };
45 const text = view.text(path);
46 return { size: Buffer.byteLength(text, "utf8"), sha256: createHash("sha256").update(text, "utf8").digest("hex") };
47 };
48 const out: ReturnType<typeof summariesBetween> = [];
49 for (const path of [...new Set(files)].toSorted()) {
50 if (!differs(before, after, path)) continue;
51 const mode = modeChange(before, after, path);
52 const bb = before.attrs(path).blob;
53 const ab = after.attrs(path).blob;
54 out.push({
55 path,
56 before: sum(before, path),
57 after: sum(after, path),
58 ...(mode ? { mode } : {}),
59 ...(bb !== ab ? { blob: { before: bb, after: ab } } : {}),
60 });
61 }
62 return out;
63}
65/** Whether a file differs between two views: its text, its mode, or its blob (what changesBetween lists). */
66export function differs(before: ReadView, after: ReadView, path: string): boolean {
67 const had = before.hasFile(path);
68 if (had !== after.hasFile(path)) return true;
69 if (had && !before.sameText(path, after)) return true;
70 const b = before.attrs(path);
71 const a = after.attrs(path);
72 return b.exec !== a.exec || b.link !== a.link || b.blob !== a.blob;
73}
75/** A file's executable bit and link target, before and after, when either changed. */
76export function modeChange(before: ReadView, after: ReadView, path: string): { before: Mode; after: Mode } | undefined {
77 const b = before.attrs(path);
78 const a = after.attrs(path);
79 if (b.exec === a.exec && b.link === a.link) return undefined;
80 return { before: { exec: b.exec, link: b.link }, after: { exec: a.exec, link: a.link } };
81}
83/** Every executable file and link in a view. */
84export function modesOf(view: ReadView): Modes {
85 const out: Modes = {};
86 for (const path of view.paths()) {
87 const a = view.attrs(path);
88 if (a.exec || a.link !== null)
89 out[path] = { ...(a.exec ? { exec: true } : {}), ...(a.link !== null ? { link: a.link } : {}) };
90 }
91 return out;
92}
94export function checkModes(modes: unknown): Modes {
95 if (typeof modes !== "object" || modes === null || Array.isArray(modes))
96 throw new ProjectError("bad-request", "modes must be {path: {exec, link}}");
97 for (const [path, m] of Object.entries(modes as Record<string, unknown>)) {
98 const mode = m as Mode;
99 if (typeof m !== "object" || m === null) throw new ProjectError("bad-request", `${path}: a mode is {exec?, link?}`);
100 if (mode.exec !== undefined && typeof mode.exec !== "boolean")
101 throw new ProjectError("bad-request", `${path}: exec is true or false`);
102 if (
103 mode.link !== undefined &&
104 mode.link !== null &&
105 (typeof mode.link !== "string" || mode.link === "" || /[\n\r]/.test(mode.link))
106 ) {
107 throw new ProjectError("bad-request", `${path}: link is a target path or null`);
108 }
109 }
110 return modes as Modes;
111}