50
from bzrlib.plugins.git.refs import (
54
57
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, default=None):
72
"""Map a branch name to a ref.
74
:param name: Branch name
81
if not name.startswith("refs/"):
82
return "refs/heads/%s" % name
87
def ref_to_branch_name(ref):
88
"""Map a ref to a branch name
91
:return: A branch name
95
if ref.startswith("refs/heads/"):
96
return ref[len("refs/heads/"):]
97
raise ValueError("unable to map ref %s back to branch name")
100
60
class GitPullResult(branch.PullResult):
102
62
def _lookup_revno(self, revid):
270
244
# perhaps should escape this ?
271
245
if self.head is None:
272
246
return revision.NULL_REVISION
273
return self.mapping.revision_id_foreign_to_bzr(self.head)
247
return self.lookup_foreign_revision_id(self.head)
275
249
def _basic_push(self, target, overwrite=False, stop_revision=None):
276
250
return branch.InterBranch.get(self, target)._basic_push(
277
251
overwrite, stop_revision)
253
def lookup_foreign_revision_id(self, foreign_revid):
254
return self.repository.lookup_foreign_revision_id(foreign_revid,
280
258
class LocalGitBranch(GitBranch):
281
259
"""A local Git branch."""
283
261
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
284
262
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
285
263
lockfiles, tagsdict)
286
if not name in repository._git.get_refs().keys():
264
refs = repository._git.get_refs()
265
if not (name in refs.keys() or "HEAD" in refs.keys()):
287
266
raise errors.NotBranchError(self.base)
289
268
def create_checkout(self, to_location, revision_id=None, lightweight=False,
379
363
to_file.write('Now on revision %d.\n' % (self.new_revno,))
380
364
self._show_tag_conficts(to_file)
366
def _lookup_revno(self, revid):
367
assert isinstance(revid, str), "was %r" % revid
368
# Try in source branch first, it'll be faster
370
return self.source_branch.revision_id_to_revno(revid)
371
except errors.NoSuchRevision:
372
# FIXME: Check using graph.find_distance_to_null() ?
373
return self.target_branch.revision_id_to_revno(revid)
375
def _get_old_revno(self):
376
if self._old_revno is not None:
377
return self._old_revno
378
return self._lookup_revno(self.old_revid)
380
def _set_old_revno(self, revno):
381
self._old_revno = revno
383
old_revno = property(_get_old_revno, _set_old_revno)
385
def _get_new_revno(self):
386
if self._new_revno is not None:
387
return self._new_revno
388
return self._lookup_revno(self.new_revid)
390
def _set_new_revno(self, revno):
391
self._new_revno = revno
393
new_revno = property(_get_new_revno, _set_new_revno)
383
396
class GitBranchPushResult(branch.BranchPushResult):
425
438
interrepo = self._get_interrepo(self.source, self.target)
426
439
def determine_wants(heads):
427
if not self.source.ref in heads:
440
if self.source.ref is not None and not self.source.ref in heads:
428
441
raise NoSuchRef(self.source.ref, heads.keys())
429
442
if stop_revision is not None:
430
443
self._last_revid = stop_revision
431
444
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(
447
if self.source.ref is not None:
448
head = heads[self.source.ref]
451
self._last_revid = self.source.lookup_foreign_revision_id(head)
437
452
if self.target.repository.has_revision(self._last_revid):
440
455
pack_hint, head = interrepo.fetch_objects(
441
456
determine_wants, self.source.mapping, limit=limit)
442
if pack_hint is not None and self.target.repository._format.pack_compresses:
457
if (pack_hint is not None and
458
self.target.repository._format.pack_compresses):
443
459
self.target.repository.pack(hint=pack_hint)
444
460
if head is not None:
445
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
461
self._last_revid = self.source.lookup_foreign_revision_id(head)
447
463
prev_last_revid = None
541
558
stop_revision = self.source.last_revision()
542
559
# FIXME: Check for diverged branches
543
560
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))
561
result.old_revid = self.target.lookup_foreign_revision_id(old_refs.get(self.target.ref, ZERO_SHA))
545
562
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
546
563
result.new_revid = stop_revision
547
564
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
548
refs["refs/tags/%s" % name] = sha
565
refs[tag_name_to_ref(name)] = sha
550
567
self.target.repository.send_pack(get_changed_refs,
551
568
self.source.repository._git.object_store.generate_pack_contents)
618
635
def update_revisions(self, *args, **kwargs):
619
636
raise NoPushSupport()
621
def push(self, overwrite=True, stop_revision=None,
638
def _get_new_refs(self, stop_revision=None):
639
if stop_revision is None:
640
stop_revision = self.source.last_revision()
641
main_ref = self.target.ref or "refs/heads/master"
642
refs = { main_ref: stop_revision }
643
for name, revid in self.source.tags.get_tag_dict().iteritems():
644
if self.source.repository.has_revision(revid):
645
refs[tag_name_to_ref(name)] = revid
646
return refs, main_ref
648
def pull(self, overwrite=False, stop_revision=None, local=False,
649
possible_transports=None):
650
from dulwich.protocol import ZERO_SHA
651
result = GitBranchPullResult()
652
result.source_branch = self.source
653
result.target_branch = self.target
654
# FIXME: Check for diverged branches
655
old_refs = self.target.repository._git.get_refs()
656
refs = dict(old_refs)
657
new_refs, main_ref = self._get_new_refs(stop_revision)
658
refs.update(new_refs)
659
self.target.repository.fetch_refs(self.source.repository, refs)
660
result.old_revid = self.target.lookup_foreign_revision_id(
661
old_refs.get(main_ref, ZERO_SHA))
662
result.new_revid = refs[main_ref]
665
def push(self, overwrite=False, stop_revision=None,
622
666
_override_hook_source_branch=None):
623
raise NoPushSupport()
667
from dulwich.protocol import ZERO_SHA
668
result = GitBranchPushResult()
669
result.source_branch = self.source
670
result.target_branch = self.target
671
# FIXME: Check for diverged branches
672
old_refs = self.target.repository._git.get_refs()
673
refs = dict(old_refs)
674
new_refs, main_ref = self._get_new_refs(stop_revision)
675
refs.update(new_refs)
676
self.target.repository.fetch_refs(self.source.repository, refs)
677
result.old_revid = self.target.lookup_foreign_revision_id(
678
old_refs.get(main_ref, ZERO_SHA))
679
result.new_revid = refs[main_ref]
625
682
def lossy_push(self, stop_revision=None):
683
from dulwich.protocol import ZERO_SHA
626
684
result = GitBranchPushResult()
627
685
result.source_branch = self.source
628
686
result.target_branch = self.target
629
if stop_revision is None:
630
stop_revision = self.source.last_revision()
631
687
# 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(
688
refs, main_ref = self._get_new_refs(stop_revision)
689
result.revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
637
690
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
691
result.old_revid = self.target.lookup_foreign_revision_id(
692
old_refs.get(self.target.ref, ZERO_SHA))
693
result.new_revid = self.target.lookup_foreign_revision_id(