33import logging
44import os
55
6+ from funcy import cached_property
67from pathspec import PathSpec
78from pathspec .patterns import GitWildMatchPattern
89
10+ from dvc .scm .tree import BaseTree
911from dvc .utils import relpath
1012
1113logger = logging .getLogger (__name__ )
@@ -72,13 +74,12 @@ def __eq__(self, other):
7274
7375
7476class DvcIgnoreFilter (object ):
75- def __init__ (self , root_dir , tree ):
77+ def __init__ (self , tree ):
7678 self .tree = tree
7779 self .ignores = {DvcIgnoreDirs ([".git" , ".hg" , ".dvc" ])}
78- self ._update (root_dir )
79- for root , dirs , _ in self .tree .walk (root_dir , dvcignore = self ):
80- for d in dirs :
81- self ._update (os .path .join (root , d ))
80+ for root , dirs , files in self .tree .walk (self .tree .tree_root ):
81+ self ._update (root )
82+ dirs [:], files [:] = self (root , dirs , files )
8283
8384 def _update (self , dirname ):
8485 ignore_file_path = os .path .join (dirname , DvcIgnore .DVCIGNORE_FILE )
@@ -90,3 +91,39 @@ def __call__(self, root, dirs, files):
9091 dirs , files = ignore (root , dirs , files )
9192
9293 return dirs , files
94+
95+
96+ class CleanTree (BaseTree ):
97+ def __init__ (self , tree ):
98+ self .tree = tree
99+
100+ @cached_property
101+ def dvcignore (self ):
102+ return DvcIgnoreFilter (self .tree )
103+
104+ @property
105+ def tree_root (self ):
106+ return self .tree .tree_root
107+
108+ def open (self , path , mode = "r" , encoding = "utf-8" ):
109+ return self .tree .open (path , mode , encoding )
110+
111+ def exists (self , path ):
112+ return self .tree .exists (path )
113+
114+ def isdir (self , path ):
115+ return self .tree .isdir (path )
116+
117+ def isfile (self , path ):
118+ return self .tree .isfile (path )
119+
120+ def walk (self , top , topdown = True ):
121+ for root , dirs , files in self .tree .walk (top , topdown ):
122+ dirs [:], files [:] = self .dvcignore (root , dirs , files )
123+
124+ yield root , dirs , files
125+
126+ def walk_files (self , top ):
127+ for root , _ , files in self .walk (top ):
128+ for file in files :
129+ yield os .path .join (root , file )
0 commit comments