From be9d55159ddf5a10b2bc0bef15b0d6f66520a194 Mon Sep 17 00:00:00 2001 From: Nathan Franke Date: Sun, 24 Jul 2022 21:09:54 -0500 Subject: [PATCH] support git packed refs in version generator (cherry picked from commit 77cf65804c732431574a99063f2132567d54a6be) --- methods.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/methods.py b/methods.py index a8255fb6770..957d2e6a032 100644 --- a/methods.py +++ b/methods.py @@ -107,9 +107,21 @@ def update_version(module_version_string=""): if os.path.isfile(os.path.join(gitfolder, "HEAD")): head = open_utf8(os.path.join(gitfolder, "HEAD"), "r").readline().strip() 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): 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: githash = head