Answer architecture round 1: 1 🔴, 2 🟡 (0 misread); read the branch's round titles and default branch one way for rule-hook and round-count

This commit is contained in:
2026-10-28 17:47:41 +01:00
parent 0254e427f2
commit fe975488ec
9 changed files with 75 additions and 42 deletions
+2 -7
View File
@@ -4,17 +4,12 @@ from failure import Failure
import git_repo
def git(directory: str, *args: str, timeout: float | None = None) -> str | Failure:
def git(directory: str, *args: str) -> str | Failure:
"""What git printed in directory, stripped, run guarded: the hook runs git in a checkout the call has not yet been allowed to touch."""
ran = git_repo.git(*args, cwd=directory, timeout=timeout, guarded=True)
ran = git_repo.git(*args, cwd=directory, guarded=True)
return ran if isinstance(ran, Failure) else ran.strip()
def local_default_branch(directory: str) -> str | None:
"""The default branch as the checkout last saw it, with no fetch, which the hook must not run."""
return next((name for name in ("origin/HEAD", "main", "master") if not isinstance(git(directory, "rev-parse", "--verify", "--quiet", name), Failure)), None)
def existing(directory: str) -> str:
path = pathlib.Path(directory or ".").absolute()
while not path.is_dir():
+4 -3
View File
@@ -6,7 +6,8 @@ import sys
import typing
from rule_hook.rules import Breach
from rule_hook.git import existing, git, local_default_branch
from git_repo import default_ref
from rule_hook.git import existing, git
from failure import Failure
from utc_stamp import utc
@@ -100,10 +101,10 @@ def print_overrides(directory: str) -> int:
if unread:
print(f"rule-hook overrides cannot read the branch's reflog, so it cannot tell which overrides are this chunk's: {unread.reason}", file=sys.stderr)
return 1
default = local_default_branch(directory)
default = default_ref(directory, guarded=True)
log = pathlib.Path(common) / LOG
entries = [parsed_entry(line) for line in log.read_text(encoding="utf-8").splitlines()] if log.is_file() else []
on_default = (lambda commit: holds(directory, default, commit)) if default else (lambda commit: False)
on_default = (lambda commit: False) if isinstance(default, Failure) else (lambda commit: holds(directory, default, commit))
for entry in entries:
if entry and this_chunks(entry, set(str(reflog).split()), branch, on_default):
print(entry.line(), end="")
+11 -12
View File
@@ -1,25 +1,24 @@
from failure import Failure
from git_command import Commit
from round_titles import CAP, past_cap, ROUND
from rule_hook.git import git, local_default_branch
from rule_hook.rules import Breach
from round_titles import CAP, past_cap, ROUND
from git_command import Commit
from failure import Failure
import git_repo
def round_cap_breaches(commit: Commit) -> list[Breach]:
"""A breach where the commit's title takes its pass past the cap, counted with the branch's earlier round commits."""
if not any(ROUND.match(message) for message in commit.messages):
return []
earlier = branch_titles(commit.directory, commit.amends)
earlier = earlier_titles(commit.directory, commit.amends)
return [Breach("round-cap", f"the pass has had its {CAP} rounds; merge once the gate passes, with no round after them", commit.directory)] if any(past_cap(message, earlier) for message in commit.messages) else []
def branch_titles(directory: str, amends: bool) -> list[str]:
"""The titles of the branch's own commits, newest first, less the one an --amend replaces; none where git cannot tell in time, so the title's own number still counts.
def earlier_titles(directory: str, amends: bool) -> list[str]:
"""The branch's titles, less the one an --amend replaces; none where git cannot tell in time, so the title's own number still counts.
--first-parent leaves out the default branch's commits merged into the branch, which a stale local default branch would otherwise count as this chunk's rounds."""
default = local_default_branch(directory)
titles = git(directory, "log", "--first-parent", "--format=%s", f"{default}..HEAD", timeout=5) if default else Failure("no default branch")
Run guarded, as the hook runs git, which neither fetches nor asks origin: the default branch is as last fetched."""
default = git_repo.default_ref(directory, guarded=True)
titles = default if isinstance(default, Failure) else git_repo.branch_titles(default, directory, timeout=5, guarded=True)
if isinstance(titles, Failure):
return []
listed = titles.splitlines()
return listed[1:] if amends else listed
return titles[1:] if amends else titles
+13 -3
View File
@@ -55,14 +55,15 @@ class Commits(unittest.TestCase):
self.assertEqual(committed("cd a && git -c x=y -C b commit -m x")[0].directory, "/r/a/b")
def test_each_message_spelling_is_read(self):
self.assertEqual(commit_message(["--message=a", "-m", "b=c", "--message", "c", "-mz", "-am", "d"]), (["a", "b=c", "c", "z", "d"], False))
self.assertEqual(commit_message(["--message=a", "-m", "b=c", "--message", "c", "-mz", "-am", "d"]), (["a", "b=c", "c", "z", "d"], False, False))
def test_an_amend_is_seen(self):
self.assertEqual([commit.amends for commit in committed("git commit --amend -m x && git commit -m y")], [True, False])
self.assertEqual(commit_message(["--", "--amend"]), ([], False, False), "after --, --amend names a path")
def test_a_file_message_is_seen(self):
self.assertEqual(commit_message(["-F", "-"]), ([], True))
self.assertEqual(commit_message(["--file=x"]), ([], True))
self.assertEqual(commit_message(["-F", "-"]), ([], True, False))
self.assertEqual(commit_message(["--file=x"]), ([], True, False))
def test_other_git_commands_are_no_commit(self):
self.assertEqual(committed("git log --grep commit; echo git commit"), [])
@@ -681,6 +682,15 @@ class Hook(unittest.TestCase):
git(self.repo, "branch", "-f", "main", "main~2")
self.assertEqual(self.round_commit("Answer stability round 1: 0 🔴, 1 🟡 (0 misread); c").returncode, 0, "a stale default branch still leaves another chunk's rounds out")
def test_a_stale_local_main_does_not_stand_in_for_a_default_branch_origin_left_unrecorded(self):
git(self.repo, "checkout", "-q", "main")
for title in ["Answer stability round 1: 0 🔴, 1 🟡 (0 misread); a", "Answer stability round 2: 0 🔴, 1 🟡 (0 misread); b"]:
git(self.repo, "commit", "-q", "--allow-empty", "-m", title)
git(self.repo, "checkout", "-q", "-B", "chunk", "main")
git(self.repo, "branch", "-f", "main", "main~2")
git(self.repo, "remote", "add", "origin", f"{self.repo}/gone.git")
self.assertEqual(self.round_commit("Answer stability round 1: 0 🔴, 1 🟡 (0 misread); c").returncode, 0, "another chunk's rounds, fast-forwarded into the default branch, are not this chunk's")
def test_a_commit_while_an_unranked_todo_has_changes_is_blocked(self):
pathlib.Path(self.repo, "docs").mkdir()
for name, content in [("README.md", README), ("docs/decisions.md", DECISIONS), ("todo.md", TODO)]: