18
17
"""An adapter between a Git Repository and a Bazaar Branch"""
20
19
from bzrlib import (
27
from bzrlib.foreign import (
31
from bzrlib.plugins.git.commit import (
34
from bzrlib.plugins.git.mapping import (
39
from bzrlib.plugins.git.tree import (
42
from bzrlib.plugins.git.versionedfiles import (
48
from dulwich.objects import (
53
class GitRepository(ForeignRepository):
28
from bzrlib.plugins.git import (
34
class GitRepository(repository.Repository):
54
35
"""An adapter to git repositories for bzr."""
57
_commit_builder_class = GitCommitBuilder
60
37
def __init__(self, gitdir, lockfiles):
61
ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir,
63
from bzrlib.plugins.git import fetch, push
64
for optimiser in [fetch.InterRemoteGitNonGitRepository,
65
fetch.InterLocalGitNonGitRepository,
66
fetch.InterGitGitRepository,
67
push.InterToLocalGitRepository,
68
push.InterToRemoteGitRepository]:
69
repository.InterRepository.register_optimiser(optimiser)
39
self.control_files = lockfiles
40
gitdirectory = gitdir.transport.local_abspath('.')
41
self._git = model.GitModel(gitdirectory)
42
self._revision_cache = {}
44
def _ancestor_revisions(self, revision_ids):
45
if revision_ids is not None:
46
git_revisions = [gitrevid_from_bzr(r) for r in revision_ids]
49
for lines in self._git.ancestor_lines(git_revisions):
50
yield self._parse_rev(lines)
71
52
def is_shared(self):
74
55
def supports_rich_root(self):
77
def _warn_if_deprecated(self, branch=None):
78
# This class isn't deprecated
81
def get_mapping(self):
82
return default_mapping
84
def make_working_trees(self):
85
return not self._git.bare
87
def revision_graph_can_have_wrong_parents(self):
90
def dfetch(self, source, stop_revision):
91
interrepo = repository.InterRepository.get(source, self)
92
return interrepo.dfetch(stop_revision)
95
class LocalGitRepository(GitRepository):
96
"""Git repository on the file system."""
98
def __init__(self, gitdir, lockfiles):
99
GitRepository.__init__(self, gitdir, lockfiles)
100
self.base = gitdir.root_transport.base
101
self._git = gitdir._git
102
self.signatures = None
103
self.revisions = GitRevisions(self, self._git.object_store)
104
self.inventories = None
105
self.texts = GitTexts(self)
107
def _iter_revision_ids(self):
108
mapping = self.get_mapping()
109
for sha in self._git.object_store:
110
o = self._git.object_store[sha]
111
if not isinstance(o, Commit):
113
rev, roundtrip_revid, verifiers = mapping.import_commit(o,
114
self.lookup_foreign_revision_id)
115
yield o.id, rev.revision_id, roundtrip_revid
117
def all_revision_ids(self):
119
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
122
ret.add(roundtrip_revid)
125
def get_parent_map(self, revids):
127
for revision_id in revids:
128
assert isinstance(revision_id, str)
129
if revision_id == revision.NULL_REVISION:
130
parent_map[revision_id] = ()
132
hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
134
commit = self._git[hexsha]
137
parent_map[revision_id] = [
138
self.lookup_foreign_revision_id(p, mapping)
139
for p in commit.parents]
142
def get_ancestry(self, revision_id, topo_sorted=True):
143
"""See Repository.get_ancestry().
145
if revision_id is None:
146
return [None, revision.NULL_REVISION] + self._all_revision_ids()
147
assert isinstance(revision_id, str)
149
graph = self.get_graph()
150
for rev, parents in graph.iter_ancestry([revision_id]):
153
return [None] + ancestry
155
def get_signature_text(self, revision_id):
156
raise errors.NoSuchRevision(self, revision_id)
158
def pack(self, hint=None, clean_obsolete_packs=False):
159
self._git.object_store.pack_loose_objects()
161
def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
162
"""Lookup a revision id.
165
assert type(foreign_revid) is str
167
mapping = self.get_mapping()
168
from dulwich.protocol import (
171
if foreign_revid == ZERO_SHA:
172
return revision.NULL_REVISION
173
commit = self._git[foreign_revid]
174
rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
176
# FIXME: check testament before doing this?
178
return roundtrip_revid
58
def get_revision_graph(self, revision_id=None):
60
if revision_id is not None:
61
param = [ids.convert_revision_id_bzr_to_git(revision_id)]
180
return rev.revision_id
182
def has_signature_for_revision_id(self, revision_id):
185
def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
187
return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
188
except errors.InvalidRevisionId:
190
mapping = self.get_mapping()
192
return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
195
# Update refs from Git commit objects
196
# FIXME: Hitting this a lot will be very inefficient...
197
for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
198
if not roundtrip_revid:
200
refname = mapping.revid_as_refname(roundtrip_revid)
201
self._git.refs[refname] = git_sha
202
if roundtrip_revid == bzr_revid:
203
return git_sha, mapping
204
raise errors.NoSuchRevision(self, bzr_revid)
64
for node, parents in self._git.ancestry(param).iteritems():
65
bzr_node = ids.convert_revision_id_git_to_bzr(node)
66
bzr_parents = [ids.convert_revision_id_git_to_bzr(n)
68
result[bzr_node] = bzr_parents
71
def get_revision_graph_with_ghosts(self, revision_ids=None):
72
graph = deprecated_graph.Graph()
73
if revision_ids is not None:
74
revision_ids = [ids.convert_revision_id_bzr_to_git(r)
75
for r in revision_ids]
76
for node, parents in self._git.ancestry(revision_ids).iteritems():
77
bzr_node = ids.convert_revision_id_git_to_bzr(node)
78
bzr_parents = [ids.convert_revision_id_git_to_bzr(n)
81
graph.add_node(bzr_node, bzr_parents)
206
84
def get_revision(self, revision_id):
207
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
209
commit = self._git[git_commit_id]
211
raise errors.NoSuchRevision(self, revision_id)
212
revision, roundtrip_revid, verifiers = mapping.import_commit(
213
commit, self.lookup_foreign_revision_id)
214
assert revision is not None
215
# FIXME: check verifiers ?
217
revision.revision_id = roundtrip_revid
85
if revision_id in self._revision_cache:
86
return self._revision_cache[revision_id]
87
raw = self._git.rev_list(
88
[ids.convert_revision_id_bzr_to_git(revision_id)],
89
max_count=1, header=True)
90
return self._parse_rev(raw)
220
92
def has_revision(self, revision_id):
222
git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
223
except errors.NoSuchRevision:
94
self.get_revision(revision_id)
95
except NoSuchRevision:
225
return (git_commit_id in self._git)
227
def has_revisions(self, revision_ids):
228
return set(filter(self.has_revision, revision_ids))
230
def get_revisions(self, revids):
231
return [self.get_revision(r) for r in revids]
100
def get_revisions(self, revisions):
101
return [self.get_revision(r) for r in revisions]
104
def _parse_rev(klass, raw):
105
"""Parse a single git revision.
107
* The first line is the git commit id.
108
* Following lines conform to the 'name value' structure, until the
110
* All lines after the first blank line and until the NULL line have 4
111
leading spaces and constitute the commit message.
113
:param raw: sequence of newline-terminated strings, its last item is a
114
single NULL character.
115
:return: a `bzrlib.revision.Revision` object.
120
committer_was_set = False
121
revision_id = ids.convert_revision_id_git_to_bzr(raw[0][:-1])
122
rev = revision.Revision(revision_id)
123
rev.inventory_sha1 = ""
124
assert raw[-1] == '\x00', (
125
"Last item of raw was not a single NULL character.")
126
for line in raw[1:-1]:
128
assert line[:4] == ' ', (
129
"Unexpected line format in commit message: %r" % line)
130
message_lines.append(line[4:])
135
name, value = line[:-1].split(' ', 1)
137
rev.parent_ids.append(
138
ids.convert_revision_id_git_to_bzr(value))
141
author, timestamp, timezone = value.rsplit(' ', 2)
142
rev.properties['author'] = author
143
rev.properties['git-author-timestamp'] = timestamp
144
rev.properties['git-author-timezone'] = timezone
145
if not committer_was_set:
146
rev.committer = author
147
rev.timestamp = float(timestamp)
148
rev.timezone = klass._parse_tz(timezone)
150
if name == 'committer':
151
committer_was_set = True
152
committer, timestamp, timezone = value.rsplit(' ', 2)
153
rev.committer = committer
154
rev.timestamp = float(timestamp)
155
rev.timezone = klass._parse_tz(timezone)
158
rev.properties['git-tree-id'] = value
161
rev.message = ''.join(message_lines)
165
def _parse_tz(klass, tz):
166
"""Parse a timezone specification in the [+|-]HHMM format.
168
:return: the timezone offset in seconds.
171
sign = {'+': +1, '-': -1}[tz[0]]
173
minutes = int(tz[3:])
174
return float(sign * 60 * (60 * hours + minutes))
233
176
def revision_trees(self, revids):
234
177
for revid in revids:
235
178
yield self.revision_tree(revid)
237
180
def revision_tree(self, revision_id):
238
revision_id = revision.ensure_null(revision_id)
239
if revision_id == revision.NULL_REVISION:
240
inv = inventory.Inventory(root_id=None)
241
inv.revision_id = revision_id
242
return revisiontree.RevisionTree(self, inv, revision_id)
243
181
return GitRevisionTree(self, revision_id)
245
183
def get_inventory(self, revision_id):
246
assert revision_id != None
247
return self.revision_tree(revision_id).inventory
249
def set_make_working_trees(self, trees):
252
def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
254
return self._git.fetch_objects(determine_wants, graph_walker, progress)
256
def _get_versioned_file_checker(self, text_key_references=None,
258
return GitVersionedFileChecker(self,
259
text_key_references=text_key_references, ancestors=ancestors)
262
class GitVersionedFileChecker(repository._VersionedFileChecker):
266
def _check_file_version_parents(self, texts, progress_bar):
270
class GitRepositoryFormat(repository.RepositoryFormat):
271
"""Git repository format."""
273
supports_tree_reference = False
274
rich_root_data = True
276
def get_format_description(self):
277
return "Git Repository"
279
def initialize(self, url, shared=False, _internal=False):
280
raise errors.UninitializableFormat(self)
282
def check_conversion_target(self, target_repo_format):
283
return target_repo_format.rich_root_data
285
def get_foreign_tests_repository_factory(self):
286
from bzrlib.plugins.git.tests.test_repository import (
287
ForeignTestsRepositoryFactory,
289
return ForeignTestsRepositoryFactory()
291
def network_name(self):
184
if revision_id is None:
185
revision_id = revision.NULL_REVISION
186
if revision_id == revision.NULL_REVISION:
187
return inventory.Inventory(
188
revision_id=revision_id, root_id=None)
189
git_commit = ids.convert_revision_id_bzr_to_git(revision_id)
190
git_inventory = self._git.get_inventory(git_commit)
191
return self._parse_inventory(revision_id, git_inventory)
194
def _parse_inventory(klass, revid, git_inv):
195
# For now, git inventory do not have root ids. It is not clear that we
196
# can reliably support root ids. -- David Allouche 2007-12-28
197
inv = inventory.Inventory(revision_id=revid)
198
for perms, git_kind, git_id, path in git_inv:
201
if git_kind == 'blob':
205
executable = bool(int(perms[-3:], 8) & 0111)
206
elif perms[1] == '2':
209
raise AssertionError(
210
"Unknown blob kind, perms=%r." % (perms,))
211
elif git_kind == 'tree':
214
raise AssertionError(
215
"Unknown git entry kind: %r" % (git_kind,))
216
# XXX: Maybe the file id should be prefixed by file kind, so when
217
# the kind of path changes, the id changes too.
218
# -- David Allouche 2007-12-28.
219
entry = inv.add_path(path, kind, file_id=path.encode('utf-8'))
220
entry.text_sha1 = text_sha1
221
entry.executable = executable
225
class GitRevisionTree(revisiontree.RevisionTree):
227
def __init__(self, repository, revision_id):
228
if revision_id is None:
229
revision_id = revision.NULL_REVISION
230
self._inventory = repository.get_inventory(revision_id)
231
self._repository = repository
232
self._revision_id = revision_id
234
def get_file_lines(self, file_id):
235
obj_id = self._inventory[file_id].text_sha1
236
return self._repository._git.cat_file('blob', obj_id)