-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_git_branch.py
37 lines (28 loc) · 1.37 KB
/
add_git_branch.py
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
# generate an include/__version.h file reflecting the current git branch and commit number
import datetime
import subprocess
Import("env")
def make_version_header():
# dont track changes to the output file
subprocess.run(["git", "update-index", "--skip-worktree", "include/__version.h"])
# fetch branch info from git
ret = subprocess.run(["git", "rev-parse", "--abbrev-ref", "HEAD"], stdout=subprocess.PIPE, text=True)
build_version = ret.stdout.strip()
ret = subprocess.run(["git", "rev-parse", "--short", "HEAD"], stdout=subprocess.PIPE, text=True)
build_version += " "
build_version += ret.stdout.strip()
ret = subprocess.run(["git", "diff", "HEAD"], stdout=subprocess.PIPE, text=True)
status = " clean" if ret.stdout.strip()=="" else " (dirty)"
build_version += status
env_name = env["PIOENV"]
print ("build_version = " + build_version)
# write to file
f = open("include/__version.h","w")
f.write("// do not edit this file - automatically generated by add_git_branch.py during build\n")
f.write("// regenerated at %s\n\n" % datetime.datetime.now())
f.write("#define COMMIT_INFO \"" + build_version + "\"\n\n")
f.write("#define ENV_NAME \"" + env_name + "\"\n\n")
f.close()
make_version_header()
# include the build environment name in the firmware filename
env.Replace(PROGNAME="firmware_%s" % env["PIOENV"])