1
# Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
from bzrlib.errors import (
32
UninitializableFormat,
34
from bzrlib.transport import (
38
from bzrlib.plugins.git import (
43
from bzrlib.plugins.git.branch import (
47
from bzrlib.plugins.git.dir import (
51
from bzrlib.plugins.git.errors import (
52
GitSmartRemoteNotSupported,
55
from bzrlib.plugins.git.mapping import (
58
from bzrlib.plugins.git.repository import (
61
from bzrlib.plugins.git.refs import (
68
from dulwich.errors import (
71
from dulwich.pack import (
79
urlparse.uses_netloc.extend(['git', 'git+ssh'])
81
from dulwich.pack import load_pack_index
84
# Don't run any tests on GitSmartTransport as it is not intended to be
85
# a full implementation of Transport
86
def get_test_permutations():
90
def split_git_url(url):
94
:return: Tuple with host, port, username, path.
96
(scheme, netloc, loc, _, _) = urlparse.urlsplit(url)
97
path = urllib.unquote(loc)
98
if path.startswith("/~"):
100
(username, hostport) = urllib.splituser(netloc)
101
(host, port) = urllib.splitnport(hostport, None)
102
return (host, port, username, path)
105
def parse_git_error(url, message):
106
"""Parse a remote git server error and return a bzr exception.
108
:param url: URL of the remote repository
109
:param message: Message sent by the remote git server
111
message = str(message).strip()
112
if message.startswith("Could not find Repository "):
113
return NotBranchError(url, message)
114
# Don't know, just return it to the user as-is
115
return BzrError(message)
118
class GitSmartTransport(Transport):
120
def __init__(self, url, _client=None):
121
Transport.__init__(self, url)
122
(self._host, self._port, self._username, self._path) = \
124
if 'transport' in debug.debug_flags:
125
trace.mutter('host: %r, user: %r, port: %r, path: %r',
126
self._host, self._username, self._port, self._path)
127
self._client = _client
129
def external_url(self):
132
def has(self, relpath):
135
def _get_client(self, thin_packs):
136
raise NotImplementedError(self._get_client)
139
return self._path.rsplit(",", 1)[0]
142
raise NoSuchFile(path)
144
def abspath(self, relpath):
145
return urlutils.join(self.base, relpath)
147
def clone(self, offset=None):
148
"""See Transport.clone()."""
152
newurl = urlutils.join(self.base, offset)
154
return self.__class__(newurl, self._client)
157
class TCPGitSmartTransport(GitSmartTransport):
161
def _get_client(self, thin_packs):
162
if self._client is not None:
166
return dulwich.client.TCPGitClient(self._host, self._port,
167
thin_packs=thin_packs, report_activity=self._report_activity)
170
class SSHGitSmartTransport(GitSmartTransport):
175
path = self._path.rsplit(",", 1)[0]
176
if path.startswith("/~/"):
180
def _get_client(self, thin_packs):
181
if self._client is not None:
185
location_config = config.LocationConfig(self.base)
186
client = dulwich.client.SSHGitClient(self._host, self._port, self._username,
187
thin_packs=thin_packs, report_activity=self._report_activity)
188
# Set up alternate pack program paths
189
upload_pack = location_config.get_user_option('git_upload_pack')
191
client.alternative_paths["upload-pack"] = upload_pack
192
receive_pack = location_config.get_user_option('git_receive_pack')
194
client.alternative_paths["receive-pack"] = receive_pack
198
class RemoteGitDir(GitDir):
200
def __init__(self, transport, format, get_client, client_path):
201
self._format = format
202
self.root_transport = transport
203
self.transport = transport
204
self._mode_check_done = None
205
self._get_client = get_client
206
self._client_path = client_path
207
self.base = self.root_transport.base
210
def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
213
trace.info("git: %s" % text)
214
client = self._get_client(thin_packs=False)
216
return client.fetch_pack(self._client_path, determine_wants,
217
graph_walker, pack_data, progress)
218
except GitProtocolError, e:
219
raise parse_git_error(self.transport.external_url(), e)
221
def send_pack(self, get_changed_refs, generate_pack_contents):
222
client = self._get_client(thin_packs=False)
224
return client.send_pack(self._client_path, get_changed_refs,
225
generate_pack_contents)
226
except GitProtocolError, e:
227
raise parse_git_error(self.transport.external_url(), e)
229
def destroy_branch(self, name=None):
230
refname = self._get_selected_ref(name)
233
def get_changed_refs(old_refs):
235
if not refname in ret:
236
raise NotBranchError(self.user_url)
237
ret[refname] = "00" * 20
239
self.send_pack(get_changed_refs, lambda have, want: [])
243
return self.control_url
246
def user_transport(self):
247
return self.root_transport
250
def control_url(self):
251
return self.control_transport.base
254
def control_transport(self):
255
return self.root_transport
257
def open_repository(self):
258
return RemoteGitRepository(self)
260
def open_branch(self, name=None, unsupported=False,
261
ignore_fallbacks=False, ref=None):
262
repo = self.open_repository()
263
refname = self._get_selected_ref(name, ref)
264
return RemoteGitBranch(self, repo, refname)
266
def open_workingtree(self, recommend_upgrade=False):
267
raise NotLocalUrl(self.transport.base)
270
if self._refs is not None:
272
self._refs = self.fetch_pack(lambda x: [], None,
273
lambda x: None, lambda x: trace.mutter("git: %s" % x))
277
class EmptyObjectStoreIterator(dict):
279
def iterobjects(self):
283
class TemporaryPackIterator(Pack):
285
def __init__(self, path, resolve_ext_ref):
286
super(TemporaryPackIterator, self).__init__(path)
287
self.resolve_ext_ref = resolve_ext_ref
291
if self._data is None:
292
self._data = PackData(self._data_path)
297
if self._idx is None:
298
if not os.path.exists(self._idx_path):
299
pb = ui.ui_factory.nested_progress_bar()
301
def report_progress(cur, total):
302
pb.update("generating index", cur, total)
303
self.data.create_index(self._idx_path,
304
progress=report_progress)
307
self._idx = load_pack_index(self._idx_path)
311
if self._idx is not None:
313
os.remove(self._idx_path)
314
if self._data is not None:
316
os.remove(self._data_path)
319
class BzrGitHttpClient(dulwich.client.HttpGitClient):
321
def __init__(self, transport, *args, **kwargs):
322
self.transport = transport
323
super(BzrGitHttpClient, self).__init__(transport.external_url(), *args, **kwargs)
325
self._http_perform = getattr(self.transport, "_perform", urllib2.urlopen)
327
def _perform(self, req):
328
req.accepted_errors = (200, 404)
329
req.follow_redirections = True
330
req.redirected_to = None
331
return self._http_perform(req)
334
class RemoteGitControlDirFormat(GitControlDirFormat):
335
"""The .git directory control format."""
337
supports_workingtrees = False
340
def _known_formats(self):
341
return set([RemoteGitControlDirFormat()])
343
def is_initializable(self):
346
def is_supported(self):
349
def open(self, transport, _found=None):
350
"""Open this directory.
353
# we dont grok readonly - git isn't integrated with transport.
355
if url.startswith('readonly+'):
356
url = url[len('readonly+'):]
357
if isinstance(transport, GitSmartTransport):
358
get_client = transport._get_client
359
client_path = transport._get_path()
360
elif urlparse.urlsplit(transport.external_url())[0] in ("http", "https"):
361
def get_client(thin_packs=False):
362
return BzrGitHttpClient(transport, thin_packs=thin_packs)
363
client_path = transport._path
365
raise NotBranchError(transport.base)
366
return RemoteGitDir(transport, self, get_client, client_path)
368
def get_format_description(self):
369
return "Remote Git Repository"
371
def initialize_on_transport(self, transport):
372
raise UninitializableFormat(self)
374
def supports_transport(self, transport):
376
external_url = transport.external_url()
377
except InProcessTransport:
378
raise NotBranchError(path=transport.base)
379
return (external_url.startswith("http:") or
380
external_url.startswith("https:") or
381
external_url.startswith("git+") or
382
external_url.startswith("git:"))
385
class RemoteGitRepository(GitRepository):
387
def __init__(self, gitdir):
388
GitRepository.__init__(self, gitdir)
392
return self.bzrdir.base
396
return self.control_url
398
def get_parent_map(self, revids):
399
raise GitSmartRemoteNotSupported(self.get_parent_map, self)
401
def fetch_pack(self, determine_wants, graph_walker, pack_data,
403
return self.bzrdir.fetch_pack(determine_wants, graph_walker,
406
def send_pack(self, get_changed_refs, generate_pack_contents):
407
return self.bzrdir.send_pack(get_changed_refs, generate_pack_contents)
409
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
411
fd, path = tempfile.mkstemp(suffix=".pack")
413
self.fetch_pack(determine_wants, graph_walker,
414
lambda x: os.write(fd, x), progress)
417
if os.path.getsize(path) == 0:
418
return EmptyObjectStoreIterator()
419
return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
421
def lookup_bzr_revision_id(self, bzr_revid):
422
# This won't work for any round-tripped bzr revisions, but it's a start..
424
return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
425
except InvalidRevisionId:
426
raise NoSuchRevision(self, bzr_revid)
428
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
429
"""Lookup a revision id.
433
mapping = self.get_mapping()
434
# Not really an easy way to parse foreign revids here..
435
return mapping.revision_id_foreign_to_bzr(foreign_revid)
438
class RemoteGitTagDict(GitTags):
441
return self.repository.bzrdir.get_refs()
443
def _iter_tag_refs(self, refs):
444
for k, (peeled, unpeeled) in extract_tags(refs).iteritems():
445
yield (k, peeled, unpeeled,
446
self.branch.mapping.revision_id_foreign_to_bzr(peeled))
448
def set_tag(self, name, revid):
449
# FIXME: Not supported yet, should do a push of a new ref
450
raise NotImplementedError(self.set_tag)
453
class RemoteGitBranch(GitBranch):
455
def __init__(self, bzrdir, repository, name):
457
super(RemoteGitBranch, self).__init__(bzrdir, repository, name)
459
def last_revision_info(self):
460
raise GitSmartRemoteNotSupported(self.last_revision_info, self)
464
return self.control_url
467
def control_url(self):
470
def revision_history(self):
471
raise GitSmartRemoteNotSupported(self.revision_history, self)
473
def revision_id_to_revno(self, revision_id):
474
raise GitSmartRemoteNotSupported(self.revision_id_to_revno, self)
476
def last_revision(self):
477
return self.lookup_foreign_revision_id(self.head)
481
if self._sha is not None:
483
refs = self.bzrdir.get_refs()
484
name = branch_name_to_ref(self.name, "HEAD")
486
self._sha = refs[name]
488
raise NoSuchRef(name, self.repository.user_url, refs)
491
def _synchronize_history(self, destination, revision_id):
492
"""See Branch._synchronize_history()."""
493
destination.generate_revision_history(self.last_revision())
495
def get_push_location(self):
498
def set_push_location(self, url):