- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/fetch.ts?version=155&format=json
92 linesFirst import 97% · review 2% · infra-split 1%Last change infra-split in version 37, 4 versions changed it
You're reading version 155. The newest is 318, and no version since then changed this file.
1// abso fetch <glob...> and abso run --fetch <glob>: bring files the box made
2// back into the folder (#37). Only paths abso ignores are written,
3// so a fetched file never becomes an edit; the rest are named and left alone.
4// Every path is screened as a sync's are (safety.ts): the box runs whatever
5// the folder held, so its files are not trusted any more than the server's.
7import { chmodSync, mkdirSync } from "node:fs";
8import { dirname } from "node:path";
9import type { Api } from "./api.ts";
10import { UsageError } from "./args.ts";
11import { isLocalOnly } from "./core.ts";
12import { absolute, loadIgnore, writeFileAtomic } from "./folder.ts";
13import { type Refused, throughLink, unsafePath } from "./safety.ts";
14import { untarGz } from "./tar.ts";
16export interface Fetched {
17 readonly written: string[];
18 /** Matched in the box but not written, and why. */
19 readonly skipped: Refused[];
20 /** Globs that matched nothing (or the box had stopped). */
21 readonly empty: string[];
22}
24/** Whether abso leaves `rel` out: itself, or a folder on its way (git's rule: nothing under an ignored folder counts). */
25export function ignoredHere(ignore: ReturnType<typeof loadIgnore>, rel: string, isDir = false): boolean {
26 const parts = rel.split("/");
27 for (let i = 1; i < parts.length; i++) if (ignore(parts.slice(0, i).join("/"), true)) return true;
28 return ignore(rel, isDir);
29}
31export async function fetchFromBox(root: string, api: Api, globs: readonly string[]): Promise<Fetched> {
32 if (globs.length === 0) throw new UsageError('name what to fetch: abso fetch <glob...>, like abso fetch "out/*.png"');
33 const ignore = loadIgnore(root);
34 const written: string[] = [];
35 const skipped: Refused[] = [];
36 const empty: string[] = [];
37 const seen = new Set<string>();
38 for (const glob of globs) {
39 const gz = await api.boxFiles(glob);
40 if (gz === null) {
41 empty.push(glob);
42 continue;
43 }
44 for (const e of untarGz(gz)) {
45 if (seen.has(e.path)) continue;
46 seen.add(e.path);
47 const why =
48 unsafePath(e.path) ??
49 (isLocalOnly(e.path) ? "it is under .git, .claude, .abso or node_modules, which stay local" : undefined) ??
50 (throughLink(root, e.path) ? "a folder on its way is a link" : undefined) ??
51 (e.link !== undefined ? "it is a link; fetch makes none" : undefined) ??
52 (!ignoredHere(ignore, e.path)
53 ? "abso records it, so writing it would make an edit; add it to .absoignore to fetch it"
54 : undefined);
55 if (why !== undefined) {
56 skipped.push({ path: e.path, why });
57 continue;
58 }
59 const path = absolute(root, e.path);
60 mkdirSync(dirname(path), { recursive: true });
61 writeFileAtomic(path, e.data);
62 if (e.mode !== undefined && (e.mode & 0o111) !== 0) {
63 try {
64 chmodSync(path, 0o755);
65 } catch {
66 // The bit is a convenience here.
67 }
68 }
69 written.push(e.path);
70 }
71 }
72 return { written: written.sort(), skipped: skipped.sort((a, b) => a.path.localeCompare(b.path)), empty };
73}
75/** What a fetch says: what it wrote, what it left alone and why, and globs that found nothing. */
76export function fetchedText(f: Fetched): string[] {
77 const shown = (list: readonly string[]) =>
78 list.length <= 8 ? list.join(", ") : `${list.slice(0, 8).join(", ")}, and ${list.length - 8} more`;
79 return [
80 ...(f.written.length > 0
81 ? [
82 `Fetched ${f.written.length === 1 ? "1 file" : `${f.written.length} files`} from the box: ${shown(f.written)}.`,
83 ]
84 : []),
85 ...(f.skipped.length > 0
86 ? [`Not fetched (${f.skipped.length}):`, ...f.skipped.map((s) => ` ${s.path}: ${s.why}`)]
87 : []),
88 ...f.empty.map(
89 (g) => `Nothing in the box matches ${g} (or the box has stopped: it keeps /w only until it idles out).`,
90 ),
91 ];
92}