import { exec } from "child_process";
import { promisify } from "util";
import { MergeConflictError, MergeFromBaseConflictError, NotGitRepoError, } from "../utils/checkout-git.js";
const execAsync = promisify(exec);
export const READ_ONLY_GIT_ENV = {
    ...process.env,
    GIT_OPTIONAL_LOCKS: "0",
};
export async function resolveCheckoutGitDir(cwd) {
    try {
        const { stdout } = await execAsync("git rev-parse --absolute-git-dir", {
            cwd,
            env: READ_ONLY_GIT_ENV,
        });
        const gitDir = stdout.trim();
        return gitDir.length > 0 ? gitDir : null;
    }
    catch {
        return null;
    }
}
export function toCheckoutError(error) {
    if (error instanceof NotGitRepoError) {
        return { code: "NOT_GIT_REPO", message: error.message };
    }
    if (error instanceof MergeConflictError) {
        return { code: "MERGE_CONFLICT", message: error.message };
    }
    if (error instanceof MergeFromBaseConflictError) {
        return { code: "MERGE_CONFLICT", message: error.message };
    }
    if (error instanceof Error) {
        return { code: "UNKNOWN", message: error.message };
    }
    return { code: "UNKNOWN", message: String(error) };
}
//# sourceMappingURL=checkout-git-utils.js.map