Lukasz Boryn 38eae57e6f refactor(lints): walk only what git does not ignore
link-check and render-check each held a byte-identical pages() that
walked the filesystem, so gitignored HANDOFF.md could redden a repo-wide
gate — 024 had to carry "clear HANDOFF.md first" as a run condition, and
a handoff had to be written link-free.

Both now import a shared pages() from _pages.py, whose census comes from
git ls-files --cached --others --exclude-standard: tracked plus
untracked-but-not-ignored is exactly not-ignored, so a page is walked
before it is ever added. Where git cannot answer — not a repo, no git on
PATH — the walk falls back to the old rglob rather than shrinking to
nothing, and the catch is narrow so an unnamed git failure still
surfaces.

023 and 019 each state the old walk in their Contract, so both take
superseded-by-026; their Contract and Verify are untouched, per 022.
023 also cited the docstring the walk moved out of, so that cite follows
it to _pages.py — a cite is a working pointer at every status, per 022 —
and _pages.py words that docstring on one line, because a cite symbol
that wraps never resolves.

026 passed its muster gate (matches: yes) and flips to implemented. Its
Justification loses three lines to fit the tally: block; its Contract and
Verify are byte-identical.

blueprint 026
2026-07-31 16:53:11 +02:00

57 lines
2.1 KiB
Python

"""The lints' shared walk over the repo's markdown pages.
A helper, not a fifth lint: no `main`, no argv contract. `link-check` and
`render-check` held a byte-identical `pages()` each; ruling the walk for
one and leaving its twin is the shape this repo has mechanised against
three times, so the walk lives here and both import it.
A page git ignores is not the repo's to gate. `HANDOFF.md` is written
per session and gitignored, and a walk that reads it lets scratch prose
redden a repo-wide check.
"""
import subprocess
from pathlib import Path
SKIP = ("OKF/blueprints/",)
def unignored(root):
"""Every `*.md` git does not ignore, relative to the root.
`--cached --others --exclude-standard` is tracked plus
untracked-but-not-ignored, which is exactly *not ignored*: a page is
walked before it is ever added, so this is a `.md` census and not a
staging gate.
Returns None where git cannot answer — not a repo, no git on PATH.
The caller falls back to the whole tree rather than shrinking to
nothing: a lint that goes quiet is worse than one that over-reports.
The catch is narrow for the same reason, so a git failure this does
not name still surfaces instead of passing as a silent full walk.
"""
try:
listed = subprocess.run(
["git", "-C", str(root), "ls-files", "-z", "--cached",
"--others", "--exclude-standard", "--", "*.md"],
capture_output=True, text=True, check=True).stdout
except (OSError, subprocess.CalledProcessError):
return None
return {Path(name) for name in listed.split("\0") if name}
def pages(root):
"""Every `*.md` under the root that git does not ignore.
Sorted, dot-directories and blueprints out, as `(page, rel)`.
"""
live = unignored(root)
for page in sorted(root.rglob("*.md")):
rel = page.relative_to(root)
if live is not None and rel not in live:
continue
if any(part.startswith(".") for part in rel.parts):
continue
if rel.as_posix().startswith(SKIP):
continue
yield page, rel