support git packed refs in version generator

This commit is contained in:
Nathan Franke 2022-07-24 21:09:54 -05:00
parent 3f39625399
commit 77cf65804c
No known key found for this signature in database
GPG Key ID: 082B90CF10A5B648
1 changed files with 13 additions and 1 deletions

View File

@ -125,9 +125,21 @@ def update_version(module_version_string=""):
if os.path.isfile(os.path.join(gitfolder, "HEAD")): if os.path.isfile(os.path.join(gitfolder, "HEAD")):
head = open(os.path.join(gitfolder, "HEAD"), "r", encoding="utf8").readline().strip() head = open(os.path.join(gitfolder, "HEAD"), "r", encoding="utf8").readline().strip()
if head.startswith("ref: "): if head.startswith("ref: "):
head = os.path.join(gitfolder, head[5:]) ref = head[5:]
head = os.path.join(gitfolder, ref)
packedrefs = os.path.join(gitfolder, "packed-refs")
if os.path.isfile(head): if os.path.isfile(head):
githash = open(head, "r").readline().strip() githash = open(head, "r").readline().strip()
elif os.path.isfile(packedrefs):
# Git may pack refs into a single file. This code searches .git/packed-refs file for the current ref's hash.
# https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-pack-refs.html
for line in open(packedrefs, "r").read().splitlines():
if line.startswith("#"):
continue
(line_hash, line_ref) = line.split(" ")
if ref == line_ref:
githash = line_hash
break
else: else:
githash = head githash = head