File size: 869 Bytes
f2ad4e1
 
 
 
 
 
 
 
 
 
 
 
 
3e28a50
f2ad4e1
 
 
 
 
3e28a50
 
 
 
 
f2ad4e1
 
3e28a50
 
 
f2ad4e1
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 | None:
    repo = Repo(repo_path)

    if not path.exists():
        raise ValueError("path does not exist.")

    try:
        last_commit_for_file = next(repo.iter_commits(paths=path, max_count=1))
        return last_commit_for_file.hexsha
    except StopIteration:
        return None


def check_is_ancestor(ancestor_rev: str, rev: str | None, repo_path=repo_path) -> bool:
    if rev is None:  # e.g. when cases are not submitted yet
        return False
    repo = Repo(repo_path)
    return repo.is_ancestor(repo.commit(ancestor_rev), repo.commit(rev))