Skip to content

Commit 6b8a726

Browse files
committed
Move diff into subproject
1 parent c574c35 commit 6b8a726

5 files changed

Lines changed: 33 additions & 36 deletions

File tree

dfetch/commands/diff.py

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@
9595

9696
import argparse
9797
import os
98+
import pathlib
9899

99100
import dfetch.commands.command
100101
import dfetch.project
101102
from dfetch.log import get_logger
102103
from dfetch.manifest.project import ProjectEntry
103104
from dfetch.project.git import GitSubProject
104-
from dfetch.project.metadata import Metadata
105105
from dfetch.project.subproject import SubProject
106106
from dfetch.project.superproject import SuperProject
107107
from dfetch.project.svn import SvnSubProject
@@ -155,19 +155,18 @@ def __call__(self, args: argparse.Namespace) -> None:
155155
for project in projects:
156156
patch_name = f"{project.name}.patch"
157157
with catch_runtime_exceptions(exceptions) as exceptions:
158-
repo = _get_repo(superproject, project)
159-
patch = _diff_from_repo(repo, project, revs)
158+
patch = _get_sub_project(superproject, project).diff(revs)
160159

161160
_dump_patch(
162-
superproject.manifest.path, revs, project, patch_name, patch
161+
superproject.root_directory, revs, project, patch_name, patch
163162
)
164163

165164
if exceptions:
166165
raise RuntimeError("\n".join(exceptions))
167166

168167

169-
def _get_repo(superproject: SuperProject, project: ProjectEntry) -> SubProject:
170-
"""Get the repo type from the project."""
168+
def _get_sub_project(superproject: SuperProject, project: ProjectEntry) -> SubProject:
169+
"""Get the subproject in the same vcs type as the superproject."""
171170
if not os.path.exists(project.destination):
172171
raise RuntimeError(
173172
"You cannot generate a diff of a project that was never fetched"
@@ -178,29 +177,7 @@ def _get_repo(superproject: SuperProject, project: ProjectEntry) -> SubProject:
178177
return SvnSubProject(project)
179178

180179
raise RuntimeError(
181-
"Can only create patch in SVN or Git repo",
182-
)
183-
184-
185-
def _diff_from_repo(repo: SubProject, project: ProjectEntry, revs: list[str]) -> str:
186-
"""Generate a relative diff for a svn repo."""
187-
if len(revs) > 2:
188-
raise RuntimeError(f"Too many revisions given! {revs}")
189-
190-
if not revs:
191-
revs.append(repo.metadata_revision())
192-
if not revs[-1]:
193-
raise RuntimeError(
194-
"When not providing any commits, dfetch starts from"
195-
f" the last commit to {Metadata.FILENAME} in {project.destination}."
196-
" Please either commit this, or specify a revision to start from with --revs"
197-
)
198-
199-
if len(revs) == 1:
200-
revs.append("")
201-
202-
return repo.get_diff(
203-
old_revision=revs[0], new_revision=revs[1], ignore=(Metadata.FILENAME,)
180+
"Can only create patch if your project is an SVN or Git repo",
204181
)
205182

206183

@@ -214,8 +191,7 @@ def _dump_patch(
214191
project.name,
215192
f"Generating patch {patch_name} {rev_range} in {os.path.dirname(path)}",
216193
)
217-
with open(patch_name, "w", encoding="UTF-8") as patch_file:
218-
patch_file.write(patch)
194+
pathlib.Path(patch_name).write_text(patch, encoding="UTF-8")
219195
else:
220196
if revs[1]:
221197
msg = f"No diffs found from {revs[0]} to {revs[1]}"

dfetch/project/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def current_revision(self) -> str:
5151
"""Get the revision of the metadata file."""
5252
return str(self._local_repo.get_current_hash())
5353

54-
def get_diff(
54+
def _diff_impl(
5555
self, old_revision: str, new_revision: Optional[str], ignore: Sequence[str]
5656
) -> str:
5757
"""Get the diff of two revisions."""

dfetch/project/subproject.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,13 @@ def current_revision(self) -> str:
373373
"""Get the revision of the metadata file."""
374374

375375
@abstractmethod
376-
def get_diff(
376+
def _diff_impl(
377377
self,
378378
old_revision: str, # noqa
379379
new_revision: Optional[str], # noqa
380380
ignore: Sequence[str],
381381
) -> str:
382-
"""Get the diff of two revisions."""
382+
"""Get the diff of two revisions, should be implemented by the child class."""
383383

384384
@abstractmethod
385385
def get_default_branch(self) -> str:
@@ -392,3 +392,24 @@ def is_license_file(filename: str) -> bool:
392392
fnmatch.fnmatch(filename.lower(), pattern)
393393
for pattern in SubProject.LICENSE_GLOBS
394394
)
395+
396+
def diff(self, revs: list[str]) -> str:
397+
"""Generate a relative diff for a subproject."""
398+
if len(revs) > 2:
399+
raise RuntimeError(f"Too many revisions given! {revs}")
400+
401+
if not revs:
402+
revs.append(self.metadata_revision())
403+
if not revs[-1]:
404+
raise RuntimeError(
405+
"When not providing any revisions, dfetch starts from"
406+
f" the last revision to {Metadata.FILENAME} in {self.local_path}."
407+
" Please either revision this, or specify a revision to start from with --revs"
408+
)
409+
410+
if len(revs) == 1:
411+
revs.append("")
412+
413+
return self._diff_impl(
414+
old_revision=revs[0], new_revision=revs[1], ignore=(Metadata.FILENAME,)
415+
)

dfetch/project/svn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def current_revision(self) -> str:
189189
"""Get the current revision of the repo."""
190190
return self._repo.get_last_changed_revision(self.local_path)
191191

192-
def get_diff(
192+
def _diff_impl(
193193
self, old_revision: str, new_revision: Optional[str], ignore: Sequence[str]
194194
) -> str:
195195
"""Get the diff between two revisions."""

tests/test_subproject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def current_revision(self):
4949
def metadata_revision(self):
5050
return "1"
5151

52-
def get_diff(self, old_revision, new_revision, ignore):
52+
def _diff_impl(self, old_revision, new_revision, ignore):
5353
return ""
5454

5555
def get_default_branch(self):

0 commit comments

Comments
 (0)