43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from git import Repo, Remote
|
|
|
|
from app.config import GitConfig, get_settings
|
|
|
|
|
|
class GitService:
|
|
def __init__(self):
|
|
self._settings = get_settings()
|
|
self._repo = self._check_preconditions(self._settings.git)
|
|
if self._repo.head.ref.name != self._settings.git.branch:
|
|
self._repo.git.checkout(self._settings.git.branch)
|
|
self._origin: Remote = self._repo.remotes.origin
|
|
|
|
|
|
def get_modified_compose(self) -> str | None:
|
|
self._update()
|
|
return self._diff()
|
|
|
|
@staticmethod
|
|
def _check_preconditions(config: GitConfig) -> Repo:
|
|
def clone():
|
|
return Repo.clone_from(config.url, config.path, branch=config.branch)
|
|
import os
|
|
if not config.path.exists():
|
|
return clone()
|
|
if not (config.path / ".git").exists():
|
|
os.rmdir(config.path)
|
|
return clone()
|
|
return Repo(config.path)
|
|
|
|
def _update(self):
|
|
self._origin.pull()
|
|
|
|
def _diff(self) -> str | None:
|
|
diff = self._repo.head.commit.diff("HEAD~1")
|
|
composes = [f for f in diff if f.a_path.endswith("docker-compose.yml")]
|
|
match len(composes):
|
|
case 0:
|
|
return None
|
|
case 1:
|
|
return composes[0].a_path
|
|
case _:
|
|
raise Exception("Multiple compose files modified")
|