60
66
from dulwich.pack import (
70
from dulwich.pack import load_pack_index
72
from dulwich.pack import PackIndex as load_pack_index
75
# Don't run any tests on GitSmartTransport as it is not intended to be
74
urlparse.uses_netloc.extend(['git', 'git+ssh'])
76
from dulwich.pack import load_pack_index
79
# Don't run any tests on GitSmartTransport as it is not intended to be
76
80
# a full implementation of Transport
77
81
def get_test_permutations():
85
def split_git_url(url):
89
:return: Tuple with host, port, username, path.
91
(scheme, netloc, loc, _, _) = urlparse.urlsplit(url)
92
path = urllib.unquote(loc)
93
if path.startswith("/~"):
95
(username, hostport) = urllib.splituser(netloc)
96
(host, port) = urllib.splitnport(hostport, None)
97
return (host, port, username, path)
81
100
class GitSmartTransport(Transport):
83
102
def __init__(self, url, _client=None):
84
103
Transport.__init__(self, url)
85
(scheme, _, loc, _, _) = urlparse.urlsplit(url)
86
hostport, self._path = urllib.splithost(loc)
87
(self._host, self._port) = urllib.splitnport(hostport, None)
104
(self._host, self._port, self._username, self._path) = \
106
if 'transport' in debug.debug_flags:
107
trace.mutter('host: %r, user: %r, port: %r, path: %r',
108
self._host, self._username, self._port, self._path)
88
109
self._client = _client
111
def external_url(self):
90
114
def has(self, relpath):
93
def _get_client(self):
117
def _get_client(self, thin_packs):
94
118
raise NotImplementedError(self._get_client)
96
123
def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
97
124
if progress is None:
98
125
def progress(text):
99
info("git: %s" % text)
100
client = self._get_client()
126
trace.info("git: %s" % text)
127
client = self._get_client(thin_packs=False)
102
client.fetch_pack(self._path, determine_wants,
129
return client.fetch_pack(self._get_path(), determine_wants,
103
130
graph_walker, pack_data, progress)
104
131
except GitProtocolError, e:
105
132
raise BzrError(e)
134
def send_pack(self, get_changed_refs, generate_pack_contents):
135
client = self._get_client(thin_packs=False)
137
return client.send_pack(self._get_path(), get_changed_refs,
138
generate_pack_contents)
139
except GitProtocolError, e:
107
142
def get(self, path):
108
143
raise NoSuchFile(path)
127
def _get_client(self):
162
def _get_client(self, thin_packs):
128
163
if self._client is not None:
129
164
ret = self._client
130
165
self._client = None
132
return git.client.TCPGitClient(self._host, self._port, thin_packs=False,
133
report_activity=self._report_activity)
167
return git.client.TCPGitClient(self._host, self._port,
168
thin_packs=thin_packs, report_activity=self._report_activity)
136
171
class SSHGitSmartTransport(GitSmartTransport):
138
173
_scheme = 'git+ssh'
140
def _get_client(self):
176
if self._path.startswith("/~/"):
177
return self._path[3:]
180
def _get_client(self, thin_packs):
141
181
if self._client is not None:
142
182
ret = self._client
143
183
self._client = None
145
return git.client.SSHGitClient(self._host, self._port, thin_packs=False,
146
report_activity=self._report_activity)
185
return git.client.SSHGitClient(self._host, self._port, self._username,
186
thin_packs=thin_packs, report_activity=self._report_activity)
149
189
class RemoteGitDir(GitDir):
155
195
self._lockfiles = lockfiles
156
196
self._mode_check_done = None
198
def _branch_name_to_ref(self, name):
199
return branch_name_to_ref(name, default="HEAD")
158
201
def open_repository(self):
159
202
return RemoteGitRepository(self, self._lockfiles)
161
def open_branch(self, ignore_fallbacks=False):
204
def _open_branch(self, name=None, ignore_fallbacks=False,
162
206
repo = self.open_repository()
163
# TODO: Support for multiple branches in one bzrdir in bzrlib!
164
return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
207
refname = self._branch_name_to_ref(name)
208
return RemoteGitBranch(self, repo, refname, self._lockfiles)
166
def open_workingtree(self):
210
def open_workingtree(self, recommend_upgrade=False):
167
211
raise NotLocalUrl(self.transport.base)
180
224
self.resolve_ext_ref = resolve_ext_ref
228
if self._data is None:
229
self._data = ThinPackData(self.resolve_ext_ref, self._data_path)
184
234
if self._idx is None:
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)
235
if not os.path.exists(self._idx_path):
236
pb = ui.ui_factory.nested_progress_bar()
238
def report_progress(cur, total):
239
pb.update("generating index", cur, total)
240
self.data.create_index(self._idx_path,
241
progress=report_progress)
195
244
self._idx = load_pack_index(self._idx_path)
198
247
def __del__(self):
199
os.remove(self._data_path)
200
os.remove(self._idx_path)
248
if self._idx is not None:
250
os.remove(self._idx_path)
251
if self._data is not None:
253
os.remove(self._data_path)
203
256
class RemoteGitRepository(GitRepository):
221
274
def get_refs(self):
222
275
if self._refs is not None:
223
276
return self._refs
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))
277
self._refs = self.bzrdir.root_transport.fetch_pack(lambda x: [], None,
278
lambda x: None, lambda x: trace.mutter("git: %s" % x))
229
279
return self._refs
231
def fetch_pack(self, determine_wants, graph_walker, pack_data,
281
def fetch_pack(self, determine_wants, graph_walker, pack_data,
233
self._transport.fetch_pack(determine_wants, graph_walker, pack_data,
236
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
283
return self._transport.fetch_pack(determine_wants, graph_walker,
286
def send_pack(self, get_changed_refs, generate_pack_contents):
287
return self._transport.send_pack(get_changed_refs, generate_pack_contents)
289
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
237
291
fd, path = tempfile.mkstemp(suffix=".pack")
238
self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
292
self.fetch_pack(determine_wants, graph_walker,
293
lambda x: os.write(fd, x), progress)
240
295
if os.path.getsize(path) == 0:
241
296
return EmptyObjectStoreIterator()
242
297
return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
299
def lookup_bzr_revision_id(self, bzr_revid):
300
# This won't work for any round-tripped bzr revisions, but it's a start..
302
return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
303
except InvalidRevisionId:
304
raise NoSuchRevision(self, bzr_revid)
245
307
class RemoteGitTagDict(tag.BasicTags):
249
311
self.repository = branch.repository
251
313
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)
315
for k, v in extract_tags(self.repository.get_refs()).iteritems():
316
tags[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
260
319
def set_tag(self, name, revid):
261
320
# FIXME: Not supported yet, should do a push of a new ref
265
324
class RemoteGitBranch(GitBranch):
267
326
def __init__(self, bzrdir, repository, name, lockfiles):
268
heads = repository.get_refs()
269
if not name in heads:
270
raise NoSuchRef(name)
271
self._ref = heads[name]
272
super(RemoteGitBranch, self).__init__(bzrdir, repository, name, self._ref, lockfiles)
328
super(RemoteGitBranch, self).__init__(bzrdir, repository, name,
274
331
def revision_history(self):
275
332
raise GitSmartRemoteNotSupported()
277
334
def last_revision(self):
278
return self.mapping.revision_id_foreign_to_bzr(self._ref)
335
return self.mapping.revision_id_foreign_to_bzr(self.head)
337
def _get_config(self):
338
class EmptyConfig(object):
340
def _get_configobj(self):
341
return config.ConfigObj()
347
if self._ref is not None:
349
heads = self.repository.get_refs()
350
if self.name in heads:
351
self._ref = heads[self.name]
352
elif ("refs/heads/" + self.name) in heads:
353
self._ref = heads["refs/heads/" + self.name]
355
raise NoSuchRef(self.name)
280
358
def _synchronize_history(self, destination, revision_id):
281
359
"""See Branch._synchronize_history()."""
282
360
destination.generate_revision_history(self.last_revision())
362
def get_push_location(self):
365
def set_push_location(self, url):