Rank, check and edit the plan at the paths the project's config names

This commit is contained in:
2026-11-13 00:38:52 +01:00
parent 2ca6fac1d4
commit 0a47b55c93
13 changed files with 246 additions and 87 deletions
+18 -8
View File
@@ -3,6 +3,8 @@ import pathlib
import subprocess
import sys
from project_config.main import located
from project_config.settings import ProjectConfig
from rule_hook.rules import Breach
from failure import Failure
from rule_hook.git import guarded_git
@@ -16,18 +18,26 @@ OWN_CHECK_SECONDS = 20
def todo_breaches(directory: str, own: pathlib.Path = OWN) -> list[Breach]:
"""Why the plan of directory's project is not ranked, where the plan has changed; the plan is the todo.md its config names."""
top = guarded_git(directory, "rev-parse", "--show-toplevel")
if isinstance(top, Failure) or not (pathlib.Path(top) / "todo.md").is_file():
if isinstance(top, Failure):
return []
todo = pathlib.Path(top) / "todo.md"
return [Breach("todo-rank-check", line, top) for line in check_failures(top, todo, own)] if changed(top, todo) else []
config = located(top, pathlib.Path.home())
if isinstance(config, Failure):
# A checkout with a todo.md and no config is one whose config is still to write.
return [Breach("todo-rank-check", config.reason, top)] if (pathlib.Path(top) / "todo.md").is_file() else []
todo, tracked = config.paths["plan"], config.tracked("plan")
# A plan outside the checkout has no commit to differ from, so it is checked on every call.
if not todo.is_file() or (tracked is not None and not changed(top, todo, tracked)):
return []
return [Breach("todo-rank-check", line, top) for line in check_failures(top, todo, config, own)]
def check_failures(top: str, todo: pathlib.Path, own: pathlib.Path) -> list[str]:
def check_failures(top: str, todo: pathlib.Path, config: ProjectConfig, own: pathlib.Path) -> list[str]:
"""Why rank_todo.py --check fails on todo. A checkout of the repo the hook runs from checks with its own bin/rank_todo.py, so a branch that changes the table's shape is held to its own rules. Any other checkout checks with the hook's ranking: running its script would run its code before every commit."""
script = pathlib.Path(top) / "bin" / "rank_todo.py"
if not script.is_file() or not same_repo(top, own):
return plan_file.check_failures(todo)
return plan_file.check_failures(todo, config)
try:
ran = subprocess.run([sys.executable, str(script), "--check", str(todo)], capture_output=True, cwd=top, text=True, timeout=OWN_CHECK_SECONDS)
except subprocess.TimeoutExpired:
@@ -45,10 +55,10 @@ def same_repo(top: str, own: pathlib.Path) -> bool:
return not isinstance(theirs, Failure) and not isinstance(mine, Failure) and bool(set(theirs.split()) & set(mine.split()))
def changed(top: str, todo: pathlib.Path) -> bool:
"""Whether todo.md differs from the index or the index from HEAD: any change, staged or not, since the hook runs before a `git add` earlier in the same command."""
def changed(top: str, todo: pathlib.Path, tracked: str) -> bool:
"""Whether todo, which git names tracked, differs from the index or the index from HEAD: any change, staged or not, since the hook runs before a `git add` earlier in the same command."""
# Hashed here, never by `git status`, which would run a clean filter the checkout's own config names before the call is allowed.
staged, committed = guarded_git(top, "rev-parse", ":todo.md"), guarded_git(top, "rev-parse", "HEAD:todo.md")
staged, committed = guarded_git(top, "rev-parse", f":{tracked}"), guarded_git(top, "rev-parse", f"HEAD:{tracked}")
algorithm = guarded_git(top, "rev-parse", "--show-object-format")
content = todo.read_bytes()
worktree = hashlib.new("sha1" if isinstance(algorithm, Failure) else algorithm, b"blob %d\0" % len(content) + content).hexdigest()