From fb300e1f9fd6b4a9d4798f9333181bd628391292 Mon Sep 17 00:00:00 2001 From: Chris Kerr Date: Tue, 31 Dec 2019 11:58:52 +0000 Subject: [PATCH] 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 --- dvc/external_repo.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/dvc/external_repo.py b/dvc/external_repo.py index cf5ecacdd..9ff2f2a41 100644 --- a/dvc/external_repo.py +++ b/dvc/external_repo.py @@ -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