@@ -140,43 +140,41 @@ def version(ctx):
140140 Context .g_module .VERSION_SPLIT = VERSION_BASE .split ('.' )
141141
142142 # first, try to get a version string from git
143- gotVersionFromGit = False
143+ version_from_git = ''
144144 try :
145- cmd = ['git' , 'describe' , '--always' , '--match' , f'{ GIT_TAG_PREFIX } *' ]
146- out = subprocess .run (cmd , capture_output = True , check = True , text = True ).stdout .strip ()
147- if out :
148- gotVersionFromGit = True
149- if out .startswith (GIT_TAG_PREFIX ):
150- Context .g_module .VERSION = out .lstrip (GIT_TAG_PREFIX )
145+ cmd = ['git' , 'describe' , '--abbrev=8' , '--always' , '--match' , f'{ GIT_TAG_PREFIX } *' ]
146+ version_from_git = subprocess .run (cmd , capture_output = True , check = True , text = True ).stdout .strip ()
147+ if version_from_git :
148+ if version_from_git .startswith (GIT_TAG_PREFIX ):
149+ Context .g_module .VERSION = version_from_git .lstrip (GIT_TAG_PREFIX )
151150 else :
152151 # no tags matched
153- Context .g_module .VERSION = f'{ VERSION_BASE } -commit- { out } '
152+ Context .g_module .VERSION = f'{ VERSION_BASE } +git. { version_from_git } '
154153 except (OSError , subprocess .SubprocessError ):
155154 pass
156155
157- versionFile = ctx .path .find_node ('VERSION.info' )
158- if not gotVersionFromGit and versionFile is not None :
156+ # fallback to the VERSION.info file, if it exists and is not empty
157+ version_from_file = ''
158+ version_file = ctx .path .find_node ('VERSION.info' )
159+ if version_file is not None :
159160 try :
160- Context .g_module .VERSION = versionFile .read ()
161- return
162- except EnvironmentError :
163- pass
164-
165- # version was obtained from git, update VERSION file if necessary
166- if versionFile is not None :
167- try :
168- if versionFile .read () == Context .g_module .VERSION :
169- # already up-to-date
170- return
171- except EnvironmentError as e :
172- Logs .warn (f'{ versionFile } exists but is not readable ({ e .strerror } )' )
173- else :
174- versionFile = ctx .path .make_node ('VERSION.info' )
161+ version_from_file = version_file .read ().strip ()
162+ except OSError as e :
163+ Logs .warn (f'{ e .filename } exists but is not readable ({ e .strerror } )' )
164+ if version_from_file and not version_from_git :
165+ Context .g_module .VERSION = version_from_file
166+ return
175167
168+ # update VERSION.info if necessary
169+ if version_from_file == Context .g_module .VERSION :
170+ # already up-to-date
171+ return
172+ if version_file is None :
173+ version_file = ctx .path .make_node ('VERSION.info' )
176174 try :
177- versionFile .write (Context .g_module .VERSION )
178- except EnvironmentError as e :
179- Logs .warn (f'{ versionFile } is not writable ({ e .strerror } )' )
175+ version_file .write (Context .g_module .VERSION )
176+ except OSError as e :
177+ Logs .warn (f'{ e . filename } is not writable ({ e .strerror } )' )
180178
181179def dist (ctx ):
182180 ctx .algo = 'tar.xz'
0 commit comments