40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import pathlib
|
|
|
|
from loop_claude.effects import Effects
|
|
from loop_claude.stop import Stop
|
|
|
|
|
|
def checkout_root(cwd: pathlib.Path, effects: Effects) -> str | Stop:
|
|
try:
|
|
found = effects.git(["rev-parse", "--show-toplevel"], str(cwd))
|
|
except OSError as failure:
|
|
return Stop(1, f"could not start git: {failure.strerror}")
|
|
if found.returncode != 0:
|
|
return Stop(1, found.stderr.strip())
|
|
return found.stdout.strip()
|
|
|
|
|
|
def worktrees(root: str, effects: Effects) -> list[str]:
|
|
found = [root]
|
|
try:
|
|
listing = effects.git(["worktree", "list", "--porcelain"], root)
|
|
except OSError:
|
|
return found
|
|
if listing.returncode == 0:
|
|
found += [line.split(" ", 1)[1] for line in listing.stdout.splitlines() if line.startswith("worktree ")]
|
|
return sorted(set(found))
|
|
|
|
|
|
def project_key(root: str, code: pathlib.Path) -> str:
|
|
return root.removeprefix(f"{code}/").lstrip("/")
|
|
|
|
|
|
def read_utf8(folder: str, name: str, consequence: str) -> str | Stop:
|
|
try:
|
|
return pathlib.Path(folder, name).read_text(encoding="utf-8")
|
|
except OSError as failure:
|
|
return Stop(1, f"{name} in {folder}: {failure.strerror} - {consequence}.")
|
|
except UnicodeDecodeError as failure:
|
|
line = failure.object.count(b"\n", 0, failure.start) + 1
|
|
return Stop(1, f"{name} in {folder} is not UTF-8 on line {line} - {consequence}.")
|