mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-17 13:00:48 +00:00
Infra: unify git root discovery
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user