(opts: RepoRootWithDeps)
| 286 | * finding a `.git`. |
| 287 | */ |
| 288 | export async function repoRootWith(opts: RepoRootWithDeps): Promise<string> { |
| 289 | const start = normalizeAbsolute(opts.dir ?? "/"); |
| 290 | const fs = opts.fs as StatFsClient; |
| 291 | let current = start; |
| 292 | while (true) { |
| 293 | const gitPath = current === "/" ? "/.git" : `${current}/.git`; |
| 294 | try { |
| 295 | await fs.promises.stat(gitPath); |
| 296 | return current; |
| 297 | } catch { |
| 298 | // Not here; climb one level. |
| 299 | } |
| 300 | if (current === "/") break; |
| 301 | const slash = current.lastIndexOf("/"); |
| 302 | current = slash <= 0 ? "/" : current.slice(0, slash); |
| 303 | } |
| 304 | throw new NotARepositoryError(start); |
| 305 | } |
| 306 | |
| 307 | function normalizeAbsolute(p: string): string { |
| 308 | // Collapse a trailing slash (except the root) so the parent |
no test coverage detected