2014-08-31 01:52:28 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import logging
|
2009-11-20 22:03:40 +01:00
|
|
|
import tempfile
|
2012-05-30 04:37:17 +02:00
|
|
|
import os.path
|
2014-05-08 18:12:34 +02:00
|
|
|
|
2014-09-12 00:40:45 +02:00
|
|
|
from pip._vendor.six.moves.urllib import parse as urllib_parse
|
|
|
|
from pip._vendor.six.moves.urllib import request as urllib_request
|
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
from pip.utils import display_path, rmtree
|
2009-11-20 22:03:40 +01:00
|
|
|
from pip.vcs import vcs, VersionControl
|
2014-05-08 18:12:34 +02:00
|
|
|
|
2014-09-12 00:40:45 +02:00
|
|
|
|
|
|
|
urlsplit = urllib_parse.urlsplit
|
|
|
|
urlunsplit = urllib_parse.urlunsplit
|
2009-11-20 22:03:40 +01:00
|
|
|
|
2011-05-04 09:44:02 +02:00
|
|
|
|
2014-08-31 01:52:28 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2009-11-20 22:03:40 +01:00
|
|
|
class Git(VersionControl):
|
|
|
|
name = 'git'
|
|
|
|
dirname = '.git'
|
|
|
|
repo_name = 'clone'
|
2014-01-27 15:07:10 +01:00
|
|
|
schemes = (
|
|
|
|
'git', 'git+http', 'git+https', 'git+ssh', 'git+git', 'git+file',
|
|
|
|
)
|
2009-11-20 22:03:40 +01:00
|
|
|
|
2010-05-18 02:33:13 +02: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:
|
2010-06-03 04:25:26 +02:00
|
|
|
scheme, netloc, path, query, fragment = urlsplit(url)
|
2010-05-18 02:33:13 +02:00
|
|
|
if scheme.endswith('file'):
|
|
|
|
initial_slashes = path[:-len(path.lstrip('/'))]
|
2014-01-27 15:07:10 +01:00
|
|
|
newpath = (
|
|
|
|
initial_slashes +
|
2014-09-12 00:40:45 +02:00
|
|
|
urllib_request.url2pathname(path)
|
|
|
|
.replace('\\', '/').lstrip('/')
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2010-05-18 02:33:13 +02:00
|
|
|
url = urlunsplit((scheme, netloc, newpath, query, fragment))
|
2012-05-28 17:29:53 +02:00
|
|
|
after_plus = scheme.find('+') + 1
|
2014-01-27 15:07:10 +01:00
|
|
|
url = scheme[:after_plus] + urlunsplit(
|
|
|
|
(scheme[after_plus:], netloc, newpath, query, fragment),
|
|
|
|
)
|
2010-05-18 02:33:13 +02:00
|
|
|
|
2010-06-03 04:25:26 +02:00
|
|
|
super(Git, self).__init__(url, *args, **kwargs)
|
2010-05-18 02:33:13 +02:00
|
|
|
|
2009-11-20 22:03:40 +01:00
|
|
|
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-03-10 12:28:09 +01:00
|
|
|
self.run_command(
|
|
|
|
['checkout-index', '-a', '-f', '--prefix', location],
|
2015-04-16 00:06:11 +02:00
|
|
|
show_stdout=False, cwd=temp_dir)
|
2009-11-20 22:03:40 +01:00
|
|
|
finally:
|
2010-11-22 20:32:14 +01:00
|
|
|
rmtree(temp_dir)
|
2009-11-20 22:03:40 +01:00
|
|
|
|
|
|
|
def check_rev_options(self, rev, dest, rev_options):
|
|
|
|
"""Check the revision options before checkout to compensate that tags
|
2010-06-27 19:19:47 +02:00
|
|
|
and branches may need origin/ as a prefix.
|
|
|
|
Returns the SHA1 of the branch or tag if found.
|
|
|
|
"""
|
2013-05-30 00:40:34 +02:00
|
|
|
revisions = self.get_refs(dest)
|
2011-05-15 17:47:06 +02:00
|
|
|
|
2010-06-27 19:19:47 +02:00
|
|
|
origin_rev = 'origin/%s' % rev
|
2011-05-15 17:47:06 +02:00
|
|
|
if origin_rev in revisions:
|
|
|
|
# remote branch
|
|
|
|
return [revisions[origin_rev]]
|
|
|
|
elif rev in revisions:
|
2011-01-23 11:52:25 +01:00
|
|
|
# a local tag or branch name
|
2011-05-15 17:47:06 +02:00
|
|
|
return [revisions[rev]]
|
2010-06-27 19:19:47 +02:00
|
|
|
else:
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.warning(
|
|
|
|
"Could not find a tag or branch '%s', assuming commit.", rev,
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2010-06-27 19:19:47 +02:00
|
|
|
return rev_options
|
2009-11-20 22:03:40 +01:00
|
|
|
|
|
|
|
def switch(self, dest, url, rev_options):
|
2015-03-10 12:28:09 +01:00
|
|
|
self.run_command(['config', 'remote.origin.url', url], cwd=dest)
|
|
|
|
self.run_command(['checkout', '-q'] + rev_options, cwd=dest)
|
2009-11-20 22:03:40 +01:00
|
|
|
|
2012-05-30 04:37:17 +02:00
|
|
|
self.update_submodules(dest)
|
|
|
|
|
2009-11-20 22:03:40 +01:00
|
|
|
def update(self, dest, rev_options):
|
2010-11-21 17:43:26 +01:00
|
|
|
# First fetch changes from the default remote
|
2015-03-10 12:28:09 +01:00
|
|
|
self.run_command(['fetch', '-q'], cwd=dest)
|
2010-11-21 17:43:26 +01:00
|
|
|
# Then reset to wanted revision (maby even origin/master)
|
2011-01-23 11:52:25 +01:00
|
|
|
if rev_options:
|
2014-01-27 15:07:10 +01:00
|
|
|
rev_options = self.check_rev_options(
|
|
|
|
rev_options[0], dest, rev_options,
|
|
|
|
)
|
2015-03-10 12:28:09 +01:00
|
|
|
self.run_command(['reset', '--hard', '-q'] + rev_options, cwd=dest)
|
2012-05-30 04:37:17 +02:00
|
|
|
#: update submodules
|
|
|
|
self.update_submodules(dest)
|
2009-11-20 22:03:40 +01:00
|
|
|
|
|
|
|
def obtain(self, dest):
|
|
|
|
url, rev = self.get_url_rev()
|
|
|
|
if rev:
|
|
|
|
rev_options = [rev]
|
|
|
|
rev_display = ' (to %s)' % rev
|
|
|
|
else:
|
2010-11-21 17:43:26 +01:00
|
|
|
rev_options = ['origin/master']
|
2009-11-20 22:03:40 +01:00
|
|
|
rev_display = ''
|
|
|
|
if self.check_destination(dest, url, rev_options, rev_display):
|
2014-08-31 01:52:28 +02:00
|
|
|
logger.info(
|
|
|
|
'Cloning %s%s to %s', url, rev_display, display_path(dest),
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2015-03-10 12:28:09 +01:00
|
|
|
self.run_command(['clone', '-q', url, dest])
|
2014-12-13 01:04:47 +01:00
|
|
|
|
2010-06-27 19:19:47 +02: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-03-10 12:28:09 +01:00
|
|
|
self.run_command(
|
|
|
|
['checkout', '-q'] + rev_options,
|
2014-01-27 15:07:10 +01:00
|
|
|
cwd=dest,
|
|
|
|
)
|
2014-12-13 01:04:47 +01:00
|
|
|
#: repo may contain submodules
|
|
|
|
self.update_submodules(dest)
|
2009-11-20 22:03:40 +01:00
|
|
|
|
|
|
|
def get_url(self, location):
|
2015-03-10 12:28:09 +01:00
|
|
|
url = self.run_command(
|
|
|
|
['config', 'remote.origin.url'],
|
2009-11-20 22:03:40 +01:00
|
|
|
show_stdout=False, cwd=location)
|
|
|
|
return url.strip()
|
|
|
|
|
|
|
|
def get_revision(self, location):
|
2015-03-10 12:28:09 +01:00
|
|
|
current_rev = self.run_command(
|
|
|
|
['rev-parse', 'HEAD'], show_stdout=False, cwd=location)
|
2009-11-20 22:03:40 +01:00
|
|
|
return current_rev.strip()
|
|
|
|
|
2013-05-30 00:40:34 +02:00
|
|
|
def get_refs(self, location):
|
|
|
|
"""Return map of named refs (branches or tags) to commit hashes."""
|
2015-03-10 12:28:09 +01:00
|
|
|
output = self.run_command(['show-ref'],
|
|
|
|
show_stdout=False, cwd=location)
|
2013-05-30 00:40:34 +02:00
|
|
|
rv = {}
|
|
|
|
for line in output.strip().splitlines():
|
|
|
|
commit, ref = line.split(' ', 1)
|
2013-05-29 22:03:26 +02:00
|
|
|
ref = ref.strip()
|
2013-05-30 00:40:34 +02:00
|
|
|
ref_name = None
|
2013-05-29 22:03:26 +02:00
|
|
|
if ref.startswith('refs/remotes/'):
|
2013-05-30 00:40:34 +02:00
|
|
|
ref_name = ref[len('refs/remotes/'):]
|
2013-05-29 22:03:26 +02:00
|
|
|
elif ref.startswith('refs/heads/'):
|
2013-05-30 00:40:34 +02:00
|
|
|
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
|
2009-11-20 22:03:40 +01:00
|
|
|
|
|
|
|
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)
|
2013-05-30 00:40:34 +02:00
|
|
|
refs = self.get_refs(location)
|
|
|
|
# refs maps names to commit hashes; we need the inverse
|
2014-09-27 23:14:58 +02: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
|
2009-11-20 22:03:40 +01:00
|
|
|
|
2013-05-30 00:40:34 +02:00
|
|
|
if current_rev in names_by_commit:
|
2014-09-26 19:20:57 +02:00
|
|
|
# It's a tag or branch.
|
2014-09-25 07:21:36 +02:00
|
|
|
name = names_by_commit[current_rev]
|
2014-01-27 15:07:10 +01:00
|
|
|
full_egg_name = (
|
2014-09-25 08:27:22 +02:00
|
|
|
'%s-%s' % (egg_project_name, self.translate_egg_surname(name))
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2009-11-20 22:03:40 +01:00
|
|
|
else:
|
2011-01-23 13:02:04 +01:00
|
|
|
full_egg_name = '%s-dev' % egg_project_name
|
2009-11-20 22:03:40 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2014-03-26 23:24:19 +01:00
|
|
|
if '://' not in self.url:
|
|
|
|
assert 'file:' not in self.url
|
2009-11-20 22:03:40 +01:00
|
|
|
self.url = self.url.replace('git+', 'git+ssh://')
|
|
|
|
url, rev = super(Git, self).get_url_rev()
|
|
|
|
url = url.replace('ssh://', '')
|
2010-06-03 04:25:26 +02:00
|
|
|
else:
|
2010-05-18 16:25:08 +02:00
|
|
|
url, rev = super(Git, self).get_url_rev()
|
|
|
|
|
2010-06-03 04:25:26 +02:00
|
|
|
return url, rev
|
|
|
|
|
2012-05-30 04:37:17 +02:00
|
|
|
def update_submodules(self, location):
|
|
|
|
if not os.path.exists(os.path.join(location, '.gitmodules')):
|
|
|
|
return
|
2015-03-10 12:28:09 +01:00
|
|
|
self.run_command(
|
|
|
|
['submodule', 'update', '--init', '--recursive', '-q'],
|
2014-01-27 15:07:10 +01:00
|
|
|
cwd=location,
|
|
|
|
)
|
2009-11-20 22:03:40 +01:00
|
|
|
|
|
|
|
vcs.register(Git)
|