1
# Copyright (C) 2009 Canonical Ltd
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.
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.
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
17
"""Map from Git sha's to Bazaar objects."""
19
from bzrlib.errors import NoSuchRevision
22
class GitObjectConverter(object):
24
def __init__(self, repository, mapping=None):
25
self.repository = repository
27
self.mapping = self.repository.get_mapping()
29
self.mapping = mapping
31
def __getitem__(self, sha):
33
revid = self.mapping.revision_id_foreign_to_bzr(sha)
35
rev = self.repository.get_revision(revid)
36
except NoSuchRevision:
39
return reconstruct_git_commit(rev)
41
# TODO: Yeah, this won't scale, but the only alternative is a
43
for (fileid, revision) in self.repository.texts.keys():
44
blob = self._get_blob(
48
class GitShaMap(object):
50
def __init__(self, transport):
51
self.transport = transport
53
def add_entry(self, sha, type, type_data):
54
"""Add a new entry to the database.
56
raise NotImplementedError(self.add_entry)
58
def lookup_git_sha(self, sha):
59
"""Lookup a Git sha in the database.
61
:param sha: Git object sha
62
:return: (type, type_data) with type_data:
63
revision: revid, tree sha
65
raise NotImplementedError(self.lookup_git_sha)
68
class MappedGitObjectConverter(GitObjectConverter):
70
def __init__(self, repository):
71
self.repository = repository
72
self._idmap = GitShaMap(self.repository._transport)
74
def _update_sha_map(self):
76
raise NotImplementedError(self._update_sha_map)
78
def __getitem__(self, sha):
79
# See if sha is in map
81
(type, type_data) = self._idmap.lookup_git_sha(sha)
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)
91
raise AssertionError("Unknown object type '%s'" % type)