@firstdraft.com/cli 0.2.1 → 0.2.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/docs/commands.md +104 -7
- package/docs/errors.md +17 -3
- package/docs/release-history.md +8 -0
- package/package.json +1 -1
- package/src/cli.js +20 -10
- package/src/commands/compilation.js +136 -109
- package/src/commands/plan-compile.js +46 -41
- package/src/compilation-artifact.js +116 -14
- package/src/root-output.js +1485 -0
|
@@ -0,0 +1,1485 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import {
|
|
4
|
+
accessSync,
|
|
5
|
+
chmodSync,
|
|
6
|
+
closeSync,
|
|
7
|
+
constants,
|
|
8
|
+
fsyncSync,
|
|
9
|
+
lstatSync,
|
|
10
|
+
mkdirSync,
|
|
11
|
+
openSync,
|
|
12
|
+
readFileSync,
|
|
13
|
+
readdirSync,
|
|
14
|
+
realpathSync,
|
|
15
|
+
renameSync,
|
|
16
|
+
rmdirSync,
|
|
17
|
+
rmSync,
|
|
18
|
+
unlinkSync,
|
|
19
|
+
writeFileSync,
|
|
20
|
+
} from "node:fs";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
|
|
23
|
+
import { isFileSystemError } from "./file-system.js";
|
|
24
|
+
|
|
25
|
+
export const ROOT_TRANSACTION_NAME = ".firstdraft-root-output";
|
|
26
|
+
export const ROOT_DESIGN_NAME = "design";
|
|
27
|
+
|
|
28
|
+
const JOURNAL_FORMAT = "firstdraft.root-output-transaction/1";
|
|
29
|
+
const JOURNAL_NAME = "journal.json";
|
|
30
|
+
const JOURNAL_TEMPORARY_NAME = "journal.tmp";
|
|
31
|
+
const PREVIEW_NAME = "ignore-preview";
|
|
32
|
+
const ARTIFACT_STAGE_NAME = "artifact";
|
|
33
|
+
const PREPARED_INDEX_NAME = "prepared.index";
|
|
34
|
+
const ORIGINAL_INDEX_NAME = "original.index";
|
|
35
|
+
const DIRECTORY_MODE = 0o755;
|
|
36
|
+
const PRIVATE_DIRECTORY_MODE = 0o700;
|
|
37
|
+
const PRIVATE_FILE_MODE = 0o600;
|
|
38
|
+
const MAX_GIT_OUTPUT_BYTES = 128 * 1024 * 1024;
|
|
39
|
+
const GIT_HASH_PATTERN = /^[0-9a-f]{40,64}$/;
|
|
40
|
+
const RESERVED_NAMES = new Set([
|
|
41
|
+
ROOT_DESIGN_NAME.toLowerCase(),
|
|
42
|
+
ROOT_TRANSACTION_NAME.toLowerCase(),
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
export class RootOutputPathError extends Error {
|
|
46
|
+
/** @param {string} message @param {string} reason @param {{cause?: unknown}} [options] */
|
|
47
|
+
constructor(message, reason, options = {}) {
|
|
48
|
+
super(message, options);
|
|
49
|
+
this.reason = reason;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class RootOutputMaterializationError extends Error {
|
|
54
|
+
/** @param {string} message @param {string} reason @param {{cause?: unknown, recoveryPath?: string}} [options] */
|
|
55
|
+
constructor(message, reason, options = {}) {
|
|
56
|
+
super(message, options);
|
|
57
|
+
this.reason = reason;
|
|
58
|
+
this.recoveryPath = options.recoveryPath;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @typedef {object} RootEntryIdentity
|
|
64
|
+
* @property {string} name
|
|
65
|
+
* @property {"file" | "directory"} kind
|
|
66
|
+
* @property {string} device
|
|
67
|
+
* @property {string} inode
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @typedef {object} GitIndexEntry
|
|
72
|
+
* @property {string} mode
|
|
73
|
+
* @property {string} object
|
|
74
|
+
* @property {string} path
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @typedef {object} RootGitContext
|
|
79
|
+
* @property {string} gitDirectory
|
|
80
|
+
* @property {string} indexPath
|
|
81
|
+
* @property {string} indexLockPath
|
|
82
|
+
* @property {GitIndexEntry[]} indexEntries
|
|
83
|
+
* @property {string[]} ignoredPaths
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @typedef {object} RootOutputTarget
|
|
88
|
+
* @property {"root"} kind
|
|
89
|
+
* @property {string} path
|
|
90
|
+
* @property {string} transactionPath
|
|
91
|
+
* @property {string} transactionDevice
|
|
92
|
+
* @property {string} transactionInode
|
|
93
|
+
* @property {RootEntryIdentity[]} snapshot
|
|
94
|
+
* @property {RootGitContext | null} git
|
|
95
|
+
* @property {Record<string, unknown>} journal
|
|
96
|
+
* @property {boolean} irreversible
|
|
97
|
+
* @property {boolean} released
|
|
98
|
+
* @property {Map<NodeJS.Signals, () => void>} signalHandlers
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Return the physical current directory when output selects root adoption.
|
|
103
|
+
* Other existing or absent outputs return null for the ordinary resolver.
|
|
104
|
+
*
|
|
105
|
+
* @param {object} options
|
|
106
|
+
* @param {string} options.cwd
|
|
107
|
+
* @param {string} options.output
|
|
108
|
+
* @returns {string | null}
|
|
109
|
+
*/
|
|
110
|
+
export function resolveRootOutputPath({ cwd, output }) {
|
|
111
|
+
let physicalCwd;
|
|
112
|
+
try {
|
|
113
|
+
physicalCwd = realpathSync(cwd);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (!isFileSystemError(error)) throw error;
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const candidate = path.resolve(cwd, output);
|
|
120
|
+
let physicalCandidate;
|
|
121
|
+
try {
|
|
122
|
+
physicalCandidate = realpathSync(candidate);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (!isFileSystemError(error)) throw error;
|
|
125
|
+
if (error.code === "ENOENT") return null;
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return physicalCandidate === physicalCwd ? physicalCwd : null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Validate and exclusively reserve one root adoption before any request.
|
|
134
|
+
*
|
|
135
|
+
* @param {object} options
|
|
136
|
+
* @param {string} options.root
|
|
137
|
+
* @param {NodeJS.Platform} [options.platform]
|
|
138
|
+
* @returns {RootOutputTarget}
|
|
139
|
+
*/
|
|
140
|
+
export function prepareRootOutput({ root, platform = process.platform }) {
|
|
141
|
+
if (platform === "win32") {
|
|
142
|
+
throw new RootOutputPathError(
|
|
143
|
+
"Root output is not supported on this platform.",
|
|
144
|
+
"root_platform_unsupported",
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
try {
|
|
149
|
+
root = realpathSync(root);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
if (!isFileSystemError(error)) throw error;
|
|
152
|
+
throw new RootOutputPathError(
|
|
153
|
+
"The root output directory could not be resolved.",
|
|
154
|
+
"root_not_real",
|
|
155
|
+
{ cause: error },
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let rootStat;
|
|
160
|
+
try {
|
|
161
|
+
rootStat = lstatSync(root, { bigint: true });
|
|
162
|
+
} catch (error) {
|
|
163
|
+
if (!isFileSystemError(error)) throw error;
|
|
164
|
+
throw new RootOutputPathError(
|
|
165
|
+
"The root output directory could not be read.",
|
|
166
|
+
"root_not_real",
|
|
167
|
+
{ cause: error },
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
if (
|
|
171
|
+
!rootStat.isDirectory() ||
|
|
172
|
+
rootStat.isSymbolicLink() ||
|
|
173
|
+
root === path.dirname(root)
|
|
174
|
+
) {
|
|
175
|
+
throw new RootOutputPathError(
|
|
176
|
+
"The root output directory is not a real non-root directory.",
|
|
177
|
+
"root_not_real",
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
accessSync(root, constants.R_OK | constants.W_OK | constants.X_OK);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (!isFileSystemError(error)) throw error;
|
|
185
|
+
throw new RootOutputPathError(
|
|
186
|
+
"The root output directory is not writable.",
|
|
187
|
+
"root_not_writable",
|
|
188
|
+
{ cause: error },
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
assertReservedPathsAvailable(root);
|
|
193
|
+
const transactionPath = path.join(root, ROOT_TRANSACTION_NAME);
|
|
194
|
+
try {
|
|
195
|
+
mkdirSync(transactionPath, { mode: PRIVATE_DIRECTORY_MODE });
|
|
196
|
+
chmodSync(transactionPath, PRIVATE_DIRECTORY_MODE);
|
|
197
|
+
} catch (error) {
|
|
198
|
+
if (!isFileSystemError(error)) throw error;
|
|
199
|
+
if (error.code === "EEXIST") throw existingTransactionError(root);
|
|
200
|
+
throw new RootOutputPathError(
|
|
201
|
+
"The root output lock could not be created.",
|
|
202
|
+
"root_not_writable",
|
|
203
|
+
{ cause: error },
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const transactionStat = lstatSync(transactionPath, { bigint: true });
|
|
208
|
+
/** @type {RootOutputTarget} */
|
|
209
|
+
const target = {
|
|
210
|
+
kind: "root",
|
|
211
|
+
path: root,
|
|
212
|
+
transactionPath,
|
|
213
|
+
transactionDevice: String(transactionStat.dev),
|
|
214
|
+
transactionInode: String(transactionStat.ino),
|
|
215
|
+
snapshot: [],
|
|
216
|
+
git: null,
|
|
217
|
+
journal: {},
|
|
218
|
+
irreversible: false,
|
|
219
|
+
released: false,
|
|
220
|
+
signalHandlers: new Map(),
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
try {
|
|
224
|
+
target.snapshot = captureRootSnapshot(root, rootStat.dev);
|
|
225
|
+
target.git = inspectGitRoot(root, transactionPath);
|
|
226
|
+
target.journal = initialJournal(target);
|
|
227
|
+
writeJournal(target);
|
|
228
|
+
if (target.git !== null) assertIgnoreProtection(target);
|
|
229
|
+
installSignalHandlers(target);
|
|
230
|
+
return target;
|
|
231
|
+
} catch (error) {
|
|
232
|
+
releaseRootOutput(target);
|
|
233
|
+
if (error instanceof RootOutputPathError) throw error;
|
|
234
|
+
if (isFileSystemError(error)) {
|
|
235
|
+
throw new RootOutputPathError(
|
|
236
|
+
"The root output preflight could not be completed.",
|
|
237
|
+
"root_not_writable",
|
|
238
|
+
{ cause: error },
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** @param {RootOutputTarget} target */
|
|
246
|
+
export function releaseRootOutput(target) {
|
|
247
|
+
removeSignalHandlers(target);
|
|
248
|
+
if (target.released || target.irreversible) return;
|
|
249
|
+
try {
|
|
250
|
+
removePreparedTransaction(target);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (isFileSystemError(error) && error.code === "ENOENT") {
|
|
253
|
+
target.released = true;
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (
|
|
257
|
+
error instanceof RootOutputMaterializationError ||
|
|
258
|
+
isFileSystemError(error)
|
|
259
|
+
) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @param {RootOutputTarget} target
|
|
268
|
+
* @param {object} artifact
|
|
269
|
+
* @param {Array<{path: string, mode: 420 | 493}>} artifact.files
|
|
270
|
+
* @param {string} artifact.manifest_sha256
|
|
271
|
+
* @param {object} callbacks
|
|
272
|
+
* @param {(root: string) => void} callbacks.writeArtifact
|
|
273
|
+
* @param {(root: string, ignoredRootEntries?: Set<string>) => void} callbacks.verifyArtifact
|
|
274
|
+
* @param {(from: string, to: string) => void} [callbacks.rename]
|
|
275
|
+
*/
|
|
276
|
+
export function materializeRootOutput(
|
|
277
|
+
target,
|
|
278
|
+
artifact,
|
|
279
|
+
{ writeArtifact, verifyArtifact, rename = renameSync },
|
|
280
|
+
) {
|
|
281
|
+
assertOwnedTransaction(target);
|
|
282
|
+
const artifactStage = path.join(target.transactionPath, ARTIFACT_STAGE_NAME);
|
|
283
|
+
|
|
284
|
+
try {
|
|
285
|
+
assertArtifactRootNames(artifact.files);
|
|
286
|
+
mkdirSync(artifactStage, { mode: DIRECTORY_MODE });
|
|
287
|
+
chmodSync(artifactStage, DIRECTORY_MODE);
|
|
288
|
+
writeArtifact(artifactStage);
|
|
289
|
+
verifyArtifact(artifactStage);
|
|
290
|
+
|
|
291
|
+
if (target.git !== null) prepareGitIndex(target, artifact.files);
|
|
292
|
+
recheckRootSnapshot(target);
|
|
293
|
+
performRootTransaction(target, artifact.files, rename);
|
|
294
|
+
verifyRootResult(target, artifact.files, verifyArtifact);
|
|
295
|
+
|
|
296
|
+
target.journal.phase = "complete";
|
|
297
|
+
target.journal.pending = null;
|
|
298
|
+
writeJournal(target);
|
|
299
|
+
removeSignalHandlers(target);
|
|
300
|
+
removeCompletedTransaction(target);
|
|
301
|
+
|
|
302
|
+
const movedEntries = target.snapshot.filter(
|
|
303
|
+
(entry) => entry.name !== ".git",
|
|
304
|
+
);
|
|
305
|
+
return {
|
|
306
|
+
path: target.path,
|
|
307
|
+
file_count: artifact.files.length,
|
|
308
|
+
manifest_sha256: artifact.manifest_sha256,
|
|
309
|
+
root_adoption: {
|
|
310
|
+
design_path:
|
|
311
|
+
movedEntries.length === 0
|
|
312
|
+
? null
|
|
313
|
+
: path.join(target.path, ROOT_DESIGN_NAME),
|
|
314
|
+
moved_entry_count: movedEntries.length,
|
|
315
|
+
git_repository_preserved: target.git !== null,
|
|
316
|
+
git_index_replaced: target.git !== null,
|
|
317
|
+
},
|
|
318
|
+
};
|
|
319
|
+
} catch (error) {
|
|
320
|
+
const materializationError = normalizeMaterializationError(error);
|
|
321
|
+
const rollbackComplete = rollbackRootTransaction(target, rename);
|
|
322
|
+
if (!rollbackComplete) {
|
|
323
|
+
target.irreversible = true;
|
|
324
|
+
target.journal.phase = "rollback_incomplete";
|
|
325
|
+
writeJournalBestEffort(target);
|
|
326
|
+
removeSignalHandlers(target);
|
|
327
|
+
throw new RootOutputMaterializationError(
|
|
328
|
+
"The root output transaction could not be rolled back.",
|
|
329
|
+
"root_rollback_incomplete",
|
|
330
|
+
{
|
|
331
|
+
cause: materializationError,
|
|
332
|
+
recoveryPath: ROOT_TRANSACTION_NAME,
|
|
333
|
+
},
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
removePreparedTransaction(target);
|
|
338
|
+
throw materializationError;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** @param {string} root */
|
|
343
|
+
function assertReservedPathsAvailable(root) {
|
|
344
|
+
const names = readdirSync(root);
|
|
345
|
+
for (const name of names) {
|
|
346
|
+
const folded = name.toLowerCase();
|
|
347
|
+
if (folded === ROOT_DESIGN_NAME.toLowerCase()) {
|
|
348
|
+
throw new RootOutputPathError(
|
|
349
|
+
"The root output design path is reserved.",
|
|
350
|
+
"root_reserved_path",
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
if (folded !== ROOT_TRANSACTION_NAME.toLowerCase()) continue;
|
|
354
|
+
if (name === ROOT_TRANSACTION_NAME) throw existingTransactionError(root);
|
|
355
|
+
throw new RootOutputPathError(
|
|
356
|
+
"The root output transaction path is reserved.",
|
|
357
|
+
"root_reserved_path",
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** @param {string} root */
|
|
363
|
+
function existingTransactionError(root) {
|
|
364
|
+
const journalPath = path.join(root, ROOT_TRANSACTION_NAME, JOURNAL_NAME);
|
|
365
|
+
try {
|
|
366
|
+
const journal = JSON.parse(readFileSync(journalPath, "utf8"));
|
|
367
|
+
if (journal?.format === JOURNAL_FORMAT && journal?.root === root) {
|
|
368
|
+
return new RootOutputPathError(
|
|
369
|
+
"Another root output transaction is active or retained.",
|
|
370
|
+
"root_busy",
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
} catch (error) {
|
|
374
|
+
if (!(error instanceof SyntaxError || isFileSystemError(error)))
|
|
375
|
+
throw error;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return new RootOutputPathError(
|
|
379
|
+
"The root output transaction path is reserved.",
|
|
380
|
+
"root_reserved_path",
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** @param {string} root @param {bigint} rootDevice */
|
|
385
|
+
function captureRootSnapshot(root, rootDevice) {
|
|
386
|
+
/** @type {RootEntryIdentity[]} */
|
|
387
|
+
const snapshot = [];
|
|
388
|
+
const names = readdirSync(root).sort(compareStrings);
|
|
389
|
+
for (const name of names) {
|
|
390
|
+
if (name === ROOT_TRANSACTION_NAME) continue;
|
|
391
|
+
if (RESERVED_NAMES.has(name.toLowerCase())) {
|
|
392
|
+
throw new RootOutputPathError(
|
|
393
|
+
"A reserved root output path appeared during preflight.",
|
|
394
|
+
"root_reserved_path",
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
const entryPath = path.join(root, name);
|
|
398
|
+
const stat = lstatSync(entryPath, { bigint: true });
|
|
399
|
+
const kind = stat.isFile()
|
|
400
|
+
? "file"
|
|
401
|
+
: stat.isDirectory()
|
|
402
|
+
? "directory"
|
|
403
|
+
: null;
|
|
404
|
+
if (kind === null || stat.isSymbolicLink() || stat.dev !== rootDevice) {
|
|
405
|
+
throw new RootOutputPathError(
|
|
406
|
+
"A root output entry is unsupported.",
|
|
407
|
+
"root_entry_unsupported",
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
snapshot.push({
|
|
411
|
+
name,
|
|
412
|
+
kind,
|
|
413
|
+
device: String(stat.dev),
|
|
414
|
+
inode: String(stat.ino),
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
return snapshot;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** @param {RootOutputTarget} target */
|
|
421
|
+
function recheckRootSnapshot(target) {
|
|
422
|
+
const rootStat = lstatSync(target.path, { bigint: true });
|
|
423
|
+
const current = captureRootSnapshot(target.path, rootStat.dev);
|
|
424
|
+
if (JSON.stringify(current) !== JSON.stringify(target.snapshot)) {
|
|
425
|
+
throw new RootOutputMaterializationError(
|
|
426
|
+
"The root output changed while Compilation was running.",
|
|
427
|
+
"output_changed",
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** @param {RootOutputTarget} target */
|
|
433
|
+
function initialJournal(target) {
|
|
434
|
+
return {
|
|
435
|
+
format: JOURNAL_FORMAT,
|
|
436
|
+
root: target.path,
|
|
437
|
+
phase: "prepared",
|
|
438
|
+
irreversible: false,
|
|
439
|
+
snapshot: target.snapshot,
|
|
440
|
+
pending: null,
|
|
441
|
+
design_created: false,
|
|
442
|
+
design_moves: [],
|
|
443
|
+
artifact_moves: [],
|
|
444
|
+
index: null,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** @param {RootOutputTarget} target */
|
|
449
|
+
function writeJournal(target) {
|
|
450
|
+
assertOwnedTransaction(target);
|
|
451
|
+
const temporary = path.join(target.transactionPath, JOURNAL_TEMPORARY_NAME);
|
|
452
|
+
const destination = path.join(target.transactionPath, JOURNAL_NAME);
|
|
453
|
+
const source = Buffer.from(`${JSON.stringify(target.journal, null, 2)}\n`);
|
|
454
|
+
let descriptor = null;
|
|
455
|
+
try {
|
|
456
|
+
descriptor = openSync(temporary, "wx", PRIVATE_FILE_MODE);
|
|
457
|
+
writeFileSync(descriptor, source);
|
|
458
|
+
fsyncSync(descriptor);
|
|
459
|
+
closeSync(descriptor);
|
|
460
|
+
descriptor = null;
|
|
461
|
+
renameSync(temporary, destination);
|
|
462
|
+
fsyncDirectory(target.transactionPath);
|
|
463
|
+
} finally {
|
|
464
|
+
if (descriptor !== null) closeSync(descriptor);
|
|
465
|
+
removePathIfPresent(temporary);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** @param {RootOutputTarget} target */
|
|
470
|
+
function writeJournalBestEffort(target) {
|
|
471
|
+
try {
|
|
472
|
+
writeJournal(target);
|
|
473
|
+
} catch (error) {
|
|
474
|
+
if (!isFileSystemError(error)) throw error;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/** @param {string} directory */
|
|
479
|
+
function fsyncDirectory(directory) {
|
|
480
|
+
let descriptor = null;
|
|
481
|
+
try {
|
|
482
|
+
descriptor = openSync(directory, constants.O_RDONLY);
|
|
483
|
+
fsyncSync(descriptor);
|
|
484
|
+
} finally {
|
|
485
|
+
if (descriptor !== null) closeSync(descriptor);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** @param {RootOutputTarget} target */
|
|
490
|
+
function installSignalHandlers(target) {
|
|
491
|
+
for (const signal of /** @type {NodeJS.Signals[]} */ ([
|
|
492
|
+
"SIGINT",
|
|
493
|
+
"SIGTERM",
|
|
494
|
+
])) {
|
|
495
|
+
const handler = () => {
|
|
496
|
+
try {
|
|
497
|
+
if (!target.irreversible) releaseRootOutput(target);
|
|
498
|
+
} finally {
|
|
499
|
+
process.removeListener(signal, handler);
|
|
500
|
+
process.kill(process.pid, signal);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
target.signalHandlers.set(signal, handler);
|
|
504
|
+
process.once(signal, handler);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** @param {RootOutputTarget} target */
|
|
509
|
+
function removeSignalHandlers(target) {
|
|
510
|
+
for (const [signal, handler] of target.signalHandlers) {
|
|
511
|
+
process.removeListener(signal, handler);
|
|
512
|
+
}
|
|
513
|
+
target.signalHandlers.clear();
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** @param {RootOutputTarget} target */
|
|
517
|
+
function assertOwnedTransaction(target) {
|
|
518
|
+
const stat = lstatSync(target.transactionPath, { bigint: true });
|
|
519
|
+
if (
|
|
520
|
+
!stat.isDirectory() ||
|
|
521
|
+
stat.isSymbolicLink() ||
|
|
522
|
+
String(stat.dev) !== target.transactionDevice ||
|
|
523
|
+
String(stat.ino) !== target.transactionInode
|
|
524
|
+
) {
|
|
525
|
+
throw new RootOutputMaterializationError(
|
|
526
|
+
"The owned root output transaction changed.",
|
|
527
|
+
"root_transaction_failed",
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** @param {RootOutputTarget} target */
|
|
533
|
+
function removePreparedTransaction(target) {
|
|
534
|
+
removeSignalHandlers(target);
|
|
535
|
+
if (target.released) return;
|
|
536
|
+
assertOwnedTransaction(target);
|
|
537
|
+
rmSync(target.transactionPath, { recursive: true, force: true });
|
|
538
|
+
target.released = true;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** @param {RootOutputTarget} target */
|
|
542
|
+
function removeCompletedTransaction(target) {
|
|
543
|
+
assertOwnedTransaction(target);
|
|
544
|
+
rmSync(target.transactionPath, { recursive: true, force: true });
|
|
545
|
+
target.released = true;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/** @param {Array<{path: string}>} files */
|
|
549
|
+
function assertArtifactRootNames(files) {
|
|
550
|
+
for (const file of files) {
|
|
551
|
+
const top = file.path.split("/", 1)[0] ?? "";
|
|
552
|
+
if (RESERVED_NAMES.has(top.toLowerCase())) {
|
|
553
|
+
throw new RootOutputMaterializationError(
|
|
554
|
+
"The compilation artifact owns a reserved root path.",
|
|
555
|
+
"root_artifact_collision",
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* @param {RootOutputTarget} target
|
|
563
|
+
* @param {Array<{path: string, mode: 420 | 493}>} files
|
|
564
|
+
* @param {(from: string, to: string) => void} rename
|
|
565
|
+
*/
|
|
566
|
+
function performRootTransaction(target, files, rename) {
|
|
567
|
+
const designEntries = target.snapshot.filter(
|
|
568
|
+
(entry) => entry.name !== ".git",
|
|
569
|
+
);
|
|
570
|
+
const designPath = path.join(target.path, ROOT_DESIGN_NAME);
|
|
571
|
+
const artifactStage = path.join(target.transactionPath, ARTIFACT_STAGE_NAME);
|
|
572
|
+
const artifactTopNames = [
|
|
573
|
+
...new Set(files.map((file) => file.path.split("/", 1)[0] ?? "")),
|
|
574
|
+
].sort(compareStrings);
|
|
575
|
+
|
|
576
|
+
if (designEntries.length > 0) {
|
|
577
|
+
startIrreversibleStep(target, {
|
|
578
|
+
kind: "create_design",
|
|
579
|
+
path: designPath,
|
|
580
|
+
});
|
|
581
|
+
mkdirSync(designPath, { mode: DIRECTORY_MODE });
|
|
582
|
+
target.journal.design_created = true;
|
|
583
|
+
chmodSync(designPath, DIRECTORY_MODE);
|
|
584
|
+
finishIrreversibleStep(target, "moving_design");
|
|
585
|
+
|
|
586
|
+
for (const entry of designEntries) {
|
|
587
|
+
const source = path.join(target.path, entry.name);
|
|
588
|
+
const destination = path.join(designPath, entry.name);
|
|
589
|
+
startIrreversibleStep(target, {
|
|
590
|
+
kind: "move_design_entry",
|
|
591
|
+
source,
|
|
592
|
+
destination,
|
|
593
|
+
});
|
|
594
|
+
rename(source, destination);
|
|
595
|
+
/** @type {Array<Record<string, string>>} */ (
|
|
596
|
+
target.journal.design_moves
|
|
597
|
+
).push({ source, destination });
|
|
598
|
+
finishIrreversibleStep(target, "moving_design");
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
for (const name of artifactTopNames) {
|
|
603
|
+
const source = path.join(artifactStage, name);
|
|
604
|
+
const destination = path.join(target.path, name);
|
|
605
|
+
if (!isAbsent(destination)) {
|
|
606
|
+
throw new RootOutputMaterializationError(
|
|
607
|
+
"An artifact root path appeared during materialization.",
|
|
608
|
+
"root_transaction_failed",
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
startIrreversibleStep(target, {
|
|
612
|
+
kind: "install_artifact_entry",
|
|
613
|
+
source,
|
|
614
|
+
destination,
|
|
615
|
+
});
|
|
616
|
+
rename(source, destination);
|
|
617
|
+
/** @type {Array<Record<string, string>>} */ (
|
|
618
|
+
target.journal.artifact_moves
|
|
619
|
+
).push({ source, destination });
|
|
620
|
+
finishIrreversibleStep(target, "installing_artifact");
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
if (target.git !== null) {
|
|
624
|
+
refreshPreparedIndex(target);
|
|
625
|
+
installPreparedIndex(target);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** @param {RootOutputTarget} target @param {Record<string, string>} pending */
|
|
630
|
+
function startIrreversibleStep(target, pending) {
|
|
631
|
+
target.irreversible = true;
|
|
632
|
+
target.journal.irreversible = true;
|
|
633
|
+
target.journal.pending = pending;
|
|
634
|
+
writeJournal(target);
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/** @param {RootOutputTarget} target @param {string} phase */
|
|
638
|
+
function finishIrreversibleStep(target, phase) {
|
|
639
|
+
target.journal.phase = phase;
|
|
640
|
+
target.journal.pending = null;
|
|
641
|
+
writeJournal(target);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* @param {RootOutputTarget} target
|
|
646
|
+
* @param {Array<{path: string}>} files
|
|
647
|
+
* @param {(root: string, ignoredRootEntries?: Set<string>) => void} verifyArtifact
|
|
648
|
+
*/
|
|
649
|
+
function verifyRootResult(target, files, verifyArtifact) {
|
|
650
|
+
const ignored = new Set([ROOT_TRANSACTION_NAME]);
|
|
651
|
+
if (target.snapshot.some((entry) => entry.name === ".git")) {
|
|
652
|
+
ignored.add(".git");
|
|
653
|
+
}
|
|
654
|
+
if (target.snapshot.some((entry) => entry.name !== ".git")) {
|
|
655
|
+
ignored.add(ROOT_DESIGN_NAME);
|
|
656
|
+
}
|
|
657
|
+
verifyArtifact(target.path, ignored);
|
|
658
|
+
|
|
659
|
+
const designPath = path.join(target.path, ROOT_DESIGN_NAME);
|
|
660
|
+
for (const identity of target.snapshot) {
|
|
661
|
+
const currentPath =
|
|
662
|
+
identity.name === ".git"
|
|
663
|
+
? path.join(target.path, identity.name)
|
|
664
|
+
: path.join(designPath, identity.name);
|
|
665
|
+
if (!sameIdentity(currentPath, identity)) {
|
|
666
|
+
throw new RootOutputMaterializationError(
|
|
667
|
+
"A preserved root entry changed during materialization.",
|
|
668
|
+
"root_transaction_failed",
|
|
669
|
+
);
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
if (target.git !== null) {
|
|
674
|
+
assertInstalledIndex(target);
|
|
675
|
+
assertMovedIgnoreProtection(target);
|
|
676
|
+
const worktree = invokeGit(target.path, [
|
|
677
|
+
"diff-files",
|
|
678
|
+
"--quiet",
|
|
679
|
+
"--ignore-submodules=none",
|
|
680
|
+
"--",
|
|
681
|
+
]);
|
|
682
|
+
if (worktree.status !== 0) {
|
|
683
|
+
const detail = invokeGit(target.path, [
|
|
684
|
+
"diff-files",
|
|
685
|
+
"--name-status",
|
|
686
|
+
"--",
|
|
687
|
+
]);
|
|
688
|
+
throw new RootOutputMaterializationError(
|
|
689
|
+
"The prepared Git index does not match the adopted worktree.",
|
|
690
|
+
"root_transaction_failed",
|
|
691
|
+
{
|
|
692
|
+
cause: new Error(detail.stdout.toString("utf8")),
|
|
693
|
+
},
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
const expectedTopNames = new Set(
|
|
699
|
+
files.map((file) => file.path.split("/", 1)[0] ?? ""),
|
|
700
|
+
);
|
|
701
|
+
for (const name of ignored) expectedTopNames.add(name);
|
|
702
|
+
const actualTopNames = new Set(readdirSync(target.path));
|
|
703
|
+
if (!setsEqual(expectedTopNames, actualTopNames)) {
|
|
704
|
+
throw new RootOutputMaterializationError(
|
|
705
|
+
"The adopted root contains an unexpected entry.",
|
|
706
|
+
"root_transaction_failed",
|
|
707
|
+
);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/** @param {RootOutputTarget} target @param {(from: string, to: string) => void} rename */
|
|
712
|
+
function rollbackRootTransaction(target, rename) {
|
|
713
|
+
let complete = true;
|
|
714
|
+
|
|
715
|
+
if (!rollbackIndex(target)) complete = false;
|
|
716
|
+
|
|
717
|
+
const artifactMoves =
|
|
718
|
+
/** @type {Array<{source: string, destination: string}>} */ (
|
|
719
|
+
target.journal.artifact_moves ?? []
|
|
720
|
+
);
|
|
721
|
+
for (const move of [...artifactMoves].reverse()) {
|
|
722
|
+
try {
|
|
723
|
+
if (!isAbsent(move.source) || isAbsent(move.destination)) {
|
|
724
|
+
complete = false;
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
rename(move.destination, move.source);
|
|
728
|
+
} catch (error) {
|
|
729
|
+
if (!isFileSystemError(error)) throw error;
|
|
730
|
+
complete = false;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
const designMoves =
|
|
735
|
+
/** @type {Array<{source: string, destination: string}>} */ (
|
|
736
|
+
target.journal.design_moves ?? []
|
|
737
|
+
);
|
|
738
|
+
for (const move of [...designMoves].reverse()) {
|
|
739
|
+
try {
|
|
740
|
+
if (!isAbsent(move.source) || isAbsent(move.destination)) {
|
|
741
|
+
complete = false;
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
rename(move.destination, move.source);
|
|
745
|
+
} catch (error) {
|
|
746
|
+
if (!isFileSystemError(error)) throw error;
|
|
747
|
+
complete = false;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
if (target.journal.design_created) {
|
|
752
|
+
try {
|
|
753
|
+
const designPath = path.join(target.path, ROOT_DESIGN_NAME);
|
|
754
|
+
if (readdirSync(designPath).length === 0) {
|
|
755
|
+
rmdirSync(designPath);
|
|
756
|
+
} else {
|
|
757
|
+
complete = false;
|
|
758
|
+
}
|
|
759
|
+
} catch (error) {
|
|
760
|
+
if (!isFileSystemError(error) || error.code !== "ENOENT") {
|
|
761
|
+
if (!isFileSystemError(error)) throw error;
|
|
762
|
+
complete = false;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
if (complete) {
|
|
768
|
+
target.irreversible = false;
|
|
769
|
+
target.journal.irreversible = false;
|
|
770
|
+
target.journal.phase = "rolled_back";
|
|
771
|
+
target.journal.pending = null;
|
|
772
|
+
writeJournalBestEffort(target);
|
|
773
|
+
}
|
|
774
|
+
return complete;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/** @param {unknown} error */
|
|
778
|
+
function normalizeMaterializationError(error) {
|
|
779
|
+
if (error instanceof RootOutputMaterializationError) return error;
|
|
780
|
+
if (error instanceof RootOutputPathError) {
|
|
781
|
+
return new RootOutputMaterializationError(
|
|
782
|
+
"The root output preconditions changed during materialization.",
|
|
783
|
+
"root_transaction_failed",
|
|
784
|
+
{ cause: error },
|
|
785
|
+
);
|
|
786
|
+
}
|
|
787
|
+
if (isFileSystemError(error)) {
|
|
788
|
+
return new RootOutputMaterializationError(
|
|
789
|
+
"The root output transaction failed.",
|
|
790
|
+
"root_transaction_failed",
|
|
791
|
+
{ cause: error },
|
|
792
|
+
);
|
|
793
|
+
}
|
|
794
|
+
return new RootOutputMaterializationError(
|
|
795
|
+
"The root output transaction failed.",
|
|
796
|
+
"root_transaction_failed",
|
|
797
|
+
{ cause: error },
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/** @param {string} root @param {string} transactionPath */
|
|
802
|
+
function inspectGitRoot(root, transactionPath) {
|
|
803
|
+
const topLevel = invokeGit(root, ["rev-parse", "--show-toplevel"]);
|
|
804
|
+
if (topLevel.status !== 0) {
|
|
805
|
+
const hasGitEntry = pathExists(path.join(root, ".git"));
|
|
806
|
+
const detail = topLevel.stderr.toString("utf8");
|
|
807
|
+
if (!hasGitEntry && detail.includes("not a git repository")) return null;
|
|
808
|
+
throw new RootOutputPathError(
|
|
809
|
+
"The Git worktree could not be discovered.",
|
|
810
|
+
"root_git_unavailable",
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
const discovered = topLevel.stdout.toString("utf8").trim();
|
|
815
|
+
let physicalTopLevel;
|
|
816
|
+
try {
|
|
817
|
+
physicalTopLevel = realpathSync(discovered);
|
|
818
|
+
} catch (error) {
|
|
819
|
+
if (!isFileSystemError(error)) throw error;
|
|
820
|
+
throw new RootOutputPathError(
|
|
821
|
+
"The Git worktree root could not be read.",
|
|
822
|
+
"root_git_unavailable",
|
|
823
|
+
{ cause: error },
|
|
824
|
+
);
|
|
825
|
+
}
|
|
826
|
+
if (physicalTopLevel !== root) {
|
|
827
|
+
throw new RootOutputPathError(
|
|
828
|
+
"Root output cannot adopt a directory inside another Git worktree.",
|
|
829
|
+
"root_enclosing_worktree",
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
for (const operation of [
|
|
834
|
+
"MERGE_HEAD",
|
|
835
|
+
"CHERRY_PICK_HEAD",
|
|
836
|
+
"REVERT_HEAD",
|
|
837
|
+
"rebase-apply",
|
|
838
|
+
"rebase-merge",
|
|
839
|
+
]) {
|
|
840
|
+
const operationPath = gitPath(root, operation);
|
|
841
|
+
if (pathExists(operationPath)) {
|
|
842
|
+
throw new RootOutputPathError(
|
|
843
|
+
"The Git worktree has an in-progress operation.",
|
|
844
|
+
"root_git_dirty",
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
if (gitBoolean(root, ["config", "--bool", "core.sparseCheckout"])) {
|
|
850
|
+
throw new RootOutputPathError(
|
|
851
|
+
"Sparse Git worktrees are not supported for root output.",
|
|
852
|
+
"root_git_unsupported",
|
|
853
|
+
);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
for (const arguments_ of [
|
|
857
|
+
["diff", "--quiet", "--ignore-submodules=none", "--"],
|
|
858
|
+
["diff", "--cached", "--quiet", "--ignore-submodules=none", "--"],
|
|
859
|
+
]) {
|
|
860
|
+
const result = invokeGit(root, arguments_);
|
|
861
|
+
if (result.status === 1) {
|
|
862
|
+
throw new RootOutputPathError(
|
|
863
|
+
"The Git worktree or index is not clean.",
|
|
864
|
+
"root_git_dirty",
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
if (result.status !== 0) {
|
|
868
|
+
throw new RootOutputPathError(
|
|
869
|
+
"The Git worktree could not be inspected.",
|
|
870
|
+
"root_git_unavailable",
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
const entriesResult = requiredGitPath(root, ["ls-files", "--stage", "-z"]);
|
|
876
|
+
const indexEntries = parseIndexEntries(entriesResult.stdout);
|
|
877
|
+
if (
|
|
878
|
+
indexEntries.some(
|
|
879
|
+
(entry) =>
|
|
880
|
+
entry.mode === "160000" ||
|
|
881
|
+
entry.path.split("/").includes(".gitmodules"),
|
|
882
|
+
)
|
|
883
|
+
) {
|
|
884
|
+
throw new RootOutputPathError(
|
|
885
|
+
"Git submodules are not supported for root output.",
|
|
886
|
+
"root_git_unsupported",
|
|
887
|
+
);
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
const unmerged = requiredGitPath(root, ["ls-files", "--unmerged", "-z"]);
|
|
891
|
+
if (unmerged.stdout.length > 0) {
|
|
892
|
+
throw new RootOutputPathError(
|
|
893
|
+
"The Git index contains unmerged entries.",
|
|
894
|
+
"root_git_dirty",
|
|
895
|
+
);
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
const status = requiredGitPath(root, [
|
|
899
|
+
"status",
|
|
900
|
+
"--porcelain=v1",
|
|
901
|
+
"-z",
|
|
902
|
+
"--untracked-files=all",
|
|
903
|
+
"--ignored=matching",
|
|
904
|
+
]);
|
|
905
|
+
const ignoredPaths = parseIgnoredPaths(status.stdout).filter(
|
|
906
|
+
(candidate) =>
|
|
907
|
+
candidate !== ROOT_TRANSACTION_NAME &&
|
|
908
|
+
!candidate.startsWith(`${ROOT_TRANSACTION_NAME}/`),
|
|
909
|
+
);
|
|
910
|
+
|
|
911
|
+
const gitDirectory = requiredGitPath(root, [
|
|
912
|
+
"rev-parse",
|
|
913
|
+
"--absolute-git-dir",
|
|
914
|
+
])
|
|
915
|
+
.stdout.toString("utf8")
|
|
916
|
+
.trim();
|
|
917
|
+
const indexPath = gitPath(root, "index");
|
|
918
|
+
const indexLockPath = gitPath(root, "index.lock");
|
|
919
|
+
if (pathExists(indexLockPath)) {
|
|
920
|
+
throw new RootOutputPathError(
|
|
921
|
+
"The Git index is currently locked.",
|
|
922
|
+
"root_busy",
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
return {
|
|
927
|
+
gitDirectory,
|
|
928
|
+
indexPath,
|
|
929
|
+
indexLockPath,
|
|
930
|
+
indexEntries,
|
|
931
|
+
ignoredPaths,
|
|
932
|
+
transactionPath,
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/** @param {string} root @param {string[]} arguments_ */
|
|
937
|
+
function gitBoolean(root, arguments_) {
|
|
938
|
+
const result = invokeGit(root, arguments_);
|
|
939
|
+
if (result.status === 1) return false;
|
|
940
|
+
if (result.status !== 0) {
|
|
941
|
+
throw new RootOutputPathError(
|
|
942
|
+
"The Git configuration could not be inspected.",
|
|
943
|
+
"root_git_unavailable",
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
return result.stdout.toString("utf8").trim() === "true";
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
/** @param {string} root @param {string} name */
|
|
950
|
+
function gitPath(root, name) {
|
|
951
|
+
return requiredGitPath(root, [
|
|
952
|
+
"rev-parse",
|
|
953
|
+
"--path-format=absolute",
|
|
954
|
+
"--git-path",
|
|
955
|
+
name,
|
|
956
|
+
])
|
|
957
|
+
.stdout.toString("utf8")
|
|
958
|
+
.trim();
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
/** @param {string} root @param {string[]} arguments_ */
|
|
962
|
+
function requiredGitPath(root, arguments_) {
|
|
963
|
+
const result = invokeGit(root, arguments_);
|
|
964
|
+
if (result.status !== 0) {
|
|
965
|
+
throw new RootOutputPathError(
|
|
966
|
+
"The required Git inspection failed.",
|
|
967
|
+
"root_git_unavailable",
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
return result;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
/** @param {Buffer} source */
|
|
974
|
+
function parseIndexEntries(source) {
|
|
975
|
+
/** @type {GitIndexEntry[]} */
|
|
976
|
+
const entries = [];
|
|
977
|
+
for (const record of nullRecords(source)) {
|
|
978
|
+
const tab = record.indexOf("\t");
|
|
979
|
+
const header = tab === -1 ? "" : record.slice(0, tab);
|
|
980
|
+
const filePath = tab === -1 ? "" : record.slice(tab + 1);
|
|
981
|
+
const match = /^(\d+) ([0-9a-f]+) (\d)$/.exec(header);
|
|
982
|
+
if (match === null || match[3] !== "0" || filePath.length === 0) {
|
|
983
|
+
throw new RootOutputPathError(
|
|
984
|
+
"The Git index shape is unsupported.",
|
|
985
|
+
"root_git_unsupported",
|
|
986
|
+
);
|
|
987
|
+
}
|
|
988
|
+
entries.push({
|
|
989
|
+
mode: match[1] ?? "",
|
|
990
|
+
object: match[2] ?? "",
|
|
991
|
+
path: filePath,
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
return entries;
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
/** @param {Buffer} source */
|
|
998
|
+
function parseIgnoredPaths(source) {
|
|
999
|
+
const ignored = [];
|
|
1000
|
+
for (const record of nullRecords(source)) {
|
|
1001
|
+
if (!record.startsWith("!! ")) continue;
|
|
1002
|
+
const candidate = normalizeGitPath(record.slice(3));
|
|
1003
|
+
if (candidate !== "") ignored.push(candidate);
|
|
1004
|
+
}
|
|
1005
|
+
return [...new Set(ignored)].sort(compareStrings);
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
/** @param {RootOutputTarget} target */
|
|
1009
|
+
function assertIgnoreProtection(target) {
|
|
1010
|
+
if (target.git === null || target.git.ignoredPaths.length === 0) return;
|
|
1011
|
+
const previewRoot = path.join(target.transactionPath, PREVIEW_NAME);
|
|
1012
|
+
const previewWorktree = path.join(previewRoot, "worktree");
|
|
1013
|
+
mkdirSync(previewWorktree, { recursive: true, mode: PRIVATE_DIRECTORY_MODE });
|
|
1014
|
+
|
|
1015
|
+
try {
|
|
1016
|
+
for (const ignoredPath of target.git.ignoredPaths) {
|
|
1017
|
+
copyApplicableIgnoreFiles(target.path, previewWorktree, ignoredPath);
|
|
1018
|
+
createPreviewEntry(target.path, previewWorktree, ignoredPath);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
const expected = target.git.ignoredPaths.map(
|
|
1022
|
+
(candidate) => `${ROOT_DESIGN_NAME}/${candidate}`,
|
|
1023
|
+
);
|
|
1024
|
+
const result = invokeGit(
|
|
1025
|
+
target.path,
|
|
1026
|
+
["check-ignore", "--no-index", "-z", "--stdin"],
|
|
1027
|
+
{
|
|
1028
|
+
input: Buffer.from(`${expected.join("\0")}\0`),
|
|
1029
|
+
gitDirectory: target.git.gitDirectory,
|
|
1030
|
+
workTree: previewWorktree,
|
|
1031
|
+
},
|
|
1032
|
+
);
|
|
1033
|
+
if (result.status !== 0 && result.status !== 1) {
|
|
1034
|
+
throw new RootOutputPathError(
|
|
1035
|
+
"Git ignore protection could not be previewed.",
|
|
1036
|
+
"root_git_unavailable",
|
|
1037
|
+
);
|
|
1038
|
+
}
|
|
1039
|
+
const actual = new Set(nullRecords(result.stdout).map(normalizeGitPath));
|
|
1040
|
+
if (!expected.every((candidate) => actual.has(candidate))) {
|
|
1041
|
+
throw new RootOutputPathError(
|
|
1042
|
+
"A currently ignored path would become visible after root adoption.",
|
|
1043
|
+
"root_ignore_not_preserved",
|
|
1044
|
+
);
|
|
1045
|
+
}
|
|
1046
|
+
} finally {
|
|
1047
|
+
rmSync(previewRoot, { recursive: true, force: true });
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/** @param {string} root @param {string} previewWorktree @param {string} ignoredPath */
|
|
1052
|
+
function copyApplicableIgnoreFiles(root, previewWorktree, ignoredPath) {
|
|
1053
|
+
const components = ignoredPath.split("/");
|
|
1054
|
+
for (let depth = 0; depth < components.length; depth += 1) {
|
|
1055
|
+
const ancestor = components.slice(0, depth);
|
|
1056
|
+
const source = path.join(root, ...ancestor, ".gitignore");
|
|
1057
|
+
let stat;
|
|
1058
|
+
try {
|
|
1059
|
+
stat = lstatSync(source);
|
|
1060
|
+
} catch (error) {
|
|
1061
|
+
if (!isFileSystemError(error)) throw error;
|
|
1062
|
+
if (error.code === "ENOENT") continue;
|
|
1063
|
+
throw error;
|
|
1064
|
+
}
|
|
1065
|
+
if (!stat.isFile() || stat.isSymbolicLink()) continue;
|
|
1066
|
+
|
|
1067
|
+
const destination = path.join(
|
|
1068
|
+
previewWorktree,
|
|
1069
|
+
ROOT_DESIGN_NAME,
|
|
1070
|
+
...ancestor,
|
|
1071
|
+
".gitignore",
|
|
1072
|
+
);
|
|
1073
|
+
mkdirSync(path.dirname(destination), {
|
|
1074
|
+
recursive: true,
|
|
1075
|
+
mode: DIRECTORY_MODE,
|
|
1076
|
+
});
|
|
1077
|
+
if (!pathExists(destination))
|
|
1078
|
+
writeFileSync(destination, readFileSync(source));
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
/** @param {string} root @param {string} previewWorktree @param {string} ignoredPath */
|
|
1083
|
+
function createPreviewEntry(root, previewWorktree, ignoredPath) {
|
|
1084
|
+
const source = path.join(root, ...ignoredPath.split("/"));
|
|
1085
|
+
const destination = path.join(
|
|
1086
|
+
previewWorktree,
|
|
1087
|
+
ROOT_DESIGN_NAME,
|
|
1088
|
+
...ignoredPath.split("/"),
|
|
1089
|
+
);
|
|
1090
|
+
let stat;
|
|
1091
|
+
try {
|
|
1092
|
+
stat = lstatSync(source);
|
|
1093
|
+
} catch (error) {
|
|
1094
|
+
if (!isFileSystemError(error)) throw error;
|
|
1095
|
+
throw new RootOutputPathError(
|
|
1096
|
+
"A currently ignored path changed during root output preflight.",
|
|
1097
|
+
"root_ignore_not_preserved",
|
|
1098
|
+
{ cause: error },
|
|
1099
|
+
);
|
|
1100
|
+
}
|
|
1101
|
+
if (stat.isDirectory()) {
|
|
1102
|
+
mkdirSync(destination, { recursive: true, mode: DIRECTORY_MODE });
|
|
1103
|
+
} else {
|
|
1104
|
+
mkdirSync(path.dirname(destination), {
|
|
1105
|
+
recursive: true,
|
|
1106
|
+
mode: DIRECTORY_MODE,
|
|
1107
|
+
});
|
|
1108
|
+
if (!pathExists(destination)) writeFileSync(destination, Buffer.alloc(0));
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/** @param {RootOutputTarget} target @param {Array<{path: string, mode: 420 | 493}>} files */
|
|
1113
|
+
function prepareGitIndex(target, files) {
|
|
1114
|
+
const git = /** @type {RootGitContext} */ (target.git);
|
|
1115
|
+
const preparedPath = path.join(target.transactionPath, PREPARED_INDEX_NAME);
|
|
1116
|
+
const originalPath = path.join(target.transactionPath, ORIGINAL_INDEX_NAME);
|
|
1117
|
+
|
|
1118
|
+
requiredGit(target.path, ["read-tree", "--empty"], {
|
|
1119
|
+
indexFile: preparedPath,
|
|
1120
|
+
});
|
|
1121
|
+
|
|
1122
|
+
const indexRecords = [];
|
|
1123
|
+
for (const entry of git.indexEntries) {
|
|
1124
|
+
indexRecords.push(
|
|
1125
|
+
Buffer.from(
|
|
1126
|
+
`${entry.mode} ${entry.object} 0\t${ROOT_DESIGN_NAME}/${entry.path}\0`,
|
|
1127
|
+
),
|
|
1128
|
+
);
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
const artifactStage = path.join(target.transactionPath, ARTIFACT_STAGE_NAME);
|
|
1132
|
+
for (const file of files) {
|
|
1133
|
+
const source = path.join(artifactStage, ...file.path.split("/"));
|
|
1134
|
+
const hashed = requiredGit(target.path, [
|
|
1135
|
+
"hash-object",
|
|
1136
|
+
"-w",
|
|
1137
|
+
"--no-filters",
|
|
1138
|
+
source,
|
|
1139
|
+
])
|
|
1140
|
+
.stdout.toString("utf8")
|
|
1141
|
+
.trim();
|
|
1142
|
+
if (!GIT_HASH_PATTERN.test(hashed)) {
|
|
1143
|
+
throw new RootOutputMaterializationError(
|
|
1144
|
+
"Git did not return a valid artifact object identity.",
|
|
1145
|
+
"root_transaction_failed",
|
|
1146
|
+
);
|
|
1147
|
+
}
|
|
1148
|
+
const mode = file.mode === 0o755 ? "100755" : "100644";
|
|
1149
|
+
indexRecords.push(Buffer.from(`${mode} ${hashed} 0\t${file.path}\0`));
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
requiredGit(target.path, ["update-index", "-z", "--index-info"], {
|
|
1153
|
+
indexFile: preparedPath,
|
|
1154
|
+
input: Buffer.concat(indexRecords),
|
|
1155
|
+
});
|
|
1156
|
+
|
|
1157
|
+
const preparedSource = readFileSync(preparedPath);
|
|
1158
|
+
const originalExists = pathExists(git.indexPath);
|
|
1159
|
+
const originalSource = originalExists ? readFileSync(git.indexPath) : null;
|
|
1160
|
+
const originalMode = originalExists
|
|
1161
|
+
? lstatSync(git.indexPath).mode & 0o777
|
|
1162
|
+
: 0o644;
|
|
1163
|
+
if (originalSource !== null) {
|
|
1164
|
+
writeFileSync(originalPath, originalSource, {
|
|
1165
|
+
flag: "wx",
|
|
1166
|
+
mode: PRIVATE_FILE_MODE,
|
|
1167
|
+
});
|
|
1168
|
+
chmodSync(originalPath, PRIVATE_FILE_MODE);
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
target.journal.index = {
|
|
1172
|
+
path: git.indexPath,
|
|
1173
|
+
lock_path: git.indexLockPath,
|
|
1174
|
+
original_exists: originalExists,
|
|
1175
|
+
original_mode: originalMode,
|
|
1176
|
+
original_sha256: originalSource === null ? null : sha256(originalSource),
|
|
1177
|
+
prepared_sha256: sha256(preparedSource),
|
|
1178
|
+
installed: false,
|
|
1179
|
+
};
|
|
1180
|
+
writeJournal(target);
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
/** @param {RootOutputTarget} target */
|
|
1184
|
+
function installPreparedIndex(target) {
|
|
1185
|
+
const git = /** @type {RootGitContext} */ (target.git);
|
|
1186
|
+
const preparedSource = readFileSync(
|
|
1187
|
+
path.join(target.transactionPath, PREPARED_INDEX_NAME),
|
|
1188
|
+
);
|
|
1189
|
+
const indexJournal = /** @type {Record<string, unknown>} */ (
|
|
1190
|
+
target.journal.index
|
|
1191
|
+
);
|
|
1192
|
+
startIrreversibleStep(target, {
|
|
1193
|
+
kind: "install_git_index",
|
|
1194
|
+
source: path.join(target.transactionPath, PREPARED_INDEX_NAME),
|
|
1195
|
+
destination: git.indexPath,
|
|
1196
|
+
});
|
|
1197
|
+
writeLockedIndex(
|
|
1198
|
+
git.indexPath,
|
|
1199
|
+
git.indexLockPath,
|
|
1200
|
+
preparedSource,
|
|
1201
|
+
Number(indexJournal.original_mode ?? 0o644),
|
|
1202
|
+
);
|
|
1203
|
+
indexJournal.installed = true;
|
|
1204
|
+
finishIrreversibleStep(target, "index_installed");
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
/** @param {RootOutputTarget} target */
|
|
1208
|
+
function refreshPreparedIndex(target) {
|
|
1209
|
+
const preparedPath = path.join(target.transactionPath, PREPARED_INDEX_NAME);
|
|
1210
|
+
requiredGit(target.path, ["update-index", "--refresh"], {
|
|
1211
|
+
indexFile: preparedPath,
|
|
1212
|
+
});
|
|
1213
|
+
const indexJournal = /** @type {Record<string, unknown>} */ (
|
|
1214
|
+
target.journal.index
|
|
1215
|
+
);
|
|
1216
|
+
indexJournal.prepared_sha256 = sha256(readFileSync(preparedPath));
|
|
1217
|
+
writeJournal(target);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
/** @param {string} indexPath @param {string} lockPath @param {Buffer} source @param {number} mode */
|
|
1221
|
+
function writeLockedIndex(indexPath, lockPath, source, mode) {
|
|
1222
|
+
let descriptor = null;
|
|
1223
|
+
let ownsLock = false;
|
|
1224
|
+
try {
|
|
1225
|
+
descriptor = openSync(lockPath, "wx", mode);
|
|
1226
|
+
ownsLock = true;
|
|
1227
|
+
writeFileSync(descriptor, source);
|
|
1228
|
+
fsyncSync(descriptor);
|
|
1229
|
+
closeSync(descriptor);
|
|
1230
|
+
descriptor = null;
|
|
1231
|
+
renameSync(lockPath, indexPath);
|
|
1232
|
+
ownsLock = false;
|
|
1233
|
+
chmodSync(indexPath, mode);
|
|
1234
|
+
fsyncDirectory(path.dirname(indexPath));
|
|
1235
|
+
} finally {
|
|
1236
|
+
if (descriptor !== null) closeSync(descriptor);
|
|
1237
|
+
if (ownsLock) removePathIfPresent(lockPath);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
/** @param {RootOutputTarget} target */
|
|
1242
|
+
function assertInstalledIndex(target) {
|
|
1243
|
+
const git = /** @type {RootGitContext} */ (target.git);
|
|
1244
|
+
const indexJournal = /** @type {Record<string, unknown>} */ (
|
|
1245
|
+
target.journal.index
|
|
1246
|
+
);
|
|
1247
|
+
if (
|
|
1248
|
+
indexJournal.installed !== true ||
|
|
1249
|
+
!pathExists(git.indexPath) ||
|
|
1250
|
+
sha256(readFileSync(git.indexPath)) !== indexJournal.prepared_sha256
|
|
1251
|
+
) {
|
|
1252
|
+
throw new RootOutputMaterializationError(
|
|
1253
|
+
"The prepared Git index was not installed exactly.",
|
|
1254
|
+
"root_transaction_failed",
|
|
1255
|
+
);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
/** @param {RootOutputTarget} target */
|
|
1260
|
+
function rollbackIndex(target) {
|
|
1261
|
+
if (target.git === null || target.journal.index === null) return true;
|
|
1262
|
+
const git = target.git;
|
|
1263
|
+
const indexJournal = /** @type {Record<string, unknown>} */ (
|
|
1264
|
+
target.journal.index
|
|
1265
|
+
);
|
|
1266
|
+
const pending = /** @type {Record<string, unknown> | null} */ (
|
|
1267
|
+
target.journal.pending
|
|
1268
|
+
);
|
|
1269
|
+
const installationAttempted =
|
|
1270
|
+
indexJournal.installed === true ||
|
|
1271
|
+
target.journal.phase === "index_installed" ||
|
|
1272
|
+
pending?.kind === "install_git_index";
|
|
1273
|
+
if (!installationAttempted) return true;
|
|
1274
|
+
|
|
1275
|
+
try {
|
|
1276
|
+
const indexExists = pathExists(git.indexPath);
|
|
1277
|
+
const currentDigest = indexExists
|
|
1278
|
+
? sha256(readFileSync(git.indexPath))
|
|
1279
|
+
: null;
|
|
1280
|
+
const originalStatePresent =
|
|
1281
|
+
indexJournal.original_exists === true
|
|
1282
|
+
? currentDigest === indexJournal.original_sha256
|
|
1283
|
+
: !indexExists;
|
|
1284
|
+
if (originalStatePresent) {
|
|
1285
|
+
indexJournal.installed = false;
|
|
1286
|
+
return true;
|
|
1287
|
+
}
|
|
1288
|
+
if (currentDigest !== indexJournal.prepared_sha256) return false;
|
|
1289
|
+
|
|
1290
|
+
if (indexJournal.original_exists === true) {
|
|
1291
|
+
const source = readFileSync(
|
|
1292
|
+
path.join(target.transactionPath, ORIGINAL_INDEX_NAME),
|
|
1293
|
+
);
|
|
1294
|
+
if (sha256(source) !== indexJournal.original_sha256) return false;
|
|
1295
|
+
writeLockedIndex(
|
|
1296
|
+
git.indexPath,
|
|
1297
|
+
git.indexLockPath,
|
|
1298
|
+
source,
|
|
1299
|
+
Number(indexJournal.original_mode),
|
|
1300
|
+
);
|
|
1301
|
+
} else {
|
|
1302
|
+
const descriptor = openSync(git.indexLockPath, "wx", PRIVATE_FILE_MODE);
|
|
1303
|
+
closeSync(descriptor);
|
|
1304
|
+
unlinkSync(git.indexPath);
|
|
1305
|
+
unlinkSync(git.indexLockPath);
|
|
1306
|
+
fsyncDirectory(path.dirname(git.indexPath));
|
|
1307
|
+
}
|
|
1308
|
+
indexJournal.installed = false;
|
|
1309
|
+
return true;
|
|
1310
|
+
} catch (error) {
|
|
1311
|
+
if (!isFileSystemError(error)) throw error;
|
|
1312
|
+
return false;
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
/** @param {RootOutputTarget} target */
|
|
1317
|
+
function assertMovedIgnoreProtection(target) {
|
|
1318
|
+
if (target.git === null || target.git.ignoredPaths.length === 0) return;
|
|
1319
|
+
const expected = target.git.ignoredPaths.map(
|
|
1320
|
+
(candidate) => `${ROOT_DESIGN_NAME}/${candidate}`,
|
|
1321
|
+
);
|
|
1322
|
+
const result = invokeGit(
|
|
1323
|
+
target.path,
|
|
1324
|
+
["check-ignore", "--no-index", "-z", "--stdin"],
|
|
1325
|
+
{ input: Buffer.from(`${expected.join("\0")}\0`) },
|
|
1326
|
+
);
|
|
1327
|
+
if (result.status !== 0 && result.status !== 1) {
|
|
1328
|
+
throw new RootOutputMaterializationError(
|
|
1329
|
+
"Git ignore protection could not be rechecked.",
|
|
1330
|
+
"root_transaction_failed",
|
|
1331
|
+
);
|
|
1332
|
+
}
|
|
1333
|
+
const actual = new Set(nullRecords(result.stdout).map(normalizeGitPath));
|
|
1334
|
+
if (!expected.every((candidate) => actual.has(candidate))) {
|
|
1335
|
+
throw new RootOutputMaterializationError(
|
|
1336
|
+
"A previously ignored path is no longer ignored.",
|
|
1337
|
+
"root_ignore_changed",
|
|
1338
|
+
);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* @param {string} root
|
|
1344
|
+
* @param {string[]} arguments_
|
|
1345
|
+
* @param {{input?: Buffer, indexFile?: string, gitDirectory?: string, workTree?: string}} [options]
|
|
1346
|
+
*/
|
|
1347
|
+
function requiredGit(root, arguments_, options = {}) {
|
|
1348
|
+
const result = invokeGit(root, arguments_, options);
|
|
1349
|
+
if (result.status !== 0) {
|
|
1350
|
+
throw new RootOutputMaterializationError(
|
|
1351
|
+
"The required Git operation failed.",
|
|
1352
|
+
"root_transaction_failed",
|
|
1353
|
+
);
|
|
1354
|
+
}
|
|
1355
|
+
return result;
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* @param {string} root
|
|
1360
|
+
* @param {string[]} arguments_
|
|
1361
|
+
* @param {{input?: Buffer, indexFile?: string, gitDirectory?: string, workTree?: string}} [options]
|
|
1362
|
+
*/
|
|
1363
|
+
function invokeGit(root, arguments_, options = {}) {
|
|
1364
|
+
/** @type {NodeJS.ProcessEnv} */
|
|
1365
|
+
const environment = {
|
|
1366
|
+
...process.env,
|
|
1367
|
+
GIT_OPTIONAL_LOCKS: "0",
|
|
1368
|
+
LC_ALL: "C",
|
|
1369
|
+
LANGUAGE: "",
|
|
1370
|
+
};
|
|
1371
|
+
delete environment.GIT_DIR;
|
|
1372
|
+
delete environment.GIT_WORK_TREE;
|
|
1373
|
+
delete environment.GIT_INDEX_FILE;
|
|
1374
|
+
if (options.gitDirectory !== undefined) {
|
|
1375
|
+
environment.GIT_DIR = options.gitDirectory;
|
|
1376
|
+
}
|
|
1377
|
+
if (options.workTree !== undefined)
|
|
1378
|
+
environment.GIT_WORK_TREE = options.workTree;
|
|
1379
|
+
if (options.indexFile !== undefined)
|
|
1380
|
+
environment.GIT_INDEX_FILE = options.indexFile;
|
|
1381
|
+
|
|
1382
|
+
const result = spawnSync(
|
|
1383
|
+
"git",
|
|
1384
|
+
["--no-optional-locks", "-C", root, ...arguments_],
|
|
1385
|
+
{
|
|
1386
|
+
env: environment,
|
|
1387
|
+
input: options.input,
|
|
1388
|
+
maxBuffer: MAX_GIT_OUTPUT_BYTES,
|
|
1389
|
+
},
|
|
1390
|
+
);
|
|
1391
|
+
if (result.error !== undefined || result.status === null) {
|
|
1392
|
+
throw new RootOutputPathError(
|
|
1393
|
+
"The Git executable is unavailable.",
|
|
1394
|
+
"root_git_unavailable",
|
|
1395
|
+
{ cause: result.error },
|
|
1396
|
+
);
|
|
1397
|
+
}
|
|
1398
|
+
return {
|
|
1399
|
+
status: result.status,
|
|
1400
|
+
stdout: result.stdout ?? Buffer.alloc(0),
|
|
1401
|
+
stderr: result.stderr ?? Buffer.alloc(0),
|
|
1402
|
+
};
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
/** @param {Buffer} source */
|
|
1406
|
+
function nullRecords(source) {
|
|
1407
|
+
const records = source.toString("utf8").split("\0");
|
|
1408
|
+
if (records.at(-1) === "") records.pop();
|
|
1409
|
+
return records;
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
/** @param {string} candidate */
|
|
1413
|
+
function normalizeGitPath(candidate) {
|
|
1414
|
+
return candidate.endsWith("/") ? candidate.slice(0, -1) : candidate;
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
/** @param {string} candidate */
|
|
1418
|
+
function pathExists(candidate) {
|
|
1419
|
+
try {
|
|
1420
|
+
lstatSync(candidate);
|
|
1421
|
+
return true;
|
|
1422
|
+
} catch (error) {
|
|
1423
|
+
if (!isFileSystemError(error)) throw error;
|
|
1424
|
+
if (error.code === "ENOENT") return false;
|
|
1425
|
+
throw error;
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
/** @param {string} candidate @param {RootEntryIdentity} expected */
|
|
1430
|
+
function sameIdentity(candidate, expected) {
|
|
1431
|
+
try {
|
|
1432
|
+
const stat = lstatSync(candidate, { bigint: true });
|
|
1433
|
+
const kind = stat.isFile()
|
|
1434
|
+
? "file"
|
|
1435
|
+
: stat.isDirectory()
|
|
1436
|
+
? "directory"
|
|
1437
|
+
: null;
|
|
1438
|
+
return (
|
|
1439
|
+
kind === expected.kind &&
|
|
1440
|
+
String(stat.dev) === expected.device &&
|
|
1441
|
+
String(stat.ino) === expected.inode
|
|
1442
|
+
);
|
|
1443
|
+
} catch (error) {
|
|
1444
|
+
if (!isFileSystemError(error)) throw error;
|
|
1445
|
+
return false;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
/** @param {string} candidate */
|
|
1450
|
+
function isAbsent(candidate) {
|
|
1451
|
+
try {
|
|
1452
|
+
lstatSync(candidate);
|
|
1453
|
+
return false;
|
|
1454
|
+
} catch (error) {
|
|
1455
|
+
if (!isFileSystemError(error)) throw error;
|
|
1456
|
+
if (error.code === "ENOENT") return true;
|
|
1457
|
+
throw error;
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
/** @param {string} candidate */
|
|
1462
|
+
function removePathIfPresent(candidate) {
|
|
1463
|
+
try {
|
|
1464
|
+
unlinkSync(candidate);
|
|
1465
|
+
} catch (error) {
|
|
1466
|
+
if (!isFileSystemError(error) || error.code !== "ENOENT") throw error;
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
/** @param {string} left @param {string} right */
|
|
1471
|
+
function compareStrings(left, right) {
|
|
1472
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
/** @param {Set<string>} left @param {Set<string>} right */
|
|
1476
|
+
function setsEqual(left, right) {
|
|
1477
|
+
return (
|
|
1478
|
+
left.size === right.size && [...left].every((value) => right.has(value))
|
|
1479
|
+
);
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
/** @param {Buffer | string} source */
|
|
1483
|
+
function sha256(source) {
|
|
1484
|
+
return createHash("sha256").update(source).digest("hex");
|
|
1485
|
+
}
|