Answer principles round 1: 0 🔴, 4 🟡 (0 misread); stage types for the cut and shipped chunks, one text_at, the path rule in the core, a parse that fails as a value

This commit is contained in:
2026-10-28 21:51:59 +01:00
parent f7532827db
commit d967219f7e
10 changed files with 280 additions and 146 deletions
+47 -18
View File
@@ -1,6 +1,8 @@
import dataclasses
import re
import typing
from failure import Failure
from comprehension_history import HISTORY
from todo_table import Item, items
from reading_cost.figures import RECORD
@@ -8,8 +10,10 @@ 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")
# 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. 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)
# How a chunk reached the default branch; rebase and push leave no merge commit to bound it.
Landing = typing.Literal["merge", "rebase", "push"]
@@ -27,14 +31,19 @@ class Commit:
@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."""
class Span:
"""A chunk on a default branch, as cut_branch cuts it. 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."""
before: str | None
tip: str
label: str
landed: Landing
merged: str
titles: tuple[str, ...]
@dataclasses.dataclass(frozen=True)
class BranchChunk(Span):
"""A chunk with shipped, each todo.md item its change removed, as scored before it, and fixed_by, the later chunks that fixed code it shipped, newest first."""
shipped: tuple[Item, ...]
fixed_by: tuple[str, ...]
@@ -43,13 +52,17 @@ class BranchChunk:
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]
chunks: list[Span]
owner: dict[str, int]
def cut_branch(log: str) -> Branch:
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:
@@ -59,18 +72,29 @@ def cut_branch(log: str) -> Branch:
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)
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),
landed=landing(run),
merged=utc(run[0].committed),
titles=tuple(titles[index]),
))
return Branch(commits, spans, owner)
def parsed(log: str) -> dict[str, Commit]:
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("\x1e")[1:]:
header, short, rest = record.split("\x1f", 2)
full, committed, authored, *parents = header.split()
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), title, tuple(name for name in files if name))
commits[full] = Commit(full, short, int(committed), int(authored), tuple(parents.split()), title, tuple(name for name in files if name))
return commits
@@ -121,7 +145,7 @@ def shipped(before: str, after: str) -> list[Item]:
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."""
"""The revisions whose todo.md shipped_by 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:
@@ -131,7 +155,12 @@ def plan_revisions(branch: Branch) -> list[str]:
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)
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, read from todo.md's text at each revision plan_revisions named, as plans holds it; none for a chunk with a revision plans lacks, which changed no plan."""
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])))
else:
found.append(())
return found