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."""
21
from bzrlib.errors import NoSuchRevision
23
from bzrlib.plugins.git.mapping import (
24
inventory_to_tree_and_blobs,
27
from bzrlib.plugins.git.shamap import GitShaMap
30
class GitObjectConverter(object):
32
def __init__(self, repository, mapping=None):
33
self.repository = repository
35
self.mapping = self.repository.get_mapping()
37
self.mapping = mapping
38
self._idmap = GitShaMap(self.repository._transport)
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
45
self._update_sha_map_revision(revid)
47
def _parent_lookup(self, sha):
48
raise NotImplementedError(self._parent_lookup)
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:
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))
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))
65
def _get_blob(self, fileid, revision):
66
text = self.repository.texts.get_record_stream([(fileid, revision)], "unordered", True).next().get_bytes_as("fulltext")
71
def _get_tree(self, fileid, revid):
72
raise NotImplementedError(self._get_tree)
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)
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
89
return self._get_commit(*type_data)
91
return self._get_blob(*type_data)
93
return self._get_tree(*type_data)
95
raise AssertionError("Unknown object type '%s'" % type)