From 0e2a505604f5a5188c3c8fddbd3fcfb2fd59ca10 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas Date: Wed, 15 May 2019 13:52:05 +0200 Subject: [PATCH 1/4] Propagate current_version and new_version to commit as environment variables so that pre-commit hooks can distinguish between normal commits and bumpversion commits. --- bumpversion/cli.py | 2 +- bumpversion/vcs.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bumpversion/cli.py b/bumpversion/cli.py index d63f054..ac2d05d 100644 --- a/bumpversion/cli.py +++ b/bumpversion/cli.py @@ -662,7 +662,7 @@ def _commit_to_vcs(files, context, config_file, config_file_exists, vcs, args): commit_message, ) if do_commit: - vcs.commit(message=commit_message) + vcs.commit(message=commit_message, context=context) return context diff --git a/bumpversion/vcs.py b/bumpversion/vcs.py index 0a577c4..9e46975 100644 --- a/bumpversion/vcs.py +++ b/bumpversion/vcs.py @@ -24,11 +24,13 @@ class BaseVCS(object): _COMMIT_COMMAND = None @classmethod - def commit(cls, message): + def commit(cls, message, context): with NamedTemporaryFile("wb", delete=False) as f: f.write(message.encode("utf-8")) env = os.environ.copy() env[str("HGENCODING")] = str("utf-8") + for key in ['current_version', 'new_version']: + env['BUMPVERSION_'+key.upper()] = context[key] try: subprocess.check_output(cls._COMMIT_COMMAND + [f.name], env=env) except subprocess.CalledProcessError as exc: From d7671bec7bc481dec942bca6c3c75a4136991072 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas Date: Wed, 15 May 2019 14:25:27 +0200 Subject: [PATCH 2/4] Minor change that might fix AppVeyor tests --- bumpversion/vcs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bumpversion/vcs.py b/bumpversion/vcs.py index 9e46975..edfb9e9 100644 --- a/bumpversion/vcs.py +++ b/bumpversion/vcs.py @@ -29,8 +29,8 @@ def commit(cls, message, context): f.write(message.encode("utf-8")) env = os.environ.copy() env[str("HGENCODING")] = str("utf-8") - for key in ['current_version', 'new_version']: - env['BUMPVERSION_'+key.upper()] = context[key] + for key in ["current_version", "new_version"]: + env[str("BUMPVERSION_"+key.upper())] = context[key] try: subprocess.check_output(cls._COMMIT_COMMAND + [f.name], env=env) except subprocess.CalledProcessError as exc: From 32cba4f7df10ab94f5115de8d5e1bd35e6ccf61c Mon Sep 17 00:00:00 2001 From: Mauricio Villegas Date: Sat, 8 Jun 2019 14:12:49 +0200 Subject: [PATCH 3/4] - Casting value to a str as suggested by @ekohl. - Added feature to README.md so that it is properly documented. --- README.md | 9 +++++++++ bumpversion/vcs.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7cf000e..a8a7454 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,15 @@ General configuration is grouped in a `[bumpversion]` section. Also available as `(--commit | --no-commit)`. + In many projects it is common to have a pre-commit hook that runs prior to a + commit and in case of failure aborts the commit. For some use cases it might + be desired that when bumping a version and having `commit = True`, the + pre-commit hook should perform slightly different actions than in regular + commits. For example run an extended set of checks only for actual releases of + the software. To allow the pre-commit hooks to distinguish a bumpversion + commit, the `BUMPVERSION_CURRENT_VERSION` and `BUMPVERSION_NEW_VERSION` + environment variables are set when executing the commit command. + #### `message =` _**[optional]**_
**default:** `Bump version: {current_version} → {new_version}` diff --git a/bumpversion/vcs.py b/bumpversion/vcs.py index edfb9e9..0b6ee8f 100644 --- a/bumpversion/vcs.py +++ b/bumpversion/vcs.py @@ -30,7 +30,7 @@ def commit(cls, message, context): env = os.environ.copy() env[str("HGENCODING")] = str("utf-8") for key in ["current_version", "new_version"]: - env[str("BUMPVERSION_"+key.upper())] = context[key] + env[str("BUMPVERSION_"+key.upper())] = str(context[key]) try: subprocess.check_output(cls._COMMIT_COMMAND + [f.name], env=env) except subprocess.CalledProcessError as exc: From c39248c7b8a279f0894f34bab0f80700a042cd57 Mon Sep 17 00:00:00 2001 From: Christian Verkerk Date: Sat, 24 Aug 2019 17:46:43 -0700 Subject: [PATCH 4/4] fix formatting a bit --- bumpversion/vcs.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bumpversion/vcs.py b/bumpversion/vcs.py index 0b6ee8f..6d4f023 100644 --- a/bumpversion/vcs.py +++ b/bumpversion/vcs.py @@ -10,7 +10,7 @@ from bumpversion.exceptions import ( WorkingDirectoryIsDirtyException, - MercurialDoesNotSupportSignedTagsException + MercurialDoesNotSupportSignedTagsException, ) from bumpversion.compat import _command_args @@ -29,8 +29,8 @@ def commit(cls, message, context): f.write(message.encode("utf-8")) env = os.environ.copy() env[str("HGENCODING")] = str("utf-8") - for key in ["current_version", "new_version"]: - env[str("BUMPVERSION_"+key.upper())] = str(context[key]) + for key in ("current_version", "new_version"): + env[str("BUMPVERSION_" + key.upper())] = str(context[key]) try: subprocess.check_output(cls._COMMIT_COMMAND + [f.name], env=env) except subprocess.CalledProcessError as exc: