Skip to content

Commit f52f532

Browse files
bearomorphismLee-W
authored andcommitted
refactor(bump): extract option validation and new version resolution to new functions
1 parent 280bea0 commit f52f532

File tree

1 file changed

+81
-76
lines changed

1 file changed

+81
-76
lines changed

commitizen/commands/bump.py

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Increment,
3030
InvalidVersion,
3131
Prerelease,
32+
VersionProtocol,
3233
get_version_scheme,
3334
)
3435

@@ -156,43 +157,91 @@ def _find_increment(self, commits: list[git.GitCommit]) -> Increment | None:
156157
)
157158
return bump.find_increment(commits, regex=bump_pattern, increments_map=bump_map)
158159

159-
def __call__(self) -> None:
160-
"""Steps executed to bump."""
161-
provider = get_provider(self.config)
162-
current_version = self.scheme(provider.get_version())
163-
164-
increment = self.arguments["increment"]
165-
prerelease = self.arguments["prerelease"]
166-
devrelease = self.arguments["devrelease"]
167-
is_local_version = self.arguments["local_version"]
168-
manual_version = self.arguments["manual_version"]
169-
build_metadata = self.arguments["build_metadata"]
170-
next_version_to_stdout = self.arguments[
171-
"get_next"
172-
] # TODO: maybe rename to `next_version_to_stdout`
173-
allow_no_commit = self.arguments["allow_no_commit"]
174-
major_version_zero = self.arguments["major_version_zero"]
175-
176-
if manual_version:
160+
def _validate_arguments(self, current_version: VersionProtocol) -> None:
161+
errors: list[str] = []
162+
if self.arguments["manual_version"]:
177163
for val, option in (
178-
(increment, "--increment"),
179-
(prerelease, "--prerelease"),
180-
(devrelease is not None, "--devrelease"),
181-
(is_local_version, "--local-version"),
182-
(build_metadata, "--build-metadata"),
183-
(major_version_zero, "--major-version-zero"),
164+
(self.arguments["increment"], "--increment"),
165+
(self.arguments["prerelease"], "--prerelease"),
166+
(self.arguments["devrelease"] is not None, "--devrelease"),
167+
(self.arguments["local_version"], "--local-version"),
168+
(self.arguments["build_metadata"], "--build-metadata"),
169+
(self.arguments["major_version_zero"], "--major-version-zero"),
184170
):
185171
if val:
186-
raise NotAllowed(f"{option} cannot be combined with MANUAL_VERSION")
172+
errors.append(f"{option} cannot be combined with MANUAL_VERSION")
187173

188-
if major_version_zero and current_version.release[0]:
189-
raise NotAllowed(
174+
if self.arguments["major_version_zero"] and current_version.release[0]:
175+
errors.append(
190176
f"--major-version-zero is meaningless for current version {current_version}"
191177
)
178+
if self.arguments["build_metadata"] and self.arguments["local_version"]:
179+
errors.append("--local-version cannot be combined with --build-metadata")
180+
181+
if errors:
182+
raise NotAllowed("\n".join(errors))
183+
184+
def _resolve_increment_and_new_version(
185+
self, current_version: VersionProtocol, current_tag: git.GitTag | None
186+
) -> tuple[Increment | None, VersionProtocol]:
187+
increment = self.arguments["increment"]
188+
if manual_version := self.arguments["manual_version"]:
189+
try:
190+
return increment, self.scheme(manual_version)
191+
except InvalidVersion as exc:
192+
raise InvalidManualVersion(
193+
"[INVALID_MANUAL_VERSION]\n"
194+
f"Invalid manual version: '{manual_version}'"
195+
) from exc
196+
197+
if increment is None:
198+
commits = git.get_commits(current_tag.name if current_tag else None)
199+
200+
# No commits, there is no need to create an empty tag.
201+
# Unless we previously had a prerelease.
202+
if (
203+
not commits
204+
and not current_version.is_prerelease
205+
and not self.arguments["allow_no_commit"]
206+
):
207+
raise NoCommitsFoundError("[NO_COMMITS_FOUND]\nNo new commits found.")
208+
209+
increment = self._find_increment(commits)
210+
211+
# It may happen that there are commits, but they are not eligible
212+
# for an increment, this generates a problem when using prerelease (#281)
213+
if (
214+
self.arguments["prerelease"]
215+
and increment is None
216+
and not current_version.is_prerelease
217+
):
218+
raise NoCommitsFoundError(
219+
"[NO_COMMITS_FOUND]\n"
220+
"No commits found to generate a pre-release.\n"
221+
"To avoid this error, manually specify the type of increment with `--increment`"
222+
)
192223

193-
if build_metadata and is_local_version:
194-
raise NotAllowed("--local-version cannot be combined with --build-metadata")
224+
# we create an empty PATCH increment for empty tag
225+
if increment is None and self.arguments["allow_no_commit"]:
226+
increment = "PATCH"
227+
228+
return increment, current_version.bump(
229+
increment,
230+
prerelease=self.arguments["prerelease"],
231+
prerelease_offset=self.bump_settings["prerelease_offset"],
232+
devrelease=self.arguments["devrelease"],
233+
is_local_version=self.arguments["local_version"],
234+
build_metadata=self.arguments["build_metadata"],
235+
exact_increment=self.arguments["increment_mode"] == "exact",
236+
)
195237

238+
def __call__(self) -> None:
239+
"""Steps executed to bump."""
240+
provider = get_provider(self.config)
241+
current_version = self.scheme(provider.get_version())
242+
self._validate_arguments(current_version)
243+
244+
next_version_to_stdout = self.arguments["get_next"]
196245
if next_version_to_stdout:
197246
for value, option in (
198247
(self.changelog_flag, "--changelog"),
@@ -219,53 +268,9 @@ def __call__(self) -> None:
219268

220269
is_initial = self._is_initial_tag(current_tag, self.arguments["yes"])
221270

222-
if manual_version:
223-
try:
224-
new_version = self.scheme(manual_version)
225-
except InvalidVersion as exc:
226-
raise InvalidManualVersion(
227-
"[INVALID_MANUAL_VERSION]\n"
228-
f"Invalid manual version: '{manual_version}'"
229-
) from exc
230-
else:
231-
if increment is None:
232-
commits = git.get_commits(current_tag.name if current_tag else None)
233-
234-
# No commits, there is no need to create an empty tag.
235-
# Unless we previously had a prerelease.
236-
if (
237-
not commits
238-
and not current_version.is_prerelease
239-
and not allow_no_commit
240-
):
241-
raise NoCommitsFoundError(
242-
"[NO_COMMITS_FOUND]\nNo new commits found."
243-
)
244-
245-
increment = self._find_increment(commits)
246-
247-
# It may happen that there are commits, but they are not eligible
248-
# for an increment, this generates a problem when using prerelease (#281)
249-
if prerelease and increment is None and not current_version.is_prerelease:
250-
raise NoCommitsFoundError(
251-
"[NO_COMMITS_FOUND]\n"
252-
"No commits found to generate a pre-release.\n"
253-
"To avoid this error, manually specify the type of increment with `--increment`"
254-
)
255-
256-
# we create an empty PATCH increment for empty tag
257-
if increment is None and allow_no_commit:
258-
increment = "PATCH"
259-
260-
new_version = current_version.bump(
261-
increment,
262-
prerelease=prerelease,
263-
prerelease_offset=self.bump_settings["prerelease_offset"],
264-
devrelease=devrelease,
265-
is_local_version=is_local_version,
266-
build_metadata=build_metadata,
267-
exact_increment=self.arguments["increment_mode"] == "exact",
268-
)
271+
increment, new_version = self._resolve_increment_and_new_version(
272+
current_version, current_tag
273+
)
269274

270275
new_tag_version = rules.normalize_tag(new_version)
271276
if next_version_to_stdout:

0 commit comments

Comments
 (0)