Fix current git commit hash detection when on a detached head
@@ -40,14 +40,18 @@ except ImportError:
git_commit_head_file = os.path.join(RANDO_ROOT_PATH, ".git", "HEAD")
if os.path.isfile(git_commit_head_file):
with open(git_commit_head_file, "r") as f:
- head_file_contents = f.read()
+ head_file_contents = f.read().strip()
if head_file_contents.startswith("ref: "):
- relative_path_to_hash_file = head_file_contents[len("ref: "):].strip()
+ # Normal head, HEAD file has a reference to a branch which contains the commit hash
+ relative_path_to_hash_file = head_file_contents[len("ref: "):]
path_to_hash_file = os.path.join(RANDO_ROOT_PATH, ".git", relative_path_to_hash_file)
if os.path.isfile(path_to_hash_file):
with open(path_to_hash_file, "r") as f:
hash_file_contents = f.read()
version_suffix = "_" + hash_file_contents[:7]
+ elif re.search(r"^[0-9a-f]{40}$", head_file_contents):
+ # Detached head, commit hash directly in the HEAD file
+ version_suffix = "_" + head_file_contents[:7]
VERSION += version_suffix
