17
18
"""An adapter between a Git Branch and a Bazaar Branch"""
20
from dulwich.objects import (
19
25
from bzrlib import (
26
from bzrlib.decorators import needs_read_lock
27
from bzrlib.trace import mutter
29
from bzrlib.plugins.git.foreign import ForeignBranch
30
from bzrlib.plugins.git.errors import LightWeightCheckoutsNotSupported
32
from dulwich.objects import (
37
class GitTagDict(tag.BasicTags):
34
from bzrlib.decorators import (
37
from bzrlib.trace import (
42
from bzrlib.plugins.git.config import (
45
from bzrlib.plugins.git.errors import (
50
class GitPullResult(branch.PullResult):
52
def _lookup_revno(self, revid):
53
assert isinstance(revid, str), "was %r" % revid
54
# Try in source branch first, it'll be faster
55
return self.target_branch.revision_id_to_revno(revid)
59
return self._lookup_revno(self.old_revid)
63
return self._lookup_revno(self.new_revid)
66
class LocalGitTagDict(tag.BasicTags):
67
"""Dictionary with tags in a local repository."""
39
69
def __init__(self, branch):
40
70
self.branch = branch
78
96
def supports_tags(self):
82
class GitBranch(ForeignBranch):
99
def make_tags(self, branch):
100
if getattr(branch.repository, "get_refs", None) is not None:
101
from bzrlib.plugins.git.remote import RemoteGitTagDict
102
return RemoteGitTagDict(branch)
104
return LocalGitTagDict(branch)
107
class GitBranch(foreign.ForeignBranch):
83
108
"""An adapter to git repositories for bzr Branch objects."""
85
110
def __init__(self, bzrdir, repository, name, head, lockfiles):
86
111
self.repository = repository
87
super(GitBranch, self).__init__(repository.get_mapping())
112
self._format = GitBranchFormat()
88
113
self.control_files = lockfiles
89
114
self.bzrdir = bzrdir
115
super(GitBranch, self).__init__(repository.get_mapping())
92
118
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
120
def _get_nick(self, local=False, possible_master_transports=None):
121
"""Find the nick name for this branch.
127
def _set_nick(self, nick):
128
raise NotImplementedError
130
nick = property(_get_nick, _set_nick)
95
132
def dpull(self, source, stop_revision=None):
96
133
if stop_revision is None:
97
134
stop_revision = source.last_revision()
98
135
# FIXME: Check for diverged branches
99
136
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
138
self.generate_revision_history(revidmap[stop_revision])
141
def generate_revision_history(self, revid, old_revid=None):
142
# FIXME: Check that old_revid is in the ancestry of revid
143
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
144
self._set_head(newhead)
146
def _set_head(self, head):
148
self.repository._git.set_ref(self.name, self.head)
103
150
def lock_write(self):
104
151
self.control_files.lock_write()
133
183
return revision.NULL_REVISION
134
184
return self.mapping.revision_id_foreign_to_bzr(self.head)
136
def create_checkout(self, to_location, revision_id=None,
137
lightweight=False, accelerator_tree=None, hardlink=False):
186
def _get_checkout_format(self):
187
"""Return the most suitable metadir for a checkout of this branch.
188
Weaves are used if this branch's repository uses weaves.
190
format = self.repository.bzrdir.checkout_metadir()
191
format.set_branch_format(self._format)
194
def create_checkout(self, to_location, revision_id=None, lightweight=False,
195
accelerator_tree=None, hardlink=False):
139
raise LightWeightCheckoutsNotSupported()
140
return self._create_heavyweight_checkout(to_location, revision_id, hardlink)
197
t = transport.get_transport(to_location)
199
format = self._get_checkout_format()
200
checkout = format.initialize_on_transport(t)
201
from_branch = branch.BranchReferenceFormat().initialize(checkout,
203
tree = checkout.create_workingtree(revision_id,
204
from_branch=from_branch, hardlink=hardlink)
207
return self._create_heavyweight_checkout(to_location, revision_id,
142
210
def _create_heavyweight_checkout(self, to_location, revision_id=None,
183
249
def supports_tags(self):
186
def sprout(self, to_bzrdir, revision_id=None):
187
"""See Branch.sprout()."""
188
result = to_bzrdir.create_branch()
189
self.copy_content_into(result, revision_id=revision_id)
190
result.set_parent(self.bzrdir.root_transport.base)
253
class GitBranchPullResult(branch.PullResult):
255
def report(self, to_file):
257
if self.old_revid == self.new_revid:
258
to_file.write('No revisions to pull.\n')
260
to_file.write('Now on revision %d (git sha: %s).\n' %
261
(self.new_revno, self.new_git_head))
262
self._show_tag_conficts(to_file)
194
265
class InterGitGenericBranch(branch.InterBranch):
266
"""InterBranch implementation that pulls from Git into bzr."""
197
269
def is_compatible(self, source, target):
198
return isinstance(source, GitBranch)
270
return (isinstance(source, GitBranch) and
271
not isinstance(target, GitBranch))
200
273
def update_revisions(self, stop_revision=None, overwrite=False,
202
275
"""See InterBranch.update_revisions()."""
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
276
interrepo = repository.InterRepository.get(self.source.repository,
277
self.target.repository)
205
279
self._last_revid = None
206
280
def determine_wants(heads):
207
281
if not self.source.name in heads:
208
raise BzrError("No such remote branch '%s', found: %r" % (
209
self.source.name, heads.keys()))
210
head = heads[self.source.name]
211
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
282
raise NoSuchRef(self.source.name, heads.keys())
283
if stop_revision is not None:
284
self._last_revid = stop_revision
285
self._head, mapping = self.source.repository.lookup_git_revid(
288
self._head = heads[self.source.name]
290
self.source.mapping.revision_id_foreign_to_bzr(self._head)
212
291
if self.target.repository.has_revision(self._last_revid):
215
294
interrepo.fetch_objects(determine_wants, self.source.mapping)
216
self.target.generate_revision_history(self._last_revid)
296
prev_last_revid = None
298
prev_last_revid = self.target.last_revision()
299
self.target.generate_revision_history(self._last_revid, prev_last_revid)
301
def pull(self, overwrite=False, stop_revision=None,
302
possible_transports=None, _hook_master=None, run_hooks=True,
303
_override_hook_target=None):
306
:param _hook_master: Private parameter - set the branch to
307
be supplied as the master to pull hooks.
308
:param run_hooks: Private parameter - if false, this branch
309
is being called because it's the master of the primary branch,
310
so it should not run its hooks.
311
:param _override_hook_target: Private parameter - set the branch to be
312
supplied as the target_branch to pull hooks.
314
result = GitBranchPullResult()
315
result.source_branch = self.source
316
if _override_hook_target is None:
317
result.target_branch = self.target
319
result.target_branch = _override_hook_target
320
self.source.lock_read()
322
# We assume that during 'pull' the target repository is closer than
324
graph = self.target.repository.get_graph(self.source.repository)
325
result.old_revno, result.old_revid = \
326
self.target.last_revision_info()
327
self.update_revisions(stop_revision, overwrite=overwrite,
329
result.new_git_head = self._head
330
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
332
result.new_revno, result.new_revid = self.target.last_revision_info()
334
result.master_branch = _hook_master
335
result.local_branch = result.target_branch
337
result.master_branch = result.target_branch
338
result.local_branch = None
340
for hook in branch.Branch.hooks['post_pull']:
219
349
branch.InterBranch.register_optimiser(InterGitGenericBranch)
352
class InterGitRemoteLocalBranch(branch.InterBranch):
353
"""InterBranch implementation that pulls between Git branches."""
356
def is_compatible(self, source, target):
357
from bzrlib.plugins.git.remote import RemoteGitBranch
358
return (isinstance(source, RemoteGitBranch) and
359
isinstance(target, LocalGitBranch))
361
def pull(self, stop_revision=None, overwrite=False,
362
possible_transports=None):
363
result = GitPullResult()
364
result.source_branch = self.source
365
result.target_branch = self.target
366
interrepo = repository.InterRepository.get(self.source.repository,
367
self.target.repository)
368
result.old_revid = self.target.last_revision()
369
if stop_revision is None:
370
stop_revision = self.source.last_revision()
371
interrepo.fetch(revision_id=stop_revision)
372
self.target.generate_revision_history(stop_revision, result.old_revid)
373
result.new_revid = self.target.last_revision()
377
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)