Skip to content

Commit aafac24

Browse files
committed
fix issue on finding subtree
1 parent 656fe46 commit aafac24

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

dvc/repo/tree.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from typing import Optional
44

5-
from funcy import cached_property, post_processing
5+
from funcy import cached_property
66

77
from dvc.dvcfile import is_valid_filename
88
from dvc.exceptions import OutputNotFoundError
@@ -277,8 +277,12 @@ def _find_subtree(self, path_prefix) -> Optional[DvcTree]:
277277
def _find_subtree_with_prefix(self, path):
278278
# dvctrees is already ordered from low to high
279279
path_prefix = os.path.abspath(path)
280+
exact_match = self._dvctrees.get(path_prefix)
281+
if exact_match:
282+
return path_prefix, exact_match
283+
280284
for pref, tree in self._dvctrees.items():
281-
if path_prefix.startswith(pref):
285+
if path_prefix.startswith(pref + os.sep):
282286
return pref, tree
283287
return "", None
284288

@@ -487,21 +491,3 @@ def hash_jobs(self):
487491

488492
def in_subtree(self, path):
489493
return self._find_subtree(path)
490-
491-
@post_processing("\r\n".join)
492-
def _visualize(self, top, **kwargs):
493-
"""`tree`-like output, useful for debugging/visualizing"""
494-
indent = 4
495-
spacing = " " * indent
496-
tee = "├── "
497-
last = "└── "
498-
for root, _, files in self.walk(top, **kwargs):
499-
level = root.replace(top, "").count(os.sep)
500-
indent = spacing * level
501-
yield "{}{}/".format(indent, os.path.basename(root))
502-
sub_indent = spacing * (level + 1)
503-
length = len(files)
504-
for i, f in enumerate(files):
505-
yield "{}{}{}".format(
506-
sub_indent, tee if i + 1 != length else last, f
507-
)

tests/func/test_get.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,13 @@ def test_get_pipeline_tracked_outs(
238238
assert (git_dir / "baz").read_text() == "foo"
239239

240240

241-
def make_subrepo(dir_, scm, config):
241+
def make_subrepo(dir_, scm, config=None):
242242
dir_.mkdir(parents=True)
243243
with dir_.chdir():
244244
dir_.scm = scm
245245
dir_.init(dvc=True, subdir=True)
246-
dir_.add_remote(config=config)
246+
if config:
247+
dir_.add_remote(config=config)
247248

248249

249250
@pytest.mark.parametrize(

tests/unit/repo/test_repo_tree.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,36 @@ def test_isdvc(tmp_dir, dvc):
177177
assert tree.isdvc("dir")
178178
assert not tree.isdvc("dir/baz")
179179
assert tree.isdvc("dir/baz", recursive=True, strict=False)
180+
181+
182+
def test_in_subtree(tmp_dir, scm, dvc):
183+
from tests.func.test_get import make_subrepo
184+
185+
subrepo1 = tmp_dir / "dir" / "repo"
186+
subrepo2 = tmp_dir / "dir" / "repo2"
187+
188+
for repo in [subrepo1, subrepo2]:
189+
make_subrepo(repo, scm)
190+
191+
(tmp_dir / "dir" / "repotxt").write_text("file to confuse RepoTree")
192+
subrepo1.dvc_gen({"foo": "foo"}, commit="FOO")
193+
subrepo2.dvc_gen({"bar": "bar"}, commit="BAR")
194+
195+
# dvc.tree ignores subrepos by default,
196+
# but we just want to test `in_subtree()`, which is purely lexical
197+
tree = RepoTree(dvc.tree, [dvc, subrepo1.dvc, subrepo2.dvc])
198+
199+
assert tree.in_subtree(str(tmp_dir / "dir")).repo == dvc
200+
assert tree.in_subtree(str(tmp_dir / "dir" / "re")).repo == dvc
201+
assert tree.in_subtree(str(tmp_dir / "dir" / "repo")).repo == subrepo1.dvc
202+
assert tree.in_subtree(str(tmp_dir / "dir" / "repotxt")).repo == dvc
203+
assert tree.in_subtree(str(tmp_dir / "dir" / "repo2")).repo == subrepo2.dvc
204+
205+
for repo in [tmp_dir, subrepo1, subrepo2]:
206+
for path in ["", "foo", "something-that-does-not-exist"]:
207+
p = os.path.join(repo, path)
208+
subtree = tree.in_subtree(p)
209+
assert subtree, f"subtree not found for path '{p}'"
210+
assert (
211+
subtree.repo == repo.dvc
212+
), f"repo did not match for path '{p}'"

0 commit comments

Comments
 (0)