Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shallow copy: 1.18.1 #5571

Merged
merged 4 commits into from Aug 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 19 additions & 12 deletions conans/client/tools/scm.py
Expand Up @@ -145,23 +145,30 @@ def clone(self, url, branch=None, args="", shallow=False):
return output

def checkout(self, element, submodule=None):
# Element can be a tag, branch or commit
self.check_repo()
output = self.run('checkout "%s"' % element)
output += self.checkout_submodules(submodule)

if submodule:
if submodule == "shallow":
output += self.run("submodule sync")
output += self.run("submodule update --init")
elif submodule == "recursive":
output += self.run("submodule sync --recursive")
output += self.run("submodule update --init --recursive")
else:
raise ConanException("Invalid 'submodule' attribute value in the 'scm'. "
"Unknown value '%s'. Allowed values: ['shallow', 'recursive']"
% submodule)
# Element can be a tag, branch or commit
return output

def checkout_submodules(self, submodule=None):
"""Do the checkout only for submodules"""
if not submodule:
return ""
if submodule == "shallow":
output = self.run("submodule sync")
output += self.run("submodule update --init")
return output
elif submodule == "recursive":
output = self.run("submodule sync --recursive")
output += self.run("submodule update --init --recursive")
return output
else:
raise ConanException("Invalid 'submodule' attribute value in the 'scm'. "
"Unknown value '%s'. Allowed values: ['shallow', 'recursive']"
% submodule)

def excluded_files(self):
ret = []
try:
Expand Down
9 changes: 6 additions & 3 deletions conans/model/scm.py
Expand Up @@ -89,14 +89,17 @@ def checkout(self):
output = ""
if self._data.type == "git":
try:
output += self.repo.clone(url=self._data.url, branch=self._data.revision, shallow=True)
output += self.repo.clone(url=self._data.url, branch=self._data.revision,
shallow=True)
except subprocess.CalledProcessError:
# remove the .git directory, otherwise, fallback clone cannot be successful
# it's completely safe to do here, as clone without branch expects empty directory
rmdir(os.path.join(self.repo_folder, ".git"))
output += self.repo.clone(url=self._data.url, shallow=False)
output += self.repo.checkout(element=self._data.revision,
submodule=self._data.submodule)
output += self.repo.checkout(element=self._data.revision,
submodule=self._data.submodule)
else:
output += self.repo.checkout_submodules(submodule=self._data.submodule)
else:
output += self.repo.checkout(url=self._data.url, revision=self._data.revision)
return output
Expand Down
33 changes: 33 additions & 0 deletions conans/test/functional/scm/scm_test.py
Expand Up @@ -4,6 +4,7 @@
from collections import namedtuple

from nose.plugins.attrib import attr
from parameterized.parameterized import parameterized

from conans.client.tools.scm import Git, SVN
from conans.client.tools.win import get_cased_path
Expand Down Expand Up @@ -290,6 +291,38 @@ def source(self):
self.assertTrue(os.path.exists(os.path.join(curdir, "source", "mysub", "myfile.txt")))
self.assertIn("SOURCE METHOD CALLED", self.client.out)

@parameterized.expand([
("local", "v1.0", True),
("local", None, False),
("https://github.com/conan-io/conan.git", "0.22.1", True),
("https://github.com/conan-io/conan.git", "c6cc15fa2f4b576bd70c9df11942e61e5cc7d746", False)])
def test_shallow_clone_remote(self, remote, revision, is_tag):
# https://github.com/conan-io/conan/issues/5570
self.client = TestClient()

# "remote" git
if remote == "local":
remote, rev = create_local_git_repo(tags=[revision] if is_tag else None,
files={"conans/model/username.py": "foo"})
if revision is None: # Get the generated commit
revision = rev

# Use explicit URL to avoid local optimization (scm_folder.txt)
conanfile = '''
import os
from conans import ConanFile, tools

class ConanLib(ConanFile):
name = "lib"
version = "0.1"
scm = {"type": "git", "url": "%s", "revision": "%s"}

def build(self):
assert os.path.exists(os.path.join(self.build_folder, "conans", "model", "username.py"))
''' % (remote, revision)
self.client.save({"conanfile.py": conanfile})
self.client.run("create . user/channel")

def test_install_checked_out(self):
test_server = TestServer()
self.servers = {"myremote": test_server}
Expand Down