@runeditrun/rer 0.2.79 → 0.2.80
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/dev-identity.mjs +2 -2
- package/cli/export.mjs +1 -1
- package/cli/inspect.mjs +38 -2
- package/cli/main.mjs +8 -2
- package/cli/managed-server.mjs +1 -1
- package/cli/parse.mjs +7 -6
- package/cli/portability-worker.mjs +2 -1
- package/cli/portability.mjs +6 -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 +2 -1
- package/cli/site-preview.mjs +4 -1
- package/cli/site.mjs +3 -2
- package/edition-authoring/adopt.mjs +39 -0
- package/edition-authoring/base-acknowledgements.mjs +24 -0
- package/edition-authoring/base-copies.mjs +418 -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/sources.mjs +114 -0
- package/edition-authoring/updates.mjs +105 -0
- package/global/CHANGELOG.md +12 -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 +8 -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 +32 -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/deploy-kit/README.md +19 -15
- package/modules/deploy-kit/owner-cli.mjs +1 -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.ts +10 -6
- 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/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/accounts/native.mjs +3 -1
- 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 +13 -4
- 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/index.d.mts +1 -1
- package/modules/operator-auth/page.mjs +14 -0
- package/modules/operator-auth/representation.mjs +83 -0
- package/modules/operator-auth/routes.mjs +25 -2
- package/modules/portability/README.md +91 -14
- 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 +5 -11
- package/modules/portability/format.mjs +7 -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 +3 -0
- 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 +20 -9
- package/modules/release/github-verify.mjs +30 -9
- 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 +26 -1
- package/onboarding/schema/business-field-inputs.mjs +37 -0
- package/onboarding/schema/json-schema.mjs +6 -2
- package/onboarding/schema/setup-validation.mjs +20 -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
package/cli/project.mjs
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
|
-
import { readFile,
|
|
2
|
-
import {
|
|
1
|
+
import { readFile, realpath } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
3
|
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { containedInput } from '../modules/demo/input-path.mjs';
|
|
4
5
|
import { readOperations } from '../operations/operations.mjs';
|
|
5
6
|
import { invalid, missing, refuse } from './errors.mjs';
|
|
6
7
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
const checkout = await realpath(root), destination = resolve(checkout, path), parts = relative(checkout, destination).split(sep);
|
|
10
|
-
let current = checkout;
|
|
11
|
-
for (let i = 0; i < parts.length; i++) {
|
|
12
|
-
current = resolve(current, parts[i]);
|
|
13
|
-
let status; try { status = await lstat(current); } catch (error) { if (error.code === 'ENOENT' && optional) return destination; if (error.code === 'ENOENT') throw missing(`Missing input file: ${path}. See the project operating documentation.`); throw error; }
|
|
14
|
-
if (status.isSymbolicLink()) throw refuse(`Symlink refused at ${path}; preserve it and resolve placement first.`);
|
|
15
|
-
if (i < parts.length - 1 && !status.isDirectory()) throw refuse(`Unsafe path ancestor for ${path}.`);
|
|
16
|
-
if (privateFile && i === parts.length - 1 && (!status.isFile() || status.nlink !== 1 || (status.mode & 0o077) !== 0 || (process.getuid && status.uid !== process.getuid()))) throw refuse(`Input must be an owned regular private file (0600): ${path}.`);
|
|
17
|
-
}
|
|
18
|
-
return destination;
|
|
8
|
+
export function contained(root, path, options = {}) {
|
|
9
|
+
return containedInput(root, path, { ...options, refuse, missing });
|
|
19
10
|
}
|
|
20
11
|
|
|
21
12
|
export async function resolveProject(root) {
|
package/cli/release.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { releaseVerb } from './contract.mjs';
|
|
1
2
|
import { parseTimeoutSeconds, timeoutBounds } from '../scripts/publish-check/timeout.mjs';
|
|
2
3
|
|
|
3
4
|
// Preserve release's accepted parser, producer, receipt and validation exits.
|
|
@@ -51,7 +52,7 @@ Key generation requires non-terminal stdout; --key-stdin requires non-terminal s
|
|
|
51
52
|
|
|
52
53
|
export async function dispatchRelease(args) {
|
|
53
54
|
if (args.length === 1 && args[0] === '--help') { console.log(releaseHelp); return 0; }
|
|
54
|
-
if (
|
|
55
|
+
if (Object.hasOwn(releaseVerb.subcommands, args[0])) return (await import('../modules/release/signing-cli.mjs')).dispatchSigning(args[0], args.slice(1));
|
|
55
56
|
try {
|
|
56
57
|
const options = {};
|
|
57
58
|
for (let index = 0; index < args.length; index++) {
|
package/cli/site-preview.mjs
CHANGED
|
@@ -47,7 +47,10 @@ export async function sitePreview(folder, { host = '127.0.0.1', ports }) {
|
|
|
47
47
|
server.once('error', failed);
|
|
48
48
|
server.listen(port, host, () => { server.off('error', failed); resolve(true); });
|
|
49
49
|
});
|
|
50
|
-
if (bound)
|
|
50
|
+
if (bound) {
|
|
51
|
+
const port = server.address().port;
|
|
52
|
+
return { url: `http://${host === '::1' ? '[::1]' : host}:${port}`, port, close: () => new Promise(resolve => { server.closeAllConnections?.(); server.close(() => resolve()); }) };
|
|
53
|
+
}
|
|
51
54
|
}
|
|
52
55
|
throw new Error('No free port for the run site preview.');
|
|
53
56
|
}
|
package/cli/site.mjs
CHANGED
|
@@ -122,7 +122,8 @@ export async function siteCommand(project, options, store, signal, out) {
|
|
|
122
122
|
return { site: { folder: built.folder, path, pages: built.pages, files: built.files, references:built.references, ...(built.runtime ? {runtime:runtimeSummary(built.runtime)} : {}) } };
|
|
123
123
|
}
|
|
124
124
|
const plan = await siteDeployment(project, options, built.folder, built.runtime);
|
|
125
|
-
const
|
|
125
|
+
const assets = relative(project.root, plan.assets);
|
|
126
|
+
const human = [`Run site plan: site Worker ${plan.worker}, Base-generated, ${built.runtime ? 'docs-search runtime with ASSETS and AI' : 'its only binding the assets'} (the app's Worker ${plan.appWorker} is not touched)`, ` assets: ${assets} (${built.files} files; HTML ${plan.assetsConfig.html_handling}, not found ${plan.assetsConfig.not_found_handling}; the Worker runs first)`,
|
|
126
127
|
` custom domains: ${plan.hostnames.join(', ')}; workers.dev off`, ...plan.redirects.map(rule => ` redirect in the Worker: ${rule.from} -> ${rule.to} (${rule.status}, keeps ${rule.keeps.join(' and ')})`)];
|
|
127
128
|
const { collectFiles } = await import('../modules/deploy-kit/artifact.mjs');
|
|
128
129
|
const { siteFingerprint } = await import('../modules/deploy-kit/site.mjs');
|
|
@@ -131,7 +132,7 @@ export async function siteCommand(project, options, store, signal, out) {
|
|
|
131
132
|
if (runtime) human.push(` runtime: ${runtime.type}; module ${runtime.module}; endpoint ${runtime.endpoint}; bindings ${runtime.bindings.join(', ')}; model ${runtime.model}; pooling ${runtime.pooling}; corpus ${runtime.corpusHash}`);
|
|
132
133
|
const fingerprint = siteFingerprint(plan, files, {preparing:Boolean(runtime) && !prepare});
|
|
133
134
|
if (runtime) human.push(` fingerprint: ${fingerprint}${prepare ? '' : ' (vectors prepared only on a real deploy)'}`);
|
|
134
|
-
const summary = { ...(runtime ? {runtime, fingerprint, vectors:prepare ? 'prepared' : 'prepare-on-deploy'} : {}), worker: plan.worker, appWorker: plan.appWorker, module: plan.module, bindings: plan.bindings, assets
|
|
135
|
+
const summary = { ...(runtime ? {runtime, fingerprint, vectors:prepare ? 'prepared' : 'prepare-on-deploy'} : {}), worker: plan.worker, appWorker: plan.appWorker, module: plan.module, bindings: plan.bindings, assets, files: built.files, references:built.references, domain: plan.domain, hostnames: plan.hostnames, workersDev: false, runWorkerFirst: true, assetsConfig: plan.assetsConfig, redirects: plan.redirects.map(({ from, to, status }) => ({ from, to, status })) };
|
|
135
136
|
if (options.dryRun) {
|
|
136
137
|
if (options.json) out.event('site', summary); else for (const line of human) out.human(line);
|
|
137
138
|
store.report('deploy --site: dry run complete; no deployment occurred and no provider request was made.');
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import {VERSION} from './manifest.mjs';
|
|
4
|
+
import {resolveSources} from './lock.mjs';
|
|
5
|
+
import {validate} from './base-copies.mjs';
|
|
6
|
+
import {git, json, noLinks, tracked, writeTree} from './files.mjs';
|
|
7
|
+
|
|
8
|
+
// Existing source and copy/composition provenance stay authoritative. Adoption
|
|
9
|
+
// adds a local-source recipe and lock; it does not regenerate the product or
|
|
10
|
+
// manufacture an upstream origin for source the seed already owns.
|
|
11
|
+
export function adoptEdition(root) {
|
|
12
|
+
if (fs.realpathSync(git(root, 'rev-parse', '--show-toplevel')) !== fs.realpathSync(root)) throw Error('Run adoption at the edition Git repository root.');
|
|
13
|
+
for (const name of ['edition.json', 'sources.lock.json', 'base-copies.json', 'rer-project.json', 'OPERATIONS.json']) noLinks(root, name);
|
|
14
|
+
if (fs.existsSync(path.join(root, 'edition.json')) || fs.existsSync(path.join(root, 'sources.lock.json'))) throw Error('Edition authoring inputs already exist. Preserve them; inspect and lock with the existing edition-authoring CLI instead of adopting again.');
|
|
15
|
+
const read = name => fs.existsSync(path.join(root, name)) ? JSON.parse(fs.readFileSync(path.join(root, name), 'utf8')) : {};
|
|
16
|
+
const id = read('rer-project.json').projectId ?? read('OPERATIONS.json').project;
|
|
17
|
+
if (typeof id !== 'string' || !/^[a-zA-Z0-9][a-zA-Z0-9._-]*\/[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(id)) throw Error('Declare a publisher-qualified projectId in rer-project.json before rer adopt-edition.');
|
|
18
|
+
const files = tracked(root);
|
|
19
|
+
const paths = Object.keys(files).filter(name => !['edition.json', 'sources.lock.json'].includes(name) && !name.startsWith('.edition'));
|
|
20
|
+
if (!paths.length) throw Error('Adoption needs tracked edition source. Add the intended source with git add, then repeat rer adopt-edition.');
|
|
21
|
+
const hasRecord = fs.existsSync(path.join(root, 'base-copies.json'));
|
|
22
|
+
if (hasRecord) validate(read('base-copies.json'));
|
|
23
|
+
const recipe = {schemaVersion: 1, id, toolchain: {id: 'runeditrun/edition-authoring', version: VERSION}, parent: null,
|
|
24
|
+
components: [{id, role: 'edition', source: {kind: 'local', paths}}]};
|
|
25
|
+
const created = [];
|
|
26
|
+
try {
|
|
27
|
+
const put = (name, value) => { writeTree(root, {[name]: {data: Buffer.from(json(value)).toString('base64'), executable: false}}); created.push(name); };
|
|
28
|
+
put('edition.json', recipe);
|
|
29
|
+
if (!hasRecord) put('base-copies.json', {schemaVersion: 1, copies: []});
|
|
30
|
+
const record = path.join(root, 'base-copies.json');
|
|
31
|
+
const additional = {'base-copies.json': {data: fs.readFileSync(record).toString('base64'), executable: !!(fs.statSync(record).mode & 0o111)}};
|
|
32
|
+
put('sources.lock.json', resolveSources(root, false, false, additional));
|
|
33
|
+
} catch (error) {
|
|
34
|
+
for (const name of created) fs.rmSync(path.join(root, name));
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
return {adopted: id, created, record: 'base-copies.json', preservedCopyRecord: hasRecord,
|
|
38
|
+
next: 'Review git diff and git status; stage edition.json, sources.lock.json and any untracked base-copies.json. Existing copy and composition provenance is preserved. An empty copy record claims no Base copies; record known copies before refreshing. Run the edition tests before committing.'};
|
|
39
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import {fileURLToPath} from 'node:url';
|
|
3
|
+
import {hash} from './files.mjs';
|
|
4
|
+
|
|
5
|
+
const cli = fileURLToPath(new URL('./cli.mjs', import.meta.url));
|
|
6
|
+
const quote = value => /^[a-zA-Z0-9_./:-]+$/.test(value) ? value : `'${value.replaceAll("'", "'\\''")}'`;
|
|
7
|
+
|
|
8
|
+
export const snapshot = file => ({sha256: hash(file.read()), mode: file.executable ? '100755' : '100644'});
|
|
9
|
+
|
|
10
|
+
// Keep the upstream baseline separate from the local bytes the owner accepted.
|
|
11
|
+
// Legacy declarations remain readable, but cannot silently acknowledge new bytes.
|
|
12
|
+
export function acknowledgementDrift(root, copy, name, file, recordPath) {
|
|
13
|
+
const relative = path.relative(root, cli);
|
|
14
|
+
const command = ['node', relative.startsWith('..') ? cli : relative, 'adapt-base', name,
|
|
15
|
+
...(recordPath === 'base-copies.json' ? [] : ['--record', recordPath])].map(quote).join(' ');
|
|
16
|
+
const detail = {path: name, copy: copy.path, acknowledge: command};
|
|
17
|
+
const expected = copy.acknowledged?.[name];
|
|
18
|
+
if (!expected) return [{kind: 'adaptation-unacknowledged', ...detail}];
|
|
19
|
+
const actual = snapshot(file);
|
|
20
|
+
const drift = [];
|
|
21
|
+
if (actual.sha256 !== expected.sha256) drift.push({kind: 'adaptation-modified', ...detail, expected: expected.sha256, actual: actual.sha256});
|
|
22
|
+
if (actual.mode !== expected.mode) drift.push({kind: 'adaptation-mode', ...detail, expected: expected.mode, actual: actual.mode});
|
|
23
|
+
return drift;
|
|
24
|
+
}
|
|
@@ -0,0 +1,418 @@
|
|
|
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
|
+
|
|
17
|
+
export const RECORD = 'base-copies.json';
|
|
18
|
+
const commit = /^[a-f0-9]{40}$/, sha256 = /^[a-f0-9]{64}$/;
|
|
19
|
+
const FILE_MODES = ['100644', '100755'];
|
|
20
|
+
// Unrecorded-copy detection skips files under this many bytes: a one-line file such
|
|
21
|
+
// as CLAUDE.md holding `@AGENTS.md` equals Base's by coincidence, not by copying.
|
|
22
|
+
export const MIN_UNRECORDED_BYTES = 16;
|
|
23
|
+
// Root licence and agent pointers commonly match by convention, not shared source.
|
|
24
|
+
const DEFAULT_DISCOVERY_IGNORES = new Set(['LICENSE', 'CLAUDE.md']);
|
|
25
|
+
const modeOf = executable => executable ? '100755' : '100644';
|
|
26
|
+
const within = (name, prefix) => name === prefix || name.startsWith(prefix + '/');
|
|
27
|
+
const mapped = (name, from, to) => to + name.slice(from.length);
|
|
28
|
+
|
|
29
|
+
function baseRepository(root) {
|
|
30
|
+
const recipe = path.join(root, 'edition.json');
|
|
31
|
+
if (!fs.existsSync(recipe)) return 'runeditrun/base';
|
|
32
|
+
const component = JSON.parse(fs.readFileSync(recipe, 'utf8')).components?.find(c => c.id === 'runeditrun/base');
|
|
33
|
+
const source = component?.source;
|
|
34
|
+
return (source?.kind === 'git' ? source.repository : source?.origin?.repository) || 'runeditrun/base';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// An explicit checkout, else the record's legacy repository, else the seed's declaration.
|
|
38
|
+
function open(record, base, root, offline = false) {
|
|
39
|
+
if (base) {
|
|
40
|
+
const repository = fs.realpathSync(base);
|
|
41
|
+
const source = gitReader(repository, offline);
|
|
42
|
+
try { return {repository, source, head: source.git('rev-parse', 'HEAD'), offline, close: source.close}; }
|
|
43
|
+
catch (error) { source.close(); throw error; }
|
|
44
|
+
}
|
|
45
|
+
const declared = record.repository ?? baseRepository(root);
|
|
46
|
+
const source = declared === 'runeditrun/base' ? 'https://github.com/runeditrun/base.git' : declared;
|
|
47
|
+
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), 'base-copies-'));
|
|
48
|
+
try {
|
|
49
|
+
execFileSync('git', ['clone', '--bare', '--quiet', '--', source, temporary], {env: {...process.env, GIT_NO_REPLACE_OBJECTS: '1'}, stdio: 'pipe'});
|
|
50
|
+
} catch {
|
|
51
|
+
fs.rmSync(temporary, {recursive: true, force: true});
|
|
52
|
+
throw Error('Cannot acquire Base source. Clone https://github.com/runeditrun/base.git when online, then repeat with --base <checkout>.');
|
|
53
|
+
}
|
|
54
|
+
return {repository: temporary, head: null, close: () => fs.rmSync(temporary, {recursive: true, force: true})};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function options(args, count) {
|
|
58
|
+
const result = {positional: [], record: RECORD, base: null, offline: false};
|
|
59
|
+
for (let i = 0; i < args.length; i++) {
|
|
60
|
+
const arg = args[i];
|
|
61
|
+
if (arg === '--offline') result.offline = true;
|
|
62
|
+
else if (arg === '--record' || arg === '--base') {
|
|
63
|
+
if (!args[i + 1]) throw Error(`${arg} requires a value`);
|
|
64
|
+
result[arg.slice(2)] = args[++i];
|
|
65
|
+
} else if (arg.startsWith('--')) throw Error(`Unknown option: ${arg}`);
|
|
66
|
+
else result.positional.push(arg);
|
|
67
|
+
}
|
|
68
|
+
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>]');
|
|
69
|
+
safe(result.record);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function validate(record) {
|
|
74
|
+
const legacy = record?.repository;
|
|
75
|
+
if (record?.schemaVersion !== 1 || (legacy !== undefined && (typeof legacy !== 'string' || !legacy || legacy.startsWith('-'))) || !Array.isArray(record.copies)) {
|
|
76
|
+
throw Error('Invalid Base copy record');
|
|
77
|
+
}
|
|
78
|
+
const paths = [];
|
|
79
|
+
for (const copy of record.copies) {
|
|
80
|
+
if (!copy || typeof copy !== 'object' || !commit.test(copy.revision) || !copy.files || typeof copy.files !== 'object' || Array.isArray(copy.files) || !Object.keys(copy.files).length) {
|
|
81
|
+
throw Error(`Invalid Base copy: ${copy?.path}`);
|
|
82
|
+
}
|
|
83
|
+
safe(copy.path); safe(copy.originPath);
|
|
84
|
+
for (const [name, digest] of Object.entries(copy.files)) {
|
|
85
|
+
safe(name);
|
|
86
|
+
if (!within(name, copy.path) || !sha256.test(digest)) throw Error(`Invalid recorded copy file: ${name}`);
|
|
87
|
+
}
|
|
88
|
+
if (copy.adapted !== undefined) {
|
|
89
|
+
if (!copy.adapted || typeof copy.adapted !== 'object' || Array.isArray(copy.adapted)) throw Error(`Invalid adaptations: ${copy.path}`);
|
|
90
|
+
for (const [name, revision] of Object.entries(copy.adapted)) {
|
|
91
|
+
if (!Object.hasOwn(copy.files, name) || !commit.test(revision)) throw Error(`Invalid adaptation: ${name}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (copy.acknowledged !== undefined) {
|
|
95
|
+
if (!copy.acknowledged || typeof copy.acknowledged !== 'object' || Array.isArray(copy.acknowledged)) throw Error(`Invalid acknowledgements: ${copy.path}`);
|
|
96
|
+
for (const [name, local] of Object.entries(copy.acknowledged)) {
|
|
97
|
+
if (!Object.hasOwn(copy.adapted || {}, name) || !local || !sha256.test(local.sha256) || !FILE_MODES.includes(local.mode)) throw Error(`Invalid acknowledgement: ${name}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// Older records have no modes, and a file without a recorded mode is not mode-checked; record-base writes every one.
|
|
101
|
+
if (copy.modes !== undefined) {
|
|
102
|
+
if (!copy.modes || typeof copy.modes !== 'object' || Array.isArray(copy.modes)) throw Error(`Invalid recorded modes: ${copy.path}`);
|
|
103
|
+
for (const [name, mode] of Object.entries(copy.modes)) {
|
|
104
|
+
if (!Object.hasOwn(copy.files, name) || !FILE_MODES.includes(mode)) throw Error(`Invalid recorded mode: ${name}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for (const other of paths) if (within(copy.path, other) || within(other, copy.path)) throw Error(`Overlapping Base copies: ${other} and ${copy.path}`);
|
|
108
|
+
paths.push(copy.path);
|
|
109
|
+
}
|
|
110
|
+
if (record.ignore !== undefined && !Array.isArray(record.ignore)) throw Error('Ignored paths must be a list');
|
|
111
|
+
(record.ignore || []).forEach(safe);
|
|
112
|
+
return record;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Every tracked and nonignored untracked file, as Git and the next commit see it.
|
|
116
|
+
function seedFiles(root) {
|
|
117
|
+
let top;
|
|
118
|
+
try { top = git(root, 'rev-parse', '--show-toplevel'); }
|
|
119
|
+
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.'); }
|
|
120
|
+
if (fs.realpathSync(top) !== fs.realpathSync(root)) throw Error('Run at the edition repository root');
|
|
121
|
+
const files = new Map();
|
|
122
|
+
let names;
|
|
123
|
+
try { names = raw(root, 'ls-files', '--cached', '--others', '--exclude-standard', '-z').toString('utf8').split('\0').filter(Boolean); }
|
|
124
|
+
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.'); }
|
|
125
|
+
for (const name of [...new Set(names)].sort()) {
|
|
126
|
+
safe(name);
|
|
127
|
+
let stat;
|
|
128
|
+
try { stat = fs.lstatSync(path.join(root, name)); }
|
|
129
|
+
catch (error) { if (error.code === 'ENOENT') continue; throw error; }
|
|
130
|
+
files.set(name, {regular: stat.isFile(), executable: !!(stat.mode & 0o100), read: () => fs.readFileSync(path.join(root, name))});
|
|
131
|
+
}
|
|
132
|
+
return files;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function reader(origin) {
|
|
136
|
+
const source = origin.source ?? gitReader(origin.repository);
|
|
137
|
+
const format = source.git('rev-parse', '--show-object-format');
|
|
138
|
+
const trees = new Map();
|
|
139
|
+
return {
|
|
140
|
+
// Git object identity of local bytes, to find copies by content without reading Base blobs.
|
|
141
|
+
objectId: bytes => crypto.createHash(format).update(`blob ${bytes.length}\0`).update(bytes).digest('hex'),
|
|
142
|
+
tree(revision) {
|
|
143
|
+
if (!trees.has(revision)) {
|
|
144
|
+
let resolved;
|
|
145
|
+
try { resolved = source.git('rev-parse', '--verify', '--quiet', `${revision}^{commit}`); } catch (error) { if (origin.offline) throw error; resolved = ''; }
|
|
146
|
+
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>`);
|
|
147
|
+
const entries = new Map();
|
|
148
|
+
for (const entry of source.raw('ls-tree', '-rz', '--full-tree', revision).toString('utf8').split('\0').filter(Boolean)) {
|
|
149
|
+
const tab = entry.indexOf('\t');
|
|
150
|
+
const [mode, type, object] = entry.slice(0, tab).split(' ');
|
|
151
|
+
if (type === 'blob' && FILE_MODES.includes(mode)) entries.set(entry.slice(tab + 1), {object, mode});
|
|
152
|
+
}
|
|
153
|
+
trees.set(revision, entries);
|
|
154
|
+
}
|
|
155
|
+
return trees.get(revision);
|
|
156
|
+
},
|
|
157
|
+
// Committed Base bytes, read in bounded batches.
|
|
158
|
+
blobs(objects) {
|
|
159
|
+
const result = new Map();
|
|
160
|
+
const unique = [...new Set(objects)];
|
|
161
|
+
for (let i = 0; i < unique.length; i += 100) {
|
|
162
|
+
const chunk = unique.slice(i, i + 100);
|
|
163
|
+
const output = source.batch(chunk.join('\n') + '\n', 'cat-file', '--batch');
|
|
164
|
+
let offset = 0;
|
|
165
|
+
for (const object of chunk) {
|
|
166
|
+
const newline = output.indexOf(10, offset);
|
|
167
|
+
const [, type, size] = output.subarray(offset, newline).toString('utf8').split(' ');
|
|
168
|
+
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.`);
|
|
169
|
+
if (type !== 'blob') throw Error(`Base object ${object} is not a file`);
|
|
170
|
+
result.set(object, Buffer.from(output.subarray(newline + 1, newline + 1 + Number(size))));
|
|
171
|
+
offset = newline + 1 + Number(size) + 1;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
},
|
|
176
|
+
digests(objects) {
|
|
177
|
+
return new Map([...this.blobs(objects)].map(([object, bytes]) => [object, hash(bytes)]));
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// The record is edition input: never read or write it through a link, and only as a regular file.
|
|
183
|
+
function recordFile(root, recordPath) {
|
|
184
|
+
noLinks(root, recordPath);
|
|
185
|
+
const filename = path.join(root, recordPath);
|
|
186
|
+
let stat;
|
|
187
|
+
try { stat = fs.lstatSync(filename); }
|
|
188
|
+
catch (error) { if (error.code === 'ENOENT') return null; throw error; }
|
|
189
|
+
if (!stat.isFile()) throw Error(`Base copy record is not a regular file: ${recordPath}`);
|
|
190
|
+
return filename;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function verifyCopies(root, {record: recordPath, base, offline}) {
|
|
194
|
+
const filename = recordFile(root, recordPath);
|
|
195
|
+
if (!filename) throw Error(`No ${recordPath} found; run record-base <path> <origin-path> <commit> first`);
|
|
196
|
+
const record = validate(JSON.parse(fs.readFileSync(filename, 'utf8')));
|
|
197
|
+
const files = seedFiles(root);
|
|
198
|
+
const drift = [], unrecordedCopies = [];
|
|
199
|
+
const copies = record.copies.map(copy => ({path: copy.path, originPath: copy.originPath, revision: copy.revision, files: Object.keys(copy.files).length}));
|
|
200
|
+
for (const copy of record.copies) {
|
|
201
|
+
for (const [name, file] of files) {
|
|
202
|
+
if (!within(name, copy.path)) continue;
|
|
203
|
+
if (!file.regular) drift.push({kind: 'not-a-file', path: name, copy: copy.path});
|
|
204
|
+
else if (!Object.hasOwn(copy.files, name)) drift.push({kind: 'unrecorded', path: name, copy: copy.path});
|
|
205
|
+
else if (Object.hasOwn(copy.adapted || {}, name)) {
|
|
206
|
+
noLinks(root, name);
|
|
207
|
+
drift.push(...acknowledgementDrift(root, copy, name, file, recordPath));
|
|
208
|
+
} else {
|
|
209
|
+
const actual = hash(file.read());
|
|
210
|
+
if (actual !== copy.files[name]) drift.push({kind: 'modified', path: name, copy: copy.path, expected: copy.files[name], actual});
|
|
211
|
+
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)});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
for (const name of Object.keys(copy.files)) if (!files.has(name)) drift.push({kind: 'missing', path: name, copy: copy.path});
|
|
215
|
+
}
|
|
216
|
+
if (!offline) {
|
|
217
|
+
const origin = open(record, base, root);
|
|
218
|
+
try {
|
|
219
|
+
const read = reader(origin);
|
|
220
|
+
for (const [index, copy] of record.copies.entries()) {
|
|
221
|
+
const tree = read.tree(copy.revision);
|
|
222
|
+
const present = [];
|
|
223
|
+
for (const [name, expected] of Object.entries(copy.files)) {
|
|
224
|
+
const originPath = mapped(name, copy.path, copy.originPath);
|
|
225
|
+
const baseline = copy.adapted?.[name] || copy.revision;
|
|
226
|
+
const tree = read.tree(baseline);
|
|
227
|
+
if (!tree.has(originPath)) drift.push({kind: 'origin-missing', path: name, originPath, revision: baseline});
|
|
228
|
+
else {
|
|
229
|
+
const {object, mode} = tree.get(originPath);
|
|
230
|
+
present.push({name, originPath, expected, object, revision: baseline});
|
|
231
|
+
if (copy.modes?.[name] && mode !== copy.modes[name]) drift.push({kind: 'origin-mode', path: name, originPath, revision: baseline, expected: copy.modes[name], actual: mode});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const digests = read.digests(present.map(item => item.object));
|
|
235
|
+
for (const item of present) {
|
|
236
|
+
const actual = digests.get(item.object);
|
|
237
|
+
if (actual !== item.expected) drift.push({kind: 'origin-mismatch', path: item.name, originPath: item.originPath, revision: item.revision, expected: item.expected, actual});
|
|
238
|
+
}
|
|
239
|
+
// Base files under the origin path that this copy deliberately leaves out.
|
|
240
|
+
copies[index].notCopied = [...tree.keys()].filter(name => within(name, copy.originPath) &&
|
|
241
|
+
!Object.hasOwn(copy.files, mapped(name, copy.originPath, copy.path)));
|
|
242
|
+
}
|
|
243
|
+
const known = new Map();
|
|
244
|
+
const revisions = [...new Set([...record.copies.map(copy => copy.revision), ...(origin.head ? [origin.head] : [])])];
|
|
245
|
+
for (const revision of revisions) for (const [name, {object}] of read.tree(revision)) if (!known.has(object)) known.set(object, {originPath: name, revision});
|
|
246
|
+
for (const [name, file] of files) {
|
|
247
|
+
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;
|
|
248
|
+
const bytes = file.read();
|
|
249
|
+
if (bytes.length < MIN_UNRECORDED_BYTES) continue;
|
|
250
|
+
const match = known.get(read.objectId(bytes));
|
|
251
|
+
if (match) unrecordedCopies.push({path: name, ...match});
|
|
252
|
+
}
|
|
253
|
+
} finally { origin.close(); }
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
ok: !drift.length && !unrecordedCopies.length, record: recordPath, repository: record.repository,
|
|
257
|
+
origin: offline ? 'not checked (--offline): local bytes against recorded SHA-256 only' : base ? 'local Base checkout' : 'fresh clone of the recorded repository',
|
|
258
|
+
copies, drift, unrecordedCopies,
|
|
259
|
+
adapted: record.copies.flatMap(copy => Object.entries(copy.adapted || {}).map(([path, revision]) => ({path, revision}))),
|
|
260
|
+
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.',
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function recordCopy(root, {positional: [localPath, originPath, revision], record: recordPath, base}) {
|
|
265
|
+
safe(localPath); safe(originPath);
|
|
266
|
+
if (!commit.test(revision)) throw Error('Record an immutable full Base commit');
|
|
267
|
+
const filename = recordFile(root, recordPath) || path.join(root, recordPath);
|
|
268
|
+
const record = validate(fs.existsSync(filename) ? JSON.parse(fs.readFileSync(filename, 'utf8')) : {schemaVersion: 1, copies: []});
|
|
269
|
+
const files = seedFiles(root);
|
|
270
|
+
const origin = open(record, base, root);
|
|
271
|
+
delete record.repository;
|
|
272
|
+
try {
|
|
273
|
+
const read = reader(origin);
|
|
274
|
+
const sources = [...read.tree(revision)].filter(([name]) => within(name, originPath));
|
|
275
|
+
if (!sources.length) throw Error(`No Base source at ${originPath} in ${revision}`);
|
|
276
|
+
const present = sources.filter(([name]) => files.get(mapped(name, originPath, localPath))?.regular);
|
|
277
|
+
if (!present.length) throw Error(`Copy Base ${originPath} into ${localPath} before recording it`);
|
|
278
|
+
// The record states Base bytes, never local bytes; verification then compares the two.
|
|
279
|
+
const digests = read.digests(present.map(([, {object}]) => object));
|
|
280
|
+
const ordered = present.map(([name, {object, mode}]) => [mapped(name, originPath, localPath), digests.get(object), mode]).sort(([a], [b]) => a.localeCompare(b));
|
|
281
|
+
const recorded = Object.fromEntries(ordered.map(([name, digest]) => [name, digest]));
|
|
282
|
+
const modes = Object.fromEntries(ordered.map(([name, , mode]) => [name, mode]));
|
|
283
|
+
record.copies = [...record.copies.filter(copy => copy.path !== localPath), {path: localPath, originPath, revision, files: recorded, modes}].sort((a, b) => a.path.localeCompare(b.path));
|
|
284
|
+
validate(record);
|
|
285
|
+
fs.writeFileSync(filename, json(record));
|
|
286
|
+
return {
|
|
287
|
+
recorded: localPath, originPath, revision, record: recordPath, files: present.length,
|
|
288
|
+
notCopied: sources.filter(entry => !present.includes(entry)).map(([name]) => name),
|
|
289
|
+
differs: Object.entries(recorded).filter(([name, digest]) => hash(files.get(name).read()) !== digest || modeOf(files.get(name).executable) !== modes[name]).map(([name]) => name),
|
|
290
|
+
localOnly: [...files.keys()].filter(name => within(name, localPath) && !Object.hasOwn(recorded, name)),
|
|
291
|
+
next: 'Run verify-base, then the edition tests, and commit the copied files with the record.',
|
|
292
|
+
};
|
|
293
|
+
} finally { origin.close(); }
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Explicitly transfer one recorded file to seed ownership without changing its bytes
|
|
297
|
+
// or its Base digest/mode. The per-file revision survives later whole-copy refreshes.
|
|
298
|
+
export function adaptCopy(root, {positional: [name], record: recordPath}) {
|
|
299
|
+
safe(name);
|
|
300
|
+
const filename = recordFile(root, recordPath);
|
|
301
|
+
if (!filename) throw Error(`No ${recordPath} found; run record-base first`);
|
|
302
|
+
const record = validate(JSON.parse(fs.readFileSync(filename, 'utf8')));
|
|
303
|
+
const copy = record.copies.find(copy => Object.hasOwn(copy.files, name));
|
|
304
|
+
if (!copy) throw Error(`${name} is not a recorded Base copy file`);
|
|
305
|
+
noLinks(root, name);
|
|
306
|
+
const file = seedFiles(root).get(name);
|
|
307
|
+
if (!file?.regular) throw Error(`Adaptation must be a regular file: ${name}`);
|
|
308
|
+
copy.adapted ||= {};
|
|
309
|
+
copy.adapted[name] ||= copy.revision;
|
|
310
|
+
copy.acknowledged ||= {};
|
|
311
|
+
copy.acknowledged[name] = snapshot(file);
|
|
312
|
+
delete record.repository;
|
|
313
|
+
fs.writeFileSync(filename, json(record));
|
|
314
|
+
return {adapted: name, revision: copy.adapted[name], sha256: copy.files[name], acknowledged: copy.acknowledged[name], record: recordPath};
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Move every recorded copy to one Base commit, after any export tool has written
|
|
318
|
+
// its files. Recorded files and files already equal to Base at that commit are
|
|
319
|
+
// kept current; files Base added under a copied path since the copy's pin are
|
|
320
|
+
// adopted (tests only into copies that carry tests); recorded files that Base or
|
|
321
|
+
// the export removed are dropped. Absent unadapted singleton copies are retired
|
|
322
|
+
// entirely, just as absent files are dropped from directory copies. Local edits
|
|
323
|
+
// are refused, never overwritten, as is any change to an existing file under a
|
|
324
|
+
// migrations folder.
|
|
325
|
+
export function refreshCopies(root, {positional: [revision], record: recordPath, base, offline}) {
|
|
326
|
+
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>.');
|
|
327
|
+
if (offline && !base) throw Error('Offline refresh needs a Base checkout. Repeat rer refresh-base --ref <commit> --offline --base <checkout> containing that commit.');
|
|
328
|
+
const filename = recordFile(root, recordPath);
|
|
329
|
+
if (!filename) throw Error(`No ${recordPath} found; run record-base <path> <origin-path> <commit> first`);
|
|
330
|
+
const record = validate(JSON.parse(fs.readFileSync(filename, 'utf8')));
|
|
331
|
+
const files = seedFiles(root);
|
|
332
|
+
requireMaterializedCopies(root, record, files);
|
|
333
|
+
const origin = open(record, base, root, offline);
|
|
334
|
+
try {
|
|
335
|
+
const read = reader(origin);
|
|
336
|
+
const next = read.tree(revision);
|
|
337
|
+
const upstream = read.digests([...next.values()].map(({object}) => object));
|
|
338
|
+
const local = name => files.get(name)?.regular ? hash(files.get(name).read()) : null;
|
|
339
|
+
const edited = [], unacknowledged = [];
|
|
340
|
+
// Check every recorded path before planning removals too. A path absent
|
|
341
|
+
// upstream still belongs to the owner when its local bytes have changed.
|
|
342
|
+
for (const copy of record.copies) for (const [name, digest] of Object.entries(copy.files)) {
|
|
343
|
+
if (Object.hasOwn(copy.adapted || {}, name)) {
|
|
344
|
+
noLinks(root, name);
|
|
345
|
+
if (!files.get(name)?.regular) throw Error(`Adaptation must be a regular file: ${name}`);
|
|
346
|
+
unacknowledged.push(...acknowledgementDrift(root, copy, name, files.get(name), recordPath));
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
const actual = local(name), source = next.get(mapped(name, copy.path, copy.originPath));
|
|
350
|
+
if (actual && actual !== digest && actual !== (source && upstream.get(source.object))) edited.push(name);
|
|
351
|
+
}
|
|
352
|
+
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')}`);
|
|
353
|
+
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.`);
|
|
354
|
+
const adapted = record.copies.flatMap(copy => Object.entries(copy.adapted || {}).map(([name, baseline]) => {
|
|
355
|
+
const originPath = mapped(name, copy.path, copy.originPath);
|
|
356
|
+
const source = next.get(originPath);
|
|
357
|
+
const conflict = !source || upstream.get(source.object) !== copy.files[name] || Boolean(copy.modes?.[name] && source.mode !== copy.modes[name]);
|
|
358
|
+
return {path: name, originPath, revision: baseline, conflict,
|
|
359
|
+
...(conflict ? {note: 'Upstream changed or removed this seed-owned adaptation; review and reconcile explicitly.',
|
|
360
|
+
diffHint: `git -C <base> diff ${baseline} ${revision} -- ${JSON.stringify(originPath)}`} : {})};
|
|
361
|
+
}));
|
|
362
|
+
const plans = record.copies.map(copy => {
|
|
363
|
+
// Absence is the existing directory-copy removal signal; there is no
|
|
364
|
+
// separate deletion-intent marker. Only an absent, unadapted singleton
|
|
365
|
+
// may retire its whole copy. Non-files and declared adaptations are not
|
|
366
|
+
// removals, and a still-present singleton must retain a Base source.
|
|
367
|
+
const retired = Object.keys(copy.files).length === 1 && Object.hasOwn(copy.files, copy.path) &&
|
|
368
|
+
!files.has(copy.path) && !fs.existsSync(path.join(root, copy.path)) && !Object.hasOwn(copy.adapted || {}, copy.path);
|
|
369
|
+
const pinned = read.tree(copy.revision);
|
|
370
|
+
if (retired) return {copy, sources: [], retired, removed: [copy.path]};
|
|
371
|
+
const tests = Object.keys(copy.files).some(name => testSource(mapped(name, copy.path, copy.originPath)));
|
|
372
|
+
const sources = [...next].filter(([name, {object}]) => {
|
|
373
|
+
if (!within(name, copy.originPath)) return false;
|
|
374
|
+
const target = mapped(name, copy.originPath, copy.path), actual = local(target);
|
|
375
|
+
if (Object.hasOwn(copy.adapted || {}, target)) return false;
|
|
376
|
+
if (Object.hasOwn(copy.files, target)) {
|
|
377
|
+
return Boolean(actual);
|
|
378
|
+
}
|
|
379
|
+
if (actual) return actual === upstream.get(object);
|
|
380
|
+
return !pinned.has(name) && (tests || !testSource(name));
|
|
381
|
+
});
|
|
382
|
+
if (!sources.length && !Object.keys(copy.adapted || {}).length) throw Error(`No Base source at ${copy.originPath} in ${revision}`);
|
|
383
|
+
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))};
|
|
384
|
+
});
|
|
385
|
+
// A migration a database may already have applied is never rewritten in place:
|
|
386
|
+
// the schema change it brings belongs in a new migration of the seed's own.
|
|
387
|
+
const applied = plans.flatMap(({copy, sources}) => sources.map(([source, {object}]) => [mapped(source, copy.originPath, copy.path), object])
|
|
388
|
+
.filter(([name, object]) => name.split('/').slice(0, -1).includes('migrations') && local(name) && local(name) !== upstream.get(object)).map(([name]) => name));
|
|
389
|
+
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.`);
|
|
390
|
+
const bytes = read.blobs(plans.flatMap(plan => plan.sources.map(([, {object}]) => object)));
|
|
391
|
+
const changes = [];
|
|
392
|
+
for (const {copy, sources, removed} of plans) {
|
|
393
|
+
for (const name of removed) { noLinks(root, name); fs.rmSync(path.join(root, name), {force: true}); }
|
|
394
|
+
const recorded = {}, modes = {}, added = [];
|
|
395
|
+
for (const name of Object.keys(copy.adapted || {})) {
|
|
396
|
+
recorded[name] = copy.files[name];
|
|
397
|
+
if (copy.modes?.[name]) modes[name] = copy.modes[name];
|
|
398
|
+
}
|
|
399
|
+
for (const [source, {object, mode}] of sources.sort(([a], [b]) => a.localeCompare(b))) {
|
|
400
|
+
const name = safe(mapped(source, copy.originPath, copy.path));
|
|
401
|
+
noLinks(root, name);
|
|
402
|
+
if (!Object.hasOwn(copy.files, name)) added.push(name);
|
|
403
|
+
const target = path.join(root, name);
|
|
404
|
+
fs.mkdirSync(path.dirname(target), {recursive: true});
|
|
405
|
+
fs.writeFileSync(target, bytes.get(object));
|
|
406
|
+
fs.chmodSync(target, mode === '100755' ? 0o755 : 0o644);
|
|
407
|
+
recorded[name] = upstream.get(object); modes[name] = mode;
|
|
408
|
+
}
|
|
409
|
+
changes.push({path: copy.path, from: copy.revision, files: sources.length, added, removed});
|
|
410
|
+
Object.assign(copy, {revision, files: recorded, modes});
|
|
411
|
+
}
|
|
412
|
+
record.copies = plans.filter(plan => !plan.retired).map(plan => plan.copy);
|
|
413
|
+
delete record.repository;
|
|
414
|
+
validate(record);
|
|
415
|
+
fs.writeFileSync(filename, json(record));
|
|
416
|
+
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.'};
|
|
417
|
+
} finally { origin.close(); }
|
|
418
|
+
}
|