/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: None
67
 
        git.object_store.lock_read = lambda: LogicalLockResult(lambda: None)
68
 
        git.object_store.lock_write = lambda: 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
 
 
326
 
    def _missing_revisions(self, revisions):
327
 
        return self._cache.idmap.missing_revisions(revisions)
328
305
 
329
306
    def _update_sha_map(self, stop_revision=None):
330
 
        if not self.is_locked():
331
 
            raise AssertionError()
332
 
        if self._map_updated:
333
 
            return
334
 
        if (stop_revision is not None and
335
 
            not self._missing_revisions([stop_revision])):
336
 
            return
337
307
        graph = self.repository.get_graph()
338
308
        if stop_revision is None:
339
 
            all_revids = self.repository.all_revision_ids()
340
 
            missing_revids = self._missing_revisions(all_revids)
 
309
            heads = graph.heads(self.repository.all_revision_ids())
341
310
        else:
342
311
            heads = set([stop_revision])
343
 
            missing_revids = self._missing_revisions(heads)
344
 
            while heads:
345
 
                parents = graph.get_parent_map(heads)
346
 
                todo = set()
347
 
                for p in parents.values():
348
 
                    todo.update([x for x in p if x not in missing_revids])
349
 
                heads = self._missing_revisions(todo)
350
 
                missing_revids.update(heads)
 
312
        missing_revids = self._cache.idmap.missing_revisions(heads)
 
313
        while heads:
 
314
            parents = graph.get_parent_map(heads)
 
315
            todo = set()
 
316
            for p in parents.values():
 
317
                todo.update([x for x in p if x not in missing_revids])
 
318
            heads = self._cache.idmap.missing_revisions(todo)
 
319
            missing_revids.update(heads)
351
320
        if NULL_REVISION in missing_revids:
352
321
            missing_revids.remove(NULL_REVISION)
353
322
        missing_revids = self.repository.has_revisions(missing_revids)
354
323
        if not missing_revids:
355
 
            if stop_revision is None:
356
 
                self._map_updated = True
357
324
            return
358
325
        self.start_write_group()
359
326
        try:
365
332
                    self._update_sha_map_revision(revid)
366
333
            finally:
367
334
                pb.finished()
368
 
            if stop_revision is None:
369
 
                self._map_updated = True
370
335
        except:
371
336
            self.abort_write_group()
372
337
            raise
434
399
        if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
435
400
            b = self._create_fileid_map_blob(tree.inventory)
436
401
            if b is not None:
437
 
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = (
438
 
                    (stat.S_IFREG | 0644), b.id)
 
402
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
439
403
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
440
404
        yield "", root_tree, root_ie
441
405
        if roundtrip:
442
 
            if getattr(StrictTestament3, "from_revision_tree", None):
443
 
                testament3 = StrictTestament3(rev, tree)
444
 
            else: # bzr < 2.4
445
 
                testament3 = StrictTestament3(rev, tree.inventory)
 
406
            testament3 = StrictTestament3(rev, tree.inventory)
446
407
            verifiers = { "testament3-sha1": testament3.as_sha1() }
447
408
        else:
448
409
            verifiers = {}
467
428
        for path, obj, ie in self._revision_to_objects(rev, tree,
468
429
            roundtrip=True):
469
430
            if isinstance(obj, Commit):
470
 
                if getattr(StrictTestament3, "from_revision_tree", None):
471
 
                    testament3 = StrictTestament3(rev, tree)
472
 
                else: # bzr < 2.4
473
 
                    testament3 = StrictTestament3(rev, tree.inventory)
 
431
                testament3 = StrictTestament3(rev, tree.inventory)
474
432
                ie = { "testament3-sha1": testament3.as_sha1() }
475
433
            updater.add_object(obj, ie, path)
476
434
        commit_obj = updater.finish()
529
487
            self.mapping.BZR_DUMMY_FILE)
530
488
        if (inv.root.file_id == fileid and
531
489
            self.mapping.BZR_FILE_IDS_FILE is not None):
532
 
            if tree is None:
533
 
                tree = Tree()
534
490
            b = self._create_fileid_map_blob(inv)
535
491
            # If this is the root tree, add the file ids
536
 
            tree[self.mapping.BZR_FILE_IDS_FILE] = (
537
 
                (stat.S_IFREG | 0644), b.id)
538
 
        if tree is not None:
539
 
            _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)
540
494
        return tree
541
495
 
542
496
    def get_parents(self, sha):
549
503
 
550
504
    def _lookup_revision_sha1(self, revid):
551
505
        """Return the SHA1 matching a Bazaar revision."""
 
506
        from dulwich.protocol import ZERO_SHA
552
507
        if revid == NULL_REVISION:
553
508
            return ZERO_SHA
554
509
        try:
557
512
            try:
558
513
                return mapping_registry.parse_revision_id(revid)[0]
559
514
            except errors.InvalidRevisionId:
560
 
                self._update_sha_map(revid)
 
515
                self.repository.lock_read()
 
516
                try:
 
517
                    self._update_sha_map(revid)
 
518
                finally:
 
519
                    self.repository.unlock()
561
520
                return self._cache.idmap.lookup_commit(revid)
562
521
 
563
522
    def get_raw(self, sha):
571
530
    def __contains__(self, sha):
572
531
        # See if sha is in map
573
532
        try:
574
 
            for (type, type_data) in self.lookup_git_sha(sha):
575
 
                if type == "commit":
576
 
                    if self.repository.has_revision(type_data[0]):
577
 
                        return True
578
 
                elif type == "blob":
579
 
                    if self.repository.texts.has_key(type_data):
580
 
                        return True
581
 
                elif type == "tree":
582
 
                    if self.repository.has_revision(type_data[1]):
583
 
                        return True
584
 
                else:
585
 
                    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])
586
540
            else:
587
 
                return False
 
541
                raise AssertionError("Unknown object type '%s'" % type)
588
542
        except KeyError:
589
543
            return False
590
544
 
591
 
    def lock_read(self):
592
 
        self._locked = 'r'
593
 
        self._map_updated = False
594
 
        self.repository.lock_read()
595
 
        return LogicalLockResult(self.unlock)
596
 
 
597
 
    def lock_write(self):
598
 
        self._locked = 'r'
599
 
        self._map_updated = False
600
 
        self.repository.lock_write()
601
 
        return LogicalLockResult(self.unlock)
602
 
 
603
 
    def is_locked(self):
604
 
        return (self._locked is not None)
605
 
 
606
 
    def unlock(self):
607
 
        self._locked = None
608
 
        self._map_updated = False
609
 
        self.repository.unlock()
610
 
 
611
 
    def lookup_git_shas(self, shas):
 
545
    def lookup_git_shas(self, shas, update_map=True):
 
546
        from dulwich.protocol import ZERO_SHA
612
547
        ret = {}
613
548
        for sha in shas:
614
549
            if sha == ZERO_SHA:
615
 
                ret[sha] = [("commit", (NULL_REVISION, None, {}))]
 
550
                ret[sha] = ("commit", (NULL_REVISION, None, {}))
616
551
                continue
617
552
            try:
618
 
                ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
 
553
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
619
554
            except KeyError:
620
 
                # if not, see if there are any unconverted revisions and
621
 
                # add them to the map, search for sha in map again
622
 
                self._update_sha_map()
623
 
                try:
624
 
                    ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
625
 
                except KeyError:
626
 
                    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
627
564
        return ret
628
565
 
629
 
    def lookup_git_sha(self, sha):
630
 
        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]
631
568
 
632
569
    def __getitem__(self, sha):
633
570
        if self._cache.content_cache is not None:
635
572
                return self._cache.content_cache[sha]
636
573
            except KeyError:
637
574
                pass
638
 
        for (kind, type_data) in self.lookup_git_sha(sha):
639
 
            # convert object to git object
640
 
            if kind == "commit":
641
 
                (revid, tree_sha, verifiers) = type_data
642
 
                try:
643
 
                    rev = self.repository.get_revision(revid)
644
 
                except errors.NoSuchRevision:
645
 
                    trace.mutter('entry for %s %s in shamap: %r, but not '
646
 
                                 'found in repository', kind, sha, type_data)
647
 
                    raise KeyError(sha)
648
 
                commit = self._reconstruct_commit(rev, tree_sha,
649
 
                    roundtrip=True, verifiers=verifiers)
650
 
                _check_expected_sha(sha, commit)
651
 
                return commit
652
 
            elif kind == "blob":
653
 
                (fileid, revision) = type_data
654
 
                blobs = self._reconstruct_blobs([(fileid, revision, sha)])
655
 
                return blobs.next()
656
 
            elif kind == "tree":
657
 
                (fileid, revid) = type_data
658
 
                try:
659
 
                    tree = self.tree_cache.revision_tree(revid)
660
 
                    rev = self.repository.get_revision(revid)
661
 
                except errors.NoSuchRevision:
662
 
                    trace.mutter('entry for %s %s in shamap: %r, but not found in '
663
 
                        'repository', kind, sha, type_data)
664
 
                    raise KeyError(sha)
665
 
                unusual_modes = extract_unusual_modes(rev)
666
 
                try:
667
 
                    return self._reconstruct_tree(fileid, revid,
668
 
                        tree.inventory, unusual_modes, expected_sha=sha)
669
 
                except errors.NoSuchRevision:
670
 
                    raise KeyError(sha)
671
 
            else:
672
 
                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)
673
606
        else:
674
 
            raise KeyError(sha)
 
607
            raise AssertionError("Unknown object type '%s'" % type)
675
608
 
676
609
    def generate_lossy_pack_contents(self, have, want, progress=None,
677
610
            get_tagged=None):
688
621
        processed = set()
689
622
        ret = self.lookup_git_shas(have + want)
690
623
        for commit_sha in have:
691
 
            commit_sha = self.unpeel_map.peel_tag(commit_sha, commit_sha)
692
624
            try:
693
 
                for (type, type_data) in ret[commit_sha]:
694
 
                    assert type == "commit"
695
 
                    processed.add(type_data[0])
 
625
                (type, (revid, tree_sha)) = ret[commit_sha]
696
626
            except KeyError:
697
 
                trace.mutter("unable to find remote ref %s", commit_sha)
 
627
                pass
 
628
            else:
 
629
                assert type == "commit"
 
630
                processed.add(revid)
698
631
        pending = set()
699
632
        for commit_sha in want:
700
633
            if commit_sha in have:
701
634
                continue
702
635
            try:
703
 
                for (type, type_data) in ret[commit_sha]:
704
 
                    assert type == "commit"
705
 
                    pending.add(type_data[0])
 
636
                (type, (revid, tree_sha)) = ret[commit_sha]
706
637
            except KeyError:
707
638
                pass
 
639
            else:
 
640
                assert type == "commit"
 
641
                pending.add(revid)
708
642
 
709
 
        graph = self.repository.get_graph()
710
 
        todo = _find_missing_bzr_revids(graph, pending, processed)
 
643
        todo = _find_missing_bzr_revids(self.repository.get_parent_map, 
 
644
                                        pending, processed)
711
645
        trace.mutter('sending revisions %r', todo)
712
 
        ret = PackTupleIterable(self)
 
646
        ret = []
713
647
        pb = ui.ui_factory.nested_progress_bar()
714
648
        try:
715
649
            for i, revid in enumerate(todo):
716
650
                pb.update("generating git objects", i, len(todo))
717
 
                try:
718
 
                    rev = self.repository.get_revision(revid)
719
 
                except errors.NoSuchRevision:
720
 
                    continue
 
651
                rev = self.repository.get_revision(revid)
721
652
                tree = self.tree_cache.revision_tree(revid)
722
653
                for path, obj, ie in self._revision_to_objects(rev, tree,
723
654
                    roundtrip=not lossy):
724
 
                    ret.add(obj.id, path)
725
 
            return ret
 
655
                    ret.append((obj, path))
726
656
        finally:
727
657
            pb.finished()
 
658
        return ret
728
659
 
729
660
    def add_thin_pack(self):
730
661
        import tempfile
746
677
            try:
747
678
                self.repository.start_write_group()
748
679
                try:
749
 
                    import_git_objects(self.repository, self.mapping,
 
680
                    import_git_objects(self.repository, self.mapping, 
750
681
                        p.iterobjects(get_raw=self.get_raw),
751
682
                        self.object_store)
752
683
                except: