| 262 | |
| 263 | |
| 264 | def get_git_commit_hash(package_name): |
| 265 | try: |
| 266 | # Import the package to locate its path |
| 267 | package = importlib.import_module(package_name) |
| 268 | # Get the path to the package using inspect |
| 269 | package_path = os.path.dirname(inspect.getfile(package)) |
| 270 | |
| 271 | # Navigate up to the Git repository root if the package is inside a subdirectory |
| 272 | git_repo_path = os.path.abspath(os.path.join(package_path, "..")) |
| 273 | git_dir = os.path.join(git_repo_path, ".git") |
| 274 | |
| 275 | if os.path.isdir(git_dir): |
| 276 | # Run the git command to get the current commit hash |
| 277 | commit_hash = ( |
| 278 | subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=git_repo_path).strip().decode("utf-8") |
| 279 | ) |
| 280 | return commit_hash |
| 281 | else: |
| 282 | return None |
| 283 | except Exception as e: |
| 284 | return f"Error: {str(e)}" |