// src/lib/gitConfig.ts
import { execFileSync } from "node:child_process";
function getGitConfig(key) {
  try {
    const value = execFileSync("git", ["config", "--get", key], {
      encoding: "utf8",
      stdio: ["ignore", "pipe", "ignore"]
    }).trim();
    return value.length > 0 ? value : void 0;
  } catch {
    return void 0;
  }
}
function getGitConfigBool(key) {
  try {
    const value = execFileSync("git", ["config", "--bool", "--get", key], {
      encoding: "utf8",
      stdio: ["ignore", "pipe", "ignore"]
    }).trim();
    if (value === "true") return true;
    if (value === "false") return false;
    return void 0;
  } catch {
    return void 0;
  }
}
function setGitConfig(key, value) {
  execFileSync("git", ["config", key, value], { stdio: "inherit" });
}

// src/lib/reporting/runWithEnvelope.ts
import { performance as performance2 } from "node:perf_hooks";

// src/lib/reporting/xyReporter.ts
import { performance } from "node:perf_hooks";
var jsonMode = false;
var strictMode = false;
var originalStdoutWrite;
var envelopeEmitted = false;
var currentCommand = "unknown";
var startTime = 0;
function isJsonMode() {
  return jsonMode;
}
function isStrictMode() {
  return strictMode || process.env.XY_STRICT === "1";
}
function setCurrentCommand(name) {
  currentCommand = name;
  startTime = performance.now();
  envelopeEmitted = false;
}
function emitJson(envelope) {
  if (envelopeEmitted) return;
  envelopeEmitted = true;
  const json = JSON.stringify(envelope) + "\n";
  if (originalStdoutWrite) {
    originalStdoutWrite(json);
  } else {
    process.stdout.write(json);
  }
}

// src/lib/reporting/runWithEnvelope.ts
async function runWithEnvelope(command, action) {
  setCurrentCommand(command);
  const start = performance2.now();
  let exitCode;
  let envelope;
  try {
    const ret = await action();
    if (typeof ret === "number") {
      exitCode = ret;
    } else {
      exitCode = ret.exitCode;
      envelope = ret.envelope;
    }
  } catch (ex) {
    exitCode = 1;
    const message = ex instanceof Error ? ex.message : String(ex);
    envelope = {
      errorCount: 1,
      errors: [{ message }]
    };
    if (!isJsonMode()) {
      console.error(message);
    }
  }
  if (isJsonMode()) {
    emitJson({
      command,
      data: envelope?.data,
      durationMs: Math.round(performance2.now() - start),
      errorCount: envelope?.errorCount ?? (exitCode === 0 ? 0 : 1),
      errors: envelope?.errors,
      exitCode,
      schemaVersion: 1,
      success: exitCode === 0,
      warningCount: envelope?.warningCount ?? 0,
      warnings: envelope?.warnings
    });
  }
  return exitCode;
}
async function withEnvelope(command, action) {
  process.exitCode = await runWithEnvelope(command, action);
}

// src/actions/gitlint.ts
import chalk from "chalk";
function gitlint() {
  console.log(`
Gitlint Start [${process.cwd()}]
`);
  let valid = 0;
  let warnings = 0;
  const errors = 0;
  const warn = (message) => {
    console.warn(chalk.yellow(`Warning: ${message}`));
    warnings++;
  };
  if (getGitConfigBool("core.ignorecase") === true) {
    warn("Please set core.ignorecase to FALSE in .git/config file [run yarn gitlint-fix]");
  } else {
    valid++;
  }
  if (getGitConfigBool("core.autocrlf") === false) {
    valid++;
  } else {
    warn("Please set core.autocrlf to FALSE in .git/config file [run yarn gitlint-fix]");
  }
  if (getGitConfig("core.eol") === "lf") {
    valid++;
  } else {
    warn('Please set core.eol to "lf" in .git/config file [run yarn gitlint-fix]');
  }
  const resultMessages = [];
  if (valid > 0) {
    resultMessages.push(chalk.green(`Passed: ${valid}`));
  }
  if (warnings > 0) {
    resultMessages.push(chalk.yellow(`Warnings: ${warnings}`));
  }
  if (errors > 0) {
    resultMessages.push(chalk.red(` Errors: ${errors}`));
  }
  console.warn(`Gitlint Finish [ ${resultMessages.join(" | ")} ]
`);
  return errors + (isStrictMode() ? warnings : 0);
}

// src/actions/gitlint-fix.ts
import chalk2 from "chalk";
function gitlintFix() {
  console.log(`
Gitlint Fix Start [${process.cwd()}]
`);
  if (getGitConfigBool("core.ignorecase") === true) {
    setGitConfig("core.ignorecase", "false");
    console.warn(chalk2.yellow("\nGitlint Fix: Updated core.ignorecase to be false\n"));
  }
  if (getGitConfigBool("core.autocrlf") !== false) {
    setGitConfig("core.autocrlf", "false");
    console.warn(chalk2.yellow("\nGitlint Fix: Updated core.autocrlf to be false\n"));
  }
  if (getGitConfig("core.eol") !== "lf") {
    setGitConfig("core.eol", "lf");
    console.warn(chalk2.yellow('\nGitlint Fix: Updated core.eol to be "lf"\n'));
  }
  return 0;
}

// src/xy/common/gitlintCommand.ts
var gitlintCommand = {
  builder: (yargs) => {
    return yargs.option("fix", {
      default: false,
      description: "Auto-fix git config issues",
      type: "boolean"
    });
  },
  command: "gitlint",
  describe: "Gitlint - Lint your git config",
  handler: async (argv) => {
    if (argv.verbose) console.log("Gitlint");
    await withEnvelope("gitlint", () => argv.fix ? gitlintFix() : gitlint());
  }
};
export {
  gitlintCommand
};
//# sourceMappingURL=gitlintCommand.mjs.map
