bump2version
bump2version copied to clipboard
Using bump2version and pre-commit hooks
Hi,
I have been using bump2version for a while. Thank you for maintaining it. Most recently, I added pre-commit hooks to my workflow. Now, my bumpversion commands are always failing since I think it interferes with the pre-commit hooks.
I am using bumpversion's --no-commit
flag for now and then commit manually. Is there a way to make them both work together ?
Here's my bumpversion config:
[bumpversion]
current_version = 1.0.0
commit = True
tag = True
And here's my pre commit hook config:
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v1.4.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- id: check-case-conflict
- id: check-merge-conflict
- id: check-yaml
args: ['--unsafe']
- id: detect-private-key
- id: forbid-new-submodules
- id: check-json
- id: pretty-format-json
- id: check-added-large-files
- id: flake8
- repo: git://github.com/CuriousLearner/pre-commit-python-sorter
sha: 5294cde9d51cff119af537e987c30c320e9fbe2f
hooks:
- id: python-import-sorter
args: ['--silent-overwrite']
- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
Let me know if you need more information from me.
Well it would be informative if you could share the error message of the failure :-)
But I see you're using end-of-file-fixer
which is known to cause problems. Please see issue #58 for a workaround.
Maybe some more info since I also have problems with pre-commit hooks and bumpversion.
More specifically my problem is connected to the fix-trailing-whitespace
hook. I configure bumpversion in setup.cfg
in a python project. This means there are other sections, sometimes containing lines like these:
[flake8]
exclude =
.git
__pycache__
Bumpversion seems to add whitespace after exclude =
which the fix-trailing-whitespace
doesn't like. This causes an error since the pre-commit hooks obviously fail (bumpversion adds whitespace, hooks remove whitespace)
Maybe the original author has similar problems.
Edit: The version I am using is
bumpversion: v1.0.0 (using Python v3.6.9)
I can confirm I have the same issue as @cbows described above with the trailing whitespace pre-commit hook.
I've disabled this hook for now, but it would be nice if bump2version
could leave the other lines untouched.
I am also having the same issue with bump2version, and the fix-trailing-whitespace precommit hook failing on setup.cfg
I can also chime in that the fix-trailing-whitespace
hook is what causes errors when versioning my projects with bumpversion
. Your workaround to manually commit when bumping versions is also how I've dealt with it.
Apparently this is caused by python ConfigParser
always appending a whitespace after the =
even on multiline values. AFAIK there is no way to prevent this in the ConfigParser
One dirty solution is to strip the extra whitespace before writing the file:
diff --git a/bumpversion/cli.py b/bumpversion/cli.py
index b107e10..378389d 100644
--- a/bumpversion/cli.py
+++ b/bumpversion/cli.py
@@ -646,11 +646,12 @@ def _update_config_file(
)
config.write(new_config)
- logger.info(new_config.getvalue())
+ config_text = new_config.getvalue().replace("= %s" % config_newlines, "=%s" % config_newlines)
+ logger.info(config_text)
if write_to_config_file:
with open(config_file, "wt", encoding="utf-8", newline=config_newlines) as f:
- f.write(new_config.getvalue().strip() + "\n")
+ f.write(config_text.strip() + config_newlines)
except UnicodeEncodeError:
warnings.warn(
It this is acceptable I can open a PR including the fix and the test changes
I also have a solution for this issue here: https://github.com/c4urself/bump2version/pull/163 Its using bump2version itself to bump the cfg, so it will only replace what is needed
@balrok If I understood correctly your approach, it looks absolutely sane
Another workaround is to exclude setup.cfg
from the hook in your .pre-commit.config.yaml
file:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
exclude: setup.cfg
But yeah, this isn't ideal, and it would be nice if bumpversion didn't modify any lines it doesn't need to. Even without the hook, if I edit setup.cfg
manually my editor is currently configured to trim trailing whitespace, so that means the diff on a commit where I'm editing setup.cfg
is a bit noisier because of the extra whitespace. What's worse is if I trim the whitespace manually, the next time bumpversion processes the file it'll put the spaces back which adds diff noise to the bumpversion commit.