/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

Provide GitRepository.reconcile.

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]):
292
286
 
293
287
    def __init__(self, repository, mapping=None):
294
288
        self.repository = repository
 
289
        self._map_updated = False
 
290
        self._locked = None
295
291
        if mapping is None:
296
292
            self.mapping = default_mapping
297
293
        else:
304
300
        self.tree_cache = LRUTreeCache(self.repository)
305
301
 
306
302
    def _update_sha_map(self, stop_revision=None):
 
303
        if not self.is_locked():
 
304
            raise AssertionError()
 
305
        if self._map_updated:
 
306
            return
307
307
        graph = self.repository.get_graph()
308
308
        if stop_revision is None:
309
309
            heads = graph.heads(self.repository.all_revision_ids())
321
321
            missing_revids.remove(NULL_REVISION)
322
322
        missing_revids = self.repository.has_revisions(missing_revids)
323
323
        if not missing_revids:
 
324
            self._map_updated = True
324
325
            return
325
326
        self.start_write_group()
326
327
        try:
332
333
                    self._update_sha_map_revision(revid)
333
334
            finally:
334
335
                pb.finished()
 
336
            self._map_updated = True
335
337
        except:
336
338
            self.abort_write_group()
337
339
            raise
399
401
        if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
400
402
            b = self._create_fileid_map_blob(tree.inventory)
401
403
            if b is not None:
402
 
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
 
404
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = (
 
405
                    (stat.S_IFREG | 0644), b.id)
403
406
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
404
407
        yield "", root_tree, root_ie
405
408
        if roundtrip:
406
 
            testament3 = StrictTestament3(rev, tree.inventory)
 
409
            if getattr(StrictTestament3, "from_revision_tree", None):
 
410
                testament3 = StrictTestament3(rev, tree)
 
411
            else: # bzr < 2.4
 
412
                testament3 = StrictTestament3(rev, tree.inventory)
407
413
            verifiers = { "testament3-sha1": testament3.as_sha1() }
408
414
        else:
409
415
            verifiers = {}
428
434
        for path, obj, ie in self._revision_to_objects(rev, tree,
429
435
            roundtrip=True):
430
436
            if isinstance(obj, Commit):
431
 
                testament3 = StrictTestament3(rev, tree.inventory)
 
437
                if getattr(StrictTestament3, "from_revision_tree", None):
 
438
                    testament3 = StrictTestament3(rev, tree)
 
439
                else: # bzr < 2.4
 
440
                    testament3 = StrictTestament3(rev, tree.inventory)
432
441
                ie = { "testament3-sha1": testament3.as_sha1() }
433
442
            updater.add_object(obj, ie, path)
434
443
        commit_obj = updater.finish()
487
496
            self.mapping.BZR_DUMMY_FILE)
488
497
        if (inv.root.file_id == fileid and
489
498
            self.mapping.BZR_FILE_IDS_FILE is not None):
 
499
            if tree is None:
 
500
                tree = Tree()
490
501
            b = self._create_fileid_map_blob(inv)
491
502
            # 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)
 
503
            tree[self.mapping.BZR_FILE_IDS_FILE] = (
 
504
                (stat.S_IFREG | 0644), b.id)
 
505
        if tree is not None:
 
506
            _check_expected_sha(expected_sha, tree)
494
507
        return tree
495
508
 
496
509
    def get_parents(self, sha):
503
516
 
504
517
    def _lookup_revision_sha1(self, revid):
505
518
        """Return the SHA1 matching a Bazaar revision."""
506
 
        from dulwich.protocol import ZERO_SHA
507
519
        if revid == NULL_REVISION:
508
520
            return ZERO_SHA
509
521
        try:
530
542
    def __contains__(self, sha):
531
543
        # See if sha is in map
532
544
        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])
 
545
            for (type, type_data) in self.lookup_git_sha(sha):
 
546
                if type == "commit":
 
547
                    if self.repository.has_revision(type_data[0]):
 
548
                        return True
 
549
                elif type == "blob":
 
550
                    if self.repository.texts.has_key(type_data):
 
551
                        return True
 
552
                elif type == "tree":
 
553
                    if self.repository.has_revision(type_data[1]):
 
554
                        return True
 
555
                else:
 
556
                    raise AssertionError("Unknown object type '%s'" % type)
540
557
            else:
541
 
                raise AssertionError("Unknown object type '%s'" % type)
 
558
                return False
542
559
        except KeyError:
543
560
            return False
544
561
 
545
 
    def lookup_git_shas(self, shas, update_map=True):
546
 
        from dulwich.protocol import ZERO_SHA
 
562
    def lock_read(self):
 
563
        self._locked = 'r'
 
564
        self._map_updated = False
 
565
        self.repository.lock_read()
 
566
        return LogicalLockResult(self.unlock)
 
567
 
 
568
    def lock_write(self):
 
569
        self._locked = 'r'
 
570
        self._map_updated = False
 
571
        self.repository.lock_write()
 
572
        return LogicalLockResult(self.unlock)
 
573
 
 
574
    def is_locked(self):
 
575
        return (self._locked is not None)
 
576
 
 
577
    def unlock(self):
 
578
        self._locked = None
 
579
        self._map_updated = False
 
580
        self.repository.unlock()
 
581
 
 
582
    def lookup_git_shas(self, shas):
547
583
        ret = {}
548
584
        for sha in shas:
549
585
            if sha == ZERO_SHA:
550
 
                ret[sha] = ("commit", (NULL_REVISION, None, {}))
 
586
                ret[sha] = [("commit", (NULL_REVISION, None, {}))]
551
587
                continue
552
588
            try:
553
 
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
 
589
                ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
554
590
            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
 
591
                # if not, see if there are any unconverted revisions and
 
592
                # add them to the map, search for sha in map again
 
593
                self._update_sha_map()
 
594
                try:
 
595
                    ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
 
596
                except KeyError:
 
597
                    pass
564
598
        return ret
565
599
 
566
 
    def lookup_git_sha(self, sha, update_map=True):
567
 
        return self.lookup_git_shas([sha], update_map=update_map)[sha]
 
600
    def lookup_git_sha(self, sha):
 
601
        return self.lookup_git_shas([sha])[sha]
568
602
 
569
603
    def __getitem__(self, sha):
570
604
        if self._cache.content_cache is not None:
572
606
                return self._cache.content_cache[sha]
573
607
            except KeyError:
574
608
                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)
 
609
        for (kind, type_data) in self.lookup_git_sha(sha):
 
610
            # convert object to git object
 
611
            if kind == "commit":
 
612
                (revid, tree_sha, verifiers) = type_data
 
613
                try:
 
614
                    rev = self.repository.get_revision(revid)
 
615
                except errors.NoSuchRevision:
 
616
                    trace.mutter('entry for %s %s in shamap: %r, but not '
 
617
                                 'found in repository', kind, sha, type_data)
 
618
                    raise KeyError(sha)
 
619
                commit = self._reconstruct_commit(rev, tree_sha,
 
620
                    roundtrip=True, verifiers=verifiers)
 
621
                _check_expected_sha(sha, commit)
 
622
                return commit
 
623
            elif kind == "blob":
 
624
                (fileid, revision) = type_data
 
625
                blobs = self._reconstruct_blobs([(fileid, revision, sha)])
 
626
                return blobs.next()
 
627
            elif kind == "tree":
 
628
                (fileid, revid) = type_data
 
629
                try:
 
630
                    tree = self.tree_cache.revision_tree(revid)
 
631
                    rev = self.repository.get_revision(revid)
 
632
                except errors.NoSuchRevision:
 
633
                    trace.mutter('entry for %s %s in shamap: %r, but not found in repository', kind, sha, type_data)
 
634
                    raise KeyError(sha)
 
635
                unusual_modes = extract_unusual_modes(rev)
 
636
                try:
 
637
                    return self._reconstruct_tree(fileid, revid,
 
638
                        tree.inventory, unusual_modes, expected_sha=sha)
 
639
                except errors.NoSuchRevision:
 
640
                    raise KeyError(sha)
 
641
            else:
 
642
                raise AssertionError("Unknown object type '%s'" % kind)
606
643
        else:
607
 
            raise AssertionError("Unknown object type '%s'" % type)
 
644
            raise KeyError(sha)
608
645
 
609
646
    def generate_lossy_pack_contents(self, have, want, progress=None,
610
647
            get_tagged=None):
622
659
        ret = self.lookup_git_shas(have + want)
623
660
        for commit_sha in have:
624
661
            try:
625
 
                (type, (revid, tree_sha)) = ret[commit_sha]
 
662
                for (type, type_data) in ret[commit_sha]:
 
663
                    assert type == "commit"
 
664
                    processed.add(type_data[0])
626
665
            except KeyError:
627
666
                pass
628
 
            else:
629
 
                assert type == "commit"
630
 
                processed.add(revid)
631
667
        pending = set()
632
668
        for commit_sha in want:
633
669
            if commit_sha in have:
634
670
                continue
635
671
            try:
636
 
                (type, (revid, tree_sha)) = ret[commit_sha]
 
672
                for (type, type_data) in ret[commit_sha]:
 
673
                    assert type == "commit"
 
674
                    pending.add(type_data[0])
637
675
            except KeyError:
638
676
                pass
639
 
            else:
640
 
                assert type == "commit"
641
 
                pending.add(revid)
642
677
 
643
 
        todo = _find_missing_bzr_revids(self.repository.get_parent_map, 
644
 
                                        pending, processed)
 
678
        graph = self.repository.get_graph()
 
679
        todo = _find_missing_bzr_revids(graph, pending, processed)
645
680
        trace.mutter('sending revisions %r', todo)
646
681
        ret = []
647
682
        pb = ui.ui_factory.nested_progress_bar()
648
683
        try:
649
684
            for i, revid in enumerate(todo):
650
685
                pb.update("generating git objects", i, len(todo))
651
 
                rev = self.repository.get_revision(revid)
 
686
                try:
 
687
                    rev = self.repository.get_revision(revid)
 
688
                except errors.NoSuchRevision:
 
689
                    continue
652
690
                tree = self.tree_cache.revision_tree(revid)
653
691
                for path, obj, ie in self._revision_to_objects(rev, tree,
654
692
                    roundtrip=not lossy):