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:
+47
-18
@@ -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
|
||||
|
||||
+40
-14
@@ -1,10 +1,11 @@
|
||||
"""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
|
||||
import typing
|
||||
|
||||
from branch_chunks.cut import Branch, BranchChunk
|
||||
from branch_chunks.cut import Branch, BranchChunk, NOT_WORK
|
||||
from todo_table import Item
|
||||
from round_titles import title_counts
|
||||
|
||||
# The Exempt cells, per ~/.claude/rules/todo-items.md, that mark an item fixing a defect.
|
||||
@@ -13,26 +14,51 @@ 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)]
|
||||
class FixingChange(typing.NamedTuple):
|
||||
"""A change that fixes a defect: the index of its chunk, the commit before it and its tip."""
|
||||
chunk: int
|
||||
before: str
|
||||
tip: str
|
||||
|
||||
|
||||
class Traced(typing.NamedTuple):
|
||||
"""The commits that last changed the lines a fixing change removes, with the index of the change's chunk."""
|
||||
chunk: int
|
||||
commits: set[str]
|
||||
|
||||
|
||||
def fixing_changes(branch: Branch, shipped: list[tuple[Item, ...]]) -> list[FixingChange]:
|
||||
"""Each change that fixes a defect: the whole change of each chunk that ships a FIXES item, as shipped_by lists them, and each round commit answering a 🔴 a reviewer marked `[misread]`."""
|
||||
changes = []
|
||||
for index, (chunk, items) in enumerate(zip(branch.chunks, shipped)):
|
||||
if chunk.before is not None and any(item.exempt in FIXES for item in items):
|
||||
changes.append(FixingChange(index, chunk.before, chunk.tip))
|
||||
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))
|
||||
changes.append(FixingChange(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."""
|
||||
def with_fixes(branch: Branch, shipped: list[tuple[Item, ...]], traced: list[Traced]) -> list[BranchChunk]:
|
||||
"""The branch's chunks, each with the items it shipped, as shipped_by lists them, and the later chunks that fixed code it shipped, from the commits each fixing change traced to."""
|
||||
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)]
|
||||
for change in traced:
|
||||
for commit in change.commits:
|
||||
# Blame at a commit names only commits the branch's log holds; one it lacks counts against no chunk.
|
||||
if commit in branch.owner and branch.owner[commit] > change.chunk:
|
||||
fixed.setdefault(branch.owner[commit], set()).add(change.chunk)
|
||||
chunks = []
|
||||
for index, (span, items) in enumerate(zip(branch.chunks, shipped)):
|
||||
fixed_by = tuple(branch.chunks[later].label for later in sorted(fixed.get(index, ())))
|
||||
chunks.append(BranchChunk(**vars(span), shipped=items, fixed_by=fixed_by))
|
||||
return chunks
|
||||
|
||||
|
||||
def removed_work(diff: str) -> dict[str, list[tuple[int, int]]]:
|
||||
"""The old lines of a chunk's work each hunk of a `git diff -U0` removes or rewrites, as removed reads them: outside the plan and the records."""
|
||||
return {path: spans for path, spans in removed(diff).items() if path not in NOT_WORK}
|
||||
|
||||
|
||||
def removed(diff: str) -> dict[str, list[tuple[int, int]]]:
|
||||
|
||||
+19
-24
@@ -1,63 +1,58 @@
|
||||
import typing
|
||||
|
||||
from branch_chunks.fixes import blamed, fixing_changes, removed, with_fixes
|
||||
from branch_chunks.cut import BranchChunk, cut_branch, LOG, NOT_WORK, plan_revisions, with_shipped
|
||||
from branch_chunks.fixes import blamed, fixing_changes, FixingChange, removed_work, Traced, with_fixes
|
||||
from branch_chunks.cut import BranchChunk, cut_branch, LOG, plan_revisions, shipped_by
|
||||
from failure import Failure
|
||||
|
||||
# Runs git with the given arguments in the checkout's top: blame and a pathspec read a path from the current directory, and the diff names each from the top.
|
||||
Run = typing.Callable[..., str | Failure]
|
||||
from git_repo import Run, text_at
|
||||
|
||||
|
||||
def branch_chunks(default: str, run: Run) -> list[BranchChunk] | Failure:
|
||||
"""Each chunk on the default branch's first-parent line, newest first, with the todo.md items it shipped and the later chunks that fixed code it shipped."""
|
||||
"""Each chunk on the default branch's first-parent line, newest first, with the todo.md items it shipped and the later chunks that fixed code it shipped.
|
||||
|
||||
run runs git in the checkout's top: blame and a pathspec read a path from the current directory, and the diff names each from the top."""
|
||||
# The -- keeps a file named like the branch from making it ambiguous.
|
||||
log = run(*LOG, default, "--")
|
||||
if isinstance(log, Failure):
|
||||
return log
|
||||
branch = cut_branch(log)
|
||||
if isinstance(branch, Failure):
|
||||
return branch
|
||||
plans = plans_at(plan_revisions(branch), run)
|
||||
if isinstance(plans, Failure):
|
||||
return plans
|
||||
branch = with_shipped(branch, plans)
|
||||
traced = traces(fixing_changes(branch), run)
|
||||
return traced if isinstance(traced, Failure) else with_fixes(branch, traced)
|
||||
shipped = shipped_by(branch, plans)
|
||||
traced = traces(fixing_changes(branch, shipped), run)
|
||||
return traced if isinstance(traced, Failure) else with_fixes(branch, shipped, traced)
|
||||
|
||||
|
||||
def plans_at(revisions: list[str], run: Run) -> dict[str, str] | Failure:
|
||||
"""todo.md's text at each revision, empty where the revision holds none."""
|
||||
plans = {}
|
||||
for revision in revisions:
|
||||
listed = run("ls-tree", "--name-only", revision, "--", "todo.md")
|
||||
if isinstance(listed, Failure):
|
||||
return listed
|
||||
plan = run("show", f"{revision}:todo.md") if listed.strip() else ""
|
||||
plan = text_at(revision, "todo.md", run)
|
||||
if isinstance(plan, Failure):
|
||||
return plan
|
||||
plans[revision] = plan
|
||||
return plans
|
||||
|
||||
|
||||
def traces(changes: list[tuple[int, str, str]], run: Run) -> list[tuple[int, set[str]]] | Failure:
|
||||
"""The commits each change traced_to traces, paired with the change's chunk's index."""
|
||||
def traces(changes: list[FixingChange], run: Run) -> list[Traced] | Failure:
|
||||
"""The commits traced_to traces for each change."""
|
||||
found = []
|
||||
for index, before, tip in changes:
|
||||
traced = traced_to(before, tip, run)
|
||||
for change in changes:
|
||||
traced = traced_to(change.before, change.tip, run)
|
||||
if isinstance(traced, Failure):
|
||||
return traced
|
||||
found.append((index, traced))
|
||||
found.append(Traced(change.chunk, traced))
|
||||
return found
|
||||
|
||||
|
||||
def traced_to(before: str, tip: str, run: Run) -> set[str] | Failure:
|
||||
"""The commits that last changed each line the change from before to tip removes or rewrites, outside the plan and the records."""
|
||||
"""The commits that last changed each line of a chunk's work that the change from before to tip removes or rewrites."""
|
||||
# The prefixes and options keep the checkout's diff config out of the output.
|
||||
diff = run("diff", "-U0", "-M", "--no-color", "--no-ext-diff", "--no-textconv", "--ignore-submodules", "--src-prefix=a/", "--dst-prefix=b/", before, tip)
|
||||
if isinstance(diff, Failure):
|
||||
return diff
|
||||
traced: set[str] = set()
|
||||
for path, spans in removed(diff).items():
|
||||
if path in NOT_WORK:
|
||||
continue
|
||||
for path, spans in removed_work(diff).items():
|
||||
# An empty --ignore-revs-file drops a configured one, which blame fails on where the file is missing.
|
||||
blame = run("blame", "--porcelain", "--no-textconv", "--ignore-revs-file=", *(f"-L{start},+{count}" for start, count in spans), before, "--", path)
|
||||
if isinstance(blame, Failure):
|
||||
|
||||
@@ -5,11 +5,12 @@ import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from branch_chunks.fixes import blamed, fixing_changes, removed, with_fixes
|
||||
from branch_chunks.cut import Branch, BranchChunk, chunk_log, Commit, landing, shipped
|
||||
from branch_chunks.fixes import blamed, fixing_changes, removed, removed_work, Traced, with_fixes
|
||||
from branch_chunks.cut import Branch, chunk_log, Commit, cut_branch, landing, NOT_WORK, shipped, Span
|
||||
from branch_chunks.read import branch_chunks, traced_to
|
||||
from failure import Failure
|
||||
from git_repo import git
|
||||
@@ -31,8 +32,8 @@ def item(id_: int, exempt: str = "") -> Item:
|
||||
return Item(id_, (0, 1, 0), exempt, f"**Item {id_}.**", {}, (), ())
|
||||
|
||||
|
||||
def chunk(label: str, shipped: tuple[Item, ...] = ()) -> BranchChunk:
|
||||
return BranchChunk(f"{label}-before", f"{label}-tip", label, "merge", "2026-10-13T10:00:00Z", (), shipped, ())
|
||||
def span(label: str) -> Span:
|
||||
return Span(f"{label}-before", f"{label}-tip", label, "merge", "2026-10-13T10:00:00Z", ())
|
||||
|
||||
|
||||
def commit(hash_: str, committed: int, authored: int, *parents: str, title: str = "") -> Commit:
|
||||
@@ -61,6 +62,19 @@ class Shipped(unittest.TestCase):
|
||||
self.assertEqual(shipped(todo(FIRST, "| x | 0.1.0 | | **Bad.** | 2 | 3 | 5 | 6 | 7 | 1 | 9.0 |"), todo()), [])
|
||||
|
||||
|
||||
class CutBranch(unittest.TestCase):
|
||||
def test_reads_a_title_that_holds_a_control_character(self):
|
||||
found = cut_branch(f"\x00{'a' * 40} 100 100 \x1faaaaaaa\x1fTake \x1e and \x1f\n\nb.py\n")
|
||||
self.assertNotIsInstance(found, Failure)
|
||||
self.assertEqual([found.titles for found in found.chunks], [("Take \x1e and \x1f",)])
|
||||
|
||||
def test_fails_on_a_commit_it_cannot_read(self):
|
||||
self.assertIsInstance(cut_branch(f"\x00{'a' * 40} 100\x1faaaaaaa\n"), Failure)
|
||||
|
||||
def test_fails_on_a_log_that_holds_no_commit(self):
|
||||
self.assertIsInstance(cut_branch(""), Failure)
|
||||
|
||||
|
||||
class ChunkLog(unittest.TestCase):
|
||||
def test_a_merge_is_a_chunk_of_its_own_and_joins_no_run_below_it(self):
|
||||
self.assertEqual(cut(commit("m", 100, 100, "a", "b"), commit("c", 100, 100, "a")), [("m", "m", 100, "merge"), ("c", "c", 100, "push")])
|
||||
@@ -83,8 +97,9 @@ class ChunkLog(unittest.TestCase):
|
||||
|
||||
class FixingChanges(unittest.TestCase):
|
||||
def test_takes_the_whole_change_of_each_chunk_that_ships_a_defect_or_a_security_item(self):
|
||||
branch = Branch({}, [chunk("d", (item(9, "security"),)), chunk("c", (item(8),)), chunk("b", (item(6, "defect"), item(7))), chunk("a")], {})
|
||||
self.assertEqual(fixing_changes(branch), [(0, "d-before", "d-tip"), (2, "b-before", "b-tip")])
|
||||
branch = Branch({}, [span("d"), span("c"), span("b"), span("a")], {})
|
||||
shipped = [(item(9, "security"),), (item(8),), (item(6, "defect"), item(7)), ()]
|
||||
self.assertEqual(fixing_changes(branch, shipped), [(0, "d-before", "d-tip"), (2, "b-before", "b-tip")])
|
||||
|
||||
def test_takes_each_round_commit_whose_title_counts_both_a_red_and_a_misread_finding(self):
|
||||
titles = {
|
||||
@@ -95,19 +110,24 @@ class FixingChanges(unittest.TestCase):
|
||||
"r5": "Do the work",
|
||||
}
|
||||
commits = {name: commit(name, 100, 100, f"{name}-parent", title=title) for name, title in titles.items()}
|
||||
branch = Branch(commits, [chunk("b"), chunk("a")], {"r1": 1, "r2": 0, "r3": 0, "r4": 0, "r5": 0})
|
||||
self.assertEqual(fixing_changes(branch), [(1, "r1-parent", "r1")])
|
||||
branch = Branch(commits, [span("b"), span("a")], {"r1": 1, "r2": 0, "r3": 0, "r4": 0, "r5": 0})
|
||||
self.assertEqual(fixing_changes(branch, [(), ()]), [(1, "r1-parent", "r1")])
|
||||
|
||||
|
||||
class WithFixes(unittest.TestCase):
|
||||
def test_names_each_later_chunk_once_against_each_chunk_whose_commits_its_fixes_traced_to_newest_first(self):
|
||||
branch = Branch({}, [chunk("d"), chunk("c"), chunk("b"), chunk("a")], {"a1": 3, "a2": 3, "b1": 2, "c1": 1, "d1": 0})
|
||||
found = with_fixes(branch, [(1, {"a1", "a2"}), (0, {"a1", "b1", "c1"}), (1, {"a2"}), (0, {"a2"})])
|
||||
branch = Branch({}, [span("d"), span("c"), span("b"), span("a")], {"a1": 3, "a2": 3, "b1": 2, "c1": 1, "d1": 0})
|
||||
found = with_fixes(branch, [()] * 4, [Traced(1, {"a1", "a2"}), Traced(0, {"a1", "b1", "c1"}), Traced(1, {"a2"}), Traced(0, {"a2"})])
|
||||
self.assertEqual([found.fixed_by for found in found], [(), ("d",), ("d",), ("d", "c")])
|
||||
|
||||
def test_counts_no_fix_of_its_own_chunks_commit_a_newer_chunks_or_one_no_chunk_brought(self):
|
||||
branch = Branch({}, [chunk("b"), chunk("a")], {"b1": 0, "a1": 1})
|
||||
self.assertEqual([found.fixed_by for found in with_fixes(branch, [(1, {"a1", "b1", "elsewhere"})])], [(), ()])
|
||||
branch = Branch({}, [span("b"), span("a")], {"b1": 0, "a1": 1})
|
||||
self.assertEqual([found.fixed_by for found in with_fixes(branch, [(), ()], [Traced(1, {"a1", "b1", "elsewhere"})])], [(), ()])
|
||||
|
||||
def test_keeps_each_chunk_as_cut_with_the_items_it_shipped(self):
|
||||
(found,) = with_fixes(Branch({}, [span("a")], {}), [(item(4),)], [])
|
||||
self.assertEqual((found.before, found.tip, found.label, found.landed, found.merged, found.titles), ("a-before", "a-tip", "a", "merge", "2026-10-13T10:00:00Z", ()))
|
||||
self.assertEqual([shipped.id for shipped in found.shipped], [4])
|
||||
|
||||
|
||||
class Removed(unittest.TestCase):
|
||||
@@ -149,6 +169,12 @@ class Removed(unittest.TestCase):
|
||||
self.assertEqual(removed(diff), {"a.md": [(3, 1)]})
|
||||
|
||||
|
||||
class RemovedWork(unittest.TestCase):
|
||||
def test_leaves_out_the_plan_and_the_records(self):
|
||||
diff = "".join(f"--- a/{path}\n+++ b/{path}\n@@ -2 +2 @@\n-x\n+y\n" for path in ("a.py", *NOT_WORK))
|
||||
self.assertEqual(removed_work(diff), {"a.py": [(2, 1)]})
|
||||
|
||||
|
||||
class Blamed(unittest.TestCase):
|
||||
def test_reads_the_commit_of_each_line_from_porcelain_output(self):
|
||||
first, second = "a" * 40, "b" * 40
|
||||
@@ -157,18 +183,11 @@ class Blamed(unittest.TestCase):
|
||||
|
||||
|
||||
class TracedTo(unittest.TestCase):
|
||||
DIFF = "--- a/a.py\n+++ b/a.py\n@@ -2 +2 @@\n-x\n+y\n--- a/todo.md\n+++ b/todo.md\n@@ -5 +4,0 @@\n-| 6 |\n--- a/docs/reading-cost.md\n+++ b/docs/reading-cost.md\n@@ -1 +1 @@\n-a\n+b\n"
|
||||
DIFF = "--- a/a.py\n+++ b/a.py\n@@ -2 +2 @@\n-x\n+y\n--- a/todo.md\n+++ b/todo.md\n@@ -5 +4,0 @@\n-| 6 |\n"
|
||||
|
||||
def test_blames_the_lines_a_change_removes_before_it_leaving_out_the_plan_and_the_records(self):
|
||||
blames = []
|
||||
|
||||
def run(*args):
|
||||
if args[0] == "diff":
|
||||
return self.DIFF
|
||||
blames.append(args)
|
||||
return f"{'a' * 40} 2 2 1\n\tx\n"
|
||||
self.assertEqual(traced_to("before", "tip", run), {"a" * 40})
|
||||
self.assertEqual([(args[-1], "-L2,+1" in args, "before" in args) for args in blames], [("a.py", True, True)])
|
||||
def test_names_the_commits_blame_names_for_the_work_a_change_removes(self):
|
||||
blames = {"a.py": f"{'a' * 40} 2 2 1\n\tx\n", "todo.md": f"{'b' * 40} 5 5 1\n\t| 6 |\n"}
|
||||
self.assertEqual(traced_to("before", "tip", lambda *args: self.DIFF if args[0] == "diff" else blames[args[-1]]), {"a" * 40})
|
||||
|
||||
def test_fails_where_git_cannot_blame(self):
|
||||
self.assertEqual(traced_to("before", "tip", lambda *args: self.DIFF if args[0] == "diff" else Failure("no blame")), Failure("no blame"))
|
||||
@@ -206,7 +225,8 @@ class BranchChunks(unittest.TestCase):
|
||||
def test_reads_each_chunk_with_the_items_it_shipped_and_the_later_chunks_that_fixed_it(self):
|
||||
shipped = self.merge("four", "Do four", {"b.py": "x\ny\n", "todo.md": todo(SECOND, FIX)}, "2026-10-13T10:00:00Z")
|
||||
fixed = self.merge("six", "Fix six", {"b.py": "x\nY\n", "todo.md": todo(SECOND)}, "2026-10-14T10:00:00Z")
|
||||
found = branch_chunks("main", functools.partial(git, cwd=str(self.checkout)))
|
||||
with unittest.mock.patch.dict(os.environ, self.env, clear=True):
|
||||
found = branch_chunks("main", functools.partial(git, cwd=str(self.checkout)))
|
||||
self.assertNotIsInstance(found, Failure)
|
||||
self.assertEqual([(found.label, found.landed, found.merged, [item.id for item in found.shipped], found.fixed_by) for found in found[:2]], [(fixed, "merge", "2026-10-14T10:00:00Z", [6], ()), (shipped, "merge", "2026-10-13T10:00:00Z", [4], (fixed,))])
|
||||
self.assertEqual(sorted(found[1].titles), ["Do four", "Merge four"])
|
||||
|
||||
Reference in New Issue
Block a user