56
from bzrlib.foreign import ForeignBranch
58
class ForeignBranch(branch.Branch):
59
def __init__(self, mapping):
60
self.mapping = mapping
61
super(ForeignBranch, self).__init__()
64
def extract_tags(refs, mapping):
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.
66
64
for k,v in refs.iteritems():
67
65
if k.startswith("refs/tags/") and not k.endswith("^{}"):
68
66
v = refs.get(k+"^{}", v)
69
ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(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":
79
if not name.startswith("refs/"):
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")
73
98
class GitPullResult(branch.PullResult):
75
100
def _lookup_revno(self, revid):
96
121
def get_tag_dict(self):
98
for k,v in self.repository._git.refs.as_dict("refs/tags").iteritems():
99
obj = self.repository._git.get_object(v)
123
for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
125
obj = self.repository._git[v]
127
mutter("Tag %s points at unknown object %s, ignoring", v, obj)
100
129
while isinstance(obj, Tag):
101
130
v = obj.object[1]
102
obj = self.repository._git.get_object(v)
131
obj = self.repository._git[v]
103
132
if not isinstance(obj, Commit):
104
133
mutter("Tag %s points at object %r that is not a commit, "
105
134
"ignoring", k, obj)
107
136
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
139
def _set_tag_dict(self, to_dict):
140
extra = set(self.repository._git.get_refs().keys())
141
for k, revid in to_dict.iteritems():
142
name = "refs/tags/%s" % k
145
self.set_tag(k, revid)
147
if name.startswith("refs/tags/"):
148
del self.repository._git[name]
110
150
def set_tag(self, name, revid):
111
151
self.repository._git.refs["refs/tags/%s" % name], _ = \
112
152
self.branch.mapping.revision_id_bzr_to_foreign(revid)
155
class DictTagDict(LocalGitTagDict):
157
def __init__(self, branch, tags):
158
super(DictTagDict, self).__init__(branch)
161
def get_tag_dict(self):
115
165
class GitBranchFormat(branch.BranchFormat):
117
167
def get_format_description(self):
118
168
return 'Git Branch'
170
def network_name(self):
120
173
def supports_tags(self):
176
def get_foreign_tests_branch_factory(self):
177
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
178
return ForeignTestsBranchFactory()
123
180
def make_tags(self, branch):
124
181
if getattr(branch.repository, "get_refs", None) is not None:
125
182
from bzrlib.plugins.git.remote import RemoteGitTagDict
131
188
class GitBranch(ForeignBranch):
132
189
"""An adapter to git repositories for bzr Branch objects."""
134
def __init__(self, bzrdir, repository, name, lockfiles):
191
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
135
192
self.repository = repository
136
193
self._format = GitBranchFormat()
137
194
self.control_files = lockfiles
138
195
self.bzrdir = bzrdir
139
196
super(GitBranch, self).__init__(repository.get_mapping())
197
if tagsdict is not None:
198
self.tags = DictTagDict(self, tagsdict)
200
self.name = ref_to_branch_name(ref)
141
201
self._head = None
142
self.base = bzrdir.transport.base
202
self.base = bzrdir.root_transport.base
204
def _get_checkout_format(self):
205
"""Return the most suitable metadir for a checkout of this branch.
206
Weaves are used if this branch's repository uses weaves.
208
return get_rich_root_format()
210
def get_child_submit_format(self):
211
"""Return the preferred format of submissions to this branch."""
212
ret = self.get_config().get_user_option("child_submit_format")
144
217
def _get_nick(self, local=False, possible_master_transports=None):
145
218
"""Find the nick name for this branch.
200
274
return branch.InterBranch.get(self, target)._basic_push(
201
275
overwrite, stop_revision)
204
278
class LocalGitBranch(GitBranch):
205
279
"""A local Git branch."""
207
def _get_checkout_format(self):
208
"""Return the most suitable metadir for a checkout of this branch.
209
Weaves are used if this branch's repository uses weaves.
211
format = self.repository.bzrdir.checkout_metadir()
212
format.set_branch_format(self._format)
281
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
282
super(LocalGitBranch, self).__init__(bzrdir, repository, name,
284
if not name in repository._git.get_refs().keys():
285
raise errors.NotBranchError(self.base)
215
287
def create_checkout(self, to_location, revision_id=None, lightweight=False,
216
288
accelerator_tree=None, hardlink=False):
328
402
"""InterBranch implementation that pulls from Git into bzr."""
331
def is_compatible(self, source, target):
332
return (isinstance(source, GitBranch) and
333
not isinstance(target, GitBranch))
335
def update_revisions(self, stop_revision=None, overwrite=False,
337
"""See InterBranch.update_revisions()."""
338
interrepo = repository.InterRepository.get(self.source.repository,
339
self.target.repository)
341
self._last_revid = None
405
def _get_interrepo(self, source, target):
406
return repository.InterRepository.get(source.repository,
410
def is_compatible(cls, source, target):
411
return (isinstance(source, GitBranch) and
412
not isinstance(target, GitBranch) and
413
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
415
def _update_revisions(self, stop_revision=None, overwrite=False,
416
graph=None, limit=None):
417
"""Like InterBranch.update_revisions(), but with additions.
419
Compared to the `update_revisions()` below, this function takes a
420
`limit` argument that limits how many git commits will be converted
421
and returns the new git head.
423
interrepo = self._get_interrepo(self.source, self.target)
342
424
def determine_wants(heads):
343
if not self.source.name in heads:
344
raise NoSuchRef(self.source.name, heads.keys())
425
if not self.source.ref in heads:
426
raise NoSuchRef(self.source.ref, heads.keys())
345
427
if stop_revision is not None:
346
428
self._last_revid = stop_revision
347
self._head, mapping = self.source.repository.lookup_git_revid(
429
head, mapping = self.source.repository.lookup_bzr_revision_id(
350
self._head = heads[self.source.name]
352
self.source.mapping.revision_id_foreign_to_bzr(self._head)
432
head = heads[self.source.ref]
433
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
353
435
if self.target.repository.has_revision(self._last_revid):
356
interrepo.fetch_objects(determine_wants, self.source.mapping)
438
pack_hint, head = interrepo.fetch_objects(
439
determine_wants, self.source.mapping, limit=limit)
440
if pack_hint is not None and self.target.repository._format.pack_compresses:
441
self.target.repository.pack(hint=pack_hint)
443
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
358
445
prev_last_revid = None
360
447
prev_last_revid = self.target.last_revision()
361
self.target.generate_revision_history(self._last_revid, prev_last_revid)
448
self.target.generate_revision_history(self._last_revid,
452
def update_revisions(self, stop_revision=None, overwrite=False,
454
"""See InterBranch.update_revisions()."""
455
self._update_revisions(stop_revision, overwrite, graph)
363
457
def pull(self, overwrite=False, stop_revision=None,
364
458
possible_transports=None, _hook_master=None, run_hooks=True,
365
_override_hook_target=None, local=False):
459
_override_hook_target=None, local=False, limit=None):
366
460
"""See Branch.pull.
368
462
:param _hook_master: Private parameter - set the branch to
387
483
# We assume that during 'pull' the target repository is closer than
388
484
# the source one.
389
485
graph = self.target.repository.get_graph(self.source.repository)
390
result.old_revno, result.old_revid = \
486
(result.old_revno, result.old_revid) = \
391
487
self.target.last_revision_info()
392
self.update_revisions(stop_revision, overwrite=overwrite,
394
result.new_git_head = self._head
488
result.new_git_head = self._update_revisions(
489
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
395
490
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
397
result.new_revno, result.new_revid = self.target.last_revision_info()
492
(result.new_revno, result.new_revid) = \
493
self.target.last_revision_info()
399
495
result.master_branch = _hook_master
400
496
result.local_branch = result.target_branch
414
510
result.target_branch = self.target
415
511
graph = self.target.repository.get_graph(self.source.repository)
416
512
result.old_revno, result.old_revid = self.target.last_revision_info()
417
self.update_revisions(stop_revision, overwrite=overwrite,
419
result.new_git_head = self._head
513
result.new_git_head = self._update_revisions(
514
stop_revision, overwrite=overwrite, graph=graph)
420
515
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
422
517
result.new_revno, result.new_revid = self.target.last_revision_info()
445
540
# FIXME: Check for diverged branches
446
541
def get_changed_refs(old_refs):
447
542
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
448
refs = { "refs/heads/master": self.source.repository.lookup_git_revid(stop_revision)[0] }
543
refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
449
544
result.new_revid = stop_revision
450
545
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
451
546
refs["refs/tags/%s" % name] = sha
453
self.target.repository.send_pack(get_changed_refs,
454
self.source.repository._git.object_store.generate_pack_contents)
548
self.target.repository.send_pack(get_changed_refs,
549
self.source.repository._git.object_store.generate_pack_contents)
478
573
def update_tags(self, refs):
479
for name, revid in extract_tags(refs, self.target.mapping).iteritems():
574
for name, v in extract_tags(refs).iteritems():
575
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
480
576
self.target.tags.set_tag(name, revid)
482
578
def update_refs(self, stop_revision=None):
483
interrepo = repository.InterRepository.get(self.source.repository,
579
interrepo = repository.InterRepository.get(self.source.repository,
484
580
self.target.repository)
485
581
if stop_revision is None:
486
582
refs = interrepo.fetch_refs(branches=["HEAD"])
489
585
refs = interrepo.fetch_refs(revision_id=stop_revision)
490
586
return refs, stop_revision
492
def pull(self, stop_revision=None, overwrite=False,
493
possible_transports=None, local=False):
588
def pull(self, stop_revision=None, overwrite=False,
589
possible_transports=None, run_hooks=True,local=False):
494
590
# This type of branch can't be bound.
496
592
raise errors.LocalRequiresBoundBranch()
504
600
result.new_revid = self.target.last_revision()
508
604
class InterToGitBranch(branch.InterBranch):
509
605
"""InterBranch implementation that pulls from Git into bzr."""
608
def _get_branch_formats_to_test():
512
612
def is_compatible(self, source, target):
513
return (not isinstance(source, GitBranch) and
613
return (not isinstance(source, GitBranch) and
514
614
isinstance(target, GitBranch))
516
616
def update_revisions(self, *args, **kwargs):
517
617
raise NoPushSupport()
519
def push(self, overwrite=True, stop_revision=None,
619
def push(self, overwrite=True, stop_revision=None,
520
620
_override_hook_source_branch=None):
521
621
raise NoPushSupport()