Skip to content

Commit c6a9bdf

Browse files
Alpenjclaude
andcommitted
fix(commit): use the configured encoding for the --edit message file
`cz commit --edit` wrote the message to a temporary file with the locale encoding and read it back the same way. On Windows the locale encoding is a legacy code page (for example cp1252), so a message with characters outside it failed with a UnicodeEncodeError, and a file that the editor saved as UTF-8 was decoded incorrectly. Use the `encoding` setting, as the other commit message files do. Assisted By: AI Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 33c248e commit c6a9bdf

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎commitizen/commands/commit.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,15 @@ def manual_edit(self, message: str) -> str:
136136
exec_path = shutil.which(editor)
137137
if exec_path is None:
138138
raise RuntimeError(f"Editor '{editor}' not found.")
139-
with tempfile.NamedTemporaryFile(mode="w", delete=False) as file:
139+
encoding = self.config.settings["encoding"]
140+
with tempfile.NamedTemporaryFile(
141+
mode="w", delete=False, encoding=encoding
142+
) as file:
140143
file.write(message)
141144
file_path = file.name
142145
argv = [exec_path, file_path]
143146
subprocess.call(argv)
144-
message = Path(file_path).read_text().strip()
147+
message = Path(file_path).read_text(encoding=encoding).strip()
145148
os.unlink(file.name)
146149
return message
147150

‎tests/commands/test_commit_command.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,35 @@ def test_manual_edit(editor, config, mocker: MockFixture, tmp_path):
320320
assert edited_message == test_message.strip()
321321

322322

323+
@pytest.mark.usefixtures("staging_is_clean")
324+
def test_manual_edit_keeps_non_ascii_message(config, mocker: MockFixture):
325+
"""The editor file uses the configured encoding, not the locale encoding.
326+
327+
On Windows the locale encoding is a legacy code page (e.g. cp1252), which
328+
cannot encode this message.
329+
"""
330+
# Arrange
331+
message = "feat: add 안녕하세요 greeting ✨"
332+
edited_message = "feat: add 안녕하세요 greeting ✨ (edited)"
333+
mocker.patch("commitizen.git.get_core_editor", return_value="vim")
334+
mocker.patch("shutil.which", return_value="vim")
335+
336+
def edit_in_editor(argv: list[str]) -> int:
337+
message_file = Path(argv[1])
338+
assert message_file.read_text(encoding="utf-8") == message
339+
message_file.write_text(edited_message, encoding="utf-8")
340+
return 0
341+
342+
mocker.patch("subprocess.call", side_effect=edit_in_editor)
343+
commit_cmd = commands.Commit(config, {"edit": True})
344+
345+
# Act
346+
result = commit_cmd.manual_edit(message)
347+
348+
# Assert
349+
assert result == edited_message
350+
351+
323352
@pytest.mark.usefixtures("staging_is_clean", "prompt_mock_feat")
324353
@pytest.mark.parametrize(
325354
"out", ["no changes added to commit", "nothing added to commit"]

0 commit comments

Comments
 (0)