Inject commit hash to FW, show on frontend
This commit is contained in:
40
scripts/git_hash.py
Normal file
40
scripts/git_hash.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""
|
||||
PlatformIO pre-build script: injects the short git commit hash into firmware
|
||||
as GIT_HASH (a C string literal), and writes web/version.js so the web UI
|
||||
can compare against it.
|
||||
|
||||
Usage: referenced from platformio.ini as:
|
||||
extra_scripts = pre:scripts/git_hash.py
|
||||
"""
|
||||
import subprocess, os, re
|
||||
|
||||
Import("env") # noqa: F821 — PlatformIO injects this
|
||||
|
||||
def get_git_hash():
|
||||
try:
|
||||
h = subprocess.check_output(
|
||||
["git", "rev-parse", "--short=7", "HEAD"],
|
||||
cwd=env.subst("$PROJECT_DIR"), # noqa: F821
|
||||
stderr=subprocess.DEVNULL,
|
||||
).decode().strip()
|
||||
# Verify it looks like a hex hash (safety check)
|
||||
if re.fullmatch(r"[0-9a-f]{4,12}", h):
|
||||
return h
|
||||
except Exception:
|
||||
pass
|
||||
return "unknown"
|
||||
|
||||
git_hash = get_git_hash()
|
||||
print(f"[git_hash] short hash = {git_hash}")
|
||||
|
||||
# ── Inject into firmware build ────────────────────────────────────────────────
|
||||
env.Append(CPPDEFINES=[("GIT_HASH", f'\\"{git_hash}\\"')]) # noqa: F821
|
||||
|
||||
# ── Write web/version.js ──────────────────────────────────────────────────────
|
||||
web_dir = os.path.join(env.subst("$PROJECT_DIR"), "web") # noqa: F821
|
||||
ver_file = os.path.join(web_dir, "version.js")
|
||||
os.makedirs(web_dir, exist_ok=True)
|
||||
with open(ver_file, "w") as f:
|
||||
f.write(f"// Auto-generated by scripts/git_hash.py — do not edit\n")
|
||||
f.write(f"const FIRMWARE_BUILD_HASH = '{git_hash}';\n")
|
||||
print(f"[git_hash] wrote {ver_file}")
|
||||
Reference in New Issue
Block a user