import { execSync } from "child_process"; import { join } from "path"; export interface GitContext { available: boolean; repoRoot: string | null; branch: string | null; recentCommit: string | null; status: string | null; } export function collectGitContext(workspaceRoot: string): GitContext { try { const gitDir = execSync("git rev-parse --show-toplevel 2>/dev/null", { cwd: workspaceRoot, encoding: "utf-8", timeout: 5000, }).trim(); if (!gitDir) { return { available: false, repoRoot: null, branch: null, recentCommit: null, status: null }; } const branch = execSync("git rev-parse --abbrev-ref HEAD 2>/dev/null", { cwd: gitDir, encoding: "utf-8", timeout: 5000, }).trim(); const recentCommit = execSync("git log -1 --oneline 2>/dev/null", { cwd: gitDir, encoding: "utf-8", timeout: 5000, }).trim(); let status: string | null = null; try { const raw = execSync("git status --short 2>/dev/null", { cwd: gitDir, encoding: "utf-8", timeout: 5000, }).trim(); if (raw) { const lines = raw.split("\n").slice(0, 30); status = lines.join("\n"); if (raw.split("\n").length > 30) { status += `\n... ${raw.split("\n").length - 30} more files`; } } } catch { // status unavailable, non-critical } return { available: true, repoRoot: gitDir, branch, recentCommit, status }; } catch { return { available: false, repoRoot: null, branch: null, recentCommit: null, status: null }; } }