99 OutputNotFoundError ,
1010 PathMissingError ,
1111)
12- from dvc .external_repo import cached_clone
12+ from dvc .external_repo import external_repo , cached_clone
1313from dvc .path_info import PathInfo
1414from dvc .stage import Stage
1515from dvc .utils import resolve_output
@@ -29,8 +29,6 @@ def __init__(self):
2929
3030@staticmethod
3131def get (url , path , out = None , rev = None ):
32- from dvc .repo import Repo
33-
3432 out = resolve_output (path , out )
3533
3634 if Stage .is_valid_filename (out ):
@@ -43,41 +41,50 @@ def get(url, path, out=None, rev=None):
4341 # and won't work with reflink/hardlink.
4442 dpath = os .path .dirname (os .path .abspath (out ))
4543 tmp_dir = os .path .join (dpath , "." + str (shortuuid .uuid ()))
44+ raw_git_dir = None
4645 try :
47- cached_clone (url , rev = rev , clone_path = tmp_dir )
4846 try :
49- repo = Repo (tmp_dir )
50-
51- # Try any links possible to avoid data duplication.
52- #
53- # Not using symlink, because we need to remove cache after we are
54- # done, and to make that work we would have to copy data over
55- # anyway before removing the cache, so we might just copy it
56- # right away.
57- #
58- # Also, we can't use theoretical "move" link type here, because
59- # the same cache file might be used a few times in a directory.
60- repo .cache .local .cache_types = ["reflink" , "hardlink" , "copy" ]
61-
62- output = repo .find_out_by_relpath (path )
63- if output .use_cache :
64- _get_cached (repo , output , out )
47+ with external_repo (cache_dir = tmp_dir , url = url , rev = rev ) as repo :
48+ # Try any links possible to avoid data duplication.
49+ #
50+ # Not using symlink, because we need to remove cache after we
51+ # are done, and to make that work we would have to copy data
52+ # over anyway before removing the cache, so we might just copy
53+ # it right away.
54+ #
55+ # Also, we can't use theoretical "move" link type here, because
56+ # the same cache file might be used a few times in a directory.
57+ repo .cache .local .cache_types = ["reflink" , "hardlink" , "copy" ]
58+
59+ try :
60+ output = repo .find_out_by_relpath (path )
61+ except OutputNotFoundError :
62+ output = None
63+
64+ if output and output .use_cache :
65+ _get_cached (repo , output , out )
66+ return
67+
68+ # Either an uncached out with absolute path or a user error
69+
70+ if os .path .isabs (path ):
71+ raise FileNotFoundError
72+
73+ fs_copy (os .path .join (repo .root_dir , path ), out )
6574 return
6675
67- except (NotDvcRepoError , OutputNotFoundError ):
76+ except NotDvcRepoError :
77+ # Not a DVC repository, continue below and copy from git
6878 pass
6979
70- # It's an uncached out with absolute path, a non-DVC repo, or a
71- # user error
72- if os .path .isabs (path ):
73- raise FileNotFoundError
74-
75- fs_copy (os .path .join (tmp_dir , path ), out )
76-
80+ raw_git_dir = cached_clone (url , rev = rev )
81+ fs_copy (os .path .join (raw_git_dir , path ), out )
7782 except (OutputNotFoundError , FileNotFoundError ):
7883 raise PathMissingError (path , url )
7984 finally :
8085 remove (tmp_dir )
86+ if raw_git_dir :
87+ remove (raw_git_dir )
8188
8289
8390def _get_cached (repo , output , out ):
0 commit comments