diff --git a/docs/architecture/scripts/doc-triggers.py b/docs/architecture/scripts/doc-triggers.py index 81b1a55..ba4fc2d 100755 --- a/docs/architecture/scripts/doc-triggers.py +++ b/docs/architecture/scripts/doc-triggers.py @@ -49,7 +49,45 @@ import re import subprocess import sys -ROOT = pathlib.Path(__file__).resolve().parents[3] +def _find_root() -> pathlib.Path: + """The repository root, found rather than assumed. + + This was `parents[3]`, which is correct only while the script sits at + `docs/architecture/scripts/` — its home in the template. The moment a + project copies it to `scripts/`, as the template's own adoption + instructions say to, `parents[3]` climbs out of the repository entirely: in + a checkout at `~/Projects/thing/scripts/`, it resolves to `~/`, and the + script reports "no docs/ directory" about a directory two levels above the + project it was run in. + + So: walk up from the script looking for a directory that has both `docs/` + and `.git`, then fall back to either alone, then to git's own answer. + """ + here = pathlib.Path(__file__).resolve() + + for parent in here.parents: + if (parent / "docs").is_dir() and (parent / ".git").exists(): + return parent + + for parent in here.parents: + if (parent / "docs").is_dir(): + return parent + + result = subprocess.run( + ["git", "rev-parse", "--show-toplevel"], + cwd=here.parent, + capture_output=True, + text=True, + check=False, + ) + + if result.returncode == 0 and result.stdout.strip(): + return pathlib.Path(result.stdout.strip()) + + return here.parents[1] + + +ROOT = _find_root() DOCS = ROOT / "docs" # The status header is a fenced block immediately after the H1, and a long value