Skip to content

Commit f0783fc

Browse files
alexbruywonder-sk
authored andcommitted
simplify handling of downloaded diffs
1 parent 227a442 commit f0783fc

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

mergin/client.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -779,23 +779,12 @@ def get_file_diff(self, project_dir, file_path, output_diff, version_from, versi
779779
versions_to_fetch = get_versions_with_file_changes(
780780
self, project_path, file_path, version_from=version_from, version_to=version_to, file_history=file_history
781781
)
782-
diffs = self.download_file_diffs(project_dir, file_path, versions_to_fetch[1:])
783-
# if some changesets were cached before, they will be missed from the
784-
# list of downloaded changesets and should be added before any attempt
785-
# to concatenate them into single file
786-
sort = False
787-
if len(diffs) < len(versions_to_fetch[1:]):
788-
sort = True
789-
for v in versions_to_fetch[1:]:
790-
file_name = mp.fpath_cache(file_history["history"][v]["diff"]["path"], v)
791-
if file_name not in diffs:
792-
diffs.append(file_name)
793-
794-
# at this point diffs in the list might be in the wrong order
795-
# we need to sort them by version in ascending order
796-
if sort:
797-
ver_regex = re.compile("\/v[0-9]+\/")
798-
diffs.sort(key=lambda x: int(ver_regex.search(x)[0].strip("/").replace("v", "")))
782+
self.download_file_diffs(project_dir, file_path, versions_to_fetch[1:])
783+
784+
# collect required versions from the cache
785+
diffs = []
786+
for v in versions_to_fetch[1:]:
787+
diffs.append(mp.fpath_cache(file_history["history"][v]["diff"]["path"], v))
799788

800789
# concatenate diffs, if needed
801790
output_dir = os.path.dirname(output_diff)
@@ -807,7 +796,8 @@ def get_file_diff(self, project_dir, file_path, output_diff, version_from, versi
807796
shutil.copy(diffs[0], output_diff)
808797

809798
def download_file_diffs(self, project_dir, file_path, versions):
810-
""" Download file diffs for specified versions.
799+
""" Download file diffs for specified versions if they are not present
800+
in the cache.
811801
812802
:param project_dir: project local directory
813803
:type project_dir: String

0 commit comments

Comments
 (0)