From 3866eea1e5b02dbef1bf610979d47be083a59cd0 Mon Sep 17 00:00:00 2001 From: "Knox S. Long" Date: Mon, 30 Sep 2024 17:02:35 -0500 Subject: [PATCH] Fix for getting git_commit --- python/lvmdrp/__init__.py | 47 ++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/python/lvmdrp/__init__.py b/python/lvmdrp/__init__.py index 08603f0a..5779fa2f 100644 --- a/python/lvmdrp/__init__.py +++ b/python/lvmdrp/__init__.py @@ -7,6 +7,7 @@ from tree import Tree from sdss_access.path import Path import subprocess +import inspect NAME = 'lvmdrp' @@ -38,12 +39,42 @@ def setup_paths(release: str = 'sdsswork', replant: bool = False): __version__ = os.getenv("LVMDRP_VERSION") or get_package_version(path=__file__, package_name=NAME) -# NOTE: taken from https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script -def get_git_revision_hash() -> str: - return subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('ascii').strip() -def get_git_revision_short_hash() -> str: - return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip() - - -DRP_COMMIT = get_git_revision_short_hash() \ No newline at end of file +# Function to find the root directory of the Git repository +def get_git_root(path): + try: + # Run the git rev-parse command to get the top-level directory + git_root = subprocess.check_output( + ['git', 'rev-parse', '--show-toplevel'], + cwd=path + ).strip().decode('utf-8') + return git_root + except subprocess.CalledProcessError: + return None + +# Function to get the current commit hash +def get_git_commit_hash(repo_path): + try: + commit_hash = subprocess.check_output( + ['git', 'rev-parse', 'HEAD'], + cwd=repo_path + ).strip().decode('ascii') + return commit_hash + except subprocess.CalledProcessError as e: + print(f"Error getting Git commit hash: {e}") + return None + +# Get the current file location (__init__.py) and traverse to the git root +current_file = os.path.abspath(inspect.getfile(inspect.currentframe())) # Absolute path to __init__.py +current_dir = os.path.dirname(current_file) # Directory of __init__.py + +# Find the git root directory +git_root = get_git_root(current_dir) +# if git_root: +# commit_hash = get_git_commit_hash(git_root) +# print(f"Current commit hash: {commit_hash}") +# else: +# print("Not inside a Git repository") + + +DRP_COMMIT = git_root