Infra: unify git root discovery

This commit is contained in:
Gustavo Madeira Santana
2026-02-18 00:45:38 -05:00
parent 639d0221ff
commit 7ea7b7e7af
4 changed files with 134 additions and 51 deletions

View File

@@ -1,6 +1,7 @@
import fs from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
import { resolveGitHeadPath } from "./git-root.js";
const formatCommit = (value?: string | null) => {
if (!value) {
@@ -13,35 +14,6 @@ const formatCommit = (value?: string | null) => {
return trimmed.length > 7 ? trimmed.slice(0, 7) : trimmed;
};
const resolveGitHead = (startDir: string) => {
let current = startDir;
for (let i = 0; i < 12; i += 1) {
const gitPath = path.join(current, ".git");
try {
const stat = fs.statSync(gitPath);
if (stat.isDirectory()) {
return path.join(gitPath, "HEAD");
}
if (stat.isFile()) {
const raw = fs.readFileSync(gitPath, "utf-8");
const match = raw.match(/gitdir:\s*(.+)/i);
if (match?.[1]) {
const resolved = path.resolve(current, match[1].trim());
return path.join(resolved, "HEAD");
}
}
} catch {
// ignore missing .git at this level
}
const parent = path.dirname(current);
if (parent === current) {
break;
}
current = parent;
}
return null;
};
let cachedCommit: string | null | undefined;
const readCommitFromPackageJson = () => {
@@ -102,7 +74,7 @@ export const resolveCommitHash = (options: { cwd?: string; env?: NodeJS.ProcessE
return cachedCommit;
}
try {
const headPath = resolveGitHead(options.cwd ?? process.cwd());
const headPath = resolveGitHeadPath(options.cwd ?? process.cwd());
if (!headPath) {
cachedCommit = null;
return cachedCommit;