from __future__ import annotations import contextlib from dataclasses import dataclass, field from pathlib import Path from typing import TYPE_CHECKING, Literal from project_manager.stacker import git, selectors from project_manager.stacker.models import ( Details, MergedStyle, PRState, TrackedBranch, ) from . import format as fmt if TYPE_CHECKING: from project_manager.stacker.ctx import StackerCtx from .ls import RenderOptions IconKind = Literal[ "local_only", "no_pr", "pr_open", "merged", "pr_draft", "pr_approved", "pr_open_comments", "pr_approved_comments", ] _SYMBOL: dict[IconKind, str] = { "local_only": "·", "no_pr": "○", "pr_open": "●", "merged": "■", "pr_draft": "▼", "pr_open_comments": "⚑", "pr_approved": "✓", "pr_approved_comments": "◼", } # Per-status fg colors for the symbol (and for name/suffix at color_mode # >= "title"). Mirrors gitstack's apply_color_mode map. _ICON_COLOR: dict[IconKind, str | None] = { "local_only": None, "no_pr": None, "pr_open": None, "merged": None, # merged styled via dim/strike, not fg "pr_draft": None, "pr_open_comments": "red", "pr_approved": "green", "pr_approved_comments": "red", # comments win visually over approval } _ICON_MEANING: dict[IconKind, str] = { "local_only": "local only", "no_pr": "pushed, no PR", "pr_open": "PR open", "merged": "merged", "pr_draft": "PR draft", "pr_open_comments": "PR open w/ comments", "pr_approved": "PR approved", "pr_approved_comments": "PR approved w/ comments", } _OFFLINE_ICONS: tuple[IconKind, ...] = ( "local_only", "no_pr", "pr_open", "merged", ) _ONLINE_ICONS: tuple[IconKind, ...] = ( "pr_draft", "pr_open_comments", "pr_approved", "pr_approved_comments", ) @dataclass(frozen=True) class GraphCtx: """Per-repo state for graph rendering; threaded through recursion. `details` and `render_opts` are tree-level, not per-node, so they live here rather than being passed as parallel args to keep `render_graph_node` within the project's arity budget. `pr_states` is preloaded once per repo so the renderer never hits the DB per-node. """ repo_name: str items: list[TrackedBranch] live: dict[str, git.WorktreeInfo] details: Details render_opts: RenderOptions pr_states: dict[str, PRState] # Branch checked out in the cwd's pm slot for this repo, or None. current_branch: str | None = None # Per-branch project tag. Populated for any branch checked out in a # project-owned pm slot, so the `[]` token can surface on # every row (not just current) and help the reader navigate to the # worktree that hosts the branch. branch_projects: dict[str, str] = field(default_factory=dict) # Per-branch worktree label keyed on `(repo_name, branch)`. When # set, the node emits `(