1818RepoFactory = Union [Callable [[str ], "Repo" ], Type ["Repo" ]]
1919
2020
21+ def _wrap_walk (dvc_fs , * args , ** kwargs ):
22+ for root , dnames , fnames in dvc_fs .walk (* args , ** kwargs ):
23+ yield dvc_fs .path .join (dvc_fs .repo .root_dir , root ), dnames , fnames
24+
25+
2126class RepoFileSystem (FileSystem ): # pylint:disable=abstract-method
2227 """DVC + git-tracked files fs.
2328
@@ -191,7 +196,9 @@ def _is_dvc_repo(self, dir_path):
191196 repo_path = os .path .join (dir_path , Repo .DVC_DIR )
192197 return self ._main_repo .fs .isdir (repo_path )
193198
194- def _get_fs_pair (self , path ) -> Tuple [FileSystem , Optional [DvcFileSystem ]]:
199+ def _get_fs_pair (
200+ self , path
201+ ) -> Tuple [FileSystem , Optional [DvcFileSystem ], str ]:
195202 """
196203 Returns a pair of fss based on repo the path falls in, using prefix.
197204 """
@@ -202,27 +209,33 @@ def _get_fs_pair(self, path) -> Tuple[FileSystem, Optional[DvcFileSystem]]:
202209 repo = self ._get_repo (path ) or self ._main_repo
203210
204211 dvc_fs = self ._dvcfss .get (repo .root_dir )
205- return repo .fs , dvc_fs
212+
213+ if path .startswith (repo .root_dir ):
214+ dvc_path = path [len (repo .root_dir ) + 1 :]
215+ else :
216+ dvc_path = path
217+
218+ return repo .fs , dvc_fs , dvc_path
206219
207220 def open (
208221 self , path , mode = "r" , encoding = "utf-8" , ** kwargs
209222 ): # pylint: disable=arguments-renamed
210223 if "b" in mode :
211224 encoding = None
212225
213- fs , dvc_fs = self ._get_fs_pair (path )
226+ fs , dvc_fs , dvc_path = self ._get_fs_pair (path )
214227 try :
215228 return fs .open (path , mode = mode , encoding = encoding )
216229 except FileNotFoundError :
217230 if not dvc_fs :
218231 raise
219232
220- return dvc_fs .open (path , mode = mode , encoding = encoding , ** kwargs )
233+ return dvc_fs .open (dvc_path , mode = mode , encoding = encoding , ** kwargs )
221234
222235 def exists (self , path ) -> bool :
223236 path = os .path .abspath (path )
224237
225- fs , dvc_fs = self ._get_fs_pair (path )
238+ fs , dvc_fs , dvc_path = self ._get_fs_pair (path )
226239
227240 if not dvc_fs :
228241 return fs .exists (path )
@@ -233,7 +246,7 @@ def exists(self, path) -> bool:
233246 if fs .exists (path ):
234247 return True
235248
236- if not dvc_fs .exists (path ):
249+ if not dvc_fs .exists (dvc_path ):
237250 return False
238251
239252 for p in self .path .parents (path ):
@@ -248,7 +261,7 @@ def exists(self, path) -> bool:
248261 def isdir (self , path ): # pylint: disable=arguments-renamed
249262 path = os .path .abspath (path )
250263
251- fs , dvc_fs = self ._get_fs_pair (path )
264+ fs , dvc_fs , dvc_path = self ._get_fs_pair (path )
252265
253266 if dvc_fs and dvc_fs .repo .dvcignore .is_ignored_dir (path ):
254267 return False
@@ -264,7 +277,7 @@ def isdir(self, path): # pylint: disable=arguments-renamed
264277 return False
265278
266279 try :
267- info = dvc_fs .info (path )
280+ info = dvc_fs .info (dvc_path )
268281 except FileNotFoundError :
269282 return False
270283
@@ -278,13 +291,13 @@ def isdir(self, path): # pylint: disable=arguments-renamed
278291 return info ["type" ] == "directory"
279292
280293 def isdvc (self , path , ** kwargs ):
281- _ , dvc_fs = self ._get_fs_pair (path )
282- return dvc_fs is not None and dvc_fs .isdvc (path , ** kwargs )
294+ _ , dvc_fs , dvc_path = self ._get_fs_pair (path )
295+ return dvc_fs is not None and dvc_fs .isdvc (dvc_path , ** kwargs )
283296
284297 def isfile (self , path ): # pylint: disable=arguments-renamed
285298 path = os .path .abspath (path )
286299
287- fs , dvc_fs = self ._get_fs_pair (path )
300+ fs , dvc_fs , dvc_path = self ._get_fs_pair (path )
288301
289302 if dvc_fs and dvc_fs .repo .dvcignore .is_ignored_file (path ):
290303 return False
@@ -300,7 +313,7 @@ def isfile(self, path): # pylint: disable=arguments-renamed
300313 return False
301314
302315 try :
303- info = dvc_fs .info (path )
316+ info = dvc_fs .info (dvc_path )
304317 except FileNotFoundError :
305318 return False
306319
@@ -328,10 +341,10 @@ def _subrepo_walk(self, dir_path, **kwargs):
328341 NOTE: subrepo will only be discovered when walking if
329342 ignore_subrepos is set to False.
330343 """
331- fs , dvc_fs = self ._get_fs_pair (dir_path )
344+ fs , dvc_fs , dvc_path = self ._get_fs_pair (dir_path )
332345 fs_walk = fs .walk (dir_path , topdown = True )
333346 if dvc_fs :
334- dvc_walk = dvc_fs . walk ( dir_path , topdown = True , ** kwargs )
347+ dvc_walk = _wrap_walk ( dvc_fs , dvc_path , topdown = True , ** kwargs )
335348 else :
336349 dvc_walk = None
337350 yield from self ._walk (fs_walk , dvc_walk , ** kwargs )
@@ -420,26 +433,26 @@ def walk(self, top, topdown=True, onerror=None, **kwargs):
420433 repo = self ._get_repo (os .path .abspath (top ))
421434 dvcfiles = kwargs .pop ("dvcfiles" , False )
422435
423- fs , dvc_fs = self ._get_fs_pair (top )
436+ fs , dvc_fs , dvc_path = self ._get_fs_pair (top )
424437 repo_exists = fs .exists (top )
425438
426439 repo_walk = repo .dvcignore .walk (
427440 fs , top , topdown = topdown , onerror = onerror , ** kwargs
428441 )
429442
430- if not dvc_fs or (repo_exists and dvc_fs .isdvc (top )):
443+ if not dvc_fs or (repo_exists and dvc_fs .isdvc (dvc_path )):
431444 yield from self ._walk (repo_walk , None , dvcfiles = dvcfiles )
432445 return
433446
434447 if not repo_exists :
435- yield from dvc_fs . walk (
436- top , topdown = topdown , onerror = onerror , ** kwargs
448+ yield from _wrap_walk (
449+ dvc_fs , dvc_path , topdown = topdown , onerror = onerror , ** kwargs
437450 )
438451
439452 dvc_walk = None
440- if dvc_fs .exists (top ):
441- dvc_walk = dvc_fs . walk (
442- top , topdown = topdown , onerror = onerror , ** kwargs
453+ if dvc_fs .exists (dvc_path ):
454+ dvc_walk = _wrap_walk (
455+ dvc_fs , dvc_path , topdown = topdown , onerror = onerror , ** kwargs
443456 )
444457
445458 yield from self ._walk (repo_walk , dvc_walk , dvcfiles = dvcfiles )
@@ -452,7 +465,7 @@ def find(self, path, prefix=None):
452465 def get_file (
453466 self , from_info , to_file , callback = DEFAULT_CALLBACK , ** kwargs
454467 ):
455- fs , dvc_fs = self ._get_fs_pair (from_info )
468+ fs , dvc_fs , dvc_path = self ._get_fs_pair (from_info )
456469 try :
457470 fs .get_file ( # pylint: disable=protected-access
458471 from_info , to_file , callback = callback , ** kwargs
@@ -463,14 +476,14 @@ def get_file(
463476 raise
464477
465478 dvc_fs .get_file ( # pylint: disable=protected-access
466- from_info , to_file , callback = callback , ** kwargs
479+ dvc_path , to_file , callback = callback , ** kwargs
467480 )
468481
469482 def info (self , path ):
470- fs , dvc_fs = self ._get_fs_pair (path )
483+ fs , dvc_fs , dvc_path = self ._get_fs_pair (path )
471484
472485 try :
473- dvc_info = dvc_fs .info (path )
486+ dvc_info = dvc_fs .info (dvc_path )
474487 except FileNotFoundError :
475488 dvc_info = None
476489
@@ -503,9 +516,9 @@ def info(self, path):
503516 return dvc_info
504517
505518 def checksum (self , path ):
506- fs , dvc_fs = self ._get_fs_pair (path )
519+ fs , dvc_fs , dvc_path = self ._get_fs_pair (path )
507520
508521 try :
509522 return fs .checksum (path )
510523 except FileNotFoundError :
511- return dvc_fs .checksum (path )
524+ return dvc_fs .checksum (dvc_path )
0 commit comments