Returns {branch: worktree_path}. ``cwd`` selects the repository when the process working directory is not the graph's repo (MCP servers started outside the project, #5535).
(cwd: str | Path | None = None)
| 403 | # ── Worktree mapping ────────────────────────────────────────────────────────── |
| 404 | |
| 405 | def fetch_worktrees(cwd: str | Path | None = None) -> dict[str, str]: |
| 406 | """Returns {branch: worktree_path}. |
| 407 | |
| 408 | ``cwd`` selects the repository when the process working directory is not |
| 409 | the graph's repo (MCP servers started outside the project, #5535). |
| 410 | """ |
| 411 | try: |
| 412 | result = subprocess.run( |
| 413 | ["git", "worktree", "list", "--porcelain"], |
| 414 | stdin=subprocess.DEVNULL, |
| 415 | capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=10, |
| 416 | cwd=str(cwd) if cwd is not None else None, |
| 417 | ) |
| 418 | if result.returncode != 0: |
| 419 | return {} |
| 420 | except (subprocess.TimeoutExpired, FileNotFoundError): |
| 421 | return {} |
| 422 | |
| 423 | mapping: dict[str, str] = {} |
| 424 | current_path = None |
| 425 | for line in result.stdout.splitlines(): |
| 426 | if not line: |
| 427 | current_path = None # blank line = record separator; reset to avoid leaking across detached HEADs |
| 428 | elif line.startswith("worktree "): |
| 429 | current_path = line[9:] |
| 430 | elif line.startswith("branch refs/heads/") and current_path: |
| 431 | mapping[line[18:]] = current_path |
| 432 | return mapping |
| 433 | |
| 434 | |
| 435 | # ── Graph impact analysis ───────────────────────────────────────────────────── |