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

Shallow clone from git. Resolve #2432. #3739

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions pip/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pip._vendor.six.moves.urllib import parse as urllib_parse
from pip._vendor.six.moves.urllib import request as urllib_request

from pip.exceptions import InstallationError
from pip.utils import display_path, rmtree
from pip.vcs import vcs, VersionControl

Expand Down Expand Up @@ -115,13 +116,21 @@ def obtain(self, dest):
rev_options = [rev]
rev_display = ' (to %s)' % rev
else:
rev = 'master'
rev_options = ['origin/master']
rev_display = ''
if self.check_destination(dest, url, rev_options, rev_display):
logger.info(
'Cloning %s%s to %s', url, rev_display, display_path(dest),
)
self.run_command(['clone', '-q', url, dest])
try:
self.run_command([
'clone', '-q', '--branch', rev, '--depth', '1', url, dest])
except InstallationError as e:
if '128' in str(e): # commit sha's can't be shallow cloned
self.run_command(['clone', '-q', url, dest])
else:
raise

if rev:
rev_options = self.check_rev_options(rev, dest, rev_options)
Expand Down Expand Up @@ -253,10 +262,15 @@ def get_url_rev(self):
def update_submodules(self, location):
if not os.path.exists(os.path.join(location, '.gitmodules')):
return
self.run_command(
['submodule', 'update', '--init', '--recursive', '-q'],
cwd=location,
)
cmd = ['submodule', 'update', '--init', '--recursive', '-q']
try:
self.run_command(cmd + ['--depth', '1'], cwd=location)
except InstallationError as e:
if '128' in str(e): # commit sha's can't be shallow cloned
self.run_command(cmd, cwd=location)
else:
raise


@classmethod
def controls_location(cls, location):
Expand Down
8 changes: 5 additions & 3 deletions tests/functional/test_install_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def test_install_noneditable_git(script, tmpdir):
result = script.pip(
'install',
'git+https://github.com/pypa/pip-test-package.git'
'@0.1.1#egg=pip-test-package'
'@0.1.1#egg=pip-test-package',
expect_stderr=True
)
egg_info_folder = (
script.site_packages /
Expand Down Expand Up @@ -97,7 +98,8 @@ def test_git_with_tag_name_as_revision(script):
_change_test_package_version(script, version_pkg_path)
script.pip(
'install', '-e', '%s@test_tag#egg=version_pkg' %
('git+file://' + version_pkg_path.abspath.replace('\\', '/'))
('git+file://' + version_pkg_path.abspath.replace('\\', '/')),
expect_stderr=True
)
version = script.run('version_pkg')
assert '0.1' in version.stdout
Expand Down Expand Up @@ -211,7 +213,7 @@ def test_git_with_ambiguous_revs(script):
(version_pkg_path.abspath.replace('\\', '/'))
)
script.run('git', 'tag', '0.1', cwd=version_pkg_path)
result = script.pip('install', '-e', package_url)
result = script.pip('install', '-e', package_url, expect_stderr=True)
assert 'Could not find a tag or branch' not in result.stdout
# it is 'version-pkg' instead of 'version_pkg' because
# egg-link name is version-pkg.egg-link because it is a single .py module
Expand Down
23 changes: 22 additions & 1 deletion tests/functional/test_install_vcs_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def test_check_submodule_addition(script):
module_path, submodule_path = _create_test_package_with_submodule(script)

install_result = script.pip(
'install', '-e', 'git+' + module_path + '#egg=version_pkg'
'install', '-e', 'git+' + module_path + '#egg=version_pkg',
expect_stderr=True
)
assert (
script.venv / 'src/version-pkg/testpkg/static/testfile'
Expand All @@ -133,3 +134,23 @@ def test_check_submodule_addition(script):
script.venv / 'src/version-pkg/testpkg/static/testfile2'
in update_result.files_created
)


@pytest.mark.network
def test_shallow_clone(script):
"""
Installing from a tag rev should be shallow.
"""
src = script.venv_path + '/src/' + 'pip-test-package'

script.pip(
'install', '-e', 'git+https://github.com/pypa/pip-test-package.git' +
'@0.1.1#egg=pip-test-package', expect_stderr=True
)

tag = script.run('git', 'describe', '--tags', cwd=src).stdout.strip('\n')
assert tag == '0.1.1'

depth = script.run(
'git', 'rev-list', 'HEAD', '--count', cwd=src).stdout.strip('\n')
assert depth == '1'