diff --git a/dvc/ignore.py b/dvc/ignore.py index 2cc35a816..7ef67763b 100644 --- a/dvc/ignore.py +++ b/dvc/ignore.py @@ -92,13 +92,14 @@ class DvcIgnoreRepo(DvcIgnore): class DvcIgnoreFilter: - def __init__(self, tree): + def __init__(self, tree, root_dir): self.tree = tree + self.root_dir = root_dir self.ignores = { DvcIgnoreDirs([".git", ".hg", ".dvc"]), DvcIgnoreRepo(), } - for root, dirs, files in self.tree.walk(self.tree.tree_root): + for root, dirs, files in self.tree.walk(self.root_dir): self._update(root) dirs[:], files[:] = self(root, dirs, files) @@ -124,7 +125,7 @@ class CleanTree(BaseTree): @cached_property def dvcignore(self): - return DvcIgnoreFilter(self.tree) + return DvcIgnoreFilter(self.tree, self.tree_root) @property def tree_root(self): diff --git a/tests/func/test_ignore.py b/tests/func/test_ignore.py index 65ad950c4..03806cb1e 100644 --- a/tests/func/test_ignore.py +++ b/tests/func/test_ignore.py @@ -11,6 +11,7 @@ from dvc.ignore import ( DvcIgnoreRepo, ) from dvc.remote import LocalRemote +from dvc.repo import Repo from dvc.scm.tree import WorkingTree from dvc.utils import relpath from dvc.utils.fs import get_mtime_and_size @@ -151,3 +152,20 @@ def test_ignore_external(tmp_dir, scm, dvc, tmp_path_factory): remote = LocalRemote(dvc, {}) result = {relpath(f, ext_dir) for f in remote.walk_files(ext_dir)} assert result == {"y.backup", "tmp"} + + +def test_ignore_subrepo(tmp_dir, scm, dvc): + tmp_dir.gen({".dvcignore": "foo", "subdir": {"foo": "foo"}}) + scm.add([".dvcignore"]) + scm.commit("init parent dvcignore") + + subrepo_dir = tmp_dir / "subdir" + assert not dvc.tree.exists(subrepo_dir / "foo") + + with subrepo_dir.chdir(): + subrepo = Repo.init(subdir=True) + scm.add(str(subrepo_dir / "foo")) + scm.commit("subrepo init") + + for _ in subrepo.brancher(all_commits=True): + assert subrepo.tree.exists(subrepo_dir / "foo")