18 lines
620 B
Python
18 lines
620 B
Python
"""项目路径解析工具。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
def find_project_root(start: str | Path | None = None) -> Path:
|
|
"""从任意子目录向上查找同时包含 AGENTS.md 与 .git 的项目根。"""
|
|
|
|
candidate = Path(start or __file__).expanduser().resolve()
|
|
if candidate.is_file():
|
|
candidate = candidate.parent
|
|
for directory in (candidate, *candidate.parents):
|
|
if (directory / "AGENTS.md").is_file() and (directory / ".git").exists():
|
|
return directory
|
|
raise FileNotFoundError(f"无法从 {candidate} 向上找到项目根")
|