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 (
26
from bzrlib.plugins.git.converter import (
29
from bzrlib.plugins.git.errors import (
32
from bzrlib.plugins.git.mapping import (
33
inventory_to_tree_and_blobs,
36
from bzrlib.plugins.git.repository import (
42
class MissingObjectsIterator(object):
43
"""Iterate over git objects that are missing from a target repository.
47
def __init__(self, source, mapping):
48
"""Create a new missing objects iterator.
52
self._object_store = BazaarObjectStore(self.source, mapping)
54
self._sent_shas = set()
57
def import_revisions(self, revids):
58
self._revids.update(revids)
59
pb = ui.ui_factory.nested_progress_bar()
61
for i, revid in enumerate(revids):
62
pb.update("pushing revisions", i, len(revids))
63
git_commit = self.import_revision(revid)
64
yield (revid, git_commit)
68
def need_sha(self, sha):
69
if sha in self._sent_shas:
71
(type, (fileid, revid)) = self._object_store._idmap.lookup_git_sha(sha)
72
assert type in ("blob", "tree")
73
if revid in self._revids:
74
# Not sent yet, and part of the set of revisions to send
76
# Not changed in the revisions to send, so either not necessary
77
# or already present remotely (as git doesn't do ghosts)
80
def queue(self, sha, obj, path, ie=None, inv=None):
83
self._pending.append((obj, path))
84
self._sent_shas.add(sha)
86
def import_revision(self, revid):
87
"""Import the gist of a revision into this Git repository.
90
inv = self.source.get_inventory(revid)
95
(sha, object) = self._object_store._get_ie_object_or_sha1(ie, inv)
96
if ie.parent_id is None:
98
if not self.need_sha(sha):
100
self.queue(sha, object, inv.id2path(ie.file_id), ie, inv)
101
if ie.kind == "directory":
102
todo.extend(ie.children.values())
103
assert tree_sha is not None
104
commit = self._object_store._get_commit(revid, tree_sha)
105
self.queue(commit.id, commit, None)
109
return len(self._pending)
112
for (object, path) in self._pending:
113
if isinstance(object, tuple):
114
object = self._object_store._get_ie_object(*object)
118
class InterToGitRepository(InterRepository):
119
"""InterRepository that copies into a Git repository."""
121
_matching_repo_format = GitRepositoryFormat()
124
def _get_repo_format_to_test():
127
def copy_content(self, revision_id=None, pb=None):
128
"""See InterRepository.copy_content."""
129
self.fetch(revision_id, pb, find_ghosts=False)
131
def fetch(self, revision_id=None, pb=None, find_ghosts=False,
133
raise NoPushSupport()
135
def missing_revisions(self, stop_revision=None):
136
if stop_revision is not None:
137
ancestry = [x for x in self.source.get_ancestry(stop_revision) if x is not None]
139
ancestry = self.source.all_revision_ids()
141
graph = self.source.get_graph()
142
for revid in graph.iter_topo_order(ancestry):
143
if not self.target.has_revision(revid):
144
missing.append(revid)
147
def dfetch(self, stop_revision=None):
148
"""Import the gist of the ancestry of a particular revision."""
150
mapping = self.target.get_mapping()
151
self.source.lock_write()
153
todo = self.missing_revisions(stop_revision)
154
object_generator = MissingObjectsIterator(self.source, mapping)
155
for old_bzr_revid, git_commit in object_generator.import_revisions(
157
new_bzr_revid = mapping.revision_id_foreign_to_bzr(git_commit)
158
revidmap[old_bzr_revid] = new_bzr_revid
159
self.target._git.object_store.add_objects(object_generator)
161
self.source.fetch(self.target,
162
revision_id=revidmap[stop_revision])
168
def is_compatible(source, target):
169
"""Be compatible with GitRepository."""
170
return (not isinstance(source, GitRepository) and
171
isinstance(target, GitRepository))