/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

add hook for updating to local git cache.

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,
24
25
    )
25
26
from dulwich.object_store import (
26
27
    BaseObjectStore,
33
34
    ui,
34
35
    urlutils,
35
36
    )
 
37
from bzrlib.lock import LogicalLockResult
36
38
from bzrlib.revision import (
37
39
    NULL_REVISION,
38
40
    )
58
60
def get_object_store(repo, mapping=None):
59
61
    git = getattr(repo, "_git", None)
60
62
    if git is not None:
 
63
        git.object_store.unlock = lambda x: None
 
64
        git.object_store.lock_read = LogicalLockResult(lambda: None)
 
65
        git.object_store.lock_write = LogicalLockResult(lambda: None)
61
66
        return git.object_store
62
67
    return BazaarObjectStore(repo, mapping)
63
68
 
94
99
                todo.append(revid)
95
100
            else:
96
101
                assert tree.get_revision_id() == revid
97
 
                assert tree.inventory.revision_id == revid
98
102
                trees[revid] = tree
99
103
        for tree in self.repository.revision_trees(todo):
100
104
            trees[tree.get_revision_id()] = tree
108
112
        self._cache.add(tree.get_revision_id(), tree)
109
113
 
110
114
 
111
 
def _find_missing_bzr_revids(get_parent_map, want, have):
 
115
def _find_missing_bzr_revids(graph, want, have):
112
116
    """Find the revisions that have to be pushed.
113
117
 
114
118
    :param get_parent_map: Function that returns the parents for a sequence
117
121
    :param have: Revisions the target already has
118
122
    :return: Set of revisions to fetch
119
123
    """
120
 
    pending = want - have
121
 
    processed = set()
122
124
    todo = set()
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
 
125
    for rev in want:
 
126
        todo.update(graph.find_unique_ancestors(rev, have))
133
127
    if NULL_REVISION in todo:
134
128
        todo.remove(NULL_REVISION)
135
129
    return todo
184
178
            except errors.NoSuchId:
185
179
                pass
186
180
            else:
187
 
                if (pie.text_sha1 == ie.text_sha1 and 
 
181
                if (pie.text_sha1 == ie.text_sha1 and
188
182
                    pie.kind == ie.kind and
189
183
                    pie.symlink_target == ie.symlink_target):
190
184
                    return pie
229
223
            tree.inventory[parent[0]].kind == "directory"):
230
224
            # Removal
231
225
            new_trees[posixpath.dirname(path[0])] = parent[0]
232
 
    
 
226
 
233
227
    # Fetch contents of the blobs that were changed
234
228
    for (path, ie), chunks in tree.iter_files_bytes(
235
229
        [(ie.file_id, (path, ie)) for (path, ie) in new_blobs]):
287
281
            shamap[ie.file_id] = obj.id
288
282
 
289
283
 
 
284
class PackTupleIterable(object):
 
285
 
 
286
    def __init__(self, store):
 
287
        self.store = store
 
288
        self.objects = {}
 
289
 
 
290
    def add(self, sha, path):
 
291
        self.objects[sha] = path
 
292
 
 
293
    def __len__(self):
 
294
        return len(self.objects)
 
295
 
 
296
    def __iter__(self):
 
297
        return ((self.store[object_id], path) for (object_id, path) in
 
298
                self.objects.iteritems())
 
299
 
 
300
 
290
301
class BazaarObjectStore(BaseObjectStore):
291
302
    """A Git-style object store backed onto a Bazaar repository."""
292
303
 
293
304
    def __init__(self, repository, mapping=None):
294
305
        self.repository = repository
 
306
        self._map_updated = False
 
307
        self._locked = None
295
308
        if mapping is None:
296
309
            self.mapping = default_mapping
297
310
        else:
298
311
            self.mapping = mapping
299
312
        self._cache = cache_from_repository(repository)
300
 
        self._content_cache_types = ("tree")
 
313
        self._content_cache_types = ("tree",)
301
314
        self.start_write_group = self._cache.idmap.start_write_group
302
315
        self.abort_write_group = self._cache.idmap.abort_write_group
303
316
        self.commit_write_group = self._cache.idmap.commit_write_group
304
317
        self.tree_cache = LRUTreeCache(self.repository)
305
318
 
306
319
    def _update_sha_map(self, stop_revision=None):
 
320
        if not self.is_locked():
 
321
            raise AssertionError()
 
322
        if self._map_updated:
 
323
            return
 
324
        if (stop_revision is not None and
 
325
            not self._cache.idmap.missing_revisions([stop_revision])):
 
326
            return
307
327
        graph = self.repository.get_graph()
308
328
        if stop_revision is None:
309
329
            heads = graph.heads(self.repository.all_revision_ids())
321
341
            missing_revids.remove(NULL_REVISION)
322
342
        missing_revids = self.repository.has_revisions(missing_revids)
323
343
        if not missing_revids:
 
344
            if stop_revision is None:
 
345
                self._map_updated = True
324
346
            return
325
347
        self.start_write_group()
326
348
        try:
332
354
                    self._update_sha_map_revision(revid)
333
355
            finally:
334
356
                pb.finished()
 
357
            if stop_revision is None:
 
358
                self._map_updated = True
335
359
        except:
336
360
            self.abort_write_group()
337
361
            raise
399
423
        if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
400
424
            b = self._create_fileid_map_blob(tree.inventory)
401
425
            if b is not None:
402
 
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
 
426
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = (
 
427
                    (stat.S_IFREG | 0644), b.id)
403
428
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
404
429
        yield "", root_tree, root_ie
405
430
        if roundtrip:
406
 
            testament3 = StrictTestament3(rev, tree.inventory)
 
431
            if getattr(StrictTestament3, "from_revision_tree", None):
 
432
                testament3 = StrictTestament3(rev, tree)
 
433
            else: # bzr < 2.4
 
434
                testament3 = StrictTestament3(rev, tree.inventory)
407
435
            verifiers = { "testament3-sha1": testament3.as_sha1() }
408
436
        else:
409
437
            verifiers = {}
428
456
        for path, obj, ie in self._revision_to_objects(rev, tree,
429
457
            roundtrip=True):
430
458
            if isinstance(obj, Commit):
431
 
                testament3 = StrictTestament3(rev, tree.inventory)
 
459
                if getattr(StrictTestament3, "from_revision_tree", None):
 
460
                    testament3 = StrictTestament3(rev, tree)
 
461
                else: # bzr < 2.4
 
462
                    testament3 = StrictTestament3(rev, tree.inventory)
432
463
                ie = { "testament3-sha1": testament3.as_sha1() }
433
464
            updater.add_object(obj, ie, path)
434
465
        commit_obj = updater.finish()
487
518
            self.mapping.BZR_DUMMY_FILE)
488
519
        if (inv.root.file_id == fileid and
489
520
            self.mapping.BZR_FILE_IDS_FILE is not None):
 
521
            if tree is None:
 
522
                tree = Tree()
490
523
            b = self._create_fileid_map_blob(inv)
491
524
            # If this is the root tree, add the file ids
492
 
            tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
493
 
        _check_expected_sha(expected_sha, tree)
 
525
            tree[self.mapping.BZR_FILE_IDS_FILE] = (
 
526
                (stat.S_IFREG | 0644), b.id)
 
527
        if tree is not None:
 
528
            _check_expected_sha(expected_sha, tree)
494
529
        return tree
495
530
 
496
531
    def get_parents(self, sha):
503
538
 
504
539
    def _lookup_revision_sha1(self, revid):
505
540
        """Return the SHA1 matching a Bazaar revision."""
506
 
        from dulwich.protocol import ZERO_SHA
507
541
        if revid == NULL_REVISION:
508
542
            return ZERO_SHA
509
543
        try:
512
546
            try:
513
547
                return mapping_registry.parse_revision_id(revid)[0]
514
548
            except errors.InvalidRevisionId:
515
 
                self.repository.lock_read()
516
 
                try:
517
 
                    self._update_sha_map(revid)
518
 
                finally:
519
 
                    self.repository.unlock()
 
549
                self._update_sha_map(revid)
520
550
                return self._cache.idmap.lookup_commit(revid)
521
551
 
522
552
    def get_raw(self, sha):
530
560
    def __contains__(self, sha):
531
561
        # See if sha is in map
532
562
        try:
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])
 
563
            for (type, type_data) in self.lookup_git_sha(sha):
 
564
                if type == "commit":
 
565
                    if self.repository.has_revision(type_data[0]):
 
566
                        return True
 
567
                elif type == "blob":
 
568
                    if self.repository.texts.has_key(type_data):
 
569
                        return True
 
570
                elif type == "tree":
 
571
                    if self.repository.has_revision(type_data[1]):
 
572
                        return True
 
573
                else:
 
574
                    raise AssertionError("Unknown object type '%s'" % type)
540
575
            else:
541
 
                raise AssertionError("Unknown object type '%s'" % type)
 
576
                return False
542
577
        except KeyError:
543
578
            return False
544
579
 
545
 
    def lookup_git_shas(self, shas, update_map=True):
546
 
        from dulwich.protocol import ZERO_SHA
 
580
    def lock_read(self):
 
581
        self._locked = 'r'
 
582
        self._map_updated = False
 
583
        self.repository.lock_read()
 
584
        return LogicalLockResult(self.unlock)
 
585
 
 
586
    def lock_write(self):
 
587
        self._locked = 'r'
 
588
        self._map_updated = False
 
589
        self.repository.lock_write()
 
590
        return LogicalLockResult(self.unlock)
 
591
 
 
592
    def is_locked(self):
 
593
        return (self._locked is not None)
 
594
 
 
595
    def unlock(self):
 
596
        self._locked = None
 
597
        self._map_updated = False
 
598
        self.repository.unlock()
 
599
 
 
600
    def lookup_git_shas(self, shas):
547
601
        ret = {}
548
602
        for sha in shas:
549
603
            if sha == ZERO_SHA:
550
 
                ret[sha] = ("commit", (NULL_REVISION, None, {}))
 
604
                ret[sha] = [("commit", (NULL_REVISION, None, {}))]
551
605
                continue
552
606
            try:
553
 
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
 
607
                ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
554
608
            except KeyError:
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
 
609
                # if not, see if there are any unconverted revisions and
 
610
                # add them to the map, search for sha in map again
 
611
                self._update_sha_map()
 
612
                try:
 
613
                    ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
 
614
                except KeyError:
 
615
                    pass
564
616
        return ret
565
617
 
566
 
    def lookup_git_sha(self, sha, update_map=True):
567
 
        return self.lookup_git_shas([sha], update_map=update_map)[sha]
 
618
    def lookup_git_sha(self, sha):
 
619
        return self.lookup_git_shas([sha])[sha]
568
620
 
569
621
    def __getitem__(self, sha):
570
622
        if self._cache.content_cache is not None:
572
624
                return self._cache.content_cache[sha]
573
625
            except KeyError:
574
626
                pass
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)
 
627
        for (kind, type_data) in self.lookup_git_sha(sha):
 
628
            # convert object to git object
 
629
            if kind == "commit":
 
630
                (revid, tree_sha, verifiers) = type_data
 
631
                try:
 
632
                    rev = self.repository.get_revision(revid)
 
633
                except errors.NoSuchRevision:
 
634
                    trace.mutter('entry for %s %s in shamap: %r, but not '
 
635
                                 'found in repository', kind, sha, type_data)
 
636
                    raise KeyError(sha)
 
637
                commit = self._reconstruct_commit(rev, tree_sha,
 
638
                    roundtrip=True, verifiers=verifiers)
 
639
                _check_expected_sha(sha, commit)
 
640
                return commit
 
641
            elif kind == "blob":
 
642
                (fileid, revision) = type_data
 
643
                blobs = self._reconstruct_blobs([(fileid, revision, sha)])
 
644
                return blobs.next()
 
645
            elif kind == "tree":
 
646
                (fileid, revid) = type_data
 
647
                try:
 
648
                    tree = self.tree_cache.revision_tree(revid)
 
649
                    rev = self.repository.get_revision(revid)
 
650
                except errors.NoSuchRevision:
 
651
                    trace.mutter('entry for %s %s in shamap: %r, but not found in repository', kind, sha, type_data)
 
652
                    raise KeyError(sha)
 
653
                unusual_modes = extract_unusual_modes(rev)
 
654
                try:
 
655
                    return self._reconstruct_tree(fileid, revid,
 
656
                        tree.inventory, unusual_modes, expected_sha=sha)
 
657
                except errors.NoSuchRevision:
 
658
                    raise KeyError(sha)
 
659
            else:
 
660
                raise AssertionError("Unknown object type '%s'" % kind)
606
661
        else:
607
 
            raise AssertionError("Unknown object type '%s'" % type)
 
662
            raise KeyError(sha)
608
663
 
609
664
    def generate_lossy_pack_contents(self, have, want, progress=None,
610
665
            get_tagged=None):
622
677
        ret = self.lookup_git_shas(have + want)
623
678
        for commit_sha in have:
624
679
            try:
625
 
                (type, (revid, tree_sha)) = ret[commit_sha]
 
680
                for (type, type_data) in ret[commit_sha]:
 
681
                    assert type == "commit"
 
682
                    processed.add(type_data[0])
626
683
            except KeyError:
627
684
                pass
628
 
            else:
629
 
                assert type == "commit"
630
 
                processed.add(revid)
631
685
        pending = set()
632
686
        for commit_sha in want:
633
687
            if commit_sha in have:
634
688
                continue
635
689
            try:
636
 
                (type, (revid, tree_sha)) = ret[commit_sha]
 
690
                for (type, type_data) in ret[commit_sha]:
 
691
                    assert type == "commit"
 
692
                    pending.add(type_data[0])
637
693
            except KeyError:
638
694
                pass
639
 
            else:
640
 
                assert type == "commit"
641
 
                pending.add(revid)
642
695
 
643
 
        todo = _find_missing_bzr_revids(self.repository.get_parent_map, 
644
 
                                        pending, processed)
 
696
        graph = self.repository.get_graph()
 
697
        todo = _find_missing_bzr_revids(graph, pending, processed)
645
698
        trace.mutter('sending revisions %r', todo)
646
 
        ret = []
 
699
        ret = PackTupleIterable(self)
647
700
        pb = ui.ui_factory.nested_progress_bar()
648
701
        try:
649
702
            for i, revid in enumerate(todo):
650
703
                pb.update("generating git objects", i, len(todo))
651
 
                rev = self.repository.get_revision(revid)
 
704
                try:
 
705
                    rev = self.repository.get_revision(revid)
 
706
                except errors.NoSuchRevision:
 
707
                    continue
652
708
                tree = self.tree_cache.revision_tree(revid)
653
709
                for path, obj, ie in self._revision_to_objects(rev, tree,
654
710
                    roundtrip=not lossy):
655
 
                    ret.append((obj, path))
 
711
                    ret.add(obj.id, path)
656
712
        finally:
657
713
            pb.finished()
658
714
        return ret
677
733
            try:
678
734
                self.repository.start_write_group()
679
735
                try:
680
 
                    import_git_objects(self.repository, self.mapping, 
 
736
                    import_git_objects(self.repository, self.mapping,
681
737
                        p.iterobjects(get_raw=self.get_raw),
682
738
                        self.object_store)
683
739
                except: