53
from bzrlib.plugins.git.refs import (
59
54
from bzrlib.foreign import ForeignBranch
57
def extract_tags(refs):
58
"""Extract the tags from a refs dictionary.
60
:param refs: Refs to extract the tags from.
61
:return: Dictionary mapping tag names to SHA1s.
64
for k,v in refs.iteritems():
65
if k.startswith("refs/tags/") and not k.endswith("^{}"):
66
v = refs.get(k+"^{}", v)
67
ret[k[len("refs/tags/"):]] = v
71
def branch_name_to_ref(name):
72
"""Map a branch name to a ref.
74
:param name: Branch name
77
if name is None or name == "HEAD":
80
return "refs/heads/%s" % name
85
def ref_to_branch_name(ref):
86
"""Map a ref to a branch name
89
:return: A branch name
93
if ref.startswith("refs/heads/"):
94
return ref[len("refs/heads/"):]
95
raise ValueError("unable to map ref %s back to branch name")
62
98
class GitPullResult(branch.PullResult):
64
100
def _lookup_revno(self, revid):
386
423
interrepo = self._get_interrepo(self.source, self.target)
387
424
def determine_wants(heads):
388
if not self.source.ref in heads:
389
raise NoSuchRef(self.source.ref, heads.keys())
425
if not self.source.name in heads:
426
raise NoSuchRef(self.source.name, heads.keys())
390
427
if stop_revision is not None:
391
428
self._last_revid = stop_revision
392
429
head, mapping = self.source.repository.lookup_bzr_revision_id(
395
head = heads[self.source.ref]
432
head = heads[self.source.name]
396
433
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
398
435
if self.target.repository.has_revision(self._last_revid):
495
532
isinstance(target, RemoteGitBranch))
497
534
def _basic_push(self, overwrite=False, stop_revision=None):
498
from dulwich.protocol import ZERO_SHA
499
535
result = GitBranchPushResult()
500
536
result.source_branch = self.source
501
537
result.target_branch = self.target
503
539
stop_revision = self.source.last_revision()
504
540
# FIXME: Check for diverged branches
505
541
def get_changed_refs(old_refs):
506
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, ZERO_SHA))
507
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
542
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
543
refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
508
544
result.new_revid = stop_revision
509
545
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
510
refs[tag_name_to_ref(name)] = sha
546
refs["refs/tags/%s" % name] = sha
512
548
self.target.repository.send_pack(get_changed_refs,
513
549
self.source.repository._git.object_store.generate_pack_contents)
583
619
def push(self, overwrite=True, stop_revision=None,
584
620
_override_hook_source_branch=None):
585
from dulwich.protocol import ZERO_SHA
586
result = GitBranchPushResult()
587
result.source_branch = self.source
588
result.target_branch = self.target
589
if stop_revision is None:
590
stop_revision = self.source.last_revision()
591
# FIXME: Check for diverged branches
592
refs = { self.target.ref: stop_revision }
593
for name, revid in self.source.tags.get_tag_dict().iteritems():
594
if self.source.repository.has_revision(revid):
595
refs[tag_name_to_ref(name)] = revid
596
old_refs = self.target.repository._git.get_refs()
597
self.target.repository.fetch_refs(self.source.repository, refs)
598
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
599
old_refs.get(self.target.ref, ZERO_SHA))
600
result.new_revid = refs[self.target.ref]
621
raise NoPushSupport()
603
623
def lossy_push(self, stop_revision=None):
604
return self._push(stop_revision=stop_revision, roundtrip=False)
605
from dulwich.protocol import ZERO_SHA
606
624
result = GitBranchPushResult()
607
625
result.source_branch = self.source
608
626
result.target_branch = self.target
628
result.old_revid = self.target.last_revision()
630
result.old_revid = revision.NULL_REVISION
609
631
if stop_revision is None:
610
632
stop_revision = self.source.last_revision()
611
633
# FIXME: Check for diverged branches
612
refs = { self.target.ref: stop_revision }
634
refs = { "refs/heads/master": stop_revision }
613
635
for name, revid in self.source.tags.get_tag_dict().iteritems():
614
636
if self.source.repository.has_revision(revid):
615
refs[tag_name_to_ref(name)] = revid
616
revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
637
refs["refs/tags/%s" % name] = revid
638
revidmap, new_refs = self.target.repository.dfetch_refs(
617
639
self.source.repository, refs)
641
self.target.generate_revision_history(revidmap[stop_revision])
642
result.new_revid = revidmap[stop_revision]
644
result.new_revid = result.old_revid
618
645
result.revidmap = revidmap
619
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
620
old_refs.get(self.target.ref, ZERO_SHA))
621
result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
622
new_refs[self.target.ref])