Skip to content

Commit

Permalink
Fix for getting git_commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kslong committed Sep 30, 2024
1 parent cb47057 commit 3866eea
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions python/lvmdrp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from tree import Tree
from sdss_access.path import Path
import subprocess
import inspect


NAME = 'lvmdrp'
Expand Down Expand Up @@ -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()
# 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

0 comments on commit 3866eea

Please sign in to comment.