77import questionary
88
99from commitizen import bump , factory , git , hooks , out
10+ from commitizen .bump_rule import VersionIncrement
1011from commitizen .changelog_formats import get_changelog_format
1112from commitizen .commands .changelog import Changelog
1213from commitizen .defaults import Settings
1819 InvalidManualVersion ,
1920 NoCommitsFoundError ,
2021 NoneIncrementExit ,
21- NoPatternMapError ,
2222 NotAGitProjectError ,
2323 NotAllowed ,
2424)
2525from commitizen .providers import get_provider
2626from commitizen .tags import TagRules
2727from commitizen .version_schemes import (
28- Increment ,
2928 InvalidVersion ,
3029 Prerelease ,
3130 VersionProtocol ,
@@ -53,7 +52,7 @@ class BumpArgs(Settings, total=False):
5352 get_next : bool # TODO: maybe rename to `next_version_to_stdout`
5453 git_output_to_stderr : bool
5554 increment_mode : str
56- increment : Increment | None
55+ increment : VersionIncrement | None
5756 local_version : bool
5857 manual_version : str | None
5958 no_verify : bool
@@ -144,28 +143,23 @@ def _is_initial_tag(
144143 )
145144 return bool (questionary .confirm ("Is this the first tag created?" ).ask ())
146145
147- def _find_increment (self , commits : list [git .GitCommit ]) -> Increment | None :
146+ def _find_increment (self , commits : list [git .GitCommit ]) -> VersionIncrement :
148147 # Update the bump map to ensure major version doesn't increment.
149- # self.cz.bump_map = defaults.bump_map_major_version_zero
150- bump_map = (
151- self .cz .bump_map_major_version_zero
152- if self .bump_settings ["major_version_zero" ]
153- else self .cz .bump_map
154- )
155- bump_pattern = self .cz .bump_pattern
148+ is_major_version_zero = self .bump_settings ["major_version_zero" ]
156149
157- if not bump_map or not bump_pattern :
158- raise NoPatternMapError (
159- f"'{ self .config .settings ['name' ]} ' rule does not support bump"
160- )
161- return bump .find_increment (commits , regex = bump_pattern , increments_map = bump_map )
150+ return VersionIncrement .get_highest_by_messages (
151+ (commit .message for commit in commits ),
152+ lambda x : self .cz .bump_rule .extract_increment (x , is_major_version_zero ),
153+ )
162154
163155 def _validate_arguments (self , current_version : VersionProtocol ) -> None :
164156 errors : list [str ] = []
157+ increment = VersionIncrement .safe_cast (self .arguments ["increment" ])
158+ prerelease = Prerelease .safe_cast (self .arguments ["prerelease" ])
165159 if self .arguments ["manual_version" ]:
166160 for val , option in (
167- (self . arguments [ " increment" ] , "--increment" ),
168- (self . arguments [ " prerelease" ] , "--prerelease" ),
161+ (increment != VersionIncrement . NONE , "--increment" ),
162+ (prerelease , "--prerelease" ),
169163 (self .arguments ["devrelease" ] is not None , "--devrelease" ),
170164 (self .arguments ["local_version" ], "--local-version" ),
171165 (self .arguments ["build_metadata" ], "--build-metadata" ),
@@ -186,8 +180,9 @@ def _validate_arguments(self, current_version: VersionProtocol) -> None:
186180
187181 def _resolve_increment_and_new_version (
188182 self , current_version : VersionProtocol , current_tag : git .GitTag | None
189- ) -> tuple [Increment | None , VersionProtocol ]:
190- increment = self .arguments ["increment" ]
183+ ) -> tuple [VersionIncrement , VersionProtocol ]:
184+ increment = VersionIncrement .safe_cast (self .arguments ["increment" ])
185+ prerelease = Prerelease .safe_cast (self .arguments ["prerelease" ])
191186 if manual_version := self .arguments ["manual_version" ]:
192187 try :
193188 return increment , self .scheme (manual_version )
@@ -197,7 +192,7 @@ def _resolve_increment_and_new_version(
197192 f"Invalid manual version: '{ manual_version } '"
198193 ) from exc
199194
200- if increment is None :
195+ if increment == VersionIncrement . NONE :
201196 commits = git .get_commits (current_tag .name if current_tag else None )
202197
203198 # No commits, there is no need to create an empty tag.
@@ -214,8 +209,8 @@ def _resolve_increment_and_new_version(
214209 # It may happen that there are commits, but they are not eligible
215210 # for an increment, this generates a problem when using prerelease (#281)
216211 if (
217- self . arguments [ " prerelease" ]
218- and increment is None
212+ prerelease
213+ and increment == VersionIncrement . NONE
219214 and not current_version .is_prerelease
220215 ):
221216 raise NoCommitsFoundError (
@@ -225,12 +220,12 @@ def _resolve_increment_and_new_version(
225220 )
226221
227222 # we create an empty PATCH increment for empty tag
228- if increment is None and self .arguments ["allow_no_commit" ]:
229- increment = " PATCH"
223+ if self .arguments ["allow_no_commit" ]:
224+ increment = max ( increment , VersionIncrement . PATCH )
230225
231226 return increment , current_version .bump (
232227 increment ,
233- prerelease = self . arguments [ " prerelease" ] ,
228+ prerelease = prerelease ,
234229 prerelease_offset = self .bump_settings ["prerelease_offset" ],
235230 devrelease = self .arguments ["devrelease" ],
236231 is_local_version = self .arguments ["local_version" ],
@@ -277,7 +272,10 @@ def __call__(self) -> None:
277272
278273 new_tag_version = rules .normalize_tag (new_version )
279274 if next_version_to_stdout :
280- if increment is None and new_tag_version == current_tag_version :
275+ if (
276+ increment == VersionIncrement .NONE
277+ and new_tag_version == current_tag_version
278+ ):
281279 raise NoneIncrementExit (
282280 "[NO_COMMITS_TO_BUMP]\n "
283281 "The commits found are not eligible to be bumped"
@@ -290,7 +288,7 @@ def __call__(self) -> None:
290288 )
291289 # Report found information
292290 information = f"{ message } \n tag to create: { new_tag_version } \n "
293- if increment :
291+ if increment != VersionIncrement . NONE :
294292 information += f"increment detected: { increment } \n "
295293
296294 if self .changelog_to_stdout :
@@ -301,7 +299,10 @@ def __call__(self) -> None:
301299 else :
302300 out .write (information )
303301
304- if increment is None and new_tag_version == current_tag_version :
302+ if (
303+ increment == VersionIncrement .NONE
304+ and new_tag_version == current_tag_version
305+ ):
305306 raise NoneIncrementExit (
306307 "[NO_COMMITS_TO_BUMP]\n The commits found are not eligible to be bumped"
307308 )
0 commit comments