From 7d0a06248d93560396f1eff42446deedc66ac433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Rowlands=20=28=EB=B3=80=EA=B8=B0=ED=98=B8=29?= Date: Thu, 28 May 2020 20:52:48 +0900 Subject: [PATCH] dvcignore: use correct root .dvcignore for subrepos (#3895) * ignore: dvcignore root is distinct from tree_root * for subrepos, dvcignore root should be the subrepo's .dvcignore, not host repo's .dvcignore * tests: add test for subrepo dvcignore behavior --- dvc/ignore.py | 7 ++++--- tests/func/test_ignore.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) 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")