Skip to content

Commit 3ceb1db

Browse files
committed
import: Allow importing from non-DVC git repos
Fixes #2977 Refactor `_copy_if_git_file` to use `cached_clone` directly, so that copying still works even if the source repo does not have a '.dvc' subdirectory.
1 parent fb300e1 commit 3ceb1db

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

dvc/dependency/repo.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
from funcy import merge
77

88
from .local import DependencyLOCAL
9+
from dvc.external_repo import cached_clone
910
from dvc.external_repo import external_repo
11+
from dvc.exceptions import NotDvcRepoError
1012
from dvc.exceptions import OutputNotFoundError
1113
from dvc.exceptions import PathMissingError
1214
from dvc.utils.fs import fs_copy
@@ -75,27 +77,35 @@ def fetch(self):
7577
return out
7678

7779
@staticmethod
78-
def _is_git_file(repo, path):
79-
if not os.path.isabs(path):
80-
try:
81-
output = repo.find_out_by_relpath(path)
82-
if not output.use_cache:
83-
return True
84-
except OutputNotFoundError:
85-
return True
86-
return False
80+
def _is_git_file(repo_dir, path):
81+
from dvc.repo import Repo
82+
83+
if os.path.isabs(path):
84+
return False
85+
86+
try:
87+
repo = Repo(repo_dir)
88+
except NotDvcRepoError:
89+
return True
90+
91+
try:
92+
output = repo.find_out_by_relpath(path)
93+
return not output.use_cache
94+
except OutputNotFoundError:
95+
return True
96+
finally:
97+
repo.close()
8798

8899
def _copy_if_git_file(self, to_path):
89100
src_path = self.def_path
90-
with self._make_repo(
91-
cache_dir=self.repo.cache.local.cache_dir
92-
) as repo:
93-
if not self._is_git_file(repo, src_path):
94-
return False
101+
repo_dir = cached_clone(**self.def_repo)
102+
103+
if not self._is_git_file(repo_dir, src_path):
104+
return False
95105

96-
src_full_path = os.path.join(repo.root_dir, src_path)
97-
dst_full_path = os.path.abspath(to_path)
98-
fs_copy(src_full_path, dst_full_path)
106+
src_full_path = os.path.join(repo_dir, src_path)
107+
dst_full_path = os.path.abspath(to_path)
108+
fs_copy(src_full_path, dst_full_path)
99109
return True
100110

101111
def download(self, to):

0 commit comments

Comments
 (0)