import {
  logger
} from "./chunk-K44MW7JJ.js";

// src/server/branch.api.ts
import { spawn } from "child_process";
import { resolve } from "path";
async function getCurrentBranch(req, res) {
  try {
    const workdir = req.query["workdir"] || process.cwd();
    const resolvedWorkdir = resolve(workdir);
    const branch = await getGitBranch(resolvedWorkdir);
    if (branch) {
      res.json({ branch, workdir: resolvedWorkdir });
    } else {
      res.json({ branch: null, workdir: resolvedWorkdir, error: "Not a git repository" });
    }
  } catch (error) {
    logger.error("Error getting current branch", { error: error instanceof Error ? error.message : String(error) });
    res.status(500).json({ error: "Failed to get branch", branch: null });
  }
}
function getGitBranch(cwd) {
  return new Promise((resolve2) => {
    const proc = spawn("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
      cwd,
      stdio: ["ignore", "pipe", "ignore"]
    });
    let stdout = "";
    proc.stdout.on("data", (data) => {
      stdout += data.toString();
    });
    proc.on("close", (code) => {
      if (code === 0 && stdout.trim()) {
        resolve2(stdout.trim());
      } else {
        resolve2(null);
      }
    });
    proc.on("error", () => {
      resolve2(null);
    });
  });
}
export {
  getCurrentBranch
};
//# sourceMappingURL=branch.api-PWFW26KC.js.map