Skip to content

Commit

Permalink
Use --depth=1 when checking out git repositories
Browse files Browse the repository at this point in the history
In most uses of pip's git backend, we don't need the full git history -
only the target head/branch/tag.  This change adds --depth=1 to git
fetch and clone commands.

NB: "Shallow" checkouts are not supported from arbitrary git commits, so
this change removes the ability to install from git SHA1 commit IDs.
Tags and branches continue to be supported.

Fixes pypa#2432
  • Loading branch information
anguslees committed Mar 30, 2015
1 parent c1c638b commit 6c18ad4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 48 deletions.
5 changes: 1 addition & 4 deletions docs/reference/pip_install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,10 @@ Here are the supported forms::
[-e] git+ssh://git.myproject.org/MyProject#egg=MyProject
-e [email protected]:MyProject#egg=MyProject

Passing branch names, a commit hash or a tag name is possible like so::
Passing branch names or a tag name is possible like so::

[-e] git://git.myproject.org/MyProject.git@master#egg=MyProject
[-e] git://git.myproject.org/[email protected]#egg=MyProject
[-e] git://git.myproject.org/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709#egg=MyProject

Mercurial
~~~~~~~~~
Expand Down Expand Up @@ -506,5 +505,3 @@ Examples
::

$ pip install --pre SomePackage


30 changes: 11 additions & 19 deletions pip/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ def switch(self, dest, url, rev_options):
call_subprocess(
[self.cmd, 'config', 'remote.origin.url', url], cwd=dest)
call_subprocess(
[self.cmd, 'checkout', '-q'] + rev_options, cwd=dest)

[self.cmd, 'fetch', '-q', '--depth', '1',
'origin', '+%s' % rev_options[0]] + rev_options[1:], cwd=dest)
self.update_submodules(dest)

def update(self, dest, rev_options):
# First fetch changes from the default remote
call_subprocess([self.cmd, 'fetch', '-q'], cwd=dest)
# Then reset to wanted revision (maby even origin/master)
# Then reset to wanted revision (maybe even origin/master)
if rev_options:
rev_options = self.check_rev_options(
rev_options[0], dest, rev_options,
Expand All @@ -107,27 +107,19 @@ def update(self, dest, rev_options):
def obtain(self, dest):
url, rev = self.get_url_rev()
if rev:
rev_options = [rev]
rev_display = ' (to %s)' % rev
else:
rev_options = ['origin/master']
rev = 'master'
rev_display = ''
if self.check_destination(dest, url, rev_options, rev_display):
if self.check_destination(dest, url, [rev], rev_display):
logger.info(
'Cloning %s%s to %s', url, rev_display, display_path(dest),
)
call_subprocess([self.cmd, 'clone', '-q', url, dest])

if rev:
rev_options = self.check_rev_options(rev, dest, rev_options)
# Only do a checkout if rev_options differs from HEAD
if not self.get_revision(dest).startswith(rev_options[0]):
call_subprocess(
[self.cmd, 'checkout', '-q'] + rev_options,
cwd=dest,
)
#: repo may contain submodules
self.update_submodules(dest)
call_subprocess([self.cmd, 'clone', '-q',
'--branch', rev,
'--depth', '1',
'--recurse-submodules',
url, dest])

def get_url(self, location):
url = call_subprocess(
Expand Down Expand Up @@ -178,7 +170,7 @@ def get_src_requirement(self, dist, location, find_tags):

if current_rev in names_by_commit:
# It's a tag or branch.
name = names_by_commit[current_rev]
current_rev = name = names_by_commit[current_rev]
full_egg_name = (
'%s-%s' % (egg_project_name, self.translate_egg_surname(name))
)
Expand Down
28 changes: 5 additions & 23 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 All @@ -42,26 +43,6 @@ def test_install_noneditable_git(script, tmpdir):
assert egg_info_folder in result.files_created, str(result)


@pytest.mark.network
def test_git_with_sha1_revisions(script):
"""
Git backend should be able to install from SHA1 revisions
"""
version_pkg_path = _create_test_package(script)
_change_test_package_version(script, version_pkg_path)
sha1 = script.run(
'git', 'rev-parse', 'HEAD~1',
cwd=version_pkg_path,
).stdout.strip()
script.pip(
'install', '-e',
'%s@%s#egg=version_pkg' %
('git+file://' + version_pkg_path.abspath.replace('\\', '/'), sha1)
)
version = script.run('version_pkg')
assert '0.1' in version.stdout, version.stdout


@pytest.mark.network
def test_git_with_branch_name_as_revision(script):
"""
Expand Down Expand Up @@ -96,7 +77,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 @@ -210,7 +192,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
4 changes: 3 additions & 1 deletion tests/functional/test_install_vcs_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ def test_check_submodule_addition(script):
"""
module_path, submodule_path = _create_test_package_with_submodule(script)

# expect error because git may write to stderr
install_result = script.pip(
'install', '-e', 'git+' + module_path + '#egg=version_pkg'
'install', '-e', 'git+' + module_path + '#egg=version_pkg',
expect_error=True,
)
assert (
script.venv / 'src/version-pkg/testpkg/static/testfile'
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_git_get_src_requirements():

assert ret == ''.join([
'git+http://github.com/pypa/pip-test-package',
'@5547fa909e83df8bd743d3978d6667497983a4b7',
'@bar',
'#egg=pip_test_package-bar'
])

Expand Down

0 comments on commit 6c18ad4

Please sign in to comment.