import pathlib from failure import Failure from git_cli.git_repo import file_at, full_history, git from review_rules.comprehension_history import HistoryChange from project_config.main import located from record_store.stores import store def history_changes(default: str, home: pathlib.Path) -> list[HistoryChange] | Failure: """Each merged change on the default branch that touched the history, newest first. A Failure where no commit holds the history, as for one the project's config puts outside the checkout.""" if partial := full_history(): return partial config = located(None, home) if isinstance(config, Failure): return config record = store(config, "comprehension-history") history = record.tracked if history is None: return Failure(f"larv-review doesn't support a comprehension history outside the checkout: no commit holds {record.label}, so it cannot tell which commit was last measured") # history names the file from the checkout's top, and git reads a path from the directory it runs in. root = str(config.top) commits = git("log", "--first-parent", "--format=%H", default, "--", history, cwd=root) if isinstance(commits, Failure): return commits changes = [] for commit in commits.split(): change = history_change(commit, history, root) if isinstance(change, Failure): return change changes.append(change) return changes def history_change(commit: str, history: str, root: str) -> HistoryChange | Failure: """The history, the file git names history, before and after commit, read in the checkout at root. A merge commit counts as one change, against its first parent.""" before = file_at(f"{commit}^1", history, root) if isinstance(before, Failure): return before after = file_at(commit, history, root) if isinstance(after, Failure): return after return HistoryChange(commit, before, after)