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
+25
View File
@@ -889,6 +889,7 @@ class TodoRankCheck(unittest.TestCase):
pathlib.Path(checkout, "bin").mkdir()
pathlib.Path(checkout, "bin", "rank_todo.py").write_text(stub)
pathlib.Path(checkout, "todo.md").write_text("# todo\n")
self.config = configured(self, pathlib.Path(checkout)) / "config.toml"
return checkout
def details(self, checkout: str) -> list[str]:
@@ -915,6 +916,30 @@ class TodoRankCheck(unittest.TestCase):
self.assertTrue(reasons)
self.assertNotIn("own script", " ".join(reasons))
def test_a_plan_the_config_puts_outside_the_checkout_is_checked_in_place_of_the_checkouts_todo_md(self):
checkout = self.changed_plan(f"{self.folder}/clone", self.STUB)
self.config.write_text(self.config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"'))
self.assertEqual(self.details(checkout), [], "the project has no plan yet, and the checkout's todo.md is its owners'")
(self.config.parent / "todo.md").write_text("# todo\n")
self.assertEqual(self.details(checkout), [f"own script checked {self.config.parent}/todo.md"])
def test_a_plan_in_a_folder_of_the_checkout_is_checked_once_it_changes(self):
checkout = self.changed_plan(f"{self.folder}/clone", self.STUB)
self.config.write_text(self.config.read_text().replace('"checkout:todo.md"', '"checkout:plan/todo.md"'))
pathlib.Path(checkout, "plan").mkdir()
pathlib.Path(checkout, "plan", "todo.md").write_text("# todo\n")
self.assertEqual(self.details(checkout), [f"own script checked {checkout}/plan/todo.md"])
git(checkout, "add", "plan")
git(checkout, "commit", "-q", "-m", "plan")
self.assertEqual(self.details(checkout), [], "committed, so unchanged")
def test_a_checkout_with_a_todo_md_and_no_config_is_told_to_write_one(self):
checkout = self.changed_plan(self.other, self.STUB)
self.config.unlink()
self.assertEqual(self.details(checkout), [f"no config names the checkout {checkout}: write ~/.claude-projects/<key>/config.toml, as `project-config --help` shows"])
pathlib.Path(checkout, "todo.md").unlink()
self.assertEqual(self.details(checkout), [])
class CommittedFiles(unittest.TestCase):
"""A git commit holds the files it would commit to the file rules, as an Edit or Write of each would be."""
+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()