Files

38 lines
2.8 KiB
Python

from rule_hook.rules import Breach
from review_rules.round_titles import cap, past_cap, round_counts, round_pass
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.templates import Titles
def round_cap_breaches(commit: CommitCall, titles: Titles) -> 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 titles, the project's title templates."""
summaries = [titles.summary(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 = [titles.summary(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)]
def refusal(name: str, title: str, earlier: list[str]) -> str:
"""Why title, a round title of the pass name, is refused, listing the pass's round commits counted, and how to commit the fixes in hand."""
counted = [earlier_title for earlier_title in earlier if round_pass(earlier_title) == name]
listed = ", ".join(f"`{counted_title}`" for counted_title in counted) or "none found"
return f"this would be {name}'s round {round_counts([*counted, title])[name]}, past its {cap(name)}; {name}'s round commits on this branch: {listed}. Fold the fixes into its last round commit with `git commit --amend` where that is the newest commit, or commit them under a title that doesn't open `Answer <pass> round`; they merge once the gate passes"
def earlier_titles(directory: str, amends: bool) -> list[str]:
"""The branch's titles, less the one an --amend replaces; none where git cannot list them within 5 seconds or the checkout records no default branch for origin, so the title's own number still counts.
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):
# Counting no earlier title lets the commit through unless its own number is past the cap: a hook that refused here would stop every round commit of an unattended run whose git is slow or whose origin has no default branch.
return []
return titles[1:] if amends else titles