"""The later chunks that fixed code each chunk on a default branch shipped: its escaped defects. A later chunk fixed code an earlier chunk shipped where one of its changes removes or rewrites lines that `git blame` traces to a commit the earlier chunk brought. Two kinds of change count: the chunk's whole change, where it ships a todo.md item marked `defect` or `security`; and a round commit of the chunk answering a 🔴 that a reviewer marked `[misread]`. Each later chunk counts at most once against each earlier chunk.""" import dataclasses import re from branch_chunks.cut import Branch, BranchChunk from round_titles import title_counts # The Exempt cells, per ~/.claude/rules/todo-items.md, that mark an item fixing a defect. FIXES = ("defect", "security") HUNK = re.compile(r"@@ -([0-9]+)(?:,([0-9]+))? \+[0-9]+(?:,([0-9]+))? @@") BLAME_HEADER = re.compile(r"([0-9a-f]{40}|[0-9a-f]{64}) [0-9]+ [0-9]+") def fixing_changes(branch: Branch) -> list[tuple[int, str, str]]: """Each change that fixes a defect, as its chunk's index, the commit before it and its tip: the whole change of each chunk that ships a FIXES item, and each round commit answering a 🔴 a reviewer marked `[misread]`.""" changes = [(index, chunk.before, chunk.tip) for index, chunk in enumerate(branch.chunks) if chunk.before is not None and any(item.exempt in FIXES for item in chunk.shipped)] for commit in branch.commits.values(): counts = title_counts(commit.title) # A title counts misread 🔴 and 🟡 findings together, so it takes one with both a 🔴 and a misread finding to answer a misread 🔴. if counts and counts.red and counts.misread and commit.parents: changes.append((branch.owner[commit.hash], commit.parents[0], commit.hash)) return changes def with_fixes(branch: Branch, traced: list[tuple[int, set[str]]]) -> list[BranchChunk]: """The branch's chunks, each naming the later chunks that fixed code it shipped, from the commits each fixing change traced to, paired with its chunk's index.""" fixed: dict[int, set[int]] = {} for index, commits in traced: for commit in commits: # A commit no chunk owns cannot be one before can reach; take it as the fixing chunk's own. if branch.owner.get(commit, index) > index: fixed.setdefault(branch.owner[commit], set()).add(index) return [dataclasses.replace(chunk, fixed_by=tuple(branch.chunks[later].label for later in sorted(fixed.get(index, ())))) for index, chunk in enumerate(branch.chunks)] def removed(diff: str) -> dict[str, list[tuple[int, int]]]: """The old lines each hunk of a `git diff -U0` removes or rewrites, as its first line and count, by the path the file had before; none for a path git quotes, as it does one holding a byte it cannot print.""" spans: dict[str, list[tuple[int, int]]] = {} path, left = None, 0 for row in diff.splitlines(): if left and row[:1] in ("-", "+", " "): left -= 1 elif row.startswith("diff --git "): path, left = None, 0 elif row.startswith("--- "): # Git ends a path holding a space with a tab. path = row[len("--- a/"):].removesuffix("\t") if row.startswith("--- a/") else None elif hunk := HUNK.match(row): start, count = int(hunk.group(1)), int(hunk.group(2) or 1) left = count + int(hunk.group(3) or 1) if path and count: spans.setdefault(path, []).append((start, count)) return spans def blamed(porcelain: str) -> set[str]: """The commit each line of `git blame --porcelain` output comes from.""" return {header.group(1) for row in porcelain.splitlines() if (header := BLAME_HEADER.match(row))}