Skip to content

Commit 34ec40d

Browse files
CopilotByron
andauthored
Use commit encoding for trailer parsing
Agent-Logs-Url: https://github.com/gitpython-developers/GitPython/sessions/519084d5-d5e2-4486-a9cc-5c258e596e13 Co-authored-by: Byron <63622+Byron@users.noreply.github.com>
1 parent 1e2a895 commit 34ec40d

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

git/objects/commit.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ def trailers_list(self) -> List[Tuple[str, str]]:
450450
:return:
451451
List containing key-value tuples of whitespace stripped trailer information.
452452
"""
453-
trailer = self._interpret_trailers(self.repo, self.message, ["--parse"]).strip()
453+
trailer = self._interpret_trailers(self.repo, self.message, ["--parse"], encoding=self.encoding).strip()
454454

455455
if not trailer:
456456
return []
@@ -463,17 +463,23 @@ def trailers_list(self) -> List[Tuple[str, str]]:
463463
return trailer_list
464464

465465
@classmethod
466-
def _interpret_trailers(cls, repo: "Repo", message: Union[str, bytes], trailer_args: Sequence[str]) -> str:
466+
def _interpret_trailers(
467+
cls,
468+
repo: "Repo",
469+
message: Union[str, bytes],
470+
trailer_args: Sequence[str],
471+
encoding: str = default_encoding,
472+
) -> str:
467473
cmd = [repo.git.GIT_PYTHON_GIT_EXECUTABLE, "interpret-trailers", *trailer_args]
468474
proc: Git.AutoInterrupt = repo.git.execute( # type: ignore[call-overload]
469475
cmd,
470476
as_process=True,
471477
istream=PIPE,
472478
)
473-
message_bytes = message if isinstance(message, bytes) else message.encode(cls.default_encoding, errors="strict")
479+
message_bytes = message if isinstance(message, bytes) else message.encode(encoding, errors="strict")
474480
stdout_bytes, _ = proc.communicate(message_bytes)
475481
finalize_process(proc)
476-
return stdout_bytes.decode(cls.default_encoding, errors="strict")
482+
return stdout_bytes.decode(encoding, errors="strict")
477483

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

test/test_commit.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,30 @@ def test_create_from_tree_with_non_utf8_trailers(self, rw_dir):
647647
assert "Reviewed-by: André <andre@example.com>" in commit.message
648648
assert commit.trailers_list == [("Reviewed-by", "André <andre@example.com>")]
649649

650+
@with_rw_directory
651+
def test_trailers_list_with_non_utf8_message_bytes(self, rw_dir):
652+
"""Test that trailer parsing handles non-UTF-8 commit message bytes."""
653+
rw_repo = Repo.init(osp.join(rw_dir, "test_trailers_non_utf8_bytes"))
654+
with rw_repo.config_writer() as writer:
655+
writer.set_value("i18n", "commitencoding", "ISO-8859-1")
656+
657+
path = osp.join(str(rw_repo.working_tree_dir), "hello.txt")
658+
touch(path)
659+
rw_repo.index.add([path])
660+
tree = rw_repo.index.write_tree()
661+
662+
commit = Commit.create_from_tree(
663+
rw_repo,
664+
tree,
665+
"Résumé",
666+
head=True,
667+
trailers={"Reviewed-by": "André <andre@example.com>"},
668+
)
669+
670+
commit.message = commit.message.encode(commit.encoding)
671+
672+
assert commit.trailers_list == [("Reviewed-by", "André <andre@example.com>")]
673+
650674
@with_rw_directory
651675
def test_index_commit_with_trailers(self, rw_dir):
652676
"""Test that IndexFile.commit() supports adding trailers."""

0 commit comments

Comments
 (0)