from larv_review.history import entries, Entry, HistoryChange def measured_commit(changes: list[HistoryChange]) -> str | None: """The newest change, of changes given newest first, whose last added paired ruling is better, or that adds a scoring run and whose last added paired ruling names no fall.""" 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) last = rulings[-1] if rulings else None ruled_better = last is not None and last.ruling == "better" scored_without_fall = any(not entry.paired for entry in added) and not (last and last.fell) if ruled_better or scored_without_fall: 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