from functools import lru_cache import os import subprocess from modules.utils import constants git = os.environ.get("GIT", "git") @lru_cache() def commit_hash(): try: return subprocess.check_output( [git, "-C", constants.ROOT_DIR, "rev-parse", "HEAD"], shell=False, encoding="utf8", ).strip() except Exception: return "" @lru_cache() def git_tag(): try: return subprocess.check_output( [git, "-C", constants.ROOT_DIR, "describe", "--tags"], shell=False, encoding="utf8", ).strip() except Exception: try: changelog_md = os.path.join( os.path.dirname(os.path.dirname(__file__)), "CHANGELOG.md" ) with open(changelog_md, "r", encoding="utf-8") as file: line = next((line.strip() for line in file if line.strip()), "") line = line.replace("## ", "") return line except Exception: return "" @lru_cache() def branch_name(): try: return subprocess.check_output( [git, "-C", constants.ROOT_DIR, "rev-parse", "--abbrev-ref", "HEAD"], shell=False, encoding="utf8", ).strip() except Exception: return "" if __name__ == "__main__": print(commit_hash()) print(git_tag()) print(branch_name())