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):
66
for k,v in refs.iteritems():
67
if k.startswith("refs/tags/") and not k.endswith("^{}"):
68
v = refs.get(k+"^{}", v)
69
ret[k[len("refs/tags/"):]] = mapping.revision_id_foreign_to_bzr(v)
53
from bzrlib.plugins.git.refs import (
59
from bzrlib.foreign import ForeignBranch
73
62
class GitPullResult(branch.PullResult):
96
85
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)
87
for k,v in extract_tags(self.repository._git.get_refs()).iteritems():
89
obj = self.repository._git[v]
91
mutter("Tag %s points at unknown object %s, ignoring", v, obj)
100
93
while isinstance(obj, Tag):
102
obj = self.repository._git.get_object(v)
95
obj = self.repository._git[v]
103
96
if not isinstance(obj, Commit):
104
97
mutter("Tag %s points at object %r that is not a commit, "
105
98
"ignoring", k, obj)
107
100
ret[k] = self.branch.mapping.revision_id_foreign_to_bzr(v)
103
def _set_tag_dict(self, to_dict):
104
extra = set(self.repository._git.get_refs().keys())
105
for k, revid in to_dict.iteritems():
106
name = tag_name_to_ref(k)
109
self.set_tag(k, revid)
111
if name.startswith("refs/tags/"):
112
del self.repository._git[name]
110
114
def set_tag(self, name, revid):
111
self.repository._git.refs["refs/tags/%s" % name], _ = \
115
self.repository._git.refs[tag_name_to_ref(name)], _ = \
112
116
self.branch.mapping.revision_id_bzr_to_foreign(revid)
119
class DictTagDict(LocalGitTagDict):
121
def __init__(self, branch, tags):
122
super(DictTagDict, self).__init__(branch)
125
def get_tag_dict(self):
115
129
class GitBranchFormat(branch.BranchFormat):
117
131
def get_format_description(self):
118
132
return 'Git Branch'
134
def network_name(self):
120
137
def supports_tags(self):
140
def get_foreign_tests_branch_factory(self):
141
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
142
return ForeignTestsBranchFactory()
123
144
def make_tags(self, branch):
124
145
if getattr(branch.repository, "get_refs", None) is not None:
125
146
from bzrlib.plugins.git.remote import RemoteGitTagDict
131
152
class GitBranch(ForeignBranch):
132
153
"""An adapter to git repositories for bzr Branch objects."""
134
def __init__(self, bzrdir, repository, name, lockfiles):
155
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
135
156
self.repository = repository
136
157
self._format = GitBranchFormat()
137
158
self.control_files = lockfiles
138
159
self.bzrdir = bzrdir
139
160
super(GitBranch, self).__init__(repository.get_mapping())
161
if tagsdict is not None:
162
self.tags = DictTagDict(self, tagsdict)
164
self.name = ref_to_branch_name(ref)
141
165
self._head = None
142
self.base = bzrdir.transport.base
166
self.base = bzrdir.root_transport.base
168
def _get_checkout_format(self):
169
"""Return the most suitable metadir for a checkout of this branch.
170
Weaves are used if this branch's repository uses weaves.
172
return get_rich_root_format()
174
def get_child_submit_format(self):
175
"""Return the preferred format of submissions to this branch."""
176
ret = self.get_config().get_user_option("child_submit_format")
144
181
def _get_nick(self, local=False, possible_master_transports=None):
145
182
"""Find the nick name for this branch.
200
238
return branch.InterBranch.get(self, target)._basic_push(
201
239
overwrite, stop_revision)
204
242
class LocalGitBranch(GitBranch):
205
243
"""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)
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)
215
251
def create_checkout(self, to_location, revision_id=None, lightweight=False,
216
252
accelerator_tree=None, hardlink=False):
264
300
self.set_last_revision(revid)
266
302
def set_last_revision(self, revid):
267
(newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
303
(newhead, self.mapping) = self.repository.lookup_bzr_revision_id(revid)
269
304
self.head = newhead
271
306
def _set_head(self, value):
272
307
self._head = value
273
self.repository._git.refs[self.name] = self._head
308
self.repository._git.refs[self.ref] = self._head
274
309
self._clear_cached_state()
276
311
head = property(_get_head, _set_head)
328
365
"""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
368
def _get_interrepo(self, source, target):
369
return repository.InterRepository.get(source.repository,
373
def is_compatible(cls, source, target):
374
return (isinstance(source, GitBranch) and
375
not isinstance(target, GitBranch) and
376
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
378
def _update_revisions(self, stop_revision=None, overwrite=False,
379
graph=None, limit=None):
380
"""Like InterBranch.update_revisions(), but with additions.
382
Compared to the `update_revisions()` below, this function takes a
383
`limit` argument that limits how many git commits will be converted
384
and returns the new git head.
386
interrepo = self._get_interrepo(self.source, self.target)
342
387
def determine_wants(heads):
343
if not self.source.name in heads:
344
raise NoSuchRef(self.source.name, heads.keys())
388
if not self.source.ref in heads:
389
raise NoSuchRef(self.source.ref, heads.keys())
345
390
if stop_revision is not None:
346
391
self._last_revid = stop_revision
347
self._head, mapping = self.source.repository.lookup_git_revid(
392
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)
395
head = heads[self.source.ref]
396
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
353
398
if self.target.repository.has_revision(self._last_revid):
356
interrepo.fetch_objects(determine_wants, self.source.mapping)
401
pack_hint, head = interrepo.fetch_objects(
402
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)
406
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
358
408
prev_last_revid = None
360
410
prev_last_revid = self.target.last_revision()
361
self.target.generate_revision_history(self._last_revid, prev_last_revid)
411
self.target.generate_revision_history(self._last_revid,
415
def update_revisions(self, stop_revision=None, overwrite=False,
417
"""See InterBranch.update_revisions()."""
418
self._update_revisions(stop_revision, overwrite, graph)
363
420
def pull(self, overwrite=False, stop_revision=None,
364
421
possible_transports=None, _hook_master=None, run_hooks=True,
365
_override_hook_target=None, local=False):
422
_override_hook_target=None, local=False, limit=None):
366
423
"""See Branch.pull.
368
425
:param _hook_master: Private parameter - set the branch to
387
446
# We assume that during 'pull' the target repository is closer than
388
447
# the source one.
389
448
graph = self.target.repository.get_graph(self.source.repository)
390
result.old_revno, result.old_revid = \
449
(result.old_revno, result.old_revid) = \
391
450
self.target.last_revision_info()
392
self.update_revisions(stop_revision, overwrite=overwrite,
394
result.new_git_head = self._head
451
result.new_git_head = self._update_revisions(
452
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
395
453
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
397
result.new_revno, result.new_revid = self.target.last_revision_info()
455
(result.new_revno, result.new_revid) = \
456
self.target.last_revision_info()
399
458
result.master_branch = _hook_master
400
459
result.local_branch = result.target_branch
414
473
result.target_branch = self.target
415
474
graph = self.target.repository.get_graph(self.source.repository)
416
475
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
476
result.new_git_head = self._update_revisions(
477
stop_revision, overwrite=overwrite, graph=graph)
420
478
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
422
480
result.new_revno, result.new_revid = self.target.last_revision_info()
434
492
def is_compatible(self, source, target):
435
493
from bzrlib.plugins.git.remote import RemoteGitBranch
436
return (isinstance(source, LocalGitBranch) and
494
return (isinstance(source, LocalGitBranch) and
437
495
isinstance(target, RemoteGitBranch))
439
497
def _basic_push(self, overwrite=False, stop_revision=None):
498
from dulwich.protocol import ZERO_SHA
440
499
result = GitBranchPushResult()
441
500
result.source_branch = self.source
442
501
result.target_branch = self.target
444
503
stop_revision = self.source.last_revision()
445
504
# FIXME: Check for diverged branches
446
505
def get_changed_refs(old_refs):
447
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] }
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] }
449
508
result.new_revid = stop_revision
450
509
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
451
refs["refs/tags/%s" % name] = sha
510
refs[tag_name_to_ref(name)] = sha
453
self.target.repository.send_pack(get_changed_refs,
454
self.source.repository._git.object_store.generate_pack_contents)
512
self.target.repository.send_pack(get_changed_refs,
513
self.source.repository._git.object_store.generate_pack_contents)
478
537
def update_tags(self, refs):
479
for name, revid in extract_tags(refs, self.target.mapping).iteritems():
538
for name, v in extract_tags(refs).iteritems():
539
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
480
540
self.target.tags.set_tag(name, revid)
482
542
def update_refs(self, stop_revision=None):
483
interrepo = repository.InterRepository.get(self.source.repository,
543
interrepo = repository.InterRepository.get(self.source.repository,
484
544
self.target.repository)
485
545
if stop_revision is None:
486
546
refs = interrepo.fetch_refs(branches=["HEAD"])
489
549
refs = interrepo.fetch_refs(revision_id=stop_revision)
490
550
return refs, stop_revision
492
def pull(self, stop_revision=None, overwrite=False,
493
possible_transports=None, local=False):
552
def pull(self, stop_revision=None, overwrite=False,
553
possible_transports=None, run_hooks=True,local=False):
494
554
# This type of branch can't be bound.
496
556
raise errors.LocalRequiresBoundBranch()
504
564
result.new_revid = self.target.last_revision()
508
568
class InterToGitBranch(branch.InterBranch):
509
569
"""InterBranch implementation that pulls from Git into bzr."""
572
def _get_branch_formats_to_test():
512
576
def is_compatible(self, source, target):
513
return (not isinstance(source, GitBranch) and
577
return (not isinstance(source, GitBranch) and
514
578
isinstance(target, GitBranch))
516
580
def update_revisions(self, *args, **kwargs):
517
581
raise NoPushSupport()
519
def push(self, overwrite=True, stop_revision=None,
583
def push(self, overwrite=True, stop_revision=None,
520
584
_override_hook_source_branch=None):
521
585
raise NoPushSupport()
523
587
def lossy_push(self, stop_revision=None):
588
from dulwich.protocol import ZERO_SHA
524
589
result = GitBranchPushResult()
525
590
result.source_branch = self.source
526
591
result.target_branch = self.target
527
result.old_revid = self.target.last_revision()
528
592
if stop_revision is None:
529
593
stop_revision = self.source.last_revision()
530
594
# FIXME: Check for diverged branches
531
refs = { "refs/heads/master": stop_revision }
595
refs = { self.target.ref: stop_revision }
532
596
for name, revid in self.source.tags.get_tag_dict().iteritems():
533
597
if self.source.repository.has_revision(revid):
534
refs["refs/tags/%s" % name] = revid
535
revidmap, new_refs = self.target.repository.dfetch_refs(
598
refs[tag_name_to_ref(name)] = revid
599
revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
536
600
self.source.repository, refs)
538
self.target.generate_revision_history(revidmap[stop_revision])
539
result.new_revid = revidmap[stop_revision]
541
result.new_revid = result.old_revid
601
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
602
old_refs.get(self.target.ref, ZERO_SHA))
603
result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
604
new_refs[self.target.ref])
542
605
result.revidmap = revidmap