Skip to content

Commit 0547e62

Browse files
ben-ednaspoorcc
authored andcommitted
Introduce OO-patch class
1 parent 7b8401a commit 0547e62

7 files changed

Lines changed: 293 additions & 252 deletions

File tree

dfetch/commands/format_patch.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import pathlib
3030
import re
3131

32-
import patch_ng
33-
3432
import dfetch.commands.command
3533
import dfetch.manifest.project
3634
import dfetch.project
@@ -40,14 +38,7 @@
4038
from dfetch.project.subproject import SubProject
4139
from dfetch.project.svnsubproject import SvnSubProject
4240
from dfetch.util.util import catch_runtime_exceptions, in_directory
43-
from dfetch.vcs.patch import (
44-
PatchAuthor,
45-
PatchInfo,
46-
add_prefix_to_patch,
47-
convert_patch_to,
48-
dump_patch,
49-
parse_patch,
50-
)
41+
from dfetch.vcs.patch import Patch, PatchAuthor, PatchInfo, PatchType
5142

5243
logger = get_logger(__name__)
5344

@@ -112,7 +103,7 @@ def __call__(self, args: argparse.Namespace) -> None:
112103
continue
113104

114105
version = subproject.on_disk_version()
115-
for idx, patch in enumerate(subproject.patch, start=1):
106+
for idx, patch_file in enumerate(subproject.patch, start=1):
116107

117108
patch_info = PatchInfo(
118109
author=PatchAuthor(
@@ -125,20 +116,20 @@ def __call__(self, args: argparse.Namespace) -> None:
125116
revision="" if not version else version.revision,
126117
)
127118

128-
corrected_patch = convert_patch_to(
129-
parse_patch(patch), _determine_target_patch_type(subproject)
119+
patch = Patch.from_file(patch_file)
120+
patch = patch.convert_type(
121+
_determine_target_patch_type(subproject)
130122
)
131-
prefixed_patch = add_prefix_to_patch(
132-
corrected_patch,
133-
path_prefix=re.split(r"\*", subproject.source, 1)[0].rstrip(
134-
"/"
135-
),
123+
patch = patch.add_prefix(
124+
re.split(r"\*", subproject.source, 1)[0].rstrip("/")
136125
)
137126

138-
output_patch_file = output_dir_path / pathlib.Path(patch).name
127+
output_patch_file = (
128+
output_dir_path / pathlib.Path(patch_file).name
129+
)
139130
output_patch_file.write_text(
140131
subproject.create_formatted_patch_header(patch_info)
141-
+ dump_patch(prefixed_patch)
132+
+ patch.dump()
142133
)
143134

144135
logger.print_info_line(
@@ -150,13 +141,13 @@ def __call__(self, args: argparse.Namespace) -> None:
150141
raise RuntimeError("\n".join(exceptions))
151142

152143

153-
def _determine_target_patch_type(subproject: SubProject) -> str:
144+
def _determine_target_patch_type(subproject: SubProject) -> PatchType:
154145
"""Determine the subproject type for the patch."""
155146
if isinstance(subproject, GitSubProject):
156-
required_type = patch_ng.GIT
147+
required_type = PatchType.GIT
157148
elif isinstance(subproject, SvnSubProject):
158-
required_type = patch_ng.SVN
149+
required_type = PatchType.SVN
159150
else:
160-
required_type = patch_ng.PLAIN
151+
required_type = PatchType.PLAIN
161152

162-
return str(required_type)
153+
return required_type

dfetch/project/subproject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from dfetch.project.metadata import Metadata
1414
from dfetch.util.util import hash_directory, safe_rm
1515
from dfetch.util.versions import latest_tag_from_list
16-
from dfetch.vcs.patch import PatchInfo, apply_patch
16+
from dfetch.vcs.patch import Patch, PatchInfo
1717

1818
logger = get_logger(__name__)
1919

@@ -161,7 +161,7 @@ def _apply_patches(self, count: int = -1) -> list[str]:
161161
normalized_patch_path = str(relative_patch_path.as_posix())
162162

163163
self._log_project(f'Applying patch "{normalized_patch_path}"')
164-
result = apply_patch(normalized_patch_path, root=self.local_path)
164+
result = Patch.from_file(normalized_patch_path).apply(root=self.local_path)
165165

166166
if result.encoding_warning:
167167
self._log_project(

dfetch/project/svnsuperproject.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
in_directory,
2121
resolve_absolute_path,
2222
)
23-
from dfetch.vcs.patch import (
24-
combine_patches,
25-
create_svn_patch_for_new_file,
26-
reverse_patch,
27-
)
23+
from dfetch.vcs.patch import Patch, PatchType
2824
from dfetch.vcs.svn import SvnRepo
2925

3026
logger = get_logger(__name__)
@@ -119,19 +115,21 @@ def diff(
119115
filtered = repo.create_diff(old, new, ignore)
120116

121117
if new:
122-
return filtered
118+
return filtered.dump()
123119

124-
patches: list[bytes] = [filtered.encode("utf-8")] if filtered else []
120+
patches: list[Patch] = [] if filtered.is_empty() else [filtered]
125121
with in_directory(path):
126122
for file_path in repo.untracked_files(".", ignore):
127-
patch = create_svn_patch_for_new_file(file_path)
128-
if patch:
129-
patches.append(patch.encode("utf-8"))
123+
patch_text = Patch.for_new_file(file_path, PatchType.SVN)
124+
if patch_text:
125+
patches.append(Patch.from_string(patch_text))
130126

131-
patch_str = combine_patches(patches)
127+
if not patches:
128+
return ""
132129

133-
# SVN has no way of producing a reverse working copy patch, reverse ourselves
134-
if reverse and not new:
135-
patch_str = reverse_patch(patch_str.encode("UTF-8"))
130+
combined = patches[0]
131+
if len(patches) > 1:
132+
combined = combined.combine(patches[1:])
136133

137-
return patch_str
134+
# SVN has no way of producing a reverse working copy patch, reverse ourselves
135+
return combined.dump() if not reverse else combined.reverse().dump()

dfetch/vcs/git.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from dfetch.log import get_logger
1313
from dfetch.util.cmdline import SubprocessCommandError, run_on_cmdline
1414
from dfetch.util.util import in_directory, safe_rmtree
15-
from dfetch.vcs.patch import create_git_patch_for_new_file
15+
from dfetch.vcs.patch import Patch, PatchType
1616

1717
logger = get_logger(__name__)
1818

@@ -477,7 +477,10 @@ def untracked_files_patch(self, ignore: Sequence[str] | None = None) -> str:
477477

478478
if untracked_files:
479479
return "\n".join(
480-
[create_git_patch_for_new_file(file) for file in untracked_files]
480+
[
481+
Patch.for_new_file(file, PatchType.GIT)
482+
for file in untracked_files
483+
]
481484
)
482485
return ""
483486

0 commit comments

Comments
 (0)