34
from bzrlib.foreign import (
37
35
from bzrlib.transport import get_transport
39
from bzrlib.plugins.git.foreign import (
37
from bzrlib.plugins.git import (
43
40
from bzrlib.plugins.git.mapping import default_mapping
45
from bzrlib.plugins.git import git
48
class GitTags(object):
50
def __init__(self, tags):
54
return iter(self._tags)
57
class GitRepository(ForeignRepository):
43
class GitRepository(repository.Repository):
58
44
"""An adapter to git repositories for bzr."""
69
55
self.revisions = versionedfiles.VirtualRevisionTexts(self)
70
56
self._format = GitFormat()
71
57
self._fallback_repositories = []
72
self.tags = GitTags(self._git.get_tags())
74
59
def _all_revision_ids(self):
75
60
if self._git.heads == []:
106
cms = self._git.commits(self.lookup_git_revid(revision_id, self.get_mapping()), max_count=max_count, skip=skip)
91
cms = self._git.commits(default_mapping.revision_id_bzr_to_foreign(revision_id), max_count=max_count, skip=skip)
108
ret += [self.get_mapping().revision_id_foreign_to_bzr(cm.id) for cm in cms]
93
ret += [default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms]
109
94
return [None] + ret
111
96
def get_signature_text(self, revision_id):
112
97
raise errors.NoSuchRevision(self, revision_id)
114
def lookup_revision_id(self, revid):
115
"""Lookup a revision id.
117
:param revid: Bazaar revision id.
118
:return: Tuple with git revisionid and mapping.
120
# Yes, this doesn't really work, but good enough as a stub
121
return osutils.sha(rev_id).hexdigest(), self.get_mapping()
123
99
def has_signature_for_revision_id(self, revision_id):
126
def get_mapping(self):
127
return default_mapping
129
102
def get_parent_map(self, revision_ids):
131
104
for revid in revision_ids:
132
105
if revid == revision.NULL_REVISION:
135
git_commit_id = self.lookup_git_revid(revid, self.get_mapping())
136
commit = self._git.commit(git_commit_id)
137
ret[revid] = tuple([self.get_mapping().revision_id_foreign_to_bzr(p.id) for p in commit.parents])
108
commit = self._git.commit(default_mapping.revision_id_bzr_to_foreign(revid))
109
ret[revid] = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
140
def lookup_git_revid(self, bzr_revid, mapping):
142
return mapping.revision_id_bzr_to_foreign(bzr_revid)
143
except errors.InvalidRevisionId:
144
raise errors.NoSuchRevision(bzr_revid, self)
146
112
def get_revision(self, revision_id):
147
git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
113
git_commit_id = default_mapping.revision_id_bzr_to_foreign(revision_id)
148
114
commit = self._git.commit(git_commit_id)
149
115
# print "fetched revision:", git_commit_id
150
revision = self._parse_rev(commit, self.get_mapping())
116
revision = self._parse_rev(commit)
153
119
def has_revision(self, revision_id):
162
128
return [self.get_revision(r) for r in revisions]
165
def _parse_rev(klass, commit, mapping):
131
def _parse_rev(klass, commit):
166
132
"""Convert a git commit to a bzr revision.
168
134
:return: a `bzrlib.revision.Revision` object.
171
raise AssertionError("Commit object can't be None")
172
rev = ForeignRevision(commit.id, mapping, mapping.revision_id_foreign_to_bzr(commit.id))
173
rev.parent_ids = tuple([mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
136
rev = revision.Revision(default_mapping.revision_id_foreign_to_bzr(commit.id))
137
rev.parent_ids = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
138
rev.inventory_sha1 = ""
174
139
rev.message = commit.message.decode("utf-8", "replace")
175
rev.committer = str(commit.committer).decode("utf-8", "replace")
176
rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
140
rev.committer = str(commit.committer)
141
rev.properties['author'] = str(commit.author)
177
142
rev.timestamp = time.mktime(commit.committed_date)
196
161
assert revision_id != None
197
162
return self.revision_tree(revision_id).inventory
199
def set_make_working_trees(self, trees):
203
165
def escape_file_id(file_id):
204
166
return file_id.replace('_', '__').replace(' ', '_s')
213
175
def __init__(self, repository, revision_id):
214
176
self._repository = repository
215
177
self.revision_id = revision_id
216
git_id = repository.lookup_git_revid(revision_id, repository.get_mapping())
178
git_id = default_mapping.revision_id_bzr_to_foreign(revision_id)
217
179
self.tree = repository._git.commit(git_id).tree
218
180
self._inventory = inventory.Inventory(revision_id=revision_id)
219
181
self._inventory.root.revision = revision_id
230
192
def _build_inventory(self, tree, ie, path):
231
193
assert isinstance(path, str)
232
for name, mode, hexsha in tree.entries():
233
basename = name.decode("utf-8")
194
for b in tree.contents:
195
basename = b.name.decode("utf-8")
237
child_path = urlutils.join(path, name)
199
child_path = urlutils.join(path, b.name)
238
200
file_id = escape_file_id(child_path.encode('utf-8'))
240
202
child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)
203
elif b.mode[0] == '1':
243
205
child_ie = inventory.InventoryFile(file_id, basename, ie.file_id)
244
206
child_ie.text_sha1 = osutils.sha_string(b.data)
207
elif b.mode[1] == '2':
246
208
child_ie = inventory.InventoryLink(file_id, basename, ie.file_id)
247
209
child_ie.text_sha1 = osutils.sha_string("")
249
211
raise AssertionError(
250
"Unknown file kind, perms=%r." % (mode,))
212
"Unknown file kind, perms=%r." % (b.mode,))
251
213
child_ie.text_id = b.id
252
214
child_ie.text_size = b.size
254
216
raise AssertionError(
255
"Unknown blob kind, perms=%r." % (mode,))
256
child_ie.executable = bool(int(mode[3:], 8) & 0111)
217
"Unknown blob kind, perms=%r." % (b.mode,))
218
child_ie.executable = bool(int(b.mode[3:], 8) & 0111)
257
219
child_ie.revision = self.revision_id
258
220
self._inventory.add(child_ie)
260
222
self._build_inventory(b, child_ie, child_path)