42 lines
1.8 KiB
Python
42 lines
1.8 KiB
Python
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
|
|
|
|
|
|
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 the project's config puts the history outside the checkout, where no commit holds it."""
|
|
if partial := full_history():
|
|
return partial
|
|
config = located(None, home)
|
|
if isinstance(config, Failure):
|
|
return config
|
|
history = config.tracked("comprehension-history")
|
|
if history is None:
|
|
return Failure(f"larv-review doesn't support a comprehension history outside the checkout: no commit holds {config.paths['comprehension-history']}, 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)
|