Read a project's language, title templates and branch template from its config

This commit is contained in:
2026-11-14 01:08:48 +01:00
parent 499d1772be
commit 82f4bbd91b
22 changed files with 316 additions and 43 deletions
+1 -1
View File
@@ -122,6 +122,6 @@ def bash_breaches(command: str, cwd: str, home: str) -> list[Breach]:
simples = simple_commands(command, cwd, home)
found = commits(simples, home)
in_files = [breach for directory in dict.fromkeys(commit.directory for commit in found) for breach in committed_file_breaches(directory)]
round_cap = [breach for commit in found for breach in round_cap_breaches(commit)]
round_cap = [breach for commit in found for breach in round_cap_breaches(commit, pathlib.Path(home))]
todo = [breach for commit in found for breach in todo_breaches(commit.directory, pathlib.Path(home))]
return command_breaches(command, simples, home) + round_cap + todo + in_files
+11 -4
View File
@@ -1,18 +1,25 @@
import functools
import pathlib
from rule_hook.rules import Breach
from review_rules.round_titles import cap, past_cap, round_counts, round_pass
from project_config.main import commit_title
from git_cli.git_command import CommitCall
from failure import Failure
from git_cli import git_repo
from review_rules.review_passes import PASSES
from project_config import templates
def round_cap_breaches(commit: CommitCall) -> list[Breach]:
"""A breach where the commit's title names no pass, or takes its pass past the cap, counted with the branch's earlier round commits."""
named = [(name, message) for message in commit.messages if (name := round_pass(message))]
def round_cap_breaches(commit: CommitCall, home: pathlib.Path) -> list[Breach]:
"""A breach where the commit's title names no pass, or takes its pass past the cap, counted with the branch's earlier round commits. Each title is read as its summary in the project's `commit-title` template."""
template = commit_title(commit.directory, home, functools.partial(git_repo.git, guarded=True))
summaries = [templates.summary_of(template, message.strip()) for message in commit.messages]
named = [(name, summary) for summary in summaries if (name := round_pass(summary))]
unknown = [Breach("round-cap", f"`{name}` names no larv-review pass, so its rounds would never reach a cap; name one of: {', '.join(PASSES)}", commit.directory) for name, _ in named if name not in PASSES]
if unknown or not named:
return unknown
earlier = earlier_titles(commit.directory, commit.amends)
earlier = [templates.summary_of(template, title) for title in earlier_titles(commit.directory, commit.amends)]
return [Breach("round-cap", refusal(name, message, earlier), commit.directory) for name, message in named if past_cap(message, earlier)]
+15 -2
View File
@@ -1,17 +1,22 @@
import dataclasses
import functools
import pathlib
import posixpath
import re
import typing
from review_rules.round_titles import CAP, COUNTS, EXAMPLE, lacks_counts, OPENING, RELEASE_REVIEW_CAP
from project_config.main import commit_title
from git_cli.git_command import commits
from git_cli.git_repo import git
from run_heavy.capped_docker import finds_this_docker
from rule_hook.heavy_command import heavy
from command_lines.shell_command import launched, Simple
from review_rules.review_folder import REPORTS
from project_config import templates
from command_lines.docker_command import unlimited
RuleName = typing.Literal["action-pin", "commit-one-line", "docker-run-memory", "env-file-read", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check"]
RuleName = typing.Literal["action-pin", "commit-one-line", "commit-title", "docker-run-memory", "env-file-read", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check"]
@dataclasses.dataclass(frozen=True)
@@ -31,6 +36,10 @@ RULES: dict[RuleName, Rule] = {
"a commit message is one line, prefixed with the ticket ID when the work has one",
"git commit takes its message as one -m with no line break; -F, a second -m and a heredoc are blocked",
),
"commit-title": Rule(
"a commit's title fits the `commit-title` template the project's config names, such as `PAY-{id} - {summary}`, so the project's log reads as its owners write it",
"the -m of a git commit in a checkout whose config sets `commit-title`; a round title's `Answer <pass> round <n>:` is the template's {summary}",
),
"docker-run-memory": Rule(
"a container made with docker run or docker create has a memory limit and a swap limit, as large as its work needs, such as `--memory 4g --memory-swap 4g`, so a runaway container fails alone instead of starving the machine; a swap limit equal to the memory limit keeps it out of swap",
"docker run, docker create, docker container run and docker container create, whether direct or through a launcher such as nice or timeout. One under run-heavy passes, as run-heavy's docker adds the limits on Linux, unless it names docker by its path, or changes or clears PATH, as env -i, sudo or doas do. A container a script or docker compose starts is not checked",
@@ -163,7 +172,11 @@ def command_breaches(command: str, simples: list[Simple], home: str) -> list[Bre
breaches.append(Breach("commit-one-line", "pass the message as one -m '<line>', not from a file", commit.directory))
elif len(commit.messages) > 1 or any("\n" in message.strip() for message in commit.messages):
breaches.append(Breach("commit-one-line", "pass the message as one -m '<line>', with no heredoc, second -m or newline", commit.directory))
if any(lacks_counts(message) for message in commit.messages):
template = commit_title(commit.directory, pathlib.Path(home), functools.partial(git, guarded=True))
# A message of several lines breaks commit-one-line above, which says how to fix it.
if misfit := [message for message in commit.messages[:1] if "\n" not in message.strip() and not templates.fits(template, message.strip())]:
breaches.append(Breach("commit-title", f"title the commit as the project's config asks, `{template}`, where {{id}} is the ID of the chunk's first item; `{misfit[0].strip()[:80]}` doesn't fit", commit.directory))
if any(lacks_counts(templates.summary_of(template, message.strip())) for message in commit.messages):
breaches.append(Breach("round-title-counts", f"open the summary after the round's colon with `{COUNTS}`, where <j> is at most <k> + <m>", commit.directory))
return breaches
+16 -1
View File
@@ -1026,6 +1026,21 @@ class Hook(unittest.TestCase):
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_is_titled_by_the_configs_template_and_its_round_title_read_through_it(self):
config = configured(self, pathlib.Path(self.repo)) / "config.toml"
config.write_text(config.read_text() + 'commit-title = "PAY-{id} - {summary}"\n')
for title in ["PAY-7 - Answer stability round 1: 1 🔴, 0 🟡 (0 misread); a", "PAY-7 - Answer stability round 2: 0 🔴, 1 🟡 (0 misread); c"]:
git(self.repo, "commit", "-q", "--allow-empty", "-m", title)
past = self.round_commit("PAY-7 - Answer stability round 2: 0 🔴, 0 🟡 (0 misread); d")
self.assertEqual(past.returncode, 2)
self.assertIn("Blocked by rule round-cap: this would be stability's round 3, past its 2", past.stderr)
self.assertIn("Blocked by rule round-title-counts", self.round_commit("PAY-7 - Answer prose round 1: fix").stderr)
bare = self.round_commit("Answer prose round 1: 0 🔴, 0 🟡 (0 misread); x")
self.assertEqual(bare.returncode, 2)
self.assertIn("Blocked by rule commit-title: title the commit as the project's config asks, `PAY-{id} - {summary}`", bare.stderr)
self.assertEqual(self.round_commit("PAY-7 - Answer prose round 1: 0 🔴, 0 🟡 (0 misread); x").returncode, 0)
self.assertEqual(self.round_commit("Tidy # rule-override commit-title: the client asked for this title").returncode, 0)
def test_a_commit_while_an_unranked_todo_has_changes_is_blocked(self):
configured(self, pathlib.Path(self.repo))
pathlib.Path(self.repo, "docs").mkdir()
@@ -1079,7 +1094,7 @@ class Hook(unittest.TestCase):
def test_help_lists_every_rule(self):
helped = subprocess.run([str(HOOK), "--help"], capture_output=True, check=True, env=isolated_env(), text=True).stdout
for rule in ["action-pin", "commit-one-line", "docker-run-memory", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check"]:
for rule in ["action-pin", "commit-one-line", "commit-title", "docker-run-memory", "ignorefile-sorted", "image-pin", "no-ai-attribution", "repeat-read", "reviewer-report", "reviewer-rung", "round-cap", "round-title-counts", "run-heavy", "todo-rank-check"]:
with self.subTest(rule):
self.assertIn(f" {rule}\n The rule: ", helped)
self.assertIn("reviewer-report\n The rule: a reviewer writes only its report", helped)