pytex/PyTeX/build/versioning/git/get_version_info.py

28 lines
840 B
Python
Raw Normal View History

2022-02-09 13:35:38 +01:00
import git
from PyTeX.format.repo_status_info import RepoStatusInfo
from .recent import get_latest_commit
from pathlib import Path
def get_repo_status_info_from_file(file: Path) -> RepoStatusInfo:
try:
repo = git.Repo(file, search_parent_directories=True)
except git.InvalidGitRepositoryError:
return RepoStatusInfo()
return get_repo_status_info(repo)
def get_repo_status_info(repo: git.Repo) -> RepoStatusInfo:
info = RepoStatusInfo()
try:
info.branch = repo.active_branch
except TypeError: # No branch available
pass
try:
info.version = repo.git.describe()
except git.GitCommandError: # No tags available to describe commit
pass
info.commit_hash = get_latest_commit(repo).hexsha
info.dirty = repo.is_dirty(untracked_files=True)
return info