@@ -622,6 +622,71 @@ 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+
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+ bytes_commit = Commit (
671+ rw_repo ,
672+ commit .binsha ,
673+ message = commit .message .encode (commit .encoding ),
674+ encoding = commit .encoding ,
675+ )
676+
677+ assert bytes_commit .trailers_list == [("Reviewed-by" , "André <andre@example.com>" )]
678+
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+
625690 @with_rw_directory
626691 def test_index_commit_with_trailers (self , rw_dir ):
627692 """Test that IndexFile.commit() supports adding trailers."""
0 commit comments