42
40
from bzrlib.plugins.git.foreign import (
45
from bzrlib.plugins.git.mapping import default_mapping
43
from bzrlib.plugins.git.mapping import default_mapping, mapping_registry, inventory_to_tree_and_blobs, revision_to_commit
44
from bzrlib.plugins.git.versionedfiles import GitTexts
47
from bzrlib.plugins.git import git
50
49
class GitTags(object):
64
63
def __init__(self, gitdir, lockfiles):
65
64
ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
66
65
from bzrlib.plugins.git import fetch
67
repository.InterRepository.register_optimiser(fetch.InterFromGitRepository)
66
repository.InterRepository.register_optimiser(fetch.InterGitRepository)
67
repository.InterRepository.register_optimiser(fetch.InterGitNonGitRepository)
69
69
def is_shared(self):
94
96
self.signatures = versionedfiles.VirtualSignatureTexts(self)
95
97
self.revisions = versionedfiles.VirtualRevisionTexts(self)
98
self.inventories = versionedfiles.VirtualInventoryTexts(self)
99
self.texts = GitTexts(self)
96
100
self.tags = GitTags(self._git.get_tags())
98
102
def all_revision_ids(self):
123
127
if revision_id == revision.NULL_REVISION:
124
128
parent_map[revision_id] = ()
126
hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
130
hexsha, mapping = self.lookup_git_revid(revision_id)
127
131
commit = self._git.commit(hexsha)
128
132
if commit is None:
131
parent_map[revision_id] = [self.get_mapping().revision_id_foreign_to_bzr(p) for p in commit.parents]
135
parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
132
136
return parent_map
134
138
def get_ancestry(self, revision_id, topo_sorted=True):
146
150
ancestry.reverse()
153
def import_revision_gist(self, source, revid, parent_lookup):
154
"""Impor the gist of another revision into this Git repository.
158
rev = source.get_revision(revid)
159
for sha, object, path in inventory_to_tree_and_blobs(source, None, revid):
162
objects.append((object, path))
163
commit = revision_to_commit(rev, tree_sha, parent_lookup)
164
objects.append((commit, None))
165
self._git.object_store.add_objects(objects)
166
return commit.sha().hexdigest()
168
def dfetch(self, source, stop_revision):
169
if stop_revision is None:
170
raise NotImplementedError
176
graph = source.get_graph()
177
ancestry = [x for x in source.get_ancestry(stop_revision) if x is not None]
178
for revid in graph.iter_topo_order(ancestry):
179
if not self.has_revision(revid):
181
pb = ui.ui_factory.nested_progress_bar()
183
for i, revid in enumerate(todo):
184
pb.update("pushing revisions", i, len(todo))
185
git_commit = self.import_revision_gist(source, revid, gitidmap.__getitem__)
186
gitidmap[revid] = git_commit
187
git_revid = self.get_mapping().revision_id_foreign_to_bzr(git_commit)
188
revidmap[revid] = git_revid
191
source.fetch(self, revision_id=revidmap[stop_revision])
149
196
def get_signature_text(self, revision_id):
150
197
raise errors.NoSuchRevision(self, revision_id)
161
208
def has_signature_for_revision_id(self, revision_id):
164
def lookup_git_revid(self, bzr_revid, mapping):
211
def lookup_git_revid(self, bzr_revid):
166
return mapping.revision_id_bzr_to_foreign(bzr_revid)
213
return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
167
214
except errors.InvalidRevisionId:
168
215
raise errors.NoSuchRevision(self, bzr_revid)
170
217
def get_revision(self, revision_id):
171
git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
172
commit = self._git.commit(git_commit_id)
218
git_commit_id, mapping = self.lookup_git_revid(revision_id)
220
commit = self._git.commit(git_commit_id)
222
raise errors.NoSuchRevision(self, revision_id)
173
223
# print "fetched revision:", git_commit_id
175
raise errors.NoSuchRevision(self, revision_id)
176
revision = self._parse_rev(commit, self.get_mapping())
224
revision = mapping.import_commit(commit)
177
225
assert revision is not None
188
236
def get_revisions(self, revids):
189
237
return [self.get_revision(r) for r in revids]
192
def _parse_rev(klass, commit, mapping):
193
"""Convert a git commit to a bzr revision.
195
:return: a `bzrlib.revision.Revision` object.
198
raise AssertionError("Commit object can't be None")
199
rev = ForeignRevision(commit.id, mapping, mapping.revision_id_foreign_to_bzr(commit.id))
200
rev.parent_ids = tuple([mapping.revision_id_foreign_to_bzr(p) for p in commit.parents])
201
rev.message = commit.message.decode("utf-8", "replace")
202
rev.committer = str(commit.committer).decode("utf-8", "replace")
203
if commit.committer != commit.author:
204
rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
205
rev.timestamp = commit.commit_time
209
239
def revision_trees(self, revids):
210
240
for revid in revids:
211
241
yield self.revision_tree(revid)
227
257
def set_make_working_trees(self, trees):
230
def fetch_pack(self, determine_wants, graph_walker, pack_data):
231
raise NotImplementedError(self.fetch_pack)
234
def escape_file_id(file_id):
235
return file_id.replace('_', '__').replace(' ', '_s')
238
def unescape_file_id(file_id):
239
return file_id.replace("_s", " ").replace("__", "_")
260
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
261
return self._git.fetch_objects(determine_wants, graph_walker, progress)
242
264
class GitRevisionTree(revisiontree.RevisionTree):
244
266
def __init__(self, repository, revision_id):
245
267
self._repository = repository
246
268
self.revision_id = revision_id
247
git_id = repository.lookup_git_revid(revision_id, repository.get_mapping())
248
self.tree = repository._git.commit(git_id).tree
269
assert isinstance(revision_id, str)
270
git_id, self.mapping = repository.lookup_git_revid(revision_id)
272
commit = repository._git.commit(git_id)
274
raise errors.NoSuchRevision(repository, revision_id)
275
self.tree = commit.tree
249
276
self._inventory = inventory.Inventory(revision_id=revision_id)
250
277
self._inventory.root.revision = revision_id
251
278
self._build_inventory(self.tree, self._inventory.root, "")
267
294
child_path = name
269
296
child_path = urlutils.join(path, name)
270
file_id = escape_file_id(child_path.encode('utf-8'))
297
file_id = self.mapping.generate_file_id(child_path)
271
298
entry_kind = (mode & 0700000) / 0100000
272
299
if entry_kind == 0:
273
300
child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)