@@ -355,3 +355,58 @@ def git(*args: str) -> str:
355355 [(modified , 1 ), (deleted , 1 ), ("plain name.md" , 1 ), ('new "file".txt' , 1 )]
356356 )
357357 assert parsed .stats ["total_files" ] == 4
358+
359+
360+ class TestNonUtf8FileContent :
361+ def test_latin1_file_round_trips_byte_for_byte (self , tmp_path : Path ) -> None :
362+ from pr_split .constants import AssignmentType
363+ from pr_split .diff_ops .reconstructor import materialize_group_files
364+ from pr_split .git_ops .branches import merge_base
365+ from pr_split .schemas import Group , GroupAssignment
366+
367+ def git (* args : str ) -> str :
368+ return subprocess .run (
369+ ["git" , "-c" , "user.name=t" , "-c" , "user.email=t@x" , * args ],
370+ cwd = tmp_path ,
371+ capture_output = True ,
372+ text = True ,
373+ check = True ,
374+ ).stdout
375+
376+ git ("init" , "-q" , "-b" , "main" )
377+ base_bytes = "caf\xe9 one\n keep\n " .encode ("latin-1" )
378+ dev_bytes = "caf\xe9 two\n keep\n " .encode ("latin-1" )
379+ (tmp_path / "legacy.txt" ).write_bytes (base_bytes )
380+ git ("add" , "-A" )
381+ git ("commit" , "-qm" , "base" )
382+ git ("checkout" , "-qb" , "dev" )
383+ (tmp_path / "legacy.txt" ).write_bytes (dev_bytes )
384+ git ("add" , "-A" )
385+ git ("commit" , "-qm" , "dev" )
386+
387+ cwd = os .getcwd ()
388+ os .chdir (tmp_path )
389+ try :
390+ parsed = parse_diff (extract_diff ("dev" , "main" ))
391+ group = Group (
392+ id = "pr-1" ,
393+ title = "t" ,
394+ description = "d" ,
395+ assignments = [
396+ GroupAssignment (
397+ file_path = "legacy.txt" ,
398+ assignment_type = AssignmentType .WHOLE_FILE ,
399+ hunk_indices = [0 ],
400+ )
401+ ],
402+ )
403+ materialized = materialize_group_files (parsed , group , merge_base ("main" , "dev" ))
404+ out = tmp_path / "out.txt"
405+ content = materialized ["legacy.txt" ]
406+ assert content is not None
407+ out .write_text (content , encoding = "utf-8" , errors = "surrogateescape" , newline = "" )
408+ finally :
409+ os .chdir (cwd )
410+
411+ assert [pf .path for pf in parsed .patch_set ] == ["legacy.txt" ]
412+ assert out .read_bytes () == dev_bytes
0 commit comments