/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to fetch.py

Return mapping in revision_id_bzr_to_foreign() as required by the interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
from bzrlib import osutils, ui, urlutils
18
 
from bzrlib.errors import InvalidRevisionId, NoSuchRevision
 
18
from bzrlib.errors import InvalidRevisionId
19
19
from bzrlib.inventory import Inventory
20
20
from bzrlib.repository import InterRepository
21
21
from bzrlib.trace import info
26
26
        GitRepository, 
27
27
        GitFormat,
28
28
        )
29
 
from bzrlib.plugins.git.shamap import GitObjectConverter
30
29
from bzrlib.plugins.git.remote import RemoteGitRepository
31
30
 
32
31
import dulwich as git
37
36
 
38
37
 
39
38
class BzrFetchGraphWalker(object):
40
 
    """GraphWalker implementation that uses a Bazaar repository."""
41
39
 
42
40
    def __init__(self, repository, mapping):
43
41
        self.repository = repository
46
44
        self.heads = set(repository.all_revision_ids())
47
45
        self.parents = {}
48
46
 
49
 
    def __iter__(self):
50
 
        return iter(self.next, None)
51
 
 
52
47
    def ack(self, sha):
53
48
        revid = self.mapping.revision_id_foreign_to_bzr(sha)
54
49
        self.remove(revid)
127
122
            raise AssertionError("Unknown blob kind, perms=%r." % (mode,))
128
123
 
129
124
 
130
 
def import_git_objects(repo, mapping, object_iter, target_git_object_retriever, 
131
 
        pb=None):
 
125
def import_git_objects(repo, mapping, num_objects, object_iter, pb=None):
132
126
    """Import a set of git objects into a bzr repository.
133
127
 
134
128
    :param repo: Bazaar repository
135
129
    :param mapping: Mapping to use
 
130
    :param num_objects: Number of objects.
136
131
    :param object_iter: Iterator over Git objects.
137
132
    """
138
133
    # TODO: a more (memory-)efficient implementation of this
 
134
    objects = {}
 
135
    for i, (o, _) in enumerate(object_iter):
 
136
        if pb is not None:
 
137
            pb.update("fetching objects", i, num_objects) 
 
138
        objects[o.id] = o
139
139
    graph = []
140
140
    root_trees = {}
141
141
    revisions = {}
142
142
    # Find and convert commit objects
143
 
    for o in object_iter.iterobjects():
 
143
    for o in objects.itervalues():
144
144
        if isinstance(o, Commit):
145
145
            rev = mapping.import_commit(o)
146
 
            root_trees[rev.revision_id] = object_iter[o.tree]
 
146
            root_trees[rev.revision_id] = objects[o.tree]
147
147
            revisions[rev.revision_id] = rev
148
148
            graph.append((rev.revision_id, rev.parent_ids))
149
149
    # Order the revisions
159
159
        inv = Inventory()
160
160
        inv.revision_id = rev.revision_id
161
161
        def lookup_object(sha):
162
 
            if sha in object_iter:
163
 
                return object_iter[sha]
164
 
            return target_git_object_retriever(sha)
 
162
            if sha in objects:
 
163
                return objects[sha]
 
164
            return reconstruct_git_object(repo, mapping, sha)
165
165
        parent_invs = [repo.get_inventory(r) for r in rev.parent_ids]
166
166
        import_git_tree(repo, mapping, "", root_tree, inv, parent_invs, 
167
167
            lookup_object)
168
168
        repo.add_revision(rev.revision_id, rev, inv)
169
169
 
170
170
 
 
171
def reconstruct_git_commit(repo, rev):
 
172
    raise NotImplementedError(self.reconstruct_git_commit)
 
173
 
 
174
 
171
175
def reconstruct_git_object(repo, mapping, sha):
172
 
    import pdb; pdb.set_trace()
 
176
    # Commit
 
177
    revid = mapping.revision_id_foreign_to_bzr(sha)
 
178
    try:
 
179
        rev = repo.get_revision(revid)
 
180
    except NoSuchRevision:
 
181
        pass
 
182
    else:
 
183
        return reconstruct_git_commit(rev)
173
184
 
174
185
    # TODO: Tree
175
186
    # TODO: Blob
188
199
        """See InterRepository.copy_content."""
189
200
        self.fetch(revision_id, pb, find_ghosts=False)
190
201
 
191
 
    def fetch_objects(self, determine_wants, mapping, pb=None):
 
202
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
 
203
              mapping=None):
 
204
        if mapping is None:
 
205
            mapping = self.source.get_mapping()
192
206
        def progress(text):
193
207
            pb.update("git: %s" % text.rstrip("\r\n"), 0, 0)
 
208
        def determine_wants(heads):
 
209
            if revision_id is None:
 
210
                ret = heads.values()
 
211
            else:
 
212
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
 
213
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
194
214
        graph_walker = BzrFetchGraphWalker(self.target, mapping)
195
215
        create_pb = None
196
216
        if pb is None:
197
217
            create_pb = pb = ui.ui_factory.nested_progress_bar()
198
 
        target_git_object_retriever = GitObjectConverter(self.target, mapping)
199
 
        
200
218
        try:
201
219
            self.target.lock_write()
202
220
            try:
203
221
                self.target.start_write_group()
204
222
                try:
205
 
                    objects_iter = self.source.fetch_objects(determine_wants, 
206
 
                                graph_walker, 
207
 
                                target_git_object_retriever.__getitem__, 
208
 
                                progress)
209
 
                    import_git_objects(self.target, mapping, objects_iter, 
210
 
                            target_git_object_retriever, pb)
 
223
                    (num_objects, objects_iter) = \
 
224
                            self.source.fetch_objects(determine_wants, 
 
225
                                graph_walker, progress)
 
226
                    import_git_objects(self.target, mapping, num_objects, 
 
227
                                       objects_iter, pb)
211
228
                finally:
212
229
                    self.target.commit_write_group()
213
230
            finally:
216
233
            if create_pb:
217
234
                create_pb.finished()
218
235
 
219
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
220
 
              mapping=None):
221
 
        if mapping is None:
222
 
            mapping = self.source.get_mapping()
223
 
        def determine_wants(heads):
224
 
            if revision_id is None:
225
 
                ret = heads.values()
226
 
            else:
227
 
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
228
 
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
229
 
        return self.fetch_objects(determine_wants, mapping, pb)
230
 
 
231
236
    @staticmethod
232
237
    def is_compatible(source, target):
233
238
        """Be compatible with GitRepository."""