Refactor cached_clone function out of _external_repo

Since this function does not create a `Repo` class, it can be called
on a git repo with no '.dvc' directory
This commit is contained in:
Chris Kerr
2019-12-31 13:13:29 +00:00
parent c393e8dcbe
commit fb300e1f9f
+25 -12
View File
@@ -33,18 +33,20 @@ def external_repo(url=None, rev=None, rev_lock=None, cache_dir=None):
repo.close()
def _external_repo(url=None, rev=None, cache_dir=None):
from dvc.config import Config
from dvc.cache import CacheConfig
from dvc.repo import Repo
def cached_clone(url, rev=None, **_ignored_kwargs):
"""Clone an external git repo to a temporary directory.
key = (url, rev, cache_dir)
if key in REPO_CACHE:
return REPO_CACHE[key]
Returns the path to a local temporary directory with the specified
revision checked out.
Uses the REPO_CACHE to avoid accessing the remote server again if
cloning from the same URL twice in the same session.
"""
new_path = tempfile.mkdtemp("dvc-erepo")
# Copy and adjust existing clone
# Copy and adjust existing clean clone
if (url, None, None) in REPO_CACHE:
old_path = REPO_CACHE[url, None, None]
@@ -59,13 +61,24 @@ def _external_repo(url=None, rev=None, cache_dir=None):
copy_tree(new_path, clean_clone_path)
REPO_CACHE[url, None, None] = clean_clone_path
# Adjust new clone/copy to fit rev and cache_dir
# Checkout needs to be done first because current branch might not be
# DVC repository
# Check out the specified revision
if rev is not None:
_git_checkout(new_path, rev)
return new_path
def _external_repo(url=None, rev=None, cache_dir=None):
from dvc.config import Config
from dvc.cache import CacheConfig
from dvc.repo import Repo
key = (url, rev, cache_dir)
if key in REPO_CACHE:
return REPO_CACHE[key]
new_path = cached_clone(url, rev=rev)
repo = Repo(new_path)
try:
# check if the URL is local and no default remote is present