/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 object_store.py

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from dulwich.objects import (
20
20
    Blob,
 
21
    Commit,
21
22
    Tree,
22
23
    sha_to_hex,
23
24
    )
35
36
from bzrlib.revision import (
36
37
    NULL_REVISION,
37
38
    )
 
39
from bzrlib.testament import(
 
40
    StrictTestament3,
 
41
    )
38
42
 
39
43
from bzrlib.plugins.git.mapping import (
40
44
    default_mapping,
71
75
        self._cache = lru_cache.LRUSizeCache(max_size=MAX_TREE_CACHE_SIZE,
72
76
            after_cleanup_size=None, compute_size=approx_tree_size)
73
77
 
74
 
    def revision_tree(self, revid):            
 
78
    def revision_tree(self, revid):
75
79
        try:
76
 
            return self._cache[revid] 
 
80
            tree = self._cache[revid]
77
81
        except KeyError:
78
82
            tree = self.repository.revision_tree(revid)
79
83
            self.add(tree)
80
 
            return tree
 
84
        assert tree.get_revision_id() == tree.inventory.revision_id
 
85
        return tree
81
86
 
82
87
    def iter_revision_trees(self, revids):
83
 
        trees = dict([(k, self._cache.get(k)) for k in revids]) 
84
 
        for tree in self.repository.revision_trees(
85
 
                [r for r, v in trees.iteritems() if v is None]):
 
88
        trees = {}
 
89
        todo = []
 
90
        for revid in revids:
 
91
            try:
 
92
                tree = self._cache[revid]
 
93
            except KeyError:
 
94
                todo.append(revid)
 
95
            else:
 
96
                assert tree.get_revision_id() == revid
 
97
                assert tree.inventory.revision_id == revid
 
98
                trees[revid] = tree
 
99
        for tree in self.repository.revision_trees(todo):
86
100
            trees[tree.get_revision_id()] = tree
87
101
            self.add(tree)
88
102
        return (trees[r] for r in revids)
175
189
                    pie.symlink_target == ie.symlink_target):
176
190
                    return pie
177
191
        raise KeyError
178
 
    
 
192
 
179
193
    # Find all the changed blobs
180
194
    for (file_id, path, changed_content, versioned, parent, name, kind,
181
195
         executable) in tree.iter_changes(base_tree):
210
224
            new_trees[posixpath.dirname(path[1])] = parent[1]
211
225
        elif kind[1] not in (None, "directory"):
212
226
            raise AssertionError(kind[1])
213
 
        if path[0] is not None and parent[0] in tree.inventory and tree.inventory[parent[0]].kind == "directory":
 
227
        if (path[0] not in (None, "") and
 
228
            parent[0] in tree.inventory and
 
229
            tree.inventory[parent[0]].kind == "directory"):
214
230
            # Removal
215
231
            new_trees[posixpath.dirname(path[0])] = parent[0]
216
232
    
225
241
    for path in unusual_modes:
226
242
        parent_path = posixpath.dirname(path)
227
243
        new_trees[parent_path] = tree.path2id(parent_path)
228
 
    
 
244
 
229
245
    trees = {}
230
246
    while new_trees:
231
247
        items = new_trees.items()
326
342
        self._update_sha_map()
327
343
        return iter(self._cache.idmap.sha1s())
328
344
 
329
 
    def _reconstruct_commit(self, rev, tree_sha, roundtrip):
 
345
    def _reconstruct_commit(self, rev, tree_sha, roundtrip, verifiers):
 
346
        """Reconstruct a Commit object.
 
347
 
 
348
        :param rev: Revision object
 
349
        :param tree_sha: SHA1 of the root tree object
 
350
        :param roundtrip: Whether or not to roundtrip bzr metadata
 
351
        :param verifiers: Verifiers for the commits
 
352
        :return: Commit object
 
353
        """
330
354
        def parent_lookup(revid):
331
355
            try:
332
356
                return self._lookup_revision_sha1(revid)
333
357
            except errors.NoSuchRevision:
334
358
                return None
335
359
        return self.mapping.export_commit(rev, tree_sha, parent_lookup,
336
 
            roundtrip)
 
360
            roundtrip, verifiers)
337
361
 
338
362
    def _create_fileid_map_blob(self, inv):
339
363
        # FIXME: This can probably be a lot more efficient, 
378
402
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
379
403
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
380
404
        yield "", root_tree, root_ie
 
405
        if roundtrip:
 
406
            testament3 = StrictTestament3(rev, tree.inventory)
 
407
            verifiers = { "testament3-sha1": testament3.as_sha1() }
 
408
        else:
 
409
            verifiers = {}
381
410
        commit_obj = self._reconstruct_commit(rev, root_tree.id,
382
 
            roundtrip=roundtrip)
 
411
            roundtrip=roundtrip, verifiers=verifiers)
383
412
        try:
384
413
            foreign_revid, mapping = mapping_registry.parse_revision_id(
385
414
                rev.revision_id)
398
427
        updater = self._get_updater(rev)
399
428
        for path, obj, ie in self._revision_to_objects(rev, tree,
400
429
            roundtrip=True):
401
 
            updater.add_object(obj, ie)
 
430
            if isinstance(obj, Commit):
 
431
                testament3 = StrictTestament3(rev, tree.inventory)
 
432
                ie = { "testament3-sha1": testament3.as_sha1() }
 
433
            updater.add_object(obj, ie, path)
402
434
        commit_obj = updater.finish()
403
435
        return commit_obj.id
404
436
 
502
534
            if type == "commit":
503
535
                return self.repository.has_revision(type_data[0])
504
536
            elif type == "blob":
505
 
                return self.repository.texts.has_version(type_data)
 
537
                return self.repository.texts.has_key(type_data)
506
538
            elif type == "tree":
507
539
                return self.repository.has_revision(type_data[1])
508
540
            else:
511
543
            return False
512
544
 
513
545
    def lookup_git_shas(self, shas, update_map=True):
 
546
        from dulwich.protocol import ZERO_SHA
514
547
        ret = {}
515
548
        for sha in shas:
 
549
            if sha == ZERO_SHA:
 
550
                ret[sha] = ("commit", (NULL_REVISION, None, {}))
 
551
                continue
516
552
            try:
517
553
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
518
554
            except KeyError:
539
575
        (type, type_data) = self.lookup_git_sha(sha)
540
576
        # convert object to git object
541
577
        if type == "commit":
542
 
            (revid, tree_sha) = type_data
 
578
            (revid, tree_sha, verifiers) = type_data
543
579
            try:
544
580
                rev = self.repository.get_revision(revid)
545
581
            except errors.NoSuchRevision:
546
582
                trace.mutter('entry for %s %s in shamap: %r, but not found in '
547
583
                             'repository', type, sha, type_data)
548
584
                raise KeyError(sha)
549
 
            commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True)
 
585
            commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True,
 
586
                verifiers=verifiers)
550
587
            _check_expected_sha(sha, commit)
551
588
            return commit
552
589
        elif type == "blob":