Skip to content

Commit bd58716

Browse files
CopilotByron
andauthored
Use consistent interpret-trailers encoding
Agent-Logs-Url: https://github.com/gitpython-developers/GitPython/sessions/1a855cb6-0111-4f52-b48d-46417aec5bde Co-authored-by: Byron <63622+Byron@users.noreply.github.com>
1 parent b5b87dd commit bd58716

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

git/objects/commit.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +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-
cmd = ["git", "interpret-trailers", "--parse"]
454-
proc: Git.AutoInterrupt = self.repo.git.execute( # type: ignore[call-overload]
455-
cmd,
456-
as_process=True,
457-
istream=PIPE,
458-
)
459-
trailer: str = proc.communicate(str(self.message).encode())[0].decode("utf8")
460-
trailer = trailer.strip()
453+
trailer = self._interpret_trailers(self.repo, self.message, ["--parse"], self.encoding).strip()
461454

462455
if not trailer:
463456
return []
@@ -469,6 +462,18 @@ def trailers_list(self) -> List[Tuple[str, str]]:
469462

470463
return trailer_list
471464

465+
@staticmethod
466+
def _interpret_trailers(repo: "Repo", message: str, trailer_args: Sequence[str], encoding: str) -> str:
467+
cmd = [repo.git.GIT_PYTHON_GIT_EXECUTABLE, "interpret-trailers", *trailer_args]
468+
proc: Git.AutoInterrupt = repo.git.execute( # type: ignore[call-overload]
469+
cmd,
470+
as_process=True,
471+
istream=PIPE,
472+
)
473+
stdout_bytes, _ = proc.communicate(message.encode(encoding, errors="strict"))
474+
finalize_process(proc)
475+
return stdout_bytes.decode(encoding, errors="strict")
476+
472477
@property
473478
def trailers_dict(self) -> Dict[str, List[str]]:
474479
"""Get the trailers of the message as a dictionary.
@@ -699,15 +704,7 @@ def create_from_tree(
699704
trailer_args.append("--trailer")
700705
trailer_args.append(f"{key}: {val}")
701706

702-
cmd = [repo.git.GIT_PYTHON_GIT_EXECUTABLE, "interpret-trailers"] + trailer_args
703-
proc: Git.AutoInterrupt = repo.git.execute( # type: ignore[call-overload]
704-
cmd,
705-
as_process=True,
706-
istream=PIPE,
707-
)
708-
stdout_bytes, _ = proc.communicate(str(message).encode())
709-
finalize_process(proc)
710-
message = stdout_bytes.decode("utf8")
707+
message = cls._interpret_trailers(repo, str(message), trailer_args, conf_encoding)
711708
# END apply trailers
712709

713710
# CREATE NEW COMMIT

test/test_commit.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,31 @@ def test_create_from_tree_with_trailers_list(self, rw_dir):
622622
"Issue": ["456"],
623623
}
624624

625+
@with_rw_directory
626+
def test_create_from_tree_with_non_utf8_trailers(self, rw_dir):
627+
"""Test that trailer creation and parsing respect the configured commit encoding."""
628+
rw_repo = Repo.init(osp.join(rw_dir, "test_trailers_non_utf8"))
629+
with rw_repo.config_writer() as writer:
630+
writer.set_value("i18n", "commitencoding", "ISO-8859-1")
631+
632+
path = osp.join(str(rw_repo.working_tree_dir), "hello.txt")
633+
touch(path)
634+
rw_repo.index.add([path])
635+
tree = rw_repo.index.write_tree()
636+
637+
commit = Commit.create_from_tree(
638+
rw_repo,
639+
tree,
640+
"Résumé",
641+
head=True,
642+
trailers={"Reviewed-by": "André <andre@example.com>"},
643+
)
644+
645+
assert commit.encoding == "ISO-8859-1"
646+
assert "Résumé" in commit.message
647+
assert "Reviewed-by: André <andre@example.com>" in commit.message
648+
assert commit.trailers_list == [("Reviewed-by", "André <andre@example.com>")]
649+
625650
@with_rw_directory
626651
def test_index_commit_with_trailers(self, rw_dir):
627652
"""Test that IndexFile.commit() supports adding trailers."""

0 commit comments

Comments
 (0)