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."""
22
from bzrlib.repository import (
25
from bzrlib.revision import (
29
from bzrlib.plugins.git.converter import (
32
from bzrlib.plugins.git.errors import (
35
from bzrlib.plugins.git.mapping import (
36
inventory_to_tree_and_blobs,
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, source, mapping, pb=None):
55
"""Create a new missing objects iterator.
59
self._object_store = BazaarObjectStore(self.source, mapping)
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 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):
88
self._pending.append((obj, path))
89
self._sent_shas.add(sha)
91
def import_revision(self, revid):
92
"""Import the gist of a revision into this Git repository.
95
inv = self.source.get_inventory(revid)
100
(sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv)
101
if ie.parent_id is None:
103
if not self.need_sha(sha):
105
self.queue(sha, object, inv.id2path(ie.file_id), ie, inv)
106
if ie.kind == "directory":
107
todo.extend(ie.children.values())
108
assert tree_sha is not None
109
commit = self._object_store._get_commit(revid, tree_sha)
110
self.queue(commit.id, commit, None)
114
return len(self._pending)
117
for i, (object, path) in enumerate(self._pending):
119
self.pb.update("writing pack objects", i, len(self))
120
if isinstance(object, tuple):
121
object = self._object_store._get_ie_object(*object)
125
class InterToGitRepository(InterRepository):
126
"""InterRepository that copies into a Git repository."""
128
_matching_repo_format = GitRepositoryFormat()
131
def _get_repo_format_to_test():
134
def copy_content(self, revision_id=None, pb=None):
135
"""See InterRepository.copy_content."""
136
self.fetch(revision_id, pb, find_ghosts=False)
138
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
140
raise NoPushSupport()
143
class InterToLocalGitRepository(InterToGitRepository):
145
def missing_revisions(self, stop_revision):
146
if stop_revision is None:
147
raise NotImplementedError
149
pb = ui.ui_factory.nested_progress_bar()
151
graph = self.source.get_graph()
152
for revid, _ in graph.iter_ancestry([stop_revision]):
153
pb.update("determining revisions to fetch", len(missing))
154
if not self.target.has_revision(revid):
155
missing.append(revid)
156
return graph.iter_topo_order(missing)
160
def dfetch(self, stop_revision=None):
161
"""Import the gist of the ancestry of a particular revision."""
163
mapping = self.target.get_mapping()
164
self.source.lock_read()
166
todo = [revid for revid in self.missing_revisions(stop_revision) if revid != NULL_REVISION]
167
pb = ui.ui_factory.nested_progress_bar()
169
object_generator = MissingObjectsIterator(self.source, mapping, pb)
170
for old_bzr_revid, git_commit in object_generator.import_revisions(
172
new_bzr_revid = mapping.revision_id_foreign_to_bzr(git_commit)
173
revidmap[old_bzr_revid] = new_bzr_revid
174
self.target._git.object_store.add_objects(object_generator)
182
def is_compatible(source, target):
183
"""Be compatible with GitRepository."""
184
return (not isinstance(source, GitRepository) and
185
isinstance(target, LocalGitRepository))
188
class InterToRemoteGitRepository(InterToGitRepository):
190
def dfetch(self, stop_revision=None):
191
"""Import the gist of the ancestry of a particular revision."""
193
mapping = self.target.get_mapping()
194
self.source.lock_read()
196
todo = [revid for revid in self.missing_revisions(stop_revision) if revid != NULL_REVISION]
197
pb = ui.ui_factory.nested_progress_bar()
199
object_generator = MissingObjectsIterator(self.source, mapping, pb)
200
for old_bzr_revid, git_commit in object_generator.import_revisions(
202
new_bzr_revid = mapping.revision_id_foreign_to_bzr(git_commit)
203
revidmap[old_bzr_revid] = new_bzr_revid
204
self.target._git.object_store.add_objects(object_generator)
212
def is_compatible(source, target):
213
"""Be compatible with GitRepository."""
214
return (not isinstance(source, GitRepository) and
215
isinstance(target, RemoteGitRepository))