Skip to content

Commit 968713c

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 968713c

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

commitizen/commands/bump.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from logging import getLogger
12
from typing import List, Optional
23

34
import questionary
@@ -18,6 +19,8 @@
1819
NoVersionSpecifiedError,
1920
)
2021

22+
logger = getLogger('commitizen')
23+
2124

2225
class Bump:
2326
"""Show prompt for the user to create a guided commit."""
@@ -209,16 +212,22 @@ def __call__(self): # noqa: C901
209212
},
210213
)
211214
changelog_cmd()
212-
c = cmd.run(f"git add {changelog_cmd.file_name}")
215+
c = cmd.run(f"git add {changelog_cmd.file_name} {' '.join(version_files)}")
213216

214217
self.config.set_key("version", str(new_version))
215218

216219
if is_files_only:
217220
raise ExpectedExit()
218221

219222
c = git.commit(message, args=self._get_commit_args())
223+
if c.return_code != 0 and self.changelog:
224+
# Maybe pre-commit reformatted some files? Retry once
225+
logger.debug("1st git.commit error: %s", c.err)
226+
logger.info("1st commit attempt failed; retrying once")
227+
cmd.run(f"git add {changelog_cmd.file_name} {' '.join(version_files)}")
228+
c = git.commit(message, args=self._get_commit_args())
220229
if c.return_code != 0:
221-
raise BumpCommitFailedError(f'git.commit error: "{c.err.strip()}"')
230+
raise BumpCommitFailedError(f'2nd git.commit error: "{c.err.strip()}"')
222231
c = git.tag(
223232
new_tag_version,
224233
annotated=self.bump_settings.get("annotated_tag", False)

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)