Skip to content

Commit d865be6

Browse files
authored
Merge pull request #20 from OpenVicProject/add/git-info-getter
Add Git information getter
2 parents 9144428 + 89e806a commit d865be6

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

SConstruct

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import SCons
1010
# Local
1111
from build.option_handler import OptionsClass
1212
from build.glob_recursive import GlobRecursive
13+
from build.git_info import get_git_info
1314
from build.cache import show_progress
1415

1516
def normalize_path(val, env):
@@ -271,5 +272,6 @@ def FinalizeOptions():
271272
env.SetupOptions = SetupOptions
272273
env.FinalizeOptions = FinalizeOptions
273274
env.GlobRecursive = GlobRecursive
275+
env.get_git_info = get_git_info
274276

275277
Return("env")

build/git_info.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)