erepo/exp: use explicit fetch + merge instead of pull

- tests: use mocker.spy instead of patch(wraps=...)
This commit is contained in:
Peter Rowlands
2022-03-21 18:17:36 +09:00
committed by Peter Rowlands (변기호)
parent ab54b5bdfc
commit 19dcba9db6
3 changed files with 52 additions and 52 deletions
+20 -16
View File
@@ -201,12 +201,14 @@ def _clone_default_branch(url, rev, for_write=False):
# given repo URL it is easier/safer for us to work with
# full clones in this case.
logger.debug("erepo: unshallowing clone for '%s'", url)
_unshallow(git)
git.fetch(unshallow=True)
_merge_upstream(git)
shallow = False
CLONES[url] = (clone_path, shallow)
else:
logger.debug("erepo: git pull '%s'", url)
git.pull()
git.fetch()
_merge_upstream(git)
else:
from dvc.scm import clone
@@ -217,10 +219,13 @@ def _clone_default_branch(url, rev, for_write=False):
try:
git = clone(url, clone_path, shallow_branch=rev)
shallow = True
logger.debug(
"erepo: using shallow clone for branch '%s'", rev
shallow = os.path.exists(
os.path.join(clone_path, Git.GIT_DIR, "shallow")
)
if shallow:
logger.debug(
"erepo: using shallow clone for branch '%s'", rev
)
except CloneError:
pass
if not git:
@@ -234,17 +239,16 @@ def _clone_default_branch(url, rev, for_write=False):
return clone_path, shallow
def _unshallow(git):
if git.gitpython.repo.head.is_detached:
# If this is a detached head (i.e. we shallow cloned a tag) switch to
# the default branch
origin_refs = git.gitpython.repo.remotes["origin"].refs
ref = origin_refs["HEAD"].reference
branch_name = ref.name.split("/")[-1]
branch = git.gitpython.repo.create_head(branch_name, ref)
branch.set_tracking_branch(ref)
branch.checkout()
git.pull(unshallow=True)
def _merge_upstream(git):
from scmrepo.exceptions import SCMError
try:
branch = git.active_branch()
upstream = f"refs/remotes/origin/{branch}"
if git.get_ref(upstream):
git.merge(upstream)
except SCMError:
pass
def _git_checkout(repo_path, rev):
+1 -1
View File
@@ -49,7 +49,7 @@ def apply(repo, rev, force=True, **kwargs):
from scmrepo.exceptions import SCMError as _SCMError
try:
repo.scm.merge(exp_rev, commit=False)
repo.scm.merge(exp_rev, commit=False, squash=True)
except _SCMError as exc:
raise SCMError(str(exc))
+31 -35
View File
@@ -1,5 +1,5 @@
import os
from unittest.mock import ANY, patch
from unittest.mock import ANY
from scmrepo.git import Git
@@ -20,7 +20,7 @@ def test_external_repo(erepo_dir, mocker):
url = os.fspath(erepo_dir)
mock = mocker.patch.object(Git, "clone", wraps=Git.clone)
clone_spy = mocker.spy(Git, "clone")
with external_repo(url) as repo:
with repo.open_by_relpath("file") as fd:
@@ -30,7 +30,7 @@ def test_external_repo(erepo_dir, mocker):
with repo.open_by_relpath("file") as fd:
assert fd.read() == "branch"
assert mock.call_count == 1
assert clone_spy.call_count == 1
def test_source_change(erepo_dir):
@@ -128,35 +128,34 @@ def test_relative_remote(erepo_dir, tmp_dir):
assert fd.read() == "contents"
def test_shallow_clone_branch(erepo_dir):
def test_shallow_clone_branch(erepo_dir, mocker):
with erepo_dir.chdir():
with erepo_dir.branch("branch", new=True):
erepo_dir.dvc_gen("file", "branch", commit="create file on branch")
erepo_dir.dvc_gen("file", "master", commit="create file on master")
url = os.fspath(erepo_dir)
clone_spy = mocker.spy(Git, "clone")
with patch.object(Git, "clone", wraps=Git.clone) as mock_clone:
with external_repo(url, rev="branch") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "branch"
with external_repo(url, rev="branch") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "branch"
mock_clone.assert_called_with(
url, ANY, shallow_branch="branch", progress=ANY
)
_, shallow = CLONES[url]
assert shallow
clone_spy.assert_called_with(
url, ANY, shallow_branch="branch", progress=ANY
)
with external_repo(url) as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "master"
path, _ = CLONES[url]
CLONES[url] = (path, True)
assert mock_clone.call_count == 1
_, shallow = CLONES[url]
assert not shallow
mock_fetch = mocker.patch.object(Git, "fetch")
with external_repo(url) as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "master"
mock_fetch.assert_called_with(unshallow=True)
def test_shallow_clone_tag(erepo_dir):
def test_shallow_clone_tag(erepo_dir, mocker):
with erepo_dir.chdir():
erepo_dir.dvc_gen("file", "foo", commit="init")
erepo_dir.scm.tag("v1")
@@ -164,24 +163,21 @@ def test_shallow_clone_tag(erepo_dir):
url = os.fspath(erepo_dir)
with patch.object(Git, "clone", wraps=Git.clone) as mock_clone:
with external_repo(url, rev="v1") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "foo"
clone_spy = mocker.spy(Git, "clone")
with external_repo(url, rev="v1") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "foo"
mock_clone.assert_called_with(
url, ANY, shallow_branch="v1", progress=ANY
)
_, shallow = CLONES[url]
assert shallow
clone_spy.assert_called_with(url, ANY, shallow_branch="v1", progress=ANY)
with external_repo(url, rev="master") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "bar"
path, _ = CLONES[url]
CLONES[url] = (path, True)
assert mock_clone.call_count == 1
_, shallow = CLONES[url]
assert not shallow
mock_fetch = mocker.patch.object(Git, "fetch")
with external_repo(url, rev="master") as repo:
with repo.open_by_relpath("file") as fd:
assert fd.read() == "bar"
mock_fetch.assert_called_with(unshallow=True)
def test_subrepos_are_ignored(tmp_dir, erepo_dir):