115
152
class GitBranch(ForeignBranch):
116
153
"""An adapter to git repositories for bzr Branch objects."""
118
def __init__(self, bzrdir, repository, name, head, lockfiles):
155
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
119
156
self.repository = repository
120
157
self._format = GitBranchFormat()
121
158
self.control_files = lockfiles
122
159
self.bzrdir = bzrdir
123
160
super(GitBranch, self).__init__(repository.get_mapping())
126
self.base = bzrdir.transport.base
161
if tagsdict is not None:
162
self.tags = DictTagDict(self, tagsdict)
164
self.name = ref_to_branch_name(ref)
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")
128
181
def _get_nick(self, local=False, possible_master_transports=None):
129
182
"""Find the nick name for this branch.
191
234
return revision.NULL_REVISION
192
235
return self.mapping.revision_id_foreign_to_bzr(self.head)
194
def _get_checkout_format(self):
195
"""Return the most suitable metadir for a checkout of this branch.
196
Weaves are used if this branch's repository uses weaves.
198
format = self.repository.bzrdir.checkout_metadir()
199
format.set_branch_format(self._format)
237
def _basic_push(self, target, overwrite=False, stop_revision=None):
238
return branch.InterBranch.get(self, target)._basic_push(
239
overwrite, stop_revision)
242
class LocalGitBranch(GitBranch):
243
"""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)
202
251
def create_checkout(self, to_location, revision_id=None, lightweight=False,
203
252
accelerator_tree=None, hardlink=False):
264
333
if not is_quiet():
265
334
if self.old_revid == self.new_revid:
266
335
to_file.write('No revisions to pull.\n')
268
to_file.write('Now on revision %d (git sha: %s).\n' %
336
elif self.new_git_head is not None:
337
to_file.write('Now on revision %d (git sha: %s).\n' %
269
338
(self.new_revno, self.new_git_head))
340
to_file.write('Now on revision %d.\n' % (self.new_revno,))
270
341
self._show_tag_conficts(to_file)
273
class InterGitGenericBranch(branch.InterBranch):
344
class GitBranchPushResult(branch.BranchPushResult):
346
def _lookup_revno(self, revid):
347
assert isinstance(revid, str), "was %r" % revid
348
# Try in source branch first, it'll be faster
350
return self.source_branch.revision_id_to_revno(revid)
351
except errors.NoSuchRevision:
352
# FIXME: Check using graph.find_distance_to_null() ?
353
return self.target_branch.revision_id_to_revno(revid)
357
return self._lookup_revno(self.old_revid)
361
return self._lookup_revno(self.new_revid)
364
class InterFromGitBranch(branch.GenericInterBranch):
274
365
"""InterBranch implementation that pulls from Git into bzr."""
277
def is_compatible(self, source, target):
278
return (isinstance(source, GitBranch) and
279
not isinstance(target, GitBranch))
281
def update_revisions(self, stop_revision=None, overwrite=False,
283
"""See InterBranch.update_revisions()."""
284
interrepo = repository.InterRepository.get(self.source.repository,
285
self.target.repository)
287
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)
288
387
def determine_wants(heads):
289
if not self.source.name in heads:
290
raise NoSuchRef(self.source.name, heads.keys())
388
if not self.source.ref in heads:
389
raise NoSuchRef(self.source.ref, heads.keys())
291
390
if stop_revision is not None:
292
391
self._last_revid = stop_revision
293
self._head, mapping = self.source.repository.lookup_git_revid(
392
head, mapping = self.source.repository.lookup_bzr_revision_id(
296
self._head = heads[self.source.name]
298
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(
299
398
if self.target.repository.has_revision(self._last_revid):
302
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)
304
408
prev_last_revid = None
306
410
prev_last_revid = self.target.last_revision()
307
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)
309
420
def pull(self, overwrite=False, stop_revision=None,
310
421
possible_transports=None, _hook_master=None, run_hooks=True,
311
_override_hook_target=None):
422
_override_hook_target=None, local=False, limit=None):
312
423
"""See Branch.pull.
314
425
:param _hook_master: Private parameter - set the branch to
351
467
self.source.unlock()
357
branch.InterBranch.register_optimiser(InterGitGenericBranch)
360
class InterGitRemoteLocalBranch(branch.InterBranch):
470
def _basic_push(self, overwrite=False, stop_revision=None):
471
result = branch.BranchPushResult()
472
result.source_branch = self.source
473
result.target_branch = self.target
474
graph = self.target.repository.get_graph(self.source.repository)
475
result.old_revno, result.old_revid = self.target.last_revision_info()
476
result.new_git_head = self._update_revisions(
477
stop_revision, overwrite=overwrite, graph=graph)
478
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
480
result.new_revno, result.new_revid = self.target.last_revision_info()
484
class InterGitBranch(branch.GenericInterBranch):
361
485
"""InterBranch implementation that pulls between Git branches."""
364
def is_compatible(self, source, target):
365
from bzrlib.plugins.git.remote import RemoteGitBranch
366
return (isinstance(source, RemoteGitBranch) and
488
class InterGitLocalRemoteBranch(InterGitBranch):
489
"""InterBranch that copies from a local to a remote git branch."""
492
def is_compatible(self, source, target):
493
from bzrlib.plugins.git.remote import RemoteGitBranch
494
return (isinstance(source, LocalGitBranch) and
495
isinstance(target, RemoteGitBranch))
497
def _basic_push(self, overwrite=False, stop_revision=None):
498
from dulwich.protocol import ZERO_SHA
499
result = GitBranchPushResult()
500
result.source_branch = self.source
501
result.target_branch = self.target
502
if stop_revision is None:
503
stop_revision = self.source.last_revision()
504
# FIXME: Check for diverged branches
505
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] }
508
result.new_revid = stop_revision
509
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
510
refs[tag_name_to_ref(name)] = sha
512
self.target.repository.send_pack(get_changed_refs,
513
self.source.repository._git.object_store.generate_pack_contents)
517
class InterGitRemoteLocalBranch(InterGitBranch):
518
"""InterBranch that copies from a remote to a local git branch."""
521
def is_compatible(self, source, target):
522
from bzrlib.plugins.git.remote import RemoteGitBranch
523
return (isinstance(source, RemoteGitBranch) and
367
524
isinstance(target, LocalGitBranch))
369
def pull(self, stop_revision=None, overwrite=False,
370
possible_transports=None):
526
def _basic_push(self, overwrite=False, stop_revision=None):
527
result = branch.BranchPushResult()
528
result.source_branch = self.source
529
result.target_branch = self.target
530
result.old_revid = self.target.last_revision()
531
refs, stop_revision = self.update_refs(stop_revision)
532
self.target.generate_revision_history(stop_revision, result.old_revid)
533
self.update_tags(refs)
534
result.new_revid = self.target.last_revision()
537
def update_tags(self, refs):
538
for name, v in extract_tags(refs).iteritems():
539
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
540
self.target.tags.set_tag(name, revid)
542
def update_refs(self, stop_revision=None):
543
interrepo = repository.InterRepository.get(self.source.repository,
544
self.target.repository)
545
if stop_revision is None:
546
refs = interrepo.fetch_refs(branches=["HEAD"])
547
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
549
refs = interrepo.fetch_refs(revision_id=stop_revision)
550
return refs, stop_revision
552
def pull(self, stop_revision=None, overwrite=False,
553
possible_transports=None, run_hooks=True,local=False):
554
# This type of branch can't be bound.
556
raise errors.LocalRequiresBoundBranch()
371
557
result = GitPullResult()
372
558
result.source_branch = self.source
373
559
result.target_branch = self.target
374
interrepo = repository.InterRepository.get(self.source.repository,
375
self.target.repository)
376
560
result.old_revid = self.target.last_revision()
561
refs, stop_revision = self.update_refs(stop_revision)
562
self.target.generate_revision_history(stop_revision, result.old_revid)
563
self.update_tags(refs)
564
result.new_revid = self.target.last_revision()
568
class InterToGitBranch(branch.InterBranch):
569
"""InterBranch implementation that pulls from Git into bzr."""
572
def _get_branch_formats_to_test():
576
def is_compatible(self, source, target):
577
return (not isinstance(source, GitBranch) and
578
isinstance(target, GitBranch))
580
def update_revisions(self, *args, **kwargs):
581
raise NoPushSupport()
583
def push(self, overwrite=True, stop_revision=None,
584
_override_hook_source_branch=None):
585
raise NoPushSupport()
587
def lossy_push(self, stop_revision=None):
588
from dulwich.protocol import ZERO_SHA
589
result = GitBranchPushResult()
590
result.source_branch = self.source
591
result.target_branch = self.target
377
592
if stop_revision is None:
378
593
stop_revision = self.source.last_revision()
379
interrepo.fetch(revision_id=stop_revision)
380
self.target.generate_revision_history(stop_revision, result.old_revid)
381
result.new_revid = self.target.last_revision()
594
# FIXME: Check for diverged branches
595
refs = { self.target.ref: stop_revision }
596
for name, revid in self.source.tags.get_tag_dict().iteritems():
597
if self.source.repository.has_revision(revid):
598
refs[tag_name_to_ref(name)] = revid
599
revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
600
self.source.repository, refs)
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])
605
result.revidmap = revidmap
385
609
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
610
branch.InterBranch.register_optimiser(InterFromGitBranch)
611
branch.InterBranch.register_optimiser(InterToGitBranch)
612
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)