- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/at.ts?draft=login-timeout&format=json
109 linesFirst import 100%0 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`), applied to the snapshot. */
29function applyBefore(s: Snapshot, changes: readonly FileChange[]): void {
30 for (const f of changes) {
31 if (isLocalOnly(f.path)) continue;
32 const blob = f.blob?.before ?? null;
33 if (blob !== null) s.files.set(f.path, { blob });
34 else if (f.before !== null) s.files.set(f.path, { text: f.before });
35 else {
36 s.files.delete(f.path);
37 s.modes.delete(f.path);
38 continue;
39 }
40 if (f.mode) s.modes.set(f.path, { exec: f.mode.before.exec === true, link: f.mode.before.link ?? null });
41 }
42}
44/** The folder's space as it was at `version`: every file, and its mode. */
45export async function snapshotAt(root: string, api: Api, version: number): Promise<Snapshot> {
46 const diff = await api.diff();
47 if (diff.base === null) throw new UsageError("this folder has no version yet: run abso sync first");
48 const where: Where = { draft: diff.draft };
49 // This agent's view, from its known copies.
50 const s: Snapshot = { version, files: new Map(), modes: new Map() };
51 for (const rel of listKnown(root)) {
52 if (isLocalOnly(rel)) continue;
53 const text = readKnown(root, rel);
54 if (text !== undefined) s.files.set(rel, { text });
55 }
56 for (const [rel, sha] of readKnownBlobs(root)) s.files.set(rel, { blob: sha });
57 for (const [rel, mode] of readKnownModes(root)) if (s.files.has(rel)) s.modes.set(rel, mode);
58 // Its base: the draft's changes taken back out.
59 applyBefore(s, diff.files);
60 // Version N: what differs from the base, as it was at N.
61 if (version !== diff.base) applyBefore(s, (await api.compare(version, diff.base, where)).files);
62 return s;
63}
65/** 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. */
66export function globTest(globs: string): (rel: string) => boolean {
67 const tests = globs
68 .split(",")
69 .map((g) => g.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+$/, ""))
70 .filter((g) => g !== "")
71 .map((g) => {
72 if (!/[*?[]/.test(g)) return (rel: string) => rel === g || rel.startsWith(`${g}/`);
73 let re = "";
74 for (let i = 0; i < g.length; i++) {
75 const c = g[i]!;
76 if (c === "*" && g[i + 1] === "*") {
77 re += g[i + 2] === "/" ? "(?:.*/)?" : ".*";
78 i += g[i + 2] === "/" ? 2 : 1;
79 } else if (c === "*") re += "[^/]*";
80 else if (c === "?") re += "[^/]";
81 else re += c.replace(/[.+^${}()|[\]\\]/g, "\\$&");
82 }
83 const r = new RegExp(`^${re}$`);
84 return (rel: string) => r.test(rel);
85 });
86 if (tests.length === 0) throw new UsageError('--files needs a path or a glob, like --files "src/*.ts"');
87 return (rel) => tests.some((t) => t(rel));
88}
90/** The tar entries for a snapshot's files (all of them, or those `pick` takes), fetching blob bytes. */
91export async function snapshotEntries(
92 api: Api,
93 s: Snapshot,
94 pick: (rel: string) => boolean = () => true,
95): Promise<TarEntry[]> {
96 const out: TarEntry[] = [];
97 for (const [rel, held] of [...s.files].sort(([a], [b]) => a.localeCompare(b))) {
98 if (!pick(rel)) continue;
99 const mode = s.modes.get(rel) ?? PLAIN;
100 if (mode.link !== null) {
101 out.push({ path: rel, data: new Uint8Array(0), link: mode.link });
102 continue;
103 }
104 const data = "blob" in held ? await api.getBlob(held.blob) : new TextEncoder().encode(held.text);
105 const script = data[0] === 0x23 && data[1] === 0x21;
106 out.push({ path: rel, data, ...(mode.exec || script ? { mode: 0o755 } : {}) });
107 }
108 return out;
109}