Files
TORCH/src/kernel/worktree-state.mjs
T

23 lines
1.0 KiB
JavaScript

import { execFileSync } from 'node:child_process';
import { existsSync } from 'node:fs';
function git(root, args) {
try {
return execFileSync('git', ['-C', root, ...args], {
encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'],
}).trim();
} catch { return ''; }
}
export function inspectManagedWorktree(root, entry, mainBranch) {
if (!existsSync(entry.path)) return { ...entry, safeToRemove: false, problems: ['worktree-missing'] };
const problems = [];
const actualBranch = git(entry.path, ['branch', '--show-current']);
if (actualBranch !== entry.branch) problems.push(`branch-mismatch:${actualBranch || 'detached'}`);
const status = git(entry.path, ['status', '--porcelain=v1', '--untracked-files=all']);
if (status) problems.push('worktree-dirty');
const ahead = Number(git(root, ['rev-list', '--count', `${mainBranch}..${entry.branch}`]) || 0);
if (ahead > 0) problems.push(`unique-commits:${ahead}`);
return { ...entry, actualBranch, ahead, safeToRemove: problems.length === 0, problems };
}