Keep the history's writer beside its reader, read plan rows with rank_todo, and fetch only for commands that read the default branch

This commit is contained in:
2026-10-09 01:10:01 +02:00
parent 77fef49fce
commit 53ed5150be
8 changed files with 189 additions and 162 deletions
+21 -20
View File
@@ -1,4 +1,3 @@
import dataclasses
import subprocess
from failure import Failure
@@ -7,13 +6,6 @@ from larv_review.rules import HistoryChange
HISTORY = "docs/comprehension-history.md"
@dataclasses.dataclass(frozen=True)
class Repository:
"""The checkout the tool runs in: its top folder, and the default branch's ref after a fetch, `origin/<branch>` with a remote."""
top: str
default: str
def git(*args: str) -> str | Failure:
try:
ran = subprocess.run(["git", *args], capture_output=True, text=True)
@@ -22,10 +14,13 @@ def git(*args: str) -> str | Failure:
return ran.stdout if ran.returncode == 0 else Failure(f"git {' '.join(args)}: {ran.stderr.strip()}")
def repository() -> Repository | Failure:
top = git("rev-parse", "--show-toplevel")
if isinstance(top, Failure):
return top
def top() -> str | Failure:
found = git("rev-parse", "--show-toplevel")
return found if isinstance(found, Failure) else found.strip()
def default_branch() -> str | Failure:
"""The default branch's ref: `origin/<branch>` after a fetch, with that remote, else the local main or master."""
remotes = git("remote")
if isinstance(remotes, Failure):
return remotes
@@ -35,18 +30,21 @@ def repository() -> Repository | Failure:
default = git("symbolic-ref", "--short", "refs/remotes/origin/HEAD")
if isinstance(default, Failure):
return Failure(f"origin names no default branch; set it with `git remote set-head origin --auto`: {default.reason}")
return Repository(top.strip(), default.strip())
return default.strip()
for branch in ("main", "master"):
if not isinstance(git("rev-parse", "--quiet", "--verify", f"refs/heads/{branch}"), Failure):
return Repository(top.strip(), branch)
return branch
return Failure("no origin remote, and no main or master branch to take as the default branch")
def file_at(revision: str, path: str) -> str | Failure:
"""A file's text at a revision, empty where the revision does not hold it."""
if isinstance(git("rev-parse", "--quiet", "--verify", f"{revision}:{path}"), Failure):
"""A file's text at a revision, empty where the revision is not a commit, such as a root commit's parent, or holds no such file."""
if isinstance(git("rev-parse", "--quiet", "--verify", f"{revision}^{{commit}}"), Failure):
return ""
return git("show", f"{revision}:{path}")
listed = git("ls-tree", "--name-only", revision, "--", path)
if isinstance(listed, Failure):
return listed
return git("show", f"{revision}:{path}") if listed.strip() else ""
def history_changes(default: str) -> list[HistoryChange] | Failure:
@@ -57,9 +55,9 @@ def history_changes(default: str) -> list[HistoryChange] | Failure:
changes = []
for commit in commits.split():
before = file_at(f"{commit}^1", HISTORY)
after = file_at(commit, HISTORY)
if isinstance(before, Failure):
return before
after = file_at(commit, HISTORY)
if isinstance(after, Failure):
return after
changes.append(HistoryChange(commit, before, after))
@@ -68,6 +66,9 @@ def history_changes(default: str) -> list[HistoryChange] | Failure:
def landed_since_release(default: str) -> bool | Failure:
"""Whether the default branch holds a commit after its newest `v*` tag, or any commit when it has none."""
tag = git("describe", "--tags", "--abbrev=0", "--match", "v*", default)
count = git("rev-list", "--count", default if isinstance(tag, Failure) else f"{tag.strip()}..{default}")
tags = git("tag", "--list", "v*", "--merged", default, "--sort=-v:refname")
if isinstance(tags, Failure):
return tags
newest = tags.split()[:1]
count = git("rev-list", "--count", f"{newest[0]}..{default}" if newest else default)
return count if isinstance(count, Failure) else int(count) > 0