/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 converter.py

Implement sha cache.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Map from Git sha's to Bazaar objects."""
 
18
 
 
19
import bzrlib
 
20
 
 
21
from bzrlib.errors import NoSuchRevision
 
22
 
 
23
from bzrlib.plugins.git.mapping import (
 
24
    inventory_to_tree_and_blobs,
 
25
    revision_to_commit,
 
26
    )
 
27
from bzrlib.plugins.git.shamap import GitShaMap
 
28
 
 
29
 
 
30
class GitObjectConverter(object):
 
31
 
 
32
    def __init__(self, repository, mapping=None):
 
33
        self.repository = repository
 
34
        if mapping is None:
 
35
            self.mapping = self.repository.get_mapping()
 
36
        else:
 
37
            self.mapping = mapping
 
38
        self._idmap = GitShaMap(self.repository._transport)
 
39
 
 
40
    def _update_sha_map(self):
 
41
        all_revids = set(self.repository.all_revision_ids())
 
42
        present_revids = set(self._idmap.revids())
 
43
        missing = all_revids - present_revids
 
44
        for revid in missing:
 
45
            self._update_sha_map_revision(revid)
 
46
 
 
47
    def _parent_lookup(self, sha):
 
48
        raise NotImplementedError(self._parent_lookup)
 
49
 
 
50
    def _update_sha_map_revision(self, revid):
 
51
        inv = self.repository.get_inventory(revid)
 
52
        objects = inventory_to_tree_and_blobs(self.repository, self.mapping, revid)
 
53
        for sha, o, path in objects:
 
54
            if path == "":
 
55
                tree_sha = sha
 
56
            ie = inv[inv.path2id(path)]
 
57
            if ie.kind in ("file", "symlink"):
 
58
                self._idmap.add_entry(sha, "blob", (ie.file_id, ie.revision))
 
59
            else:
 
60
                self._idmap.add_entry(sha, "tree", (ie.file_id, ie.revision))
 
61
        rev = self.repository.get_revision(revid)
 
62
        commit_obj = revision_to_commit(rev, tree_sha, self._parent_lookup)
 
63
        self._idmap.add_entry(commit_obj.sha(), "commit", (revid, tree_sha))
 
64
 
 
65
    def _get_blob(self, fileid, revision):
 
66
        text = self.repository.texts.get_record_stream([(fileid, revision)], "unordered", True).next().get_bytes_as("fulltext")
 
67
        blob = Blob()
 
68
        blob._text = text
 
69
        return blob
 
70
 
 
71
    def _get_tree(self, fileid, revid):
 
72
        raise NotImplementedError(self._get_tree)
 
73
 
 
74
    def _get_commit(self, revid, tree_sha):
 
75
        rev = self.repository.get_revision(revid)
 
76
        return revision_to_commit(rev, tree_sha, self._parent_lookup)
 
77
 
 
78
    def __getitem__(self, sha):
 
79
        # See if sha is in map
 
80
        try:
 
81
            (type, type_data) = self._idmap.lookup_git_sha(sha)
 
82
        except KeyError:
 
83
            # if not, see if there are any unconverted revisions and add them 
 
84
            # to the map, search for sha in map again
 
85
            self._update_sha_map()
 
86
            (type, type_data) = self._idmap.lookup_git_sha(sha)
 
87
        # convert object to git object
 
88
        if type == "commit":
 
89
            return self._get_commit(*type_data)
 
90
        elif type == "blob":
 
91
            return self._get_blob(*type_data)
 
92
        elif type == "tree":
 
93
            return self._get_tree(*type_data)
 
94
        else:
 
95
            raise AssertionError("Unknown object type '%s'" % type)