feat: add durable isolated Git workspaces and source-bound delivery
This commit is contained in:
@@ -13,12 +13,12 @@ export function gitArguments(root, args, hooksDirectory) {
|
||||
...(hooksDirectory ? ['-c', 'core.hooksPath=' + hooksDirectory] : []), ...args];
|
||||
}
|
||||
|
||||
export function runGit(root, args, { input, env = {}, signal, timeoutMs = 10000, hooksDirectory, maximumBytes = 2 * 1024 * 1024 } = {}) {
|
||||
export function runGit(root, args, { input, env = {}, signal, timeoutMs = 10000, hooksDirectory, maximumBytes = 2 * 1024 * 1024, onSpawn } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (signal?.aborted) return reject(signal.reason ?? new Error('Git操作已停止。'));
|
||||
const child = spawn('git', gitArguments(root, args, hooksDirectory), { env: gitEnvironment(env), windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'] });
|
||||
const chunks = [], errors = [];
|
||||
let bytes = 0, exceeded = false, timedOut = false;
|
||||
let bytes = 0, exceeded = false, timedOut = false, spawnError = null;
|
||||
const capture = list => chunk => { bytes += chunk.length; if (bytes > maximumBytes) { exceeded = true; child.kill(); } else list.push(chunk); };
|
||||
child.stdout.on('data', capture(chunks)); child.stderr.on('data', capture(errors));
|
||||
child.stdin.on('error', () => {});
|
||||
@@ -28,10 +28,15 @@ export function runGit(root, args, { input, env = {}, signal, timeoutMs = 10000,
|
||||
child.once('error', error => { clearTimeout(timer); signal?.removeEventListener('abort', stop); reject(error); });
|
||||
child.once('close', code => {
|
||||
clearTimeout(timer); signal?.removeEventListener('abort', stop);
|
||||
if (spawnError) return reject(spawnError);
|
||||
const output = Buffer.concat(chunks), errorOutput = Buffer.concat(errors).toString('utf8');
|
||||
resolve({ ok: code === 0 && !exceeded && !timedOut && !signal?.aborted, code, output, text: output.toString('utf8'),
|
||||
error: errorOutput, exceeded, timedOut, aborted: !!signal?.aborted });
|
||||
});
|
||||
child.once('spawn', () => {
|
||||
try { onSpawn?.(child.pid); }
|
||||
catch (error) { spawnError = error; child.kill(); }
|
||||
});
|
||||
child.stdin.end(input);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user