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_cli.git_repo import Git, text_at 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