diff --git a/dvc/external_repo.py b/dvc/external_repo.py index a598eb6692..f5117332b6 100644 --- a/dvc/external_repo.py +++ b/dvc/external_repo.py @@ -248,7 +248,7 @@ def _clone_default_branch(url, rev): else: logger.debug("erepo: git clone %s to a temporary dir", url) clone_path = tempfile.mkdtemp("dvc-clone") - git = Git.clone(url, clone_path) + git = Git.clone(url, clone_path, rev=rev) CLONES[url] = clone_path finally: if git: diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py index e70d6f7b0f..2c60f49207 100644 --- a/dvc/scm/git/__init__.py +++ b/dvc/scm/git/__init__.py @@ -1,5 +1,6 @@ """Manages Git.""" +from functools import partial import logging import os import yaml @@ -71,16 +72,31 @@ def clone(url, to_path, rev=None): # [1] https://github.com/gitpython-developers/GitPython/issues/924 env[ld_key] = "" + clone_from = partial( + git.Repo.clone_from, + url, + to_path, + env=env, # needed before we can fix it in __init__ + no_single_branch=True, + ) + try: - tmp_repo = git.Repo.clone_from( - url, - to_path, - env=env, # needed before we can fix it in __init__ - no_single_branch=True, - ) - tmp_repo.close() + if rev: + logger.debug("attempting shallow clone of branch %s", rev) + tmp_repo = clone_from(branch=rev, depth=1) + else: + logger.debug("full clone") + tmp_repo = clone_from() except git.exc.GitCommandError as exc: - raise CloneError(url, to_path) from exc + if not rev or ("Remote branch %s not found" % rev) not in str(exc): + raise CloneError(url, to_path) from exc + try: + logger.debug("not a branch - performing full clone") + tmp_repo = clone_from() + except git.exc.GitCommandError as exc: + raise CloneError(url, to_path) from exc + + tmp_repo.close() # NOTE: using our wrapper to make sure that env is fixed in __init__ repo = Git(to_path)