Something went wrong. Try again.
small gleam coding and (not yet) persistent agent daemon with a detachable cli forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197"""worktrees: create, list, query, inspect, and remove isolated VCS worktrees."""
from __future__ import annotations
import osfrom dataclasses import dataclassfrom typing import cast
from albedo_api import Host, PythonApi, Record
host: Host
@dataclass(frozen=True)class Worktree: """A handle to a version-control worktree under ~/.albedo/wt."""
id: str name: str path: str workspace: str repo: str origin: str kind: str revision: str | None = None branch: str | None = None session: str | None = None exists: bool = True
async def remove(self, *, force: bool = False) -> bool: """Remove this worktree from version control and delete its directory.""" return await worktrees.remove(self, force=force)
def _worktree(raw: dict) -> Worktree: return Worktree( id=str(raw["id"]), name=str(raw["name"]), path=str(raw["path"]), workspace=str(raw.get("workspace") or raw["path"]), repo=str(raw["repo"]), origin=str(raw.get("origin") or raw["repo"]), kind=str(raw["kind"]), revision=cast("str | None", raw.get("revision")), branch=cast("str | None", raw.get("branch")), session=cast("str | None", raw.get("session")), exists=bool(raw.get("exists", True)), )
def _target(target: Worktree | str) -> str: key = target.id if isinstance(target, Worktree) else target if not isinstance(key, str) or not key.strip(): raise TypeError("target is a Worktree or a worktree id, name, or path") return key.strip()
def _directory(path: str) -> str: return os.path.abspath(os.path.expanduser(path))
def _arguments(**given: object) -> dict[str, object]: return {key: value for key, value in given.items() if value is not None}
WorktreeList = list[Worktree]
def _cwd(cwd: str | None) -> str | None: return None if cwd is None else _directory(cwd)
class Worktrees: """Create, find, and remove worktrees."""
async def create( self, name: str | None = None, *, revision: str | None = None, branch: str | None = None, kind: str | None = None, cwd: str | None = None, ) -> Worktree: """Make a worktree of the repository enclosing `cwd` (default: your workspace).
Without `name` one is chosen. `revision` and `branch` say what to start from and what to call it, in the owning VCS's terms. `kind` ("jj" or "git") picks the VCS when several own the repository; jj wins by default. """ arguments = _arguments( name=name, revision=revision, branch=branch, kind=kind, cwd=_cwd(cwd) ) return _worktree(cast(dict, await host("worktrees.create", arguments)))
async def get(self, target: Worktree | str, *, cwd: str | None = None) -> Worktree: """The worktree with this id, name, or path; a Worktree is looked up afresh.""" arguments = _arguments(target=_target(target), cwd=_cwd(cwd)) return _worktree(cast(dict, await host("worktrees.get", arguments)))
async def query( self, query: str = "", *, repo: str | None = None, kind: str | None = None, session: object = None, all: bool = False, ) -> Record: """Worktrees matching every filter given, as {worktrees, count}.
`query` matches, case-insensitively, the id, name, path, workspace, repo, kind, revision, branch, and the owning session's name or id. `repo` is a repository path, or a substring of one; `kind` is "jj", "git", ...; `session` is "self", "parent", a name, a session id, or a handle, and keeps the worktrees that session works in. Without `all` or `repo` only your own repository is searched. """ arguments: dict[str, object] = {} substring = "" if repo is not None: if repo.startswith(("/", "~", "./", "../")) or repo in (".", ".."): arguments["repo"] = _directory(repo) else: substring = repo.casefold() all = True if all: arguments["all"] = True if session is not None: target = getattr(session, "id", session) if not isinstance(target, str) or not target.strip(): raise TypeError( 'session is "self", "parent", a name, a session id, or an agent handle' ) arguments["to"] = target.strip() raw = cast(dict, await host("worktrees.list", arguments)) sessions = cast(dict, raw.get("sessions") or {}) needle = query.casefold() wanted = None if kind is None else kind.casefold() matched = [] for item in cast(list, raw["worktrees"]): wt = _worktree(item) owner = str(sessions.get(wt.session or "", {}).get("name", "")) if wanted is not None and wt.kind.casefold() != wanted: continue if substring and substring not in wt.repo.casefold(): continue fields = ( wt.id, wt.name, wt.path, wt.workspace, wt.repo, wt.kind, wt.revision or "", wt.branch or "", wt.session or "", owner, ) if needle and not any(needle in field.casefold() for field in fields): continue matched.append(wt) return Record({"worktrees": matched, "count": len(matched)})
async def list( self, query: str = "", *, repo: str | None = None, kind: str | None = None, session: object = None, all: bool = False, ) -> WorktreeList: """The worktrees `query` finds, as a list.""" found = await self.query(query, repo=repo, kind=kind, session=session, all=all) return cast(WorktreeList, found.worktrees)
async def remove( self, target: Worktree | str, *, force: bool = False, cwd: str | None = None ) -> bool: """Delete a worktree from its VCS and disk; refuses mid-turn sessions without `force`.""" arguments = _arguments(target=_target(target), force=force, cwd=_cwd(cwd)) return bool(await host("worktrees.remove", arguments))
worktrees = Worktrees()
async def setup(api: PythonApi) -> dict[str, object]: global host host = api.host return { "worktrees": worktrees, "Worktree": Worktree, "WorktreeError": api.HostError, }