import { log } from "../../deps.js";
import { execSync } from "node:child_process";
import { WM_FORK_PREFIX } from "../main.js";
export function getCurrentGitBranch() {
    try {
        const result = execSync("git rev-parse --abbrev-ref HEAD", {
            encoding: "utf8",
            stdio: "pipe"
        });
        const branch = result.trim();
        return branch || null;
    }
    catch (error) {
        log.debug(`Failed to get Git branch: ${error}`);
        return null;
    }
}
export function getOriginalBranchForWorkspaceForks(branchName) {
    if (!branchName || !branchName.startsWith(WM_FORK_PREFIX)) {
        return null;
    }
    const start = branchName.indexOf("/") + 1;
    const end = branchName.lastIndexOf("/");
    if (start < 0 || end < 0 || end - start <= 0) {
        return null;
    }
    return branchName.slice(start, end);
}
export function getWorkspaceIdForWorkspaceForkFromBranchName(branchName) {
    if (!branchName.startsWith(WM_FORK_PREFIX)) {
        return null;
    }
    const start = branchName.lastIndexOf("/") + 1;
    if (start < 0) {
        return null;
    }
    return `${WM_FORK_PREFIX}-${branchName.slice(start)}`;
}
export function isGitRepository() {
    try {
        execSync("git rev-parse --git-dir", {
            encoding: "utf8",
            stdio: "pipe"
        });
        return true;
    }
    catch (error) {
        log.debug(`Failed to check Git repository: ${error}`);
        return false;
    }
}
