@@ -86,7 +86,7 @@ def assert_nondirty(cls) -> None:
86
86
raise NotImplementedError ()
87
87
88
88
@classmethod
89
- def latest_tag_info (cls , tag_pattern : str ) -> SCMInfo :
89
+ def latest_tag_info (cls , tag_name : str , parse_pattern : str ) -> SCMInfo :
90
90
"""Return information about the latest tag."""
91
91
raise NotImplementedError ()
92
92
@@ -109,6 +109,14 @@ def get_all_tags(cls) -> List[str]:
109
109
except (FileNotFoundError , PermissionError , NotADirectoryError , subprocess .CalledProcessError ):
110
110
return []
111
111
112
+ @classmethod
113
+ def get_version_from_tag (cls , tag : str , tag_name : str , parse_pattern : str ) -> Optional [str ]:
114
+ """Return the version from a tag."""
115
+ version_pattern = parse_pattern .replace ("\\ \\ " , "\\ " )
116
+ rep = tag_name .replace ("{new_version}" , f"(?P<current_version>{ version_pattern } )" )
117
+ tag_regex = re .compile (rep )
118
+ return match ["current_version" ] if (match := tag_regex .match (tag )) else None
119
+
112
120
@classmethod
113
121
def commit_to_scm (
114
122
cls ,
@@ -173,7 +181,6 @@ def tag_in_scm(cls, config: "Config", context: MutableMapping, dry_run: bool = F
173
181
tag_name = config .tag_name .format (** context )
174
182
tag_message = config .tag_message .format (** context )
175
183
existing_tags = cls .get_all_tags ()
176
-
177
184
do_tag = not dry_run
178
185
179
186
if tag_name in existing_tags :
@@ -219,15 +226,15 @@ def assert_nondirty(cls) -> None:
219
226
raise DirtyWorkingDirectoryError (f"Git working directory is not clean:\n \n { joined_lines } " )
220
227
221
228
@classmethod
222
- def latest_tag_info (cls , tag_pattern : str ) -> SCMInfo :
229
+ def latest_tag_info (cls , tag_name : str , parse_pattern : str ) -> SCMInfo :
223
230
"""Return information about the latest tag."""
224
231
try :
225
232
# git-describe doesn't update the git-index, so we do that
226
233
subprocess .run (["git" , "update-index" , "--refresh" , "-q" ], capture_output = True ) # noqa: S603, S607
227
234
except subprocess .CalledProcessError as e :
228
235
logger .debug ("Error when running git update-index: %s" , e .stderr )
229
236
return SCMInfo (tool = cls )
230
-
237
+ tag_pattern = tag_name . replace ( "{new_version}" , "*" )
231
238
try :
232
239
# get info about the latest tag in git
233
240
git_cmd = [
@@ -260,7 +267,8 @@ def latest_tag_info(cls, tag_pattern: str) -> SCMInfo:
260
267
261
268
info .commit_sha = describe_out .pop ().lstrip ("g" )
262
269
info .distance_to_latest_tag = int (describe_out .pop ())
263
- info .current_version = "-" .join (describe_out ).lstrip ("v" )
270
+ version = cls .get_version_from_tag (describe_out [- 1 ], tag_name , parse_pattern )
271
+ info .current_version = version or "-" .join (describe_out ).lstrip ("v" )
264
272
265
273
return info
266
274
@@ -298,10 +306,10 @@ class Mercurial(SourceCodeManager):
298
306
_ALL_TAGS_COMMAND : ClassVar [List [str ]] = ["hg" , "log" , '--rev="tag()"' , '--template="{tags}\n "' ]
299
307
300
308
@classmethod
301
- def latest_tag_info (cls , tag_pattern : str ) -> SCMInfo :
309
+ def latest_tag_info (cls , tag_name : str , parse_pattern : str ) -> SCMInfo :
302
310
"""Return information about the latest tag."""
303
311
current_version = None
304
- re_pattern = tag_pattern .replace ("* " , ".*" )
312
+ re_pattern = tag_name .replace ("{new_version} " , ".*" )
305
313
result = subprocess .run (
306
314
["hg" , "log" , "-r" , f"tag('re:{ re_pattern } ')" , "--template" , "{latesttag}\n " ], # noqa: S603, S607
307
315
text = True ,
@@ -310,7 +318,10 @@ def latest_tag_info(cls, tag_pattern: str) -> SCMInfo:
310
318
)
311
319
result .check_returncode ()
312
320
if result .stdout :
313
- current_version = result .stdout .splitlines (keepends = False )[- 1 ].lstrip ("v" )
321
+ tag_string = result .stdout .splitlines (keepends = False )[- 1 ]
322
+ current_version = cls .get_version_from_tag (tag_string , tag_name , parse_pattern )
323
+ else :
324
+ logger .debug ("No tags found" )
314
325
is_dirty = len (subprocess .check_output (["hg" , "status" , "-mard" ])) != 0 # noqa: S603, S607
315
326
return SCMInfo (tool = cls , current_version = current_version , dirty = is_dirty )
316
327
@@ -356,11 +367,11 @@ def tag(cls, name: str, sign: bool = False, message: Optional[str] = None) -> No
356
367
subprocess .check_output (command ) # noqa: S603
357
368
358
369
359
- def get_scm_info (tag_pattern : str ) -> SCMInfo :
370
+ def get_scm_info (tag_name : str , parse_pattern : str ) -> SCMInfo :
360
371
"""Return a dict with the latest source code management info."""
361
372
if Git .is_usable ():
362
- return Git .latest_tag_info (tag_pattern )
373
+ return Git .latest_tag_info (tag_name , parse_pattern )
363
374
elif Mercurial .is_usable ():
364
- return Mercurial .latest_tag_info (tag_pattern )
375
+ return Mercurial .latest_tag_info (tag_name , parse_pattern )
365
376
else :
366
377
return SCMInfo ()
0 commit comments