22import datetime
33import warnings
44import re
5- from itertools import chain , repeat , islice
65
76from .config import Configuration
87from .utils import trace , string_types , utc
1615SEMVER_LEN = 3
1716
1817
19- def _pad (iterable , size , padding = None ):
20- padded = chain (iterable , repeat (padding ))
21- return list (islice (padded , size ))
22-
23-
2418def _parse_version_tag (tag , config ):
2519 tagstring = tag if not isinstance (tag , string_types ) else str (tag )
2620 match = config .tag_regex .match (tagstring )
@@ -132,6 +126,7 @@ def __init__(
132126 dirty = False ,
133127 preformatted = False ,
134128 branch = None ,
129+ config = None ,
135130 ** kw
136131 ):
137132 if kw :
@@ -146,6 +141,7 @@ def __init__(
146141 self .dirty = dirty
147142 self .preformatted = preformatted
148143 self .branch = branch
144+ self .config = config
149145
150146 @property
151147 def extra (self ):
@@ -193,7 +189,14 @@ def _parse_tag(tag, preformatted, config):
193189
194190
195191def meta (
196- tag , distance = None , dirty = False , node = None , preformatted = False , config = None , ** kw
192+ tag ,
193+ distance = None ,
194+ dirty = False ,
195+ node = None ,
196+ preformatted = False ,
197+ branch = None ,
198+ config = None ,
199+ ** kw
197200):
198201 if not config :
199202 warnings .warn (
@@ -203,7 +206,9 @@ def meta(
203206 parsed_version = _parse_tag (tag , preformatted , config )
204207 trace ("version" , tag , "->" , parsed_version )
205208 assert parsed_version is not None , "cant parse version %s" % tag
206- return ScmVersion (parsed_version , distance , node , dirty , preformatted , ** kw )
209+ return ScmVersion (
210+ parsed_version , distance , node , dirty , preformatted , branch , config , ** kw
211+ )
207212
208213
209214def guess_next_version (tag_version ):
@@ -238,12 +243,14 @@ def guess_next_dev_version(version):
238243
239244
240245def guess_next_simple_semver (version , retain , increment = True ):
241- parts = map (int , str (version ).split ("." ))
242- parts = _pad (parts , retain , 0 )
246+ parts = [int (i ) for i in str (version ).split ("." )[:retain ]]
247+ while len (parts ) < retain :
248+ parts .append (0 )
243249 if increment :
244250 parts [- 1 ] += 1
245- parts = _pad (parts , SEMVER_LEN , 0 )
246- return "." .join (map (str , parts ))
251+ while len (parts ) < SEMVER_LEN :
252+ parts .append (0 )
253+ return "." .join (str (i ) for i in parts )
247254
248255
249256def simplified_semver_version (version ):
@@ -260,6 +267,25 @@ def simplified_semver_version(version):
260267 )
261268
262269
270+ def release_branch_semver (version ):
271+ if version .exact :
272+ return version .format_with ("{tag}" )
273+ if version .branch is not None :
274+ # Does the branch name (stripped of namespace) parse as a version?
275+ branch_ver = _parse_version_tag (version .branch .split ("/" )[- 1 ], version .config )
276+ if branch_ver is not None :
277+ # Does the branch version up to the minor part match the tag? If not it
278+ # might be like, an issue number or something and not a version number, so
279+ # we only want to use it if it matches.
280+ tag_ver_up_to_minor = str (version .tag ).split ("." )[:SEMVER_MINOR ]
281+ branch_ver_up_to_minor = branch_ver ["version" ].split ("." )[:SEMVER_MINOR ]
282+ if branch_ver_up_to_minor == tag_ver_up_to_minor :
283+ # We're in a release/maintenance branch, next is a patch/rc/beta bump:
284+ return version .format_next_version (guess_next_version )
285+ # We're in a development branch, next is a minor bump:
286+ return version .format_next_version (guess_next_simple_semver , retain = SEMVER_MINOR )
287+
288+
263289def _format_local_with_time (version , time_format ):
264290
265291 if version .exact or version .node is None :
0 commit comments