Skip to content

Commit 9242e3d

Browse files
committedFeb 19, 2024
py/makeversionhdr.py: Reinstate MICROPY_GIT_HASH in mpversion.h.
MICROPY_GIT_HASH was removed in 69e34b6 but it is useful for, and used by, third-party code to tell which hash of MicroPython is used. Signed-off-by: Damien George <[email protected]>
1 parent 1ef2944 commit 9242e3d

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed
 

‎py/makeversionhdr.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def get_version_info_from_git(repo_path):
2525
# Python 2.6 doesn't have check_output, so check for that
2626
try:
2727
subprocess.check_output
28-
subprocess.check_call
2928
except AttributeError:
3029
return None
3130

@@ -44,9 +43,25 @@ def get_version_info_from_git(repo_path):
4443
return git_tag[0]
4544
else:
4645
return git_tag[0] + "-" + git_tag[1].replace("-", ".")
47-
except subprocess.CalledProcessError:
46+
except (subprocess.CalledProcessError, OSError):
4847
return None
49-
except OSError:
48+
49+
50+
def get_hash_from_git(repo_path):
51+
# Python 2.6 doesn't have check_output, so check for that.
52+
try:
53+
subprocess.check_output
54+
except AttributeError:
55+
return None
56+
57+
try:
58+
return subprocess.check_output(
59+
["git", "rev-parse", "--short", "HEAD"],
60+
cwd=repo_path,
61+
stderr=subprocess.STDOUT,
62+
universal_newlines=True,
63+
).strip()
64+
except (subprocess.CalledProcessError, OSError):
5065
return None
5166

5267

@@ -86,10 +101,13 @@ def get_version_info_from_mpconfig(repo_path):
86101

87102
def make_version_header(repo_path, filename):
88103
git_tag = None
104+
git_hash = None
89105
if "MICROPY_GIT_TAG" in os.environ:
90106
git_tag = os.environ["MICROPY_GIT_TAG"]
107+
git_hash = os.environ.get("MICROPY_GIT_HASH")
91108
if git_tag is None:
92109
git_tag = get_version_info_from_git(repo_path)
110+
git_hash = get_hash_from_git(repo_path)
93111
if git_tag is None:
94112
git_tag = get_version_info_from_mpconfig(repo_path)
95113

@@ -104,12 +122,15 @@ def make_version_header(repo_path, filename):
104122
).date()
105123

106124
# Generate the file with the git and version info
125+
# Note: MICROPY_GIT_HASH may be used by third-party code.
107126
file_data = """\
108127
// This file was generated by py/makeversionhdr.py
109128
#define MICROPY_GIT_TAG "%s"
129+
#define MICROPY_GIT_HASH "%s"
110130
#define MICROPY_BUILD_DATE "%s"
111131
""" % (
112132
git_tag,
133+
git_hash or "<no hash>",
113134
build_date.strftime("%Y-%m-%d"),
114135
)
115136

0 commit comments

Comments
 (0)
Please sign in to comment.