Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 81 additions & 55 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import questionary

from commitizen import bump, factory, git, hooks, out
from commitizen import bump, cmd, factory, git, hooks, out
from commitizen.changelog_formats import get_changelog_format
from commitizen.commands.changelog import Changelog
from commitizen.defaults import Settings
Expand Down Expand Up @@ -289,7 +289,9 @@ def __call__(self) -> None:

new_tag_version = rules.normalize_tag(new_version)
if next_version_to_stdout:
if increment is None and new_tag_version == current_tag_version:
if self._is_not_eligible_for_bump(
increment, new_tag_version, current_tag_version
):
raise NoneIncrementExit(
"[NO_COMMITS_TO_BUMP]\n"
"The commits found are not eligible to be bumped"
Expand All @@ -313,7 +315,9 @@ def __call__(self) -> None:
else:
out.write(information)

if increment is None and new_tag_version == current_tag_version:
if self._is_not_eligible_for_bump(
increment, new_tag_version, current_tag_version
):
raise NoneIncrementExit(
"[NO_COMMITS_TO_BUMP]\nThe commits found are not eligible to be bumped"
)
Expand All @@ -322,34 +326,7 @@ def __call__(self) -> None:
changelog_file_name = None
dry_run = self.arguments["dry_run"]
if self.changelog_flag:
changelog_args = {
"unreleased_version": new_tag_version,
"template": self.template,
"extras": self.extras,
"incremental": True,
"dry_run": dry_run,
# governs logic for merge_prerelease
"during_version_bump": self.arguments["prerelease"] is None,
}
if self.changelog_to_stdout:
try:
Changelog(
self.config,
{**changelog_args, "dry_run": True}, # type: ignore[typeddict-item]
)()
except DryRunExit:
pass

changelog_cmd = Changelog(
self.config,
{
**changelog_args, # type: ignore[typeddict-item]
"file_name": self.file_name,
"allow_no_commit": bool(self.arguments["allow_no_commit"]),
},
)
changelog_cmd()
changelog_file_name = changelog_cmd.file_name
changelog_file_name = self._update_changelog(new_tag_version, dry_run)
updated_files.append(changelog_file_name)

# Do not perform operations over files or git.
Expand Down Expand Up @@ -394,42 +371,43 @@ def __call__(self) -> None:

# FIXME: check if any changes have been staged
git.add(*updated_files)
c = git.commit(message, args=self._get_commit_args())
if self.retry and c.return_code != 0 and self.changelog_flag:
commit_result = git.commit(message, args=self._get_commit_args())
if self._should_retry_commit(commit_result):
# Maybe pre-commit reformatted some files? Retry once
logger.debug("1st git.commit error: %s", c.err)
logger.debug("1st git.commit error: %s", commit_result.err)
logger.info("1st commit attempt failed; retrying once")
git.add(*updated_files)
c = git.commit(message, args=self._get_commit_args())
if c.return_code != 0:
err = c.err.strip() or c.out
commit_result = git.commit(message, args=self._get_commit_args())
if commit_result.return_code != 0:
err = commit_result.err.strip() or commit_result.out
raise BumpCommitFailedError(f'2nd git.commit error: "{err}"')

for msg in (c.out, c.err):
for msg in (commit_result.out, commit_result.err):
if msg:
out_func = out.diagnostic if self.git_output_to_stderr else out.write
out_func(msg)

c = git.tag(
should_sign_tag = any(
(
self.bump_settings.get("gpg_sign"),
self.config.settings.get("gpg_sign"),
)
)
should_annotate_tag = any(
(
self.bump_settings.get("annotated_tag"),
self.config.settings.get("annotated_tag"),
self.bump_settings.get("annotated_tag_message"),
)
)
tag_result = git.tag(
new_tag_version,
signed=any(
(
self.bump_settings.get("gpg_sign"),
self.config.settings.get("gpg_sign"),
)
),
annotated=any(
(
self.bump_settings.get("annotated_tag"),
self.config.settings.get("annotated_tag"),
self.bump_settings.get("annotated_tag_message"),
)
),
signed=should_sign_tag,
annotated=should_annotate_tag,
msg=self.bump_settings.get("annotated_tag_message", None),
# TODO: also get from self.config.settings?
)
if c.return_code != 0:
raise BumpTagFailedError(c.err)
if tag_result.return_code != 0:
raise BumpTagFailedError(tag_result.err)

if self.post_bump_hooks:
hooks.run(
Expand All @@ -451,6 +429,54 @@ def __call__(self) -> None:
else:
out.success("Done!")

def _update_changelog(self, new_tag_version: str, dry_run: bool) -> str:
"""Generate the changelog for the new version and return its file name."""
changelog_args = {
"unreleased_version": new_tag_version,
"template": self.template,
"extras": self.extras,
"incremental": True,
"dry_run": dry_run,
# governs logic for merge_prerelease
"during_version_bump": self.arguments["prerelease"] is None,
}
if self.changelog_to_stdout:
try:
Changelog(
self.config,
{**changelog_args, "dry_run": True}, # type: ignore[typeddict-item]
)()
except DryRunExit:
pass
changelog_cmd = Changelog(
self.config,
{
**changelog_args, # type: ignore[typeddict-item]
"file_name": self.file_name,
"allow_no_commit": bool(self.arguments["allow_no_commit"]),
},
)
changelog_cmd()
return changelog_cmd.file_name

@staticmethod
def _is_not_eligible_for_bump(
increment: Increment | None,
new_tag_version: str,
current_tag_version: str,
) -> bool:
"""No increment was detected and the tag would not change."""
return increment is None and new_tag_version == current_tag_version

def _should_retry_commit(self, commit_result: cmd.Command) -> bool:
"""Retry once if the commit failed and a changelog was generated.

A pre-commit hook may have reformatted the changelog, so a second
attempt can succeed.
"""
commit_failed = commit_result.return_code != 0
return self.retry and commit_failed and self.changelog_flag

def _get_commit_args(self) -> list[str]:
commit_args = ["-a"]
if self.no_verify:
Expand Down
Loading