17
17
"""Map from Git sha's to Bazaar objects."""
23
from bzrlib.errors import NoSuchRevision
19
from dulwich.objects import (
25
30
from bzrlib.plugins.git.mapping import (
26
31
inventory_to_tree_and_blobs,
27
33
revision_to_commit,
29
from bzrlib.plugins.git.shamap import GitShaMap
31
from dulwich.objects import (
35
from bzrlib.plugins.git.shamap import (
36
class GitObjectConverter(object):
40
class BazaarObjectStore(object):
41
"""A Git-style object store backed onto a Bazaar repository."""
38
43
def __init__(self, repository, mapping=None):
39
44
self.repository = repository
55
60
pb.update("updating git map", i, len(all_revids))
56
61
self._update_sha_map_revision(revid)
60
66
def _update_sha_map_revision(self, revid):
61
67
inv = self.repository.get_inventory(revid)
62
objects = inventory_to_tree_and_blobs(self.repository, self.mapping, revid)
68
objects = inventory_to_tree_and_blobs(self.repository, self.mapping,
63
70
for sha, o, path in objects:
66
73
ie = inv[inv.path2id(path)]
67
74
if ie.kind in ("file", "symlink"):
68
self._idmap.add_entry(sha, "blob", (ie.file_id, ie.revision))
76
elif ie.kind == "directory":
70
self._idmap.add_entry(sha, "tree", (ie.file_id, ie.revision))
79
raise AssertionError()
80
self._idmap.add_entry(sha, git_kind, (ie.file_id, ie.revision))
71
81
rev = self.repository.get_revision(revid)
72
commit_obj = revision_to_commit(rev, tree_sha, self._idmap._parent_lookup)
73
self._idmap.add_entry(commit_obj.sha().hexdigest(), "commit", (revid, tree_sha))
82
commit_obj = revision_to_commit(rev, tree_sha,
83
self._idmap._parent_lookup)
85
foreign_revid, mapping = mapping_registry.parse_revision_id(revid)
86
except errors.InvalidRevisionId:
89
if foreign_revid != commit_obj.id:
90
raise AssertionError("recreated git commit had different sha1: expected %s, got %s" % (foreign_revid, commit_obj.id))
91
self._idmap.add_entry(commit_obj.id, "commit", (revid, tree_sha))
75
93
def _get_blob(self, fileid, revision):
76
text = self.repository.texts.get_record_stream([(fileid, revision)], "unordered", True).next().get_bytes_as("fulltext")
94
"""Return a Git Blob object from a fileid and revision stored in bzr.
96
:param fileid: File id of the text
97
:param revision: Revision of the text
99
text = self.repository.texts.get_record_stream([(fileid, revision)],
100
"unordered", True).next().get_bytes_as("fulltext")
81
def _get_tree(self, fileid, revid):
82
raise NotImplementedError(self._get_tree)
105
def _get_tree(self, fileid, revid, inv=None):
106
"""Return a Git Tree object from a file id and a revision stored in bzr.
108
:param fileid: fileid in the tree.
109
:param revision: Revision of the tree.
112
inv = self.repository.get_inventory(revid)
114
for name, ie in inv[fileid].children.iteritems():
115
if ie.kind == "directory":
116
subtree = self._get_tree(ie.file_id, revid, inv)
117
tree.add(stat.S_IFDIR, name.encode('UTF-8'), subtree.id)
118
elif ie.kind == "file":
119
blob = self._get_blob(ie.file_id, ie.revision)
120
mode = stat.S_IFREG | 0644
123
tree.add(mode, name.encode('UTF-8'), blob.id)
124
elif ie.kind == "symlink":
125
raise AssertionError("Symlinks not yet supported")
84
129
def _get_commit(self, revid, tree_sha):
85
130
rev = self.repository.get_revision(revid)
86
131
return revision_to_commit(rev, tree_sha, self._idmap._parent_lookup)
133
def get_raw(self, sha):
88
138
def __getitem__(self, sha):
89
139
# See if sha is in map