Repository navigation
Expand file tree
/
Copy pathextra_script.py
More file actions
46 lines (38 loc) · 1.51 KB
/
Copy pathextra_script.py
File metadata and controls
46 lines (38 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import subprocess
import sys
Import("env")
def get_git_revision_short_hash():
try:
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip()
except Exception:
return "unknown"
def get_git_commit_count():
# Monotonic build number: total commits reachable from HEAD. Climbs on
# every release. Falls back to 0 when built outside a git checkout.
try:
return int(subprocess.check_output(['git', 'rev-list', '--count', 'HEAD']).decode('utf-8').strip())
except Exception:
return 0
def is_shallow_clone():
try:
out = subprocess.check_output(['git', 'rev-parse', '--is-shallow-repository'])
return out.decode('utf-8').strip() == 'true'
except Exception:
return False
git_revision = get_git_revision_short_hash()
version_build = get_git_commit_count()
# A shallow clone undercounts commits, so the build number would be wrong. The
# OTA server orders releases by it, so CI must never ship such a build.
if is_shallow_clone():
message = (f"Shallow git clone: build number {version_build} is not the real "
"commit count. Fetch full history (git fetch --unshallow).")
if os.environ.get('GITHUB_ACTIONS') == 'true':
sys.stderr.write(f"Error: {message}\n")
env.Exit(1)
print(f"WARNING: {message}")
env.Append(CPPDEFINES=[
("GIT_REV", f'\\"{git_revision}\\"'),
("VERSION_BUILD", version_build),
])
print(f"Current git revision: {git_revision} (build {version_build})")