import { execSync } from "node:child_process";

//#region src/utils/git_metadata.ts
/**
* Git metadata auto-capture utility.
*
* Resolves git SHA, ref (branch), and repo URL from environment variables
* or by shelling out to git. Results are cached for the process lifetime.
*/
function runGitCommand(args) {
	try {
		return execSync(["git", ...args].join(" "), {
			timeout: 2e3,
			stdio: [
				"ignore",
				"pipe",
				"ignore"
			],
			encoding: "utf-8"
		}).trim() || null;
	} catch {
		return null;
	}
}
function resolveGitSha() {
	return process.env.AMPLITUDE_GIT_SHA ?? process.env.GIT_SHA ?? runGitCommand(["rev-parse", "HEAD"]);
}
function resolveGitRef() {
	return process.env.AMPLITUDE_GIT_REF ?? process.env.GIT_REF ?? runGitCommand([
		"symbolic-ref",
		"--short",
		"HEAD"
	]);
}
function resolveGitRepo() {
	return process.env.AMPLITUDE_GIT_REPO ?? process.env.GIT_REPO ?? runGitCommand([
		"remote",
		"get-url",
		"origin"
	]);
}
let cachedMetadata = null;
function getGitMetadata() {
	if (cachedMetadata == null) {
		cachedMetadata = {};
		const sha = resolveGitSha();
		if (sha) cachedMetadata.gitSha = sha;
		const ref = resolveGitRef();
		if (ref) cachedMetadata.gitRef = ref;
		const repo = resolveGitRepo();
		if (repo) cachedMetadata.gitRepo = repo;
	}
	return { ...cachedMetadata };
}
/** @internal Reset the cache. For test isolation only. */
function _resetCache() {
	cachedMetadata = null;
}

//#endregion
export { _resetCache, getGitMetadata };
//# sourceMappingURL=git_metadata.js.map