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 path from "node:path";
import type { OpenClawConfig } from "../config/config.js";
import { findGitRoot } from "../infra/git-root.js";
import {
formatUserTime,
resolveUserTimeFormat,
@@ -92,24 +93,3 @@ function resolveRepoRoot(params: {
}
return undefined;
}
function findGitRoot(startDir: string): string | null {
let current = path.resolve(startDir);
for (let i = 0; i < 12; i += 1) {
const gitPath = path.join(current, ".git");
try {
const stat = fs.statSync(gitPath);
if (stat.isDirectory() || stat.isFile()) {
return current;
}
} catch {
// ignore missing .git at this level
}
const parent = path.dirname(current);
if (parent === current) {
break;
}
current = parent;
}
return null;
}