/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,
43
47
    mapping_registry,
44
48
    symlink_to_blob,
45
49
    )
46
 
from bzrlib.plugins.git.shamap import (
 
50
from bzrlib.plugins.git.cache import (
47
51
    from_repository as cache_from_repository,
48
52
    )
49
53
 
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)
142
156
            expected_sha))
143
157
 
144
158
 
145
 
def _tree_to_objects(tree, parent_trees, idmap, unusual_modes, dummy_file_name=None):
 
159
def _tree_to_objects(tree, parent_trees, idmap, unusual_modes,
 
160
                     dummy_file_name=None):
146
161
    """Iterate over the objects that were introduced in a revision.
147
162
 
148
163
    :param idmap: id map
149
 
    :param unusual_modes: Unusual file modes
 
164
    :param parent_trees: Parent revision trees
 
165
    :param unusual_modes: Unusual file modes dictionary
150
166
    :param dummy_file_name: File name to use for dummy files
151
167
        in empty directories. None to skip empty directories
152
168
    :return: Yields (path, object, ie) entries
173
189
                    pie.symlink_target == ie.symlink_target):
174
190
                    return pie
175
191
        raise KeyError
 
192
 
 
193
    # Find all the changed blobs
176
194
    for (file_id, path, changed_content, versioned, parent, name, kind,
177
195
         executable) in tree.iter_changes(base_tree):
178
196
        if kind[1] == "file":
206
224
            new_trees[posixpath.dirname(path[1])] = parent[1]
207
225
        elif kind[1] not in (None, "directory"):
208
226
            raise AssertionError(kind[1])
209
 
        if path[0] is not None:
 
227
        if (path[0] not in (None, "") and
 
228
            parent[0] in tree.inventory and
 
229
            tree.inventory[parent[0]].kind == "directory"):
 
230
            # Removal
210
231
            new_trees[posixpath.dirname(path[0])] = parent[0]
211
232
    
 
233
    # Fetch contents of the blobs that were changed
212
234
    for (path, ie), chunks in tree.iter_files_bytes(
213
235
        [(ie.file_id, (path, ie)) for (path, ie) in new_blobs]):
214
236
        obj = Blob()
219
241
    for path in unusual_modes:
220
242
        parent_path = posixpath.dirname(path)
221
243
        new_trees[parent_path] = tree.path2id(parent_path)
222
 
    
 
244
 
223
245
    trees = {}
224
246
    while new_trees:
225
247
        items = new_trees.items()
226
248
        new_trees = {}
227
249
        for path, file_id in items:
228
 
            try:
229
 
                parent_id = tree.inventory[file_id].parent_id
230
 
            except errors.NoSuchId:
231
 
                # Directory was removed recursively perhaps ?
232
 
                continue
 
250
            parent_id = tree.inventory[file_id].parent_id
233
251
            if parent_id is not None:
234
252
                parent_path = urlutils.dirname(path)
235
253
                new_trees[parent_path] = parent_id
324
342
        self._update_sha_map()
325
343
        return iter(self._cache.idmap.sha1s())
326
344
 
327
 
    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
        """
328
354
        def parent_lookup(revid):
329
355
            try:
330
356
                return self._lookup_revision_sha1(revid)
331
357
            except errors.NoSuchRevision:
332
 
                trace.warning("Ignoring ghost parent %s", revid)
333
358
                return None
334
359
        return self.mapping.export_commit(rev, tree_sha, parent_lookup,
335
 
            roundtrip)
 
360
            roundtrip, verifiers)
 
361
 
 
362
    def _create_fileid_map_blob(self, inv):
 
363
        # FIXME: This can probably be a lot more efficient, 
 
364
        # not all files necessarily have to be processed.
 
365
        file_ids = {}
 
366
        for (path, ie) in inv.iter_entries():
 
367
            if self.mapping.generate_file_id(path) != ie.file_id:
 
368
                file_ids[path] = ie.file_id
 
369
        return self.mapping.export_fileid_map(file_ids)
336
370
 
337
371
    def _revision_to_objects(self, rev, tree, roundtrip):
338
372
        """Convert a revision to a set of git objects.
362
396
                base_sha1 = self._lookup_revision_sha1(rev.parent_ids[0])
363
397
                root_tree = self[self[base_sha1].tree]
364
398
            root_ie = tree.inventory.root
365
 
        if roundtrip:
366
 
            # FIXME: This can probably be a lot more efficient, 
367
 
            # not all files necessarily have to be processed.
368
 
            file_ids = {}
369
 
            for (path, ie) in tree.inventory.iter_entries():
370
 
                if self.mapping.generate_file_id(path) != ie.file_id:
371
 
                    file_ids[path] = ie.file_id
372
 
            b = self.mapping.export_fileid_map(file_ids)
 
399
        if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
 
400
            b = self._create_fileid_map_blob(tree.inventory)
373
401
            if b is not None:
374
402
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
375
403
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
376
404
        yield "", root_tree, root_ie
377
 
        commit_obj = self._reconstruct_commit(rev, root_tree.id, roundtrip=roundtrip)
 
405
        if roundtrip:
 
406
            testament3 = StrictTestament3(rev, tree.inventory)
 
407
            verifiers = { "testament3-sha1": testament3.as_sha1() }
 
408
        else:
 
409
            verifiers = {}
 
410
        commit_obj = self._reconstruct_commit(rev, root_tree.id,
 
411
            roundtrip=roundtrip, verifiers=verifiers)
378
412
        try:
379
413
            foreign_revid, mapping = mapping_registry.parse_revision_id(
380
414
                rev.revision_id)
393
427
        updater = self._get_updater(rev)
394
428
        for path, obj, ie in self._revision_to_objects(rev, tree,
395
429
            roundtrip=True):
396
 
            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)
397
434
        commit_obj = updater.finish()
398
435
        return commit_obj.id
399
436
 
448
485
                raise AssertionError("unknown entry kind '%s'" % entry.kind)
449
486
        tree = directory_to_tree(inv[fileid], get_ie_sha1, unusual_modes,
450
487
            self.mapping.BZR_DUMMY_FILE)
 
488
        if (inv.root.file_id == fileid and
 
489
            self.mapping.BZR_FILE_IDS_FILE is not None):
 
490
            b = self._create_fileid_map_blob(inv)
 
491
            # If this is the root tree, add the file ids
 
492
            tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
451
493
        _check_expected_sha(expected_sha, tree)
452
494
        return tree
453
495
 
470
512
            try:
471
513
                return mapping_registry.parse_revision_id(revid)[0]
472
514
            except errors.InvalidRevisionId:
473
 
                self._update_sha_map(revid)
 
515
                self.repository.lock_read()
 
516
                try:
 
517
                    self._update_sha_map(revid)
 
518
                finally:
 
519
                    self.repository.unlock()
474
520
                return self._cache.idmap.lookup_commit(revid)
475
521
 
476
522
    def get_raw(self, sha):
488
534
            if type == "commit":
489
535
                return self.repository.has_revision(type_data[0])
490
536
            elif type == "blob":
491
 
                return self.repository.texts.has_version(type_data)
 
537
                return self.repository.texts.has_key(type_data)
492
538
            elif type == "tree":
493
539
                return self.repository.has_revision(type_data[1])
494
540
            else:
497
543
            return False
498
544
 
499
545
    def lookup_git_shas(self, shas, update_map=True):
 
546
        from dulwich.protocol import ZERO_SHA
500
547
        ret = {}
501
548
        for sha in shas:
 
549
            if sha == ZERO_SHA:
 
550
                ret[sha] = ("commit", (NULL_REVISION, None, {}))
 
551
                continue
502
552
            try:
503
553
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
504
554
            except KeyError:
525
575
        (type, type_data) = self.lookup_git_sha(sha)
526
576
        # convert object to git object
527
577
        if type == "commit":
528
 
            (revid, tree_sha) = type_data
 
578
            (revid, tree_sha, verifiers) = type_data
529
579
            try:
530
580
                rev = self.repository.get_revision(revid)
531
581
            except errors.NoSuchRevision:
532
582
                trace.mutter('entry for %s %s in shamap: %r, but not found in '
533
583
                             'repository', type, sha, type_data)
534
584
                raise KeyError(sha)
535
 
            commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True)
 
585
            commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True,
 
586
                verifiers=verifiers)
536
587
            _check_expected_sha(sha, commit)
537
588
            return commit
538
589
        elif type == "blob":