Skip to content

Commit 62b66a7

Browse files
committed
fix: respect pre-commit reformats when bumping
Fix #502. Just attempt to commit twice if the 1st commit fails.
1 parent 66c6320 commit 62b66a7

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

commitizen/commands/bump.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,18 @@ def __call__(self): # noqa: C901
209209
},
210210
)
211211
changelog_cmd()
212-
c = cmd.run(f"git add {changelog_cmd.file_name}")
212+
c = cmd.run(f"git add {changelog_cmd.file_name} {' '.join(version_files)}")
213213

214214
self.config.set_key("version", str(new_version))
215215

216216
if is_files_only:
217217
raise ExpectedExit()
218218

219219
c = git.commit(message, args=self._get_commit_args())
220+
if c.return_code != 0 and self.changelog:
221+
# Maybe pre-commit reformatted some files? Retry once
222+
cmd.run(f"git add {changelog_cmd.file_name} {' '.join(version_files)}")
223+
c = git.commit(message, args=self._get_commit_args())
220224
if c.return_code != 0:
221225
raise BumpCommitFailedError(f'git.commit error: "{c.err.strip()}"')
222226
c = git.tag(

tests/test_bump_create_commit_message.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import sys
2+
from pathlib import Path
3+
from textwrap import dedent
4+
15
import pytest
26
from packaging.version import Version
37

4-
from commitizen import bump
8+
from commitizen import bump, cli, cmd
59

610
conversion = [
711
(
@@ -20,3 +24,42 @@ def test_create_tag(test_input, expected):
2024
Version(current_version), Version(new_version), message_template
2125
)
2226
assert new_tag == expected
27+
28+
29+
def test_bump_pre_commit_changelog(tmp_commitizen_project, mocker, freezer):
30+
freezer.move_to("2022-04-01")
31+
testargs = ["cz", "bump", "--changelog", "--yes"]
32+
mocker.patch.object(sys, "argv", testargs)
33+
with tmp_commitizen_project.as_cwd():
34+
# Configure prettier as a pre-commit hook
35+
Path(".pre-commit-config.yaml").write_text(
36+
"""
37+
repos:
38+
- repo: https://github.com/pre-commit/mirrors-prettier
39+
rev: v2.6.2
40+
hooks:
41+
- id: prettier
42+
stages: [commit]
43+
"""
44+
)
45+
# Prettier inherits editorconfig
46+
Path(".editorconfig").write_text(
47+
"""
48+
[*]
49+
indent_size = 4
50+
"""
51+
)
52+
cmd.run("git add -A")
53+
cmd.run("git commit -m 'fix: _test'")
54+
cmd.run("pre-commit install")
55+
cli.main()
56+
# Pre-commit fixed last line adding extra indent and "\" char
57+
assert Path("CHANGELOG.md").read_text() == dedent(
58+
"""\
59+
## 0.1.1 (2022-04-01)
60+
61+
### Fix
62+
63+
- \\_test
64+
"""
65+
)

0 commit comments

Comments
 (0)