@foldrun/core 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +239 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +43 -0
- package/dist/src/agent-tools.d.ts +31 -0
- package/dist/src/agent-tools.js +99 -0
- package/dist/src/agents-md.d.ts +9 -0
- package/dist/src/agents-md.js +83 -0
- package/dist/src/api-tools.d.ts +38 -0
- package/dist/src/api-tools.js +313 -0
- package/dist/src/approvals.d.ts +39 -0
- package/dist/src/approvals.js +103 -0
- package/dist/src/arrange.d.ts +8 -0
- package/dist/src/arrange.js +40 -0
- package/dist/src/catalog.d.ts +108 -0
- package/dist/src/catalog.js +255 -0
- package/dist/src/completions.d.ts +61 -0
- package/dist/src/completions.js +578 -0
- package/dist/src/confine.d.ts +31 -0
- package/dist/src/confine.js +298 -0
- package/dist/src/container.d.ts +30 -0
- package/dist/src/container.js +172 -0
- package/dist/src/context-tools.d.ts +81 -0
- package/dist/src/context-tools.js +347 -0
- package/dist/src/deploy.d.ts +100 -0
- package/dist/src/deploy.js +377 -0
- package/dist/src/egress.d.ts +75 -0
- package/dist/src/egress.js +133 -0
- package/dist/src/evals.d.ts +150 -0
- package/dist/src/evals.js +651 -0
- package/dist/src/flow-lint.d.ts +16 -0
- package/dist/src/flow-lint.js +177 -0
- package/dist/src/git-hook.d.ts +1 -0
- package/dist/src/git-hook.js +70 -0
- package/dist/src/git.d.ts +37 -0
- package/dist/src/git.js +101 -0
- package/dist/src/gitrepo.d.ts +95 -0
- package/dist/src/gitrepo.js +355 -0
- package/dist/src/history.d.ts +82 -0
- package/dist/src/history.js +259 -0
- package/dist/src/kinds.d.ts +95 -0
- package/dist/src/kinds.js +421 -0
- package/dist/src/library.d.ts +94 -0
- package/dist/src/library.js +341 -0
- package/dist/src/linkable.d.ts +20 -0
- package/dist/src/linkable.js +113 -0
- package/dist/src/model-probe.d.ts +21 -0
- package/dist/src/model-probe.js +81 -0
- package/dist/src/notify.d.ts +61 -0
- package/dist/src/notify.js +252 -0
- package/dist/src/oauth-presets.d.ts +13 -0
- package/dist/src/oauth-presets.js +39 -0
- package/dist/src/okf.d.ts +220 -0
- package/dist/src/okf.js +656 -0
- package/dist/src/openapi.d.ts +66 -0
- package/dist/src/openapi.js +342 -0
- package/dist/src/paths.d.ts +33 -0
- package/dist/src/paths.js +78 -0
- package/dist/src/platform.d.ts +32 -0
- package/dist/src/platform.js +48 -0
- package/dist/src/providers.d.ts +47 -0
- package/dist/src/providers.js +114 -0
- package/dist/src/refs.d.ts +20 -0
- package/dist/src/refs.js +78 -0
- package/dist/src/run-container.d.ts +227 -0
- package/dist/src/run-container.js +760 -0
- package/dist/src/runner.d.ts +241 -0
- package/dist/src/runner.js +3215 -0
- package/dist/src/runtime.d.ts +41 -0
- package/dist/src/runtime.js +357 -0
- package/dist/src/script-tools.d.ts +62 -0
- package/dist/src/script-tools.js +296 -0
- package/dist/src/secret-files.d.ts +12 -0
- package/dist/src/secret-files.js +92 -0
- package/dist/src/secrets.d.ts +147 -0
- package/dist/src/secrets.js +626 -0
- package/dist/src/starter.d.ts +21 -0
- package/dist/src/starter.js +399 -0
- package/dist/src/step-exec.d.ts +136 -0
- package/dist/src/step-exec.js +391 -0
- package/dist/src/storage.d.ts +123 -0
- package/dist/src/storage.js +640 -0
- package/dist/src/store.d.ts +1052 -0
- package/dist/src/store.js +2378 -0
- package/dist/src/tar.d.ts +30 -0
- package/dist/src/tar.js +111 -0
- package/dist/src/tool-names.d.ts +39 -0
- package/dist/src/tool-names.js +79 -0
- package/dist/src/tool-programs.d.ts +18 -0
- package/dist/src/tool-programs.js +52 -0
- package/dist/src/tool-test.d.ts +19 -0
- package/dist/src/tool-test.js +335 -0
- package/dist/src/translator.d.ts +159 -0
- package/dist/src/translator.js +986 -0
- package/dist/src/triggers.d.ts +60 -0
- package/dist/src/triggers.js +270 -0
- package/dist/src/webhook.d.ts +47 -0
- package/dist/src/webhook.js +148 -0
- package/index.ts +44 -0
- package/package.json +64 -0
- package/src/agent-tools.ts +138 -0
- package/src/agents-md.ts +81 -0
- package/src/api-tools.ts +389 -0
- package/src/approvals.ts +129 -0
- package/src/arrange.ts +42 -0
- package/src/catalog.ts +321 -0
- package/src/completions.ts +652 -0
- package/src/confine.ts +350 -0
- package/src/container.ts +220 -0
- package/src/context-tools.ts +435 -0
- package/src/deploy.ts +458 -0
- package/src/egress.ts +176 -0
- package/src/evals.ts +812 -0
- package/src/flow-lint.ts +215 -0
- package/src/git-hook.ts +73 -0
- package/src/git.ts +117 -0
- package/src/gitrepo.ts +391 -0
- package/src/history.ts +329 -0
- package/src/kinds.ts +507 -0
- package/src/library.ts +419 -0
- package/src/linkable.ts +123 -0
- package/src/model-probe.ts +112 -0
- package/src/notify.ts +281 -0
- package/src/oauth-presets.ts +54 -0
- package/src/okf.ts +768 -0
- package/src/openapi.ts +383 -0
- package/src/paths.ts +79 -0
- package/src/platform.ts +87 -0
- package/src/providers.ts +147 -0
- package/src/refs.ts +83 -0
- package/src/run-container.ts +883 -0
- package/src/runner.ts +3446 -0
- package/src/runtime.ts +404 -0
- package/src/script-tools.ts +383 -0
- package/src/secret-files.ts +101 -0
- package/src/secrets.ts +799 -0
- package/src/starter.ts +415 -0
- package/src/step-exec.ts +498 -0
- package/src/storage.ts +771 -0
- package/src/store.ts +2985 -0
- package/src/tar.ts +124 -0
- package/src/tool-names.ts +91 -0
- package/src/tool-programs.ts +65 -0
- package/src/tool-test.ts +401 -0
- package/src/translator.ts +1029 -0
- package/src/triggers.ts +287 -0
- package/src/webhook.ts +183 -0
- package/templates/blog-desk/AGENTS.md +13 -0
- package/templates/blog-desk/agents/researcher/agent.md +11 -0
- package/templates/blog-desk/agents/writer/agent.md +8 -0
- package/templates/blog-desk/evals/writer-quality.md +10 -0
- package/templates/blog-desk/flows/publish.md +10 -0
- package/templates/blog-desk/knowledge/house-style.md +9 -0
- package/templates/blog-desk/knowledge/index.md +9 -0
- package/templates/hello/AGENTS.md +18 -0
- package/templates/hello/agents/curator/agent.md +32 -0
- package/templates/hello/agents/notetaker/agent.md +24 -0
- package/templates/hello/evals/house-style.md +16 -0
- package/templates/hello/flows/memory-review.md +7 -0
- package/templates/hello/flows/note.md +7 -0
- package/templates/hello/knowledge/house-style.md +15 -0
- package/templates/hello/knowledge/index.md +9 -0
- package/templates/hello/skills/plain-english/SKILL.md +11 -0
- package/templates/hello/state/publishing.md +7 -0
- package/templates/hello/tools/wordcount/run.py +13 -0
- package/templates/hello/tools/wordcount/tool.md +22 -0
package/src/gitrepo.ts
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
// A git repository per workspace — the platform as the remote.
|
|
2
|
+
//
|
|
3
|
+
// git remote add foldrun https://app.foldrun.io/git/<tenant>/<workspace>.git
|
|
4
|
+
// git push foldrun main → deploys, commit on record
|
|
5
|
+
// git clone …/<workspace>.git → the workspace, with its whole history
|
|
6
|
+
//
|
|
7
|
+
// One bare repository per workspace on the data volume. Every change the
|
|
8
|
+
// platform makes — a dashboard save, a deploy, a deletion — is a commit into
|
|
9
|
+
// it, made with git's own plumbing against a scratch index so no working copy
|
|
10
|
+
// is ever kept. Every change a person makes is a push into it. So there is
|
|
11
|
+
// one history, it is real git, and anyone can take it away with `git clone`.
|
|
12
|
+
//
|
|
13
|
+
// This deliberately stops at being a remote. Pull requests, reviews and
|
|
14
|
+
// issues are a forge, which is a product; GitHub and GitLab remain a fine
|
|
15
|
+
// mirror for anyone who wants that on top.
|
|
16
|
+
//
|
|
17
|
+
// history.ts delegates here when a repository exists, so the History page,
|
|
18
|
+
// the per-file panel and Observe's eval attribution all read git without
|
|
19
|
+
// knowing it. The journal it kept before stays as the fallback for an install
|
|
20
|
+
// without a git binary.
|
|
21
|
+
|
|
22
|
+
import fs from "node:fs";
|
|
23
|
+
import os from "node:os";
|
|
24
|
+
import path from "node:path";
|
|
25
|
+
import { spawnSync } from "node:child_process";
|
|
26
|
+
import { dataRoot } from "./paths.ts";
|
|
27
|
+
import type { DiffOp, Revision, RevisionFile, RevisionSummary } from "./history.ts";
|
|
28
|
+
|
|
29
|
+
const SAFE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,120}$/;
|
|
30
|
+
const safe = (v: string, what: string) => {
|
|
31
|
+
if (!SAFE.test(v)) throw new Error(`illegal ${what}: ${v}`);
|
|
32
|
+
return v;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** Where a workspace's bare repository lives. `@library` is the account shelf. */
|
|
36
|
+
export function repoDir(tenant: string, scope: string): string {
|
|
37
|
+
safe(tenant, "tenant");
|
|
38
|
+
const name = scope === "@library" ? "_library" : safe(scope, "workspace");
|
|
39
|
+
return path.join(dataRoot(), tenant, ".git", `${name}.git`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let gitChecked: boolean | null = null;
|
|
43
|
+
/** Is there a git binary? Checked once; an install without one keeps the journal. */
|
|
44
|
+
export function gitAvailable(): boolean {
|
|
45
|
+
if (gitChecked === null) {
|
|
46
|
+
gitChecked = spawnSync("git", ["--version"], { stdio: "ignore" }).status === 0;
|
|
47
|
+
}
|
|
48
|
+
return gitChecked;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function git(dir: string, args: string[], opts: { input?: string | Buffer; env?: Record<string, string> } = {}) {
|
|
52
|
+
const r = spawnSync("git", ["--git-dir", dir, ...args], {
|
|
53
|
+
input: opts.input,
|
|
54
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0", ...opts.env },
|
|
55
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
56
|
+
});
|
|
57
|
+
if (r.status !== 0) {
|
|
58
|
+
throw new Error(`git ${args[0]} failed: ${(r.stderr?.toString() ?? "").trim().slice(0, 500)}`);
|
|
59
|
+
}
|
|
60
|
+
return r.stdout.toString();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function repoExists(tenant: string, scope: string): boolean {
|
|
64
|
+
return fs.existsSync(path.join(repoDir(tenant, scope), "HEAD"));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Create the repository if it is not there. Idempotent. */
|
|
68
|
+
export function ensureRepo(tenant: string, scope: string): string {
|
|
69
|
+
const dir = repoDir(tenant, scope);
|
|
70
|
+
if (!fs.existsSync(path.join(dir, "HEAD"))) {
|
|
71
|
+
fs.mkdirSync(path.dirname(dir), { recursive: true });
|
|
72
|
+
spawnSync("git", ["init", "--bare", "--quiet", "--initial-branch=main", dir], { stdio: "ignore" });
|
|
73
|
+
// Pushes over HTTP need this; it is the one config a bare remote wants.
|
|
74
|
+
git(dir, ["config", "http.receivepack", "true"]);
|
|
75
|
+
}
|
|
76
|
+
installHooks(dir);
|
|
77
|
+
return dir;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function headSha(tenant: string, scope: string, ref = "refs/heads/main"): string | null {
|
|
81
|
+
if (!repoExists(tenant, scope)) return null;
|
|
82
|
+
const r = spawnSync("git", ["--git-dir", repoDir(tenant, scope), "rev-parse", "--verify", "--quiet", ref]);
|
|
83
|
+
return r.status === 0 ? r.stdout.toString().trim() : null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Apply a change set as one commit on main, using plumbing against a scratch
|
|
88
|
+
* index seeded from HEAD. No working copy, no checkout: each changed path is
|
|
89
|
+
* hashed straight from its content into the object store.
|
|
90
|
+
*
|
|
91
|
+
* Returns the new commit, or null when the tree did not change.
|
|
92
|
+
*/
|
|
93
|
+
export function commitChanges(
|
|
94
|
+
tenant: string,
|
|
95
|
+
scope: string,
|
|
96
|
+
files: RevisionFile[],
|
|
97
|
+
meta: { by?: string; message?: string; at?: string } = {},
|
|
98
|
+
): string | null {
|
|
99
|
+
const dir = ensureRepo(tenant, scope);
|
|
100
|
+
const parent = headSha(tenant, scope);
|
|
101
|
+
const scratch = fs.mkdtempSync(path.join(os.tmpdir(), "foldrun-index-"));
|
|
102
|
+
const index = path.join(scratch, "index");
|
|
103
|
+
// update-index insists on a work tree even when every entry arrives as
|
|
104
|
+
// --cacheinfo and nothing on disk is read. The scratch directory is one.
|
|
105
|
+
const env = { GIT_INDEX_FILE: index, GIT_WORK_TREE: scratch };
|
|
106
|
+
try {
|
|
107
|
+
if (parent) git(dir, ["read-tree", parent], { env });
|
|
108
|
+
for (const f of files) {
|
|
109
|
+
if (f.after === null) {
|
|
110
|
+
if (parent) git(dir, ["update-index", "--force-remove", "--", f.path], { env });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const blob = git(dir, ["hash-object", "-w", "--stdin"], { input: f.after }).trim();
|
|
114
|
+
// Executable where code lives, matching what the writers chmod on disk.
|
|
115
|
+
const mode = /\.(py|sh|mjs|js|rb)$/.test(f.path) && /(^|\/)(scripts|tools)\//.test(f.path) ? "100755" : "100644";
|
|
116
|
+
git(dir, ["update-index", "--add", "--cacheinfo", `${mode},${blob},${f.path}`], { env });
|
|
117
|
+
}
|
|
118
|
+
const tree = git(dir, ["write-tree"], { env }).trim();
|
|
119
|
+
if (parent && git(dir, ["rev-parse", `${parent}^{tree}`]).trim() === tree) return null;
|
|
120
|
+
|
|
121
|
+
const who = meta.by ?? "system";
|
|
122
|
+
const email = who.includes("@") ? who : `${who}@foldrun`;
|
|
123
|
+
const name = who.includes("@") ? who.split("@")[0] : who;
|
|
124
|
+
const date = meta.at ? new Date(meta.at).toISOString() : new Date().toISOString();
|
|
125
|
+
const commit = git(
|
|
126
|
+
dir,
|
|
127
|
+
["commit-tree", tree, ...(parent ? ["-p", parent] : []), "-m", meta.message ?? describe(files)],
|
|
128
|
+
{
|
|
129
|
+
env: {
|
|
130
|
+
GIT_AUTHOR_NAME: name, GIT_AUTHOR_EMAIL: email, GIT_AUTHOR_DATE: date,
|
|
131
|
+
GIT_COMMITTER_NAME: "foldrun", GIT_COMMITTER_EMAIL: "platform@foldrun", GIT_COMMITTER_DATE: date,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
).trim();
|
|
135
|
+
git(dir, ["update-ref", "refs/heads/main", commit, ...(parent ? [parent] : [])]);
|
|
136
|
+
return commit;
|
|
137
|
+
} finally {
|
|
138
|
+
fs.rmSync(scratch, { recursive: true, force: true });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function describe(files: RevisionFile[]): string {
|
|
143
|
+
if (files.length === 1) {
|
|
144
|
+
const f = files[0];
|
|
145
|
+
return `${f.before === null ? "created" : f.after === null ? "deleted" : "edited"} ${f.path}`;
|
|
146
|
+
}
|
|
147
|
+
return `changed ${files.length} files`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ------------------------------------------------------------------ reading
|
|
151
|
+
|
|
152
|
+
const SEP = "";
|
|
153
|
+
|
|
154
|
+
/** Commits on main, newest first, as the same summary the journal gave. */
|
|
155
|
+
export function listCommits(
|
|
156
|
+
tenant: string,
|
|
157
|
+
scope: string,
|
|
158
|
+
opts: { forPath?: string; limit?: number; ref?: string } = {},
|
|
159
|
+
): RevisionSummary[] {
|
|
160
|
+
if (!headSha(tenant, scope, opts.ref)) return [];
|
|
161
|
+
const dir = repoDir(tenant, scope);
|
|
162
|
+
const out = git(dir, [
|
|
163
|
+
"log",
|
|
164
|
+
opts.ref ?? "refs/heads/main",
|
|
165
|
+
`--max-count=${opts.limit ?? 200}`,
|
|
166
|
+
`--format=%H${SEP}%aI${SEP}%ae${SEP}%s`,
|
|
167
|
+
"--name-only",
|
|
168
|
+
...(opts.forPath ? ["--", opts.forPath] : []),
|
|
169
|
+
]);
|
|
170
|
+
const rows: RevisionSummary[] = [];
|
|
171
|
+
let current: RevisionSummary | null = null;
|
|
172
|
+
for (const line of out.split("\n")) {
|
|
173
|
+
if (line.includes(SEP)) {
|
|
174
|
+
const [id, at, by, message] = line.split(SEP);
|
|
175
|
+
current = { id, at, by: by.endsWith("@foldrun") ? by.replace(/@foldrun$/, "") : by, message, commit: id, paths: [] };
|
|
176
|
+
rows.push(current);
|
|
177
|
+
} else if (line.trim() && current) {
|
|
178
|
+
current.paths.push(line.trim());
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return rows;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** One commit in full: every changed file with its before and after. */
|
|
185
|
+
export function readCommit(tenant: string, scope: string, sha: string, forPath?: string): Revision | null {
|
|
186
|
+
if (!repoExists(tenant, scope) || !/^[0-9a-f]{4,40}$/.test(sha)) return null;
|
|
187
|
+
const dir = repoDir(tenant, scope);
|
|
188
|
+
const head = spawnSync("git", ["--git-dir", dir, "rev-parse", "--verify", "--quiet", `${sha}^{commit}`]);
|
|
189
|
+
if (head.status !== 0) return null;
|
|
190
|
+
const full = head.stdout.toString().trim();
|
|
191
|
+
const [at, by, ...msg] = git(dir, ["log", "-1", `--format=%aI${SEP}%ae${SEP}%s`, full]).trim().split(SEP);
|
|
192
|
+
const hasParent = spawnSync("git", ["--git-dir", dir, "rev-parse", "--verify", "--quiet", `${full}^`]).status === 0;
|
|
193
|
+
const names = git(dir, [
|
|
194
|
+
"diff-tree", "--no-commit-id", "--name-status", "-r", "--root", full,
|
|
195
|
+
...(forPath ? ["--", forPath] : []),
|
|
196
|
+
]);
|
|
197
|
+
const files: RevisionFile[] = [];
|
|
198
|
+
for (const line of names.split("\n").filter(Boolean)) {
|
|
199
|
+
const [status, p] = line.split("\t");
|
|
200
|
+
const before = status === "A" || !hasParent ? null : blob(dir, `${full}^`, p);
|
|
201
|
+
const after = status === "D" ? null : blob(dir, full, p);
|
|
202
|
+
files.push({ path: p, before, after });
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
id: full,
|
|
206
|
+
at,
|
|
207
|
+
by: by.endsWith("@foldrun") ? by.replace(/@foldrun$/, "") : by,
|
|
208
|
+
message: msg.join(SEP),
|
|
209
|
+
commit: full,
|
|
210
|
+
files,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function blob(dir: string, rev: string, p: string): string | null {
|
|
215
|
+
const r = spawnSync("git", ["--git-dir", dir, "show", `${rev}:${p}`], { maxBuffer: 64 * 1024 * 1024 });
|
|
216
|
+
return r.status === 0 ? r.stdout.toString() : null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export interface RefInfo {
|
|
220
|
+
name: string;
|
|
221
|
+
sha: string;
|
|
222
|
+
/** Commit subject and time, for a branch list that says what each is. */
|
|
223
|
+
subject: string;
|
|
224
|
+
at: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function listRefs(tenant: string, scope: string, kind: "heads" | "tags"): RefInfo[] {
|
|
228
|
+
if (!repoExists(tenant, scope)) return [];
|
|
229
|
+
const out = git(repoDir(tenant, scope), [
|
|
230
|
+
"for-each-ref", `refs/${kind}`, "--sort=-committerdate",
|
|
231
|
+
`--format=%(refname:short)${SEP}%(objectname)${SEP}%(subject)${SEP}%(committerdate:iso-strict)`,
|
|
232
|
+
]);
|
|
233
|
+
return out.split("\n").filter(Boolean).map((l) => {
|
|
234
|
+
const [name, sha, subject, at] = l.split(SEP);
|
|
235
|
+
return { name, sha, subject, at };
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The files at a commit, for a browser or a deploy. */
|
|
240
|
+
export function listTree(tenant: string, scope: string, ref = "refs/heads/main"): { path: string; mode: string }[] {
|
|
241
|
+
if (!headSha(tenant, scope, ref)) return [];
|
|
242
|
+
return git(repoDir(tenant, scope), ["ls-tree", "-r", "--format=%(objectmode) %(path)", ref])
|
|
243
|
+
.split("\n")
|
|
244
|
+
.filter(Boolean)
|
|
245
|
+
.map((l) => {
|
|
246
|
+
const [mode, ...rest] = l.split(" ");
|
|
247
|
+
return { mode, path: rest.join(" ") };
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export function readBlob(tenant: string, scope: string, ref: string, p: string): string | null {
|
|
252
|
+
if (!repoExists(tenant, scope)) return null;
|
|
253
|
+
return blob(repoDir(tenant, scope), ref, p);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** The whole tree at a ref as deploy files — what a push turns into a workspace. */
|
|
257
|
+
export function filesAt(tenant: string, scope: string, ref: string): { path: string; content: string }[] {
|
|
258
|
+
const dir = repoDir(tenant, scope);
|
|
259
|
+
return listTree(tenant, scope, ref).flatMap((t) => {
|
|
260
|
+
const content = blob(dir, ref, t.path);
|
|
261
|
+
return content === null ? [] : [{ path: t.path, content }];
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* What a ref would change on main: every file that differs, with both sides.
|
|
267
|
+
* The branch's own commits are `listCommits({ ref })`; this is the net
|
|
268
|
+
* effect, which is what a reviewer reads.
|
|
269
|
+
*/
|
|
270
|
+
export function diffRefs(tenant: string, scope: string, base: string, head: string): RevisionFile[] {
|
|
271
|
+
if (!headSha(tenant, scope, base) || !headSha(tenant, scope, head)) return [];
|
|
272
|
+
const dir = repoDir(tenant, scope);
|
|
273
|
+
const out = git(dir, ["diff", "--name-status", `${base}...${head}`]);
|
|
274
|
+
return out.split("\n").filter(Boolean).map((line) => {
|
|
275
|
+
const [status, p] = line.split("\t");
|
|
276
|
+
return {
|
|
277
|
+
path: p,
|
|
278
|
+
before: status === "A" ? null : blob(dir, base, p),
|
|
279
|
+
after: status === "D" ? null : blob(dir, head, p),
|
|
280
|
+
};
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Merge a branch into main and return the new main. A fast-forward when it
|
|
286
|
+
* is one; otherwise a merge commit whose tree is the BRANCH's — the branch
|
|
287
|
+
* is what the person reviewed and chose, and a three-way textual merge of
|
|
288
|
+
* markdown with no one to resolve conflicts would deploy text nobody wrote.
|
|
289
|
+
* Both parents are recorded, so history shows the merge for what it was.
|
|
290
|
+
*/
|
|
291
|
+
export function mergeBranch(
|
|
292
|
+
tenant: string,
|
|
293
|
+
scope: string,
|
|
294
|
+
branch: string,
|
|
295
|
+
meta: { by?: string } = {},
|
|
296
|
+
): { sha: string; fastForward: boolean } {
|
|
297
|
+
const dir = repoDir(tenant, scope);
|
|
298
|
+
const main = headSha(tenant, scope);
|
|
299
|
+
const head = headSha(tenant, scope, `refs/heads/${safe(branch, "branch")}`);
|
|
300
|
+
if (!head) throw new Error(`no branch "${branch}"`);
|
|
301
|
+
if (!main) {
|
|
302
|
+
git(dir, ["update-ref", "refs/heads/main", head]);
|
|
303
|
+
return { sha: head, fastForward: true };
|
|
304
|
+
}
|
|
305
|
+
const isAncestor = spawnSync("git", ["--git-dir", dir, "merge-base", "--is-ancestor", main, head]).status === 0;
|
|
306
|
+
if (isAncestor) {
|
|
307
|
+
git(dir, ["update-ref", "refs/heads/main", head, main]);
|
|
308
|
+
return { sha: head, fastForward: true };
|
|
309
|
+
}
|
|
310
|
+
const tree = git(dir, ["rev-parse", `${head}^{tree}`]).trim();
|
|
311
|
+
const who = meta.by ?? "foldrun";
|
|
312
|
+
const email = who.includes("@") ? who : `${who}@foldrun`;
|
|
313
|
+
const name = who.includes("@") ? who.split("@")[0] : who;
|
|
314
|
+
const date = new Date().toISOString();
|
|
315
|
+
const sha = git(dir, ["commit-tree", tree, "-p", main, "-p", head, "-m", `merge ${branch} into main`], {
|
|
316
|
+
env: {
|
|
317
|
+
GIT_AUTHOR_NAME: name, GIT_AUTHOR_EMAIL: email, GIT_AUTHOR_DATE: date,
|
|
318
|
+
GIT_COMMITTER_NAME: "foldrun", GIT_COMMITTER_EMAIL: "platform@foldrun", GIT_COMMITTER_DATE: date,
|
|
319
|
+
},
|
|
320
|
+
}).trim();
|
|
321
|
+
git(dir, ["update-ref", "refs/heads/main", sha, main]);
|
|
322
|
+
return { sha, fastForward: false };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function deleteBranch(tenant: string, scope: string, branch: string) {
|
|
326
|
+
if (branch === "main") throw new Error("main is the default branch");
|
|
327
|
+
git(repoDir(tenant, scope), ["update-ref", "-d", `refs/heads/${safe(branch, "branch")}`]);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Push main to another remote — a GitHub or GitLab mirror. Blocking; callers
|
|
331
|
+
* run it off the request path. The token never touches disk: it is in the
|
|
332
|
+
* URL for this one process and nowhere else. */
|
|
333
|
+
export function pushMirror(tenant: string, scope: string, url: string): { ok: boolean; detail: string } {
|
|
334
|
+
if (!headSha(tenant, scope)) return { ok: false, detail: "nothing to push yet" };
|
|
335
|
+
const r = spawnSync("git", ["--git-dir", repoDir(tenant, scope), "push", "--quiet", url, "+refs/heads/main:refs/heads/main"], {
|
|
336
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0" },
|
|
337
|
+
timeout: 60_000,
|
|
338
|
+
});
|
|
339
|
+
const detail = (r.stderr?.toString() ?? "").trim()
|
|
340
|
+
// Never echo the URL back: it carries the token.
|
|
341
|
+
.replaceAll(url, "<mirror>")
|
|
342
|
+
.slice(0, 500);
|
|
343
|
+
return { ok: r.status === 0, detail: r.status === 0 ? "pushed" : detail || "push failed" };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Where the compiled pre-receive check is, found on disk rather than through
|
|
348
|
+
* require.resolve — a bundler rewrites require.resolve("pkg") into a module
|
|
349
|
+
* id, and the hook was once told to run a script called "51113". Searched
|
|
350
|
+
* from the working directory upward: the monorepo layout, then an installed
|
|
351
|
+
* package. Empty when there is no compiled check (a dev server on the source
|
|
352
|
+
* tree), in which case a push is checked at deploy instead of refused — the
|
|
353
|
+
* same rules, one step later.
|
|
354
|
+
*/
|
|
355
|
+
export function hookScriptPath(): string {
|
|
356
|
+
let dir = process.cwd();
|
|
357
|
+
for (let i = 0; i < 6; i++) {
|
|
358
|
+
for (const rel of ["packages/core/dist/src/git-hook.js", "node_modules/@foldrun/core/dist/src/git-hook.js"]) {
|
|
359
|
+
const candidate = path.join(dir, rel);
|
|
360
|
+
if (fs.existsSync(candidate)) return candidate;
|
|
361
|
+
}
|
|
362
|
+
const parent = path.dirname(dir);
|
|
363
|
+
if (parent === dir) break;
|
|
364
|
+
dir = parent;
|
|
365
|
+
}
|
|
366
|
+
return "";
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* The pre-receive hook: a push that fails the deploy checks is refused with
|
|
371
|
+
* the errors in the pusher's terminal, instead of accepted and then not
|
|
372
|
+
* deployed. The hook shells out to node with the compiled check, so it runs
|
|
373
|
+
* the same rules as a dashboard deploy; which node and which script are
|
|
374
|
+
* passed in the environment by the HTTP route, because a hook inherits git's
|
|
375
|
+
* environment and nothing else.
|
|
376
|
+
*/
|
|
377
|
+
export function installHooks(dir: string) {
|
|
378
|
+
const hooks = path.join(dir, "hooks");
|
|
379
|
+
fs.mkdirSync(hooks, { recursive: true });
|
|
380
|
+
const hook = path.join(hooks, "pre-receive");
|
|
381
|
+
const script = `#!/bin/sh
|
|
382
|
+
# Installed by foldrun. Refuses a push to main whose tree fails the checks.
|
|
383
|
+
[ -n "$FOLDRUN_HOOK_NODE" ] && [ -n "$FOLDRUN_HOOK_SCRIPT" ] || exit 0
|
|
384
|
+
exec "$FOLDRUN_HOOK_NODE" "$FOLDRUN_HOOK_SCRIPT"
|
|
385
|
+
`;
|
|
386
|
+
if (!fs.existsSync(hook) || fs.readFileSync(hook, "utf8") !== script) {
|
|
387
|
+
fs.writeFileSync(hook, script, { mode: 0o755 });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export { type DiffOp };
|