75
167
def get_format_description(self):
76
168
return 'Git Branch'
170
def network_name(self):
78
173
def supports_tags(self):
176
def get_foreign_tests_branch_factory(self):
177
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
178
return ForeignTestsBranchFactory()
180
def make_tags(self, branch):
181
if getattr(branch.repository, "get_refs", None) is not None:
182
from bzrlib.plugins.git.remote import RemoteGitTagDict
183
return RemoteGitTagDict(branch)
185
return LocalGitTagDict(branch)
82
188
class GitBranch(ForeignBranch):
83
189
"""An adapter to git repositories for bzr Branch objects."""
85
def __init__(self, bzrdir, repository, name, head, lockfiles):
191
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
86
192
self.repository = repository
87
super(GitBranch, self).__init__(repository.get_mapping())
193
self._format = GitBranchFormat()
88
194
self.control_files = lockfiles
89
195
self.bzrdir = bzrdir
92
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])
196
super(GitBranch, self).__init__(repository.get_mapping())
197
if tagsdict is not None:
198
self.tags = DictTagDict(self, tagsdict)
200
self.name = ref_to_branch_name(ref)
202
self.base = bzrdir.root_transport.base
204
def _get_checkout_format(self):
205
"""Return the most suitable metadir for a checkout of this branch.
206
Weaves are used if this branch's repository uses weaves.
208
return get_rich_root_format()
210
def get_child_submit_format(self):
211
"""Return the preferred format of submissions to this branch."""
212
ret = self.get_config().get_user_option("child_submit_format")
217
def _get_nick(self, local=False, possible_master_transports=None):
218
"""Find the nick name for this branch.
224
def _set_nick(self, nick):
225
raise NotImplementedError
227
nick = property(_get_nick, _set_nick)
230
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
233
def generate_revision_history(self, revid, old_revid=None):
234
# FIXME: Check that old_revid is in the ancestry of revid
235
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
236
self._set_head(newhead)
103
238
def lock_write(self):
104
239
self.control_files.lock_write()
106
241
def get_stacked_on_url(self):
107
242
# Git doesn't do stacking (yet...)
243
raise errors.UnstackableBranchFormat(self._format, self.base)
110
245
def get_parent(self):
111
246
"""See Branch.get_parent()."""
247
# FIXME: Set "origin" url from .git/config ?
114
250
def set_parent(self, url):
251
# FIXME: Set "origin" url in .git/config ?
117
254
def lock_read(self):
118
255
self.control_files.lock_read()
258
return self.control_files.is_locked()
120
260
def unlock(self):
121
261
self.control_files.unlock()
123
263
def get_physical_lock_status(self):
127
class LocalGitBranch(GitBranch):
130
267
def last_revision(self):
131
268
# perhaps should escape this ?
183
363
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):
197
def is_compatible(self, source, target):
198
return isinstance(source, GitBranch)
200
def update_revisions(self, stop_revision=None, overwrite=False,
202
"""See InterBranch.update_revisions()."""
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
205
self._last_revid = None
367
class GitBranchPullResult(branch.PullResult):
369
def report(self, to_file):
371
if self.old_revid == self.new_revid:
372
to_file.write('No revisions to pull.\n')
373
elif self.new_git_head is not None:
374
to_file.write('Now on revision %d (git sha: %s).\n' %
375
(self.new_revno, self.new_git_head))
377
to_file.write('Now on revision %d.\n' % (self.new_revno,))
378
self._show_tag_conficts(to_file)
381
class GitBranchPushResult(branch.BranchPushResult):
383
def _lookup_revno(self, revid):
384
assert isinstance(revid, str), "was %r" % revid
385
# Try in source branch first, it'll be faster
387
return self.source_branch.revision_id_to_revno(revid)
388
except errors.NoSuchRevision:
389
# FIXME: Check using graph.find_distance_to_null() ?
390
return self.target_branch.revision_id_to_revno(revid)
394
return self._lookup_revno(self.old_revid)
398
return self._lookup_revno(self.new_revid)
401
class InterFromGitBranch(branch.GenericInterBranch):
402
"""InterBranch implementation that pulls from Git into bzr."""
405
def _get_interrepo(self, source, target):
406
return repository.InterRepository.get(source.repository,
410
def is_compatible(cls, source, target):
411
return (isinstance(source, GitBranch) and
412
not isinstance(target, GitBranch) and
413
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
415
def _update_revisions(self, stop_revision=None, overwrite=False,
416
graph=None, limit=None):
417
"""Like InterBranch.update_revisions(), but with additions.
419
Compared to the `update_revisions()` below, this function takes a
420
`limit` argument that limits how many git commits will be converted
421
and returns the new git head.
423
interrepo = self._get_interrepo(self.source, self.target)
206
424
def determine_wants(heads):
207
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)
425
if not self.source.ref in heads:
426
raise NoSuchRef(self.source.ref, heads.keys())
427
if stop_revision is not None:
428
self._last_revid = stop_revision
429
head, mapping = self.source.repository.lookup_bzr_revision_id(
432
head = heads[self.source.ref]
433
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
212
435
if self.target.repository.has_revision(self._last_revid):
215
interrepo.fetch_objects(determine_wants, self.source.mapping)
216
self.target.generate_revision_history(self._last_revid)
219
branch.InterBranch.register_optimiser(InterGitGenericBranch)
438
pack_hint, head = interrepo.fetch_objects(
439
determine_wants, self.source.mapping, limit=limit)
440
if pack_hint is not None and self.target.repository._format.pack_compresses:
441
self.target.repository.pack(hint=pack_hint)
443
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
445
prev_last_revid = None
447
prev_last_revid = self.target.last_revision()
448
self.target.generate_revision_history(self._last_revid,
452
def update_revisions(self, stop_revision=None, overwrite=False,
454
"""See InterBranch.update_revisions()."""
455
self._update_revisions(stop_revision, overwrite, graph)
457
def pull(self, overwrite=False, stop_revision=None,
458
possible_transports=None, _hook_master=None, run_hooks=True,
459
_override_hook_target=None, local=False, limit=None):
462
:param _hook_master: Private parameter - set the branch to
463
be supplied as the master to pull hooks.
464
:param run_hooks: Private parameter - if false, this branch
465
is being called because it's the master of the primary branch,
466
so it should not run its hooks.
467
:param _override_hook_target: Private parameter - set the branch to be
468
supplied as the target_branch to pull hooks.
469
:param limit: Only import this many revisons. `None`, the default,
470
means import all revisions.
472
# This type of branch can't be bound.
474
raise errors.LocalRequiresBoundBranch()
475
result = GitBranchPullResult()
476
result.source_branch = self.source
477
if _override_hook_target is None:
478
result.target_branch = self.target
480
result.target_branch = _override_hook_target
481
self.source.lock_read()
483
# We assume that during 'pull' the target repository is closer than
485
graph = self.target.repository.get_graph(self.source.repository)
486
(result.old_revno, result.old_revid) = \
487
self.target.last_revision_info()
488
result.new_git_head = self._update_revisions(
489
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
490
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
492
(result.new_revno, result.new_revid) = \
493
self.target.last_revision_info()
495
result.master_branch = _hook_master
496
result.local_branch = result.target_branch
498
result.master_branch = result.target_branch
499
result.local_branch = None
501
for hook in branch.Branch.hooks['post_pull']:
507
def _basic_push(self, overwrite=False, stop_revision=None):
508
result = branch.BranchPushResult()
509
result.source_branch = self.source
510
result.target_branch = self.target
511
graph = self.target.repository.get_graph(self.source.repository)
512
result.old_revno, result.old_revid = self.target.last_revision_info()
513
result.new_git_head = self._update_revisions(
514
stop_revision, overwrite=overwrite, graph=graph)
515
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
517
result.new_revno, result.new_revid = self.target.last_revision_info()
521
class InterGitBranch(branch.GenericInterBranch):
522
"""InterBranch implementation that pulls between Git branches."""
525
class InterGitLocalRemoteBranch(InterGitBranch):
526
"""InterBranch that copies from a local to a remote git branch."""
529
def is_compatible(self, source, target):
530
from bzrlib.plugins.git.remote import RemoteGitBranch
531
return (isinstance(source, LocalGitBranch) and
532
isinstance(target, RemoteGitBranch))
534
def _basic_push(self, overwrite=False, stop_revision=None):
535
result = GitBranchPushResult()
536
result.source_branch = self.source
537
result.target_branch = self.target
538
if stop_revision is None:
539
stop_revision = self.source.last_revision()
540
# FIXME: Check for diverged branches
541
def get_changed_refs(old_refs):
542
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, "0" * 40))
543
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
544
result.new_revid = stop_revision
545
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
546
refs["refs/tags/%s" % name] = sha
548
self.target.repository.send_pack(get_changed_refs,
549
self.source.repository._git.object_store.generate_pack_contents)
553
class InterGitRemoteLocalBranch(InterGitBranch):
554
"""InterBranch that copies from a remote to a local git branch."""
557
def is_compatible(self, source, target):
558
from bzrlib.plugins.git.remote import RemoteGitBranch
559
return (isinstance(source, RemoteGitBranch) and
560
isinstance(target, LocalGitBranch))
562
def _basic_push(self, overwrite=False, stop_revision=None):
563
result = branch.BranchPushResult()
564
result.source_branch = self.source
565
result.target_branch = self.target
566
result.old_revid = self.target.last_revision()
567
refs, stop_revision = self.update_refs(stop_revision)
568
self.target.generate_revision_history(stop_revision, result.old_revid)
569
self.update_tags(refs)
570
result.new_revid = self.target.last_revision()
573
def update_tags(self, refs):
574
for name, v in extract_tags(refs).iteritems():
575
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
576
self.target.tags.set_tag(name, revid)
578
def update_refs(self, stop_revision=None):
579
interrepo = repository.InterRepository.get(self.source.repository,
580
self.target.repository)
581
if stop_revision is None:
582
refs = interrepo.fetch_refs(branches=["HEAD"])
583
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
585
refs = interrepo.fetch_refs(revision_id=stop_revision)
586
return refs, stop_revision
588
def pull(self, stop_revision=None, overwrite=False,
589
possible_transports=None, run_hooks=True,local=False):
590
# This type of branch can't be bound.
592
raise errors.LocalRequiresBoundBranch()
593
result = GitPullResult()
594
result.source_branch = self.source
595
result.target_branch = self.target
596
result.old_revid = self.target.last_revision()
597
refs, stop_revision = self.update_refs(stop_revision)
598
self.target.generate_revision_history(stop_revision, result.old_revid)
599
self.update_tags(refs)
600
result.new_revid = self.target.last_revision()
604
class InterToGitBranch(branch.InterBranch):
605
"""InterBranch implementation that pulls from Git into bzr."""
608
def _get_branch_formats_to_test():
612
def is_compatible(self, source, target):
613
return (not isinstance(source, GitBranch) and
614
isinstance(target, GitBranch))
616
def update_revisions(self, *args, **kwargs):
617
raise NoPushSupport()
619
def push(self, overwrite=True, stop_revision=None,
620
_override_hook_source_branch=None):
621
raise NoPushSupport()
623
def lossy_push(self, stop_revision=None):
624
result = GitBranchPushResult()
625
result.source_branch = self.source
626
result.target_branch = self.target
627
if stop_revision is None:
628
stop_revision = self.source.last_revision()
629
# FIXME: Check for diverged branches
630
refs = { self.target.ref: stop_revision }
631
for name, revid in self.source.tags.get_tag_dict().iteritems():
632
if self.source.repository.has_revision(revid):
633
refs["refs/tags/%s" % name] = revid
634
revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
635
self.source.repository, refs)
636
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
637
old_refs.get(self.target.ref, "0" * 40))
638
result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
639
new_refs[self.target.ref])
640
result.revidmap = revidmap
644
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
645
branch.InterBranch.register_optimiser(InterFromGitBranch)
646
branch.InterBranch.register_optimiser(InterToGitBranch)
647
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)