/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

Escape slashes.

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
    )
40
42
    StrictTestament3,
41
43
    )
42
44
 
 
45
from bzrlib.plugins.git.cache import (
 
46
    from_repository as cache_from_repository,
 
47
    )
43
48
from bzrlib.plugins.git.mapping import (
44
49
    default_mapping,
45
50
    directory_to_tree,
47
52
    mapping_registry,
48
53
    symlink_to_blob,
49
54
    )
50
 
from bzrlib.plugins.git.cache import (
51
 
    from_repository as cache_from_repository,
 
55
from bzrlib.plugins.git.unpeel_map import (
 
56
    UnpeelMap,
52
57
    )
53
58
 
54
59
import posixpath
58
63
def get_object_store(repo, mapping=None):
59
64
    git = getattr(repo, "_git", None)
60
65
    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)
61
69
        return git.object_store
62
70
    return BazaarObjectStore(repo, mapping)
63
71
 
94
102
                todo.append(revid)
95
103
            else:
96
104
                assert tree.get_revision_id() == revid
97
 
                assert tree.inventory.revision_id == revid
98
105
                trees[revid] = tree
99
106
        for tree in self.repository.revision_trees(todo):
100
107
            trees[tree.get_revision_id()] = tree
108
115
        self._cache.add(tree.get_revision_id(), tree)
109
116
 
110
117
 
111
 
def _find_missing_bzr_revids(get_parent_map, want, have):
 
118
def _find_missing_bzr_revids(graph, want, have):
112
119
    """Find the revisions that have to be pushed.
113
120
 
114
121
    :param get_parent_map: Function that returns the parents for a sequence
117
124
    :param have: Revisions the target already has
118
125
    :return: Set of revisions to fetch
119
126
    """
120
 
    pending = want - have
121
 
    processed = set()
 
127
    handled = set(have)
122
128
    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
 
129
    for rev in want:
 
130
        extra_todo = graph.find_unique_ancestors(rev, handled)
 
131
        todo.update(extra_todo)
 
132
        handled.update(extra_todo)
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
225
225
        elif kind[1] not in (None, "directory"):
226
226
            raise AssertionError(kind[1])
227
227
        if (path[0] not in (None, "") and
228
 
            parent[0] in tree.inventory and
 
228
            tree.has_id(parent[0]) and
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
 
290
307
class BazaarObjectStore(BaseObjectStore):
291
308
    """A Git-style object store backed onto a Bazaar repository."""
292
309
 
293
310
    def __init__(self, repository, mapping=None):
294
311
        self.repository = repository
 
312
        self._map_updated = False
 
313
        self._locked = None
295
314
        if mapping is None:
296
315
            self.mapping = default_mapping
297
316
        else:
298
317
            self.mapping = mapping
299
318
        self._cache = cache_from_repository(repository)
300
 
        self._content_cache_types = ("tree")
 
319
        self._content_cache_types = ("tree",)
301
320
        self.start_write_group = self._cache.idmap.start_write_group
302
321
        self.abort_write_group = self._cache.idmap.abort_write_group
303
322
        self.commit_write_group = self._cache.idmap.commit_write_group
304
323
        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)
305
328
 
306
329
    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
307
337
        graph = self.repository.get_graph()
308
338
        if stop_revision is None:
309
 
            heads = graph.heads(self.repository.all_revision_ids())
 
339
            all_revids = self.repository.all_revision_ids()
 
340
            missing_revids = self._missing_revisions(all_revids)
310
341
        else:
311
342
            heads = set([stop_revision])
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)
 
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)
320
351
        if NULL_REVISION in missing_revids:
321
352
            missing_revids.remove(NULL_REVISION)
322
353
        missing_revids = self.repository.has_revisions(missing_revids)
323
354
        if not missing_revids:
 
355
            if stop_revision is None:
 
356
                self._map_updated = True
324
357
            return
325
358
        self.start_write_group()
326
359
        try:
332
365
                    self._update_sha_map_revision(revid)
333
366
            finally:
334
367
                pb.finished()
 
368
            if stop_revision is None:
 
369
                self._map_updated = True
335
370
        except:
336
371
            self.abort_write_group()
337
372
            raise
399
434
        if roundtrip and self.mapping.BZR_FILE_IDS_FILE is not None:
400
435
            b = self._create_fileid_map_blob(tree.inventory)
401
436
            if b is not None:
402
 
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = ((stat.S_IFREG | 0644), b.id)
 
437
                root_tree[self.mapping.BZR_FILE_IDS_FILE] = (
 
438
                    (stat.S_IFREG | 0644), b.id)
403
439
                yield self.mapping.BZR_FILE_IDS_FILE, b, None
404
440
        yield "", root_tree, root_ie
405
441
        if roundtrip:
406
 
            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)
407
446
            verifiers = { "testament3-sha1": testament3.as_sha1() }
408
447
        else:
409
448
            verifiers = {}
428
467
        for path, obj, ie in self._revision_to_objects(rev, tree,
429
468
            roundtrip=True):
430
469
            if isinstance(obj, Commit):
431
 
                testament3 = StrictTestament3(rev, tree.inventory)
 
470
                if getattr(StrictTestament3, "from_revision_tree", None):
 
471
                    testament3 = StrictTestament3(rev, tree)
 
472
                else: # bzr < 2.4
 
473
                    testament3 = StrictTestament3(rev, tree.inventory)
432
474
                ie = { "testament3-sha1": testament3.as_sha1() }
433
475
            updater.add_object(obj, ie, path)
434
476
        commit_obj = updater.finish()
487
529
            self.mapping.BZR_DUMMY_FILE)
488
530
        if (inv.root.file_id == fileid and
489
531
            self.mapping.BZR_FILE_IDS_FILE is not None):
 
532
            if tree is None:
 
533
                tree = Tree()
490
534
            b = self._create_fileid_map_blob(inv)
491
535
            # 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)
 
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)
494
540
        return tree
495
541
 
496
542
    def get_parents(self, sha):
503
549
 
504
550
    def _lookup_revision_sha1(self, revid):
505
551
        """Return the SHA1 matching a Bazaar revision."""
506
 
        from dulwich.protocol import ZERO_SHA
507
552
        if revid == NULL_REVISION:
508
553
            return ZERO_SHA
509
554
        try:
512
557
            try:
513
558
                return mapping_registry.parse_revision_id(revid)[0]
514
559
            except errors.InvalidRevisionId:
515
 
                self.repository.lock_read()
516
 
                try:
517
 
                    self._update_sha_map(revid)
518
 
                finally:
519
 
                    self.repository.unlock()
 
560
                self._update_sha_map(revid)
520
561
                return self._cache.idmap.lookup_commit(revid)
521
562
 
522
563
    def get_raw(self, sha):
530
571
    def __contains__(self, sha):
531
572
        # See if sha is in map
532
573
        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])
 
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)
540
586
            else:
541
 
                raise AssertionError("Unknown object type '%s'" % type)
 
587
                return False
542
588
        except KeyError:
543
589
            return False
544
590
 
545
 
    def lookup_git_shas(self, shas, update_map=True):
546
 
        from dulwich.protocol import ZERO_SHA
 
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):
547
612
        ret = {}
548
613
        for sha in shas:
549
614
            if sha == ZERO_SHA:
550
 
                ret[sha] = ("commit", (NULL_REVISION, None, {}))
 
615
                ret[sha] = [("commit", (NULL_REVISION, None, {}))]
551
616
                continue
552
617
            try:
553
 
                ret[sha] = self._cache.idmap.lookup_git_sha(sha)
 
618
                ret[sha] = list(self._cache.idmap.lookup_git_sha(sha))
554
619
            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
 
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
564
627
        return ret
565
628
 
566
 
    def lookup_git_sha(self, sha, update_map=True):
567
 
        return self.lookup_git_shas([sha], update_map=update_map)[sha]
 
629
    def lookup_git_sha(self, sha):
 
630
        return self.lookup_git_shas([sha])[sha]
568
631
 
569
632
    def __getitem__(self, sha):
570
633
        if self._cache.content_cache is not None:
572
635
                return self._cache.content_cache[sha]
573
636
            except KeyError:
574
637
                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)
 
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
                    if revid == NULL_REVISION:
 
646
                        raise AssertionError(
 
647
                            "should not try to look up NULL_REVISION")
 
648
                    trace.mutter('entry for %s %s in shamap: %r, but not '
 
649
                                 'found in repository', kind, sha, type_data)
 
650
                    raise KeyError(sha)
 
651
                commit = self._reconstruct_commit(rev, tree_sha,
 
652
                    roundtrip=True, verifiers=verifiers)
 
653
                _check_expected_sha(sha, commit)
 
654
                return commit
 
655
            elif kind == "blob":
 
656
                (fileid, revision) = type_data
 
657
                blobs = self._reconstruct_blobs([(fileid, revision, sha)])
 
658
                return blobs.next()
 
659
            elif kind == "tree":
 
660
                (fileid, revid) = type_data
 
661
                try:
 
662
                    tree = self.tree_cache.revision_tree(revid)
 
663
                    rev = self.repository.get_revision(revid)
 
664
                except errors.NoSuchRevision:
 
665
                    trace.mutter('entry for %s %s in shamap: %r, but not found in '
 
666
                        'repository', kind, sha, type_data)
 
667
                    raise KeyError(sha)
 
668
                unusual_modes = extract_unusual_modes(rev)
 
669
                try:
 
670
                    return self._reconstruct_tree(fileid, revid,
 
671
                        tree.inventory, unusual_modes, expected_sha=sha)
 
672
                except errors.NoSuchRevision:
 
673
                    raise KeyError(sha)
 
674
            else:
 
675
                raise AssertionError("Unknown object type '%s'" % kind)
606
676
        else:
607
 
            raise AssertionError("Unknown object type '%s'" % type)
 
677
            raise KeyError(sha)
608
678
 
609
679
    def generate_lossy_pack_contents(self, have, want, progress=None,
610
680
            get_tagged=None):
621
691
        processed = set()
622
692
        ret = self.lookup_git_shas(have + want)
623
693
        for commit_sha in have:
 
694
            commit_sha = self.unpeel_map.peel_tag(commit_sha, commit_sha)
624
695
            try:
625
 
                (type, (revid, tree_sha)) = ret[commit_sha]
 
696
                for (type, type_data) in ret[commit_sha]:
 
697
                    assert type == "commit"
 
698
                    processed.add(type_data[0])
626
699
            except KeyError:
627
 
                pass
628
 
            else:
629
 
                assert type == "commit"
630
 
                processed.add(revid)
 
700
                trace.mutter("unable to find remote ref %s", commit_sha)
631
701
        pending = set()
632
702
        for commit_sha in want:
633
703
            if commit_sha in have:
634
704
                continue
635
705
            try:
636
 
                (type, (revid, tree_sha)) = ret[commit_sha]
 
706
                for (type, type_data) in ret[commit_sha]:
 
707
                    assert type == "commit"
 
708
                    pending.add(type_data[0])
637
709
            except KeyError:
638
710
                pass
639
 
            else:
640
 
                assert type == "commit"
641
 
                pending.add(revid)
642
711
 
643
 
        todo = _find_missing_bzr_revids(self.repository.get_parent_map, 
644
 
                                        pending, processed)
 
712
        graph = self.repository.get_graph()
 
713
        todo = _find_missing_bzr_revids(graph, pending, processed)
645
714
        trace.mutter('sending revisions %r', todo)
646
 
        ret = []
 
715
        ret = PackTupleIterable(self)
647
716
        pb = ui.ui_factory.nested_progress_bar()
648
717
        try:
649
718
            for i, revid in enumerate(todo):
650
719
                pb.update("generating git objects", i, len(todo))
651
 
                rev = self.repository.get_revision(revid)
 
720
                try:
 
721
                    rev = self.repository.get_revision(revid)
 
722
                except errors.NoSuchRevision:
 
723
                    continue
652
724
                tree = self.tree_cache.revision_tree(revid)
653
725
                for path, obj, ie in self._revision_to_objects(rev, tree,
654
726
                    roundtrip=not lossy):
655
 
                    ret.append((obj, path))
 
727
                    ret.add(obj.id, path)
 
728
            return ret
656
729
        finally:
657
730
            pb.finished()
658
 
        return ret
659
731
 
660
732
    def add_thin_pack(self):
661
733
        import tempfile
677
749
            try:
678
750
                self.repository.start_write_group()
679
751
                try:
680
 
                    import_git_objects(self.repository, self.mapping, 
 
752
                    import_git_objects(self.repository, self.mapping,
681
753
                        p.iterobjects(get_raw=self.get_raw),
682
754
                        self.object_store)
683
755
                except: