/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

Fix branch cloning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib.trace import info
22
22
from bzrlib.tsort import topo_sort
23
23
 
 
24
from bzrlib.plugins.git import git
24
25
from bzrlib.plugins.git.repository import (
25
26
        LocalGitRepository, 
26
27
        GitRepository, 
28
29
        )
29
30
from bzrlib.plugins.git.remote import RemoteGitRepository
30
31
 
31
 
import dulwich as git
32
 
from dulwich.client import SimpleFetchGraphWalker
33
32
from dulwich.objects import Commit
34
33
 
35
34
from cStringIO import StringIO
36
35
 
37
36
 
38
37
class BzrFetchGraphWalker(object):
39
 
    """GraphWalker implementation that uses a Bazaar repository."""
40
38
 
41
39
    def __init__(self, repository, mapping):
42
40
        self.repository = repository
45
43
        self.heads = set(repository.all_revision_ids())
46
44
        self.parents = {}
47
45
 
48
 
    def __iter__(self):
49
 
        return iter(self.next, None)
50
 
 
51
46
    def ack(self, sha):
52
47
        revid = self.mapping.revision_id_foreign_to_bzr(sha)
53
48
        self.remove(revid)
54
49
 
55
50
    def remove(self, revid):
56
51
        self.done.add(revid)
57
 
        if revid in self.heads:
 
52
        if ref in self.heads:
58
53
            self.heads.remove(revid)
59
54
        if revid in self.parents:
60
55
            for p in self.parents[revid]:
68
63
            self.heads.update([p for p in ps if not p in self.done])
69
64
            try:
70
65
                self.done.add(ret)
71
 
                return self.mapping.revision_id_bzr_to_foreign(ret)[0]
 
66
                return self.mapping.revision_id_bzr_to_foreign(ret)
72
67
            except InvalidRevisionId:
73
68
                pass
74
69
        return None
126
121
            raise AssertionError("Unknown blob kind, perms=%r." % (mode,))
127
122
 
128
123
 
129
 
def import_git_objects(repo, mapping, num_objects, object_iter, pb=None):
 
124
def import_git_objects(repo, mapping, object_iter, pb=None):
130
125
    """Import a set of git objects into a bzr repository.
131
126
 
132
127
    :param repo: Bazaar repository
133
128
    :param mapping: Mapping to use
134
 
    :param num_objects: Number of objects.
135
129
    :param object_iter: Iterator over Git objects.
136
130
    """
137
131
    # TODO: a more (memory-)efficient implementation of this
138
132
    objects = {}
139
 
    for i, (o, _) in enumerate(object_iter):
 
133
    for i, o in enumerate(object_iter):
140
134
        if pb is not None:
141
 
            pb.update("fetching objects", i, num_objects) 
 
135
            pb.update("fetching objects", i) 
142
136
        objects[o.id] = o
143
137
    graph = []
144
138
    root_trees = {}
167
161
                return objects[sha]
168
162
            return reconstruct_git_object(repo, mapping, sha)
169
163
        parent_invs = [repo.get_inventory(r) for r in rev.parent_ids]
170
 
        import_git_tree(repo, mapping, "", root_tree, inv, parent_invs, 
171
 
            lookup_object)
 
164
        import_git_tree(repo, mapping, "", root_tree, inv, parent_invs, lookup_object)
172
165
        repo.add_revision(rev.revision_id, rev, inv)
173
166
 
174
167
 
191
184
    raise KeyError("No such object %s" % sha)
192
185
 
193
186
 
194
 
class InterGitNonGitRepository(InterRepository):
 
187
class InterGitRepository(InterRepository):
195
188
 
196
189
    _matching_repo_format = GitFormat()
197
190
 
208
201
        if mapping is None:
209
202
            mapping = self.source.get_mapping()
210
203
        def progress(text):
211
 
            pb.update("git: %s" % text.rstrip("\r\n"), 0, 0)
 
204
            pb.note("git: %s", text)
212
205
        def determine_wants(heads):
213
206
            if revision_id is None:
214
207
                ret = heads.values()
215
208
            else:
216
 
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
 
209
                ret = [mapping.revision_id_bzr_to_foreign(revision_id)]
217
210
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
218
211
        graph_walker = BzrFetchGraphWalker(self.target, mapping)
219
212
        create_pb = None
224
217
            try:
225
218
                self.target.start_write_group()
226
219
                try:
227
 
                    (num_objects, objects_iter) = \
228
 
                            self.source.fetch_objects(determine_wants, 
229
 
                                graph_walker, progress)
230
 
                    import_git_objects(self.target, mapping, num_objects, 
231
 
                                       objects_iter, pb)
 
220
                    import_git_objects(self.target, mapping,
 
221
                        iter(self.source.fetch_objects(determine_wants, graph_walker, 
 
222
                            progress)), pb)
232
223
                finally:
233
224
                    self.target.commit_write_group()
234
225
            finally:
242
233
        """Be compatible with GitRepository."""
243
234
        # FIXME: Also check target uses VersionedFile
244
235
        return (isinstance(source, GitRepository) and 
245
 
                target.supports_rich_root() and
246
 
                not isinstance(target, GitRepository))
247
 
 
248
 
 
249
 
class InterGitRepository(InterRepository):
250
 
 
251
 
    _matching_repo_format = GitFormat()
252
 
 
253
 
    @staticmethod
254
 
    def _get_repo_format_to_test():
255
 
        return None
256
 
 
257
 
    def copy_content(self, revision_id=None, pb=None):
258
 
        """See InterRepository.copy_content."""
259
 
        self.fetch(revision_id, pb, find_ghosts=False)
260
 
 
261
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False, 
262
 
              mapping=None):
263
 
        if mapping is None:
264
 
            mapping = self.source.get_mapping()
265
 
        def progress(text):
266
 
            info("git: %s", text)
267
 
        r = self.target._git
268
 
        if revision_id is None:
269
 
            determine_wants = lambda x: [y for y in x.values() if not y in r.object_store]
270
 
        else:
271
 
            args = [mapping.revision_id_bzr_to_foreign(revision_id)[0]]
272
 
            determine_wants = lambda x: [y for y in args if not y in r.object_store]
273
 
 
274
 
        graphwalker = SimpleFetchGraphWalker(r.heads().values(), r.get_parents)
275
 
        f, commit = r.object_store.add_pack()
276
 
        try:
277
 
            self.source._git.fetch_pack(path, determine_wants, graphwalker, f.write, progress)
278
 
            f.close()
279
 
            commit()
280
 
        except:
281
 
            f.close()
282
 
            raise
283
 
 
284
 
    @staticmethod
285
 
    def is_compatible(source, target):
286
 
        """Be compatible with GitRepository."""
287
 
        return (isinstance(source, GitRepository) and 
288
 
                isinstance(target, GitRepository))
 
236
                target.supports_rich_root())