This commit is contained in:
Timothy Jaeryang Baek committed 2026-08-09 12:36:23 -06:00
1 parent 5e65d0a150
commit fcc26371f9
12 files changed
+1382 -260

No files matched your search

+12 -1
View File
@@ -21,7 +21,18 @@ class AuditLevel(str, Enum):
REQUEST_RESPONSE = "REQUEST_RESPONSE"
_SENSITIVE_KEYS = {"password", "api_key", "token", "authorization", "cookie", "secret"}
_SENSITIVE_KEYS = {
"password",
"api_key",
"token",
"authorization",
"cookie",
"secret",
"device_code",
"user_code",
"verification_uri",
"verification_uri_complete",
}
def _redact(value: Any) -> Any:
+229
View File
@@ -0,0 +1,229 @@
"""GitHub CLI operations via subprocess."""
from __future__ import annotations
import asyncio
import json
import os
import re
import shutil
import time
import uuid
from dataclasses import dataclass, field
from typing import Any
from cptr.utils.identity import ExecutionIdentity, env_for, preexec_for
class GhError(Exception):
def __init__(self, message: str, returncode: int = 1):
super().__init__(message)
self.returncode = returncode
@dataclass
class GhLoginSession:
id: str
owner_key: str
proc: asyncio.subprocess.Process
started_at: float
verification_uri: str = ""
user_code: str = ""
output: list[str] = field(default_factory=list)
_login_sessions: dict[str, GhLoginSession] = {}
def _owner_key(identity: ExecutionIdentity) -> str:
return f"{identity.app_user_id or ''}:{identity.username}:{identity.uid or ''}"
def _gh_path() -> str | None:
return shutil.which("gh")
async def run_gh(
args: list[str],
*,
identity: ExecutionIdentity,
cwd: str | None = None,
check: bool = True,
timeout: float = 20,
) -> tuple[int, str, str]:
gh = _gh_path()
if not gh:
raise GhError("GitHub CLI is not installed")
work_dir = cwd or identity.home
env = env_for(identity, work_dir) if identity.is_pam else os.environ.copy()
proc = await asyncio.create_subprocess_exec(
gh,
*args,
cwd=work_dir,
env=env,
preexec_fn=preexec_for(identity) if identity.is_pam else None,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
try:
stdout_bytes, stderr_bytes = await asyncio.wait_for(proc.communicate(), timeout=timeout)
except asyncio.TimeoutError as exc:
proc.kill()
await proc.wait()
raise GhError("GitHub CLI command timed out") from exc
stdout = stdout_bytes.decode("utf-8", errors="replace")
stderr = stderr_bytes.decode("utf-8", errors="replace")
if check and proc.returncode != 0:
raise GhError((stderr or stdout).strip() or "GitHub CLI command failed", proc.returncode)
return proc.returncode or 0, stdout, stderr
async def version(identity: ExecutionIdentity) -> str | None:
if not _gh_path():
return None
code, out, _ = await run_gh(["--version"], identity=identity, check=False, timeout=5)
if code != 0:
return None
return out.splitlines()[0].strip() if out.strip() else None
async def auth_status(identity: ExecutionIdentity, hostname: str | None = None) -> dict[str, Any]:
gh_version = await version(identity)
if gh_version is None:
return {"installed": False, "version": None, "hosts": {}}
args = ["auth", "status", "--json", "hosts"]
if hostname:
args.extend(["--hostname", hostname])
code, out, err = await run_gh(args, identity=identity, check=False, timeout=10)
if not out.strip():
return {"installed": True, "version": gh_version, "hosts": {}, "message": err.strip()}
try:
data = json.loads(out)
except json.JSONDecodeError:
return {"installed": True, "version": gh_version, "hosts": {}, "message": err.strip()}
return {"installed": True, "version": gh_version, "hosts": data.get("hosts") or {}}
def parse_device_prompt(text: str) -> tuple[str, str]:
uri_match = re.search(r"https://\S+/login/device\b", text)
code_match = re.search(r"\b[A-Z0-9]{4}-[A-Z0-9]{4}\b", text)
return (
uri_match.group(0).rstrip(".,)") if uri_match else "",
code_match.group(0) if code_match else "",
)
async def _read_login_stream(session: GhLoginSession, stream: asyncio.StreamReader | None) -> None:
if stream is None:
return
while True:
chunk = await stream.readline()
if not chunk:
return
text = chunk.decode("utf-8", errors="replace")
session.output.append(text)
uri, code = parse_device_prompt("".join(session.output))
if uri:
session.verification_uri = uri
if code:
session.user_code = code
async def start_login(
identity: ExecutionIdentity,
*,
hostname: str = "github.com",
git_protocol: str = "https",
) -> dict[str, Any]:
gh = _gh_path()
if not gh:
raise GhError("GitHub CLI is not installed")
work_dir = identity.home
env = env_for(identity, work_dir) if identity.is_pam else os.environ.copy()
proc = await asyncio.create_subprocess_exec(
gh,
"auth",
"login",
"--web",
"--hostname",
hostname,
"--git-protocol",
git_protocol,
cwd=work_dir,
env=env,
preexec_fn=preexec_for(identity) if identity.is_pam else None,
stdin=asyncio.subprocess.DEVNULL,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
session = GhLoginSession(
id=uuid.uuid4().hex,
owner_key=_owner_key(identity),
proc=proc,
started_at=time.time(),
)
_login_sessions[session.id] = session
asyncio.create_task(_read_login_stream(session, proc.stdout))
asyncio.create_task(_read_login_stream(session, proc.stderr))
deadline = time.time() + 8
while time.time() < deadline and proc.returncode is None:
if session.verification_uri and session.user_code:
break
await asyncio.sleep(0.1)
if proc.returncode is not None and proc.returncode != 0:
_login_sessions.pop(session.id, None)
raise GhError("".join(session.output).strip() or "GitHub CLI login failed", proc.returncode)
return login_status(identity, session.id)
def login_status(identity: ExecutionIdentity, session_id: str) -> dict[str, Any]:
session = _login_sessions.get(session_id)
if not session or session.owner_key != _owner_key(identity):
raise GhError("Login session not found", 404)
state = "pending"
if session.proc.returncode == 0:
state = "complete"
_login_sessions.pop(session_id, None)
elif session.proc.returncode is not None:
state = "failed"
_login_sessions.pop(session_id, None)
elif time.time() - session.started_at > 900:
session.proc.kill()
state = "expired"
_login_sessions.pop(session_id, None)
return {
"session_id": session_id,
"status": state,
"verification_uri": session.verification_uri,
"user_code": session.user_code,
}
def cancel_login(identity: ExecutionIdentity, session_id: str) -> None:
session = _login_sessions.get(session_id)
if not session or session.owner_key != _owner_key(identity):
raise GhError("Login session not found", 404)
if session.proc.returncode is None:
session.proc.kill()
_login_sessions.pop(session_id, None)
async def logout(identity: ExecutionIdentity, hostname: str, user: str | None = None) -> None:
args = ["auth", "logout", "--hostname", hostname]
if user:
args.extend(["--user", user])
await run_gh(args, identity=identity, timeout=15)
async def switch(identity: ExecutionIdentity, hostname: str, user: str) -> None:
await run_gh(["auth", "switch", "--hostname", hostname, "--user", user], identity=identity)
async def setup_git(identity: ExecutionIdentity, hostname: str | None = None) -> None:
args = ["auth", "setup-git"]
if hostname:
args.extend(["--hostname", hostname])
await run_gh(args, identity=identity)
+197 -65
View File
@@ -11,23 +11,39 @@ import os
import sys
from typing import Any
from cptr.utils.identity import ExecutionIdentity, env_for, preexec_for
async def _run(
*args: str,
cwd: str,
check: bool = True,
identity: ExecutionIdentity | None = None,
extra_env: dict[str, str] | None = None,
) -> tuple[int, str, str]:
"""Run a git command and return (returncode, stdout, stderr)."""
proc = await asyncio.create_subprocess_exec(
"git",
"-c",
"core.quotePath=false",
*args,
cwd=cwd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env={**os.environ, "GIT_TERMINAL_PROMPT": "0", "LC_ALL": "C"},
env_extra = {"GIT_TERMINAL_PROMPT": "0", "LC_ALL": "C", **(extra_env or {})}
env = (
env_for(identity, cwd, env_extra)
if identity and identity.is_pam
else {**os.environ, **env_extra}
)
try:
proc = await asyncio.create_subprocess_exec(
"git",
"-c",
"core.quotePath=false",
*args,
cwd=cwd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
env=env,
preexec_fn=preexec_for(identity) if identity and identity.is_pam else None,
)
except FileNotFoundError as exc:
if exc.filename != "git":
raise
raise GitError("Git is not installed", 127) from exc
stdout_bytes, stderr_bytes = await proc.communicate()
stdout = stdout_bytes.decode("utf-8", errors="replace")
stderr = stderr_bytes.decode("utf-8", errors="replace")
@@ -44,25 +60,44 @@ class GitError(Exception):
self.returncode = returncode
async def is_repo(root: str) -> bool:
async def is_repo(root: str, identity: ExecutionIdentity | None = None) -> bool:
"""Check if directory is inside a git repo."""
code, _, _ = await _run(
"rev-parse",
"--is-inside-work-tree",
cwd=root,
check=False,
)
try:
code, _, _ = await _run(
"rev-parse",
"--is-inside-work-tree",
cwd=root,
check=False,
identity=identity,
)
except GitError:
return False
return code == 0
async def status(root: str) -> dict[str, Any]:
async def version(root: str, identity: ExecutionIdentity | None = None) -> str | None:
"""Return the installed git version, if git is available."""
cwd = root if os.path.isdir(root) else "/"
try:
code, out, _ = await _run("--version", cwd=cwd, check=False, identity=identity)
except GitError:
return None
return out.strip() if code == 0 else None
async def status(root: str, identity: ExecutionIdentity | None = None) -> dict[str, Any]:
"""Get repo status using porcelain v2 format."""
# Refresh Git's cached file metadata first so status catches edits whose
# filesystem stat information was stale.
await _run("update-index", "-q", "--refresh", cwd=root, check=False)
await _run("update-index", "-q", "--refresh", cwd=root, check=False, identity=identity)
_, out, _ = await _run(
"status", "--porcelain=v2", "--branch", "--untracked-files=all", cwd=root
"status",
"--porcelain=v2",
"--branch",
"--untracked-files=all",
cwd=root,
identity=identity,
)
branch = ""
@@ -98,7 +133,9 @@ async def status(root: str) -> dict[str, Any]:
entry["status"] = status_text
async def add_numstat(*args: str) -> None:
code, numstat, _ = await _run("diff", "--numstat", *args, cwd=root, check=False)
code, numstat, _ = await _run(
"diff", "--numstat", *args, cwd=root, check=False, identity=identity
)
if code != 0:
return
for line in numstat.splitlines():
@@ -168,7 +205,9 @@ async def status(root: str) -> dict[str, Any]:
await add_numstat("--staged")
# Get remote URL for "View on GitHub/GitLab" link
code, remote_out, _ = await _run("remote", "get-url", "origin", cwd=root, check=False)
code, remote_out, _ = await _run(
"remote", "get-url", "origin", cwd=root, check=False, identity=identity
)
remote_url = remote_out.strip() if code == 0 else ""
return {
@@ -206,12 +245,61 @@ def _count_text_lines(path: str) -> int | None:
return data.count(b"\n") + (0 if data.endswith(b"\n") else 1)
async def _config_value(
root: str,
scope: str,
key: str,
identity: ExecutionIdentity | None = None,
) -> str:
code, out, _ = await _run(
"config", scope, "--get", key, cwd=root, check=False, identity=identity
)
return out.strip() if code == 0 else ""
async def _config_values(
root: str,
key: str,
identity: ExecutionIdentity | None = None,
) -> list[str]:
code, out, _ = await _run(
"config", "--get-all", key, cwd=root, check=False, identity=identity
)
if code != 0:
return []
return [line.strip() for line in out.splitlines() if line.strip()]
async def effective_config(
root: str, identity: ExecutionIdentity | None = None
) -> dict[str, Any]:
"""Return the Git config fields the settings UI needs."""
local_name = await _config_value(root, "--local", "user.name", identity)
global_name = await _config_value(root, "--global", "user.name", identity)
local_email = await _config_value(root, "--local", "user.email", identity)
global_email = await _config_value(root, "--global", "user.email", identity)
code, remote_out, _ = await _run(
"remote", "get-url", "origin", cwd=root, check=False, identity=identity
)
return {
"identity": {
"name": local_name or global_name,
"name_source": "local" if local_name else "global" if global_name else "",
"email": local_email or global_email,
"email_source": "local" if local_email else "global" if global_email else "",
},
"credential_helpers": await _config_values(root, "credential.helper", identity),
"remote_url": remote_out.strip() if code == 0 else "",
}
async def diff(
root: str,
file: str | None = None,
staged: bool = False,
untracked: bool = False,
ignore_whitespace: bool = False,
identity: ExecutionIdentity | None = None,
) -> dict[str, Any]:
"""Get diff output as structured data."""
if untracked and file:
@@ -221,7 +309,7 @@ async def diff(
if ignore_whitespace:
args.append("--ignore-all-space")
args.extend(["--", null_device, file])
_, out, _ = await _run(*args, cwd=root, check=False)
_, out, _ = await _run(*args, cwd=root, check=False, identity=identity)
return _parse_diff(out)
args = ["diff", "--unified=3"]
@@ -232,13 +320,15 @@ async def diff(
if file:
args.extend(["--", file])
_, out, _ = await _run(*args, cwd=root)
_, out, _ = await _run(*args, cwd=root, identity=identity)
return _parse_diff(out)
async def staged_diff(root: str, max_chars: int = 30000) -> str:
async def staged_diff(
root: str, max_chars: int = 30000, identity: ExecutionIdentity | None = None
) -> str:
"""Return the staged patch, capped for lightweight AI requests."""
_, out, _ = await _run("diff", "--staged", "--unified=3", cwd=root)
_, out, _ = await _run("diff", "--staged", "--unified=3", cwd=root, identity=identity)
return out[:max_chars]
@@ -282,21 +372,27 @@ def _parse_diff(raw: str) -> dict[str, Any]:
return {"files": files}
async def stage(root: str, files: list[str]) -> None:
async def stage(
root: str, files: list[str], identity: ExecutionIdentity | None = None
) -> None:
"""Stage files for commit."""
if not files:
return
await _run("add", "--", *files, cwd=root)
await _run("add", "--", *files, cwd=root, identity=identity)
async def unstage(root: str, files: list[str]) -> None:
async def unstage(
root: str, files: list[str], identity: ExecutionIdentity | None = None
) -> None:
"""Unstage files."""
if not files:
return
await _run("restore", "--staged", "--", *files, cwd=root)
await _run("restore", "--staged", "--", *files, cwd=root, identity=identity)
async def discard(root: str, files: list[str]) -> None:
async def discard(
root: str, files: list[str], identity: ExecutionIdentity | None = None
) -> None:
"""Fully discard all changes for files — both staged and unstaged.
Tracked modified/deleted files are unstaged then restored via checkout.
@@ -314,6 +410,7 @@ async def discard(root: str, files: list[str]) -> None:
"--untracked-files=all",
cwd=root,
check=False,
identity=identity,
)
to_unstage: list[str] = [] # staged changes that need unstaging first
@@ -351,11 +448,11 @@ async def discard(root: str, files: list[str]) -> None:
# 1. Unstage any staged changes (reverts index to HEAD)
if to_unstage:
await _run("restore", "--staged", "--", *to_unstage, cwd=root)
await _run("restore", "--staged", "--", *to_unstage, cwd=root, identity=identity)
# 2. Restore working-tree files from HEAD
if to_checkout:
await _run("checkout", "--", *to_checkout, cwd=root)
await _run("checkout", "--", *to_checkout, cwd=root, identity=identity)
# 3. Remove untracked / newly-added files
for f in to_delete:
@@ -364,9 +461,14 @@ async def discard(root: str, files: list[str]) -> None:
os.remove(full)
async def commit(root: str, message: str) -> dict[str, str]:
async def commit(
root: str,
message: str,
identity: ExecutionIdentity | None = None,
author_env: dict[str, str] | None = None,
) -> dict[str, str]:
"""Create a commit. Returns hash and message."""
_, out, _ = await _run("commit", "-m", message, cwd=root)
_, out, _ = await _run("commit", "-m", message, cwd=root, identity=identity, extra_env=author_env)
# Parse "main abc1234] message"
hash_short = ""
for line in out.splitlines():
@@ -383,6 +485,7 @@ async def log(
root: str,
limit: int = 50,
offset: int = 0,
identity: ExecutionIdentity | None = None,
) -> list[dict[str, Any]]:
"""Get commit log."""
fmt = "%H%x00%h%x00%an%x00%aI%x00%s"
@@ -394,6 +497,7 @@ async def log(
"--no-merges",
cwd=root,
check=False,
identity=identity,
)
commits = []
@@ -412,13 +516,18 @@ async def log(
return commits
async def show(root: str, ref: str, ignore_whitespace: bool = False) -> dict[str, Any]:
async def show(
root: str,
ref: str,
ignore_whitespace: bool = False,
identity: ExecutionIdentity | None = None,
) -> dict[str, Any]:
"""Show a commit's diff."""
fmt = "%H%x00%h%x00%an%x00%aI%x00%s"
args = ["show", ref, f"--format={fmt}", "--patch"]
if ignore_whitespace:
args.append("--ignore-all-space")
_, out, _ = await _run(*args, cwd=root)
_, out, _ = await _run(*args, cwd=root, identity=identity)
# First line is the formatted header, rest is diff
lines = out.split("\n", 1)
@@ -439,7 +548,7 @@ async def show(root: str, ref: str, ignore_whitespace: bool = False) -> dict[str
return info
async def branches(root: str) -> dict[str, Any]:
async def branches(root: str, identity: ExecutionIdentity | None = None) -> dict[str, Any]:
"""List branches."""
# Local branches
_, local_out, _ = await _run(
@@ -447,6 +556,7 @@ async def branches(root: str) -> dict[str, Any]:
"--format=%(refname:short)\t%(HEAD)",
cwd=root,
check=False,
identity=identity,
)
# Remote branches
_, remote_out, _ = await _run(
@@ -455,6 +565,7 @@ async def branches(root: str) -> dict[str, Any]:
"--format=%(refname:short)",
cwd=root,
check=False,
identity=identity,
)
current = ""
@@ -550,11 +661,13 @@ def _parse_worktree_list(raw: str, current_root: str = "") -> list[dict[str, Any
return worktrees
async def worktrees(root: str) -> dict[str, Any]:
async def worktrees(root: str, identity: ExecutionIdentity | None = None) -> dict[str, Any]:
"""List worktrees for the current repository."""
_, repo_root_out, _ = await _run("rev-parse", "--show-toplevel", cwd=root)
_, repo_root_out, _ = await _run(
"rev-parse", "--show-toplevel", cwd=root, identity=identity
)
repo_root = repo_root_out.strip() or root
_, out, _ = await _run("worktree", "list", "--porcelain", cwd=root)
_, out, _ = await _run("worktree", "list", "--porcelain", cwd=root, identity=identity)
items = _parse_worktree_list(out, repo_root)
relpath = os.path.relpath(os.path.abspath(root), os.path.abspath(repo_root))
@@ -578,51 +691,57 @@ async def create_worktree(
root: str,
branch: str,
path: str | None = None,
identity: ExecutionIdentity | None = None,
) -> dict[str, str]:
"""Create a new branch-backed worktree beside the current repository."""
_, repo_root_out, _ = await _run("rev-parse", "--show-toplevel", cwd=root)
_, repo_root_out, _ = await _run(
"rev-parse", "--show-toplevel", cwd=root, identity=identity
)
repo_root = repo_root_out.strip() or root
target_path = path or _worktree_path_for_branch(repo_root, branch)
await _run("worktree", "add", "-b", branch, target_path, cwd=root)
await _run("worktree", "add", "-b", branch, target_path, cwd=root, identity=identity)
return {"path": target_path}
async def checkout(root: str, branch: str) -> None:
async def checkout(root: str, branch: str, identity: ExecutionIdentity | None = None) -> None:
"""Switch branch."""
await _run("checkout", branch, cwd=root)
await _run("checkout", branch, cwd=root, identity=identity)
async def create_branch(
root: str,
name: str,
from_ref: str | None = None,
identity: ExecutionIdentity | None = None,
) -> None:
"""Create and switch to a new branch."""
args = ["checkout", "-b", name]
if from_ref:
args.append(from_ref)
await _run(*args, cwd=root)
await _run(*args, cwd=root, identity=identity)
async def delete_branch(root: str, name: str) -> None:
async def delete_branch(root: str, name: str, identity: ExecutionIdentity | None = None) -> None:
"""Delete a local branch."""
await _run("branch", "-d", name, cwd=root)
await _run("branch", "-d", name, cwd=root, identity=identity)
async def rename_branch(root: str, old_name: str, new_name: str) -> None:
async def rename_branch(
root: str, old_name: str, new_name: str, identity: ExecutionIdentity | None = None
) -> None:
"""Rename a local branch."""
await _run("branch", "-m", old_name, new_name, cwd=root)
await _run("branch", "-m", old_name, new_name, cwd=root, identity=identity)
async def pull(root: str) -> dict[str, Any]:
async def pull(root: str, identity: ExecutionIdentity | None = None) -> dict[str, Any]:
"""Pull from remote."""
code, out, err = await _run("pull", cwd=root, check=False)
code, out, err = await _run("pull", cwd=root, check=False, identity=identity)
return {"ok": code == 0, "message": (out + err).strip()}
async def fetch(root: str) -> dict[str, Any]:
async def fetch(root: str, identity: ExecutionIdentity | None = None) -> dict[str, Any]:
"""Fetch remote refs without merging."""
code, out, err = await _run("fetch", "--prune", cwd=root, check=False)
code, out, err = await _run("fetch", "--prune", cwd=root, check=False, identity=identity)
return {"ok": code == 0, "message": (out + err).strip()}
@@ -632,6 +751,7 @@ async def push(
set_upstream: bool = False,
branch: str | None = None,
remote: str = "origin",
identity: ExecutionIdentity | None = None,
) -> dict[str, Any]:
"""Push to remote. Use *set_upstream* for first-time branch publish."""
args = ["push"]
@@ -639,36 +759,42 @@ async def push(
args.extend(["-u", remote, branch or "HEAD"])
if force:
args.append("--force-with-lease")
code, out, err = await _run(*args, cwd=root, check=False)
code, out, err = await _run(*args, cwd=root, check=False, identity=identity)
return {"ok": code == 0, "message": (out + err).strip()}
async def uncommit(root: str) -> dict[str, str]:
async def uncommit(root: str, identity: ExecutionIdentity | None = None) -> dict[str, str]:
"""Undo the last commit, moving its changes back to the staging area.
Uses ``git reset --soft HEAD~1``, or ``git update-ref -d HEAD`` for root
commits (no parent).
"""
# Grab info about the commit we're about to undo
_, log_out, _ = await _run("log", "-1", "--format=%H%x00%h%x00%s", cwd=root, check=False)
_, log_out, _ = await _run(
"log", "-1", "--format=%H%x00%h%x00%s", cwd=root, check=False, identity=identity
)
parts = log_out.strip().split("\x00")
undone_hash = parts[1] if len(parts) >= 2 else ""
undone_msg = parts[2] if len(parts) >= 3 else ""
# Check if HEAD~1 exists (root commits have no parent)
code, _, _ = await _run("rev-parse", "--verify", "HEAD~1", cwd=root, check=False)
code, _, _ = await _run(
"rev-parse", "--verify", "HEAD~1", cwd=root, check=False, identity=identity
)
if code != 0:
# Root commit: remove HEAD ref, keeps index (staged files) intact
await _run("update-ref", "-d", "HEAD", cwd=root)
await _run("update-ref", "-d", "HEAD", cwd=root, identity=identity)
else:
await _run("reset", "--soft", "HEAD~1", cwd=root)
await _run("reset", "--soft", "HEAD~1", cwd=root, identity=identity)
return {"hash": undone_hash, "message": undone_msg}
async def stash_list(root: str) -> list[dict[str, str]]:
async def stash_list(root: str, identity: ExecutionIdentity | None = None) -> list[dict[str, str]]:
"""List stashes."""
_, out, _ = await _run("stash", "list", "--format=%gd%x00%s", cwd=root, check=False)
_, out, _ = await _run(
"stash", "list", "--format=%gd%x00%s", cwd=root, check=False, identity=identity
)
stashes = []
for line in out.strip().splitlines():
parts = line.split("\x00", 1)
@@ -677,16 +803,22 @@ async def stash_list(root: str) -> list[dict[str, str]]:
return stashes
async def stash_save(root: str, message: str | None = None) -> dict[str, Any]:
async def stash_save(
root: str, message: str | None = None, identity: ExecutionIdentity | None = None
) -> dict[str, Any]:
"""Stash changes."""
args = ["stash", "push", "--include-untracked"]
if message:
args.extend(["-m", message])
code, out, err = await _run(*args, cwd=root, check=False)
code, out, err = await _run(*args, cwd=root, check=False, identity=identity)
return {"ok": code == 0, "message": (out + err).strip()}
async def stash_pop(root: str, index: int = 0) -> dict[str, Any]:
async def stash_pop(
root: str, index: int = 0, identity: ExecutionIdentity | None = None
) -> dict[str, Any]:
"""Pop a stash."""
code, out, err = await _run("stash", "pop", f"stash@{{{index}}}", cwd=root, check=False)
code, out, err = await _run(
"stash", "pop", f"stash@{{{index}}}", cwd=root, check=False, identity=identity
)
return {"ok": code == 0, "message": (out + err).strip()}