-
Notifications
You must be signed in to change notification settings - Fork 1
/
add_git_branch.py
32 lines (24 loc) · 1.21 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
# generate an include/__version.h file reflecting the current git branch and commit number
import datetime
import subprocess
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
print ("build_version = " + build_version)
# write to file
f = open("include/__version.h","w")
f.write("// do not edit this file - automatically generated 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 __BUILD_TIME__ \"%s\"" % datetime.datetime.now())
f.close()
make_version_header()