1
# Copyright (C) 2009-2010 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
17
"""Push implementation that simply prints message saying push is not supported."""
19
from dulwich.objects import ZERO_SHA
20
from dulwich.walk import Walker
26
from bzrlib.repository import (
29
from bzrlib.revision import (
33
from bzrlib.plugins.git.errors import (
36
from bzrlib.plugins.git.object_store import (
39
from bzrlib.plugins.git.repository import (
44
from bzrlib.plugins.git.remote import (
47
from bzrlib.plugins.git.unpeel_map import (
52
class MissingObjectsIterator(object):
53
"""Iterate over git objects that are missing from a target repository.
57
def __init__(self, store, source, pb=None):
58
"""Create a new missing objects iterator.
62
self._object_store = store
66
def import_revisions(self, revids, roundtrip):
67
"""Import a set of revisions into this git repository.
69
:param revids: Revision ids of revisions to import
70
:param roundtrip: Whether to roundtrip bzr metadata
72
for i, revid in enumerate(revids):
74
self.pb.update("pushing revisions", i, len(revids))
75
git_commit = self.import_revision(revid, roundtrip)
76
yield (revid, git_commit)
78
def import_revision(self, revid, roundtrip):
79
"""Import a revision into this Git repository.
81
:param revid: Revision id of the revision
82
:param roundtrip: Whether to roundtrip bzr metadata
84
tree = self._object_store.tree_cache.revision_tree(revid)
85
rev = self.source.get_revision(revid)
87
for path, obj, ie in self._object_store._revision_to_objects(rev, tree,
89
if obj.type_name == "commit":
91
self._pending.append((obj, path))
95
return len(self._pending)
98
return iter(self._pending)
101
class InterToGitRepository(InterRepository):
102
"""InterRepository that copies into a Git repository."""
104
_matching_repo_format = GitRepositoryFormat()
106
def __init__(self, source, target):
107
super(InterToGitRepository, self).__init__(source, target)
108
self.mapping = self.target.get_mapping()
109
self.source_store = get_object_store(self.source, self.mapping)
112
def _get_repo_format_to_test():
115
def copy_content(self, revision_id=None, pb=None):
116
"""See InterRepository.copy_content."""
117
self.fetch(revision_id, pb, find_ghosts=False)
119
def fetch_refs(self, update_refs, lossy):
120
"""Fetch possibly roundtripped revisions into the target repository
123
:param update_refs: Generate refs to fetch. Receives dictionary
124
with old refs (git shas), returns dictionary of new names to
126
:param lossy: Whether to roundtrip
127
:return: old refs, new refs
129
raise NotImplementedError(self.fetch_refs)
131
def search_missing_revision_ids(self,
132
find_ghosts=True, revision_ids=None, if_present_ids=None,
137
todo.extend(revision_ids)
139
todo.extend(revision_ids)
140
self.source_store.lock_read()
142
for revid in revision_ids:
143
if revid == NULL_REVISION:
145
git_sha = self.source_store._lookup_revision_sha1(revid)
146
git_shas.append(git_sha)
147
walker = Walker(self.source_store,
148
include=git_shas, exclude=[sha for sha in self.target._git.get_refs().values() if sha != ZERO_SHA])
149
missing_revids = set()
151
for (kind, type_data) in self.source_store.lookup_git_sha(entry.commit.id):
153
missing_revids.add(type_data[0])
155
self.source_store.unlock()
156
return self.source.revision_ids_to_search_result(missing_revids)
159
class InterToLocalGitRepository(InterToGitRepository):
160
"""InterBranch implementation between a Bazaar and a Git repository."""
162
def __init__(self, source, target):
163
super(InterToLocalGitRepository, self).__init__(source, target)
164
self.target_store = self.target._git.object_store
165
self.target_refs = self.target._git.refs
167
def _revision_needs_fetching(self, sha_id, revid):
168
if revid == NULL_REVISION:
172
sha_id = self.source_store._lookup_revision_sha1(revid)
176
return (sha_id not in self.target_store)
177
except errors.NoSuchRevision:
181
def missing_revisions(self, stop_revisions):
182
"""Find the revisions that are missing from the target repository.
184
:param stop_revisions: Revisions to check for (tuples with
186
:return: sequence of missing revisions, in topological order
187
:raise: NoSuchRevision if the stop_revisions are not present in
193
for (sha1, revid) in stop_revisions:
194
if sha1 is not None and revid is not None:
195
revid_sha_map[revid] = sha1
196
elif sha1 is not None:
197
stop_sha1s.append(sha1)
199
assert revid is not None
200
stop_revids.append(revid)
202
graph = self.source.get_graph()
203
pb = ui.ui_factory.nested_progress_bar()
205
for revid, _ in graph.iter_ancestry(stop_revids):
206
assert type(revid) is str
207
pb.update("determining revisions to fetch", len(missing))
208
sha1 = revid_sha_map.get(revid)
209
if self._revision_needs_fetching(sha1, revid):
210
missing.append(revid)
213
for sha1 in stop_sha1s:
215
for (kind, (revid, tree_sha, verifiers)) in self.source_store.lookup_git_sha(sha1):
216
missing.append(revid)
217
revid_sha_map[revid] = sha1
220
return graph.iter_topo_order(missing)
222
def _get_target_bzr_refs(self):
223
"""Return a dictionary with references.
225
:return: Dictionary with reference names as keys and tuples
226
with Git SHA, Bazaar revid as values.
230
for k in self.target._git.refs.allkeys():
231
v = self.target._git.refs.read_ref(k)
233
for (kind, type_data) in self.source_store.lookup_git_sha(v):
234
if kind == "commit" and self.source.has_revision(type_data[0]):
241
bzr_refs[k] = (v, revid)
244
def fetch_refs(self, update_refs, lossy):
245
self.source_store.lock_read()
247
old_refs = self._get_target_bzr_refs()
248
new_refs = update_refs(old_refs)
249
revidmap = self.fetch_objects(new_refs.values(), roundtrip=not lossy)
250
for name, (gitid, revid) in new_refs.iteritems():
253
gitid = revidmap[revid][0]
255
gitid = self.source_store._lookup_revision_sha1(revid)
256
assert len(gitid) == 40 or gitid.startswith('ref: ')
257
self.target_refs[name] = gitid
259
self.source_store.unlock()
260
return revidmap, old_refs, new_refs
262
def fetch_objects(self, revs, roundtrip):
263
todo = list(self.missing_revisions(revs))
265
pb = ui.ui_factory.nested_progress_bar()
267
object_generator = self._get_missing_objects_iterator(pb)
268
for (old_revid, git_sha) in object_generator.import_revisions(
269
todo, roundtrip=roundtrip):
271
self.mapping.revision_id_bzr_to_foreign(old_revid)
272
except errors.InvalidRevisionId:
273
self.target_refs[self.mapping.revid_as_refname(old_revid)] = git_sha
275
new_revid = self.mapping.revision_id_foreign_to_bzr(git_sha)
277
new_revid = old_revid
278
revidmap[old_revid] = (git_sha, new_revid)
279
self.target_store.add_objects(object_generator)
284
def _get_missing_objects_iterator(self, pb):
285
return MissingObjectsIterator(self.source_store, self.source, pb)
287
def dfetch(self, stop_revisions):
288
"""Import the gist of the ancestry of a particular revision."""
290
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
291
fetch_spec=None, mapped_refs=None):
292
if not self.mapping.roundtripping:
293
raise NoPushSupport()
294
self.source_store.lock_read()
296
if mapped_refs is not None:
297
stop_revisions = mapped_refs
298
elif revision_id is not None:
299
stop_revisions = [(None, revision_id)]
300
elif fetch_spec is not None:
301
recipe = fetch_spec.get_recipe()
302
if recipe[0] in ("search", "proxy-search"):
303
stop_revisions = [(None, revid) for revid in recipe[1]]
305
raise AssertionError("Unsupported search result type %s" % recipe[0])
307
stop_revisions = [(None, revid) for revid in self.source.all_revision_ids()]
308
self.fetch_objects(stop_revisions, roundtrip=True)
310
self.source_store.unlock()
313
def is_compatible(source, target):
314
"""Be compatible with GitRepository."""
315
return (not isinstance(source, GitRepository) and
316
isinstance(target, LocalGitRepository))
319
class InterToRemoteGitRepository(InterToGitRepository):
321
def fetch_refs(self, update_refs, lossy):
322
"""Import the gist of the ancestry of a particular revision."""
324
raise NoPushSupport()
325
unpeel_map = UnpeelMap.from_repository(self.source)
327
def determine_wants(old_refs):
329
self.old_refs = dict([(k, (v, None)) for (k, v) in old_refs.iteritems()])
330
self.new_refs = update_refs(self.old_refs)
331
for name, (gitid, revid) in self.new_refs.iteritems():
333
git_sha = self.source_store._lookup_revision_sha1(revid)
334
ret[name] = unpeel_map.re_unpeel_tag(git_sha, old_refs.get(name))
338
self.source_store.lock_read()
340
new_refs = self.target.send_pack(determine_wants,
341
self.source_store.generate_lossy_pack_contents)
343
self.source_store.unlock()
345
return revidmap, self.old_refs, self.new_refs
348
def is_compatible(source, target):
349
"""Be compatible with GitRepository."""
350
return (not isinstance(source, GitRepository) and
351
isinstance(target, RemoteGitRepository))