54 lines
1.2 KiB
Python
54 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from acupedia_ops.common.process import run_command
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class RepositoryStatus:
|
|
path: Path
|
|
is_git_repo: bool
|
|
branch: str | None
|
|
dirty: bool | None
|
|
revision: str | None
|
|
|
|
|
|
def repository_status(path: Path) -> RepositoryStatus:
|
|
if not (path / ".git").exists():
|
|
return RepositoryStatus(
|
|
path=path,
|
|
is_git_repo=False,
|
|
branch=None,
|
|
dirty=None,
|
|
revision=None,
|
|
)
|
|
|
|
branch_result = run_command(
|
|
["git", "branch", "--show-current"],
|
|
cwd=path,
|
|
capture=True,
|
|
check=False,
|
|
)
|
|
revision_result = run_command(
|
|
["git", "rev-parse", "--short", "HEAD"],
|
|
cwd=path,
|
|
capture=True,
|
|
check=False,
|
|
)
|
|
dirty_result = run_command(
|
|
["git", "status", "--porcelain"],
|
|
cwd=path,
|
|
capture=True,
|
|
check=False,
|
|
)
|
|
|
|
return RepositoryStatus(
|
|
path=path,
|
|
is_git_repo=True,
|
|
branch=branch_result.stdout.strip() or None,
|
|
dirty=bool(dirty_result.stdout.strip()),
|
|
revision=revision_result.stdout.strip() or None,
|
|
)
|