1
# Copyright (C) 2007-2009 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
23
from bzrlib.bzrdir import (
27
from bzrlib.errors import (
32
from bzrlib.lockable_files import (
35
from bzrlib.repository import (
38
from bzrlib.trace import (
41
from bzrlib.transport import (
45
from bzrlib.plugins.git import (
50
from bzrlib.plugins.git.branch import (
53
from bzrlib.plugins.git.errors import (
56
from bzrlib.plugins.git.dir import (
59
from bzrlib.plugins.git.foreign import (
62
from bzrlib.plugins.git.repository import (
68
from dulwich.errors import (
71
from dulwich.pack import (
82
# Don't run any tests on GitSmartTransport as it is not intended to be
83
# a full implementation of Transport
84
def get_test_permutations():
88
class GitSmartTransport(Transport):
90
def __init__(self, url, _client=None):
91
Transport.__init__(self, url)
92
(scheme, _, loc, _, _) = urlparse.urlsplit(url)
93
assert scheme == "git"
94
hostport, self._path = urllib.splithost(loc)
95
(self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
96
self._client = _client
98
def has(self, relpath):
101
def _get_client(self):
102
if self._client is not None:
106
return git.client.TCPGitClient(self._host, self._port, thin_packs=False)
108
def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
111
info("git: %s" % text)
112
client = self._get_client()
114
client.fetch_pack(self._path, determine_wants,
115
graph_walker, pack_data, progress)
116
except GitProtocolError, e:
120
raise NoSuchFile(path)
122
def abspath(self, relpath):
123
return urlutils.join(self.base, relpath)
125
def clone(self, offset=None):
126
"""See Transport.clone()."""
130
newurl = urlutils.join(self.base, offset)
132
return GitSmartTransport(newurl, self._client)
135
class RemoteGitDir(GitDir):
137
def __init__(self, transport, lockfiles, format):
138
self._format = format
139
self.root_transport = transport
140
self.transport = transport
141
self._lockfiles = lockfiles
143
def open_repository(self):
144
return RemoteGitRepository(self, self._lockfiles)
146
def open_branch(self, _unsupported=False):
147
repo = self.open_repository()
148
# TODO: Support for multiple branches in one bzrdir in bzrlib!
149
return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
151
def open_workingtree(self):
152
raise NotLocalUrl(self.transport.base)
155
class EmptyObjectStoreIterator(dict):
157
def iterobjects(self):
161
class TemporaryPackIterator(Pack):
163
def __init__(self, path, resolve_ext_ref):
164
self.resolve_ext_ref = resolve_ext_ref
165
super(TemporaryPackIterator, self).__init__(path)
169
if self._idx is None:
170
if self._data is None:
171
self._data = PackData(self._data_path)
172
self._data.create_index_v2(self._idx_path, self.resolve_ext_ref)
173
self._idx = PackIndex(self._idx_path)
177
os.remove(self._data_path)
178
os.remove(self._idx_path)
181
class RemoteGitRepository(GitRepository):
183
def __init__(self, gitdir, lockfiles):
184
GitRepository.__init__(self, gitdir, lockfiles)
188
if self._refs is not None:
190
def determine_wants(heads):
193
self.bzrdir.root_transport.fetch_pack(determine_wants, None,
194
lambda x: None, lambda x: mutter("git: %s" % x))
197
def fetch_pack(self, determine_wants, graph_walker, pack_data,
199
self._transport.fetch_pack(determine_wants, graph_walker, pack_data,
202
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
203
fd, path = tempfile.mkstemp(suffix=".pack")
204
self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
206
if os.path.getsize(path) == 0:
207
return EmptyObjectStoreIterator()
208
return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
211
class RemoteGitTagDict(tag.BasicTags):
213
def __init__(self, branch):
215
self.repository = branch.repository
217
def get_tag_dict(self):
219
refs = self.repository.get_refs()
220
for k,v in refs.iteritems():
221
if k.startswith("refs/tags/") and not k.endswith("^{}"):
222
v = refs.get(k+"^{}", v)
223
ret[k[len("refs/tags/"):]] = self.branch.mapping.revision_id_foreign_to_bzr(v)
226
def set_tag(self, name, revid):
227
# FIXME: Not supported yet, should do a push of a new ref
228
raise NotImplementedError(self.set_tag)
231
class RemoteGitBranch(GitBranch):
233
def __init__(self, bzrdir, repository, name, lockfiles):
234
heads = repository.get_refs()
235
if not name in heads:
236
raise NoSuchRef(name)
237
self._ref = heads[name]
238
super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
240
def last_revision(self):
241
return self.mapping.revision_id_foreign_to_bzr(self._ref)
243
def _synchronize_history(self, destination, revision_id):
244
"""See Branch._synchronize_history()."""
245
destination.generate_revision_history(self.last_revision())