- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/at.ts?format=json
110 linesFirst import 92% · candidates 7% · rename 1%Last change candidates in version 186, 3 versions changed it
1// abso run --at <version> [--files <glob>]: run a past version of the
2// folder's space, or old copies of a few files, in the box, without touching
3// a file in the folder (Backtracker's bisect, #25). Swapping files in by hand
4// let the edit hook record the old copies.
5//
6// The version is rebuilt from what the client already knows, in two calls:
7// the known copies are this agent's view (its base plus its recorded draft);
8// its diff gives each drafted file as it was at the base; and compare(N, base)
9// gives each file that differs between version N and the base, as it was at N.
11import type { Api, FileChange, Where } from "./api.ts";
12import { UsageError } from "./args.ts";
13import { readKnownBlobs } from "./blobs.ts";
14import { isLocalOnly } from "./core.ts";
15import { listKnown, readKnown } from "./folder.ts";
16import { type FullMode, PLAIN, readKnownModes } from "./modes.ts";
17import type { TarEntry } from "./tar.ts";
19/** One file of a version: its text, or its bytes by hash; and its mode. */
20type Held = { readonly text: string } | { readonly blob: string };
22export interface Snapshot {
23 readonly version: number;
24 readonly files: Map<string, Held>;
25 readonly modes: Map<string, FullMode>;
26}
28/** Each file as it is on one side of a change (`before` unless said), applied to the snapshot. */
29export function applySide(s: Snapshot, changes: readonly FileChange[], side: "before" | "after" = "before"): void {
30 for (const f of changes) {
31 if (isLocalOnly(f.path)) continue;
32 const blob = f.blob?.[side] ?? null;
33 const text = f[side];
34 if (blob !== null) s.files.set(f.path, { blob });
35 else if (text !== null) s.files.set(f.path, { text });
36 else {
37 s.files.delete(f.path);
38 s.modes.delete(f.path);
39 continue;
40 }
41 if (f.mode) s.modes.set(f.path, { exec: f.mode[side].exec === true, link: f.mode[side].link ?? null });
42 }
43}
45/** The folder's space as it was at `version`: every file, and its mode. */
46export async function snapshotAt(root: string, api: Api, version: number): Promise<Snapshot> {
47 const diff = await api.diff();
48 if (diff.base === null) throw new UsageError("this folder has no version yet: run abso pull first");
49 const where: Where = { draft: diff.draft };
50 // This agent's view, from its known copies.
51 const s: Snapshot = { version, files: new Map(), modes: new Map() };
52 for (const rel of listKnown(root)) {
53 if (isLocalOnly(rel)) continue;
54 const text = readKnown(root, rel);
55 if (text !== undefined) s.files.set(rel, { text });
56 }
57 for (const [rel, sha] of readKnownBlobs(root)) s.files.set(rel, { blob: sha });
58 for (const [rel, mode] of readKnownModes(root)) if (s.files.has(rel)) s.modes.set(rel, mode);
59 // Its base: the draft's changes taken back out.
60 applySide(s, diff.files);
61 // Version N: what differs from the base, as it was at N.
62 if (version !== diff.base) applySide(s, (await api.compare(version, diff.base, where)).files);
63 return s;
64}
66/** A glob (or several, comma-separated) as a test on folder-relative paths. `**` crosses folders, `*` and `?` don't; a plain path matches itself and what is under it. */
67export function globTest(globs: string): (rel: string) => boolean {
68 const tests = globs
69 .split(",")
70 .map((g) => g.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, ""))
71 .filter((g) => g !== "")
72 .map((g) => {
73 if (!/[*?[]/.test(g)) return (rel: string) => rel === g || rel.startsWith(`${g}/`);
74 let re = "";
75 for (let i = 0; i < g.length; i++) {
76 const c = g[i]!;
77 if (c === "*" && g[i + 1] === "*") {
78 re += g[i + 2] === "/" ? "(?:.*/)?" : ".*";
79 i += g[i + 2] === "/" ? 2 : 1;
80 } else if (c === "*") re += "[^/]*";
81 else if (c === "?") re += "[^/]";
82 else re += c.replace(/[.+^${}()|[\]\\]/g, "\\$&");
83 }
84 const r = new RegExp(`^${re}$`);
85 return (rel: string) => r.test(rel);
86 });
87 if (tests.length === 0) throw new UsageError('--files needs a path or a glob, like --files "src/*.ts"');
88 return (rel) => tests.some((t) => t(rel));
89}
91/** The tar entries for a snapshot's files (all of them, or those `pick` takes), fetching blob bytes. */
92export async function snapshotEntries(
93 api: Api,
94 s: Snapshot,
95 pick: (rel: string) => boolean = () => true,
96): Promise<TarEntry[]> {
97 const out: TarEntry[] = [];
98 for (const [rel, held] of [...s.files].sort(([a], [b]) => a.localeCompare(b))) {
99 if (!pick(rel)) continue;
100 const mode = s.modes.get(rel) ?? PLAIN;
101 if (mode.link !== null) {
102 out.push({ path: rel, data: new Uint8Array(0), link: mode.link });
103 continue;
104 }
105 const data = "blob" in held ? await api.getBlob(held.blob) : new TextEncoder().encode(held.text);
106 const script = data[0] === 0x23 && data[1] === 0x21;
107 out.push({ path: rel, data, ...(mode.exec || script ? { mode: 0o755 } : {}) });
108 }
109 return out;
110}