75
169
def get_format_description(self):
76
170
return 'Git Branch'
172
def network_name(self):
78
175
def supports_tags(self):
178
def get_foreign_tests_branch_factory(self):
179
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
180
return ForeignTestsBranchFactory()
182
def make_tags(self, branch):
183
if getattr(branch.repository, "get_refs", None) is not None:
184
from bzrlib.plugins.git.remote import RemoteGitTagDict
185
return RemoteGitTagDict(branch)
187
return LocalGitTagDict(branch)
82
190
class GitBranch(ForeignBranch):
83
191
"""An adapter to git repositories for bzr Branch objects."""
85
def __init__(self, bzrdir, repository, name, head, lockfiles):
193
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
86
194
self.repository = repository
87
super(GitBranch, self).__init__(repository.get_mapping())
195
self._format = GitBranchFormat()
88
196
self.control_files = lockfiles
89
197
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])
198
super(GitBranch, self).__init__(repository.get_mapping())
199
if tagsdict is not None:
200
self.tags = DictTagDict(self, tagsdict)
202
self.name = ref_to_branch_name(ref)
204
self.base = bzrdir.root_transport.base
206
def _get_checkout_format(self):
207
"""Return the most suitable metadir for a checkout of this branch.
208
Weaves are used if this branch's repository uses weaves.
210
return get_rich_root_format()
212
def get_child_submit_format(self):
213
"""Return the preferred format of submissions to this branch."""
214
ret = self.get_config().get_user_option("child_submit_format")
219
def _get_nick(self, local=False, possible_master_transports=None):
220
"""Find the nick name for this branch.
226
def _set_nick(self, nick):
227
raise NotImplementedError
229
nick = property(_get_nick, _set_nick)
232
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
235
def generate_revision_history(self, revid, old_revid=None):
236
# FIXME: Check that old_revid is in the ancestry of revid
237
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
238
self._set_head(newhead)
103
240
def lock_write(self):
104
241
self.control_files.lock_write()
106
243
def get_stacked_on_url(self):
107
244
# Git doesn't do stacking (yet...)
245
raise errors.UnstackableBranchFormat(self._format, self.base)
110
247
def get_parent(self):
111
248
"""See Branch.get_parent()."""
249
# FIXME: Set "origin" url from .git/config ?
114
252
def set_parent(self, url):
253
# FIXME: Set "origin" url in .git/config ?
117
256
def lock_read(self):
118
257
self.control_files.lock_read()
260
return self.control_files.is_locked()
120
262
def unlock(self):
121
263
self.control_files.unlock()
123
265
def get_physical_lock_status(self):
127
class LocalGitBranch(GitBranch):
130
269
def last_revision(self):
131
270
# perhaps should escape this ?
183
365
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
369
class GitBranchPullResult(branch.PullResult):
371
def report(self, to_file):
373
if self.old_revid == self.new_revid:
374
to_file.write('No revisions to pull.\n')
375
elif self.new_git_head is not None:
376
to_file.write('Now on revision %d (git sha: %s).\n' %
377
(self.new_revno, self.new_git_head))
379
to_file.write('Now on revision %d.\n' % (self.new_revno,))
380
self._show_tag_conficts(to_file)
383
class GitBranchPushResult(branch.BranchPushResult):
385
def _lookup_revno(self, revid):
386
assert isinstance(revid, str), "was %r" % revid
387
# Try in source branch first, it'll be faster
389
return self.source_branch.revision_id_to_revno(revid)
390
except errors.NoSuchRevision:
391
# FIXME: Check using graph.find_distance_to_null() ?
392
return self.target_branch.revision_id_to_revno(revid)
396
return self._lookup_revno(self.old_revid)
400
return self._lookup_revno(self.new_revid)
403
class InterFromGitBranch(branch.GenericInterBranch):
404
"""InterBranch implementation that pulls from Git into bzr."""
407
def _get_interrepo(self, source, target):
408
return repository.InterRepository.get(source.repository,
412
def is_compatible(cls, source, target):
413
return (isinstance(source, GitBranch) and
414
not isinstance(target, GitBranch) and
415
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
417
def _update_revisions(self, stop_revision=None, overwrite=False,
418
graph=None, limit=None):
419
"""Like InterBranch.update_revisions(), but with additions.
421
Compared to the `update_revisions()` below, this function takes a
422
`limit` argument that limits how many git commits will be converted
423
and returns the new git head.
425
interrepo = self._get_interrepo(self.source, self.target)
206
426
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)
427
if not self.source.ref in heads:
428
raise NoSuchRef(self.source.ref, heads.keys())
429
if stop_revision is not None:
430
self._last_revid = stop_revision
431
head, mapping = self.source.repository.lookup_bzr_revision_id(
434
head = heads[self.source.ref]
435
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
212
437
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)
440
pack_hint, head = interrepo.fetch_objects(
441
determine_wants, self.source.mapping, limit=limit)
442
if pack_hint is not None and self.target.repository._format.pack_compresses:
443
self.target.repository.pack(hint=pack_hint)
445
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
447
prev_last_revid = None
449
prev_last_revid = self.target.last_revision()
450
self.target.generate_revision_history(self._last_revid,
454
def update_revisions(self, stop_revision=None, overwrite=False,
456
"""See InterBranch.update_revisions()."""
457
self._update_revisions(stop_revision, overwrite, graph)
459
def pull(self, overwrite=False, stop_revision=None,
460
possible_transports=None, _hook_master=None, run_hooks=True,
461
_override_hook_target=None, local=False, limit=None):
464
:param _hook_master: Private parameter - set the branch to
465
be supplied as the master to pull hooks.
466
:param run_hooks: Private parameter - if false, this branch
467
is being called because it's the master of the primary branch,
468
so it should not run its hooks.
469
:param _override_hook_target: Private parameter - set the branch to be
470
supplied as the target_branch to pull hooks.
471
:param limit: Only import this many revisons. `None`, the default,
472
means import all revisions.
474
# This type of branch can't be bound.
476
raise errors.LocalRequiresBoundBranch()
477
result = GitBranchPullResult()
478
result.source_branch = self.source
479
if _override_hook_target is None:
480
result.target_branch = self.target
482
result.target_branch = _override_hook_target
483
self.source.lock_read()
485
# We assume that during 'pull' the target repository is closer than
487
graph = self.target.repository.get_graph(self.source.repository)
488
(result.old_revno, result.old_revid) = \
489
self.target.last_revision_info()
490
result.new_git_head = self._update_revisions(
491
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
492
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
494
(result.new_revno, result.new_revid) = \
495
self.target.last_revision_info()
497
result.master_branch = _hook_master
498
result.local_branch = result.target_branch
500
result.master_branch = result.target_branch
501
result.local_branch = None
503
for hook in branch.Branch.hooks['post_pull']:
509
def _basic_push(self, overwrite=False, stop_revision=None):
510
result = branch.BranchPushResult()
511
result.source_branch = self.source
512
result.target_branch = self.target
513
graph = self.target.repository.get_graph(self.source.repository)
514
result.old_revno, result.old_revid = self.target.last_revision_info()
515
result.new_git_head = self._update_revisions(
516
stop_revision, overwrite=overwrite, graph=graph)
517
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
519
result.new_revno, result.new_revid = self.target.last_revision_info()
523
class InterGitBranch(branch.GenericInterBranch):
524
"""InterBranch implementation that pulls between Git branches."""
527
class InterGitLocalRemoteBranch(InterGitBranch):
528
"""InterBranch that copies from a local to a remote git branch."""
531
def is_compatible(self, source, target):
532
from bzrlib.plugins.git.remote import RemoteGitBranch
533
return (isinstance(source, LocalGitBranch) and
534
isinstance(target, RemoteGitBranch))
536
def _basic_push(self, overwrite=False, stop_revision=None):
537
result = GitBranchPushResult()
538
result.source_branch = self.source
539
result.target_branch = self.target
540
if stop_revision is None:
541
stop_revision = self.source.last_revision()
542
# FIXME: Check for diverged branches
543
def get_changed_refs(old_refs):
544
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, "0" * 40))
545
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
546
result.new_revid = stop_revision
547
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
548
refs["refs/tags/%s" % name] = sha
550
self.target.repository.send_pack(get_changed_refs,
551
self.source.repository._git.object_store.generate_pack_contents)
555
class InterGitRemoteLocalBranch(InterGitBranch):
556
"""InterBranch that copies from a remote to a local git branch."""
559
def is_compatible(self, source, target):
560
from bzrlib.plugins.git.remote import RemoteGitBranch
561
return (isinstance(source, RemoteGitBranch) and
562
isinstance(target, LocalGitBranch))
564
def _basic_push(self, overwrite=False, stop_revision=None):
565
result = branch.BranchPushResult()
566
result.source_branch = self.source
567
result.target_branch = self.target
568
result.old_revid = self.target.last_revision()
569
refs, stop_revision = self.update_refs(stop_revision)
570
self.target.generate_revision_history(stop_revision, result.old_revid)
571
self.update_tags(refs)
572
result.new_revid = self.target.last_revision()
575
def update_tags(self, refs):
576
for name, v in extract_tags(refs).iteritems():
577
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
578
self.target.tags.set_tag(name, revid)
580
def update_refs(self, stop_revision=None):
581
interrepo = repository.InterRepository.get(self.source.repository,
582
self.target.repository)
583
if stop_revision is None:
584
refs = interrepo.fetch_refs(branches=["HEAD"])
585
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
587
refs = interrepo.fetch_refs(revision_id=stop_revision)
588
return refs, stop_revision
590
def pull(self, stop_revision=None, overwrite=False,
591
possible_transports=None, run_hooks=True,local=False):
592
# This type of branch can't be bound.
594
raise errors.LocalRequiresBoundBranch()
595
result = GitPullResult()
596
result.source_branch = self.source
597
result.target_branch = self.target
598
result.old_revid = self.target.last_revision()
599
refs, stop_revision = self.update_refs(stop_revision)
600
self.target.generate_revision_history(stop_revision, result.old_revid)
601
self.update_tags(refs)
602
result.new_revid = self.target.last_revision()
606
class InterToGitBranch(branch.InterBranch):
607
"""InterBranch implementation that pulls from Git into bzr."""
610
def _get_branch_formats_to_test():
614
def is_compatible(self, source, target):
615
return (not isinstance(source, GitBranch) and
616
isinstance(target, GitBranch))
618
def update_revisions(self, *args, **kwargs):
619
raise NoPushSupport()
621
def push(self, overwrite=True, stop_revision=None,
622
_override_hook_source_branch=None):
623
raise NoPushSupport()
625
def lossy_push(self, stop_revision=None):
626
result = GitBranchPushResult()
627
result.source_branch = self.source
628
result.target_branch = self.target
629
if stop_revision is None:
630
stop_revision = self.source.last_revision()
631
# FIXME: Check for diverged branches
632
refs = { self.target.ref: stop_revision }
633
for name, revid in self.source.tags.get_tag_dict().iteritems():
634
if self.source.repository.has_revision(revid):
635
refs["refs/tags/%s" % name] = revid
636
revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
637
self.source.repository, refs)
638
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
639
old_refs.get(self.target.ref, "0" * 40))
640
result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
641
new_refs[self.target.ref])
642
result.revidmap = revidmap
646
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
647
branch.InterBranch.register_optimiser(InterFromGitBranch)
648
branch.InterBranch.register_optimiser(InterToGitBranch)
649
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)