Something went wrong. Try again.
Integrated repository, worktree, git stacker, and project manager
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169"""Domain helpers shared across stacker commands.
Pure business-logic utilities — every CLI-specific argparse helper movedto `project_manager.cli._shared` during the cyclopts migration. Thethings remaining here all resolve/construct values the `StackerService`needs; they know nothing about how the user invoked the command."""
from project_manager import renderfrom project_manager.cli._params import resolve_repo, resolve_repo_optionalfrom project_manager.cli._shared import StackerScopefrom project_manager.paths import Pathsfrom project_manager.stacker import git, locate, selectorsfrom project_manager.stacker.db import StackerDBfrom project_manager.stacker.models import ( ParentLocator, Scope, ScopeSpec, SelectorTarget,)from project_manager.stacker.service import StackerService
# Re-exported so existing `_common.resolve_repo(...)` call sites and tests# keep working; canonical definitions live in `cli._params`.__all__ = [ "emit", "emit_result", "resolve_branch", "resolve_on_spec", "resolve_repo", "resolve_repo_optional", "scope_of", "scope_spec", "service", "stderr_progress", "target",]
def service(paths: Paths) -> StackerService: db = StackerDB(paths.stacker_db()) return StackerService(db, paths, progress=stderr_progress)
def stderr_progress(msg: str) -> None: render.console(stderr=True).print(f"pm: {msg}", markup=False, highlight=False)
def resolve_branch(branch: str | None, paths: Paths) -> str: if branch: return branch here = locate.slot_for_cwd(paths) if here is None: raise git.GitError("pass <branch> (or run from inside a pm worktree slot).") current = git.current_branch(here.path) if not current: raise git.GitError(f"worktree {here.path} is detached; pass <branch> explicitly.") return current
def target(repo: str | None, branch: str | None, paths: Paths) -> SelectorTarget: return SelectorTarget( repo_name=resolve_repo(repo, paths), branch=resolve_branch(branch, paths), )
def resolve_on_spec( paths: Paths, repo_name: str, on_spec: str, *, fallback_branch: str | None = None,) -> ParentLocator: """Resolve an `--on` value into a `ParentLocator`.
Keywords `current` / `parent` resolve against the cwd's pm slot; `fallback_branch` is substituted when `--on current` is used in a context where the target branch overrides the slot's current (e.g. `create --replace <b>` adopts `b` and `--on current` should point to the slot's branch). Any other value is a literal branch name. """ if on_spec == "current": here = locate.slot_for_cwd(paths) if here is None: raise git.GitError("--on current requires running from a pm worktree slot.") current = fallback_branch or git.current_branch(here.path) if not current: raise git.GitError("--on current: current worktree has no branch checked out.") return ParentLocator(repo_name=repo_name, branch=current) if on_spec == "parent": here = locate.slot_for_cwd(paths) if here is None: raise git.GitError("--on parent requires running from a pm worktree slot.") current = git.current_branch(here.path) if not current: raise git.GitError("--on parent: current worktree is detached; pass a branch instead.") svc = service(paths) tracked = svc.db.get_branch(repo_name, current) if tracked is None: raise git.GitError(f"{current} is not tracked; cannot resolve --on parent.") return ParentLocator( repo_name=tracked.parent_repo_name, branch=tracked.parent_branch, ) # Literal branch reference — may include cross-repo `repo:branch` form. return selectors.resolve_parent_for_base(paths, repo_name, on_spec)
def scope_of(s: StackerScope) -> Scope: return "all" if s.all else "current"
def scope_spec(s: StackerScope, *, only: bool = False) -> ScopeSpec: return ScopeSpec( scope=scope_of(s), skip_ancestors=s.skip_ancestors, skip_descendants=s.skip_descendants, only=only, from_branch=s.from_branch, )
def emit(result: str) -> int: """Print a service result string (rich markup resolved) and return 0.""" render.emit_markup(result) return 0
def emit_result( result: str, *, json: bool, command: str, repo: str | None, branch: str | None, paths: Paths, ok: bool = True, code: int = 0,) -> int: """Emit a service RESULT as markup text, or as a uniform JSON envelope.
Every stacker verb routes through here so it supports `--json`. The envelope is `{ok, command, repo, branch, message, operation}` where `message` is the raw rich-markup result (clients render it) and `operation` is the post-command paused-op state (or None) — so a client can surface a conflict/pause and offer continue/abort.
`ok`/`code` let a command report a non-fatal negative outcome (e.g. `pr refresh` finding no PR) as `{ok: false}` with the same exit code it would return in text mode. Markup-emitting commands call this for both modes (text → `emit`); plain-`print` commands call it only on their `--json` path so their text output is unchanged. """ if not json: return emit(result) op = service(paths).db.get_operation(repo) if repo else None render.emit_json( { "ok": ok, "command": command, "repo": repo, "branch": branch, "message": result, "operation": op.__pm_json__() if op is not None else None, } ) return code