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

82 lines
4.0 KiB
Python

import pathlib
from branch_chunks.fixes import blamed, fixing_changes, FixingChange, removed_work, Traced, with_fixes
from branch_chunks.cut import BranchChunk, cut_branch, LOG, NOT_WORK, plan_revisions, shipped_by
from failure import Failure
from git_cli.git_repo import git, Git, text_at
from project_config.main import located
from record_store.stores import store
# Where branch_chunks reads the plan and the records, by the setting that names each in a project's config.
READ_AT = {"plan": NOT_WORK[0], "comprehension-history": NOT_WORK[1], "reading-cost": NOT_WORK[2]}
def unsupported(checkout: str | None, home: pathlib.Path, run: Git = git) -> str | None:
"""Why branch_chunks cannot tell what the chunks of checkout's project shipped, else the current directory's: its config under home puts the plan or a record where their commits are not read. None where it can, as for a checkout no config names."""
config = located(checkout, home, run)
if isinstance(config, Failure):
return None
held = [(str(config.paths["plan"]), config.tracked("plan")), *((record.label, record.tracked) for record in (store(config, name) for name in READ_AT if name != "plan"))]
elsewhere = [label for (label, tracked), path in zip(held, READ_AT.values()) if tracked != path]
if not elsewhere:
return None
return f"the project's config puts {', '.join(elsewhere)} elsewhere than {', '.join(READ_AT.values())} in the checkout, the only paths whose commits are read so far"
def branch_chunks(default: str, run: Git) -> 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 must run git in the checkout's top: the diff names each path from the top, while blame and a pathspec read a path from the current directory."""
# 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: Git) -> 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: Git) -> 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: Git) -> 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. Only hunk headers, paths and commits are read, so a file's text that is not UTF-8 is replaced.
diff = run("diff", "-U0", "--inter-hunk-context=0", "-M", "--no-color", "--no-ext-diff", "--no-textconv", "--ignore-submodules", "--src-prefix=a/", "--dst-prefix=b/", before, tip, errors="replace")
if isinstance(diff, Failure):
return diff
traced: set[str] = set()
for path, spans in removed_work(diff).items():
# --no-ignore-revs-file drops each revs file the config names, since blame fails on one that is missing. An empty --ignore-revs-file= doesn't: blame opens the configured files first.
blame = run("blame", "--porcelain", "--no-textconv", "--no-ignore-revs-file", *(f"-L{start},+{count}" for start, count in spans), before, "--", path, errors="replace")
if isinstance(blame, Failure):
return blame
traced |= blamed(blame)
return traced