#!/usr/bin/env python3 """Report duplicate task headings and unfinished Git worktrees without changing them.""" import argparse import re import subprocess from pathlib import Path TASK = re.compile(r"^##\s+((?:[A-Z][A-Z0-9]*-)?[A-Z]*\d{3,})\s+[—-]", re.M) def ids(paths): found = {} for path in paths: for match in TASK.finditer(Path(path).read_text(encoding="utf-8")): line = Path(path).read_text(encoding="utf-8")[:match.start()].count("\n") + 1 found.setdefault(match.group(1), []).append(f"{path}:{line}") return found def git(*args): return subprocess.check_output(["git", *args], text=True).strip() def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("task_files", nargs="*", help="task registers to compare") parser.add_argument("--worktrees", action="store_true", help="report worktree changes and handoffs") args = parser.parse_args() collisions = {key: loc for key, loc in ids(args.task_files).items() if len(loc) > 1} for key, loc in sorted(collisions.items()): print(f"COLLISION {key}: {', '.join(loc)}") if args.worktrees: for block in git("worktree", "list", "--porcelain").split("\n\n"): if not block: continue path = Path(block.splitlines()[0].removeprefix("worktree ")) status = subprocess.check_output(["git", "-C", str(path), "status", "--porcelain"], text=True).strip() handoff = path / "docs/HANDOFF.md" print(f"WORKTREE {path}: {'CHANGES' if status else 'clean'}; handoff {'present' if handoff.is_file() and handoff.read_text().strip() else 'MISSING'}") return 1 if collisions else 0 if __name__ == "__main__": raise SystemExit(main())