@@ -25,7 +25,6 @@ def get_version_info_from_git(repo_path):
25
25
# Python 2.6 doesn't have check_output, so check for that
26
26
try :
27
27
subprocess .check_output
28
- subprocess .check_call
29
28
except AttributeError :
30
29
return None
31
30
@@ -44,9 +43,25 @@ def get_version_info_from_git(repo_path):
44
43
return git_tag [0 ]
45
44
else :
46
45
return git_tag [0 ] + "-" + git_tag [1 ].replace ("-" , "." )
47
- except subprocess .CalledProcessError :
46
+ except ( subprocess .CalledProcessError , OSError ) :
48
47
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 ):
50
65
return None
51
66
52
67
@@ -86,10 +101,13 @@ def get_version_info_from_mpconfig(repo_path):
86
101
87
102
def make_version_header (repo_path , filename ):
88
103
git_tag = None
104
+ git_hash = None
89
105
if "MICROPY_GIT_TAG" in os .environ :
90
106
git_tag = os .environ ["MICROPY_GIT_TAG" ]
107
+ git_hash = os .environ .get ("MICROPY_GIT_HASH" )
91
108
if git_tag is None :
92
109
git_tag = get_version_info_from_git (repo_path )
110
+ git_hash = get_hash_from_git (repo_path )
93
111
if git_tag is None :
94
112
git_tag = get_version_info_from_mpconfig (repo_path )
95
113
@@ -104,12 +122,15 @@ def make_version_header(repo_path, filename):
104
122
).date ()
105
123
106
124
# Generate the file with the git and version info
125
+ # Note: MICROPY_GIT_HASH may be used by third-party code.
107
126
file_data = """\
108
127
// This file was generated by py/makeversionhdr.py
109
128
#define MICROPY_GIT_TAG "%s"
129
+ #define MICROPY_GIT_HASH "%s"
110
130
#define MICROPY_BUILD_DATE "%s"
111
131
""" % (
112
132
git_tag ,
133
+ git_hash or "<no hash>" ,
113
134
build_date .strftime ("%Y-%m-%d" ),
114
135
)
115
136
0 commit comments