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
+6 -4
View File
@@ -19,7 +19,7 @@ def commits(simples: list[Simple], home: str) -> list[Commit]:
for simple in simples:
invoked = git_subcommand(simple.words, simple.directory, home)
if invoked and invoked[0] == "commit":
found.append(Commit(invoked[2], *commit_message(invoked[1]), "--amend" in invoked[1]))
found.append(Commit(invoked[2], *commit_message(invoked[1])))
return found
@@ -37,14 +37,16 @@ def git_subcommand(words: list[str], directory: str, home: str) -> tuple[str, li
return None
def commit_message(args: list[str]) -> tuple[list[str], bool]:
"""git commit's messages, and whether it reads one from a file."""
def commit_message(args: list[str]) -> tuple[list[str], bool, bool]:
"""git commit's messages, whether it reads one from a file, and whether it amends."""
messages = []
from_file = False
amends = False
words = iter(args)
for word in words:
if word == "--":
break
amends = amends or word == "--amend"
name, equals, value = word.partition("=")
if name in ("--message", "--file"):
given = value if equals else next(words, "")
@@ -56,4 +58,4 @@ def commit_message(args: list[str]) -> tuple[list[str], bool]:
continue
messages += [given] if name == "--message" else []
from_file = from_file or name == "--file"
return messages, from_file
return messages, from_file, amends
+18 -6
View File
@@ -60,20 +60,32 @@ def default_branch(stale_ok: bool, timeout: float | None = None) -> tuple[str, s
return ref if isinstance(ref, Failure) else (ref, note)
def default_ref(cwd: str | None = None, timeout: float | None = None) -> str | Failure:
"""The default branch's ref as last fetched, read in cwd, else in the current directory: `origin/<branch>` with that remote, else the local main or master. Asking origin, where the checkout records no default branch for it, fails after timeout seconds."""
remotes = git("remote", cwd=cwd)
def default_ref(cwd: str | None = None, timeout: float | None = None, guarded: bool = False) -> str | Failure:
"""The default branch's ref as last fetched, read in cwd, else in the current directory: `origin/<branch>` with that remote, else the local main or master.
Where the checkout records no default branch for origin, it asks origin, which fails after timeout seconds. Guarded, it runs git as `git` does with guarded, and fails there instead of asking origin."""
remotes = git("remote", cwd=cwd, guarded=guarded)
if isinstance(remotes, Failure):
return remotes
if "origin" in remotes.split():
recorded = git("symbolic-ref", "--short", "refs/remotes/origin/HEAD", cwd=cwd)
return recorded.strip() if not isinstance(recorded, Failure) else origin_head(cwd, timeout)
recorded = git("symbolic-ref", "--short", "refs/remotes/origin/HEAD", cwd=cwd, guarded=guarded)
if not isinstance(recorded, Failure):
return recorded.strip()
return Failure("origin records no default branch; set it with `git remote set-head origin --auto`") if guarded else origin_head(cwd, timeout)
for branch in ("main", "master"):
if not isinstance(git("rev-parse", "--quiet", "--verify", f"refs/heads/{branch}", cwd=cwd), Failure):
if not isinstance(git("rev-parse", "--quiet", "--verify", f"refs/heads/{branch}", cwd=cwd, guarded=guarded), Failure):
return branch
return Failure("no origin remote, and no main or master branch to take as the default branch")
def branch_titles(default: str, cwd: str | None = None, timeout: float | None = None, guarded: bool = False) -> list[str] | Failure:
"""The titles of the branch's own commits since default, newest first, read in cwd, else in the current directory.
--first-parent leaves out the commits a merge brought in, such as another chunk's rounds merged in from a default branch the checkout last fetched before them."""
titles = git("log", "--first-parent", "--format=%s", f"{default}..HEAD", cwd=cwd, timeout=timeout, guarded=guarded)
return titles if isinstance(titles, Failure) else titles.splitlines()
def origin_head(cwd: str | None, timeout: float | None) -> str | Failure:
"""The default branch origin names, for a clone made by `git init` and `git remote add`, which records none."""
listed = git("ls-remote", "--symref", "origin", "HEAD", cwd=cwd, timeout=timeout)
+8 -7
View File
@@ -22,10 +22,11 @@ measured commit is the default branch's newest merged change that adds a paired
run to the history; a merge commit counts as one change against its first parent. With none, it is
the merge base of HEAD and the default branch.
round-count prints each pass's round count among the commits not yet on the default branch whose
titles open `Answer <pass> round <n>:`, ignoring case. The count is the highest <n>, or the number of
such commits when that is more. At or past the cap of {cap} rounds, it says the pass takes no more
rounds. It also lists each name in such a title that is not a pass.
round-count prints each pass's round count among the branch's commits since the default branch,
following first parents as rule-hook's round-cap does, whose titles open `Answer <pass> round <n>:`,
ignoring case. The count is the highest <n>, or the number of such commits when that is more. At or
past the cap of {cap} rounds, it says the pass takes no more rounds. It also lists each name in such
a title that is not a pass.
needs-scoring-run takes the IDs of the todo.md items the chunk ships, none when it ships none, and
prints why the chunk is due a scoring run, or `No scoring run due.` It is due when the chunk ships
@@ -101,8 +102,8 @@ import re
import sys
import typing
from git_repo import branch_titles, default_branch, git, top
from round_titles import CAP, round_counts
from git_repo import default_branch, git, top
from failure import Failure
from comprehension_history import FLOOR
from larv_review.history_changes import history_changes
@@ -194,10 +195,10 @@ def show_ruling_base(default: str) -> Failure | None:
def round_count(default: str) -> Failure | None:
titles = git("log", "--format=%s", f"{default}..HEAD")
titles = branch_titles(default)
if isinstance(titles, Failure):
return titles
for name, count in round_counts(titles.splitlines()).items():
for name, count in round_counts(titles).items():
print(f"{name}: {count}{cap_note(count) if name in PASSES else ', not a pass: check the commit title'}")
return None
+8
View File
@@ -395,6 +395,14 @@ class LarvReviewTool(unittest.TestCase):
self.assertIn("prose: 1\n", ran.stdout)
self.assertIn("architecture: 0\n", ran.stdout)
def test_leaves_out_the_round_commits_of_a_branch_merged_in_as_the_hook_does(self):
self.git("switch", "-q", "-c", "sibling")
self.commit("Answer stability round 1: a")
self.commit("Answer stability round 2: b")
self.git("switch", "-q", "chunk")
self.git("merge", "-q", "--no-ff", "-m", "Merge sibling", "sibling")
self.assertIn("stability: 0\n", self.run_tool("round-count").stdout)
def test_says_whether_a_scoring_run_is_due_and_refuses_an_unknown_item(self):
self.assertEqual(self.run_tool("needs-scoring-run", "1").stdout, "Scoring run due: the chunk leaves release 0.1.0 with no plan item.\n")
self.assertEqual(self.run_tool("needs-scoring-run", "2").stdout, "No scoring run due.\n")
+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)]:
+5
View File
@@ -87,6 +87,11 @@ class StalledOrigin(unittest.TestCase):
self.assertIsInstance(ref, Failure)
self.assertIn("stopped after 0.5 s", ref.reason)
def test_reading_the_default_branch_guarded_asks_no_one_and_fails(self):
ref = git_repo.default_ref(self.root, 0.5, guarded=True)
self.assertIsInstance(ref, Failure)
self.assertIn("origin records no default branch", ref.reason)
def test_fetching_the_default_branch_fails_after_the_timeout(self):
with contextlib.chdir(self.root):
found = git_repo.default_branch(stale_ok=True, timeout=0.5)