implement getting git version info

This commit is contained in:
Maximilian Keßler 2022-02-09 13:35:38 +01:00
parent 209829ffe3
commit c3b4a87b51
5 changed files with 50 additions and 1 deletions

View file

@ -13,6 +13,8 @@ from .pytex_file import PyTeXSourceFile
from .relative_path import RelativePath from .relative_path import RelativePath
from .hashing import md5 from .hashing import md5
from .pytex_output_file import PyTeXOutputFile from .pytex_output_file import PyTeXOutputFile
from PyTeX.format.git_version_info import GitVersionInfo
from ..versioning.git.get_version_info import get_repo_status_info_from_file
class PyTeXBuilder: class PyTeXBuilder:
@ -44,6 +46,18 @@ class PyTeXBuilder:
self._files_to_overwrite: Set[RelativePath] = set() self._files_to_overwrite: Set[RelativePath] = set()
self._files_to_build: Set[PyTeXSourceFile] = set() self._files_to_build: Set[PyTeXSourceFile] = set()
self._tmp_dir: Path = self._root_dir / '.pytex' self._tmp_dir: Path = self._root_dir / '.pytex'
self._git_version_info: GitVersionInfo = self._foo()
pass
def _foo(self):
version = GitVersionInfo()
version.repo_version = get_repo_status_info_from_file(
self._root_dir
)
version.pytex_version = get_repo_status_info_from_file(
Path(__file__)
)
return version
@classmethod @classmethod
def is_ignored_in_build_dir(cls, path: RelativePath): def is_ignored_in_build_dir(cls, path: RelativePath):

View file

@ -0,0 +1,27 @@
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

View file

@ -4,7 +4,7 @@ from typing import Union, Optional, List
import git import git
def get_latest_commit(repo): def get_latest_commit(repo) -> git.Commit:
if repo.head.is_detached: if repo.head.is_detached:
return repo.head.commit return repo.head.commit
else: else:

View file

@ -33,6 +33,10 @@ class GitVersionInfo(Config):
else: else:
return self._pytex_version return self._pytex_version
@pytex_version.setter
def pytex_version(self, pytex_version: RepoStatusInfo):
self._pytex_version = pytex_version
@property @property
def repo_version(self) -> RepoStatusInfo: def repo_version(self) -> RepoStatusInfo:
if self._repo_version is None: if self._repo_version is None:
@ -40,6 +44,10 @@ class GitVersionInfo(Config):
else: else:
return self._repo_version return self._repo_version
@repo_version.setter
def repo_version(self, repo_version: RepoStatusInfo):
self._repo_version = repo_version
@property @property
def has_pytex_version(self) -> bool: def has_pytex_version(self) -> bool:
return self._pytex_version is not None return self._pytex_version is not None