import dataclasses import typing from comprehension_history import HISTORY from todo_table import Item, items from reading_cost.figures import RECORD from 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 are those it changed against its first parent. LOG = ("log", "--format=%x1e%H %ct %at %P%x1f%h%x1f%s", "--name-only", "--diff-merges=first-parent") # 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 BranchChunk: """A chunk on a default branch. Its change runs from before, None for the branch's first chunk, to tip, its newest commit on the first-parent line. label names it as the reports do, landed says how it reached the branch, and merged is tip's commit time. titles are those of every commit it brought; shipped holds each todo.md item its change removed, as scored before it; fixed_by names the later chunks that fixed code it shipped, newest first.""" before: str | None tip: str label: str landed: Landing merged: str titles: tuple[str, ...] 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[BranchChunk] owner: dict[str, int] def cut_branch(log: str) -> Branch: """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) # 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) chunks = [BranchChunk(run[-1].parents[0] if run[-1].parents else None, run[0].hash, label(run[-1].short, run[0].short), landing(run), utc(run[0].committed), tuple(titles[index]), (), ()) for index, run in enumerate(runs)] return Branch(commits, chunks, owner) def parsed(log: str) -> dict[str, Commit]: """Each commit LOG printed, by hash, in the order it printed them.""" commits = {} for record in log.split("\x1e")[1:]: header, short, rest = record.split("\x1f", 2) full, committed, authored, *parents = header.split() title, *files = rest.split("\n") commits[full] = Commit(full, short, int(committed), int(authored), tuple(parents), 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 chunk 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) -> list[Item]: """Each item the todo.md before a change holds and the todo.md after it does not, as scored before; 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} return sorted((item for item in planned if item.id not in kept), key=lambda item: item.id) def plan_revisions(branch: Branch) -> list[str]: """The revisions whose todo.md with_shipped needs: the commit before and the tip of each chunk, but the branch's first, that brought a commit changing todo.md.""" 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 with_shipped(branch: Branch, plans: dict[str, str]) -> Branch: """The branch with each chunk's shipped items, read from todo.md's text at each revision plan_revisions named, as plans holds it; a chunk with a revision plans lacks changed no plan.""" chunks = [dataclasses.replace(chunk, shipped=tuple(shipped(plans[chunk.before], plans[chunk.tip]))) if chunk.before is not None and chunk.before in plans and chunk.tip in plans else chunk for chunk in branch.chunks] return dataclasses.replace(branch, chunks=chunks)