"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_primordials_map_set = require('../primordials/map-set.js');
const require_node_fs = require('../node/fs.js');
const require_node_path = require('../node/path.js');
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process);

//#region src/git/repo.ts
/**
* @file Git repository discovery + foundational lazy fs/path/cwd helpers shared
*   across `git/*` leaves. Owns `findGitRoot`, the realpath cache, the cwd
*   resolver, and the lazy `node:fs` / `node:path` loaders — pulling these
*   together keeps the dependency direction one-way: `_internal.ts` and the
*   public-surface leaves all import from here.
*/
const realpathCache = new require_primordials_map_set.MapCtor();
const gitRootCache = new require_primordials_map_set.MapCtor();
/**
* Find git repository root by walking up from the given directory.
*
* Searches for a `.git` directory or file by traversing parent directories
* upward until found or filesystem root is reached. Returns the original path
* if no git repository is found.
*
* This function is exported primarily for testing purposes.
*
* @example
*   ;```typescript
*   const root = findGitRoot('/path/to/repo/src/subdir')
*   // => '/path/to/repo'
*
*   const notFound = findGitRoot('/not/a/repo')
*   // => '/not/a/repo'
*   ```
*
* @param startPath - Directory path to start searching from.
*
* @returns Git repository root path, or `startPath` if not found.
*/
function findGitRoot(startPath) {
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	/* c8 ignore start */
	const cached = gitRootCache.get(startPath);
	if (cached) {
		if (fs.existsSync(path.join(cached, ".git"))) return cached;
		gitRootCache.delete(startPath);
	}
	/* c8 ignore stop */
	let currentPath = startPath;
	while (true) {
		try {
			const gitPath = path.join(currentPath, ".git");
			if (fs.existsSync(gitPath)) {
				gitRootCache.set(startPath, currentPath);
				return currentPath;
			}
		} catch {}
		const parentPath = path.dirname(currentPath);
		if (parentPath === currentPath) return startPath;
		currentPath = parentPath;
	}
}
/**
* Get the real path with caching to avoid repeated filesystem calls. Validates
* cache with existsSync() which is cheaper than realpathSync().
*
* ENOENT/ENOTDIR are re-thrown because the caller explicitly passed a path they
* expect to exist — swallowing these would turn "file not found" into a silent
* no-op. Other errors (EACCES, EPERM, EIO) fall back to the input path since
* they can happen on container/overlay filesystems where the path exists but
* realpath resolution is restricted.
*/
function getCachedRealpath(pathname) {
	const fs = /* @__PURE__ */ require_node_fs.getNodeFs();
	const cached = realpathCache.get(pathname);
	/* c8 ignore start */
	if (cached) {
		if (fs.existsSync(cached)) return cached;
		realpathCache.delete(pathname);
	}
	/* c8 ignore stop */
	let resolved;
	try {
		resolved = fs.realpathSync(pathname);
	} catch (e) {
		const code = e.code;
		if (code === "ENOENT" || code === "ENOTDIR") throw e;
		resolved = pathname;
	}
	/* c8 ignore stop */
	realpathCache.set(pathname, resolved);
	return resolved;
}
/**
* Get the current working directory for git operations.
*
* Returns the real path to handle symlinks correctly. This is important because
* symlinked directories like `/tmp -> /private/tmp` can cause path mismatches
* when comparing git output.
*
* @example
*   ;```typescript
*   const cwd = getCwd()
*   // In /tmp (symlink to /private/tmp):
*   // => '/private/tmp'
*   ```
*
* @returns The resolved real path of `process.cwd()`.
*/
function getCwd() {
	return getCachedRealpath(node_process.default.cwd());
}

//#endregion
exports.findGitRoot = findGitRoot;
exports.getCachedRealpath = getCachedRealpath;
exports.getCwd = getCwd;
exports.getFs = require_node_fs.getNodeFs;
exports.getPath = require_node_path.getNodePath;
exports.gitRootCache = gitRootCache;
exports.realpathCache = realpathCache;