/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:
21
21
    Commit,
22
22
    Tree,
23
23
    sha_to_hex,
24
 
    ZERO_SHA,
25
24
    )
26
25
from dulwich.object_store import (
27
26
    BaseObjectStore,
34
33
    ui,
35
34
    urlutils,
36
35
    )
37
 
from bzrlib.lock import LogicalLockResult
38
36
from bzrlib.revision import (
39
37
    NULL_REVISION,
40
38
    )
42
40
    StrictTestament3,
43
41
    )
44
42
 
45
 
from bzrlib.plugins.git.cache import (
46
 
    from_repository as cache_from_repository,
47
 
    )
48
43
from bzrlib.plugins.git.mapping import (
49
44
    default_mapping,
50
45
    directory_to_tree,
52
47
    mapping_registry,
53
48
    symlink_to_blob,
54
49
    )
55
 
from bzrlib.plugins.git.unpeel_map import (
56
 
    UnpeelMap,
 
50
from bzrlib.plugins.git.cache import (
 
51
    from_repository as cache_from_repository,
57
52
    )
58
53
 
59
54
import posixpath
63
58
def get_object_store(repo, mapping=None):
64
59
    git = getattr(repo, "_git", None)
65
60
    if git is not None:
66
 
        git.object_store.unlock = lambda x: None
67
 
        git.object_store.lock_read = LogicalLockResult(lambda: None)
68
 
        git.object_store.lock_write = LogicalLockResult(lambda: None)
69
61
        return git.object_store
70
62
    return BazaarObjectStore(repo, mapping)
71
63
 
102
94
                todo.append(revid)
103
95
            else:
104
96
                assert tree.get_revision_id() == revid
 
97
                assert tree.inventory.revision_id == revid
105
98
                trees[revid] = tree
106
99
        for tree in self.repository.revision_trees(todo):
107
100
            trees[tree.get_revision_id()] = tree
115
108
        self._cache.add(tree.get_revision_id(), tree)
116
109
 
117
110
 
118
 
def _find_missing_bzr_revids(graph, want, have):
 
111
def _find_missing_bzr_revids(get_parent_map, want, have):
119
112
    """Find the revisions that have to be pushed.
120
113
 
121
114
    :param get_parent_map: Function that returns the parents for a sequence
124
117
    :param have: Revisions the target already has
125
118
    :return: Set of revisions to fetch
126
119
    """
127
 
    handled = set(have)
 
120
    pending = want - have
 
121
    processed = set()
128
122
    todo = set()
129
 
    for rev in want:
130
 
        extra_todo = graph.find_unique_ancestors(rev, handled)
131
 
        todo.update(extra_todo)
132
 
        handled.update(extra_todo)
 
123
    while pending:
 
124
        processed.update(pending)
 
125
        next_map = get_parent_map(pending)
 
126
        next_pending = set()
 
127
        for item in next_map.iteritems():
 
128
            if item[0] in have:
 
129
                continue
 
130
            todo.add(item[0])
 
131
            next_pending.update(p for p in item[1] if p not in processed)
 
132
        pending = next_pending
133
133
    if NULL_REVISION in todo:
134
134
        todo.remove(NULL_REVISION)
135
135
    return todo
184
184
            except errors.NoSuchId:
185
185
                pass
186
186
            else:
187
 
                if (pie.text_sha1 == ie.text_sha1 and
 
187
                if (pie.text_sha1 == ie.text_sha1 and 
188
188
                    pie.kind == ie.kind and
189
189
                    pie.symlink_target == ie.symlink_target):
190
190
                    return pie
229
229
            tree.inventory[parent[0]].kind == "directory"):
230
230
            # Removal
231
231
            new_trees[posixpath.dirname(path[0])] = parent[0]
232
 
 
 
232
    
233
233
    # Fetch contents of the blobs that were changed
234
234
    for (path, ie), chunks in tree.iter_files_bytes(
235
235
        [(ie.file_id, (path, ie)) for (path, ie) in new_blobs]):
287
287
            shamap[ie.file_id] = obj.id
288
288
 
289
289
 
290
 
class PackTupleIterable(object):
291
 
 
292
 
    def __init__(self, store):
293
 
        self.store = store
294
 
        self.objects = {}
295
 
 
296
 
    def add(self, sha, path):
297
 
        self.objects[sha] = path
298
 
 
299
 
    def __len__(self):
300
 
        return len(self.objects)
301
 
 
302
 
    def __iter__(self):
303
 
        return ((self.store[object_id], path) for (object_id, path) in
304
 
                self.objects.iteritems())
305
 
 
306
 
 
307
290
class BazaarObjectStore(BaseObjectStore):
308
291
    """A Git-style object store backed onto a Bazaar repository."""
309
292
 
310
293
    def __init__(self, repository, mapping=None):
311
294
        self.repository = repository
312
 
        self._map_updated = False
313
 
        self._locked = None
314
295
        if mapping is None:
315
296
            self.mapping = default_mapping
316
297
        else:
317
298
            self.mapping = mapping
318
299
        self._cache = cache_from_repository(repository)
319
 
        self._content_cache_types = ("tree",)
 
300
        self._content_cache_types = ("tree")
320
301
        self.start_write_group = self._cache.idmap.start_write_group
321
302
        self.abort_write_group = self._cache.idmap.abort_write_group
322
303
        self.commit_write_group = self._cache.idmap.commit_write_group
323
304
        self.tree_cache = LRUTreeCache(self.repository)
324
 
        self.unpeel_map = UnpeelMap.from_repository(self.repository)
325
305
 
326
306
    def _update_sha_map(self, stop_revision=None):
327
 
        if not self.is_locked():
328
 
            raise AssertionError()
329
 
        if self._map_updated:
330
 
            return
331
 
        if (stop_revision is not None and
332
 
            not self._cache.idmap.missing_revisions([stop_revision])):
333
 
            return
334
307
        graph = self.repository.get_graph()
335
308
        if stop_revision is None:
336
309
            heads = graph.heads(self.repository.all_revision_ids())
348
321
            missing_revids.remove(NULL_REVISION)
349
322
        missing_revids = self.repository.has_revisions(missing_revids)
350
323
        if not missing_revids:
351
 
            if stop_revision is None:
352
 
                self._map_updated = True
353
324
            return
354
325
        self.start_write_group()
355
326
        try:
361
332
                    self._update_sha_map_revision(revid)
362
333
            finally:
363
334
                pb.finished()
364
 
            if stop_revision is None:
365
 
                self._map_updated = True
366
335
        except:
367
336
            self.abort_write_group()
368
337
            raise
430
399
        if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
431
400
            b = self._create_fileid_map_blob(tree.inventory)
432
401
            if b is not None:
433
 
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = (
434
 
                    (stat.S_IFREG | 0644), b.id)
 
402
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
435
403
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
436
404
        yield "", root_tree, root_ie
437
405
        if roundtrip:
438
 
            if getattr(StrictTestament3, "from_revision_tree", None):
439
 
                testament3 = StrictTestament3(rev, tree)
440
 
            else: # bzr < 2.4
441
 
                testament3 = StrictTestament3(rev, tree.inventory)
 
406
            testament3 = StrictTestament3(rev, tree.inventory)
442
407
            verifiers = { "testament3-sha1": testament3.as_sha1() }
443
408
        else:
444
409
            verifiers = {}
463
428
        for path, obj, ie in self._revision_to_objects(rev, tree,
464
429
            roundtrip=True):
465
430
            if isinstance(obj, Commit):
466
 
                if getattr(StrictTestament3, "from_revision_tree", None):
467
 
                    testament3 = StrictTestament3(rev, tree)
468
 
                else: # bzr < 2.4
469
 
                    testament3 = StrictTestament3(rev, tree.inventory)
 
431
                testament3 = StrictTestament3(rev, tree.inventory)
470
432
                ie = { "testament3-sha1": testament3.as_sha1() }
471
433
            updater.add_object(obj, ie, path)
472
434
        commit_obj = updater.finish()
525
487
            self.mapping.BZR_DUMMY_FILE)
526
488
        if (inv.root.file_id == fileid and
527
489
            self.mapping.BZR_FILE_IDS_FILE is not None):
528
 
            if tree is None:
529
 
                tree = Tree()
530
490
            b = self._create_fileid_map_blob(inv)
531
491
            # If this is the root tree, add the file ids
532
 
            tree[self.mapping.BZR_FILE_IDS_FILE] = (
533
 
                (stat.S_IFREG | 0644), b.id)
534
 
        if tree is not None:
535
 
            _check_expected_sha(expected_sha, tree)
 
492
            tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
 
493
        _check_expected_sha(expected_sha, tree)
536
494
        return tree
537
495
 
538
496
    def get_parents(self, sha):
545
503
 
546
504
    def _lookup_revision_sha1(self, revid):
547
505
        """Return the SHA1 matching a Bazaar revision."""
 
506
        from dulwich.protocol import ZERO_SHA
548
507
        if revid == NULL_REVISION:
549
508
            return ZERO_SHA
550
509
        try:
553
512
            try:
554
513
                return mapping_registry.parse_revision_id(revid)[0]
555
514
            except errors.InvalidRevisionId:
556
 
                self._update_sha_map(revid)
 
515
                self.repository.lock_read()
 
516
                try:
 
517
                    self._update_sha_map(revid)
 
518
                finally:
 
519
                    self.repository.unlock()
557
520
                return self._cache.idmap.lookup_commit(revid)
558
521
 
559
522
    def get_raw(self, sha):
567
530
    def __contains__(self, sha):
568
531
        # See if sha is in map
569
532
        try:
570
 
            for (type, type_data) in self.lookup_git_sha(sha):
571
 
                if type == "commit":
572
 
                    if self.repository.has_revision(type_data[0]):
573
 
                        return True
574
 
                elif type == "blob":
575
 
                    if self.repository.texts.has_key(type_data):
576
 
                        return True
577
 
                elif type == "tree":
578
 
                    if self.repository.has_revision(type_data[1]):
579
 
                        return True
580
 
                else:
581
 
                    raise AssertionError("Unknown object type '%s'" % type)
 
533
            (type, type_data) = self.lookup_git_sha(sha)
 
534
            if type == "commit":
 
535
                return self.repository.has_revision(type_data[0])
 
536
            elif type == "blob":
 
537
                return self.repository.texts.has_key(type_data)
 
538
            elif type == "tree":
 
539
                return self.repository.has_revision(type_data[1])
582
540
            else:
583
 
                return False
 
541
                raise AssertionError("Unknown object type '%s'" % type)
584
542
        except KeyError:
585
543
            return False
586
544
 
587
 
    def lock_read(self):
588
 
        self._locked = 'r'
589
 
        self._map_updated = False
590
 
        self.repository.lock_read()
591
 
        return LogicalLockResult(self.unlock)
592
 
 
593
 
    def lock_write(self):
594
 
        self._locked = 'r'
595
 
        self._map_updated = False
596
 
        self.repository.lock_write()
597
 
        return LogicalLockResult(self.unlock)
598
 
 
599
 
    def is_locked(self):
600
 
        return (self._locked is not None)
601
 
 
602
 
    def unlock(self):
603
 
        self._locked = None
604
 
        self._map_updated = False
605
 
        self.repository.unlock()
606
 
 
607
 
    def lookup_git_shas(self, shas):
 
545
    def lookup_git_shas(self, shas, update_map=True):
 
546
        from dulwich.protocol import ZERO_SHA
608
547
        ret = {}
609
548
        for sha in shas:
610
549
            if sha == ZERO_SHA:
611
 
                ret[sha] = [("commit", (NULL_REVISION, None, {}))]
 
550
                ret[sha] = ("commit", (NULL_REVISION, None, {}))
612
551
                continue
613
552
            try:
614
 
                ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
 
553
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
615
554
            except KeyError:
616
 
                # if not, see if there are any unconverted revisions and
617
 
                # add them to the map, search for sha in map again
618
 
                self._update_sha_map()
619
 
                try:
620
 
                    ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
621
 
                except KeyError:
622
 
                    pass
 
555
                if update_map:
 
556
                    # if not, see if there are any unconverted revisions and add
 
557
                    # them to the map, search for sha in map again
 
558
                    self._update_sha_map()
 
559
                    update_map = False
 
560
                    try:
 
561
                        ret[sha] = self._cache.idmap.lookup_git_sha(sha)
 
562
                    except KeyError:
 
563
                        pass
623
564
        return ret
624
565
 
625
 
    def lookup_git_sha(self, sha):
626
 
        return self.lookup_git_shas([sha])[sha]
 
566
    def lookup_git_sha(self, sha, update_map=True):
 
567
        return self.lookup_git_shas([sha], update_map=update_map)[sha]
627
568
 
628
569
    def __getitem__(self, sha):
629
570
        if self._cache.content_cache is not None:
631
572
                return self._cache.content_cache[sha]
632
573
            except KeyError:
633
574
                pass
634
 
        for (kind, type_data) in self.lookup_git_sha(sha):
635
 
            # convert object to git object
636
 
            if kind == "commit":
637
 
                (revid, tree_sha, verifiers) = type_data
638
 
                try:
639
 
                    rev = self.repository.get_revision(revid)
640
 
                except errors.NoSuchRevision:
641
 
                    trace.mutter('entry for %s %s in shamap: %r, but not '
642
 
                                 'found in repository', kind, sha, type_data)
643
 
                    raise KeyError(sha)
644
 
                commit = self._reconstruct_commit(rev, tree_sha,
645
 
                    roundtrip=True, verifiers=verifiers)
646
 
                _check_expected_sha(sha, commit)
647
 
                return commit
648
 
            elif kind == "blob":
649
 
                (fileid, revision) = type_data
650
 
                blobs = self._reconstruct_blobs([(fileid, revision, sha)])
651
 
                return blobs.next()
652
 
            elif kind == "tree":
653
 
                (fileid, revid) = type_data
654
 
                try:
655
 
                    tree = self.tree_cache.revision_tree(revid)
656
 
                    rev = self.repository.get_revision(revid)
657
 
                except errors.NoSuchRevision:
658
 
                    trace.mutter('entry for %s %s in shamap: %r, but not found in repository', kind, sha, type_data)
659
 
                    raise KeyError(sha)
660
 
                unusual_modes = extract_unusual_modes(rev)
661
 
                try:
662
 
                    return self._reconstruct_tree(fileid, revid,
663
 
                        tree.inventory, unusual_modes, expected_sha=sha)
664
 
                except errors.NoSuchRevision:
665
 
                    raise KeyError(sha)
666
 
            else:
667
 
                raise AssertionError("Unknown object type '%s'" % kind)
 
575
        (type, type_data) = self.lookup_git_sha(sha)
 
576
        # convert object to git object
 
577
        if type == "commit":
 
578
            (revid, tree_sha, verifiers) = type_data
 
579
            try:
 
580
                rev = self.repository.get_revision(revid)
 
581
            except errors.NoSuchRevision:
 
582
                trace.mutter('entry for %s %s in shamap: %r, but not found in '
 
583
                             'repository', type, sha, type_data)
 
584
                raise KeyError(sha)
 
585
            commit = self._reconstruct_commit(rev, tree_sha, roundtrip=True,
 
586
                verifiers=verifiers)
 
587
            _check_expected_sha(sha, commit)
 
588
            return commit
 
589
        elif type == "blob":
 
590
            (fileid, revision) = type_data
 
591
            return self._reconstruct_blobs([(fileid, revision, sha)]).next()
 
592
        elif type == "tree":
 
593
            (fileid, revid) = type_data
 
594
            try:
 
595
                tree = self.tree_cache.revision_tree(revid)
 
596
                rev = self.repository.get_revision(revid)
 
597
            except errors.NoSuchRevision:
 
598
                trace.mutter('entry for %s %s in shamap: %r, but not found in repository', type, sha, type_data)
 
599
                raise KeyError(sha)
 
600
            unusual_modes = extract_unusual_modes(rev)
 
601
            try:
 
602
                return self._reconstruct_tree(fileid, revid, tree.inventory,
 
603
                    unusual_modes, expected_sha=sha)
 
604
            except errors.NoSuchRevision:
 
605
                raise KeyError(sha)
668
606
        else:
669
 
            raise KeyError(sha)
 
607
            raise AssertionError("Unknown object type '%s'" % type)
670
608
 
671
609
    def generate_lossy_pack_contents(self, have, want, progress=None,
672
610
            get_tagged=None):
683
621
        processed = set()
684
622
        ret = self.lookup_git_shas(have + want)
685
623
        for commit_sha in have:
686
 
            commit_sha = self.unpeel_map.peel_tag(commit_sha, commit_sha)
687
624
            try:
688
 
                for (type, type_data) in ret[commit_sha]:
689
 
                    assert type == "commit"
690
 
                    processed.add(type_data[0])
 
625
                (type, (revid, tree_sha)) = ret[commit_sha]
691
626
            except KeyError:
692
 
                trace.mutter("unable to find remote ref %s", commit_sha)
 
627
                pass
 
628
            else:
 
629
                assert type == "commit"
 
630
                processed.add(revid)
693
631
        pending = set()
694
632
        for commit_sha in want:
695
633
            if commit_sha in have:
696
634
                continue
697
635
            try:
698
 
                for (type, type_data) in ret[commit_sha]:
699
 
                    assert type == "commit"
700
 
                    pending.add(type_data[0])
 
636
                (type, (revid, tree_sha)) = ret[commit_sha]
701
637
            except KeyError:
702
638
                pass
 
639
            else:
 
640
                assert type == "commit"
 
641
                pending.add(revid)
703
642
 
704
 
        graph = self.repository.get_graph()
705
 
        todo = _find_missing_bzr_revids(graph, pending, processed)
 
643
        todo = _find_missing_bzr_revids(self.repository.get_parent_map, 
 
644
                                        pending, processed)
706
645
        trace.mutter('sending revisions %r', todo)
707
 
        ret = PackTupleIterable(self)
 
646
        ret = []
708
647
        pb = ui.ui_factory.nested_progress_bar()
709
648
        try:
710
649
            for i, revid in enumerate(todo):
711
650
                pb.update("generating git objects", i, len(todo))
712
 
                try:
713
 
                    rev = self.repository.get_revision(revid)
714
 
                except errors.NoSuchRevision:
715
 
                    continue
 
651
                rev = self.repository.get_revision(revid)
716
652
                tree = self.tree_cache.revision_tree(revid)
717
653
                for path, obj, ie in self._revision_to_objects(rev, tree,
718
654
                    roundtrip=not lossy):
719
 
                    ret.add(obj.id, path)
720
 
            return ret
 
655
                    ret.append((obj, path))
721
656
        finally:
722
657
            pb.finished()
 
658
        return ret
723
659
 
724
660
    def add_thin_pack(self):
725
661
        import tempfile
741
677
            try:
742
678
                self.repository.start_write_group()
743
679
                try:
744
 
                    import_git_objects(self.repository, self.mapping,
 
680
                    import_git_objects(self.repository, self.mapping, 
745
681
                        p.iterobjects(get_raw=self.get_raw),
746
682
                        self.object_store)
747
683
                except: