from pathlib import Path from git import Repo repo_path = Path(__file__).parent.parent def get_current_revision(repo_path: Path = repo_path) -> str: repo = Repo(repo_path) commit_id = repo.head.object.hexsha return commit_id def get_latest_revision(path: Path, repo_path=repo_path) -> str: repo = Repo(repo_path) if not path.exists(): raise ValueError("path does not exist.") last_commit_for_file = next(repo.iter_commits(paths=path, max_count=1)) return last_commit_for_file.hexsha def check_is_ancestor(ancestor_rev: str, rev: str, repo_path=repo_path) -> bool: repo = Repo(repo_path) return repo.is_ancestor(repo.commit(ancestor_rev), repo.commit(rev))