76
from dulwich.pack import load_pack_index
78
from dulwich.pack import PackIndex as load_pack_index
81
# Don't run any tests on GitSmartTransport as it is not intended to be
71
urlparse.uses_netloc.extend(['git', 'git+ssh'])
73
from dulwich.pack import load_pack_index
76
# Don't run any tests on GitSmartTransport as it is not intended to be
82
77
# a full implementation of Transport
83
78
def get_test_permutations():
82
def split_git_url(url):
86
:return: Tuple with host, port, username, path.
88
(scheme, netloc, loc, _, _) = urlparse.urlsplit(url)
89
path = urllib.unquote(loc)
90
if path.startswith("/~"):
92
(username, hostport) = urllib.splituser(netloc)
93
(host, port) = urllib.splitnport(hostport, None)
94
return (host, port, username, path)
87
97
class GitSmartTransport(Transport):
89
99
def __init__(self, url, _client=None):
90
100
Transport.__init__(self, url)
91
(scheme, _, loc, _, _) = urlparse.urlsplit(url)
92
hostport, self._path = urllib.splithost(loc)
93
(self._username, hostport) = urllib.splituser(hostport)
94
(self._host, self._port) = urllib.splitnport(hostport, None)
101
(self._host, self._port, self._username, self._path) = \
103
if 'transport' in debug.debug_flags:
104
trace.mutter('host: %r, user: %r, port: %r, path: %r',
105
self._host, self._username, self._port, self._path)
95
106
self._client = _client
97
108
def external_url(self):
109
120
def fetch_pack(self, determine_wants, graph_walker, pack_data, progress=None):
110
121
if progress is None:
111
122
def progress(text):
112
info("git: %s" % text)
113
client = self._get_client()
123
trace.info("git: %s" % text)
124
client = self._get_client(thin_packs=False)
115
return client.fetch_pack(self._get_path(), determine_wants,
126
return client.fetch_pack(self._get_path(), determine_wants,
116
127
graph_walker, pack_data, progress)
117
128
except GitProtocolError, e:
118
129
raise BzrError(e)
120
131
def send_pack(self, get_changed_refs, generate_pack_contents):
121
client = self._get_client()
132
client = self._get_client(thin_packs=False)
123
return client.send_pack(self._get_path(), get_changed_refs,
134
return client.send_pack(self._get_path(), get_changed_refs,
124
135
generate_pack_contents)
125
136
except GitProtocolError, e:
126
137
raise BzrError(e)
148
def _get_client(self):
159
def _get_client(self, thin_packs):
149
160
if self._client is not None:
150
161
ret = self._client
151
162
self._client = None
153
return git.client.TCPGitClient(self._host, self._port, thin_packs=False,
164
return git.client.TCPGitClient(self._host, self._port, thin_packs=thin_packs,
154
165
report_activity=self._report_activity)
163
174
return self._path[3:]
164
175
return self._path
166
def _get_client(self):
177
def _get_client(self, thin_packs):
167
178
if self._client is not None:
168
179
ret = self._client
169
180
self._client = None
171
182
return git.client.SSHGitClient(self._host, self._port, self._username,
172
thin_packs=False, report_activity=self._report_activity)
183
thin_packs=thin_packs, report_activity=self._report_activity)
175
186
class RemoteGitDir(GitDir):
184
195
def open_repository(self):
185
196
return RemoteGitRepository(self, self._lockfiles)
187
def open_branch(self, ignore_fallbacks=False):
198
def _open_branch(self, name=None, ignore_fallbacks=False,
188
200
repo = self.open_repository()
189
# TODO: Support for multiple branches in one bzrdir in bzrlib!
190
return RemoteGitBranch(self, repo, "HEAD", self._lockfiles)
201
refname = self._branch_name_to_ref(name)
202
return RemoteGitBranch(self, repo, refname, self._lockfiles)
192
def open_workingtree(self):
204
def open_workingtree(self, recommend_upgrade=False):
193
205
raise NotLocalUrl(self.transport.base)
206
218
self.resolve_ext_ref = resolve_ext_ref
222
if self._data is None:
223
self._data = PackData(self._data_path)
210
228
if self._idx is None:
211
pb = ui.ui_factory.nested_progress_bar()
213
def report_progress(cur, total):
214
pb.update("generating index", cur, total)
215
self.data.create_index(self._idx_path, self.resolve_ext_ref,
216
progress=report_progress)
229
if not os.path.exists(self._idx_path):
230
pb = ui.ui_factory.nested_progress_bar()
232
def report_progress(cur, total):
233
pb.update("generating index", cur, total)
234
self.data.create_index(self._idx_path, self.resolve_ext_ref,
235
progress=report_progress)
219
238
self._idx = load_pack_index(self._idx_path)
222
241
def __del__(self):
223
os.remove(self._data_path)
224
os.remove(self._idx_path)
242
if self._idx is not None:
244
os.remove(self._idx_path)
245
if self._data is not None:
247
os.remove(self._data_path)
227
250
class RemoteGitRepository(GitRepository):
245
268
def get_refs(self):
246
269
if self._refs is not None:
247
270
return self._refs
248
self._refs = self.bzrdir.root_transport.fetch_pack(lambda x: [], None,
249
lambda x: None, lambda x: mutter("git: %s" % x))
271
self._refs = self.bzrdir.root_transport.fetch_pack(lambda x: [], None,
272
lambda x: None, lambda x: trace.mutter("git: %s" % x))
250
273
return self._refs
252
def fetch_pack(self, determine_wants, graph_walker, pack_data,
275
def fetch_pack(self, determine_wants, graph_walker, pack_data,
254
277
return self._transport.fetch_pack(determine_wants, graph_walker,
255
278
pack_data, progress)
257
280
def send_pack(self, get_changed_refs, generate_pack_contents):
258
281
return self._transport.send_pack(get_changed_refs, generate_pack_contents)
260
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
283
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
261
285
fd, path = tempfile.mkstemp(suffix=".pack")
262
self.fetch_pack(determine_wants, graph_walker, lambda x: os.write(fd, x), progress)
286
self.fetch_pack(determine_wants, graph_walker,
287
lambda x: os.write(fd, x), progress)
264
289
if os.path.getsize(path) == 0:
265
290
return EmptyObjectStoreIterator()
266
291
return TemporaryPackIterator(path[:-len(".pack")], resolve_ext_ref)
268
def lookup_git_revid(self, bzr_revid):
293
def lookup_bzr_revision_id(self, bzr_revid):
269
294
# This won't work for any round-tripped bzr revisions, but it's a start..
271
296
return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
280
305
self.repository = branch.repository
282
307
def get_tag_dict(self):
283
return extract_tags(self.repository.get_refs(), self.branch.mapping)
309
for k, v in extract_tags(self.repository.get_refs()).iteritems():
310
tags[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
285
313
def set_tag(self, name, revid):
286
314
# FIXME: Not supported yet, should do a push of a new ref
292
320
def __init__(self, bzrdir, repository, name, lockfiles):
294
super(RemoteGitBranch, self).__init__(bzrdir, repository, name,
322
super(RemoteGitBranch, self).__init__(bzrdir, repository, name,
297
325
def revision_history(self):
300
328
def last_revision(self):
301
329
return self.mapping.revision_id_foreign_to_bzr(self.head)
331
def _get_config(self):
332
class EmptyConfig(object):
334
def _get_configobj(self):
335
return config.ConfigObj()
305
341
if self._ref is not None:
307
343
heads = self.repository.get_refs()
308
if not self.name in heads:
309
raise NoSuchRef(name)
310
self._ref = heads[self.name]
344
if self.name in heads:
345
self._ref = heads[self.name]
346
elif ("refs/heads/" + self.name) in heads:
347
self._ref = heads["refs/heads/" + self.name]
349
raise NoSuchRef(self.name)
313
352
def _synchronize_history(self, destination, revision_id):
314
353
"""See Branch._synchronize_history()."""
315
354
destination.generate_revision_history(self.last_revision())
317
356
def get_push_location(self):