From 11722e383463e8d538873091f105232ec6ab3770 Mon Sep 17 00:00:00 2001 From: Vinil Polepalli <105608101+vinilpolepalli@users.noreply.github.com> Date: Fri, 4 Dec 2026 00:21:22 -0500 Subject: [PATCH] fix(changelog): keep the case of titles when reading the changelog The changelog formats read the existing changelog in lower case. A `tag_format` with an uppercase character (e.g. `Release-$version`) then does not match the latest release title. The incremental changelog does not find the latest version and writes all versions again, so the changelog gets duplicate entries. Read the titles with their original case. Only the "unreleased" check stays case-insensitive. Assisted By: AI Co-Authored-By: Claude Opus 5.5 --- commitizen/changelog_formats/base.py | 7 +++-- .../changelog_formats/restructuredtext.py | 5 ++-- tests/commands/test_changelog_command.py | 28 +++++++++++++++++++ tests/test_changelog_format_asciidoc.py | 1 + tests/test_changelog_format_markdown.py | 1 + .../test_changelog_format_restructuredtext.py | 1 + tests/test_changelog_format_textile.py | 1 + 7 files changed, 39 insertions(+), 5 deletions(-) diff --git a/commitizen/changelog_formats/base.py b/commitizen/changelog_formats/base.py index dcddb5d7e0..87747f96f5 100644 --- a/commitizen/changelog_formats/base.py +++ b/commitizen/changelog_formats/base.py @@ -47,10 +47,11 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata: meta = Metadata() unreleased_level: int | None = None for index, line in enumerate(file): - line = line.strip().lower() + # Keep the original case: `tag_format` can contain uppercase characters. + line = line.strip() unreleased: int | None = None - if "unreleased" in line: + if "unreleased" in line.lower(): unreleased = self.parse_title_level(line) # Try to find beginning and end lines of the unreleased block if unreleased: @@ -84,7 +85,7 @@ def get_latest_full_release_from_file(self, file: IO[Any]) -> IncrementalMergeIn latest_version_index: int | None = None for index, line in enumerate(file): latest_version_index = index - line = line.strip().lower() + line = line.strip() parsed_version = self.parse_version_from_title(line) if ( diff --git a/commitizen/changelog_formats/restructuredtext.py b/commitizen/changelog_formats/restructuredtext.py index e8ce411e80..5b4380c333 100644 --- a/commitizen/changelog_formats/restructuredtext.py +++ b/commitizen/changelog_formats/restructuredtext.py @@ -24,7 +24,8 @@ def get_metadata_from_file(self, file: IO[str]) -> Metadata: out_metadata = Metadata() unreleased_title_kind: str | tuple[str, str] | None = None is_overlined_title = False - lines = [line.strip().lower() for line in file.readlines()] + # Keep the original case: `tag_format` can contain uppercase characters. + lines = [line.strip() for line in file.readlines()] for index, (first, second, third) in enumerate( zip_longest(lines, lines[1:], lines[2:], fillvalue="") @@ -44,7 +45,7 @@ def get_metadata_from_file(self, file: IO[str]) -> Metadata: if not title: continue - if "unreleased" in title: + if "unreleased" in title.lower(): unreleased_title_kind = kind out_metadata.unreleased_start = index continue diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 8922b256f9..d6a955cc9c 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1461,6 +1461,34 @@ def test_changelog_only_tag_matching_tag_format_included_suffix_sep( assert "## 0.2.0 (2021-06-11)" not in out +@pytest.mark.usefixtures("tmp_commitizen_project") +@pytest.mark.freeze_time("2021-06-11") +def test_changelog_incremental_with_uppercase_tag_format( + changelog_path: Path, + config_path: Path, + util: UtilFixture, +): + """An uppercase character in `tag_format` must not break the incremental changelog. + + Before the fix, the existing changelog was read in lower case, so the latest + release title did not match `tag_format` and all versions were written again. + """ + # Arrange + with config_path.open("a", encoding="utf-8") as f: + f.write('\ntag_format = "Release-${version}"\n') + util.create_file_and_commit("feat: new file") + util.run_cli("bump", "--changelog", "--yes") + util.create_file_and_commit("fix: some fix") + + # Act + util.run_cli("bump", "--changelog", "--yes") + + # Assert + out = changelog_path.read_text() + assert out.startswith("## Release-0.2.1 (2021-06-11)") + assert out.count("## Release-0.2.0 (2021-06-11)") == 1 + + @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_legacy_tags( changelog_path: Path, diff --git a/tests/test_changelog_format_asciidoc.py b/tests/test_changelog_format_asciidoc.py index a56a05ba08..03a4d4d67c 100644 --- a/tests/test_changelog_format_asciidoc.py +++ b/tests/test_changelog_format_asciidoc.py @@ -197,6 +197,7 @@ def test_get_latest_full_release_no_file(format: AsciiDoc): "1.0.0-a1.dev1", ), pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), + pytest.param("Release-${version}", "Release-1.0.0", "1.0.0"), ], indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_markdown.py b/tests/test_changelog_format_markdown.py index 1b75ed1f7f..0cb758cd5a 100644 --- a/tests/test_changelog_format_markdown.py +++ b/tests/test_changelog_format_markdown.py @@ -198,6 +198,7 @@ def test_get_metadata( "1.0.0-a1.dev1", ), pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), + pytest.param("Release-${version}", "Release-1.0.0", "1.0.0"), ], indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_restructuredtext.py b/tests/test_changelog_format_restructuredtext.py index 00f7740737..2da44182d4 100644 --- a/tests/test_changelog_format_restructuredtext.py +++ b/tests/test_changelog_format_restructuredtext.py @@ -368,6 +368,7 @@ def test_is_overlined_title(text: str, expected: bool): "1.0.0-a1.dev1", ), pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), + pytest.param("Release-${version}", "Release-1.0.0", "1.0.0"), ], indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_textile.py b/tests/test_changelog_format_textile.py index 481f903a7a..8eada39542 100644 --- a/tests/test_changelog_format_textile.py +++ b/tests/test_changelog_format_textile.py @@ -186,6 +186,7 @@ def test_get_metadata( "1.0.0-a1.dev1", ), pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), + pytest.param("Release-${version}", "Release-1.0.0", "1.0.0"), ], indirect=["format_with_tags"], )