import { p as path, U as Uri, s as spawn } from "./agent.worker-C43kSzdJ.mjs";
import { w as wrapInActiveSpan, C as ContextItemSource } from "./browser-C8qAwvsz.mjs";
async function getContextFileFromGitLog(file, options) {
  return wrapInActiveSpan("commands.context.git-log", async (span) => {
    const cwd = path.dirname(file.fsPath);
    const args = ["log", `-L:${options.funcname}:${file.fsPath}`, `--max-count=${options.maxCount}`];
    const result = await spawnAsync("git", args, { cwd });
    if (result.code !== 0) {
      if (result.stderr.includes("no match")) {
        return [];
      }
      throw new Error(`git log failed with exit code ${result.code}: ${result.stderr}`);
    }
    return [
      {
        type: "file",
        content: result.stdout,
        title: "Terminal Output",
        uri: Uri.file("terminal-output"),
        source: ContextItemSource.History
      }
    ];
  });
}
async function spawnAsync(command, args, opts) {
  const childProcess = spawn();
  let stdout = "";
  let stderr = "";
  childProcess.stdout.on("data", (data) => {
    stdout += data.toString();
  });
  childProcess.stderr.on("data", (data) => {
    stderr += data.toString();
  });
  let code = -1;
  await new Promise((resolve, reject) => {
    childProcess.on("close", (returnCode) => {
      if (returnCode === null) {
        reject(new Error("Process closed with null return code"));
      } else {
        code = returnCode;
        resolve();
      }
    });
  });
  return {
    stdout,
    stderr,
    code
  };
}
export {
  getContextFileFromGitLog
};
