Files
claude-config/bin/branch_chunks/read.py
T

62 lines
2.6 KiB
Python

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
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.
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
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:
plan = text_at(revision, "todo.md", run)
if isinstance(plan, Failure):
return plan
plans[revision] = plan
return plans
def traces(changes: list[FixingChange], run: Run) -> list[Traced] | Failure:
"""The commits traced_to traces for each change."""
found = []
for change in changes:
traced = traced_to(change.before, change.tip, run)
if isinstance(traced, Failure):
return 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 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_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):
return blame
traced |= blamed(blame)
return traced