Skip to content

Commit 8c20258

Browse files
authored
Merge pull request #581 from lovetheguitar/fix/regression_since_v2.27.0_untracked_changelog_gets_git_added
fix(bump.py): `CHANGELOG.md` gets git added and commited correctly
2 parents 0e026d4 + f6ab845 commit 8c20258

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

commitizen/commands/bump.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ def __call__(self): # noqa: C901
205205
},
206206
)
207207
changelog_cmd()
208-
c = cmd.run(f"git add {changelog_cmd.file_name} {' '.join(version_files)}")
208+
git_add_changelog_and_version_files_command = (
209+
f"git add {changelog_cmd.file_name} "
210+
f"{' '.join(file_name.partition(':')[0] for file_name in version_files)}"
211+
)
212+
c = cmd.run(git_add_changelog_and_version_files_command)
209213

210214
# Do not perform operations over files or git.
211215
if dry_run:
@@ -228,7 +232,7 @@ def __call__(self): # noqa: C901
228232
# Maybe pre-commit reformatted some files? Retry once
229233
logger.debug("1st git.commit error: %s", c.err)
230234
logger.info("1st commit attempt failed; retrying once")
231-
cmd.run(f"git add {changelog_cmd.file_name} {' '.join(version_files)}")
235+
cmd.run(git_add_changelog_and_version_files_command)
232236
c = git.commit(message, args=self._get_commit_args())
233237
if c.return_code != 0:
234238
raise BumpCommitFailedError(f'2nd git.commit error: "{c.err.strip()}"')

commitizen/git.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ def get_commits(
126126
return git_commits
127127

128128

129+
def get_filenames_in_commit(git_reference: str = ""):
130+
"""Get the list of files that were committed in the requested git reference.
131+
132+
:param git_reference: a git reference as accepted by `git show`, default: the last commit
133+
134+
:returns: file names committed in the last commit by default or inside the passed git reference
135+
"""
136+
c = cmd.run(f"git show --name-only --pretty=format: {git_reference}")
137+
if c.return_code == 0:
138+
return c.out.strip().split("\n")
139+
else:
140+
raise GitCommandError(c.err)
141+
142+
129143
def get_tags(dateformat: str = "%Y-%m-%d") -> List[GitTag]:
130144
inner_delimiter = "---inner_delimiter---"
131145
formatter = (

tests/commands/test_bump_command.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
22
import sys
3+
from typing import Tuple
34
from unittest.mock import MagicMock
45

56
import pytest
@@ -528,3 +529,88 @@ def test_bump_with_changelog_to_stdout_dry_run_arg(mocker, capsys, changelog_pat
528529
assert out.startswith("#")
529530
assert "this should appear in stdout with dry-run enabled" in out
530531
assert "0.2.0" in out
532+
533+
534+
@pytest.mark.parametrize(
535+
"version_filepath, version_regex, version_file_content",
536+
[
537+
pytest.param(
538+
"pyproject.toml",
539+
"pyproject.toml:^version",
540+
"""
541+
[tool.poetry]
542+
name = "my_package"
543+
version = "0.1.0"
544+
""",
545+
id="version in pyproject.toml with regex",
546+
),
547+
pytest.param(
548+
"pyproject.toml",
549+
"pyproject.toml",
550+
"""
551+
[tool.poetry]
552+
name = "my_package"
553+
version = "0.1.0"
554+
""",
555+
id="version in pyproject.toml without regex",
556+
),
557+
pytest.param(
558+
"__init__.py",
559+
"__init__.py:^__version__",
560+
"""
561+
'''This is a test file.'''
562+
__version__ = "0.1.0"
563+
""",
564+
id="version in __init__.py with regex",
565+
),
566+
],
567+
)
568+
@pytest.mark.parametrize(
569+
"cli_bump_changelog_args",
570+
[
571+
("cz", "bump", "--changelog", "--yes"),
572+
(
573+
"cz",
574+
"bump",
575+
"--changelog",
576+
"--changelog-to-stdout",
577+
"--annotated",
578+
"--check-consistency",
579+
"--yes",
580+
),
581+
],
582+
ids=lambda cmd_tuple: " ".join(cmd_tuple),
583+
)
584+
def test_bump_changelog_command_commits_untracked_changelog_and_version_files(
585+
tmp_commitizen_project,
586+
mocker,
587+
cli_bump_changelog_args: Tuple[str, ...],
588+
version_filepath: str,
589+
version_regex: str,
590+
version_file_content: str,
591+
):
592+
"""Ensure that changelog always gets committed, no matter what version file or cli options get passed.
593+
594+
Steps:
595+
- Append the version file's name and regex commitizen configuration lines to `pyproject.toml`.
596+
- Append to or create the version file.
597+
- Add a commit of type fix to be eligible for a version bump.
598+
- Call commitizen main cli and assert that the `CHANGELOG.md` and the version file were committed.
599+
"""
600+
601+
with tmp_commitizen_project.join("pyproject.toml").open(
602+
mode="a"
603+
) as commitizen_config:
604+
commitizen_config.write(f"version_files = [\n" f"'{version_regex}'\n]")
605+
606+
with tmp_commitizen_project.join(version_filepath).open(mode="a+") as version_file:
607+
version_file.write(version_file_content)
608+
609+
create_file_and_commit("fix: some test commit")
610+
611+
mocker.patch.object(sys, "argv", cli_bump_changelog_args)
612+
cli.main()
613+
614+
commit_file_names = git.get_filenames_in_commit()
615+
assert "CHANGELOG.md" in commit_file_names
616+
assert version_filepath in commit_file_names

0 commit comments

Comments
 (0)