diff --git a/src/extractcode/patch.py b/src/extractcode/patch.py index 81d4529..87101da 100644 --- a/src/extractcode/patch.py +++ b/src/extractcode/patch.py @@ -31,6 +31,15 @@ LOG = logging.getLogger(__name__) +def _as_text(line): + if not isinstance(line, bytes): + return line + try: + return line.decode("utf-8") + except UnicodeDecodeError: + return line.decode("latin-1") + + def extract(location, target_dir): """ Extract each patch of a patch file at `location` as files in a target_dir @@ -74,7 +83,7 @@ def extract(location, target_dir): with open(subfile_path, "w") as subfile: subfile.write("\n".join(text)) - return [] + return [] def is_patch(location, include_extracted=False): @@ -140,5 +149,5 @@ def patch_info(location): for ptch in patchset.items: src = fileutils.as_posixpath(ptch.source.strip()) tgt = fileutils.as_posixpath(ptch.target.strip()) - text = [l.strip() for l in patch_text(ptch) if l] - yield src, tgt, text + lines = [_as_text(line).strip() for line in patch_text(ptch) if line] + yield src, tgt, lines diff --git a/tests/test_patch.py b/tests/test_patch.py index 126edc1..4df4929 100644 --- a/tests/test_patch.py +++ b/tests/test_patch.py @@ -1615,3 +1615,23 @@ def test_patch_info_patch_patches_xvidcap_xorg_patch(self): test_file = self.get_test_loc("patch/patches/xvidcap-xorg.patch") expected_file = self.get_test_loc("patch/patches/xvidcap-xorg.patch.expected") check_patch(test_file, expected_file) + + +def test_extract_writes_a_subfile_for_each_patch_segment(tmp_path): + test_file = os.path.join( + os.path.dirname(__file__), "data", "patch", "patches", "dnsmasq_2.63-1.diff" + ) + warnings = patch.extract(test_file, str(tmp_path)) + extracted = [p for p in tmp_path.rglob("*") if p.is_file()] + assert warnings == [] + assert len(extracted) == 19 + + +def test_extract_handles_bytes_patch_lines(tmp_path): + test_file = os.path.join( + os.path.dirname(__file__), "data", "patch", "patches", "libmediainfo-0.7.43.diff" + ) + warnings = patch.extract(test_file, str(tmp_path)) + extracted = [p for p in tmp_path.rglob("*") if p.is_file()] + assert warnings == [] + assert len(extracted) == 1