@wildshard/shard-format 0.69.2 → 0.71.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +21 -3
- package/src/cli/corpus-candidate.ts +228 -0
- package/src/cli/corpus-command.ts +67 -0
- package/src/cli/corpus-pins.ts +73 -0
- package/src/cli/corpus-report.ts +36 -0
- package/src/cli/corpus-tar.ts +28 -0
- package/src/cli/corpus-usage.ts +106 -0
- package/src/cli/corpus.ts +50 -68
- package/src/cli/format-diff-data.ts +89 -0
- package/src/cli/format-diff-lock.ts +122 -0
- package/src/cli/format-diff-normal.ts +300 -0
- package/src/cli/format-diff-paths.ts +103 -0
- package/src/cli/format-diff-range.ts +112 -0
- package/src/cli/format-diff-report.ts +30 -0
- package/src/cli/format-diff-rules.ts +232 -0
- package/src/cli/format-diff.ts +226 -0
- package/src/cli/main.ts +81 -32
- package/src/cli/pack.ts +20 -3
- package/src/cli/validate.ts +1 -1
- package/src/format/clientOwnership.ts +1 -1
- package/src/format/clientTrails.ts +121 -46
- package/src/format/cook-data.ts +22 -3
- package/src/format/cook-deprecations.ts +53 -0
- package/src/format/cook-preparation.ts +105 -70
- package/src/format/creature-rest-rig.ts +248 -0
- package/src/format/creature-skin-bounds.ts +146 -0
- package/src/format/deprecations.ts +30 -21
- package/src/format/edge-observations.ts +67 -0
- package/src/format/empty-shardfile.ts +4 -2
- package/src/format/entry-read-set.ts +147 -0
- package/src/format/format-migrate-rebuild.ts +4 -1
- package/src/format/format-migrate.ts +10 -5
- package/src/format/format-migration-envelope.ts +40 -0
- package/src/format/format-migration-prune.ts +42 -1
- package/src/format/format-migrations.ts +10 -1
- package/src/format/format-window.ts +71 -0
- package/src/format/immutable.ts +4 -1
- package/src/format/json-schema.ts +1468 -0
- package/src/format/lootEconomy.ts +4 -1
- package/src/format/mat4.ts +225 -0
- package/src/format/memory-armed.json +1 -9
- package/src/format/memory-armed.ts +46 -17
- package/src/format/memory-epoch.ts +10 -3
- package/src/format/memory-line.ts +17 -6
- package/src/format/outputFields.ts +3 -2
- package/src/format/outputIndex.ts +41 -6
- package/src/format/platformDataSections.ts +5 -0
- package/src/format/platformFileFamilies.ts +3 -3
- package/src/format/play-area.ts +95 -0
- package/src/format/policy-verdict.ts +12 -2
- package/src/format/preparation-checks.ts +100 -0
- package/src/format/preparation-spec.ts +45 -0
- package/src/format/registeredJson.ts +2 -2
- package/src/format/runtime-tier.ts +14 -7
- package/src/format/save-revision.ts +105 -0
- package/src/format/schema-source.ts +70 -0
- package/src/format/sections.ts +4 -1
- package/src/format/server-plan.ts +42 -0
- package/src/format/server-reads.ts +23 -0
- package/src/format/shardSchema.ts +9 -4
- package/src/format/shardSections.ts +17 -7
- package/src/format/shardfile-assets.ts +5 -5
- package/src/format/shardfile-stream-admit.ts +19 -10
- package/src/format/shardfile-stream-facts.ts +2 -1
- package/src/format/shardfile-stream.ts +2 -3
- package/src/format/shardfileValidation.ts +16 -9
- package/src/format/species-deprecations.ts +79 -0
- package/src/format/stateLineage.ts +15 -7
- package/src/format/tread-boxes.ts +65 -0
- package/src/format/vehicle-hulls.ts +124 -0
- package/src/format/version.ts +96 -10
- package/src/format/whole-inputs.ts +51 -0
- package/src/tools/runtime-hosts.json +4 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { isRecord } from './format-diff-lock.ts'
|
|
2
|
+
|
|
3
|
+
import type { Json } from './format-diff-lock.ts'
|
|
4
|
+
|
|
5
|
+
// SHARD-COMPAT FE2: a lock's rows put in a form two versions compare on.
|
|
6
|
+
// 1. A `ref` row takes its target's row and the target's whole subtree, so
|
|
7
|
+
// sharing a schema or walking modules in a new order moves nothing.
|
|
8
|
+
// 2. `expects` is dropped: it is message text derived from the other facts.
|
|
9
|
+
// 3. A wrapper (optional, nullable, ...) folds into the node it wraps, as a
|
|
10
|
+
// `wrap` list, so making a field optional changes one row, not the path
|
|
11
|
+
// of every row under it.
|
|
12
|
+
// 4. A variant option is keyed by its discriminating literal (`|=boss`), not
|
|
13
|
+
// its index, so inserting an option renames none of the others.
|
|
14
|
+
|
|
15
|
+
interface SchemaRow {
|
|
16
|
+
readonly kind: 'schema'
|
|
17
|
+
readonly type: string
|
|
18
|
+
readonly wrap: readonly string[]
|
|
19
|
+
readonly facts: Readonly<Record<string, Json>>
|
|
20
|
+
}
|
|
21
|
+
interface DataRow {
|
|
22
|
+
readonly kind: 'data'
|
|
23
|
+
readonly value: Json
|
|
24
|
+
}
|
|
25
|
+
type Row = DataRow | SchemaRow
|
|
26
|
+
|
|
27
|
+
const MARKS: ReadonlySet<string> = new Set(['.', '[', '|', '?', '{'])
|
|
28
|
+
const SCHEMA_KEYS: ReadonlySet<string> = new Set([
|
|
29
|
+
'async',
|
|
30
|
+
'default',
|
|
31
|
+
'expects',
|
|
32
|
+
'getter',
|
|
33
|
+
'literal',
|
|
34
|
+
'pipe',
|
|
35
|
+
'requirement',
|
|
36
|
+
'type',
|
|
37
|
+
])
|
|
38
|
+
const WRAPPERS: ReadonlySet<string> = new Set([
|
|
39
|
+
'exact_optional',
|
|
40
|
+
'non_nullable',
|
|
41
|
+
'non_nullish',
|
|
42
|
+
'non_optional',
|
|
43
|
+
'nullable',
|
|
44
|
+
'nullish',
|
|
45
|
+
'optional',
|
|
46
|
+
'undefinedable',
|
|
47
|
+
])
|
|
48
|
+
const MAX_PASSES = 64
|
|
49
|
+
const OPTION = /^(?<group>.*)\|(?<index>\d+)$/u
|
|
50
|
+
const UNSAFE = /[.[|?{}#]/gu
|
|
51
|
+
const NONE = 0
|
|
52
|
+
const NEXT = 1
|
|
53
|
+
const HALF = 2
|
|
54
|
+
const SEGMENT = /\|\d+/gu
|
|
55
|
+
|
|
56
|
+
function refOf(value: Json | undefined): string | undefined {
|
|
57
|
+
return isRecord(value) && typeof value['ref'] === 'string'
|
|
58
|
+
? value['ref']
|
|
59
|
+
: undefined
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The first index in a sorted list at or after `key`. */
|
|
63
|
+
function lowerBound(sorted: readonly string[], key: string): number {
|
|
64
|
+
let low = NONE
|
|
65
|
+
let high = sorted.length
|
|
66
|
+
while (low < high) {
|
|
67
|
+
const middle = Math.floor((low + high) / HALF)
|
|
68
|
+
if ((sorted[middle] ?? '') < key) {
|
|
69
|
+
low = middle + NEXT
|
|
70
|
+
} else {
|
|
71
|
+
high = middle
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return low
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The keys strictly under `prefix` (a mark follows it), from a sorted list. */
|
|
78
|
+
function under(sorted: readonly string[], prefix: string): readonly string[] {
|
|
79
|
+
const found: string[] = []
|
|
80
|
+
for (const key of sorted.slice(lowerBound(sorted, prefix))) {
|
|
81
|
+
if (!key.startsWith(prefix)) {
|
|
82
|
+
break
|
|
83
|
+
}
|
|
84
|
+
if (MARKS.has(key.charAt(prefix.length))) {
|
|
85
|
+
found.push(key)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return found
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function expandOnce(rows: ReadonlyMap<string, Json>): Map<string, Json> {
|
|
92
|
+
const sorted = [...rows.keys()].toSorted()
|
|
93
|
+
const out = new Map(rows)
|
|
94
|
+
for (const [key, value] of rows) {
|
|
95
|
+
const target = refOf(value)
|
|
96
|
+
const row = target === undefined ? undefined : rows.get(target)
|
|
97
|
+
if (target !== undefined && row !== undefined) {
|
|
98
|
+
out.set(key, row)
|
|
99
|
+
for (const sub of under(sorted, target)) {
|
|
100
|
+
out.set(`${key}${sub.slice(target.length)}`, rows.get(sub) ?? row)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return out
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Every `ref` replaced by its target and the target's subtree. */
|
|
108
|
+
function expandRefs(raw: Readonly<Record<string, Json>>): Map<string, Json> {
|
|
109
|
+
let rows = new Map(Object.entries(raw))
|
|
110
|
+
let passes = NONE
|
|
111
|
+
while (
|
|
112
|
+
passes < MAX_PASSES &&
|
|
113
|
+
[...rows.values()].some(
|
|
114
|
+
(value: Json): boolean => refOf(value) !== undefined,
|
|
115
|
+
)
|
|
116
|
+
) {
|
|
117
|
+
rows = expandOnce(rows)
|
|
118
|
+
passes += NEXT
|
|
119
|
+
}
|
|
120
|
+
return rows
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function withoutExpects(pipe: Json): Json {
|
|
124
|
+
return Array.isArray(pipe)
|
|
125
|
+
? pipe.map((action: Json): Json =>
|
|
126
|
+
isRecord(action)
|
|
127
|
+
? Object.fromEntries(
|
|
128
|
+
Object.entries(action).filter(
|
|
129
|
+
([key]: readonly [string, Json]): boolean => key !== 'expects',
|
|
130
|
+
),
|
|
131
|
+
)
|
|
132
|
+
: action,
|
|
133
|
+
)
|
|
134
|
+
: pipe
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** One raw row as a schema node's facts (without `expects`) or plain data. */
|
|
138
|
+
function rowOf(value: Json): Row {
|
|
139
|
+
if (
|
|
140
|
+
!isRecord(value) ||
|
|
141
|
+
typeof value['type'] !== 'string' ||
|
|
142
|
+
!Object.keys(value).every((key: string): boolean => SCHEMA_KEYS.has(key))
|
|
143
|
+
) {
|
|
144
|
+
return { kind: 'data', value }
|
|
145
|
+
}
|
|
146
|
+
const facts: Record<string, Json> = {}
|
|
147
|
+
for (const [key, fact] of Object.entries(value)) {
|
|
148
|
+
if (key !== 'type' && key !== 'expects') {
|
|
149
|
+
facts[key] = key === 'pipe' ? withoutExpects(fact) : fact
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return { kind: 'schema', type: value['type'], wrap: [], facts }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A wrapper row folded over the row it wraps. */
|
|
156
|
+
function folded(outer: SchemaRow, inner: Row): Row {
|
|
157
|
+
if (inner.kind === 'data') {
|
|
158
|
+
return inner
|
|
159
|
+
}
|
|
160
|
+
const outerFacts = Object.entries(outer.facts).map(
|
|
161
|
+
([key, fact]: readonly [string, Json]): readonly [string, Json] => [
|
|
162
|
+
`wrap:${key}`,
|
|
163
|
+
fact,
|
|
164
|
+
],
|
|
165
|
+
)
|
|
166
|
+
return {
|
|
167
|
+
...inner,
|
|
168
|
+
wrap: [...outer.wrap, outer.type, ...inner.wrap],
|
|
169
|
+
facts: { ...inner.facts, ...Object.fromEntries(outerFacts) },
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Wrappers folded into the nodes they wrap, under paths without `?`. */
|
|
174
|
+
function foldWrappers(rows: ReadonlyMap<string, Json>): Map<string, Row> {
|
|
175
|
+
const out = new Map<string, Row>()
|
|
176
|
+
for (const key of [...rows.keys()].toSorted()) {
|
|
177
|
+
const row = rowOf(rows.get(key) ?? '')
|
|
178
|
+
const at = key.replaceAll('?', '')
|
|
179
|
+
const outer = out.get(at)
|
|
180
|
+
out.set(
|
|
181
|
+
at,
|
|
182
|
+
outer?.kind === 'schema' && WRAPPERS.has(outer.type) && key !== at
|
|
183
|
+
? folded(outer, row)
|
|
184
|
+
: row,
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
return out
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function literalText(value: Json | undefined): string {
|
|
191
|
+
if (value === undefined) {
|
|
192
|
+
return ''
|
|
193
|
+
}
|
|
194
|
+
return typeof value === 'string' ? value : JSON.stringify(value)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Each node's direct literal children, by node path then field name. */
|
|
198
|
+
function literalChildren(
|
|
199
|
+
rows: ReadonlyMap<string, Row>,
|
|
200
|
+
): ReadonlyMap<string, ReadonlyMap<string, string>> {
|
|
201
|
+
const out = new Map<string, Map<string, string>>()
|
|
202
|
+
for (const [key, row] of rows) {
|
|
203
|
+
const dot = key.lastIndexOf('.')
|
|
204
|
+
const name = key.slice(dot + NEXT)
|
|
205
|
+
if (
|
|
206
|
+
row.kind === 'schema' &&
|
|
207
|
+
row.type === 'literal' &&
|
|
208
|
+
dot > NONE &&
|
|
209
|
+
!/[[|{]/u.test(name)
|
|
210
|
+
) {
|
|
211
|
+
const parent = key.slice(NONE, dot)
|
|
212
|
+
const children = out.get(parent) ?? new Map<string, string>()
|
|
213
|
+
children.set(name, literalText(row.facts['literal']))
|
|
214
|
+
out.set(parent, children)
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return out
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The literal each option of one union holds under a name every option shares, by option path. */
|
|
221
|
+
function discriminants(
|
|
222
|
+
literals: ReadonlyMap<string, ReadonlyMap<string, string>>,
|
|
223
|
+
options: readonly string[],
|
|
224
|
+
): readonly (readonly [string, string])[] {
|
|
225
|
+
const found = options.map(
|
|
226
|
+
(option: string): ReadonlyMap<string, string> =>
|
|
227
|
+
literals.get(option) ?? new Map<string, string>(),
|
|
228
|
+
)
|
|
229
|
+
const name = [...(found.at(NONE)?.keys() ?? [])].find(
|
|
230
|
+
(candidate: string): boolean => {
|
|
231
|
+
const values = found.map(
|
|
232
|
+
(children: ReadonlyMap<string, string>): string | undefined =>
|
|
233
|
+
children.get(candidate),
|
|
234
|
+
)
|
|
235
|
+
return (
|
|
236
|
+
values.every(
|
|
237
|
+
(value: string | undefined): boolean => value !== undefined,
|
|
238
|
+
) && new Set(values).size === values.length
|
|
239
|
+
)
|
|
240
|
+
},
|
|
241
|
+
)
|
|
242
|
+
return name === undefined
|
|
243
|
+
? []
|
|
244
|
+
: options.map((option: string, at: number): readonly [string, string] => [
|
|
245
|
+
option,
|
|
246
|
+
(found[at]?.get(name) ?? '').replaceAll(UNSAFE, '_'),
|
|
247
|
+
])
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** A key with every renamed option segment (`|n`) spelled by its literal (`|=boss`). */
|
|
251
|
+
function renamed(key: string, renames: ReadonlyMap<string, string>): string {
|
|
252
|
+
let out = ''
|
|
253
|
+
let from = NONE
|
|
254
|
+
for (const match of key.matchAll(SEGMENT)) {
|
|
255
|
+
const end = match.index + match[NONE].length
|
|
256
|
+
const literal = renames.get(key.slice(NONE, end))
|
|
257
|
+
if (
|
|
258
|
+
literal !== undefined &&
|
|
259
|
+
(end === key.length || MARKS.has(key.charAt(end)))
|
|
260
|
+
) {
|
|
261
|
+
out += `${key.slice(from, match.index)}|=${literal}`
|
|
262
|
+
from = end
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return `${out}${key.slice(from)}`
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Variant options keyed by their discriminating literal. */
|
|
269
|
+
function keyVariants(rows: ReadonlyMap<string, Row>): Map<string, Row> {
|
|
270
|
+
const groups = new Map<string, string[]>()
|
|
271
|
+
for (const key of rows.keys()) {
|
|
272
|
+
const group = OPTION.exec(key)?.groups?.['group']
|
|
273
|
+
if (group !== undefined) {
|
|
274
|
+
groups.set(group, [...(groups.get(group) ?? []), key])
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const literals = literalChildren(rows)
|
|
278
|
+
const renames = new Map(
|
|
279
|
+
[...groups.values()].flatMap(
|
|
280
|
+
(options: readonly string[]): readonly (readonly [string, string])[] =>
|
|
281
|
+
discriminants(literals, options),
|
|
282
|
+
),
|
|
283
|
+
)
|
|
284
|
+
return new Map(
|
|
285
|
+
[...rows].map(
|
|
286
|
+
([key, row]: readonly [string, Row]): readonly [string, Row] => [
|
|
287
|
+
renamed(key, renames),
|
|
288
|
+
row,
|
|
289
|
+
],
|
|
290
|
+
),
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** A lock's rows in compare form. */
|
|
295
|
+
function normalRows(raw: Readonly<Record<string, Json>>): Map<string, Row> {
|
|
296
|
+
return keyVariants(foldWrappers(expandRefs(raw)))
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export { MARKS, normalRows, WRAPPERS }
|
|
300
|
+
export type { DataRow, Row, SchemaRow }
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { MARKS } from './format-diff-normal.ts'
|
|
2
|
+
|
|
3
|
+
import type { Change } from './format-diff-data.ts'
|
|
4
|
+
import type { Row } from './format-diff-normal.ts'
|
|
5
|
+
|
|
6
|
+
// SHARD-COMPAT FE2: lock paths: their last segment, their subtree, and the
|
|
7
|
+
// `rename?` pairing of a removed and an added part with the same subtree.
|
|
8
|
+
|
|
9
|
+
type Sides = readonly [ReadonlyMap<string, Row>, ReadonlyMap<string, Row>]
|
|
10
|
+
|
|
11
|
+
const DEPRECATIONS = './deprecations#'
|
|
12
|
+
const CHAIN = './format-chain#'
|
|
13
|
+
const LAST_MARK = /[.[|{](?=[^.[|{]*$)/u
|
|
14
|
+
const NONE = 0
|
|
15
|
+
const NEXT = 1
|
|
16
|
+
|
|
17
|
+
/** A deprecation or chain step row: an id, never renamed. */
|
|
18
|
+
function isRegistry(key: string): boolean {
|
|
19
|
+
return key.startsWith(DEPRECATIONS) || key.startsWith(CHAIN)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The path above a key's last segment, and the mark that starts that segment ('' for an export). */
|
|
23
|
+
function lastSegment(key: string): readonly [string, string] {
|
|
24
|
+
const hash = key.indexOf('#')
|
|
25
|
+
const match = LAST_MARK.exec(key.slice(hash + NEXT))
|
|
26
|
+
if (match === null) {
|
|
27
|
+
return [key.slice(NONE, hash), '']
|
|
28
|
+
}
|
|
29
|
+
const at = hash + NEXT + match.index
|
|
30
|
+
return [key.slice(NONE, at), key.charAt(at)]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A part's subtree, relative to it, as one comparable string. */
|
|
34
|
+
function subtree(key: string, rows: ReadonlyMap<string, Row>): string {
|
|
35
|
+
const keys = [...rows.keys()]
|
|
36
|
+
.filter(
|
|
37
|
+
(other: string): boolean =>
|
|
38
|
+
other === key ||
|
|
39
|
+
(other.startsWith(key) && MARKS.has(other.charAt(key.length))),
|
|
40
|
+
)
|
|
41
|
+
.toSorted()
|
|
42
|
+
return JSON.stringify(
|
|
43
|
+
keys.map((other: string): readonly [string, Row | undefined] => [
|
|
44
|
+
other.slice(key.length),
|
|
45
|
+
rows.get(other),
|
|
46
|
+
]),
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function bySignature(
|
|
51
|
+
keys: readonly string[],
|
|
52
|
+
rows: ReadonlyMap<string, Row>,
|
|
53
|
+
): Map<string, string[]> {
|
|
54
|
+
const out = new Map<string, string[]>()
|
|
55
|
+
for (const key of keys.filter((name: string): boolean => !isRegistry(name))) {
|
|
56
|
+
const sign = subtree(key, rows)
|
|
57
|
+
out.set(sign, [...(out.get(sign) ?? []), key])
|
|
58
|
+
}
|
|
59
|
+
return out
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The candidate under the same parent as `key`, else the first. */
|
|
63
|
+
function closest(key: string, found: readonly string[]): string | undefined {
|
|
64
|
+
const [parent] = lastSegment(key)
|
|
65
|
+
return (
|
|
66
|
+
found.find(
|
|
67
|
+
(other: string): boolean => lastSegment(other)[NONE] === parent,
|
|
68
|
+
) ?? found.at(NONE)
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Removed and added parts with the same subtree, as `rename?` pairs (same parent first); the rest stay unpaired. */
|
|
73
|
+
function renames(
|
|
74
|
+
gone: readonly string[],
|
|
75
|
+
added: readonly string[],
|
|
76
|
+
sides: Sides,
|
|
77
|
+
): ReadonlyMap<string, string> {
|
|
78
|
+
const [before, after] = sides
|
|
79
|
+
const candidates = bySignature(added, after)
|
|
80
|
+
const pairs = new Map<string, string>()
|
|
81
|
+
for (const key of gone.filter((name: string): boolean => !isRegistry(name))) {
|
|
82
|
+
const found = candidates.get(subtree(key, before)) ?? []
|
|
83
|
+
const pick = closest(key, found)
|
|
84
|
+
if (pick !== undefined) {
|
|
85
|
+
pairs.set(key, pick)
|
|
86
|
+
found.splice(found.indexOf(pick), NEXT)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return pairs
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function renameChange(from: string, to: string): Change {
|
|
93
|
+
const same = lastSegment(from)[NONE] === lastSegment(to)[NONE]
|
|
94
|
+
return {
|
|
95
|
+
path: from,
|
|
96
|
+
kind: 'breaking',
|
|
97
|
+
what: `rename? ${from} -> ${to}`,
|
|
98
|
+
op: same ? 'rename' : 'moveSection',
|
|
99
|
+
rename: { from, to },
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export { CHAIN, DEPRECATIONS, lastSegment, renameChange, renames }
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process'
|
|
2
|
+
import { promisify } from 'node:util'
|
|
3
|
+
|
|
4
|
+
import { readLock } from './format-diff-lock.ts'
|
|
5
|
+
import { formatDiffReport } from './format-diff-report.ts'
|
|
6
|
+
import { formatDiff } from './format-diff.ts'
|
|
7
|
+
|
|
8
|
+
import type { LockData } from './format-diff-lock.ts'
|
|
9
|
+
import type { Report } from './format-diff.ts'
|
|
10
|
+
|
|
11
|
+
// SHARD-COMPAT FE6: range mode. `shard-format format-diff <a>..<b>` diffs
|
|
12
|
+
// each commit in the range that touched the format lock against the one
|
|
13
|
+
// before it, then the range's two ends: the net delta. A part added and
|
|
14
|
+
// removed inside the range shows in its steps and not in the net.
|
|
15
|
+
|
|
16
|
+
interface Locked {
|
|
17
|
+
readonly ref: string
|
|
18
|
+
readonly lock: LockData
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface RangeStep {
|
|
22
|
+
readonly from: string
|
|
23
|
+
readonly to: string
|
|
24
|
+
readonly report: Report
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface RangeReport {
|
|
28
|
+
readonly steps: readonly RangeStep[]
|
|
29
|
+
readonly net: Report
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// eslint-disable-next-line typescript/strict-void-return -- Node execFile returns a child while its callback supplies the result.
|
|
33
|
+
const run = promisify(execFile)
|
|
34
|
+
const RANGE = '..'
|
|
35
|
+
const LOCK_PATH = 'src/format/lock'
|
|
36
|
+
const SHORT = 9
|
|
37
|
+
const FIRST = 0
|
|
38
|
+
const NEXT = 1
|
|
39
|
+
|
|
40
|
+
/** Each consecutive pair's diff, and the ends' diff. */
|
|
41
|
+
function rangeDiff(locks: readonly Readonly<Locked>[]): RangeReport {
|
|
42
|
+
const first = locks.at(FIRST)
|
|
43
|
+
const last = locks.at(-NEXT)
|
|
44
|
+
if (first === undefined || last === undefined) {
|
|
45
|
+
throw new Error('a range needs at least one lock')
|
|
46
|
+
}
|
|
47
|
+
const steps = locks
|
|
48
|
+
.slice(NEXT)
|
|
49
|
+
.map((to: Readonly<Locked>, at: number): RangeStep => ({
|
|
50
|
+
from: locks[at]?.ref ?? first.ref,
|
|
51
|
+
to: to.ref,
|
|
52
|
+
report: formatDiff(locks[at]?.lock ?? first.lock, to.lock),
|
|
53
|
+
}))
|
|
54
|
+
return { steps, net: formatDiff(first.lock, last.lock) }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** `<a>..<b>`'s ends and every commit between that touched the lock, oldest first. */
|
|
58
|
+
async function rangeRefs(
|
|
59
|
+
range: string,
|
|
60
|
+
cwd: string,
|
|
61
|
+
): Promise<readonly string[]> {
|
|
62
|
+
const [from = '', to = ''] = range.split(RANGE)
|
|
63
|
+
const { stdout } = await run(
|
|
64
|
+
'git',
|
|
65
|
+
['rev-list', '--reverse', `${from}..${to}`, '--', LOCK_PATH],
|
|
66
|
+
{ cwd, encoding: 'utf8' },
|
|
67
|
+
)
|
|
68
|
+
const inside = stdout
|
|
69
|
+
.split('\n')
|
|
70
|
+
.filter((line: string): boolean => line !== '')
|
|
71
|
+
const parsed = await run('git', ['rev-parse', to], { cwd, encoding: 'utf8' })
|
|
72
|
+
const tip = parsed.stdout.trim()
|
|
73
|
+
return [from, ...inside.filter((sha: string): boolean => sha !== tip), to]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Lock every ref of a range and diff it. */
|
|
77
|
+
async function readRange(range: string, cwd: string): Promise<RangeReport> {
|
|
78
|
+
const refs = await rangeRefs(range, cwd)
|
|
79
|
+
const locks: Locked[] = []
|
|
80
|
+
for (const ref of refs) {
|
|
81
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- each ref unpacks into the same scratch folder
|
|
82
|
+
locks.push({ ref, lock: await readLock(ref, cwd) })
|
|
83
|
+
}
|
|
84
|
+
return rangeDiff(locks)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function isRange(operand: string): boolean {
|
|
88
|
+
return operand.includes(RANGE)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const short = (ref: string): string =>
|
|
92
|
+
/^[\da-f]{40}$/u.test(ref) ? ref.slice(FIRST, SHORT) : ref
|
|
93
|
+
|
|
94
|
+
/** Each step's report under its commit pair, then the net delta. */
|
|
95
|
+
function rangeReport(report: Readonly<RangeReport>): string {
|
|
96
|
+
return [
|
|
97
|
+
...report.steps.map(
|
|
98
|
+
(step: Readonly<RangeStep>): string =>
|
|
99
|
+
`${short(step.from)}..${short(step.to)}\n${formatDiffReport(step.report)}`,
|
|
100
|
+
),
|
|
101
|
+
`net\n${formatDiffReport(report.net)}`,
|
|
102
|
+
].join('\n\n')
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** `format-diff <a>..<b>`: the range's report, as text or JSON. */
|
|
106
|
+
async function rangeOutput(range: string, json: boolean): Promise<string> {
|
|
107
|
+
const report = await readRange(range, process.cwd())
|
|
108
|
+
return json ? JSON.stringify(report, undefined, NEXT) : rangeReport(report)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export { isRange, rangeDiff, rangeOutput, rangeReport, readRange }
|
|
112
|
+
export type { Locked, RangeReport, RangeStep }
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { KINDS } from './format-diff.ts'
|
|
2
|
+
|
|
3
|
+
import type { Change, ChangeKind } from './format-diff-data.ts'
|
|
4
|
+
import type { Report } from './format-diff.ts'
|
|
5
|
+
|
|
6
|
+
// SHARD-COMPAT FE2: format-diff's report as text, one change per line.
|
|
7
|
+
|
|
8
|
+
const NONE = 0
|
|
9
|
+
const SHORT_ID = 12
|
|
10
|
+
const KIND_WIDTH = 11
|
|
11
|
+
|
|
12
|
+
/** The report as text, one change per line. */
|
|
13
|
+
function formatDiffReport(report: Readonly<Report>): string {
|
|
14
|
+
const counts = KINDS.map(
|
|
15
|
+
(kind: ChangeKind): string =>
|
|
16
|
+
`${String(report.changes.filter((found: Readonly<Change>): boolean => found.kind === kind).length)} ${kind}`,
|
|
17
|
+
)
|
|
18
|
+
const chain =
|
|
19
|
+
report.chain === undefined
|
|
20
|
+
? 'chain id unchanged'
|
|
21
|
+
: `chain id ${report.chain.before.slice(NONE, SHORT_ID)} -> ${report.chain.after.slice(NONE, SHORT_ID)}`
|
|
22
|
+
const lines = report.changes.map(
|
|
23
|
+
(found: Readonly<Change>): string =>
|
|
24
|
+
`${found.kind.padEnd(KIND_WIDTH)} ${found.path} ${found.what}${found.op === undefined ? '' : ` (op: ${found.op})`}`,
|
|
25
|
+
)
|
|
26
|
+
const head = `format-diff: ${String(report.changes.length)} changes (${counts.join(', ')}); ${chain}; worst ${report.worst}`
|
|
27
|
+
return [head, ...lines].join('\n')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { formatDiffReport }
|