41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
import dataclasses
|
|
|
|
from comprehension_history import entries, Entry
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class HistoryChange:
|
|
"""A merged change on the default branch that touched the history, and the history before and after it."""
|
|
commit: str
|
|
before: str
|
|
after: str
|
|
|
|
|
|
def measured_commit(changes: list[HistoryChange]) -> str | None:
|
|
"""The newest change, of changes given newest first, that adds a scoring run to the history, or whose last added paired ruling is better.
|
|
|
|
It is the ratchet's base. A ruling of same leaves the base where it was, so a run of small falls each ruled same still adds up against one commit. Entry times are UTC stamps in ISO order, so they sort as text.
|
|
"""
|
|
for change in changes:
|
|
held = {entry.heading for entry in entries(change.before)}
|
|
added = [entry for entry in entries(change.after) if entry.heading not in held]
|
|
rulings = sorted((entry for entry in added if entry.paired), key=lambda entry: entry.time)
|
|
ruled_better = bool(rulings) and rulings[-1].ruling == "better"
|
|
if ruled_better or any(not entry.paired for entry in added):
|
|
return change.commit
|
|
return None
|
|
|
|
|
|
def first_ruling(history: str) -> Entry | None:
|
|
rulings = sorted((entry for entry in entries(history) if entry.paired), key=lambda entry: entry.time)
|
|
return rulings[0] if rulings else None
|
|
|
|
|
|
def base(changes: list[HistoryChange], history: str) -> tuple[str, str] | None:
|
|
"""The commit a paired ruling reads against, and which fallback found it; None leaves the merge base of HEAD and the default branch. history is the default branch's."""
|
|
if found := measured_commit(changes):
|
|
return found, "the last measured commit"
|
|
if (first := first_ruling(history)) and first.against:
|
|
return first.against, "the base the history's first paired ruling names"
|
|
return None
|