From aa337d8d1fb0e584c36e0e55e6dc302b26a0caab Mon Sep 17 00:00:00 2001 From: GAARAN <334284286+stella-h-choi@users.noreply.github.com> Date: Sat, 5 Dec 2026 11:48:37 +0900 Subject: [PATCH 1/5] fix(changelog): read and write the exported template as UTF-8 `cz changelog --export-template` used the locale encoding, while Jinja loads the template as UTF-8, so a non-ASCII template failed to export where the locale is not UTF-8 (for example EUC-KR or cp949). Fixes #2136 Co-Authored-By: Claude Opus 5.5 --- commitizen/commands/changelog.py | 4 ++-- tests/commands/test_changelog_command.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index abe03a924..b2ad1ca7b 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -186,8 +186,8 @@ def _export_template(self, dist: str) -> None: if filename is None: raise NotAllowed("Template filename is not set") - text = Path(filename).read_text() - Path(dist).write_text(text) + text = Path(filename).read_text(encoding="utf-8") + Path(dist).write_text(text, encoding="utf-8") def __call__(self) -> None: commit_parser = self.cz.commit_parser diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 8922b256f..6f7874b97 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1,5 +1,6 @@ from __future__ import annotations +import io import itertools from pathlib import Path from textwrap import dedent @@ -1663,6 +1664,29 @@ def test_export_changelog_template_from_plugin( assert changelog_jinja_file.read_text() == tpl +def test_export_changelog_template_with_non_utf8_locale( + changelog_jinja_file: Path, + mock_plugin: BaseCommitizen, + changelog_format: ChangelogFormat, + tmp_path: Path, + util: UtilFixture, + monkeypatch: pytest.MonkeyPatch, +): + # pathlib falls back to io.text_encoding when no encoding is given. + # Make it return cp949, the locale encoding on Korean Windows. + monkeypatch.setattr( + io, "text_encoding", lambda encoding, stacklevel=2: encoding or "cp949" + ) + src = tmp_path / changelog_format.template + # cp949 cannot decode these UTF-8 bytes, and cannot encode the sparkles + src.write_bytes("## 변경 ✨\n".encode()) + mock_plugin.template_loader = FileSystemLoader(tmp_path) + + util.run_cli("changelog", "--export-template", str(changelog_jinja_file)) + + assert changelog_jinja_file.read_bytes() == src.read_bytes() + + def test_export_changelog_template_fails_when_template_has_no_filename( mocker: MockFixture, changelog_jinja_file: Path, From 2a41ab9e8497418cc90c62dc893bcbc4199fa988 Mon Sep 17 00:00:00 2001 From: GAARAN <334284286+stella-h-choi@users.noreply.github.com> Date: Sat, 5 Dec 2026 12:37:55 +0900 Subject: [PATCH 2/5] test(changelog): compare the exported template as text Windows writes text files with CRLF line endings, so compare the decoded text instead of the raw bytes. Co-Authored-By: Claude Opus 5.5 --- commitizen/commands/changelog.py | 4 ++-- tests/commands/test_changelog_command.py | 4 +++- uv.lock | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index b2ad1ca7b..e9975aea2 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -186,8 +186,8 @@ def _export_template(self, dist: str) -> None: if filename is None: raise NotAllowed("Template filename is not set") - text = Path(filename).read_text(encoding="utf-8") - Path(dist).write_text(text, encoding="utf-8") + # Copy bytes verbatim so Windows does not translate newlines. + Path(dist).write_bytes(Path(filename).read_bytes()) def __call__(self) -> None: commit_parser = self.cz.commit_parser diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 6f7874b97..e8283969c 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1684,7 +1684,9 @@ def test_export_changelog_template_with_non_utf8_locale( util.run_cli("changelog", "--export-template", str(changelog_jinja_file)) - assert changelog_jinja_file.read_bytes() == src.read_bytes() + assert changelog_jinja_file.read_text(encoding="utf-8") == src.read_text( + encoding="utf-8" + ) def test_export_changelog_template_fails_when_template_has_no_filename( diff --git a/uv.lock b/uv.lock index 17c71de7d..d76c1620d 100644 --- a/uv.lock +++ b/uv.lock @@ -300,7 +300,7 @@ test = [ [package.metadata] requires-dist = [ - { name = "argcomplete", specifier = ">=1.12.1,<3.8" }, + { name = "argcomplete", specifier = ">=1.12.1,<3.9" }, { name = "charset-normalizer", specifier = ">=2.1.0,<4" }, { name = "colorama", specifier = ">=0.4.1,<1.0" }, { name = "decli", specifier = ">=0.6.0,<1.0" }, From a364d02f8022ba1a67130a56332977a5eb1124f1 Mon Sep 17 00:00:00 2001 From: GAARAN <334284286+stella-h-choi@users.noreply.github.com> Date: Sat, 5 Dec 2026 12:38:24 +0900 Subject: [PATCH 3/5] fix(changelog): keep reading and writing the template as UTF-8 Undo the byte copy and the uv.lock change that slipped into the previous commit. The test now compares decoded text, so the UTF-8 read and write pass on Windows as well. Co-Authored-By: Claude Opus 5.5 --- commitizen/commands/changelog.py | 4 ++-- uv.lock | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index e9975aea2..b2ad1ca7b 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -186,8 +186,8 @@ def _export_template(self, dist: str) -> None: if filename is None: raise NotAllowed("Template filename is not set") - # Copy bytes verbatim so Windows does not translate newlines. - Path(dist).write_bytes(Path(filename).read_bytes()) + text = Path(filename).read_text(encoding="utf-8") + Path(dist).write_text(text, encoding="utf-8") def __call__(self) -> None: commit_parser = self.cz.commit_parser diff --git a/uv.lock b/uv.lock index d76c1620d..17c71de7d 100644 --- a/uv.lock +++ b/uv.lock @@ -300,7 +300,7 @@ test = [ [package.metadata] requires-dist = [ - { name = "argcomplete", specifier = ">=1.12.1,<3.9" }, + { name = "argcomplete", specifier = ">=1.12.1,<3.8" }, { name = "charset-normalizer", specifier = ">=2.1.0,<4" }, { name = "colorama", specifier = ">=0.4.1,<1.0" }, { name = "decli", specifier = ">=0.6.0,<1.0" }, From f978122a3c1622b96dec390d799abec3a5304fa1 Mon Sep 17 00:00:00 2001 From: GAARAN <334284286+stella-h-choi@users.noreply.github.com> Date: Sat, 5 Dec 2026 12:46:41 +0900 Subject: [PATCH 4/5] fix(changelog): copy the exported template byte for byte Reading and writing in text mode translates newlines on Windows, so the exported file ended up with CRLF. Copy the bytes instead and compare bytes in the test again. Co-Authored-By: Claude Sonnet 5.5 --- commitizen/commands/changelog.py | 4 ++-- tests/commands/test_changelog_command.py | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index b2ad1ca7b..e9975aea2 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -186,8 +186,8 @@ def _export_template(self, dist: str) -> None: if filename is None: raise NotAllowed("Template filename is not set") - text = Path(filename).read_text(encoding="utf-8") - Path(dist).write_text(text, encoding="utf-8") + # Copy bytes verbatim so Windows does not translate newlines. + Path(dist).write_bytes(Path(filename).read_bytes()) def __call__(self) -> None: commit_parser = self.cz.commit_parser diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index e8283969c..6f7874b97 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1684,9 +1684,7 @@ def test_export_changelog_template_with_non_utf8_locale( util.run_cli("changelog", "--export-template", str(changelog_jinja_file)) - assert changelog_jinja_file.read_text(encoding="utf-8") == src.read_text( - encoding="utf-8" - ) + assert changelog_jinja_file.read_bytes() == src.read_bytes() def test_export_changelog_template_fails_when_template_has_no_filename( From ebb872d0c413727187d1ab43090b1b1d8ffda884 Mon Sep 17 00:00:00 2001 From: GAARAN <334284286+stella-h-choi@users.noreply.github.com> Date: Sat, 5 Dec 2026 12:47:19 +0900 Subject: [PATCH 5/5] Revert "fix(changelog): copy the exported template byte for byte" This reverts commit f978122a3c16, which was pushed by mistake. The UTF-8 read and write with the text comparison already pass on Windows. Co-Authored-By: Claude Opus 5.5 --- commitizen/commands/changelog.py | 4 ++-- tests/commands/test_changelog_command.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index e9975aea2..b2ad1ca7b 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -186,8 +186,8 @@ def _export_template(self, dist: str) -> None: if filename is None: raise NotAllowed("Template filename is not set") - # Copy bytes verbatim so Windows does not translate newlines. - Path(dist).write_bytes(Path(filename).read_bytes()) + text = Path(filename).read_text(encoding="utf-8") + Path(dist).write_text(text, encoding="utf-8") def __call__(self) -> None: commit_parser = self.cz.commit_parser diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 6f7874b97..e8283969c 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -1684,7 +1684,9 @@ def test_export_changelog_template_with_non_utf8_locale( util.run_cli("changelog", "--export-template", str(changelog_jinja_file)) - assert changelog_jinja_file.read_bytes() == src.read_bytes() + assert changelog_jinja_file.read_text(encoding="utf-8") == src.read_text( + encoding="utf-8" + ) def test_export_changelog_template_fails_when_template_has_no_filename(