/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

Update docs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from dulwich.objects import (
22
22
    Commit,
23
23
    Tag,
24
 
    S_ISGITLINK,
25
24
    )
26
25
from dulwich.object_store import (
27
26
    tree_lookup_path,
65
64
from bzrlib.plugins.git.mapping import (
66
65
    DEFAULT_FILE_MODE,
67
66
    inventory_to_tree_and_blobs,
68
 
    mode_is_executable,
69
 
    squash_revision,
70
67
    text_to_blob,
71
 
    warn_unusual_mode,
72
68
    )
73
69
from bzrlib.plugins.git.object_store import (
74
70
    BazaarObjectStore,
83
79
    )
84
80
 
85
81
 
 
82
class BzrFetchGraphWalker(object):
 
83
    """GraphWalker implementation that uses a Bazaar repository."""
 
84
 
 
85
    def __init__(self, repository, mapping):
 
86
        self.repository = repository
 
87
        self.mapping = mapping
 
88
        self.done = set()
 
89
        self.heads = set(repository.all_revision_ids())
 
90
        self.parents = {}
 
91
 
 
92
    def __iter__(self):
 
93
        return iter(self.next, None)
 
94
 
 
95
    def ack(self, sha):
 
96
        revid = self.mapping.revision_id_foreign_to_bzr(sha)
 
97
        self.remove(revid)
 
98
 
 
99
    def remove(self, revid):
 
100
        self.done.add(revid)
 
101
        if revid in self.heads:
 
102
            self.heads.remove(revid)
 
103
        if revid in self.parents:
 
104
            for p in self.parents[revid]:
 
105
                self.remove(p)
 
106
 
 
107
    def next(self):
 
108
        while self.heads:
 
109
            ret = self.heads.pop()
 
110
            ps = self.repository.get_parent_map([ret])[ret]
 
111
            self.parents[ret] = ps
 
112
            self.heads.update([p for p in ps if not p in self.done])
 
113
            try:
 
114
                self.done.add(ret)
 
115
                return self.mapping.revision_id_bzr_to_foreign(ret)[0]
 
116
            except InvalidRevisionId:
 
117
                pass
 
118
        return None
 
119
 
 
120
 
86
121
def import_git_blob(texts, mapping, path, hexsha, base_inv, parent_id, 
87
122
    revision_id, parent_invs, shagitmap, lookup_object, executable, symlink):
88
123
    """Import a git blob object into a bzr repository.
100
135
    # We just have to hope this is indeed utf-8:
101
136
    ie = cls(file_id, urlutils.basename(path).decode("utf-8"), parent_id)
102
137
    ie.executable = executable
 
138
    ie.text_id = hexsha
103
139
    # See if this has changed at all
104
140
    try:
105
141
        base_ie = base_inv[file_id]
107
143
        base_ie = None
108
144
        base_sha = None
109
145
    else:
 
146
        base_sha = base_ie.text_id
110
147
        try:
111
 
            base_sha = shagitmap.lookup_blob(file_id, base_ie.revision)
 
148
            if base_sha is None:
 
149
                base_sha = shagitmap.lookup_blob(file_id, base_ie.revision)
112
150
        except KeyError:
113
151
            base_sha = None
114
152
        else:
116
154
                and base_ie.kind == ie.kind):
117
155
                # If nothing has changed since the base revision, we're done
118
156
                return [], []
119
 
    if base_sha == hexsha and base_ie.kind == ie.kind:
 
157
    if base_sha == hexsha:
120
158
        ie.text_size = base_ie.text_size
121
159
        ie.text_sha1 = base_ie.text_sha1
122
160
        ie.symlink_target = base_ie.symlink_target
123
 
        if ie.executable == base_ie.executable:
124
 
            ie.revision = base_ie.revision
125
 
        else:
126
 
            blob = lookup_object(hexsha)
 
161
        ie.revision = base_ie.revision
127
162
    else:
128
163
        blob = lookup_object(hexsha)
129
164
        if ie.kind == "symlink":
130
 
            ie.revision = None
131
165
            ie.symlink_target = blob.data
132
166
            ie.text_size = None
133
167
            ie.text_sha1 = None
146
180
                pie = pinv[file_id]
147
181
            except NoSuchId:
148
182
                continue
149
 
        if pie.text_sha1 == ie.text_sha1 and pie.executable == ie.executable and pie.symlink_target == ie.symlink_target:
 
183
        if pie.text_sha1 == ie.text_sha1:
150
184
            # found a revision in one of the parents to use
151
185
            ie.revision = pie.revision
152
186
            break
164
198
        old_path = base_inv.id2path(file_id)
165
199
    else:
166
200
        old_path = None
167
 
    invdelta = [(old_path, path, file_id, ie)]
168
 
    invdelta.extend(remove_disappeared_children(base_inv, base_ie, []))
169
 
    return (invdelta, shamap)
170
 
 
171
 
 
172
 
def import_git_submodule(texts, mapping, path, hexsha, base_inv, parent_id, 
173
 
    revision_id, parent_invs, shagitmap, lookup_object):
174
 
    raise NotImplementedError(import_git_submodule)
175
 
 
176
 
 
177
 
def remove_disappeared_children(base_inv, base_ie, existing_children):
178
 
    if base_ie is None or base_ie.kind != 'directory':
179
 
        return []
180
 
    ret = []
181
 
    deletable = [v for k,v in base_ie.children.iteritems() if k not in existing_children]
182
 
    while deletable:
183
 
        ie = deletable.pop()
184
 
        ret.append((base_inv.id2path(ie.file_id), None, ie.file_id, None))
185
 
        if ie.kind == "directory":
186
 
            deletable.extend(ie.children.values())
187
 
    return ret
 
201
    return ([(old_path, path, file_id, ie)], shamap)
188
202
 
189
203
 
190
204
def import_git_tree(texts, mapping, path, hexsha, base_inv, parent_id, 
202
216
    # We just have to hope this is indeed utf-8:
203
217
    ie = InventoryDirectory(file_id, urlutils.basename(path.decode("utf-8")), 
204
218
        parent_id)
 
219
    ie.text_id = hexsha
205
220
    try:
206
221
        base_ie = base_inv[file_id]
207
222
    except NoSuchId:
211
226
        texts.add_lines((file_id, ie.revision), (), [])
212
227
        invdelta.append((None, path, file_id, ie))
213
228
    else:
 
229
        base_sha = base_ie.text_id
214
230
        # See if this has changed at all
215
231
        try:
216
 
            base_sha = shagitmap.lookup_tree(file_id, base_inv.revision_id)
 
232
            if base_sha is None:
 
233
                base_sha = shagitmap.lookup_tree(file_id, base_inv.revision_id)
217
234
        except KeyError:
218
235
            pass
219
236
        else:
220
237
            if base_sha == hexsha:
221
238
                # If nothing has changed since the base revision, we're done
222
239
                return [], {}, []
223
 
        if base_ie.kind != "directory":
224
 
            ie.revision = revision_id
225
 
            texts.add_lines((ie.file_id, ie.revision), (), [])
226
 
            invdelta.append((base_inv.id2path(ie.file_id), path, ie.file_id, ie))
227
240
    # Remember for next time
228
241
    existing_children = set()
229
242
    child_modes = {}
234
247
        existing_children.add(basename)
235
248
        child_path = osutils.pathjoin(path, name)
236
249
        if stat.S_ISDIR(mode):
237
 
            subinvdelta, grandchildmodes, subshamap = import_git_tree(
238
 
                    texts, mapping, child_path, child_hexsha, base_inv, 
239
 
                    file_id, revision_id, parent_invs, shagitmap, lookup_object)
240
 
            invdelta.extend(subinvdelta)
241
 
            child_modes.update(grandchildmodes)
242
 
            shamap.extend(subshamap)
243
 
        elif S_ISGITLINK(mode): # submodule
244
 
            subinvdelta, grandchildmodes, subshamap = import_git_submodule(
245
 
                    texts, mapping, child_path, child_hexsha, base_inv,
246
 
                    file_id, revision_id, parent_invs, shagitmap, lookup_object)
 
250
            subinvdelta, grandchildmodes, subshamap = import_git_tree(texts, 
 
251
                    mapping, child_path, child_hexsha, base_inv, file_id, 
 
252
                    revision_id, parent_invs, shagitmap, lookup_object)
247
253
            invdelta.extend(subinvdelta)
248
254
            child_modes.update(grandchildmodes)
249
255
            shamap.extend(subshamap)
250
256
        else:
 
257
            fs_mode = stat.S_IMODE(mode)
 
258
            symlink = stat.S_ISLNK(mode)
251
259
            subinvdelta, subshamap = import_git_blob(texts, mapping, 
252
260
                    child_path, child_hexsha, base_inv, file_id, revision_id, 
253
261
                    parent_invs, shagitmap, lookup_object, 
254
 
                    mode_is_executable(mode), stat.S_ISLNK(mode))
 
262
                    bool(fs_mode & 0111), symlink)
255
263
            invdelta.extend(subinvdelta)
256
264
            shamap.extend(subshamap)
257
265
        if mode not in (stat.S_IFDIR, DEFAULT_FILE_MODE,
258
266
                        stat.S_IFLNK, DEFAULT_FILE_MODE|0111):
259
267
            child_modes[child_path] = mode
260
268
    # Remove any children that have disappeared
261
 
    invdelta.extend(remove_disappeared_children(base_inv, base_ie, existing_children))
 
269
    if base_ie is not None:
 
270
        deletable = [v for k,v in base_ie.children.iteritems() if k not in existing_children]
 
271
        while deletable:
 
272
            ie = deletable.pop()
 
273
            invdelta.append((base_inv.id2path(ie.file_id), None, ie.file_id, None))
 
274
            if ie.kind == "directory":
 
275
                deletable.extend(ie.children.values())
262
276
    shamap.append((hexsha, "tree", (file_id, revision_id)))
263
277
    return invdelta, child_modes, shamap
264
278
 
267
281
        heads, pb=None):
268
282
    """Import a set of git objects into a bzr repository.
269
283
 
270
 
    :param repo: Target Bazaar repository
 
284
    :param repo: Bazaar repository
271
285
    :param mapping: Mapping to use
272
286
    :param object_iter: Iterator over Git objects.
273
287
    """
274
 
    def lookup_object(sha):
275
 
        try:
276
 
            return object_iter[sha]
277
 
        except KeyError:
278
 
            return target_git_object_retriever[sha]
279
288
    # TODO: a more (memory-)efficient implementation of this
280
289
    graph = []
281
290
    root_trees = {}
290
299
        head = heads.pop()
291
300
        assert isinstance(head, str)
292
301
        try:
293
 
            o = lookup_object(head)
 
302
            o = object_iter[head]
294
303
        except KeyError:
295
304
            continue
296
305
        if isinstance(o, Commit):
297
306
            rev = mapping.import_commit(o)
298
307
            if repo.has_revision(rev.revision_id):
299
308
                continue
300
 
            squash_revision(repo, rev)
301
309
            root_trees[rev.revision_id] = o.tree
302
310
            revisions[rev.revision_id] = rev
303
311
            graph.append((rev.revision_id, rev.parent_ids))
318
326
        # We have to do this here, since we have to walk the tree and 
319
327
        # we need to make sure to import the blobs / trees with the right 
320
328
        # path; this may involve adding them more than once.
 
329
        def lookup_object(sha):
 
330
            try:
 
331
                return object_iter[sha]
 
332
            except KeyError:
 
333
                return target_git_object_retriever[sha]
321
334
        parent_invs = []
322
335
        for parent_id in rev.parent_ids:
323
336
            try:
335
348
                parent_invs, target_git_object_retriever._idmap, lookup_object)
336
349
        target_git_object_retriever._idmap.add_entries(shamap)
337
350
        if unusual_modes != {}:
338
 
            for path, mode in unusual_modes.iteritems():
339
 
                warn_unusual_mode(rev.foreign_revid, path, mode)
340
 
            mapping.import_unusual_file_modes(rev, unusual_modes)
 
351
            ret = "unusual modes: \n"
 
352
            for item in unusual_modes.iteritems():
 
353
                ret += "\t%s: %o\n" % item
 
354
            raise AssertionError(ret)
341
355
        try:
342
356
            basis_id = rev.parent_ids[0]
343
357
        except IndexError:
347
361
        parent_invs_cache[rev.revision_id] = inv
348
362
        repo.add_revision(rev.revision_id, rev)
349
363
        if "verify" in debug.debug_flags:
350
 
            new_unusual_modes = mapping.export_unusual_file_modes(rev)
351
 
            if new_unusual_modes != unusual_modes:
352
 
                raise AssertionError("unusual modes don't match: %r != %r" % (unusual_modes, new_unusual_modes))
353
 
            objs = inventory_to_tree_and_blobs(inv, repo.texts, mapping, unusual_modes)
 
364
            objs = inventory_to_tree_and_blobs(inv, repo.texts, mapping)
354
365
            for sha1, newobj, path in objs:
355
366
                assert path is not None
356
367
                oldobj = tree_lookup_path(lookup_object, root_trees[revid], path)
357
 
                if oldobj != newobj:
358
 
                    raise AssertionError("%r != %r in %s" % (oldobj, newobj, path))
 
368
                assert oldobj == newobj, "%r != %r in %s" % (oldobj, newobj, path)
359
369
 
360
370
    target_git_object_retriever._idmap.commit()
361
371
 
398
408
            if interesting_heads is None:
399
409
                ret = [sha for (ref, sha) in refs.iteritems() if not ref.endswith("^{}")]
400
410
            else:
401
 
                ret = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in interesting_heads if revid not in (None, NULL_REVISION)]
 
411
                ret = [mapping.revision_id_bzr_to_foreign(revid)[0] for revid in interesting_heads if revid != NULL_REVISION]
402
412
            return [rev for rev in ret if not self.target.has_revision(mapping.revision_id_foreign_to_bzr(rev))]
403
413
        self.fetch_objects(determine_wants, mapping, pb)
404
414
        return self._refs
405
415
 
406
416
 
 
417
 
407
418
class InterRemoteGitNonGitRepository(InterGitNonGitRepository):
408
419
    """InterRepository that copies revisions from a remote Git into a non-Git 
409
420
    repository."""
411
422
    def fetch_objects(self, determine_wants, mapping, pb=None):
412
423
        def progress(text):
413
424
            pb.update("git: %s" % text.rstrip("\r\n"), 0, 0)
414
 
        store = BazaarObjectStore(self.target, mapping)
415
 
        self.target.lock_write()
416
 
        try:
417
 
            heads = self.target.get_graph().heads(self.target.all_revision_ids())
418
 
            graph_walker = store.get_graph_walker(
419
 
                    [store._lookup_revision_sha1(head) for head in heads])
420
 
            recorded_wants = []
 
425
        graph_walker = BzrFetchGraphWalker(self.target, mapping)
 
426
        create_pb = None
 
427
        if pb is None:
 
428
            create_pb = pb = ui.ui_factory.nested_progress_bar()
 
429
        target_git_object_retriever = BazaarObjectStore(self.target, mapping)
 
430
        recorded_wants = []
421
431
 
422
 
            def record_determine_wants(heads):
423
 
                wants = determine_wants(heads)
424
 
                recorded_wants.extend(wants)
425
 
                return wants
 
432
        def record_determine_wants(heads):
 
433
            wants = determine_wants(heads)
 
434
            recorded_wants.extend(wants)
 
435
            return wants
426
436
        
427
 
            create_pb = None
428
 
            if pb is None:
429
 
                create_pb = pb = ui.ui_factory.nested_progress_bar()
 
437
        try:
 
438
            self.target.lock_write()
430
439
            try:
431
440
                self.target.start_write_group()
432
441
                try:
433
442
                    objects_iter = self.source.fetch_objects(
434
 
                                record_determine_wants, graph_walker, 
435
 
                                store.get_raw, progress)
 
443
                                record_determine_wants, 
 
444
                                graph_walker, 
 
445
                                target_git_object_retriever.get_raw, 
 
446
                                progress)
436
447
                    import_git_objects(self.target, mapping, objects_iter, 
437
 
                            store, recorded_wants, pb)
 
448
                            target_git_object_retriever, recorded_wants, pb)
438
449
                finally:
439
450
                    self.target.commit_write_group()
440
451
            finally:
441
 
                if create_pb:
442
 
                    create_pb.finished()
 
452
                self.target.unlock()
443
453
        finally:
444
 
            self.target.unlock()
 
454
            if create_pb:
 
455
                create_pb.finished()
445
456
 
446
457
    @staticmethod
447
458
    def is_compatible(source, target):
453
464
 
454
465
 
455
466
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
456
 
    """InterRepository that copies revisions from a local Git into a non-Git 
 
467
    """InterRepository that copies revisions from a remote Git into a non-Git 
457
468
    repository."""
458
469
 
459
470
    def fetch_objects(self, determine_wants, mapping, pb=None):