Skip to content
Snippets Groups Projects
Commit fdc6c628 authored by Jan David Mol's avatar Jan David Mol
Browse files

L2SS-176: Cache git version to avoid reporting the disk instead of loaded version

parent 7109f83d
No related branches found
No related tags found
1 merge request!35Resolve L2SS-176 "Expose git version"
import git # pip3 install gitpython import git # pip3 install gitpython
import os import os
from functools import lru_cache
def get_repo(starting_directory: str = os.path.dirname(__file__)) -> git.Repo: def get_repo(starting_directory: str = os.path.dirname(__file__)) -> git.Repo:
""" Try finding the repository by traversing up the tree. """ Try finding the repository by traversing up the tree.
...@@ -27,13 +28,28 @@ def get_repo(starting_directory: str = os.path.dirname(__file__)) -> git.Repo: ...@@ -27,13 +28,28 @@ def get_repo(starting_directory: str = os.path.dirname(__file__)) -> git.Repo:
raise git.InvalidGitRepositoryError("Could not find git repository root in {}".format(starting_directory)) raise git.InvalidGitRepositoryError("Could not find git repository root in {}".format(starting_directory))
@lru_cache(maxsize=None)
def get_version(repo: git.Repo = None) -> str: def get_version(repo: git.Repo = None) -> str:
""" Return a version string for the current commit. """ """ Return a version string for the current commit.
There is a practical issue: the repository changes over time, f.e. switching branches with 'git checkout'. We want
to know the version that is running in memory, not the one that is on disk.
As a work-around, we cache the version information, in that it is at least consistent. It is up to the caller
to request the version early enough. """
if repo is None: if repo is None:
repo = get_repo() repo = get_repo()
return "{} [{}{}]".format(repo.active_branch, repo.commit(), " (dirty)" if repo.is_dirty() else "") return "{} [{}]{}".format(repo.active_branch, repo.commit(), " (dirty)" if repo.is_dirty() else "")
# at least cache the current repo version immediately
try:
_ = get_version()
except:
pass
if __name__ == "__main__": if __name__ == "__main__":
print(get_version()) print(get_version())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment