Skip to content

Commit

Permalink
index: diff: support passing multiple roots
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop committed Sep 28, 2023
1 parent 0db221f commit 3c4aaeb
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/dvc_data/index/diff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import deque
from typing import TYPE_CHECKING, Any, Callable, Iterable, Optional
from typing import TYPE_CHECKING, Any, Callable, Iterable, Optional, Deque

from attrs import define
from dvc_objects.fs.callbacks import DEFAULT_CALLBACK, Callback
Expand Down Expand Up @@ -167,26 +167,29 @@ def _diff(
meta_cmp_key: Optional[Callable[[Optional["Meta"]], Any]] = None,
shallow: Optional[bool] = False,
callback: Callback = DEFAULT_CALLBACK,
start: Optional["DataIndexKey"] = None,
roots: Optional[Iterable["DataIndexKey"]] = None,
):
old_root_items = {}
new_root_items = {}
roots = roots or [()]
todo: Deque[Tuple[dict, dict, bool]] = deque()

start = start or ()
for root in roots:
old_root_items = {}
new_root_items = {}

if old is not None:
try:
old_root_items[start] = old.info(start)
except FileNotFoundError:
pass
if old is not None:
try:
old_root_items[root] = old.info(root)
except KeyError:
pass

if new is not None:
try:
new_root_items[root] = new.info(root)
except KeyError:
pass

if new is not None:
try:
new_root_items[start] = new.info(start)
except FileNotFoundError:
pass
todo.append((old_root_items, new_root_items, False))

todo = deque([(old_root_items, new_root_items, False)])
while todo:
old_items, new_items, unknown = todo.popleft()
for key in callback.wrap(old_items.keys() | new_items.keys()):
Expand Down Expand Up @@ -293,7 +296,7 @@ def diff(
meta_cmp_key: Optional[Callable[[Optional["Meta"]], Any]] = None,
shallow: Optional[bool] = False,
callback: Callback = DEFAULT_CALLBACK,
start: Optional["DataIndexKey"] = None,
roots: Optional[Iterable["DataIndexKey"]] = None,
):
changes = _diff(
old,
Expand All @@ -305,7 +308,7 @@ def diff(
meta_cmp_key=meta_cmp_key,
shallow=shallow,
callback=callback,
start=start,
roots=roots,
)

if with_renames and old is not None and new is not None:
Expand Down

0 comments on commit 3c4aaeb

Please sign in to comment.