auto-commit-service/src/auto_commit_service/git/repository.py
2026-04-17 21:20:13 -07:00

72 lines
1.7 KiB
Python

"""Repository abstraction."""
from dataclasses import dataclass, field
from pathlib import Path
@dataclass
class Repository:
"""Represents a git repository to be processed."""
name: str
path: Path
remote: str = "origin"
branch: str = "main"
enabled: bool = True
def __post_init__(self) -> None:
"""Validate repository path."""
if isinstance(self.path, str):
self.path = Path(self.path)
@property
def git_dir(self) -> Path:
"""Get the .git directory path."""
return self.path / ".git"
@property
def exists(self) -> bool:
"""Check if the repository exists.
Supports both regular repos (.git is a directory) and
submodules (.git is a gitlink file).
"""
return self.path.exists() and (self.git_dir.is_dir() or self.git_dir.is_file())
def __str__(self) -> str:
return f"Repository({self.name} @ {self.path})"
@dataclass
class GitStatus:
"""Result of git status command."""
has_changes: bool
staged: list[str] = field(default_factory=list)
modified: list[str] = field(default_factory=list)
untracked: list[str] = field(default_factory=list)
deleted: list[str] = field(default_factory=list)
branch: str = "main"
ahead: int = 0
behind: int = 0
@dataclass
class CommitResult:
"""Result of git commit command."""
success: bool
commit_hash: str | None = None
message: str | None = None
error: str | None = None
@dataclass
class PushResult:
"""Result of git push command."""
success: bool
remote: str = "origin"
branch: str = "main"
error: str | None = None
rejected: bool = False