1
# Copyright (C) 2007-2008 Canonical Ltd
1
# Copyright (C) 2007-2009 Jelmer Vernooij <jelmer@samba.org>
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib import urlutils
19
from bzrlib.bzrdir import BzrDir, BzrDirFormat
20
from bzrlib.errors import NoSuchFile, NotLocalUrl
21
from bzrlib.lockable_files import TransportLock
22
from bzrlib.repository import Repository
23
from bzrlib.trace import info
24
from bzrlib.transport import Transport
24
from bzrlib.errors import (
29
from bzrlib.trace import (
32
from bzrlib.transport import (
26
from bzrlib.plugins.git import lazy_check_versions
36
from bzrlib.plugins.git import (
27
39
lazy_check_versions()
29
from bzrlib.plugins.git.branch import GitBranch
30
from bzrlib.plugins.git.errors import NoSuchRef
31
from bzrlib.plugins.git.dir import GitDir
32
from bzrlib.plugins.git.foreign import ForeignBranch
33
from bzrlib.plugins.git.repository import GitFormat, GitRepository
41
from bzrlib.plugins.git.branch import (
44
from bzrlib.plugins.git.errors import (
45
GitSmartRemoteNotSupported,
48
from bzrlib.plugins.git.dir import (
51
from bzrlib.plugins.git.repository import (
57
from dulwich.errors import (
60
from dulwich.pack import (
41
from dulwich.pack import PackData, Pack, PackIndex
70
from dulwich.pack import load_pack_index
72
from dulwich.pack import PackIndex as load_pack_index
43
75
# Don't run any tests on GitSmartTransport as it is not intended to be
44
76
# a full implementation of Transport
51
83
def __init__(self, url, _client=None):
52
84
Transport.__init__(self, url)
53
85
(scheme, _, loc, _, _) = urlparse.urlsplit(url)
54
assert scheme == "git"
55
86
hostport, self._path = urllib.splithost(loc)
56
(self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
87
(self._host, self._port) = urllib.splitnport(hostport, None)
57
88
self._client = _client
90
def has(self, relpath):
59
93
def _get_client(self):
60
if self._client is not None:
64
return git.client.TCPGitClient(self._host, self._port,
65
capabilities=["multi_ack", "side-band-64k", "ofs-delta", "side-band"])
94
raise NotImplementedError(self._get_client)
67
96
def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
68
97
if progress is None:
69
98
def progress(text):
70
99
info("git: %s" % text)
71
self._get_client().fetch_pack(self._path, determine_wants,
72
graph_walker, pack_data, progress)
100
client = self._get_client()
102
client.fetch_pack(self._path, determine_wants,
103
graph_walker, pack_data, progress)
104
except GitProtocolError, e:
74
107
def get(self, path):
75
108
raise NoSuchFile(path)
85
118
newurl = urlutils.join(self.base, offset)
87
return GitSmartTransport(newurl, self._client)
120
return self.__class__(newurl, self._client)
123
class TCPGitSmartTransport(GitSmartTransport):
127
def _get_client(self):
128
if self._client is not None:
132
return git.client.TCPGitClient(self._host, self._port, thin_packs=False,
133
report_activity=self._report_activity)
136
class SSHGitSmartTransport(GitSmartTransport):
140
def _get_client(self):
141
if self._client is not None:
145
return git.client.SSHGitClient(self._host, self._port, thin_packs=False,
146
report_activity=self._report_activity)
90
149
class RemoteGitDir(GitDir):
94
153
self.root_transport = transport
95
154
self.transport = transport
96
155
self._lockfiles = lockfiles
156
self._mode_check_done = None
98
158
def open_repository(self):
99
159
return RemoteGitRepository(self, self._lockfiles)
101
def open_branch(self, _unsupported=False):
161
def open_branch(self, ignore_fallbacks=False):
102
162
repo = self.open_repository()
103
163
# TODO: Support for multiple branches in one bzrdir in bzrlib!
104
164
return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
116
176
class TemporaryPackIterator(Pack):
118
178
def __init__(self, path, resolve_ext_ref):
179
super(TemporaryPackIterator, self).__init__(path)
119
180
self.resolve_ext_ref = resolve_ext_ref
120
super(TemporaryPackIterator, self).__init__(path)
124
184
if self._idx is None:
125
self._data.create_index_v2(self._idx_path, self.resolve_ext_ref)
126
self._idx = PackIndex(self._idx_path)
185
if self._data is None:
186
self._data = PackData(self._data_path)
187
pb = ui.ui_factory.nested_progress_bar()
189
def report_progress(cur, total):
190
pb.update("generating index", cur, total)
191
self._data.create_index_v2(self._idx_path, self.resolve_ext_ref,
192
progress=report_progress)
195
self._idx = load_pack_index(self._idx_path)
129
198
def __del__(self):
136
205
def __init__(self, gitdir, lockfiles):
137
206
GitRepository.__init__(self, gitdir, lockfiles)
210
def inventories(self):
211
raise GitSmartRemoteNotSupported()
215
raise GitSmartRemoteNotSupported()
219
raise GitSmartRemoteNotSupported()
222
if self._refs is not None:
224
def determine_wants(heads):
227
self.bzrdir.root_transport.fetch_pack(determine_wants, None,
228
lambda x: None, lambda x: mutter("git: %s" % x))
139
231
def fetch_pack(self, determine_wants, graph_walker, pack_data,
150
242
return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
245
class RemoteGitTagDict(tag.BasicTags):
247
def __init__(self, branch):
249
self.repository = branch.repository
251
def get_tag_dict(self):
253
refs = self.repository.get_refs()
254
for k,v in refs.iteritems():
255
if k.startswith("refs/tags/") and not k.endswith("^{}"):
256
v = refs.get(k+"^{}", v)
257
ret[k[len("refs/tags/"):]] = self.branch.mapping.revision_id_foreign_to_bzr(v)
260
def set_tag(self, name, revid):
261
# FIXME: Not supported yet, should do a push of a new ref
262
raise NotImplementedError(self.set_tag)
153
265
class RemoteGitBranch(GitBranch):
155
267
def __init__(self, bzrdir, repository, name, lockfiles):
156
def determine_wants(heads):
157
if not name in heads:
158
raise NoSuchRef(name)
159
self._ref = heads[name]
160
bzrdir.root_transport.fetch_pack(determine_wants, None, lambda x: None,
161
lambda x: mutter("git: %s" % x))
268
heads = repository.get_refs()
269
if not name in heads:
270
raise NoSuchRef(name)
271
self._ref = heads[name]
162
272
super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
164
274
def last_revision(self):