@runeditrun/rer 0.2.79 → 0.2.81
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/README.md +17 -3
- package/cli/README.md +25 -13
- package/cli/authoring.mjs +14 -0
- package/cli/contract.mjs +25 -5
- package/cli/demo-host.mjs +4 -2
- package/cli/deploy-needs.mjs +4 -2
- package/cli/dev-identity.mjs +2 -2
- package/cli/export.mjs +1 -1
- package/cli/inspect.mjs +38 -2
- package/cli/ledger-refusal.mjs +18 -0
- package/cli/main.mjs +8 -2
- package/cli/managed-server.mjs +1 -1
- package/cli/parse.mjs +7 -6
- package/cli/portability-migrations.mjs +30 -11
- package/cli/portability-worker.mjs +4 -1
- package/cli/portability.mjs +8 -2
- package/cli/production-auth.mjs +2 -1
- package/cli/production-mail.mjs +36 -9
- package/cli/project.mjs +5 -14
- package/cli/release.mjs +6 -5
- package/cli/site-hosting.mjs +90 -0
- package/cli/site-preview.mjs +4 -1
- package/cli/site-render.mjs +3 -2
- package/cli/site.mjs +8 -4
- package/edition-authoring/adopt.mjs +39 -0
- package/edition-authoring/base-acknowledgements.mjs +24 -0
- package/edition-authoring/base-copies.mjs +427 -0
- package/edition-authoring/cli.mjs +85 -0
- package/edition-authoring/copy-checkout.mjs +69 -0
- package/edition-authoring/files.mjs +15 -2
- package/edition-authoring/lock.mjs +48 -0
- package/edition-authoring/offline-git.mjs +28 -0
- package/edition-authoring/parent-comparison.mjs +77 -0
- package/edition-authoring/projection-lock.mjs +38 -0
- package/edition-authoring/release.mjs +48 -0
- package/edition-authoring/runtime-dependencies.mjs +108 -0
- package/edition-authoring/sources.mjs +114 -0
- package/edition-authoring/updates.mjs +105 -0
- package/global/CHANGELOG.md +24 -0
- package/global/README.md +17 -3
- package/global/cli/install.mjs +3 -1
- package/global/main.mjs +9 -12
- package/modules/container/build.mjs +1 -1
- package/modules/d1-transport/index.mjs +68 -21
- package/modules/demo/README.md +16 -2
- package/modules/demo/declaration.mjs +21 -0
- package/modules/demo/input-path.mjs +18 -0
- package/modules/demo/label.d.mts +1 -1
- package/modules/demo/label.mjs +10 -5
- package/modules/demo/navigation.mjs +148 -0
- package/modules/demo/run-url.mjs +9 -0
- package/modules/demo/vite.d.mts +7 -2
- package/modules/demo/vite.mjs +17 -2
- package/modules/demo-host/README.md +5 -3
- package/modules/demo-host/page-style.mjs +2 -2
- package/modules/demo-host/pages.mjs +3 -3
- package/modules/demo-host/plan.mjs +6 -4
- package/modules/demo-host/theme.mjs +5 -2
- package/modules/dependencies/auth.mjs +2 -2
- package/modules/deploy-kit/README.md +19 -15
- package/modules/deploy-kit/owner-cli.mjs +1 -1
- package/modules/deploy-kit/owner.mjs +8 -1
- package/modules/deploy-kit/readiness-window.mjs +3 -3
- package/modules/erasure/README.md +10 -5
- package/modules/import-kit/src/d1/README.md +15 -9
- package/modules/import-kit/src/d1/apply-effects.ts +33 -0
- package/modules/import-kit/src/d1/apply.ts +46 -14
- package/modules/import-kit/src/d1/atomic-prefix.ts +64 -0
- package/modules/import-kit/src/d1/budget.ts +48 -26
- package/modules/import-kit/src/d1/compressed-plan.ts +9 -2
- package/modules/import-kit/src/d1/database.ts +2 -1
- package/modules/import-kit/src/d1/jobs.ts +2 -1
- package/modules/import-kit/src/d1/plans.ts +4 -2
- package/modules/import-kit/src/d1/verified-plan.ts +63 -0
- package/modules/import-kit/src/types.ts +3 -1
- package/modules/integrations/core/README.md +2 -2
- package/modules/mail/README.md +4 -3
- package/modules/mail/pnpm-lock.yaml +16 -0
- package/modules/operator-auth/README.md +92 -3
- package/modules/operator-auth/accounts/admin.mjs +52 -20
- package/modules/operator-auth/accounts/audit.mjs +24 -0
- package/modules/operator-auth/accounts/business-team.mjs +91 -0
- package/modules/operator-auth/accounts/domain-audit.mjs +24 -0
- package/modules/operator-auth/accounts/native.mjs +6 -2
- package/modules/operator-auth/accounts/provider-control.mjs +7 -4
- package/modules/operator-auth/accounts/provider-setup.mjs +34 -0
- package/modules/operator-auth/accounts/settings-page.mjs +26 -4
- package/modules/operator-auth/accounts/status.mjs +7 -2
- package/modules/operator-auth/accounts/store.mjs +32 -6
- package/modules/operator-auth/business-settings.mjs +40 -0
- package/modules/operator-auth/conformance/README.md +1 -1
- package/modules/operator-auth/dev/index.mjs +22 -4
- package/modules/operator-auth/dev/refusal.mjs +19 -0
- package/modules/operator-auth/docs/README.md +6 -5
- package/modules/operator-auth/docs/tanstack-start.md +1 -1
- package/modules/operator-auth/docs/vite-spa.md +5 -6
- package/modules/operator-auth/identity/email-link/index.mjs +1 -1
- package/modules/operator-auth/identity/oidc/admin-audit.mjs +31 -0
- package/modules/operator-auth/identity/oidc/index.mjs +12 -5
- package/modules/operator-auth/identity/oidc/store.mjs +2 -0
- package/modules/operator-auth/index.d.mts +41 -2
- package/modules/operator-auth/page.mjs +14 -0
- package/modules/operator-auth/representation.mjs +83 -0
- package/modules/operator-auth/routes.mjs +31 -3
- package/modules/operator-auth/service.mjs +8 -3
- package/modules/operator-auth/signin/index.mjs +1 -1
- package/modules/portability/README.md +173 -15
- package/modules/portability/bookkeeping.mjs +3 -3
- package/modules/portability/bridge.mjs +5 -4
- package/modules/portability/catalogue.mjs +12 -8
- package/modules/portability/cli.mjs +5 -1
- package/modules/portability/export.mjs +10 -14
- package/modules/portability/format.mjs +12 -1
- package/modules/portability/http-errors.mjs +51 -0
- package/modules/portability/http.mjs +4 -1
- package/modules/portability/import-lease.mjs +13 -2
- package/modules/portability/import.mjs +10 -0
- package/modules/portability/index.d.mts +13 -2
- package/modules/portability/index.mjs +3 -2
- package/modules/portability/ledgers.mjs +27 -4
- package/modules/portability/references.mjs +20 -0
- package/modules/portability/schema-identity.mjs +13 -0
- package/modules/portability/schema-legacy.mjs +21 -0
- package/modules/portability/sqlite-tokens.mjs +121 -0
- package/modules/release/README.md +94 -11
- package/modules/release/github-verify.mjs +30 -9
- package/modules/release/sign.mjs +4 -2
- package/modules/release/signature.mjs +14 -7
- package/modules/release/signing-cli.mjs +2 -1
- package/modules/release/statement.mjs +2 -2
- package/modules/release/vectors/generate.mjs +1 -0
- package/modules/release/vectors/publisher-projects.mjs +133 -0
- package/modules/release/vectors/release-signature-vectors.json +5195 -0
- package/modules/release/verify.mjs +5 -1
- package/modules/site/docs-search/search.css +1 -1
- package/onboarding/README.md +16 -4
- package/onboarding/adapter.mjs +5 -1
- package/onboarding/schema/SETUP.schema.json +68 -1
- package/onboarding/schema/business-field-inputs.mjs +36 -0
- package/onboarding/schema/json-schema.mjs +6 -2
- package/onboarding/schema/setup-validation.mjs +23 -3
- package/onboarding/server.mjs +1 -1
- package/onboarding/setup-manifest.mjs +2 -0
- package/onboarding/start-origin-diagnostic.mjs +24 -0
- package/onboarding/ui/style.css +9 -9
- package/package.json +1 -1
- package/scripts/publish-check/README.md +1 -1
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
// Verbatim copies of maintained Base source inside an edition. A record names
|
|
2
|
+
// each copied path, its Base origin path and commit, and the SHA-256 of every
|
|
3
|
+
// copied file. Verification checks local bytes against the record, the record
|
|
4
|
+
// against Base at that commit, and looks for Base files copied without a record.
|
|
5
|
+
// The full commit and file digests identify Base bytes wherever Base is hosted, so
|
|
6
|
+
// the record names no repository; Base is read from --base or the seed's declared
|
|
7
|
+
// Base source. Older records naming a repository are still read.
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import crypto from 'node:crypto';
|
|
12
|
+
import {execFileSync} from 'node:child_process';
|
|
13
|
+
import {hash, json, git, raw, gitReader, safe, noLinks, testSource} from './files.mjs';
|
|
14
|
+
import {snapshot, acknowledgementDrift} from './base-acknowledgements.mjs';
|
|
15
|
+
import {requireMaterializedCopies} from './copy-checkout.mjs';
|
|
16
|
+
import {completeRuntimeCopies} from './runtime-dependencies.mjs';
|
|
17
|
+
|
|
18
|
+
export const RECORD = 'base-copies.json';
|
|
19
|
+
const commit = /^[a-f0-9]{40}$/, sha256 = /^[a-f0-9]{64}$/;
|
|
20
|
+
const FILE_MODES = ['100644', '100755'];
|
|
21
|
+
// Unrecorded-copy detection skips files under this many bytes: a one-line file such
|
|
22
|
+
// as CLAUDE.md holding `@AGENTS.md` equals Base's by coincidence, not by copying.
|
|
23
|
+
export const MIN_UNRECORDED_BYTES = 16;
|
|
24
|
+
// Root licence and agent pointers commonly match by convention, not shared source.
|
|
25
|
+
const DEFAULT_DISCOVERY_IGNORES = new Set(['LICENSE', 'CLAUDE.md']);
|
|
26
|
+
const modeOf = executable => executable ? '100755' : '100644';
|
|
27
|
+
const within = (name, prefix) => name === prefix || name.startsWith(prefix + '/');
|
|
28
|
+
const mapped = (name, from, to) => to + name.slice(from.length);
|
|
29
|
+
|
|
30
|
+
function baseRepository(root) {
|
|
31
|
+
const recipe = path.join(root, 'edition.json');
|
|
32
|
+
if (!fs.existsSync(recipe)) return 'runeditrun/base';
|
|
33
|
+
const component = JSON.parse(fs.readFileSync(recipe, 'utf8')).components?.find(c => c.id === 'runeditrun/base');
|
|
34
|
+
const source = component?.source;
|
|
35
|
+
return (source?.kind === 'git' ? source.repository : source?.origin?.repository) || 'runeditrun/base';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// An explicit checkout, else the record's legacy repository, else the seed's declaration.
|
|
39
|
+
function open(record, base, root, offline = false) {
|
|
40
|
+
if (base) {
|
|
41
|
+
const repository = fs.realpathSync(base);
|
|
42
|
+
const source = gitReader(repository, offline);
|
|
43
|
+
try { return {repository, source, head: source.git('rev-parse', 'HEAD'), offline, close: source.close}; }
|
|
44
|
+
catch (error) { source.close(); throw error; }
|
|
45
|
+
}
|
|
46
|
+
const declared = record.repository ?? baseRepository(root);
|
|
47
|
+
const source = declared === 'runeditrun/base' ? 'https://github.com/runeditrun/base.git' : declared;
|
|
48
|
+
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'base-copies-'));
|
|
49
|
+
try {
|
|
50
|
+
execFileSync('git', ['clone', '--bare', '--quiet', '--', source, temporary], {env: {...process.env, GIT_NO_REPLACE_OBJECTS: '1'}, stdio: 'pipe'});
|
|
51
|
+
} catch {
|
|
52
|
+
fs.rmSync(temporary, {recursive: true, force: true});
|
|
53
|
+
throw Error('Cannot acquire Base source. Clone https://github.com/runeditrun/base.git when online, then repeat with --base <checkout>.');
|
|
54
|
+
}
|
|
55
|
+
return {repository: temporary, head: null, close: () => fs.rmSync(temporary, {recursive: true, force: true})};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function options(args, count) {
|
|
59
|
+
const result = {positional: [], record: RECORD, base: null, offline: false};
|
|
60
|
+
for (let i = 0; i < args.length; i++) {
|
|
61
|
+
const arg = args[i];
|
|
62
|
+
if (arg === '--offline') result.offline = true;
|
|
63
|
+
else if (arg === '--record' || arg === '--base') {
|
|
64
|
+
if (!args[i + 1]) throw Error(`${arg} requires a value`);
|
|
65
|
+
result[arg.slice(2)] = args[++i];
|
|
66
|
+
} else if (arg.startsWith('--')) throw Error(`Unknown option: ${arg}`);
|
|
67
|
+
else result.positional.push(arg);
|
|
68
|
+
}
|
|
69
|
+
if (result.positional.length !== count) throw Error('Usage: verify-base [--record <file>] [--base <checkout>] [--offline] | record-base <path> <origin-path> <revision> [--record <file>] [--base <checkout>] | adapt-base <file> [--record <file>] | refresh-base <revision> [--record <file>] [--base <checkout>]');
|
|
70
|
+
safe(result.record);
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function validate(record) {
|
|
75
|
+
const legacy = record?.repository;
|
|
76
|
+
if (record?.schemaVersion !== 1 || (legacy !== undefined && (typeof legacy !== 'string' || !legacy || legacy.startsWith('-'))) || !Array.isArray(record.copies)) {
|
|
77
|
+
throw Error('Invalid Base copy record');
|
|
78
|
+
}
|
|
79
|
+
const paths = [];
|
|
80
|
+
for (const copy of record.copies) {
|
|
81
|
+
if (!copy || typeof copy !== 'object' || !commit.test(copy.revision) || !copy.files || typeof copy.files !== 'object' || Array.isArray(copy.files) || !Object.keys(copy.files).length) {
|
|
82
|
+
throw Error(`Invalid Base copy: ${copy?.path}`);
|
|
83
|
+
}
|
|
84
|
+
safe(copy.path); safe(copy.originPath);
|
|
85
|
+
for (const [name, digest] of Object.entries(copy.files)) {
|
|
86
|
+
safe(name);
|
|
87
|
+
if (!within(name, copy.path) || !sha256.test(digest)) throw Error(`Invalid recorded copy file: ${name}`);
|
|
88
|
+
}
|
|
89
|
+
if (copy.adapted !== undefined) {
|
|
90
|
+
if (!copy.adapted || typeof copy.adapted !== 'object' || Array.isArray(copy.adapted)) throw Error(`Invalid adaptations: ${copy.path}`);
|
|
91
|
+
for (const [name, revision] of Object.entries(copy.adapted)) {
|
|
92
|
+
if (!Object.hasOwn(copy.files, name) || !commit.test(revision)) throw Error(`Invalid adaptation: ${name}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (copy.acknowledged !== undefined) {
|
|
96
|
+
if (!copy.acknowledged || typeof copy.acknowledged !== 'object' || Array.isArray(copy.acknowledged)) throw Error(`Invalid acknowledgements: ${copy.path}`);
|
|
97
|
+
for (const [name, local] of Object.entries(copy.acknowledged)) {
|
|
98
|
+
if (!Object.hasOwn(copy.adapted || {}, name) || !local || !sha256.test(local.sha256) || !FILE_MODES.includes(local.mode)) throw Error(`Invalid acknowledgement: ${name}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Older records have no modes, and a file without a recorded mode is not mode-checked; record-base writes every one.
|
|
102
|
+
if (copy.modes !== undefined) {
|
|
103
|
+
if (!copy.modes || typeof copy.modes !== 'object' || Array.isArray(copy.modes)) throw Error(`Invalid recorded modes: ${copy.path}`);
|
|
104
|
+
for (const [name, mode] of Object.entries(copy.modes)) {
|
|
105
|
+
if (!Object.hasOwn(copy.files, name) || !FILE_MODES.includes(mode)) throw Error(`Invalid recorded mode: ${name}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const other of paths) if (within(copy.path, other) || within(other, copy.path)) throw Error(`Overlapping Base copies: ${other} and ${copy.path}`);
|
|
109
|
+
paths.push(copy.path);
|
|
110
|
+
}
|
|
111
|
+
if (record.ignore !== undefined && !Array.isArray(record.ignore)) throw Error('Ignored paths must be a list');
|
|
112
|
+
(record.ignore || []).forEach(safe);
|
|
113
|
+
return record;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Every tracked and nonignored untracked file, as Git and the next commit see it.
|
|
117
|
+
function seedFiles(root) {
|
|
118
|
+
let top;
|
|
119
|
+
try { top = git(root, 'rev-parse', '--show-toplevel'); }
|
|
120
|
+
catch { throw Error('Cannot read the Git checkout/index. Ensure Git is available and run git status to repair the checkout before working with Base copies; no copy record was changed.'); }
|
|
121
|
+
if (fs.realpathSync(top) !== fs.realpathSync(root)) throw Error('Run at the edition repository root');
|
|
122
|
+
const files = new Map();
|
|
123
|
+
let names;
|
|
124
|
+
try { names = raw(root, 'ls-files', '--cached', '--others', '--exclude-standard', '-z').toString('utf8').split('\0').filter(Boolean); }
|
|
125
|
+
catch { throw Error('Cannot read the Git index. Run git status and repair the checkout before working with Base copies; no copy record was changed.'); }
|
|
126
|
+
for (const name of [...new Set(names)].sort()) {
|
|
127
|
+
safe(name);
|
|
128
|
+
let stat;
|
|
129
|
+
try { stat = fs.lstatSync(path.join(root, name)); }
|
|
130
|
+
catch (error) { if (error.code === 'ENOENT') continue; throw error; }
|
|
131
|
+
files.set(name, {regular: stat.isFile(), executable: !!(stat.mode & 0o100), read: () => fs.readFileSync(path.join(root, name))});
|
|
132
|
+
}
|
|
133
|
+
return files;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function reader(origin) {
|
|
137
|
+
const source = origin.source ?? gitReader(origin.repository);
|
|
138
|
+
const format = source.git('rev-parse', '--show-object-format');
|
|
139
|
+
const trees = new Map();
|
|
140
|
+
return {
|
|
141
|
+
// Git object identity of local bytes, to find copies by content without reading Base blobs.
|
|
142
|
+
objectId: bytes => crypto.createHash(format).update(`blob ${bytes.length}\0`).update(bytes).digest('hex'),
|
|
143
|
+
tree(revision) {
|
|
144
|
+
if (!trees.has(revision)) {
|
|
145
|
+
let resolved;
|
|
146
|
+
try { resolved = source.git('rev-parse', '--verify', '--quiet', `${revision}^{commit}`); } catch (error) { if (origin.offline) throw error; resolved = ''; }
|
|
147
|
+
if (resolved !== revision) throw Error(`Base commit ${revision} is not available. Check the full commit with git -C <base-checkout> rev-parse <tag>^{commit}, fetch it, then repeat with --base <base-checkout>`);
|
|
148
|
+
const entries = new Map();
|
|
149
|
+
for (const entry of source.raw('ls-tree', '-rz', '--full-tree', revision).toString('utf8').split('\0').filter(Boolean)) {
|
|
150
|
+
const tab = entry.indexOf('\t');
|
|
151
|
+
const [mode, type, object] = entry.slice(0, tab).split(' ');
|
|
152
|
+
if (type === 'blob' && FILE_MODES.includes(mode)) entries.set(entry.slice(tab + 1), {object, mode});
|
|
153
|
+
}
|
|
154
|
+
trees.set(revision, entries);
|
|
155
|
+
}
|
|
156
|
+
return trees.get(revision);
|
|
157
|
+
},
|
|
158
|
+
// Committed Base bytes, read in bounded batches.
|
|
159
|
+
blobs(objects) {
|
|
160
|
+
const result = new Map();
|
|
161
|
+
const unique = [...new Set(objects)];
|
|
162
|
+
for (let i = 0; i < unique.length; i += 100) {
|
|
163
|
+
const chunk = unique.slice(i, i + 100);
|
|
164
|
+
const output = source.batch(chunk.join('\n') + '\n', 'cat-file', '--batch');
|
|
165
|
+
let offset = 0;
|
|
166
|
+
for (const object of chunk) {
|
|
167
|
+
const newline = output.indexOf(10, offset);
|
|
168
|
+
const [, type, size] = output.subarray(offset, newline).toString('utf8').split(' ');
|
|
169
|
+
if (type === 'missing' && origin.offline) throw Error(`Base object ${object} is missing locally. Repeat rer refresh-base --ref <commit> --base <base-checkout> without --offline while online to acquire it, then retry offline. No copy record was changed.`);
|
|
170
|
+
if (type !== 'blob') throw Error(`Base object ${object} is not a file`);
|
|
171
|
+
result.set(object, Buffer.from(output.subarray(newline + 1, newline + 1 + Number(size))));
|
|
172
|
+
offset = newline + 1 + Number(size) + 1;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
},
|
|
177
|
+
digests(objects) {
|
|
178
|
+
return new Map([...this.blobs(objects)].map(([object, bytes]) => [object, hash(bytes)]));
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// The record is edition input: never read or write it through a link, and only as a regular file.
|
|
184
|
+
function recordFile(root, recordPath) {
|
|
185
|
+
noLinks(root, recordPath);
|
|
186
|
+
const filename = path.join(root, recordPath);
|
|
187
|
+
let stat;
|
|
188
|
+
try { stat = fs.lstatSync(filename); }
|
|
189
|
+
catch (error) { if (error.code === 'ENOENT') return null; throw error; }
|
|
190
|
+
if (!stat.isFile()) throw Error(`Base copy record is not a regular file: ${recordPath}`);
|
|
191
|
+
return filename;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function verifyCopies(root, {record: recordPath, base, offline}) {
|
|
195
|
+
const filename = recordFile(root, recordPath);
|
|
196
|
+
if (!filename) throw Error(`No ${recordPath} found; run record-base <path> <origin-path> <commit> first`);
|
|
197
|
+
const record = validate(JSON.parse(fs.readFileSync(filename, 'utf8')));
|
|
198
|
+
const files = seedFiles(root);
|
|
199
|
+
const drift = [], unrecordedCopies = [];
|
|
200
|
+
const copies = record.copies.map(copy => ({path: copy.path, originPath: copy.originPath, revision: copy.revision, files: Object.keys(copy.files).length}));
|
|
201
|
+
for (const copy of record.copies) {
|
|
202
|
+
for (const [name, file] of files) {
|
|
203
|
+
if (!within(name, copy.path)) continue;
|
|
204
|
+
if (!file.regular) drift.push({kind: 'not-a-file', path: name, copy: copy.path});
|
|
205
|
+
else if (!Object.hasOwn(copy.files, name)) drift.push({kind: 'unrecorded', path: name, copy: copy.path});
|
|
206
|
+
else if (Object.hasOwn(copy.adapted || {}, name)) {
|
|
207
|
+
noLinks(root, name);
|
|
208
|
+
drift.push(...acknowledgementDrift(root, copy, name, file, recordPath));
|
|
209
|
+
} else {
|
|
210
|
+
const actual = hash(file.read());
|
|
211
|
+
if (actual !== copy.files[name]) drift.push({kind: 'modified', path: name, copy: copy.path, expected: copy.files[name], actual});
|
|
212
|
+
if (copy.modes?.[name] && modeOf(file.executable) !== copy.modes[name]) drift.push({kind: 'mode', path: name, copy: copy.path, expected: copy.modes[name], actual: modeOf(file.executable)});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
for (const name of Object.keys(copy.files)) if (!files.has(name)) drift.push({kind: 'missing', path: name, copy: copy.path});
|
|
216
|
+
}
|
|
217
|
+
if (!offline) {
|
|
218
|
+
const origin = open(record, base, root);
|
|
219
|
+
try {
|
|
220
|
+
const read = reader(origin);
|
|
221
|
+
for (const [index, copy] of record.copies.entries()) {
|
|
222
|
+
const tree = read.tree(copy.revision);
|
|
223
|
+
const present = [];
|
|
224
|
+
for (const [name, expected] of Object.entries(copy.files)) {
|
|
225
|
+
const originPath = mapped(name, copy.path, copy.originPath);
|
|
226
|
+
const baseline = copy.adapted?.[name] || copy.revision;
|
|
227
|
+
const tree = read.tree(baseline);
|
|
228
|
+
if (!tree.has(originPath)) drift.push({kind: 'origin-missing', path: name, originPath, revision: baseline});
|
|
229
|
+
else {
|
|
230
|
+
const {object, mode} = tree.get(originPath);
|
|
231
|
+
present.push({name, originPath, expected, object, revision: baseline});
|
|
232
|
+
if (copy.modes?.[name] && mode !== copy.modes[name]) drift.push({kind: 'origin-mode', path: name, originPath, revision: baseline, expected: copy.modes[name], actual: mode});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const digests = read.digests(present.map(item => item.object));
|
|
236
|
+
for (const item of present) {
|
|
237
|
+
const actual = digests.get(item.object);
|
|
238
|
+
if (actual !== item.expected) drift.push({kind: 'origin-mismatch', path: item.name, originPath: item.originPath, revision: item.revision, expected: item.expected, actual});
|
|
239
|
+
}
|
|
240
|
+
// Base files under the origin path that this copy deliberately leaves out.
|
|
241
|
+
copies[index].notCopied = [...tree.keys()].filter(name => within(name, copy.originPath) &&
|
|
242
|
+
!Object.hasOwn(copy.files, mapped(name, copy.originPath, copy.path)));
|
|
243
|
+
}
|
|
244
|
+
const known = new Map();
|
|
245
|
+
const revisions = [...new Set([...record.copies.map(copy => copy.revision), ...(origin.head ? [origin.head] : [])])];
|
|
246
|
+
for (const revision of revisions) for (const [name, {object}] of read.tree(revision)) if (!known.has(object)) known.set(object, {originPath: name, revision});
|
|
247
|
+
for (const [name, file] of files) {
|
|
248
|
+
if (!file.regular || name === recordPath || DEFAULT_DISCOVERY_IGNORES.has(name) || record.copies.some(copy => within(name, copy.path)) || (record.ignore || []).some(p => within(name, p))) continue;
|
|
249
|
+
const bytes = file.read();
|
|
250
|
+
if (bytes.length < MIN_UNRECORDED_BYTES) continue;
|
|
251
|
+
const match = known.get(read.objectId(bytes));
|
|
252
|
+
if (match) unrecordedCopies.push({path: name, ...match});
|
|
253
|
+
}
|
|
254
|
+
} finally { origin.close(); }
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
ok: !drift.length && !unrecordedCopies.length, record: recordPath, repository: record.repository,
|
|
258
|
+
origin: offline ? 'not checked (--offline): local bytes against recorded SHA-256 only' : base ? 'local Base checkout' : 'fresh clone of the recorded repository',
|
|
259
|
+
copies, drift, unrecordedCopies,
|
|
260
|
+
adapted: record.copies.flatMap(copy => Object.entries(copy.adapted || {}).map(([path, revision]) => ({path, revision}))),
|
|
261
|
+
meaning: 'Undeclared copies claim Base byte equality. Declared adaptations must match their acknowledged local bytes and mode; their recorded Base bytes and revision remain comparison baselines.',
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function recordCopy(root, {positional: [localPath, originPath, revision], record: recordPath, base}) {
|
|
266
|
+
safe(localPath); safe(originPath);
|
|
267
|
+
if (!commit.test(revision)) throw Error('Record an immutable full Base commit');
|
|
268
|
+
const filename = recordFile(root, recordPath) || path.join(root, recordPath);
|
|
269
|
+
const record = validate(fs.existsSync(filename) ? JSON.parse(fs.readFileSync(filename, 'utf8')) : {schemaVersion: 1, copies: []});
|
|
270
|
+
const files = seedFiles(root);
|
|
271
|
+
const origin = open(record, base, root);
|
|
272
|
+
delete record.repository;
|
|
273
|
+
try {
|
|
274
|
+
const read = reader(origin);
|
|
275
|
+
const sources = [...read.tree(revision)].filter(([name]) => within(name, originPath));
|
|
276
|
+
if (!sources.length) throw Error(`No Base source at ${originPath} in ${revision}`);
|
|
277
|
+
const present = sources.filter(([name]) => files.get(mapped(name, originPath, localPath))?.regular);
|
|
278
|
+
if (!present.length) throw Error(`Copy Base ${originPath} into ${localPath} before recording it`);
|
|
279
|
+
// The record states Base bytes, never local bytes; verification then compares the two.
|
|
280
|
+
const digests = read.digests(present.map(([, {object}]) => object));
|
|
281
|
+
const ordered = present.map(([name, {object, mode}]) => [mapped(name, originPath, localPath), digests.get(object), mode]).sort(([a], [b]) => a.localeCompare(b));
|
|
282
|
+
const recorded = Object.fromEntries(ordered.map(([name, digest]) => [name, digest]));
|
|
283
|
+
const modes = Object.fromEntries(ordered.map(([name, , mode]) => [name, mode]));
|
|
284
|
+
record.copies = [...record.copies.filter(copy => copy.path !== localPath), {path: localPath, originPath, revision, files: recorded, modes}].sort((a, b) => a.path.localeCompare(b.path));
|
|
285
|
+
validate(record);
|
|
286
|
+
fs.writeFileSync(filename, json(record));
|
|
287
|
+
return {
|
|
288
|
+
recorded: localPath, originPath, revision, record: recordPath, files: present.length,
|
|
289
|
+
notCopied: sources.filter(entry => !present.includes(entry)).map(([name]) => name),
|
|
290
|
+
differs: Object.entries(recorded).filter(([name, digest]) => hash(files.get(name).read()) !== digest || modeOf(files.get(name).executable) !== modes[name]).map(([name]) => name),
|
|
291
|
+
localOnly: [...files.keys()].filter(name => within(name, localPath) && !Object.hasOwn(recorded, name)),
|
|
292
|
+
next: 'Run verify-base, then the edition tests, and commit the copied files with the record.',
|
|
293
|
+
};
|
|
294
|
+
} finally { origin.close(); }
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Explicitly transfer one recorded file to seed ownership without changing its bytes
|
|
298
|
+
// or its Base digest/mode. The per-file revision survives later whole-copy refreshes.
|
|
299
|
+
export function adaptCopy(root, {positional: [name], record: recordPath}) {
|
|
300
|
+
safe(name);
|
|
301
|
+
const filename = recordFile(root, recordPath);
|
|
302
|
+
if (!filename) throw Error(`No ${recordPath} found; run record-base first`);
|
|
303
|
+
const record = validate(JSON.parse(fs.readFileSync(filename, 'utf8')));
|
|
304
|
+
const copy = record.copies.find(copy => Object.hasOwn(copy.files, name));
|
|
305
|
+
if (!copy) throw Error(`${name} is not a recorded Base copy file`);
|
|
306
|
+
noLinks(root, name);
|
|
307
|
+
const file = seedFiles(root).get(name);
|
|
308
|
+
if (!file?.regular) throw Error(`Adaptation must be a regular file: ${name}`);
|
|
309
|
+
copy.adapted ||= {};
|
|
310
|
+
copy.adapted[name] ||= copy.revision;
|
|
311
|
+
copy.acknowledged ||= {};
|
|
312
|
+
copy.acknowledged[name] = snapshot(file);
|
|
313
|
+
delete record.repository;
|
|
314
|
+
fs.writeFileSync(filename, json(record));
|
|
315
|
+
return {adapted: name, revision: copy.adapted[name], sha256: copy.files[name], acknowledged: copy.acknowledged[name], record: recordPath};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Move every recorded copy to one Base commit, after any export tool has written
|
|
319
|
+
// its files. Recorded files and files already equal to Base at that commit are
|
|
320
|
+
// kept current; files Base added under a copied path since the copy's pin are
|
|
321
|
+
// adopted (tests only into copies that carry tests); recorded files that Base or
|
|
322
|
+
// the export removed are dropped. Absent unadapted singleton copies are retired
|
|
323
|
+
// entirely, just as absent files are dropped from directory copies. Local edits
|
|
324
|
+
// are refused, never overwritten, as is any change to an existing file under a
|
|
325
|
+
// migrations folder.
|
|
326
|
+
export function refreshCopies(root, {positional: [revision], record: recordPath, base, offline}) {
|
|
327
|
+
if (!commit.test(revision)) throw Error('Refresh requires an immutable full 40-character Base commit. Resolve the release tag with git -C <base-checkout> rev-parse <tag>^{commit}, then repeat with --ref <commit> --base <base-checkout>.');
|
|
328
|
+
if (offline && !base) throw Error('Offline refresh needs a Base checkout. Repeat rer refresh-base --ref <commit> --offline --base <checkout> containing that commit.');
|
|
329
|
+
const filename = recordFile(root, recordPath);
|
|
330
|
+
if (!filename) throw Error(`No ${recordPath} found; run record-base <path> <origin-path> <commit> first`);
|
|
331
|
+
const record = validate(JSON.parse(fs.readFileSync(filename, 'utf8')));
|
|
332
|
+
const files = seedFiles(root);
|
|
333
|
+
requireMaterializedCopies(root, record, files);
|
|
334
|
+
const origin = open(record, base, root, offline);
|
|
335
|
+
try {
|
|
336
|
+
const read = reader(origin);
|
|
337
|
+
const next = read.tree(revision);
|
|
338
|
+
const upstream = read.digests([...next.values()].map(({object}) => object));
|
|
339
|
+
const local = name => files.get(name)?.regular ? hash(files.get(name).read()) : null;
|
|
340
|
+
const edited = [], unacknowledged = [];
|
|
341
|
+
// Check every recorded path before planning removals too. A path absent
|
|
342
|
+
// upstream still belongs to the owner when its local bytes have changed.
|
|
343
|
+
for (const copy of record.copies) for (const [name, digest] of Object.entries(copy.files)) {
|
|
344
|
+
if (Object.hasOwn(copy.adapted || {}, name)) {
|
|
345
|
+
noLinks(root, name);
|
|
346
|
+
if (!files.get(name)?.regular) throw Error(`Adaptation must be a regular file: ${name}`);
|
|
347
|
+
unacknowledged.push(...acknowledgementDrift(root, copy, name, files.get(name), recordPath));
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
const actual = local(name), source = next.get(mapped(name, copy.path, copy.originPath));
|
|
351
|
+
if (actual && actual !== digest && actual !== (source && upstream.get(source.object))) edited.push(name);
|
|
352
|
+
}
|
|
353
|
+
if (unacknowledged.length) throw Error(`Adapted files need acknowledgement: ${[...new Set(unacknowledged.map(item => item.path))].join(', ')}. Review local bytes and mode, then run:\n${[...new Set(unacknowledged.map(item => item.acknowledge))].join('\n')}`);
|
|
354
|
+
if (edited.length) throw Error(`Recorded copies have local edits; move adapted source out of the record first: ${edited.join(', ')}. To keep each intentional adaptation, run adapt-base <file> first.`);
|
|
355
|
+
const adapted = record.copies.flatMap(copy => Object.entries(copy.adapted || {}).map(([name, baseline]) => {
|
|
356
|
+
const originPath = mapped(name, copy.path, copy.originPath);
|
|
357
|
+
const source = next.get(originPath);
|
|
358
|
+
const conflict = !source || upstream.get(source.object) !== copy.files[name] || Boolean(copy.modes?.[name] && source.mode !== copy.modes[name]);
|
|
359
|
+
return {path: name, originPath, revision: baseline, conflict,
|
|
360
|
+
...(conflict ? {note: 'Upstream changed or removed this seed-owned adaptation; review and reconcile explicitly.',
|
|
361
|
+
diffHint: `git -C <base> diff ${baseline} ${revision} -- ${JSON.stringify(originPath)}`} : {})};
|
|
362
|
+
}));
|
|
363
|
+
const plans = record.copies.map(copy => {
|
|
364
|
+
// Absence is the existing directory-copy removal signal; there is no
|
|
365
|
+
// separate deletion-intent marker. Only an absent, unadapted singleton
|
|
366
|
+
// may retire its whole copy. Non-files and declared adaptations are not
|
|
367
|
+
// removals, and a still-present singleton must retain a Base source.
|
|
368
|
+
const retired = Object.keys(copy.files).length === 1 && Object.hasOwn(copy.files, copy.path) &&
|
|
369
|
+
!files.has(copy.path) && !fs.existsSync(path.join(root, copy.path)) && !Object.hasOwn(copy.adapted || {}, copy.path);
|
|
370
|
+
const pinned = read.tree(copy.revision);
|
|
371
|
+
if (retired) return {copy, sources: [], retired, removed: [copy.path]};
|
|
372
|
+
const tests = Object.keys(copy.files).some(name => testSource(mapped(name, copy.path, copy.originPath)));
|
|
373
|
+
const sources = [...next].filter(([name, {object}]) => {
|
|
374
|
+
if (!within(name, copy.originPath)) return false;
|
|
375
|
+
const target = mapped(name, copy.originPath, copy.path), actual = local(target);
|
|
376
|
+
if (Object.hasOwn(copy.adapted || {}, target)) return false;
|
|
377
|
+
if (Object.hasOwn(copy.files, target)) {
|
|
378
|
+
return Boolean(actual);
|
|
379
|
+
}
|
|
380
|
+
if (actual) return actual === upstream.get(object);
|
|
381
|
+
return !pinned.has(name) && (tests || !testSource(name));
|
|
382
|
+
});
|
|
383
|
+
if (!sources.length && !Object.keys(copy.adapted || {}).length) throw Error(`No Base source at ${copy.originPath} in ${revision}`);
|
|
384
|
+
return {copy, sources, retired, removed: Object.keys(copy.files).filter(name => !Object.hasOwn(copy.adapted || {}, name) && !sources.some(([source]) => mapped(source, copy.originPath, copy.path) === name))};
|
|
385
|
+
});
|
|
386
|
+
completeRuntimeCopies(plans, {tree: next, read, local, upstream, revision, checkTarget(name) {
|
|
387
|
+
safe(name); noLinks(root, name);
|
|
388
|
+
const target = path.join(root, name);
|
|
389
|
+
if (fs.existsSync(target) && !fs.lstatSync(target).isFile()) throw Error(`Runtime dependency is not a regular file: ${name}`);
|
|
390
|
+
}});
|
|
391
|
+
// Closure may add individually recorded siblings when the seed originally
|
|
392
|
+
// selected runtime files one at a time instead of copying the directory.
|
|
393
|
+
record.copies = plans.map(plan => plan.copy).sort((a, b) => a.path.localeCompare(b.path));
|
|
394
|
+
// A migration a database may already have applied is never rewritten in place:
|
|
395
|
+
// the schema change it brings belongs in a new migration of the seed's own.
|
|
396
|
+
const applied = plans.flatMap(({copy, sources}) => sources.map(([source, {object}]) => [mapped(source, copy.originPath, copy.path), object])
|
|
397
|
+
.filter(([name, object]) => name.split('/').slice(0, -1).includes('migrations') && local(name) && local(name) !== upstream.get(object)).map(([name]) => name));
|
|
398
|
+
if (applied.length) throw Error(`Refusing to rewrite migrations that may already be applied: ${applied.join(', ')}. Base changed these files after they were copied; keep them as they are, drop them from ${recordPath}, and add a new migration for the change instead.`);
|
|
399
|
+
const bytes = read.blobs(plans.flatMap(plan => plan.sources.map(([, {object}]) => object)));
|
|
400
|
+
const changes = [];
|
|
401
|
+
for (const {copy, sources, removed} of plans) {
|
|
402
|
+
for (const name of removed) { noLinks(root, name); fs.rmSync(path.join(root, name), {force: true}); }
|
|
403
|
+
const recorded = {}, modes = {}, added = [];
|
|
404
|
+
for (const name of Object.keys(copy.adapted || {})) {
|
|
405
|
+
recorded[name] = copy.files[name];
|
|
406
|
+
if (copy.modes?.[name]) modes[name] = copy.modes[name];
|
|
407
|
+
}
|
|
408
|
+
for (const [source, {object, mode}] of sources.sort(([a], [b]) => a.localeCompare(b))) {
|
|
409
|
+
const name = safe(mapped(source, copy.originPath, copy.path));
|
|
410
|
+
noLinks(root, name);
|
|
411
|
+
if (!Object.hasOwn(copy.files, name)) added.push(name);
|
|
412
|
+
const target = path.join(root, name);
|
|
413
|
+
fs.mkdirSync(path.dirname(target), {recursive: true});
|
|
414
|
+
fs.writeFileSync(target, bytes.get(object));
|
|
415
|
+
fs.chmodSync(target, mode === '100755' ? 0o755 : 0o644);
|
|
416
|
+
recorded[name] = upstream.get(object); modes[name] = mode;
|
|
417
|
+
}
|
|
418
|
+
changes.push({path: copy.path, from: copy.revision, files: sources.length, added, removed});
|
|
419
|
+
Object.assign(copy, {revision, files: recorded, modes});
|
|
420
|
+
}
|
|
421
|
+
record.copies = plans.filter(plan => !plan.retired).map(plan => plan.copy);
|
|
422
|
+
delete record.repository;
|
|
423
|
+
validate(record);
|
|
424
|
+
fs.writeFileSync(filename, json(record));
|
|
425
|
+
return {refreshed: revision, record: recordPath, copies: changes, retired: plans.filter(plan => plan.retired).map(plan => plan.copy.path), adapted, next: 'Run verify-base, then the edition tests, and commit the copied files with the record.'};
|
|
426
|
+
} finally { origin.close(); }
|
|
427
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import {release, verifyReceipt} from './release.mjs';
|
|
5
|
+
import {changes, extract, compare} from './updates.mjs';
|
|
6
|
+
import {manifest} from './manifest.mjs';
|
|
7
|
+
import {snapshot, source, merge} from './sources.mjs';
|
|
8
|
+
import {options, adaptCopy, recordCopy, refreshCopies, verifyCopies} from './base-copies.mjs';
|
|
9
|
+
import {json, tracked, writeTree, fresh} from './files.mjs';
|
|
10
|
+
|
|
11
|
+
import {resolveSources} from './lock.mjs';
|
|
12
|
+
import {adoptEdition} from './adopt.mjs';
|
|
13
|
+
|
|
14
|
+
const root = process.cwd();
|
|
15
|
+
const resolve = (retain = false, diagnostics = false) => resolveSources(root, retain, diagnostics);
|
|
16
|
+
function check() {
|
|
17
|
+
const expected = JSON.parse(fs.readFileSync(path.join(root, 'sources.lock.json'), 'utf8'));
|
|
18
|
+
const actual = resolve();
|
|
19
|
+
if (json(expected) !== json(actual)) throw Error('Stale lock or source/tool integrity mismatch; run changes to inspect intentional edits before lock');
|
|
20
|
+
return actual;
|
|
21
|
+
}
|
|
22
|
+
function materialize(destination) {
|
|
23
|
+
const output = fresh(destination, root);
|
|
24
|
+
const recipe = manifest(root);
|
|
25
|
+
const files = tracked(root);
|
|
26
|
+
for (const component of recipe.components) {
|
|
27
|
+
if (component.source.kind === 'git') merge(files, component.source.destination, source(root, component.source, true));
|
|
28
|
+
}
|
|
29
|
+
fs.mkdirSync(output, {recursive: true});
|
|
30
|
+
writeTree(output, files);
|
|
31
|
+
return {output, development: true};
|
|
32
|
+
}
|
|
33
|
+
function promote(id) {
|
|
34
|
+
const recipe = manifest(root);
|
|
35
|
+
const component = recipe.components.find(c => c.id === id);
|
|
36
|
+
if (!component || component.source.kind !== 'git') throw Error('Select a Git component');
|
|
37
|
+
const selected = component.source;
|
|
38
|
+
const destinationExisted = fs.existsSync(path.join(root, selected.destination));
|
|
39
|
+
const files = source(root, selected, true);
|
|
40
|
+
snapshot(root, selected, true);
|
|
41
|
+
merge(tracked(root), selected.destination, files);
|
|
42
|
+
const retained = Object.fromEntries(Object.entries(files).map(([name, value]) => [selected.destination + '/' + name, value]));
|
|
43
|
+
writeTree(root, retained);
|
|
44
|
+
component.source = {
|
|
45
|
+
kind: 'local', paths: destinationExisted ? Object.keys(retained) : [selected.destination],
|
|
46
|
+
origin: {
|
|
47
|
+
repository: selected.repository, ...(selected.projection ? {projection: selected.projection} : {revision: selected.revision}), path: selected.path || '.',
|
|
48
|
+
...(selected.paths ? {paths: selected.paths} : {}),
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
fs.writeFileSync(path.join(root, 'edition.json'), json(recipe));
|
|
52
|
+
return {promoted: id, destination: selected.destination, next: 'git add the retained source and recipe, then lock and commit'};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const args = process.argv.slice(2);
|
|
57
|
+
// Package scripts may forward their argument separator after the command.
|
|
58
|
+
if (args[1] === '--') args.splice(1, 1);
|
|
59
|
+
const [command, argument, candidate, current] = args;
|
|
60
|
+
let result;
|
|
61
|
+
if (command === 'adopt') result = adoptEdition(root);
|
|
62
|
+
else if (command === 'inspect') result = manifest(root);
|
|
63
|
+
else if (command === 'lock') {
|
|
64
|
+
result = resolve(true);
|
|
65
|
+
fs.writeFileSync(path.join(root, 'sources.lock.json'), json(result));
|
|
66
|
+
} else if (command === 'check') result = {ok: true, ...check()};
|
|
67
|
+
else if (command === 'materialize' && argument) result = materialize(argument);
|
|
68
|
+
else if (command === 'promote' && argument) result = promote(argument);
|
|
69
|
+
else if (command === 'release' && argument) result = release(root, argument, check);
|
|
70
|
+
else if (command === 'receipt' && argument) result = verifyReceipt(root, argument, check);
|
|
71
|
+
else if (command === 'changes') result = changes(JSON.parse(fs.readFileSync(path.join(root, 'sources.lock.json'))), resolve(false, true));
|
|
72
|
+
else if (command === 'extract' && argument && candidate) result = extract(root, argument, candidate);
|
|
73
|
+
else if (command === 'compare' && argument && candidate) result = compare(root, argument, candidate, current);
|
|
74
|
+
else if (command === 'verify-base') {
|
|
75
|
+
result = verifyCopies(root, options(args.slice(1), 0));
|
|
76
|
+
if (!result.ok) process.exitCode = 1;
|
|
77
|
+
} else if (command === 'record-base') result = recordCopy(root, options(args.slice(1), 3));
|
|
78
|
+
else if (command === 'adapt-base') result = adaptCopy(root, options(args.slice(1), 1));
|
|
79
|
+
else if (command === 'refresh-base') result = refreshCopies(root, options(args.slice(1), 1));
|
|
80
|
+
else throw Error('Usage: cli.mjs adopt|inspect|lock|check|receipt <sidecar-json>|changes|extract <id|parent> <new-output>|compare <id|parent> <candidate-checkout> [current-parent-checkout]|materialize <new-output>|promote <id>|release <new-local-git-repository>|verify-base [--record <file>] [--base <checkout>] [--offline]|record-base <path> <origin-path> <revision> [--record <file>] [--base <checkout>]|adapt-base <file> [--record <file>]|refresh-base <revision> [--record <file>] [--base <checkout>]');
|
|
81
|
+
console.log(json(result));
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error(error.message);
|
|
84
|
+
process.exitCode = 1;
|
|
85
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {git, raw, safe, noLinks} from './files.mjs';
|
|
4
|
+
const quote = value => "'" + value.replaceAll("'", "'\\''") + "'";
|
|
5
|
+
|
|
6
|
+
// -v marks assume-unchanged; -f marks fsmonitor-valid. Read them separately
|
|
7
|
+
// because either option can lowercase the normal checkout-state tag.
|
|
8
|
+
function index(root) {
|
|
9
|
+
try {
|
|
10
|
+
const read = flag => new Map(raw(root, 'ls-files', '--stage', flag, '-z').toString('utf8').split('\0').filter(Boolean).map(line => {
|
|
11
|
+
const match = /^([HSMRCK?hsmrck]) ([0-7]{6}) ([a-f0-9]{40,64}) 0\t(.+)$/.exec(line);
|
|
12
|
+
if (!match) throw Error('Unsupported index entry');
|
|
13
|
+
return [safe(match[4]), {tag: match[1], mode: match[2], object: match[3]}];
|
|
14
|
+
}));
|
|
15
|
+
const entries = read('-v'), monitor = read('-f');
|
|
16
|
+
if (entries.size !== monitor.size) throw Error('Index changed');
|
|
17
|
+
for (const [name, entry] of entries) {
|
|
18
|
+
const other = monitor.get(name);
|
|
19
|
+
if (!other || entry.mode !== other.mode || entry.object !== other.object) throw Error('Index changed');
|
|
20
|
+
entry.monitor = other.tag;
|
|
21
|
+
}
|
|
22
|
+
return entries;
|
|
23
|
+
} catch {
|
|
24
|
+
throw Error('Cannot read or interpret the Git index to decide whether Base copies were removed. Run git status and repair the checkout, then repeat refresh-base. No copy record was changed.');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Absence alone is a removal signal only in a complete checkout. For a
|
|
29
|
+
// materialized submodule, inspect its own index and read the recorded child;
|
|
30
|
+
// the parent index lists the gitlink, never the child's files.
|
|
31
|
+
export function requireMaterializedCopies(root, record, files) {
|
|
32
|
+
const indexes = new Map();
|
|
33
|
+
const entriesAt = directory => {
|
|
34
|
+
if (!indexes.has(directory)) indexes.set(directory, index(directory));
|
|
35
|
+
return indexes.get(directory);
|
|
36
|
+
};
|
|
37
|
+
entriesAt(root);
|
|
38
|
+
for (const name of record.copies.flatMap(copy => Object.keys(copy.files))) {
|
|
39
|
+
let directory = root, relative = name;
|
|
40
|
+
for (;;) {
|
|
41
|
+
const entries = entriesAt(directory), entry = entries.get(relative);
|
|
42
|
+
const link = [...entries].find(([prefix, value]) => value.mode === '160000' && relative.startsWith(prefix + '/'));
|
|
43
|
+
if (link) {
|
|
44
|
+
const prefix = path.relative(root, path.join(directory, link[0])).split(path.sep).join('/');
|
|
45
|
+
const child = path.join(root, prefix);
|
|
46
|
+
let initialized = false;
|
|
47
|
+
try { initialized = fs.realpathSync(git(child, 'rev-parse', '--show-toplevel')) === fs.realpathSync(child); } catch {}
|
|
48
|
+
if (!initialized) throw Error(`Base copy ${name} is beneath the uninitialized Git submodule ${prefix}. Run git -C ${quote(directory)} submodule update --init -- ${quote(link[0])}, then repeat refresh-base. No copy record was changed.`);
|
|
49
|
+
relative = relative.slice(link[0].length + 1);
|
|
50
|
+
directory = child;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const target = path.join(root, name);
|
|
54
|
+
if (!fs.existsSync(target)) {
|
|
55
|
+
const repairs = [];
|
|
56
|
+
if (entry?.tag.toUpperCase() === 'S') repairs.push('git sparse-checkout disable');
|
|
57
|
+
if (entry && entry.tag === entry.tag.toLowerCase()) repairs.push(`git update-index --no-assume-unchanged -- ${quote(relative)}`);
|
|
58
|
+
if (entry && entry.monitor === entry.monitor.toLowerCase()) repairs.push(`git update-index --no-fsmonitor-valid -- ${quote(relative)}`);
|
|
59
|
+
if (directory !== root) repairs.push(`git restore -- ${quote(relative)}`);
|
|
60
|
+
if (repairs.length) throw Error(`Base copy ${name} has incomplete Git checkout evidence, not a confirmed removal. In ${directory}, run ${repairs.join(', then ')}, restore the file if absent, then repeat refresh-base. No copy record was changed.`);
|
|
61
|
+
} else if (directory !== root) {
|
|
62
|
+
noLinks(root, name);
|
|
63
|
+
const stat = fs.lstatSync(target);
|
|
64
|
+
files.set(name, {regular: stat.isFile(), executable: !!(stat.mode & 0o100), read: () => fs.readFileSync(target)});
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -2,6 +2,7 @@ import fs from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import crypto from 'node:crypto';
|
|
4
4
|
import {execFileSync} from 'node:child_process';
|
|
5
|
+
import {offlineRepository} from './offline-git.mjs';
|
|
5
6
|
export const hash = value => crypto.createHash('sha256').update(value).digest('hex');
|
|
6
7
|
export const json = value => JSON.stringify(value, null, 2) + '\n';
|
|
7
8
|
const environment = {...process.env, GIT_NO_REPLACE_OBJECTS: '1'};
|
|
@@ -9,9 +10,9 @@ export function git(root, ...args) {
|
|
|
9
10
|
return execFileSync('git', ['-C', root, ...args], {env: environment, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe']}).trim();
|
|
10
11
|
}
|
|
11
12
|
// Raw Git output, for committed source bytes. The limit fails explicitly rather than truncating.
|
|
12
|
-
function output(root, args, input) {
|
|
13
|
+
function output(root, args, input, env = environment) {
|
|
13
14
|
try {
|
|
14
|
-
return execFileSync('git', ['-C', root, ...args], {env
|
|
15
|
+
return execFileSync('git', ['-C', root, ...args], {env, input, maxBuffer: 64 * 1024 * 1024, stdio: [input === undefined ? 'ignore' : 'pipe', 'pipe', 'pipe']});
|
|
15
16
|
} catch (error) {
|
|
16
17
|
if (error.code === 'ENOBUFS') throw Error('Git source output exceeds the 64 MiB acquisition limit');
|
|
17
18
|
throw error;
|
|
@@ -19,6 +20,18 @@ function output(root, args, input) {
|
|
|
19
20
|
}
|
|
20
21
|
export const raw = (root, ...args) => output(root, args);
|
|
21
22
|
export const batch = (root, input, ...args) => output(root, args, input);
|
|
23
|
+
// Scope offline object access to this repository reader, never global state.
|
|
24
|
+
export function gitReader(root, offline = false) {
|
|
25
|
+
const repository = offline ? offlineRepository(root) : {root, env: environment, close() {}};
|
|
26
|
+
const read = (args, input) => {
|
|
27
|
+
try { return output(repository.root, args, input, repository.env); }
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (offline && typeof error.status === 'number') throw Error('Cannot read required Base objects offline. Run git -C <base-checkout> fsck to diagnose missing or corrupt local objects. If objects are missing locally, run rer refresh-base --ref <commit> --base <base-checkout> without --offline while online to acquire them, then retry offline. Repair corrupt objects before retrying. No copy record was changed.');
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
return {git: (...args) => read(args).toString('utf8').trim(), raw: (...args) => read(args), batch: (input, ...args) => read(args, input), close: repository.close};
|
|
34
|
+
}
|
|
22
35
|
export function safe(name) {
|
|
23
36
|
if (typeof name !== 'string' || !name || name.includes('\\') || /[\x00-\x1f\x7f]/.test(name) || path.posix.isAbsolute(name) || name.split('/').some(p => !p || p === '.' || p === '..' || p === '.git')) {
|
|
24
37
|
throw Error(`Unsafe path: ${name}`);
|