Skip to content

Commit

Permalink
Merge pull request #133 from callowayproject/130-crash-when-supplying…
Browse files Browse the repository at this point in the history
…-commit-argument

130 crash when supplying commit argument
  • Loading branch information
coordt authored Jan 25, 2024
2 parents 1deef89 + 10e5d7d commit 3bcec28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions bumpversion/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
if TYPE_CHECKING: # pragma: no-coverage
from bumpversion.config import Config

from bumpversion.exceptions import DirtyWorkingDirectoryError, SignedTagsError
from bumpversion.exceptions import BumpVersionError, DirtyWorkingDirectoryError, SignedTagsError

logger = get_indented_logger(__name__)

Expand Down Expand Up @@ -54,6 +54,12 @@ class SourceCodeManager:
def commit(cls, message: str, current_version: str, new_version: str, extra_args: Optional[list] = None) -> None:
"""Commit the changes."""
extra_args = extra_args or []
if not current_version:
logger.warning("No current version given, using an empty string.")
current_version = ""
if not new_version:
logger.warning("No new version given, using an empty string.")
new_version = ""

with NamedTemporaryFile("wb", delete=False) as f:
f.write(message.encode("utf-8"))
Expand All @@ -66,10 +72,13 @@ def commit(cls, message: str, current_version: str, new_version: str, extra_args
try:
cmd = [*cls._COMMIT_COMMAND, f.name, *extra_args]
subprocess.run(cmd, env=env, capture_output=True, check=True) # noqa: S603
except subprocess.CalledProcessError as exc: # pragma: no-coverage
err_msg = f"Failed to run {exc.cmd}: return code {exc.returncode}, output: {exc.output}"
except (subprocess.CalledProcessError, TypeError) as exc: # pragma: no-coverage
if isinstance(exc, TypeError):
err_msg = f"Failed to run {cls._COMMIT_COMMAND}: {exc}"
else:
err_msg = f"Failed to run {exc.cmd}: return code {exc.returncode}, output: {exc.output}"
logger.exception(err_msg)
raise exc
raise BumpVersionError(err_msg) from exc
finally:
os.unlink(f.name)

Expand Down
2 changes: 1 addition & 1 deletion bumpversion/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_context(
) -> ChainMap:
"""Return the context for rendering messages and tags."""
ctx = base_context(config.scm_info)
ctx.new_child({"current_version": config.current_version})
ctx = ctx.new_child({"current_version": config.current_version})
if current_version:
ctx = ctx.new_child({f"current_{part}": current_version[part].value for part in current_version})
if new_version:
Expand Down

0 comments on commit 3bcec28

Please sign in to comment.