1+ import os
2+ import subprocess
3+ from pathlib import Path
4+
5+ base_folder = Path (__file__ ).resolve ().parent
6+
7+ def get_git_tag ():
8+ git_tag = os .getenv ("OPENVIC_TAG" , "<tag missing>" )
9+ if os .path .exists (".git" ):
10+ try :
11+ result = subprocess .check_output (["git" , "describe" , "--tags" , "--abbrev=0" ], encoding = "utf-8" ).strip ()
12+ if result != "" :
13+ git_tag = result
14+ except (subprocess .CalledProcessError , OSError ):
15+ # `git` not found in PATH.
16+ pass
17+
18+ return git_tag
19+
20+ def get_git_release ():
21+ git_release = os .getenv ("OPENVIC_RELEASE" , "<release missing>" )
22+ if os .path .exists (".git" ):
23+ try :
24+ result = subprocess .check_output (["gh" , "release" , "list" , "--json" , "name" , "-q" , ".[0] | .name" ], encoding = "utf-8" ).strip ()
25+ if result != "" :
26+ git_release = result
27+ except (subprocess .CalledProcessError , OSError ):
28+ # `gh` not found in PATH.
29+ git_tag = get_git_tag ()
30+ if git_tag != "<tag missing>" :
31+ git_release = git_tag
32+
33+ return git_release
34+
35+ def get_git_hash ():
36+ # Parse Git hash if we're in a Git repo.
37+ git_hash = "0000000000000000000000000000000000000000"
38+ git_folder = ".git"
39+
40+ if os .path .isfile (".git" ):
41+ with open (".git" , "r" , encoding = "utf-8" ) as file :
42+ module_folder = file .readline ().strip ()
43+ if module_folder .startswith ("gitdir: " ):
44+ git_folder = module_folder [8 :]
45+
46+ if os .path .isfile (os .path .join (git_folder , "HEAD" )):
47+ with open (os .path .join (git_folder , "HEAD" ), "r" , encoding = "utf8" ) as file :
48+ head = file .readline ().strip ()
49+ if head .startswith ("ref: " ):
50+ ref = head [5 :]
51+ # If this directory is a Git worktree instead of a root clone.
52+ parts = git_folder .split ("/" )
53+ if len (parts ) > 2 and parts [- 2 ] == "worktrees" :
54+ git_folder = "/" .join (parts [0 :- 2 ])
55+ head = os .path .join (git_folder , ref )
56+ packedrefs = os .path .join (git_folder , "packed-refs" )
57+ if os .path .isfile (head ):
58+ with open (head , "r" , encoding = "utf-8" ) as file :
59+ git_hash = file .readline ().strip ()
60+ elif os .path .isfile (packedrefs ):
61+ # Git may pack refs into a single file. This code searches .git/packed-refs file for the current ref's hash.
62+ # https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-pack-refs.html
63+ for line in open (packedrefs , "r" , encoding = "utf-8" ).read ().splitlines ():
64+ if line .startswith ("#" ):
65+ continue
66+ (line_hash , line_ref ) = line .split (" " )
67+ if ref == line_ref :
68+ git_hash = line_hash
69+ break
70+ else :
71+ git_hash = head
72+
73+ # Get the UNIX timestamp of the build commit.
74+ git_timestamp = 0
75+ if os .path .exists (".git" ):
76+ try :
77+ git_timestamp = subprocess .check_output (
78+ ["git" , "log" , "-1" , "--pretty=format:%ct" , "--no-show-signature" , git_hash ], encoding = "utf-8"
79+ )
80+ except (subprocess .CalledProcessError , OSError ):
81+ # `git` not found in PATH.
82+ pass
83+
84+ return {
85+ "git_hash" : git_hash ,
86+ "git_timestamp" : git_timestamp ,
87+ }
88+
89+ def get_git_info ():
90+ return {** get_git_hash (), "git_tag" : get_git_tag (), "git_release" : get_git_release () }
0 commit comments