53
from bzrlib.plugins.git.refs import (
59
54
from bzrlib.foreign import ForeignBranch
57
def extract_tags(refs):
59
for k,v in refs.iteritems():
60
if k.startswith("refs/tags/") and not k.endswith("^{}"):
61
v = refs.get(k+"^{}", v)
62
ret[k[len("refs/tags/"):]] = v
62
66
class GitPullResult(branch.PullResult):
64
68
def _lookup_revno(self, revid):
112
116
del self.repository._git[name]
114
118
def set_tag(self, name, revid):
115
self.repository._git.refs[tag_name_to_ref(name)], _ = \
119
self.repository._git.refs["refs/tags/%s" % name], _ = \
116
120
self.branch.mapping.revision_id_bzr_to_foreign(revid)
119
123
class DictTagDict(LocalGitTagDict):
121
126
def __init__(self, branch, tags):
122
127
super(DictTagDict, self).__init__(branch)
123
128
self._tags = tags
152
158
class GitBranch(ForeignBranch):
153
159
"""An adapter to git repositories for bzr Branch objects."""
155
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
161
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
156
162
self.repository = repository
157
163
self._format = GitBranchFormat()
158
164
self.control_files = lockfiles
160
166
super(GitBranch, self).__init__(repository.get_mapping())
161
167
if tagsdict is not None:
162
168
self.tags = DictTagDict(self, tagsdict)
164
self.name = ref_to_branch_name(ref)
165
170
self._head = None
166
171
self.base = bzrdir.root_transport.base
172
if self.name != "HEAD":
173
self.base += ",%s" % self.name
168
175
def _get_checkout_format(self):
169
176
"""Return the most suitable metadir for a checkout of this branch.
191
198
nick = property(_get_nick, _set_nick)
193
200
def __repr__(self):
194
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
201
return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
197
203
def generate_revision_history(self, revid, old_revid=None):
198
204
# FIXME: Check that old_revid is in the ancestry of revid
242
248
class LocalGitBranch(GitBranch):
243
249
"""A local Git branch."""
245
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
246
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
248
if not name in repository._git.get_refs().keys():
249
raise errors.NotBranchError(self.base)
251
251
def create_checkout(self, to_location, revision_id=None, lightweight=False,
252
252
accelerator_tree=None, hardlink=False):
300
300
self.set_last_revision(revid)
302
302
def set_last_revision(self, revid):
303
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
303
(newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
304
305
self.head = newhead
306
307
def _set_head(self, value):
307
308
self._head = value
308
self.repository._git.refs[self.ref] = self._head
309
self.repository._git.refs[self.name] = self._head
309
310
self._clear_cached_state()
311
312
head = property(_get_head, _set_head)
386
387
interrepo = self._get_interrepo(self.source, self.target)
387
388
def determine_wants(heads):
388
if not self.source.ref in heads:
389
raise NoSuchRef(self.source.ref, heads.keys())
389
if not self.source.name in heads:
390
raise NoSuchRef(self.source.name, heads.keys())
390
391
if stop_revision is not None:
391
392
self._last_revid = stop_revision
392
393
head, mapping = self.source.repository.lookup_bzr_revision_id(
395
head = heads[self.source.ref]
396
head = heads[self.source.name]
396
397
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
398
399
if self.target.repository.has_revision(self._last_revid):
401
pack_hint, head = interrepo.fetch_objects(
402
_, head = interrepo.fetch_objects(
402
403
determine_wants, self.source.mapping, limit=limit)
403
if pack_hint is not None and self.target.repository._format.pack_compresses:
404
self.target.repository.pack(hint=pack_hint)
405
404
if head is not None:
406
405
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
408
407
prev_last_revid = None
410
409
prev_last_revid = self.target.last_revision()
411
self.target.generate_revision_history(self._last_revid,
410
self.target.generate_revision_history(self._last_revid, prev_last_revid)
415
413
def update_revisions(self, stop_revision=None, overwrite=False,
495
493
isinstance(target, RemoteGitBranch))
497
495
def _basic_push(self, overwrite=False, stop_revision=None):
498
from dulwich.protocol import ZERO_SHA
499
496
result = GitBranchPushResult()
500
497
result.source_branch = self.source
501
498
result.target_branch = self.target
503
500
stop_revision = self.source.last_revision()
504
501
# FIXME: Check for diverged branches
505
502
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] }
503
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
504
refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
508
505
result.new_revid = stop_revision
509
506
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
510
refs[tag_name_to_ref(name)] = sha
507
refs["refs/tags/%s" % name] = sha
512
509
self.target.repository.send_pack(get_changed_refs,
513
510
self.source.repository._git.object_store.generate_pack_contents)
550
547
return refs, stop_revision
552
549
def pull(self, stop_revision=None, overwrite=False,
553
possible_transports=None, run_hooks=True,local=False):
550
possible_transports=None, local=False):
554
551
# This type of branch can't be bound.
556
553
raise errors.LocalRequiresBoundBranch()
583
580
def push(self, overwrite=True, stop_revision=None,
584
581
_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]
582
raise NoPushSupport()
603
584
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
585
result = GitBranchPushResult()
607
586
result.source_branch = self.source
608
587
result.target_branch = self.target
589
result.old_revid = self.target.last_revision()
591
result.old_revid = revision.NULL_REVISION
609
592
if stop_revision is None:
610
593
stop_revision = self.source.last_revision()
611
594
# FIXME: Check for diverged branches
612
refs = { self.target.ref: stop_revision }
595
refs = { "refs/heads/master": stop_revision }
613
596
for name, revid in self.source.tags.get_tag_dict().iteritems():
614
597
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(
598
refs["refs/tags/%s" % name] = revid
599
revidmap, new_refs = self.target.repository.dfetch_refs(
617
600
self.source.repository, refs)
602
self.target.generate_revision_history(revidmap[stop_revision])
603
result.new_revid = revidmap[stop_revision]
605
result.new_revid = result.old_revid
618
606
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])