import subprocess import os from functools import lru_cache @lru_cache(maxsize=1) def get_git_commit_hash(short=True): """ Return the current Git commit hash. If not in a git repo or git not available, returns 'unknown'. """ try: # Run from the project root (where manage.py is) project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) cmd = ['git', 'rev-parse', '--short', 'HEAD'] result = subprocess.run( cmd, cwd=project_root, capture_output=True, text=True, timeout=5 ) if result.returncode == 0: return result.stdout.strip() except Exception: pass return "unknown"