78
125
def supports_tags(self):
128
def make_tags(self, branch):
129
if getattr(branch.repository, "get_refs", None) is not None:
130
from bzrlib.plugins.git.remote import RemoteGitTagDict
131
return RemoteGitTagDict(branch)
133
return LocalGitTagDict(branch)
82
136
class GitBranch(ForeignBranch):
83
137
"""An adapter to git repositories for bzr Branch objects."""
85
def __init__(self, bzrdir, repository, name, head, lockfiles):
139
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
86
140
self.repository = repository
87
super(GitBranch, self).__init__(repository.get_mapping())
141
self._format = GitBranchFormat()
88
142
self.control_files = lockfiles
89
143
self.bzrdir = bzrdir
144
super(GitBranch, self).__init__(repository.get_mapping())
145
if tagsdict is not None:
146
self.tags = DictTagDict(self, tagsdict)
92
149
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
95
def dpull(self, source, stop_revision=None):
96
if stop_revision is None:
97
stop_revision = source.last_revision()
98
# FIXME: Check for diverged branches
99
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
151
def _get_checkout_format(self):
152
"""Return the most suitable metadir for a checkout of this branch.
153
Weaves are used if this branch's repository uses weaves.
155
return get_rich_root_format()
157
def get_child_submit_format(self):
158
"""Return the preferred format of submissions to this branch."""
159
ret = self.get_config().get_user_option("child_submit_format")
164
def _get_nick(self, local=False, possible_master_transports=None):
165
"""Find the nick name for this branch.
171
def _set_nick(self, nick):
172
raise NotImplementedError
174
nick = property(_get_nick, _set_nick)
177
return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
179
def generate_revision_history(self, revid, old_revid=None):
180
# FIXME: Check that old_revid is in the ancestry of revid
181
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
182
self._set_head(newhead)
103
184
def lock_write(self):
104
185
self.control_files.lock_write()
183
303
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)
194
class InterGitGenericBranch(branch.InterBranch):
307
class GitBranchPullResult(branch.PullResult):
309
def report(self, to_file):
311
if self.old_revid == self.new_revid:
312
to_file.write('No revisions to pull.\n')
314
to_file.write('Now on revision %d (git sha: %s).\n' %
315
(self.new_revno, self.new_git_head))
316
self._show_tag_conficts(to_file)
319
class GitBranchPushResult(branch.BranchPushResult):
321
def _lookup_revno(self, revid):
322
assert isinstance(revid, str), "was %r" % revid
323
# Try in source branch first, it'll be faster
325
return self.source_branch.revision_id_to_revno(revid)
326
except errors.NoSuchRevision:
327
# FIXME: Check using graph.find_distance_to_null() ?
328
return self.target_branch.revision_id_to_revno(revid)
332
return self._lookup_revno(self.old_revid)
336
return self._lookup_revno(self.new_revid)
339
class InterFromGitBranch(branch.GenericInterBranch):
340
"""InterBranch implementation that pulls from Git into bzr."""
197
343
def is_compatible(self, source, target):
198
return isinstance(source, GitBranch)
344
return (isinstance(source, GitBranch) and
345
not isinstance(target, GitBranch))
200
347
def update_revisions(self, stop_revision=None, overwrite=False,
202
349
"""See InterBranch.update_revisions()."""
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
350
interrepo = repository.InterRepository.get(self.source.repository,
351
self.target.repository)
205
353
self._last_revid = None
206
354
def determine_wants(heads):
207
355
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)
356
raise NoSuchRef(self.source.name, heads.keys())
357
if stop_revision is not None:
358
self._last_revid = stop_revision
359
self._head, mapping = self.source.repository.lookup_git_revid(
362
self._head = heads[self.source.name]
364
self.source.mapping.revision_id_foreign_to_bzr(self._head)
212
365
if self.target.repository.has_revision(self._last_revid):
215
368
interrepo.fetch_objects(determine_wants, self.source.mapping)
216
self.target.generate_revision_history(self._last_revid)
219
branch.InterBranch.register_optimiser(InterGitGenericBranch)
370
prev_last_revid = None
372
prev_last_revid = self.target.last_revision()
373
self.target.generate_revision_history(self._last_revid, prev_last_revid)
375
def pull(self, overwrite=False, stop_revision=None,
376
possible_transports=None, _hook_master=None, run_hooks=True,
377
_override_hook_target=None, local=False):
380
:param _hook_master: Private parameter - set the branch to
381
be supplied as the master to pull hooks.
382
:param run_hooks: Private parameter - if false, this branch
383
is being called because it's the master of the primary branch,
384
so it should not run its hooks.
385
:param _override_hook_target: Private parameter - set the branch to be
386
supplied as the target_branch to pull hooks.
388
# This type of branch can't be bound.
390
raise errors.LocalRequiresBoundBranch()
391
result = GitBranchPullResult()
392
result.source_branch = self.source
393
if _override_hook_target is None:
394
result.target_branch = self.target
396
result.target_branch = _override_hook_target
397
self.source.lock_read()
399
# We assume that during 'pull' the target repository is closer than
401
graph = self.target.repository.get_graph(self.source.repository)
402
result.old_revno, result.old_revid = \
403
self.target.last_revision_info()
404
self.update_revisions(stop_revision, overwrite=overwrite,
406
result.new_git_head = self._head
407
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
409
result.new_revno, result.new_revid = self.target.last_revision_info()
411
result.master_branch = _hook_master
412
result.local_branch = result.target_branch
414
result.master_branch = result.target_branch
415
result.local_branch = None
417
for hook in branch.Branch.hooks['post_pull']:
423
def _basic_push(self, overwrite=False, stop_revision=None):
424
result = branch.BranchPushResult()
425
result.source_branch = self.source
426
result.target_branch = self.target
427
graph = self.target.repository.get_graph(self.source.repository)
428
result.old_revno, result.old_revid = self.target.last_revision_info()
429
self.update_revisions(stop_revision, overwrite=overwrite,
431
result.new_git_head = self._head
432
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
434
result.new_revno, result.new_revid = self.target.last_revision_info()
438
class InterGitBranch(branch.GenericInterBranch):
439
"""InterBranch implementation that pulls between Git branches."""
442
class InterGitLocalRemoteBranch(InterGitBranch):
443
"""InterBranch that copies from a local to a remote git branch."""
446
def is_compatible(self, source, target):
447
from bzrlib.plugins.git.remote import RemoteGitBranch
448
return (isinstance(source, LocalGitBranch) and
449
isinstance(target, RemoteGitBranch))
451
def _basic_push(self, overwrite=False, stop_revision=None):
452
result = GitBranchPushResult()
453
result.source_branch = self.source
454
result.target_branch = self.target
455
if stop_revision is None:
456
stop_revision = self.source.last_revision()
457
# FIXME: Check for diverged branches
458
def get_changed_refs(old_refs):
459
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
460
refs = { "refs/heads/master": self.source.repository.lookup_git_revid(stop_revision)[0] }
461
result.new_revid = stop_revision
462
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
463
refs["refs/tags/%s" % name] = sha
465
self.target.repository.send_pack(get_changed_refs,
466
self.source.repository._git.object_store.generate_pack_contents)
470
class InterGitRemoteLocalBranch(InterGitBranch):
471
"""InterBranch that copies from a remote to a local git branch."""
474
def is_compatible(self, source, target):
475
from bzrlib.plugins.git.remote import RemoteGitBranch
476
return (isinstance(source, RemoteGitBranch) and
477
isinstance(target, LocalGitBranch))
479
def _basic_push(self, overwrite=False, stop_revision=None):
480
result = branch.BranchPushResult()
481
result.source_branch = self.source
482
result.target_branch = self.target
483
result.old_revid = self.target.last_revision()
484
refs, stop_revision = self.update_refs(stop_revision)
485
self.target.generate_revision_history(stop_revision, result.old_revid)
486
self.update_tags(refs)
487
result.new_revid = self.target.last_revision()
490
def update_tags(self, refs):
491
for name, revid in extract_tags(refs, self.target.mapping).iteritems():
492
self.target.tags.set_tag(name, revid)
494
def update_refs(self, stop_revision=None):
495
interrepo = repository.InterRepository.get(self.source.repository,
496
self.target.repository)
497
if stop_revision is None:
498
refs = interrepo.fetch_refs(branches=["HEAD"])
499
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
501
refs = interrepo.fetch_refs(revision_id=stop_revision)
502
return refs, stop_revision
504
def pull(self, stop_revision=None, overwrite=False,
505
possible_transports=None, local=False):
506
# This type of branch can't be bound.
508
raise errors.LocalRequiresBoundBranch()
509
result = GitPullResult()
510
result.source_branch = self.source
511
result.target_branch = self.target
512
result.old_revid = self.target.last_revision()
513
refs, stop_revision = self.update_refs(stop_revision)
514
self.target.generate_revision_history(stop_revision, result.old_revid)
515
self.update_tags(refs)
516
result.new_revid = self.target.last_revision()
520
class InterToGitBranch(branch.InterBranch):
521
"""InterBranch implementation that pulls from Git into bzr."""
524
def is_compatible(self, source, target):
525
return (not isinstance(source, GitBranch) and
526
isinstance(target, GitBranch))
528
def update_revisions(self, *args, **kwargs):
529
raise NoPushSupport()
531
def push(self, overwrite=True, stop_revision=None,
532
_override_hook_source_branch=None):
533
raise NoPushSupport()
535
def lossy_push(self, stop_revision=None):
536
result = GitBranchPushResult()
537
result.source_branch = self.source
538
result.target_branch = self.target
540
result.old_revid = self.target.last_revision()
542
result.old_revid = revision.NULL_REVISION
543
if stop_revision is None:
544
stop_revision = self.source.last_revision()
545
# FIXME: Check for diverged branches
546
refs = { "refs/heads/master": stop_revision }
547
for name, revid in self.source.tags.get_tag_dict().iteritems():
548
if self.source.repository.has_revision(revid):
549
refs["refs/tags/%s" % name] = revid
550
revidmap, new_refs = self.target.repository.dfetch_refs(
551
self.source.repository, refs)
553
self.target.generate_revision_history(revidmap[stop_revision])
554
result.new_revid = revidmap[stop_revision]
556
result.new_revid = result.old_revid
557
result.revidmap = revidmap
561
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
562
branch.InterBranch.register_optimiser(InterFromGitBranch)
563
branch.InterBranch.register_optimiser(InterToGitBranch)
564
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)