"""Hold larv-review's mechanical rules: the paired ruling's base and vote count, each pass's rounds, the scoring run's trigger, and the comprehension history's format and latest places. usage: larv-review ruling-base | round-count | needs-scoring-run [...] | record-ruling | record-scoring [...] | places The comprehension check records how hard a project is to read, two ways, and blocks no merge. In a paired ruling, simulated readers, the seats, read the branch's head beside a base and vote better, same or worse, overall and on each dimension. In a scoring run, four seats score the project from 1 to 10. Each seat names the places it found hard. Run it inside a project checkout or one of its worktrees. Each command but record-ruling and places first fetches origin, when the checkout has that remote, and takes origin's default branch, or else the local main or master, as the default branch. When the fetch fails, round-count reads origin as last fetched and says so on stderr; the other three exit 1. The history is docs/comprehension-history.md in the checkout. ruling-base prints the commit a paired ruling reads against, then which of these found it. The last measured commit is the default branch's newest merged change that adds a paired ruling or a scoring 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 round :`, ignoring case. The count is the highest , or the number of such commits when that is more. At or past the cap of {cap} rounds, it says to ask the human before another round. 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 an item whose tagline names the comprehension floor, or when it leaves a release with no item in the checkout's todo.md, less the shipped items, and work has landed towards that release. Work has landed when the chunk ships one of the release's items, or when the release is the earliest the default branch's todo.md names and the default branch has a commit after its newest v* tag, or has no v* tag. A seats file's first line names what the seats read, ` at `, ending `, against ` in a votes file. Each seat's line follows, opening with its label written exactly: `Junior A:`, `Mid A:`, `Maintainability senior:` or `Inherited architect:`. Its places follow it, one `- ` bullet each, as the history writes them. A votes file: PR #231 at abc1234, against def5678 Mid A: Navigation better, Locality worse, Shape same, Self-sufficiency same, Overall better - Navigation, Overall: `README.md:40` "Tools": names the tool a symptom leads to - Locality: `bin/x.py:12` `run`: three branches to hold at once Inherited architect: Navigation better, Locality worse, Shape same, Self-sufficiency same, Overall better - Navigation: `README.md:40` "Tools": ... - Locality: `bin/x.py:12` `run`: ... A scores file holds Junior A's, Mid A's, the maintainability senior's and the inherited architect's lines in that order, each `Navigation , Locality , Shape , Self-sufficiency , Overall `: each score from 1 to 10, and the overall at most 1 above the line's lowest dimension. record-ruling takes Mid A's and the inherited architect's votes on each dimension and overall, each better, same or worse. Each place opens with the names from the seat's line that it moves, joined by `, ` and followed by `: `. A dimension's vote counts as same unless one of the seat's places opens with that dimension and holds a `path:line` in backticks; the overall vote counts as same unless any place holds one. The printed votes say so. When the two counted overall votes differ, it appends nothing and prints `Split:`, saying to add the maintainability senior's and Junior A's lines. Otherwise the ruling is the overall vote that leads each other vote by two or more, else same. It prints `Ruling:` and appends the ruling. record-scoring takes the scores file of a run needs-scoring-run calls, whose first line names no `, against`, and the same item IDs, and exits 1 when they call no run. It appends the seats' scores and their means, rounded half up to two decimals, prints the means and, when a mean is under the floor of {floor}, a line opening `Floor:` naming each one. places prints the places seats found hard since the newest scoring run: that run's places, and, in each later ruling, each place opening with a dimension, or Overall, that its seat voted worse. A ruling recorded with scoring seats' places counts as a scoring run. Each entry's places come under its heading, oldest first. With no scoring run, it reads every ruling. Each record is appended to the history, created when missing, under a heading of the current UTC time and the seats file's first line. Each commit that line names must be in the checkout. It exits 1 when the history already holds an entry on what that line reads. Exit 1, saying why, on unknown arguments, an unknown item ID, a seats file it cannot read or that breaks the shape above, or when git cannot start, finds no checkout or no default branch, cannot fetch origin for ruling-base, needs-scoring-run or record-scoring, or cannot read or write the history or todo.md. """ import pathlib import re import sys import typing from round_titles import CAP, PASSES, round_counts from git_repo import default_branch, git from failure import Failure from larv_review.scoring import FLOOR from larv_review.git import history_changes from larv_review.ruling_base import measured_commit from larv_review.plan import plan_reasons from larv_review.record import record_ruling, record_scoring, show_places USAGE = "usage: larv-review ruling-base | round-count | needs-scoring-run [...] | record-ruling | record-scoring [...] | places" def main(argv: list[str]) -> int: if argv in (["-h"], ["--help"]): print(__doc__.strip().format(cap=CAP, floor=FLOOR)) return 0 command, *given = argv or [""] if command == "ruling-base" and not given: done = with_default_branch(show_ruling_base, stale_ok=False) elif command == "round-count" and not given: done = with_default_branch(round_count, stale_ok=True) elif command == "needs-scoring-run" and all(re.fullmatch(r"[0-9]+", id_) for id_ in given): done = with_default_branch(lambda default: needs_scoring_run(default, [int(id_) for id_ in given]), stale_ok=False) elif command == "record-ruling" and len(given) == 1: done = record_ruling(pathlib.Path(given[0])) elif command == "record-scoring" and given and all(re.fullmatch(r"[0-9]+", id_) for id_ in given[1:]): done = with_default_branch(lambda default: record_scoring(default, pathlib.Path(given[0]), [int(id_) for id_ in given[1:]]), stale_ok=False) elif command == "places" and not given: done = show_places() elif command in ("needs-scoring-run", "record-scoring") and given: named = given[1:] if command == "record-scoring" else given print(f"{command} takes item IDs, the numbers in todo.md's ID column, not {' '.join(id_ for id_ in named if not re.fullmatch(r'[0-9]+', id_))}; {USAGE}", file=sys.stderr) return 1 elif not argv: print(USAGE, file=sys.stderr) return 1 else: print(f"unknown arguments {' '.join(argv)}; {USAGE}", file=sys.stderr) return 1 if isinstance(done, Failure): print(done.reason, file=sys.stderr) return 1 return 0 def with_default_branch(command: typing.Callable[[str], Failure | None], stale_ok: bool) -> Failure | None: default = default_branch(stale_ok) if isinstance(default, Failure): return default branch, note = default if note: print(note, file=sys.stderr) return command(branch) def show_ruling_base(default: str) -> Failure | None: changes = history_changes(default) if isinstance(changes, Failure): return changes if found := measured_commit(changes): print(f"{found}\nthe last measured commit") return None merge_base = git("merge-base", "HEAD", default) if isinstance(merge_base, Failure): return merge_base print(f"{merge_base.strip()}\nthe merge base of HEAD and {default}") return None def round_count(default: str) -> Failure | None: titles = git("log", "--format=%s", f"{default}..HEAD") if isinstance(titles, Failure): return titles for name, count in round_counts(titles.splitlines()).items(): print(f"{name}: {count}{cap_note(count) if name in PASSES else ', not a pass: check the commit title'}") return None def cap_note(count: int) -> str: if count < CAP: return "" return f", {'at' if count == CAP else 'past'} the cap: ask the human before another round" def needs_scoring_run(default: str, ids: list[int]) -> Failure | None: reasons = plan_reasons(default, ids) if isinstance(reasons, Failure): return reasons print(f"Scoring run due: the chunk {' and '.join(reasons)}." if reasons else "No scoring run due.") return None