From fdc6c6283712a5efaab7977d3e5c77dc7202229c Mon Sep 17 00:00:00 2001 From: Jan David Mol <mol@astron.nl> Date: Tue, 18 May 2021 17:18:18 +0200 Subject: [PATCH] L2SS-176: Cache git version to avoid reporting the disk instead of loaded version --- devices/util/lofar_git.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/devices/util/lofar_git.py b/devices/util/lofar_git.py index 7ba42cb01..f6c93ff22 100644 --- a/devices/util/lofar_git.py +++ b/devices/util/lofar_git.py @@ -1,5 +1,6 @@ import git # pip3 install gitpython import os +from functools import lru_cache def get_repo(starting_directory: str = os.path.dirname(__file__)) -> git.Repo: """ 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: 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: - """ 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: 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__": print(get_version()) -- GitLab