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 (
45
class MissingObjectsIterator(object):
46
"""Iterate over git objects that are missing from a target repository.
50
def __init__(self, source, mapping, pb=None):
51
"""Create a new missing objects iterator.
55
self._object_store = BazaarObjectStore(self.source, mapping)
57
self._sent_shas = set()
61
def import_revisions(self, revids):
62
self._revids.update(revids)
63
for i, revid in enumerate(revids):
65
self.pb.update("pushing revisions", i, len(revids))
66
git_commit = self.import_revision(revid)
67
yield (revid, git_commit)
69
def need_sha(self, sha):
70
if sha in self._sent_shas:
72
(type, (fileid, revid)) = self._object_store._idmap.lookup_git_sha(sha)
73
assert type in ("blob", "tree")
74
if revid in self._revids:
75
# Not sent yet, and part of the set of revisions to send
77
# Not changed in the revisions to send, so either not necessary
78
# or already present remotely (as git doesn't do ghosts)
81
def queue(self, sha, obj, path, ie=None, inv=None):
84
self._pending.append((obj, path))
85
self._sent_shas.add(sha)
87
def import_revision(self, revid):
88
"""Import the gist of a revision into this Git repository.
91
inv = self.source.get_inventory(revid)
96
(sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv)
97
if ie.parent_id is None:
99
if not self.need_sha(sha):
101
self.queue(sha, object, inv.id2path(ie.file_id), ie, inv)
102
if ie.kind == "directory":
103
todo.extend(ie.children.values())
104
assert tree_sha is not None
105
commit = self._object_store._get_commit(revid, tree_sha)
106
self.queue(commit.id, commit, None)
110
return len(self._pending)
113
for i, (object, path) in enumerate(self._pending):
115
self.pb.update("writing pack objects", i, len(self))
116
if isinstance(object, tuple):
117
object = self._object_store._get_ie_object(*object)
121
class InterToGitRepository(InterRepository):
122
"""InterRepository that copies into a Git repository."""
124
_matching_repo_format = GitRepositoryFormat()
127
def _get_repo_format_to_test():
130
def copy_content(self, revision_id=None, pb=None):
131
"""See InterRepository.copy_content."""
132
self.fetch(revision_id, pb, find_ghosts=False)
134
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
136
raise NoPushSupport()
138
def missing_revisions(self, stop_revision):
139
if stop_revision is None:
140
raise NotImplementedError
142
pb = ui.ui_factory.nested_progress_bar()
144
graph = self.source.get_graph()
145
for revid, _ in graph.iter_ancestry([stop_revision]):
146
pb.update("determining revisions to fetch", len(missing))
147
if not self.target.has_revision(revid):
148
missing.append(revid)
149
return graph.iter_topo_order(missing)
153
def dfetch(self, stop_revision=None):
154
"""Import the gist of the ancestry of a particular revision."""
156
mapping = self.target.get_mapping()
157
self.source.lock_read()
159
todo = [revid for revid in self.missing_revisions(stop_revision) if revid != NULL_REVISION]
160
pb = ui.ui_factory.nested_progress_bar()
162
object_generator = MissingObjectsIterator(self.source, mapping, pb)
163
for old_bzr_revid, git_commit in object_generator.import_revisions(
165
new_bzr_revid = mapping.revision_id_foreign_to_bzr(git_commit)
166
revidmap[old_bzr_revid] = new_bzr_revid
167
self.target._git.object_store.add_objects(object_generator)
175
def is_compatible(source, target):
176
"""Be compatible with GitRepository."""
177
return (not isinstance(source, GitRepository) and
178
isinstance(target, GitRepository))