110
111
for name in extra:
111
112
if name.startswith("refs/tags/"):
112
113
del self.repository._git[name]
114
115
def set_tag(self, name, revid):
115
116
self.repository._git.refs[tag_name_to_ref(name)], _ = \
116
self.branch.mapping.revision_id_bzr_to_foreign(revid)
117
self.branch.lookup_bzr_revision_id(revid)
119
120
class DictTagDict(LocalGitTagDict):
149
150
return LocalGitTagDict(branch)
153
class GitReadLock(object):
155
def __init__(self, unlock):
159
class GitWriteLock(object):
161
def __init__(self, unlock):
152
165
class GitBranch(ForeignBranch):
153
166
"""An adapter to git repositories for bzr Branch objects."""
169
182
"""Return the most suitable metadir for a checkout of this branch.
170
183
Weaves are used if this branch's repository uses weaves.
172
return get_rich_root_format()
185
return bzrdir.format_registry.make_bzrdir("default")
174
187
def get_child_submit_format(self):
175
188
"""Return the preferred format of submissions to this branch."""
232
247
# perhaps should escape this ?
233
248
if self.head is None:
234
249
return revision.NULL_REVISION
235
return self.mapping.revision_id_foreign_to_bzr(self.head)
250
return self.lookup_foreign_revision_id(self.head)
237
252
def _basic_push(self, target, overwrite=False, stop_revision=None):
238
253
return branch.InterBranch.get(self, target)._basic_push(
239
254
overwrite, stop_revision)
256
def lookup_foreign_revision_id(self, foreign_revid):
257
return self.repository.lookup_foreign_revision_id(foreign_revid,
260
def lookup_bzr_revision_id(self, revid):
261
return self.repository.lookup_bzr_revision_id(
262
revid, mapping=self.mapping)
242
265
class LocalGitBranch(GitBranch):
243
266
"""A local Git branch."""
245
268
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
246
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
269
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
247
270
lockfiles, tagsdict)
248
if not name in repository._git.get_refs().keys():
271
refs = repository._git.get_refs()
272
if not (name in refs.keys() or "HEAD" in refs.keys()):
249
273
raise errors.NotBranchError(self.base)
251
275
def create_checkout(self, to_location, revision_id=None, lightweight=False,
274
298
:return: WorkingTree object of checkout.
276
300
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
277
to_location, force_new_tree=False, format=get_rich_root_format())
301
to_location, force_new_tree=False)
278
302
checkout = checkout_branch.bzrdir
279
303
checkout_branch.bind(self)
280
304
# pull up to the specified revision_id to set the initial
306
330
def _set_head(self, value):
307
331
self._head = value
308
self.repository._git.refs[self.ref] = self._head
332
self.repository._git.refs[self.ref or "HEAD"] = self._head
309
333
self._clear_cached_state()
311
335
head = property(_get_head, _set_head)
399
423
class InterFromGitBranch(branch.GenericInterBranch):
400
424
"""InterBranch implementation that pulls from Git into bzr."""
427
def _get_branch_formats_to_test():
403
431
def _get_interrepo(self, source, target):
404
432
return repository.InterRepository.get(source.repository,
421
449
interrepo = self._get_interrepo(self.source, self.target)
422
450
def determine_wants(heads):
423
if not self.source.ref in heads:
451
if self.source.ref is not None and not self.source.ref in heads:
424
452
raise NoSuchRef(self.source.ref, heads.keys())
425
453
if stop_revision is not None:
426
454
self._last_revid = stop_revision
427
455
head, mapping = self.source.repository.lookup_bzr_revision_id(
430
head = heads[self.source.ref]
431
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
458
if self.source.ref is not None:
459
head = heads[self.source.ref]
462
self._last_revid = self.source.lookup_foreign_revision_id(head)
433
463
if self.target.repository.has_revision(self._last_revid):
436
pack_hint, head = interrepo.fetch_objects(
466
pack_hint, head, refs = interrepo.fetch_objects(
437
467
determine_wants, self.source.mapping, limit=limit)
438
if pack_hint is not None and self.target.repository._format.pack_compresses:
468
if (pack_hint is not None and
469
self.target.repository._format.pack_compresses):
439
470
self.target.repository.pack(hint=pack_hint)
440
471
if head is not None:
441
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
472
self._last_revid = self.source.lookup_foreign_revision_id(head)
443
474
prev_last_revid = None
523
554
class InterGitLocalRemoteBranch(InterGitBranch):
524
555
"""InterBranch that copies from a local to a remote git branch."""
558
def _get_branch_formats_to_test():
527
562
def is_compatible(self, source, target):
528
563
from bzrlib.plugins.git.remote import RemoteGitBranch
538
573
stop_revision = self.source.last_revision()
539
574
# FIXME: Check for diverged branches
540
575
def get_changed_refs(old_refs):
541
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, ZERO_SHA))
576
result.old_revid = self.target.lookup_foreign_revision_id(old_refs.get(self.target.ref, ZERO_SHA))
542
577
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
543
578
result.new_revid = stop_revision
544
579
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
552
587
class InterGitRemoteLocalBranch(InterGitBranch):
553
588
"""InterBranch that copies from a remote to a local git branch."""
591
def _get_branch_formats_to_test():
556
595
def is_compatible(self, source, target):
557
596
from bzrlib.plugins.git.remote import RemoteGitBranch
572
611
def update_tags(self, refs):
573
612
for name, v in extract_tags(refs).iteritems():
574
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
613
revid = self.target.lookup_foreign_revision_id(v)
575
614
self.target.tags.set_tag(name, revid)
577
616
def update_refs(self, stop_revision=None):
578
617
interrepo = repository.InterRepository.get(self.source.repository,
579
618
self.target.repository)
580
619
if stop_revision is None:
581
refs = interrepo.fetch_refs(branches=["HEAD"])
582
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
620
refs = interrepo.fetch(branches=["HEAD"])
621
stop_revision = self.target.lookup_foreign_revision_id(refs["HEAD"])
584
refs = interrepo.fetch_refs(revision_id=stop_revision)
623
refs = interrepo.fetch(revision_id=stop_revision)
585
624
return refs, stop_revision
587
626
def pull(self, stop_revision=None, overwrite=False,
603
class InterToGitBranch(branch.InterBranch):
642
class InterToGitBranch(branch.GenericInterBranch):
604
643
"""InterBranch implementation that pulls from Git into bzr."""
645
def __init__(self, source, target):
646
super(InterToGitBranch, self).__init__(source, target)
647
self.interrepo = repository.InterRepository.get(source.repository,
607
651
def _get_branch_formats_to_test():
611
655
def is_compatible(self, source, target):
618
662
def _get_new_refs(self, stop_revision=None):
619
663
if stop_revision is None:
620
664
stop_revision = self.source.last_revision()
621
refs = { self.target.ref: stop_revision }
665
assert type(stop_revision) is str
666
main_ref = self.target.ref or "refs/heads/master"
667
refs = { main_ref: (None, stop_revision) }
622
668
for name, revid in self.source.tags.get_tag_dict().iteritems():
623
669
if self.source.repository.has_revision(revid):
624
refs[tag_name_to_ref(name)] = revid
670
refs[tag_name_to_ref(name)] = (None, revid)
671
return refs, main_ref
627
673
def pull(self, overwrite=False, stop_revision=None, local=False,
628
674
possible_transports=None):
630
676
result = GitBranchPullResult()
631
677
result.source_branch = self.source
632
678
result.target_branch = self.target
633
# FIXME: Check for diverged branches
634
old_refs = self.target.repository._git.get_refs()
635
refs = dict(old_refs)
636
refs.update(self._get_new_refs(stop_revision))
637
self.target.repository.fetch_refs(self.source.repository, refs)
638
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
639
old_refs.get(self.target.ref, ZERO_SHA))
640
result.new_revid = refs[self.target.ref]
679
new_refs, main_ref = self._get_new_refs(stop_revision)
680
def update_refs(old_refs):
681
refs = dict(old_refs)
682
# FIXME: Check for diverged branches
683
refs.update(new_refs)
685
old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
686
result.old_revid = self.target.lookup_foreign_revision_id(
687
old_refs.get(main_ref, ZERO_SHA))
688
result.new_revid = new_refs[main_ref]
643
691
def push(self, overwrite=False, stop_revision=None,
646
694
result = GitBranchPushResult()
647
695
result.source_branch = self.source
648
696
result.target_branch = self.target
649
# FIXME: Check for diverged branches
650
old_refs = self.target.repository._git.get_refs()
651
refs = dict(old_refs)
652
refs.update(self._get_new_refs(stop_revision))
653
self.target.repository.fetch_refs(self.source.repository, refs)
654
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
655
old_refs.get(self.target.ref, ZERO_SHA))
656
result.new_revid = refs[self.target.ref]
697
new_refs, main_ref = self._get_new_refs(stop_revision)
698
def update_refs(old_refs):
699
refs = dict(old_refs)
700
# FIXME: Check for diverged branches
701
refs.update(new_refs)
703
old_refs, new_refs = self.interrepo.fetch_refs(update_refs)
704
(result.old_revid, old_sha1) = old_refs.get(main_ref, (ZERO_SHA, NULL_REVISION))
705
if result.old_revid is None:
706
result.old_revid = self.target.lookup_foreign_revision_id(old_sha1)
707
result.new_revid = new_refs[main_ref]
659
710
def lossy_push(self, stop_revision=None):
660
from dulwich.protocol import ZERO_SHA
661
711
result = GitBranchPushResult()
662
712
result.source_branch = self.source
663
713
result.target_branch = self.target
664
# FIXME: Check for diverged branches
665
refs = self._get_new_refs(stop_revision)
666
result.revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
667
self.source.repository, refs)
668
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
669
old_refs.get(self.target.ref, ZERO_SHA))
670
result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
671
new_refs[self.target.ref])
714
new_refs, main_ref = self._get_new_refs(stop_revision)
715
def update_refs(old_refs):
716
refs = dict(old_refs)
717
# FIXME: Check for diverged branches
718
refs.update(new_refs)
720
result.revidmap, old_refs, new_refs = self.interrepo.dfetch_refs(
722
result.old_revid = old_refs.get(self.target.ref, (None, NULL_REVISION))[1]
723
result.new_revid = new_refs[main_ref][1]