@sublang/spex 6.0.0 → 7.0.0-beta.2
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 +419 -12
- package/dist/cli.js +12 -1
- package/dist/commands/add.d.ts +1 -1
- package/dist/commands/add.js +2 -2
- package/dist/commands/environment.d.ts +3 -3
- package/dist/commands/environment.js +5 -7
- package/dist/commands/export.js +2 -2
- package/dist/commands/home.d.ts +2 -2
- package/dist/commands/home.js +3 -3
- package/dist/commands/list.js +1 -1
- package/dist/commands/login.js +12 -7
- package/dist/commands/logout.js +2 -3
- package/dist/commands/publish.js +1 -1
- package/dist/commands/remove.js +1 -1
- package/dist/commands/run.d.ts +1 -1
- package/dist/commands/run.js +16 -0
- package/dist/commands/settle.d.ts +1 -1
- package/dist/commands/settle.js +1 -1
- package/dist/guards/approval.d.ts +56 -0
- package/dist/guards/approval.js +158 -0
- package/dist/guards/checks.d.ts +207 -0
- package/dist/guards/checks.js +681 -0
- package/dist/guards/commands.d.ts +15 -0
- package/dist/guards/commands.js +313 -0
- package/dist/guards/copy.d.ts +67 -0
- package/dist/guards/copy.js +306 -0
- package/dist/guards/declaration.d.ts +45 -0
- package/dist/guards/declaration.js +224 -0
- package/dist/guards/files.d.ts +137 -0
- package/dist/guards/files.js +544 -0
- package/dist/guards/harness.d.ts +58 -0
- package/dist/guards/harness.js +420 -0
- package/dist/guards/items.d.ts +61 -0
- package/dist/guards/items.js +161 -0
- package/dist/guards/patch.d.ts +55 -0
- package/dist/guards/patch.js +258 -0
- package/dist/guards/processes.d.ts +49 -0
- package/dist/guards/processes.js +233 -0
- package/dist/guards/proofs.d.ts +88 -0
- package/dist/guards/proofs.js +503 -0
- package/dist/guards/records.d.ts +176 -0
- package/dist/guards/records.js +513 -0
- package/dist/guards/report.d.ts +26 -0
- package/dist/guards/report.js +492 -0
- package/dist/guards/runner.d.ts +42 -0
- package/dist/guards/runner.js +167 -0
- package/dist/guards/tla.d.ts +51 -0
- package/dist/guards/tla.js +352 -0
- package/dist/guards/tlapm.d.ts +81 -0
- package/dist/guards/tlapm.js +172 -0
- package/dist/guards/tlc.d.ts +191 -0
- package/dist/guards/tlc.js +457 -0
- package/dist/guards/translation.d.ts +66 -0
- package/dist/guards/translation.js +202 -0
- package/dist/guards/types.d.ts +185 -0
- package/dist/guards/types.js +437 -0
- package/dist/guards/verify.d.ts +290 -0
- package/dist/guards/verify.js +1191 -0
- package/dist/guards/xml.d.ts +18 -0
- package/dist/guards/xml.js +105 -0
- package/dist/lib/environment/archive.d.ts +2 -2
- package/dist/lib/environment/archive.js +1 -1
- package/dist/lib/environment/builtins.d.ts +10 -12
- package/dist/lib/environment/builtins.js +17 -19
- package/dist/lib/environment/exports.d.ts +14 -13
- package/dist/lib/environment/exports.js +31 -16
- package/dist/lib/environment/format.d.ts +2 -2
- package/dist/lib/environment/format.js +3 -3
- package/dist/lib/environment/git-source.js +6 -3
- package/dist/lib/environment/install.d.ts +14 -16
- package/dist/lib/environment/install.js +66 -61
- package/dist/lib/environment/lock.d.ts +4 -3
- package/dist/lib/environment/lock.js +6 -5
- package/dist/lib/environment/marker.js +1 -2
- package/dist/lib/environment/own-paths.d.ts +7 -10
- package/dist/lib/environment/own-paths.js +10 -13
- package/dist/lib/environment/placed-record.d.ts +3 -2
- package/dist/lib/environment/placed-record.js +5 -4
- package/dist/lib/environment/registry.d.ts +1 -1
- package/dist/lib/environment/registry.js +1 -1
- package/dist/lib/environment/requests.d.ts +2 -2
- package/dist/lib/environment/requests.js +2 -2
- package/dist/lib/environment/resolve.d.ts +4 -4
- package/dist/lib/environment/resolve.js +3 -4
- package/dist/lib/environment/store.d.ts +34 -8
- package/dist/lib/environment/store.js +138 -43
- package/dist/lib/files.d.ts +72 -12
- package/dist/lib/files.js +200 -65
- package/dist/lib/git-host.d.ts +52 -258
- package/dist/lib/git-host.js +52 -265
- package/dist/lib/home-file.d.ts +13 -10
- package/dist/lib/home-file.js +28 -18
- package/dist/lib/instance.d.ts +1 -1
- package/dist/lib/instance.js +1 -1
- package/dist/lint.d.ts +22 -0
- package/dist/lint.js +36 -0
- package/dist/testing/standin-registry.js +1 -1
- package/package.json +7 -3
- package/scaffold/.file-history.json +14 -7
- package/scaffold/i18n/zh/source-pins.json +41 -0
- package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
- package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
- package/scaffold/i18n/zh/specs/map.md +0 -1
- package/scaffold/i18n/zh/specs/meta.md +18 -35
- package/scaffold/i18n/zh/specs/packages/git.md +0 -1
- package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
- package/scaffold/specs/meta.md +18 -3
- package/scripts/test-exit-guard.mjs +28 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
/*
|
|
4
|
+
* Guards package (guards-2, guards-37), DR-252: the spec items a
|
|
5
|
+
* guard reads. They are read with the linter's own parser, so an item
|
|
6
|
+
* here is exactly what `spex lint` takes for one.
|
|
7
|
+
*
|
|
8
|
+
* For guard `<id>`: the guard item `<id>` in a Verification section,
|
|
9
|
+
* with its cases; the one behavior item of the same file it cites;
|
|
10
|
+
* and the items those two cite, one step.
|
|
11
|
+
*/
|
|
12
|
+
import { readSpecItems } from "../lint.js";
|
|
13
|
+
import { GuardError, byText } from "./types.js";
|
|
14
|
+
const BEHAVIOR_SECTIONS = new Set(["External Behavior", "Internal Behavior"]);
|
|
15
|
+
/** A Markdown link, `[text](target)`; the first group is its text. */
|
|
16
|
+
const LINK_RE = /\[([^\[\]]*)\]\([^()]*\)/g;
|
|
17
|
+
/** A line that starts a list item, with or without an indent. */
|
|
18
|
+
const LIST_RE = /^\s*(?:[-*+]|\d+[.)])\s/;
|
|
19
|
+
/** A case entry's first line: a list mark with no indent, the kind,
|
|
20
|
+
* and an em dash with one space either side. */
|
|
21
|
+
const CASE_RE = /^[-*+][ \t]+(permitted|forbidden) — (.*)$/;
|
|
22
|
+
/** A list item, with or without an indent, whose first word is
|
|
23
|
+
* `permitted` or `forbidden` in any letter case: one a reader takes
|
|
24
|
+
* for a case. */
|
|
25
|
+
const LIKE_CASE_RE = /^\s*(?:[-*+]|\d+[.)])\s+(permitted|forbidden)(?![A-Za-z0-9])/i;
|
|
26
|
+
function toItem(source) {
|
|
27
|
+
const lines = source.lines.map((line) => line.replace(/\r$/, ""));
|
|
28
|
+
while (lines.length > 1 && lines[lines.length - 1].trim() === "")
|
|
29
|
+
lines.pop();
|
|
30
|
+
const paragraph = [];
|
|
31
|
+
for (const line of lines.slice(1)) {
|
|
32
|
+
if (line.trim() === "") {
|
|
33
|
+
if (paragraph.length > 0)
|
|
34
|
+
break;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (LIST_RE.test(line))
|
|
38
|
+
break;
|
|
39
|
+
paragraph.push(line.trim());
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
id: source.id,
|
|
43
|
+
file: source.relPath,
|
|
44
|
+
line: source.line,
|
|
45
|
+
section: source.section,
|
|
46
|
+
text: lines.join("\n"),
|
|
47
|
+
statement: paragraph.join(" ").replace(LINK_RE, "$1"),
|
|
48
|
+
cites: [...new Set(source.cites.map((cited) => cited.id))].sort(byText),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Read every spec item under `<root>/specs`. */
|
|
52
|
+
export function readItems(root) {
|
|
53
|
+
const byId = new Map();
|
|
54
|
+
for (const source of readSpecItems(root)) {
|
|
55
|
+
const item = toItem(source);
|
|
56
|
+
const same = byId.get(item.id);
|
|
57
|
+
if (same === undefined)
|
|
58
|
+
byId.set(item.id, [item]);
|
|
59
|
+
else
|
|
60
|
+
same.push(item);
|
|
61
|
+
}
|
|
62
|
+
return { byId };
|
|
63
|
+
}
|
|
64
|
+
function one(index, id) {
|
|
65
|
+
const found = index.byId.get(id) ?? [];
|
|
66
|
+
if (found.length === 0)
|
|
67
|
+
throw new GuardError(`${id}: no spec item has this ID`);
|
|
68
|
+
if (found.length > 1) {
|
|
69
|
+
throw new GuardError(`${id}: several spec items have this ID (${found.map((item) => `${item.file}:${item.line}`).join(", ")})`);
|
|
70
|
+
}
|
|
71
|
+
return found[0];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The cases of a guard item (guards-37), read from its text: every
|
|
75
|
+
* list item with no indent that starts `permitted — ` or
|
|
76
|
+
* `forbidden — `, with the indented lines that follow it. A list
|
|
77
|
+
* item a reader would take for a case and that is not written so is
|
|
78
|
+
* an error, so that no case the item shows goes unchecked; any other
|
|
79
|
+
* list item is no case. Every such error is a GuardError starting
|
|
80
|
+
* with the item's ID.
|
|
81
|
+
*/
|
|
82
|
+
export function readCases(item) {
|
|
83
|
+
const bad = (cause) => {
|
|
84
|
+
throw new GuardError(`${item.id}: ${cause}`);
|
|
85
|
+
};
|
|
86
|
+
const cases = [];
|
|
87
|
+
const lines = item.text.split("\n").slice(1);
|
|
88
|
+
for (const line of lines) {
|
|
89
|
+
const like = LIKE_CASE_RE.exec(line);
|
|
90
|
+
if (like !== null && !CASE_RE.test(line)) {
|
|
91
|
+
const kind = like[1].toLowerCase();
|
|
92
|
+
bad(`the list entry '${line.trim()}' starts with ${like[1]} and is not written as a case: a case is a list item with no indent, '- ${kind} — <label>: <the case in plain words>', the dash being — with one space either side`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
for (let at = 0; at < lines.length; at += 1) {
|
|
96
|
+
const match = CASE_RE.exec(lines[at]);
|
|
97
|
+
if (match === null)
|
|
98
|
+
continue;
|
|
99
|
+
const kind = match[1];
|
|
100
|
+
const parts = [match[2].trim()];
|
|
101
|
+
while (at + 1 < lines.length && /^[ \t]/.test(lines[at + 1]) && lines[at + 1].trim() !== "") {
|
|
102
|
+
at += 1;
|
|
103
|
+
parts.push(lines[at].trim());
|
|
104
|
+
}
|
|
105
|
+
const entry = parts.filter((part) => part !== "").join(" ");
|
|
106
|
+
const colon = entry.indexOf(":");
|
|
107
|
+
if (colon < 0)
|
|
108
|
+
bad(`the case entry '${kind} — ${entry}' has no colon after its label`);
|
|
109
|
+
const label = entry.slice(0, colon).trim();
|
|
110
|
+
if (label === "")
|
|
111
|
+
bad(`a ${kind} case has an empty label`);
|
|
112
|
+
const words = entry
|
|
113
|
+
.slice(colon + 1)
|
|
114
|
+
.trim()
|
|
115
|
+
.replace(LINK_RE, "$1")
|
|
116
|
+
.replace(/[;.]$/, "")
|
|
117
|
+
.trim();
|
|
118
|
+
if (words === "")
|
|
119
|
+
bad(`case '${label}' has no plain words after its label`);
|
|
120
|
+
if (cases.some((other) => other.label === label))
|
|
121
|
+
bad(`two cases share the label '${label}'`);
|
|
122
|
+
cases.push({ kind, label, words });
|
|
123
|
+
}
|
|
124
|
+
for (const kind of ["permitted", "forbidden"]) {
|
|
125
|
+
if (!cases.some((entry) => entry.kind === kind)) {
|
|
126
|
+
bad(`no ${kind} case; a guard item has at least one, written '- ${kind} — <label>: <the case in plain words>'`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return cases;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The items of guard `guardId` (guards-2). Every error is a
|
|
133
|
+
* GuardError starting with the ID of the item at fault.
|
|
134
|
+
*/
|
|
135
|
+
export function guardItems(index, guardId) {
|
|
136
|
+
const guard = one(index, guardId);
|
|
137
|
+
if (guard.section !== "Verification") {
|
|
138
|
+
throw new GuardError(`${guardId}: not in a Verification section, so it cannot be a guard item`);
|
|
139
|
+
}
|
|
140
|
+
const behaviors = guard.cites
|
|
141
|
+
.map((id) => one(index, id))
|
|
142
|
+
.filter((item) => item.file === guard.file && item.section !== null && BEHAVIOR_SECTIONS.has(item.section));
|
|
143
|
+
if (behaviors.length === 0) {
|
|
144
|
+
throw new GuardError(`${guardId}: cites no behavior item of its own file; a guard checks one`);
|
|
145
|
+
}
|
|
146
|
+
if (behaviors.length > 1) {
|
|
147
|
+
throw new GuardError(`${guardId}: cites several behavior items (${behaviors.map((item) => item.id).join(", ")}); a guard checks one`);
|
|
148
|
+
}
|
|
149
|
+
const behavior = behaviors[0];
|
|
150
|
+
const cases = readCases(guard);
|
|
151
|
+
const own = new Set([guard.id, behavior.id]);
|
|
152
|
+
const citedIds = new Set();
|
|
153
|
+
for (const item of [guard, behavior]) {
|
|
154
|
+
for (const id of item.cites)
|
|
155
|
+
if (!own.has(id))
|
|
156
|
+
citedIds.add(id);
|
|
157
|
+
}
|
|
158
|
+
const cited = [...citedIds].sort(byText).map((id) => one(index, id));
|
|
159
|
+
return { guard, behavior, cases, cited };
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=items.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** One line of a change: kept (` `), taken out (`-`) or put in (`+`). */
|
|
2
|
+
export type PatchLine = {
|
|
3
|
+
kind: " " | "-" | "+";
|
|
4
|
+
text: string;
|
|
5
|
+
noNewline: boolean;
|
|
6
|
+
};
|
|
7
|
+
/** One `@@` block. `line` is the 1-based line it states for the file
|
|
8
|
+
* before the change. */
|
|
9
|
+
export type Hunk = {
|
|
10
|
+
line: number;
|
|
11
|
+
lines: PatchLine[];
|
|
12
|
+
};
|
|
13
|
+
/** The changes to one file. `path` is relative to the root, with `/`. */
|
|
14
|
+
export type FilePatch = {
|
|
15
|
+
path: string;
|
|
16
|
+
hunks: Hunk[];
|
|
17
|
+
};
|
|
18
|
+
/** A parsed patch. The text before its first file is a description
|
|
19
|
+
* for people and is not read. */
|
|
20
|
+
export type Patch = {
|
|
21
|
+
files: FilePatch[];
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Read a unified diff. Throws a GuardError, naming the line, when the
|
|
25
|
+
* text is not one, when it adds, removes, renames or names a file
|
|
26
|
+
* twice, changes a binary file or names a path outside the root, or
|
|
27
|
+
* when a change has more or fewer lines than its header counts.
|
|
28
|
+
*/
|
|
29
|
+
export declare function parsePatch(text: string): Patch;
|
|
30
|
+
/**
|
|
31
|
+
* Apply one file's changes to its text. Returns the new text, or the
|
|
32
|
+
* stated line of the first change that matches nowhere.
|
|
33
|
+
*/
|
|
34
|
+
export declare function applyToText(text: string, file: FilePatch): {
|
|
35
|
+
text: string;
|
|
36
|
+
} | {
|
|
37
|
+
failedAt: number;
|
|
38
|
+
};
|
|
39
|
+
/** What applying a patch came to. A patch that applies but leaves
|
|
40
|
+
* every file as it was has `changed: false`. */
|
|
41
|
+
export type PatchOutcome = {
|
|
42
|
+
applies: true;
|
|
43
|
+
changed: boolean;
|
|
44
|
+
} | {
|
|
45
|
+
applies: false;
|
|
46
|
+
cause: string;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Apply a patch to the files under `folder`. Nothing is written
|
|
50
|
+
* unless every change applies. A missing file, a file whose real
|
|
51
|
+
* path is outside `folder` (reached through a link), a file that is
|
|
52
|
+
* not text, or a change that matches nowhere means the patch does
|
|
53
|
+
* not apply, with the cause in plain words.
|
|
54
|
+
*/
|
|
55
|
+
export declare function applyPatch(folder: string, patch: Patch): PatchOutcome;
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
/*
|
|
4
|
+
* Guards package, DR-252: a kept fault's patch. It is a unified diff
|
|
5
|
+
* as `git diff` writes it, changing existing files. Spex reads and
|
|
6
|
+
* applies it itself, with no `git`: each change goes at its stated
|
|
7
|
+
* line, else at the nearest place where its lines match. Lines
|
|
8
|
+
* compare ignoring a trailing CR, and a file keeps its own line
|
|
9
|
+
* endings.
|
|
10
|
+
*/
|
|
11
|
+
import { chmodSync, existsSync, readFileSync, realpathSync, statSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { join, sep } from "node:path";
|
|
13
|
+
import { GuardError } from "./types.js";
|
|
14
|
+
const HUNK_RE = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
|
|
15
|
+
/** The path a `--- ` or `+++ ` line names; `line` is that line's
|
|
16
|
+
* number, for the error. */
|
|
17
|
+
function patchPath(raw, line) {
|
|
18
|
+
const path = raw.split("\t")[0].trim();
|
|
19
|
+
if (path === "/dev/null")
|
|
20
|
+
return null;
|
|
21
|
+
if (path.startsWith('"')) {
|
|
22
|
+
throw new GuardError(`line ${line}: the path ${path} has characters a kept fault's patch cannot name`);
|
|
23
|
+
}
|
|
24
|
+
return path;
|
|
25
|
+
}
|
|
26
|
+
/** A patch's file name without its first folder, where that folder
|
|
27
|
+
* is one character long: the prefix `git diff` writes. */
|
|
28
|
+
function unprefixed(name) {
|
|
29
|
+
return name[1] === "/" ? name.slice(2) : name;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Read a unified diff. Throws a GuardError, naming the line, when the
|
|
33
|
+
* text is not one, when it adds, removes, renames or names a file
|
|
34
|
+
* twice, changes a binary file or names a path outside the root, or
|
|
35
|
+
* when a change has more or fewer lines than its header counts.
|
|
36
|
+
*/
|
|
37
|
+
export function parsePatch(text) {
|
|
38
|
+
const lines = text.split("\n").map((line) => line.replace(/\r$/, ""));
|
|
39
|
+
if (lines[lines.length - 1] === "")
|
|
40
|
+
lines.pop();
|
|
41
|
+
let at = lines.findIndex((line) => line.startsWith("diff --git ") || line.startsWith("--- "));
|
|
42
|
+
if (at < 0)
|
|
43
|
+
throw new GuardError("no change found; a kept fault is a unified diff, as git diff writes it");
|
|
44
|
+
const files = [];
|
|
45
|
+
while (at < lines.length) {
|
|
46
|
+
const line = lines[at];
|
|
47
|
+
if (line.startsWith("@@"))
|
|
48
|
+
throw new GuardError(`line ${at + 1}: a change comes before its file is named`);
|
|
49
|
+
if (/^(rename|copy) (from|to) /.test(line)) {
|
|
50
|
+
throw new GuardError(`line ${at + 1}: it renames or copies a file; a kept fault changes existing files`);
|
|
51
|
+
}
|
|
52
|
+
if (/^(new|deleted) file mode /.test(line)) {
|
|
53
|
+
throw new GuardError(`line ${at + 1}: it adds or removes a file; a kept fault changes existing files`);
|
|
54
|
+
}
|
|
55
|
+
if (line.startsWith("GIT binary patch") || /^Binary files .* differ$/.test(line)) {
|
|
56
|
+
throw new GuardError(`line ${at + 1}: it changes a binary file; a kept fault changes text`);
|
|
57
|
+
}
|
|
58
|
+
if (!line.startsWith("--- ")) {
|
|
59
|
+
// A line of a change outside any change: the header before it
|
|
60
|
+
// counted fewer lines than were written.
|
|
61
|
+
if (/^[ +-]/.test(line)) {
|
|
62
|
+
throw new GuardError(files.length === 0
|
|
63
|
+
? `line ${at + 1}: a change comes before its file is named`
|
|
64
|
+
: `line ${at + 1}: the change has more lines than its header says`);
|
|
65
|
+
}
|
|
66
|
+
at += 1;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const next = lines[at + 1] ?? "";
|
|
70
|
+
if (!next.startsWith("+++ "))
|
|
71
|
+
throw new GuardError(`line ${at + 2}: expected a '+++ ' line after '--- '`);
|
|
72
|
+
const before = patchPath(line.slice(4), at + 1);
|
|
73
|
+
const after = patchPath(next.slice(4), at + 2);
|
|
74
|
+
if (before === null || after === null) {
|
|
75
|
+
throw new GuardError(`line ${at + 1}: it adds or removes a file; a kept fault changes existing files`);
|
|
76
|
+
}
|
|
77
|
+
// Git writes a one-character folder before each name: a/ and b/,
|
|
78
|
+
// or i/, w/, c/, o/, 1/ and 2/ under other settings.
|
|
79
|
+
const path = unprefixed(before);
|
|
80
|
+
if (unprefixed(after) !== path) {
|
|
81
|
+
throw new GuardError(`line ${at + 1}: it renames ${before} to ${after}; a kept fault changes existing files`);
|
|
82
|
+
}
|
|
83
|
+
if (path === "" || path.startsWith("/") || /^[A-Za-z]:/.test(path) || path.includes("\\") || path.split("/").includes("..")) {
|
|
84
|
+
throw new GuardError(`line ${at + 1}: the path ${path} does not stay inside the project`);
|
|
85
|
+
}
|
|
86
|
+
if (files.some((file) => file.path === path)) {
|
|
87
|
+
throw new GuardError(`line ${at + 1}: ${path} is named twice`);
|
|
88
|
+
}
|
|
89
|
+
const named = at + 1;
|
|
90
|
+
at += 2;
|
|
91
|
+
const hunks = [];
|
|
92
|
+
while (at < lines.length && lines[at].startsWith("@@")) {
|
|
93
|
+
const header = HUNK_RE.exec(lines[at]);
|
|
94
|
+
if (header === null)
|
|
95
|
+
throw new GuardError(`line ${at + 1}: not a change header: ${lines[at]}`);
|
|
96
|
+
let old = header[2] === undefined ? 1 : Number(header[2]);
|
|
97
|
+
let fresh = header[4] === undefined ? 1 : Number(header[4]);
|
|
98
|
+
const hunk = { line: Number(header[1]), lines: [] };
|
|
99
|
+
at += 1;
|
|
100
|
+
while (old > 0 || fresh > 0) {
|
|
101
|
+
const body = lines[at];
|
|
102
|
+
if (body === undefined)
|
|
103
|
+
throw new GuardError(`line ${at + 1}: the change ends early`);
|
|
104
|
+
const kind = body === "" ? " " : body[0];
|
|
105
|
+
if (kind === "\\") {
|
|
106
|
+
if (hunk.lines.length > 0)
|
|
107
|
+
hunk.lines[hunk.lines.length - 1].noNewline = true;
|
|
108
|
+
at += 1;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (kind !== " " && kind !== "-" && kind !== "+") {
|
|
112
|
+
throw new GuardError(`line ${at + 1}: expected a line starting with ' ', '-' or '+'`);
|
|
113
|
+
}
|
|
114
|
+
if (kind !== "+")
|
|
115
|
+
old -= 1;
|
|
116
|
+
if (kind !== "-")
|
|
117
|
+
fresh -= 1;
|
|
118
|
+
if (old < 0 || fresh < 0)
|
|
119
|
+
throw new GuardError(`line ${at + 1}: the change has more lines than its header says`);
|
|
120
|
+
hunk.lines.push({ kind, text: body.slice(1), noNewline: false });
|
|
121
|
+
at += 1;
|
|
122
|
+
}
|
|
123
|
+
if (lines[at]?.startsWith("\\")) {
|
|
124
|
+
hunk.lines[hunk.lines.length - 1].noNewline = true;
|
|
125
|
+
at += 1;
|
|
126
|
+
}
|
|
127
|
+
hunks.push(hunk);
|
|
128
|
+
}
|
|
129
|
+
if (hunks.length === 0)
|
|
130
|
+
throw new GuardError(`line ${named}: no change follows the name of ${path}`);
|
|
131
|
+
files.push({ path, hunks });
|
|
132
|
+
}
|
|
133
|
+
if (files.length === 0)
|
|
134
|
+
throw new GuardError("no change found; a kept fault is a unified diff, as git diff writes it");
|
|
135
|
+
return { files };
|
|
136
|
+
}
|
|
137
|
+
function split(text) {
|
|
138
|
+
const lines = [];
|
|
139
|
+
const re = /([^\n]*)\n/y;
|
|
140
|
+
let at = 0;
|
|
141
|
+
for (;;) {
|
|
142
|
+
re.lastIndex = at;
|
|
143
|
+
const match = re.exec(text);
|
|
144
|
+
if (match === null)
|
|
145
|
+
break;
|
|
146
|
+
const cr = match[1].endsWith("\r");
|
|
147
|
+
lines.push({ text: cr ? match[1].slice(0, -1) : match[1], end: cr ? "\r\n" : "\n" });
|
|
148
|
+
at = re.lastIndex;
|
|
149
|
+
}
|
|
150
|
+
if (at < text.length)
|
|
151
|
+
lines.push({ text: text.slice(at), end: "" });
|
|
152
|
+
return lines;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Apply one file's changes to its text. Returns the new text, or the
|
|
156
|
+
* stated line of the first change that matches nowhere.
|
|
157
|
+
*/
|
|
158
|
+
export function applyToText(text, file) {
|
|
159
|
+
const lines = split(text);
|
|
160
|
+
const ending = lines.length > 0 && lines[0].end === "\r\n" ? "\r\n" : "\n";
|
|
161
|
+
let shift = 0;
|
|
162
|
+
let floor = 0;
|
|
163
|
+
for (const hunk of file.hunks) {
|
|
164
|
+
const old = hunk.lines.filter((line) => line.kind !== "+");
|
|
165
|
+
const matches = (start) => start >= floor &&
|
|
166
|
+
start + old.length <= lines.length &&
|
|
167
|
+
old.every((line, index) => lines[start + index].text === line.text);
|
|
168
|
+
// A change with no old lines states the line it goes after.
|
|
169
|
+
const stated = (old.length === 0 ? hunk.line : hunk.line - 1) + shift;
|
|
170
|
+
let found = -1;
|
|
171
|
+
const reach = lines.length + Math.abs(stated);
|
|
172
|
+
for (let away = 0; away <= reach && found < 0; away += 1) {
|
|
173
|
+
if (matches(stated - away))
|
|
174
|
+
found = stated - away;
|
|
175
|
+
else if (matches(stated + away))
|
|
176
|
+
found = stated + away;
|
|
177
|
+
}
|
|
178
|
+
if (found < 0)
|
|
179
|
+
return { failedAt: hunk.line };
|
|
180
|
+
const fresh = [];
|
|
181
|
+
let from = found;
|
|
182
|
+
for (const line of hunk.lines) {
|
|
183
|
+
if (line.kind === " ") {
|
|
184
|
+
fresh.push(lines[from]);
|
|
185
|
+
from += 1;
|
|
186
|
+
}
|
|
187
|
+
else if (line.kind === "-") {
|
|
188
|
+
from += 1;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
fresh.push({ text: line.text, end: line.noNewline ? "" : ending });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
lines.splice(found, old.length, ...fresh);
|
|
195
|
+
shift += found - stated + (fresh.length - old.length);
|
|
196
|
+
floor = found + fresh.length;
|
|
197
|
+
}
|
|
198
|
+
// Only the last line may go without a line ending.
|
|
199
|
+
for (const line of lines.slice(0, -1))
|
|
200
|
+
if (line.end === "")
|
|
201
|
+
line.end = ending;
|
|
202
|
+
return { text: lines.map((line) => line.text + line.end).join("") };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Apply a patch to the files under `folder`. Nothing is written
|
|
206
|
+
* unless every change applies. A missing file, a file whose real
|
|
207
|
+
* path is outside `folder` (reached through a link), a file that is
|
|
208
|
+
* not text, or a change that matches nowhere means the patch does
|
|
209
|
+
* not apply, with the cause in plain words.
|
|
210
|
+
*/
|
|
211
|
+
export function applyPatch(folder, patch) {
|
|
212
|
+
const results = [];
|
|
213
|
+
const inside = realpathSync.native(folder) + sep;
|
|
214
|
+
let changed = false;
|
|
215
|
+
for (const entry of patch.files) {
|
|
216
|
+
const file = join(folder, ...entry.path.split("/"));
|
|
217
|
+
if (!existsSync(file) || !statSync(file).isFile()) {
|
|
218
|
+
return { applies: false, cause: `${entry.path} is not among the copied files` };
|
|
219
|
+
}
|
|
220
|
+
// A link in the folder may lead out of it; nothing is written there.
|
|
221
|
+
if (!realpathSync.native(file).startsWith(inside)) {
|
|
222
|
+
return { applies: false, cause: `${entry.path} is outside the copy` };
|
|
223
|
+
}
|
|
224
|
+
const bytes = readFileSync(file);
|
|
225
|
+
const text = bytes.toString("utf8");
|
|
226
|
+
if (!Buffer.from(text, "utf8").equals(bytes)) {
|
|
227
|
+
return { applies: false, cause: `${entry.path} is not text` };
|
|
228
|
+
}
|
|
229
|
+
const result = applyToText(text, entry);
|
|
230
|
+
if ("failedAt" in result) {
|
|
231
|
+
return {
|
|
232
|
+
applies: false,
|
|
233
|
+
cause: `the change at line ${result.failedAt} of ${entry.path} matches nowhere in the file`,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
if (result.text !== text) {
|
|
237
|
+
changed = true;
|
|
238
|
+
results.push({ file, text: result.text });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
for (const result of results) {
|
|
242
|
+
// A file the project keeps read-only is written all the same and
|
|
243
|
+
// left read-only: the copy's code meets it as the project has it.
|
|
244
|
+
const mode = statSync(result.file).mode & 0o777;
|
|
245
|
+
const locked = (mode & 0o200) === 0;
|
|
246
|
+
if (locked)
|
|
247
|
+
chmodSync(result.file, mode | 0o200);
|
|
248
|
+
try {
|
|
249
|
+
writeFileSync(result.file, result.text);
|
|
250
|
+
}
|
|
251
|
+
finally {
|
|
252
|
+
if (locked)
|
|
253
|
+
chmodSync(result.file, mode);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return { applies: true, changed };
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=patch.js.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** How one process ended. */
|
|
2
|
+
export type Ran = {
|
|
3
|
+
/** Its exit status; null where a signal ended it. */
|
|
4
|
+
code: number | null;
|
|
5
|
+
/** It was stopped at the time limit. */
|
|
6
|
+
timedOut: boolean;
|
|
7
|
+
/** The end of what it wrote on standard error. */
|
|
8
|
+
errors: string;
|
|
9
|
+
/** Why it could not start, where it could not. */
|
|
10
|
+
failed: string | null;
|
|
11
|
+
/** It ended by itself and left a process it started running, which
|
|
12
|
+
* was then stopped. Always false on Windows, which cannot see one. */
|
|
13
|
+
left: boolean;
|
|
14
|
+
};
|
|
15
|
+
/** What `runProcess` needs beside the program and its words. */
|
|
16
|
+
export type RunOptions = {
|
|
17
|
+
cwd: string;
|
|
18
|
+
env: NodeJS.ProcessEnv;
|
|
19
|
+
/** The time limit. */
|
|
20
|
+
seconds: number;
|
|
21
|
+
/** A file to hold what the process writes on standard error. */
|
|
22
|
+
errors: string;
|
|
23
|
+
/** A file to hold what it writes on standard output; dropped where
|
|
24
|
+
* none is named. Where it is `errors`, both go to that one file in
|
|
25
|
+
* the order written. */
|
|
26
|
+
output?: string;
|
|
27
|
+
/** Text that an entry of the environment of every process the run
|
|
28
|
+
* starts holds, as a path in `env` that no other process names. On
|
|
29
|
+
* Linux a process that holds it is the run's, though it left the
|
|
30
|
+
* group. */
|
|
31
|
+
mark?: string;
|
|
32
|
+
};
|
|
33
|
+
/** Stop every process still running, with all they started. Safe to
|
|
34
|
+
* call at any time; it waits for nothing. */
|
|
35
|
+
export declare function stopProcesses(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Run one program to its end or to the time limit, whichever comes
|
|
38
|
+
* first. What it prints on standard output is dropped, or kept in the
|
|
39
|
+
* file `output` names. When it ends, however it ends, every process
|
|
40
|
+
* it started that can be found is stopped too; where it ended by
|
|
41
|
+
* itself, `left` says whether one was still running.
|
|
42
|
+
*/
|
|
43
|
+
export declare function runProcess(program: string, args: readonly string[], options: RunOptions): Promise<Ran>;
|
|
44
|
+
/**
|
|
45
|
+
* The cause a process gave for ending early: the last line of its
|
|
46
|
+
* standard error that is neither Node's `Node.js v…` line nor a line
|
|
47
|
+
* of a stack, starting `at `. Empty where it wrote none.
|
|
48
|
+
*/
|
|
49
|
+
export declare function lastWords(errors: string): string;
|