Skip to content

Commit fb300e1

Browse files
committed
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
1 parent c393e8d commit fb300e1

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

dvc/external_repo.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ def external_repo(url=None, rev=None, rev_lock=None, cache_dir=None):
3333
repo.close()
3434

3535

36-
def _external_repo(url=None, rev=None, cache_dir=None):
37-
from dvc.config import Config
38-
from dvc.cache import CacheConfig
39-
from dvc.repo import Repo
36+
def cached_clone(url, rev=None, **_ignored_kwargs):
37+
"""Clone an external git repo to a temporary directory.
4038
41-
key = (url, rev, cache_dir)
42-
if key in REPO_CACHE:
43-
return REPO_CACHE[key]
39+
Returns the path to a local temporary directory with the specified
40+
revision checked out.
41+
42+
Uses the REPO_CACHE to avoid accessing the remote server again if
43+
cloning from the same URL twice in the same session.
44+
45+
"""
4446

4547
new_path = tempfile.mkdtemp("dvc-erepo")
4648

47-
# Copy and adjust existing clone
49+
# Copy and adjust existing clean clone
4850
if (url, None, None) in REPO_CACHE:
4951
old_path = REPO_CACHE[url, None, None]
5052

@@ -59,13 +61,24 @@ def _external_repo(url=None, rev=None, cache_dir=None):
5961
copy_tree(new_path, clean_clone_path)
6062
REPO_CACHE[url, None, None] = clean_clone_path
6163

62-
# Adjust new clone/copy to fit rev and cache_dir
63-
64-
# Checkout needs to be done first because current branch might not be
65-
# DVC repository
64+
# Check out the specified revision
6665
if rev is not None:
6766
_git_checkout(new_path, rev)
6867

68+
return new_path
69+
70+
71+
def _external_repo(url=None, rev=None, cache_dir=None):
72+
from dvc.config import Config
73+
from dvc.cache import CacheConfig
74+
from dvc.repo import Repo
75+
76+
key = (url, rev, cache_dir)
77+
if key in REPO_CACHE:
78+
return REPO_CACHE[key]
79+
80+
new_path = cached_clone(url, rev=rev)
81+
6982
repo = Repo(new_path)
7083
try:
7184
# check if the URL is local and no default remote is present

0 commit comments

Comments
 (0)