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

  • Committer: Robert Collins
  • Date: 2010-05-06 07:48:22 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506074822-0bsgf2j4h8jx0xkk
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
our first in-tree matcher. See the module docstring for details.
(Robert Collins)

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
 
from bzrlib.errors import NoSuchRevision
20
 
 
21
 
 
22
 
class GitObjectConverter(object):
23
 
 
24
 
    def __init__(self, repository, mapping=None):
25
 
        self.repository = repository
26
 
        if mapping is None:
27
 
            self.mapping = self.repository.get_mapping()
28
 
        else:
29
 
            self.mapping = mapping
30
 
 
31
 
    def __getitem__(self, sha):
32
 
        # Commit
33
 
        revid = self.mapping.revision_id_foreign_to_bzr(sha)
34
 
        try:
35
 
            rev = self.repository.get_revision(revid)
36
 
        except NoSuchRevision:
37
 
            pass
38
 
        else:
39
 
            return reconstruct_git_commit(rev)
40
 
 
41
 
        # TODO: Yeah, this won't scale, but the only alternative is a 
42
 
        # custom map..
43
 
        for (fileid, revision) in self.repository.texts.keys():
44
 
            blob = self._get_blob(
45
 
            print revision
46
 
            
47
 
 
48
 
class GitShaMap(object):
49
 
 
50
 
    def __init__(self, transport):
51
 
        self.transport = transport
52
 
 
53
 
    def add_entry(self, sha, type, type_data):
54
 
        """Add a new entry to the database.
55
 
        """
56
 
        raise NotImplementedError(self.add_entry)
57
 
 
58
 
    def lookup_git_sha(self, sha):
59
 
        """Lookup a Git sha in the database.
60
 
 
61
 
        :param sha: Git object sha
62
 
        :return: (type, type_data) with type_data:
63
 
            revision: revid, tree sha
64
 
        """
65
 
        raise NotImplementedError(self.lookup_git_sha)
66
 
 
67
 
 
68
 
class MappedGitObjectConverter(GitObjectConverter):
69
 
    
70
 
    def __init__(self, repository):
71
 
        self.repository = repository
72
 
        self._idmap = GitShaMap(self.repository._transport)
73
 
 
74
 
    def _update_sha_map(self):
75
 
        # TODO: Check which 
76
 
        raise NotImplementedError(self._update_sha_map)
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 == "revision":
89
 
            return self._get_commit(*type_data)
90
 
        else:
91
 
            raise AssertionError("Unknown object type '%s'" % type)