2015-11-25 01:25:01 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import logging
|
2013-10-11 17:28:32 +00:00
|
|
|
import tempfile
|
|
|
|
import os.path
|
2015-11-25 01:25:01 +00:00
|
|
|
|
|
|
|
from pip._vendor.six.moves.urllib import parse as urllib_parse
|
|
|
|
from pip._vendor.six.moves.urllib import request as urllib_request
|
|
|
|
|
|
|
|
from pip.utils import display_path, rmtree
|
2013-10-11 17:28:32 +00:00
|
|
|
from pip.vcs import vcs, VersionControl
|
2015-11-25 01:25:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
urlsplit = urllib_parse.urlsplit
|
|
|
|
urlunsplit = urllib_parse.urlunsplit
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Git(VersionControl):
|
|
|
|
name = 'git'
|
|
|
|
dirname = '.git'
|
|
|
|
repo_name = 'clone'
|
2015-11-25 01:25:01 +00:00
|
|
|
schemes = (
|
|
|
|
'git', 'git+http', 'git+https', 'git+ssh', 'git+git', 'git+file',
|
|
|
|
)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def __init__(self, url=None, *args, **kwargs):
|
|
|
|
|
|
|
|
# Works around an apparent Git bug
|
|
|
|
# (see http://article.gmane.org/gmane.comp.version-control.git/146500)
|
|
|
|
if url:
|
|
|
|
scheme, netloc, path, query, fragment = urlsplit(url)
|
|
|
|
if scheme.endswith('file'):
|
|
|
|
initial_slashes = path[:-len(path.lstrip('/'))]
|
2015-11-25 01:25:01 +00:00
|
|
|
newpath = (
|
|
|
|
initial_slashes +
|
|
|
|
urllib_request.url2pathname(path)
|
|
|
|
.replace('\\', '/').lstrip('/')
|
|
|
|
)
|
2013-10-11 17:28:32 +00:00
|
|
|
url = urlunsplit((scheme, netloc, newpath, query, fragment))
|
|
|
|
after_plus = scheme.find('+') + 1
|
2015-11-25 01:25:01 +00:00
|
|
|
url = scheme[:after_plus] + urlunsplit(
|
|
|
|
(scheme[after_plus:], netloc, newpath, query, fragment),
|
|
|
|
)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
super(Git, self).__init__(url, *args, **kwargs)
|
|
|
|
|
|
|
|
def export(self, location):
|
|
|
|
"""Export the Git repository at the url to the destination location"""
|
|
|
|
temp_dir = tempfile.mkdtemp('-export', 'pip-')
|
|
|
|
self.unpack(temp_dir)
|
|
|
|
try:
|
|
|
|
if not location.endswith('/'):
|
|
|
|
location = location + '/'
|
2015-11-25 01:25:01 +00:00
|
|
|
self.run_command(
|
|
|
|
['checkout-index', '-a', '-f', '--prefix', location],
|
|
|
|
show_stdout=False, cwd=temp_dir)
|
2013-10-11 17:28:32 +00:00
|
|
|
finally:
|
|
|
|
rmtree(temp_dir)
|
|
|
|
|
|
|
|
def check_rev_options(self, rev, dest, rev_options):
|
|
|
|
"""Check the revision options before checkout to compensate that tags
|
|
|
|
and branches may need origin/ as a prefix.
|
|
|
|
Returns the SHA1 of the branch or tag if found.
|
|
|
|
"""
|
|
|
|
revisions = self.get_refs(dest)
|
|
|
|
|
|
|
|
origin_rev = 'origin/%s' % rev
|
|
|
|
if origin_rev in revisions:
|
|
|
|
# remote branch
|
|
|
|
return [revisions[origin_rev]]
|
|
|
|
elif rev in revisions:
|
|
|
|
# a local tag or branch name
|
|
|
|
return [revisions[rev]]
|
|
|
|
else:
|
2015-11-25 01:25:01 +00:00
|
|
|
logger.warning(
|
|
|
|
"Could not find a tag or branch '%s', assuming commit.", rev,
|
|
|
|
)
|
2013-10-11 17:28:32 +00:00
|
|
|
return rev_options
|
|
|
|
|
|
|
|
def switch(self, dest, url, rev_options):
|
2015-11-25 01:25:01 +00:00
|
|
|
self.run_command(['config', 'remote.origin.url', url], cwd=dest)
|
|
|
|
self.run_command(['checkout', '-q'] + rev_options, cwd=dest)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
self.update_submodules(dest)
|
|
|
|
|
|
|
|
def update(self, dest, rev_options):
|
|
|
|
# First fetch changes from the default remote
|
2015-11-25 01:25:01 +00:00
|
|
|
self.run_command(['fetch', '-q'], cwd=dest)
|
2013-10-11 17:28:32 +00:00
|
|
|
# Then reset to wanted revision (maby even origin/master)
|
|
|
|
if rev_options:
|
2015-11-25 01:25:01 +00:00
|
|
|
rev_options = self.check_rev_options(
|
|
|
|
rev_options[0], dest, rev_options,
|
|
|
|
)
|
|
|
|
self.run_command(['reset', '--hard', '-q'] + rev_options, cwd=dest)
|
2013-10-11 17:28:32 +00:00
|
|
|
#: update submodules
|
|
|
|
self.update_submodules(dest)
|
|
|
|
|
|
|
|
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_display = ''
|
|
|
|
if self.check_destination(dest, url, rev_options, rev_display):
|
2015-11-25 01:25:01 +00:00
|
|
|
logger.info(
|
|
|
|
'Cloning %s%s to %s', url, rev_display, display_path(dest),
|
|
|
|
)
|
|
|
|
self.run_command(['clone', '-q', url, dest])
|
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
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]):
|
2015-11-25 01:25:01 +00:00
|
|
|
self.run_command(
|
|
|
|
['checkout', '-q'] + rev_options,
|
|
|
|
cwd=dest,
|
|
|
|
)
|
|
|
|
#: repo may contain submodules
|
|
|
|
self.update_submodules(dest)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
def get_url(self, location):
|
2015-11-25 01:25:01 +00:00
|
|
|
url = self.run_command(
|
|
|
|
['config', 'remote.origin.url'],
|
2013-10-11 17:28:32 +00:00
|
|
|
show_stdout=False, cwd=location)
|
|
|
|
return url.strip()
|
|
|
|
|
|
|
|
def get_revision(self, location):
|
2015-11-25 01:25:01 +00:00
|
|
|
current_rev = self.run_command(
|
|
|
|
['rev-parse', 'HEAD'], show_stdout=False, cwd=location)
|
2013-10-11 17:28:32 +00:00
|
|
|
return current_rev.strip()
|
|
|
|
|
|
|
|
def get_refs(self, location):
|
|
|
|
"""Return map of named refs (branches or tags) to commit hashes."""
|
2015-11-25 01:25:01 +00:00
|
|
|
output = self.run_command(['show-ref'],
|
|
|
|
show_stdout=False, cwd=location)
|
2013-10-11 17:28:32 +00:00
|
|
|
rv = {}
|
|
|
|
for line in output.strip().splitlines():
|
|
|
|
commit, ref = line.split(' ', 1)
|
|
|
|
ref = ref.strip()
|
|
|
|
ref_name = None
|
|
|
|
if ref.startswith('refs/remotes/'):
|
|
|
|
ref_name = ref[len('refs/remotes/'):]
|
|
|
|
elif ref.startswith('refs/heads/'):
|
|
|
|
ref_name = ref[len('refs/heads/'):]
|
|
|
|
elif ref.startswith('refs/tags/'):
|
|
|
|
ref_name = ref[len('refs/tags/'):]
|
|
|
|
if ref_name is not None:
|
|
|
|
rv[ref_name] = commit.strip()
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def get_src_requirement(self, dist, location, find_tags):
|
|
|
|
repo = self.get_url(location)
|
|
|
|
if not repo.lower().startswith('git:'):
|
|
|
|
repo = 'git+' + repo
|
|
|
|
egg_project_name = dist.egg_name().split('-', 1)[0]
|
|
|
|
if not repo:
|
|
|
|
return None
|
|
|
|
current_rev = self.get_revision(location)
|
|
|
|
refs = self.get_refs(location)
|
|
|
|
# refs maps names to commit hashes; we need the inverse
|
2015-11-25 01:25:01 +00:00
|
|
|
# if multiple names map to a single commit, we pick the first one
|
|
|
|
# alphabetically
|
|
|
|
names_by_commit = {}
|
|
|
|
for ref, commit in sorted(refs.items()):
|
|
|
|
if commit not in names_by_commit:
|
|
|
|
names_by_commit[commit] = ref
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
if current_rev in names_by_commit:
|
2015-11-25 01:25:01 +00:00
|
|
|
# It's a tag or branch.
|
|
|
|
name = names_by_commit[current_rev]
|
|
|
|
full_egg_name = (
|
|
|
|
'%s-%s' % (egg_project_name, self.translate_egg_surname(name))
|
|
|
|
)
|
2013-10-11 17:28:32 +00:00
|
|
|
else:
|
|
|
|
full_egg_name = '%s-dev' % egg_project_name
|
|
|
|
|
|
|
|
return '%s@%s#egg=%s' % (repo, current_rev, full_egg_name)
|
|
|
|
|
|
|
|
def get_url_rev(self):
|
|
|
|
"""
|
|
|
|
Prefixes stub URLs like 'user@hostname:user/repo.git' with 'ssh://'.
|
|
|
|
That's required because although they use SSH they sometimes doesn't
|
|
|
|
work with a ssh:// scheme (e.g. Github). But we need a scheme for
|
|
|
|
parsing. Hence we remove it again afterwards and return it as a stub.
|
|
|
|
"""
|
2015-11-25 01:25:01 +00:00
|
|
|
if '://' not in self.url:
|
|
|
|
assert 'file:' not in self.url
|
2013-10-11 17:28:32 +00:00
|
|
|
self.url = self.url.replace('git+', 'git+ssh://')
|
|
|
|
url, rev = super(Git, self).get_url_rev()
|
|
|
|
url = url.replace('ssh://', '')
|
|
|
|
else:
|
|
|
|
url, rev = super(Git, self).get_url_rev()
|
|
|
|
|
|
|
|
return url, rev
|
|
|
|
|
|
|
|
def update_submodules(self, location):
|
|
|
|
if not os.path.exists(os.path.join(location, '.gitmodules')):
|
|
|
|
return
|
2015-11-25 01:25:01 +00:00
|
|
|
self.run_command(
|
|
|
|
['submodule', 'update', '--init', '--recursive', '-q'],
|
|
|
|
cwd=location,
|
|
|
|
)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
vcs.register(Git)
|