174 lines
7.6 KiB
Python
174 lines
7.6 KiB
Python
import dataclasses
|
|
import re
|
|
import typing
|
|
|
|
from failure import Failure
|
|
from review_rules.comprehension_history import HISTORY
|
|
from release_plan.todo_table import Item, items
|
|
from reading_cost.figures import RECORD
|
|
from text_files.utc_stamp import utc
|
|
|
|
# The plan, and the records tools append to: none of their lines are the chunk's work.
|
|
NOT_WORK = ("todo.md", HISTORY, RECORD)
|
|
# Each commit as its hash, commit and author times and parents, its short hash and title, then the files it changed; a merge's files are those it changed against its first parent. git refuses a NUL byte in a commit message, so a title can't split a record.
|
|
LOG = ("log", "--format=%x00%H %ct %at %P%x1f%h%x1f%s", "--name-only", "--diff-merges=first-parent")
|
|
# One commit's record in LOG's output, without the NUL that opens it.
|
|
LOG_RECORD = re.compile(r"([0-9a-f]+) ([0-9]+) ([0-9]+) ([0-9a-f ]*)\x1f([0-9a-f]+)\x1f(.*)", re.DOTALL)
|
|
# A title opening `Drop item 69: <why>` or `Drop items 69, 70 and 71: <why>` removes those items unbuilt; only at the start, where a log skimmed by its titles shows it.
|
|
DROP = re.compile(r"Drop items? ([0-9]+(?:, [0-9]+)*(?: and [0-9]+)?):")
|
|
# How a chunk reached the default branch; rebase and push leave no merge commit to bound it.
|
|
Landing = typing.Literal["merge", "rebase", "push"]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Commit:
|
|
"""A commit as LOG prints it; committed and authored are its commit and author times, in seconds since the epoch."""
|
|
hash: str
|
|
short: str
|
|
committed: int
|
|
authored: int
|
|
parents: tuple[str, ...]
|
|
title: str
|
|
files: tuple[str, ...]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Span:
|
|
"""A chunk on a default branch, as cut_branch cuts it. before is the commit its change starts from, None for the branch's oldest chunk; tip is its newest commit on the first-parent line. label names it as the reports do, landing says how it reached the branch, and merged is tip's commit time. titles are those of every commit it brought."""
|
|
before: str | None
|
|
tip: str
|
|
label: str
|
|
landing: Landing
|
|
merged: str
|
|
titles: tuple[str, ...]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class BranchChunk(Span):
|
|
"""A Span with two more fields: shipped holds each todo.md item its change removed, as scored before the change, less any item one of its commit titles drops; fixed_by names the later chunks that fixed code it shipped, newest first."""
|
|
shipped: tuple[Item, ...]
|
|
fixed_by: tuple[str, ...]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Branch:
|
|
"""A default branch's commits by hash, its chunks newest first, and each commit's chunk as an index into chunks."""
|
|
commits: dict[str, Commit]
|
|
chunks: list[Span]
|
|
owner: dict[str, int]
|
|
|
|
|
|
def cut_branch(log: str) -> Branch | Failure:
|
|
"""The branch LOG printed, cut into chunks as chunk_log cuts its first-parent line; a commit belongs to the oldest chunk whose commits reach it."""
|
|
commits = parsed(log)
|
|
if isinstance(commits, Failure):
|
|
return commits
|
|
if not commits:
|
|
return Failure("git log printed no commit")
|
|
# git log prints the commit it starts from first.
|
|
line = [commits[next(iter(commits))]]
|
|
while line[-1].parents:
|
|
line.append(commits[line[-1].parents[0]])
|
|
runs = chunk_log(line)
|
|
owner = owners(runs, commits)
|
|
titles: list[list[str]] = [[] for _ in runs]
|
|
for commit in commits.values():
|
|
titles[owner[commit.hash]].append(commit.title)
|
|
spans = []
|
|
for index, run in enumerate(runs):
|
|
spans.append(Span(
|
|
before=run[-1].parents[0] if run[-1].parents else None,
|
|
tip=run[0].hash,
|
|
label=label(run[-1].short, run[0].short),
|
|
landing=landing(run),
|
|
merged=utc(run[0].committed),
|
|
titles=tuple(titles[index]),
|
|
))
|
|
return Branch(commits, spans, owner)
|
|
|
|
|
|
def parsed(log: str) -> dict[str, Commit] | Failure:
|
|
"""Each commit LOG printed, by hash, in the order it printed them."""
|
|
commits = {}
|
|
for record in log.split("\x00")[1:]:
|
|
found = LOG_RECORD.fullmatch(record)
|
|
if not found:
|
|
return Failure(f"git log printed a commit it could not read: {record[:200]!r}")
|
|
full, committed, authored, parents, short, rest = found.groups()
|
|
title, *files = rest.split("\n")
|
|
commits[full] = Commit(full, short, int(committed), int(authored), tuple(parents.split()), title, tuple(name for name in files if name))
|
|
return commits
|
|
|
|
|
|
def chunk_log(line: list[Commit]) -> list[list[Commit]]:
|
|
"""The first-parent line, newest first, cut into chunks, newest first, each holding its commits newest first. A merge is a chunk of its own; any other commit joins the chunk of the newer commit above it when that is no merge and both share a commit time."""
|
|
runs: list[list[Commit]] = []
|
|
for commit in line:
|
|
if runs and len(commit.parents) < 2 and len(runs[-1][-1].parents) < 2 and runs[-1][-1].committed == commit.committed:
|
|
runs[-1].append(commit)
|
|
else:
|
|
runs.append([commit])
|
|
return runs
|
|
|
|
|
|
def landing(run: list[Commit]) -> Landing:
|
|
"""How a run that chunk_log cut reached the branch: by a merge commit, by a rebase that gave a commit a commit time other than its author time, or by commits never replayed."""
|
|
if len(run[0].parents) > 1:
|
|
return "merge"
|
|
return "rebase" if any(commit.committed != commit.authored for commit in run) else "push"
|
|
|
|
|
|
def label(first: str, last: str) -> str:
|
|
"""How the report names a chunk: its one commit's short hash, or its first and last commits' as a range."""
|
|
return first if first == last else f"{first}^..{last}"
|
|
|
|
|
|
def owners(runs: list[list[Commit]], commits: dict[str, Commit]) -> dict[str, int]:
|
|
"""Each commit's chunk, as its index in runs: the oldest chunk whose commits reach it."""
|
|
owner: dict[str, int] = {}
|
|
for index in reversed(range(len(runs))):
|
|
for commit in reversed(runs[index]):
|
|
unowned = [commit.hash]
|
|
while unowned:
|
|
found = unowned.pop()
|
|
if found not in owner:
|
|
owner[found] = index
|
|
unowned += commits[found].parents
|
|
return owner
|
|
|
|
|
|
def shipped(before: str, after: str, titles: tuple[str, ...] = ()) -> list[Item]:
|
|
"""Each item the todo.md before a change holds and the todo.md after it does not, as scored before, less those a title of the change drops; none where either holds no items table it can read."""
|
|
planned, left = items(before), items(after)
|
|
if not isinstance(planned, list) or not isinstance(left, list):
|
|
return []
|
|
kept = {item.id for item in left} | dropped(titles)
|
|
return sorted((item for item in planned if item.id not in kept), key=lambda item: item.id)
|
|
|
|
|
|
def dropped(titles: tuple[str, ...]) -> set[int]:
|
|
"""The IDs of the items the titles drop, each title opening as DROP reads."""
|
|
return {int(number) for title in titles if (found := DROP.match(title)) for number in re.findall(r"[0-9]+", found[1])}
|
|
|
|
|
|
def plan_revisions(branch: Branch) -> list[str]:
|
|
"""The revisions whose todo.md shipped_by needs: for each chunk that brought a commit changing todo.md, other than the branch's oldest chunk, the commit before it and its tip."""
|
|
changed = sorted({branch.owner[commit.hash] for commit in branch.commits.values() if "todo.md" in commit.files})
|
|
revisions: dict[str, None] = {}
|
|
for index in changed:
|
|
before = branch.chunks[index].before
|
|
if before is not None:
|
|
revisions.update(dict.fromkeys((before, branch.chunks[index].tip)))
|
|
return list(revisions)
|
|
|
|
|
|
def shipped_by(branch: Branch, plans: dict[str, str]) -> list[tuple[Item, ...]]:
|
|
"""The items each of the branch's chunks shipped, in the order of its chunks. plans holds todo.md's text at each revision plan_revisions names. A chunk with a revision plans lacks changed no plan, so it shipped none, nor an item one of its titles drops."""
|
|
found: list[tuple[Item, ...]] = []
|
|
for chunk in branch.chunks:
|
|
if chunk.before is not None and chunk.before in plans and chunk.tip in plans:
|
|
found.append(tuple(shipped(plans[chunk.before], plans[chunk.tip], chunk.titles)))
|
|
else:
|
|
found.append(())
|
|
return found
|