Skip to content

Commit 633abdb

Browse files
CopilotByron
andauthored
Fix trailer subprocess lifetime
Agent-Logs-Url: https://github.com/gitpython-developers/GitPython/sessions/3cc0bd6d-d54d-4299-9a18-1576c2a91c12 Co-authored-by: Byron <63622+Byron@users.noreply.github.com>
1 parent 4aa8157 commit 633abdb

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

git/objects/commit.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,18 @@ def _interpret_trailers(
470470
trailer_args: Sequence[str],
471471
encoding: str = default_encoding,
472472
) -> str:
473+
message_bytes = message if isinstance(message, bytes) else message.encode(encoding, errors="strict")
473474
cmd = [repo.git.GIT_PYTHON_GIT_EXECUTABLE, "interpret-trailers", *trailer_args]
474475
proc: Git.AutoInterrupt = repo.git.execute( # type: ignore[call-overload]
475476
cmd,
476477
as_process=True,
477478
istream=PIPE,
478479
)
479-
message_bytes = message if isinstance(message, bytes) else message.encode(encoding, errors="strict")
480-
stdout_bytes, _ = proc.communicate(message_bytes)
481-
finalize_process(proc)
482-
return stdout_bytes.decode(encoding, errors="strict")
480+
try:
481+
stdout_bytes, _ = proc.communicate(message_bytes)
482+
return stdout_bytes.decode(encoding, errors="strict")
483+
finally:
484+
finalize_process(proc)
483485

484486
@property
485487
def trailers_dict(self) -> Dict[str, List[str]]:

test/test_commit.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,17 @@ def test_trailers_list_with_non_utf8_message_bytes(self, rw_dir):
676676

677677
assert bytes_commit.trailers_list == [("Reviewed-by", "André <andre@example.com>")]
678678

679+
def test_interpret_trailers_encodes_before_launching_process(self):
680+
"""Test that encoding failures happen before spawning interpret-trailers."""
681+
repo = Mock()
682+
repo.git = Mock()
683+
repo.git.GIT_PYTHON_GIT_EXECUTABLE = "git"
684+
685+
with self.assertRaises(UnicodeEncodeError):
686+
Commit._interpret_trailers(repo, "Euro: €", ["--parse"], encoding="ISO-8859-1")
687+
688+
repo.git.execute.assert_not_called()
689+
679690
@with_rw_directory
680691
def test_index_commit_with_trailers(self, rw_dir):
681692
"""Test that IndexFile.commit() supports adding trailers."""

0 commit comments

Comments
 (0)