// Planu — git diff parser for merge risk assessor
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
/**
 * Runs `git diff --stat {baseBranch}...{branch}` and parses the output.
 * Uses execFile (not exec) to prevent shell injection.
 */
export async function getDiffStats(projectPath, branch, baseBranch) {
    const { stdout } = await execFileAsync('git', ['diff', '--stat', `${baseBranch}...${branch}`], {
        cwd: projectPath,
    });
    return parseDiffStatOutput(stdout);
}
/**
 * Parses the output of `git diff --stat` into structured stats.
 * Example output:
 *   src/foo.ts | 10 +++--
 *   src/bar.ts |  4 +
 *   2 files changed, 11 insertions(+), 4 deletions(-)
 */
export function parseDiffStatOutput(output) {
    const lines = output.trim().split('\n').filter(Boolean);
    if (lines.length === 0) {
        return { filesChanged: 0, linesAdded: 0, linesRemoved: 0, files: [] };
    }
    const files = [];
    for (const line of lines) {
        // File lines contain a pipe character
        if (line.includes('|')) {
            const filePart = line.split('|')[0];
            if (filePart !== undefined) {
                const fileName = filePart.trim();
                if (fileName) {
                    files.push(fileName);
                }
            }
        }
    }
    // Summary line: "N files changed, X insertions(+), Y deletions(-)"
    const summaryLine = lines[lines.length - 1] ?? '';
    const filesMatch = /(\d+) files? changed/.exec(summaryLine);
    const insertionsMatch = /(\d+) insertions?/.exec(summaryLine);
    const deletionsMatch = /(\d+) deletions?/.exec(summaryLine);
    const filesChanged = filesMatch?.[1] ? parseInt(filesMatch[1], 10) : files.length;
    const linesAdded = insertionsMatch?.[1] ? parseInt(insertionsMatch[1], 10) : 0;
    const linesRemoved = deletionsMatch?.[1] ? parseInt(deletionsMatch[1], 10) : 0;
    return { filesChanged, linesAdded, linesRemoved, files };
}
//# sourceMappingURL=git-diff-parser.js.map