git_rev.py (1323B)
1 # PlatformIO pre-build hook: inject the git short rev as the GIT_REV macro so the 2 # firmware can print which build is flashed on boot. A trailing "+" marks a build 3 # made with uncommitted (tracked) changes under firmware/. This is what lets you 4 # glance at the badge and know exactly which commit is running — the safeguard 5 # against flashing a stale/stranded build. 6 Import("env") # noqa: F821 (provided by PlatformIO/SCons) 7 import subprocess 8 9 proj = env["PROJECT_DIR"] # the firmware/ dir (holds platformio.ini) 10 11 12 def _git(args, default=""): 13 try: 14 out = subprocess.check_output(["git"] + args, cwd=proj, 15 stderr=subprocess.DEVNULL) 16 return out.decode().strip() 17 except Exception: 18 return default 19 20 21 rev = _git(["rev-parse", "--short", "HEAD"], "nogit") 22 if rev != "nogit": 23 # Dirty = uncommitted *tracked* changes under firmware/. Untracked build 24 # artifacts (e.g. src/idf_component.yml.orig) are ignored on purpose so they 25 # don't spuriously flag every build dirty. 26 dirty = subprocess.call(["git", "diff", "--quiet", "HEAD", "--", "."], 27 cwd=proj, stderr=subprocess.DEVNULL) != 0 28 if dirty: 29 rev += "+" 30 31 env.Append(CPPDEFINES=[("GIT_REV", env.StringifyMacro(rev))]) 32 print("git_rev: GIT_REV=%s" % rev)