Skip to content

Commit

Permalink
- shallow git clone
Browse files Browse the repository at this point in the history
Signed-off-by: SSE4 <tomskside@gmail.com>
  • Loading branch information
SSE4 committed Jul 19, 2019
1 parent f86e035 commit dc1c646
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
15 changes: 15 additions & 0 deletions conans/client/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@
# cacert_path # environment CONAN_CACERT_PATH
[scm]
# shallow_clone = False # environment CONAN_SHALLOW_CLONE
[storage]
# This is the default path, but you can write your own. It must be an absolute path or a
# path beginning with "~" (if the environment var CONAN_USER_HOME is specified, this directory, even
Expand Down Expand Up @@ -249,6 +253,7 @@ def env_vars(self):
"CONAN_CACERT_PATH": self._env_c("general.cacert_path", "CONAN_CACERT_PATH", None),
"CONAN_DEFAULT_PACKAGE_ID_MODE": self._env_c("general.default_package_id_mode",
"CONAN_DEFAULT_PACKAGE_ID_MODE", None),
"CONAN_SHALLOW_CLONE": self._env_c("scm.shallow_clone", "CONAN_SHALLOW_CLONE", "False"),
}

# Filter None values
Expand Down Expand Up @@ -574,3 +579,13 @@ def log_run_to_output(self):
return log_run_to_output.lower() in ("1", "true")
except ConanException:
return True

@property
def shallow_clone(self):
try:
shallow_clone = get_env("CONAN_SHALLOW_CLONE")
if shallow_clone is None:
shallow_clone = self.get_item("scm.shallow_clone")
return shallow_clone.lower() in ("1", "true")
except ConanException:
return False
30 changes: 25 additions & 5 deletions conans/client/tools/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from conans.client.tools.files import chdir
from conans.errors import ConanException
from conans.model.version import Version
from conans.util.env_reader import get_env
from conans.util.files import decode_text, to_file_bytes, walk


Expand Down Expand Up @@ -89,7 +90,9 @@ def run(self, command):
command = self._configure_ssl_verify + command
return super(Git, self).run(command)

def clone(self, url, branch=None, args=""):
def clone(self, url, branch=None, args="", shallow=False):

shallow = get_env("CONAN_SHALLOW_CLONE", shallow)
url = self.get_url_with_credentials(url)
if os.path.exists(url):
url = url.replace("\\", "/") # Windows local directory
Expand All @@ -101,17 +104,34 @@ def clone(self, url, branch=None, args=""):
"attribute in the 'scm'" % self.folder)
output = self.run("init")
output += self.run('remote add origin "%s"' % url)
output += self.run("fetch ")
if shallow:
try:
output += self.run('fetch --depth 1 origin "%s"' % branch)
except CalledProcessError:
output += self.run("fetch")
else:
output += self.run("fetch")
output += self.run("checkout -t origin/%s" % branch)
else:
branch_cmd = "--branch %s" % branch if branch else ""
output = self.run('clone "%s" . %s %s' % (url, branch_cmd, args))
shallow_cmd = "--depth 1" if shallow else ""
output = self.run('clone "%s" . %s %s %s' % (url, branch_cmd, shallow_cmd, args))

return output

def checkout(self, element, submodule=None):
def checkout(self, element, submodule=None, shallow=False):

shallow = get_env("CONAN_SHALLOW_CLONE", shallow)
self.check_repo()
output = self.run('checkout "%s"' % element)

output = ""
if shallow:
try:
output += self.run('fetch --depth 1 origin "%s"' % element)
except CalledProcessError:
# fall back to the full clone in case server doesn't support allowReachableSHA1InWant
output += self.run('fetch --unshallow origin')
output += self.run('checkout "%s"' % element)

if submodule:
if submodule == "shallow":
Expand Down
11 changes: 11 additions & 0 deletions conans/test/unittests/client/tools/scm/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import six
import unittest
import subprocess

from nose.plugins.attrib import attr

Expand Down Expand Up @@ -74,6 +75,16 @@ def test_clone_git(self):
git.clone(path)
self.assertTrue(os.path.exists(os.path.join(tmp, "myfile")))

def test_clone_git_shallow(self):
path, _ = create_local_git_repo({"myfile": "contents"}, commits=2)
tmp = temp_folder()
git = Git(tmp)
git.clone("file://" + path, shallow=True) # --depth is ignored in local clones
with self.assertRaises(subprocess.CalledProcessError):
git.checkout(element="HEAD~1", shallow=False)
git.checkout(element="HEAD~1", shallow=True)
self.assertTrue(os.path.exists(os.path.join(tmp, "myfile")))

def test_clone_existing_folder_git(self):
path, commit = create_local_git_repo({"myfile": "contents"}, branch="my_release")

Expand Down
5 changes: 3 additions & 2 deletions conans/test/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def __contains__(self, value):
return value in self.__repr__()


def create_local_git_repo(files=None, branch=None, submodules=None, folder=None):
def create_local_git_repo(files=None, branch=None, submodules=None, folder=None, commits=1):
tmp = folder or temp_folder()
tmp = get_cased_path(tmp)
if files:
Expand All @@ -509,7 +509,8 @@ def create_local_git_repo(files=None, branch=None, submodules=None, folder=None)
git.run("checkout -b %s" % branch)

git.run("add .")
git.run('commit -m "commiting"')
for i in range(0, commits):
git.run('commit --allow-empty -m "commiting"')

if submodules:
for submodule in submodules:
Expand Down

0 comments on commit dc1c646

Please sign in to comment.