Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use cache dir as a default remote when importing from local repo #2915

Merged
merged 15 commits into from
Dec 11, 2019
18 changes: 16 additions & 2 deletions dvc/external_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import tempfile
from contextlib import contextmanager
from distutils.dir_util import copy_tree

maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
from funcy import retry

from dvc.config import NoRemoteError
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
from dvc.exceptions import NoRemoteInExternalRepoError
from dvc.remote import RemoteConfig
from dvc.config import NoRemoteError
from dvc.exceptions import NoOutputInExternalRepoError
from dvc.exceptions import OutputNotFoundError
from dvc.utils.fs import remove
Expand Down Expand Up @@ -69,6 +69,20 @@ def _external_repo(url=None, rev=None, cache_dir=None):

repo = Repo(new_path)
try:
# check if the URL is local and no default remote is present
# add default remote pointing to the original repo's cache location
if os.path.isdir(url):
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
rconfig = RemoteConfig(repo.config)
if not rconfig.remote_config_exists():
original_repo = Repo(url)
rconfig.add(
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
"upstream",
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
original_repo.cache.local.cache_dir,
default=True,
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
level=Config.LEVEL_LOCAL,
)
original_repo.close()

if cache_dir is not None:
cache_config = CacheConfig(repo.config)
cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL)
Expand Down
15 changes: 15 additions & 0 deletions dvc/remote/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,18 @@ def get_default(self, level=None):
return self.config.get(
Config.SECTION_CORE, Config.SECTION_CORE_REMOTE, level=level
)

def remote_config_exists(self):
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
"""
Checks if default remote config is present.
Args:
rconfig: a remote config

Returns:
True if the remote config exists, else False
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
"""
try:
default = self.get_default()
except ConfigError:
default = None
return bool(default)
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 0 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from git import Repo
from git.exc import GitCommandNotFound

from dvc.remote.config import RemoteConfig
from dvc.remote.ssh.connection import SSHConnection
from dvc.repo import Repo as DvcRepo
from dvc.utils.compat import cast_bytes_py2
Expand Down Expand Up @@ -148,11 +147,6 @@ def erepo(repo_dir):
repo.dvc.scm.add([stage_foo.path, stage_bar.path, stage_data_dir.path])
repo.dvc.scm.commit("init repo")

rconfig = RemoteConfig(repo.dvc.config)
rconfig.add("upstream", repo.dvc.cache.local.cache_dir, default=True)
repo.dvc.scm.add([repo.dvc.config.config_file])
repo.dvc.scm.commit("add remote")

maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
repo.create("version", "master")
repo.dvc.add("version")
repo.dvc.scm.add([".gitignore", "version.dvc"])
Expand Down
12 changes: 12 additions & 0 deletions tests/func/test_external_repo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import filecmp
import os

from mock import patch

from dvc.repo import Repo
from dvc.external_repo import external_repo
from dvc.scm.git import Git
from dvc.utils.fs import path_isin
Expand All @@ -26,3 +28,13 @@ def test_external_repo(erepo):
assert path_isin(repo.cache.local.cache_dir, repo.root_dir)

assert mock.call_count == 1


def test_external_repo_import_without_remote(erepo, dvc_repo):
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
src = erepo.FOO
dst = os.path.join(dvc_repo.root_dir, "foo")

Repo.get(erepo.root_dir, src, dst)
assert os.path.exists(dst)
assert os.path.isfile(dst)
assert filecmp.cmp(erepo.FOO, dst, shallow=False)
1 change: 0 additions & 1 deletion tests/func/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ def test_get_from_non_dvc_master(erepo, tmp_path, monkeypatch, caplog):
with caplog.at_level(logging.INFO, logger="dvc"):
Repo.get(erepo._root_dir, erepo.FOO, out=imported_file, rev="branch")

assert caplog.text == ""
maykulkarni marked this conversation as resolved.
Show resolved Hide resolved
assert filecmp.cmp(
os.path.join(erepo._root_dir, erepo.FOO), imported_file, shallow=False
)