
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="c1328ea8-0a40-5b06-a427-f81249f30600")}catch(e){}}();
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { createGitProcessEnv } from "../core/git-process-env.js";
const execFileAsync = promisify(execFile);
const GIT_MAX_BUFFER_BYTES = 10 * 1024 * 1024;
function normalizeProcessExitCode(code) {
    if (typeof code === "number" && Number.isFinite(code)) {
        return code;
    }
    if (typeof code === "string") {
        const parsed = Number(code);
        if (Number.isInteger(parsed)) {
            return parsed;
        }
    }
    return -1;
}
export async function runGit(cwd, args, options = {}) {
    try {
        const { stdout, stderr } = await execFileAsync("git", args, {
            cwd,
            encoding: "utf8",
            maxBuffer: GIT_MAX_BUFFER_BYTES,
            env: options.env || createGitProcessEnv(),
        });
        const normalizedStdout = String(stdout ?? "").trim();
        const normalizedStderr = String(stderr ?? "").trim();
        return {
            ok: true,
            stdout: options.trimStdout === false ? stdout : normalizedStdout,
            stderr: normalizedStderr,
            output: [normalizedStdout, normalizedStderr].filter(Boolean).join("\n"),
            error: null,
            exitCode: 0,
        };
    }
    catch (error) {
        const candidate = error;
        const rawStdout = String(candidate.stdout ?? "");
        const stdout = options.trimStdout === false ? rawStdout : rawStdout.trim();
        const stderr = String(candidate.stderr ?? "").trim();
        const message = String(candidate.message ?? "").trim();
        const command = `git ${args.join(" ")} failed`;
        const errorMessage = `Failed to run Git Command: \n Command: \n ${command} \n ${stderr || message}`;
        const exitCode = normalizeProcessExitCode(candidate.code);
        return {
            ok: false,
            stdout,
            stderr,
            output: [stdout, stderr].filter(Boolean).join("\n"),
            error: errorMessage,
            exitCode,
        };
    }
}
export async function getGitStdout(args, cwd, options = {}) {
    const result = await runGit(cwd, args, options);
    if (!result.ok) {
        throw new Error(result.error || result.stdout);
    }
    return result.stdout;
}
/**
 * Read the current HEAD commit, branch name, and detached state for a
 * repository (or worktree) at `cwd`.
 */
export async function readGitHeadInfo(cwd) {
    const headResult = await runGit(cwd, ["rev-parse", "--verify", "HEAD"]);
    const headCommit = headResult.ok ? headResult.stdout : null;
    const branchResult = await runGit(cwd, ["symbolic-ref", "--quiet", "--short", "HEAD"]);
    const branch = branchResult.ok ? branchResult.stdout : null;
    return {
        branch,
        headCommit,
        isDetached: headCommit !== null && branch === null,
    };
}
export function getGitCommandErrorMessage(error) {
    if (error && typeof error === "object" && "stderr" in error) {
        const stderr = error.stderr;
        if (typeof stderr === "string" && stderr.trim()) {
            return stderr.trim();
        }
    }
    return error instanceof Error ? error.message : String(error);
}
//# sourceMappingURL=git-utils.js.map
//# debugId=c1328ea8-0a40-5b06-a427-f81249f30600
