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 (
81
from dulwich.pack import load_pack_index
83
from dulwich.pack import PackIndex as load_pack_index
86
# Don't run any tests on GitSmartTransport as it is not intended to be
87
# a full implementation of Transport
88
def get_test_permutations():
92
class GitSmartTransport(Transport):
94
def __init__(self, url, _client=None):
95
Transport.__init__(self, url)
96
(scheme, _, loc, _, _) = urlparse.urlsplit(url)
97
assert scheme == "git"
98
hostport, self._path = urllib.splithost(loc)
99
(self._host, self._port) = urllib.splitnport(hostport, git.protocol.TCP_GIT_PORT)
100
self._client = _client
102
def has(self, relpath):
105
def _get_client(self):
106
if self._client is not None:
110
return git.client.TCPGitClient(self._host, self._port, thin_packs=False)
112
def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
115
info("git: %s" % text)
116
client = self._get_client()
118
client.fetch_pack(self._path, determine_wants,
119
graph_walker, pack_data, progress)
120
except GitProtocolError, e:
124
raise NoSuchFile(path)
126
def abspath(self, relpath):
127
return urlutils.join(self.base, relpath)
129
def clone(self, offset=None):
130
"""See Transport.clone()."""
134
newurl = urlutils.join(self.base, offset)
136
return GitSmartTransport(newurl, self._client)
139
class RemoteGitDir(GitDir):
141
def __init__(self, transport, lockfiles, format):
142
self._format = format
143
self.root_transport = transport
144
self.transport = transport
145
self._lockfiles = lockfiles
147
def open_repository(self):
148
return RemoteGitRepository(self, self._lockfiles)
150
def open_branch(self, _unsupported=False):
151
repo = self.open_repository()
152
# TODO: Support for multiple branches in one bzrdir in bzrlib!
153
return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
155
def open_workingtree(self):
156
raise NotLocalUrl(self.transport.base)
159
class EmptyObjectStoreIterator(dict):
161
def iterobjects(self):
165
class TemporaryPackIterator(Pack):
167
def __init__(self, path, resolve_ext_ref):
168
self.resolve_ext_ref = resolve_ext_ref
169
super(TemporaryPackIterator, self).__init__(path)
173
if self._idx is None:
174
if self._data is None:
175
self._data = PackData(self._data_path)
176
self._data.create_index_v2(self._idx_path, self.resolve_ext_ref)
177
self._idx = load_pack_index(self._idx_path)
181
os.remove(self._data_path)
182
os.remove(self._idx_path)
185
class RemoteGitRepository(GitRepository):
187
def __init__(self, gitdir, lockfiles):
188
GitRepository.__init__(self, gitdir, lockfiles)
192
if self._refs is not None:
194
def determine_wants(heads):
197
self.bzrdir.root_transport.fetch_pack(determine_wants, None,
198
lambda x: None, lambda x: mutter("git: %s" % x))
201
def fetch_pack(self, determine_wants, graph_walker, pack_data,
203
self._transport.fetch_pack(determine_wants, graph_walker, pack_data,
206
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
207
fd, path = tempfile.mkstemp(suffix=".pack")
208
self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
210
if os.path.getsize(path) == 0:
211
return EmptyObjectStoreIterator()
212
return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
215
class RemoteGitTagDict(tag.BasicTags):
217
def __init__(self, branch):
219
self.repository = branch.repository
221
def get_tag_dict(self):
223
refs = self.repository.get_refs()
224
for k,v in refs.iteritems():
225
if k.startswith("refs/tags/") and not k.endswith("^{}"):
226
v = refs.get(k+"^{}", v)
227
ret[k[len("refs/tags/"):]] = self.branch.mapping.revision_id_foreign_to_bzr(v)
230
def set_tag(self, name, revid):
231
# FIXME: Not supported yet, should do a push of a new ref
232
raise NotImplementedError(self.set_tag)
235
class RemoteGitBranch(GitBranch):
237
def __init__(self, bzrdir, repository, name, lockfiles):
238
heads = repository.get_refs()
239
if not name in heads:
240
raise NoSuchRef(name)
241
self._ref = heads[name]
242
super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
244
def last_revision(self):
245
return self.mapping.revision_id_foreign_to_bzr(self._ref)
247
def _synchronize_history(self, destination, revision_id):
248
"""See Branch._synchronize_history()."""
249
destination.generate_revision_history(self.last_revision())