implement getting git version info
This commit is contained in:
parent
209829ffe3
commit
c3b4a87b51
5 changed files with 50 additions and 1 deletions
|
@ -13,6 +13,8 @@ from .pytex_file import PyTeXSourceFile
|
|||
from .relative_path import RelativePath
|
||||
from .hashing import md5
|
||||
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:
|
||||
|
@ -44,6 +46,18 @@ class PyTeXBuilder:
|
|||
self._files_to_overwrite: Set[RelativePath] = set()
|
||||
self._files_to_build: Set[PyTeXSourceFile] = set()
|
||||
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
|
||||
def is_ignored_in_build_dir(cls, path: RelativePath):
|
||||
|
|
27
PyTeX/build/versioning/git/get_version_info.py
Normal file
27
PyTeX/build/versioning/git/get_version_info.py
Normal 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
|
|
@ -4,7 +4,7 @@ from typing import Union, Optional, List
|
|||
import git
|
||||
|
||||
|
||||
def get_latest_commit(repo):
|
||||
def get_latest_commit(repo) -> git.Commit:
|
||||
if repo.head.is_detached:
|
||||
return repo.head.commit
|
||||
else:
|
||||
|
|
|
@ -33,6 +33,10 @@ class GitVersionInfo(Config):
|
|||
else:
|
||||
return self._pytex_version
|
||||
|
||||
@pytex_version.setter
|
||||
def pytex_version(self, pytex_version: RepoStatusInfo):
|
||||
self._pytex_version = pytex_version
|
||||
|
||||
@property
|
||||
def repo_version(self) -> RepoStatusInfo:
|
||||
if self._repo_version is None:
|
||||
|
@ -40,6 +44,10 @@ class GitVersionInfo(Config):
|
|||
else:
|
||||
return self._repo_version
|
||||
|
||||
@repo_version.setter
|
||||
def repo_version(self, repo_version: RepoStatusInfo):
|
||||
self._repo_version = repo_version
|
||||
|
||||
@property
|
||||
def has_pytex_version(self) -> bool:
|
||||
return self._pytex_version is not None
|
||||
|
|
Loading…
Reference in a new issue