/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

Fix two mistakes in 'bzr help git'.

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