1
# Copyright (C) 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
17
"""Push implementation that simply prints message saying push is not supported."""
23
from bzrlib.repository import (
26
from bzrlib.revision import (
30
from bzrlib.plugins.git.errors import (
33
from bzrlib.plugins.git.mapping import (
34
extract_unusual_modes,
36
from bzrlib.plugins.git.object_store import (
39
from bzrlib.plugins.git.repository import (
44
from bzrlib.plugins.git.remote import (
49
class MissingObjectsIterator(object):
50
"""Iterate over git objects that are missing from a target repository.
54
def __init__(self, store, source, pb=None):
55
"""Create a new missing objects iterator.
59
self._object_store = store
61
self._sent_shas = set()
65
def import_revisions(self, revids):
66
self._revids.update(revids)
67
for i, revid in enumerate(revids):
69
self.pb.update("pushing revisions", i, len(revids))
70
git_commit = self.import_revision(revid)
71
yield (revid, git_commit)
73
def need_sha(self, sha):
74
if sha is None or sha in self._sent_shas:
76
(type, (fileid, revid)) = self._object_store._idmap.lookup_git_sha(sha)
77
assert type in ("blob", "tree")
78
if revid in self._revids:
79
# Not sent yet, and part of the set of revisions to send
81
# Not changed in the revisions to send, so either not necessary
82
# or already present remotely (as git doesn't do ghosts)
85
def queue(self, sha, obj, path, ie=None, inv=None, unusual_modes=None):
87
# Can't lazy-evaluate directories, since they might be eliminated
88
if ie.kind == "directory":
89
obj = self._object_store._get_ie_object(ie, inv, unusual_modes)
93
obj = (ie, inv, unusual_modes)
94
self._pending.append((obj, path))
95
self._sent_shas.add(sha)
97
def import_revision(self, revid):
98
"""Import the gist of a revision into this Git repository.
101
inv = self.source.get_inventory(revid)
102
rev = self.source.get_revision(revid)
103
invshamap = self._object_store._idmap.get_inventory_sha_map(revid)
104
unusual_modes = extract_unusual_modes(rev)
109
(sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv, invshamap, unusual_modes)
110
if ie.parent_id is None:
112
if not self.need_sha(sha):
114
self.queue(sha, object, inv.id2path(ie.file_id), ie, inv, unusual_modes)
115
if ie.kind == "directory":
116
todo.extend(ie.children.values())
117
assert tree_sha is not None
118
commit = self._object_store._get_commit(rev, tree_sha)
119
self.queue(commit.id, commit, None, None)
123
return len(self._pending)
126
for i, (object, path) in enumerate(self._pending):
128
self.pb.update("writing pack objects", i, len(self))
129
if isinstance(object, tuple):
130
object = self._object_store._get_ie_object(*object)
134
class InterToGitRepository(InterRepository):
135
"""InterRepository that copies into a Git repository."""
137
_matching_repo_format = GitRepositoryFormat()
139
def __init__(self, source, target):
140
super(InterToGitRepository, self).__init__(source, target)
141
self.mapping = self.target.get_mapping()
142
self.source_store = BazaarObjectStore(self.source, self.mapping)
145
def _get_repo_format_to_test():
148
def copy_content(self, revision_id=None, pb=None):
149
"""See InterRepository.copy_content."""
150
self.fetch(revision_id, pb, find_ghosts=False)
152
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
154
raise NoPushSupport()
157
class InterToLocalGitRepository(InterToGitRepository):
159
def missing_revisions(self, stop_revisions, check_revid):
161
pb = ui.ui_factory.nested_progress_bar()
163
graph = self.source.get_graph()
164
for revid, _ in graph.iter_ancestry(stop_revisions):
165
pb.update("determining revisions to fetch", len(missing))
166
if not check_revid(revid):
167
missing.append(revid)
168
return graph.iter_topo_order(missing)
172
def dfetch_refs(self, refs):
174
revidmap, gitidmap = self.dfetch(refs.values())
175
for name, revid in refs.iteritems():
176
if revid in gitidmap:
177
gitid = gitidmap[revid]
179
gitid = self.source_store._lookup_revision_sha1(revid)
180
self.target._git.refs[name] = gitid
181
new_refs[name] = gitid
182
return revidmap, new_refs
184
def dfetch(self, stop_revisions):
185
"""Import the gist of the ancestry of a particular revision."""
188
self.source.lock_read()
190
target_store = self.target._git.object_store
191
def check_revid(revid):
192
if revid == NULL_REVISION:
195
return (self.source_store._lookup_revision_sha1(revid) in target_store)
196
except errors.NoSuchRevision:
199
todo = list(self.missing_revisions(stop_revisions, check_revid))
200
pb = ui.ui_factory.nested_progress_bar()
202
object_generator = MissingObjectsIterator(self.source_store, self.source, pb)
203
for old_bzr_revid, git_commit in object_generator.import_revisions(
205
new_bzr_revid = self.mapping.revision_id_foreign_to_bzr(git_commit)
206
revidmap[old_bzr_revid] = new_bzr_revid
207
gitidmap[old_bzr_revid] = git_commit
208
target_store.add_objects(object_generator)
213
return revidmap, gitidmap
216
def is_compatible(source, target):
217
"""Be compatible with GitRepository."""
218
return (not isinstance(source, GitRepository) and
219
isinstance(target, LocalGitRepository))
222
class InterToRemoteGitRepository(InterToGitRepository):
224
def dfetch_refs(self, new_refs):
225
"""Import the gist of the ancestry of a particular revision."""
227
def determine_wants(refs):
229
for name, revid in new_refs.iteritems():
230
ret[name] = self.source_store._lookup_revision_sha1(revid)
232
self.source.lock_read()
234
new_refs = self.target.send_pack(determine_wants,
235
self.source_store.generate_pack_contents)
238
return revidmap, new_refs
241
def is_compatible(source, target):
242
"""Be compatible with GitRepository."""
243
return (not isinstance(source, GitRepository) and
244
isinstance(target, RemoteGitRepository))