Files
stack/packages/project/src/git.mjs
T
rocko 4bdc7e0e86 processing: inspectable per-run stop seam for lifecycle fire
Unify halt and fire cancellation by verified run identity with durable first-owner receipts and shared deadlines. Integrate the instance control with the current host, library and tester-socket paths. Focused nine-file evidence is 62 passing tests; broad suites, mutants and review remain pending.
2026-10-21 22:42:12 -05:00

91 lines
4.7 KiB
JavaScript

import { lstatSync, readdirSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { spawn } from 'node:child_process';
import { gitEnv, runGit, GIT_LIMITS } from '../../seat/src/checkout.mjs';
import { ProjectError } from './paths.mjs';
import { currentProcessing } from '../../processing-control/src/effects.mjs';
export const SAFE = ['-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'core.ignorestat=false',
'-c', 'protocol.allow=never', '-c', 'submodule.recurse=false', '-c', 'commit.gpgsign=false',
'-c', 'maintenance.auto=false', '-c', 'gc.auto=0'];
export async function git(repo, args, identity = {}) {
const result = await runGit([...SAFE, '-C', repo, ...args], {
...gitEnv(), GIT_CEILING_DIRECTORIES: dirname(repo), ...identity,
});
if (!result.ok) throw new ProjectError('project-git', `local git ${args[0]} refused (${result.why})`);
return result.out.trimEnd();
}
export async function inspectGit(repo) {
// Refuse gitdir indirection, alternates, links or hardlinks before git reads metadata.
const walk = directory => {
const st = lstatSync(directory);
if (!st.isDirectory() || st.isSymbolicLink()) throw new ProjectError('project-repository', 'expected local .git directory');
for (const name of readdirSync(directory)) {
const path = join(directory, name), entry = lstatSync(path);
if (entry.isDirectory()) walk(path);
else if (!entry.isFile() || entry.nlink !== 1)
throw new ProjectError('project-repository', 'git metadata must contain regular private files');
}
};
walk(join(repo, '.git'));
for (const name of ['commondir', 'objects/info/alternates', 'objects/info/http-alternates']) {
try { lstatSync(join(repo, '.git', name)); }
catch (e) { if (e.code === 'ENOENT') continue; throw e; }
throw new ProjectError('project-repository', 'git metadata indirection refused');
}
const config = lstatSync(join(repo, '.git/config'));
if (config.size > 65536) throw new ProjectError('project-repository', 'git config is too large');
const list = await git(repo, ['config', '--no-includes', '--file', join(repo, '.git/config'), '--list', '--null']);
const allowed = {
'core.repositoryformatversion': ['0'], 'core.bare': ['false'], 'core.filemode': ['true', 'false'],
'core.logallrefupdates': ['true'], 'core.ignorecase': ['true', 'false'],
};
for (const entry of list.split('\0').filter(Boolean)) {
const split = entry.indexOf('\n'), key = entry.slice(0, split), value = entry.slice(split + 1);
if (key.startsWith('remote.')) throw new ProjectError('project-remote', 'project repository must have no remote');
if (['user.name', 'user.email'].includes(key)) continue;
if (!allowed[key]?.includes(value)) throw new ProjectError('project-repository', `unsupported local git config: ${key}`);
}
}
export async function requireClean(repo) {
const status = await git(repo, ['status', '--porcelain=v1', '--untracked-files=all', '--ignored=matching']);
const flags = await git(repo, ['ls-files', '-v']);
if (status || flags.split('\n').some(line => /^[a-zS] /.test(line)))
throw new ProjectError('project-dirty', 'pending edits, staged changes, ignored content or hidden index flags refuse');
}
export const identity = name => ({ GIT_AUTHOR_NAME: name, GIT_AUTHOR_EMAIL: `${name}@mosaic.invalid`,
GIT_COMMITTER_NAME: name, GIT_COMMITTER_EMAIL: `${name}@mosaic.invalid` });
// Blob/log reads preserve bytes and bound memory before decoding untrusted Git data.
export async function gitBytes(repo, args, maxBytes) {
return new Promise((resolve, reject) => {
const processing = currentProcessing();
processing?.guard();
const child = spawn('git', [...SAFE, '-C', repo, ...args], {
env: { ...gitEnv(), GIT_CEILING_DIRECTORIES: dirname(repo) },
detached: true, stdio: ['ignore', 'pipe', 'pipe'],
});
processing?.track(child);
let size = 0, failed = false, settled = false;
const chunks = [];
const kill = () => { try { process.kill(-child.pid, 'SIGKILL'); } catch {} };
const timer = setTimeout(() => { failed = true; kill(); }, GIT_LIMITS.timeoutMs);
const finish = (code) => {
if (settled) return;
settled = true;
clearTimeout(timer);
if (failed || code !== 0) reject(new ProjectError('project-git', 'bounded local Git read refused'));
else resolve(Buffer.concat(chunks));
};
child.stdout.on('data', chunk => {
size += chunk.length;
if (size > maxBytes) { failed = true; kill(); }
else chunks.push(chunk);
});
// Drain stderr without retaining repository-controlled text.
child.stderr.on('data', () => {});
child.on('error', () => finish(-1));
child.on('close', finish);
});
}