60
62
class GitPullResult(branch.PullResult):
63
"""Result of a pull from a Git branch."""
62
65
def _lookup_revno(self, revid):
63
66
assert isinstance(revid, str), "was %r" % revid
108
111
for name in extra:
109
112
if name.startswith("refs/tags/"):
110
113
del self.repository._git[name]
112
115
def set_tag(self, name, revid):
113
116
self.repository._git.refs[tag_name_to_ref(name)], _ = \
114
117
self.branch.mapping.revision_id_bzr_to_foreign(revid)
251
254
overwrite, stop_revision)
253
256
def lookup_foreign_revision_id(self, foreign_revid):
254
return self.repository.lookup_foreign_revision_id(foreign_revid,
257
return self.repository.lookup_foreign_revision_id(foreign_revid,
259
262
"""A local Git branch."""
261
264
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
262
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
265
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
263
266
lockfiles, tagsdict)
264
267
refs = repository._git.get_refs()
265
268
if not (name in refs.keys() or "HEAD" in refs.keys()):
281
284
return self._create_heavyweight_checkout(to_location, revision_id,
284
287
def _create_heavyweight_checkout(self, to_location, revision_id=None,
416
419
class InterFromGitBranch(branch.GenericInterBranch):
417
420
"""InterBranch implementation that pulls from Git into bzr."""
423
def _get_branch_formats_to_test():
420
427
def _get_interrepo(self, source, target):
421
428
return repository.InterRepository.get(source.repository,
452
459
if self.target.repository.has_revision(self._last_revid):
455
pack_hint, head = interrepo.fetch_objects(
462
pack_hint, head, refs = interrepo.fetch_objects(
456
463
determine_wants, self.source.mapping, limit=limit)
457
464
if (pack_hint is not None and
458
465
self.target.repository._format.pack_compresses):
543
550
class InterGitLocalRemoteBranch(InterGitBranch):
544
551
"""InterBranch that copies from a local to a remote git branch."""
554
def _get_branch_formats_to_test():
547
558
def is_compatible(self, source, target):
548
559
from bzrlib.plugins.git.remote import RemoteGitBranch
572
583
class InterGitRemoteLocalBranch(InterGitBranch):
573
584
"""InterBranch that copies from a remote to a local git branch."""
587
def _get_branch_formats_to_test():
576
591
def is_compatible(self, source, target):
577
592
from bzrlib.plugins.git.remote import RemoteGitBranch
598
613
interrepo = repository.InterRepository.get(self.source.repository,
599
614
self.target.repository)
600
615
if stop_revision is None:
601
refs = interrepo.fetch_refs(branches=["HEAD"])
616
refs = interrepo.fetch(branches=["HEAD"])
602
617
stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
604
refs = interrepo.fetch_refs(revision_id=stop_revision)
619
refs = interrepo.fetch(revision_id=stop_revision)
605
620
return refs, stop_revision
607
622
def pull(self, stop_revision=None, overwrite=False,
623
class InterToGitBranch(branch.InterBranch):
638
class InterToGitBranch(branch.GenericInterBranch):
624
639
"""InterBranch implementation that pulls from Git into bzr."""
626
641
def __init__(self, source, target):
643
658
def _get_new_refs(self, stop_revision=None):
644
659
if stop_revision is None:
645
660
stop_revision = self.source.last_revision()
661
assert type(stop_revision) is str
646
662
main_ref = self.target.ref or "refs/heads/master"
647
refs = { main_ref: stop_revision }
663
refs = { main_ref: (None, stop_revision) }
648
664
for name, revid in self.source.tags.get_tag_dict().iteritems():
649
665
if self.source.repository.has_revision(revid):
650
refs[tag_name_to_ref(name)] = revid
666
refs[tag_name_to_ref(name)] = (None, revid)
651
667
return refs, main_ref
653
669
def pull(self, overwrite=False, stop_revision=None, local=False,
656
672
result = GitBranchPullResult()
657
673
result.source_branch = self.source
658
674
result.target_branch = self.target
659
# FIXME: Check for diverged branches
660
old_refs = self.target.repository._git.get_refs()
661
refs = dict(old_refs)
662
675
new_refs, main_ref = self._get_new_refs(stop_revision)
663
refs.update(new_refs)
664
self.interrepo.fetch_refs(refs)
676
def update_refs(old_refs):
677
refs = dict(old_refs)
678
# FIXME: Check for diverged branches
679
refs.update(new_refs)
681
old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
665
682
result.old_revid = self.target.lookup_foreign_revision_id(
666
683
old_refs.get(main_ref, ZERO_SHA))
667
result.new_revid = refs[main_ref]
684
result.new_revid = new_refs[main_ref]
670
687
def push(self, overwrite=False, stop_revision=None,
673
690
result = GitBranchPushResult()
674
691
result.source_branch = self.source
675
692
result.target_branch = self.target
676
# FIXME: Check for diverged branches
677
old_refs = self.target.repository._git.get_refs()
678
refs = dict(old_refs)
679
693
new_refs, main_ref = self._get_new_refs(stop_revision)
680
refs.update(new_refs)
681
self.interrepo.fetch_refs(refs)
694
def update_refs(old_refs):
695
refs = dict(old_refs)
696
# FIXME: Check for diverged branches
697
refs.update(new_refs)
699
old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
682
700
result.old_revid = self.target.lookup_foreign_revision_id(
683
701
old_refs.get(main_ref, ZERO_SHA))
684
result.new_revid = refs[main_ref]
702
result.new_revid = new_refs[main_ref]
687
705
def lossy_push(self, stop_revision=None):
688
from dulwich.protocol import ZERO_SHA
689
706
result = GitBranchPushResult()
690
707
result.source_branch = self.source
691
708
result.target_branch = self.target
692
# FIXME: Check for diverged branches
693
refs, main_ref = self._get_new_refs(stop_revision)
694
result.revidmap, old_refs, new_refs = self.interrepo.dfetch_refs(refs)
695
result.old_revid = self.target.lookup_foreign_revision_id(
696
old_refs.get(self.target.ref, ZERO_SHA))
697
result.new_revid = self.target.lookup_foreign_revision_id(
709
new_refs, main_ref = self._get_new_refs(stop_revision)
710
def update_refs(old_refs):
711
refs = dict(old_refs)
712
# FIXME: Check for diverged branches
713
refs.update(new_refs)
715
result.revidmap, old_refs, new_refs = self.interrepo.dfetch_refs(
717
result.old_revid = old_refs.get(self.target.ref, (None, NULL_REVISION))[1]
718
result.new_revid = new_refs[main_ref][1]