75
130
def get_format_description(self):
76
131
return 'Git Branch'
133
def network_name(self):
78
136
def supports_tags(self):
139
def get_foreign_tests_branch_factory(self):
140
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
141
return ForeignTestsBranchFactory()
143
def make_tags(self, branch):
144
if getattr(branch.repository, "get_refs", None) is not None:
145
from bzrlib.plugins.git.remote import RemoteGitTagDict
146
return RemoteGitTagDict(branch)
148
return LocalGitTagDict(branch)
82
151
class GitBranch(ForeignBranch):
83
152
"""An adapter to git repositories for bzr Branch objects."""
85
def __init__(self, bzrdir, repository, name, head, lockfiles):
154
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
86
155
self.repository = repository
87
super(GitBranch, self).__init__(repository.get_mapping())
156
self._format = GitBranchFormat()
88
157
self.control_files = lockfiles
89
158
self.bzrdir = bzrdir
92
self.base = bzrdir.transport.base
93
self._format = GitBranchFormat()
95
def dpull(self, source, stop_revision=None):
96
if stop_revision is None:
97
stop_revision = source.last_revision()
98
# FIXME: Check for diverged branches
99
revidmap = self.repository.dfetch(source.repository, stop_revision)
100
self.head, self.mapping = self.mapping.revision_id_bzr_to_foreign(revidmap[stop_revision])
159
super(GitBranch, self).__init__(repository.get_mapping())
160
if tagsdict is not None:
161
self.tags = DictTagDict(self, tagsdict)
163
self.name = ref_to_branch_name(ref)
165
self.base = bzrdir.root_transport.base
167
def _get_checkout_format(self):
168
"""Return the most suitable metadir for a checkout of this branch.
169
Weaves are used if this branch's repository uses weaves.
171
return get_rich_root_format()
173
def get_child_submit_format(self):
174
"""Return the preferred format of submissions to this branch."""
175
ret = self.get_config().get_user_option("child_submit_format")
180
def _get_nick(self, local=False, possible_master_transports=None):
181
"""Find the nick name for this branch.
187
def _set_nick(self, nick):
188
raise NotImplementedError
190
nick = property(_get_nick, _set_nick)
193
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
196
def generate_revision_history(self, revid, old_revid=None):
197
# FIXME: Check that old_revid is in the ancestry of revid
198
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
199
self._set_head(newhead)
103
201
def lock_write(self):
104
202
self.control_files.lock_write()
106
204
def get_stacked_on_url(self):
107
205
# Git doesn't do stacking (yet...)
206
raise errors.UnstackableBranchFormat(self._format, self.base)
110
208
def get_parent(self):
111
209
"""See Branch.get_parent()."""
210
# FIXME: Set "origin" url from .git/config ?
114
213
def set_parent(self, url):
214
# FIXME: Set "origin" url in .git/config ?
117
217
def lock_read(self):
118
218
self.control_files.lock_read()
221
return self.control_files.is_locked()
120
223
def unlock(self):
121
224
self.control_files.unlock()
123
226
def get_physical_lock_status(self):
127
class LocalGitBranch(GitBranch):
130
230
def last_revision(self):
131
231
# perhaps should escape this ?
183
326
def supports_tags(self):
186
def sprout(self, to_bzrdir, revision_id=None):
187
"""See Branch.sprout()."""
188
result = to_bzrdir.create_branch()
189
self.copy_content_into(result, revision_id=revision_id)
190
result.set_parent(self.bzrdir.root_transport.base)
194
class InterGitGenericBranch(branch.InterBranch):
197
def is_compatible(self, source, target):
198
return isinstance(source, GitBranch)
200
def update_revisions(self, stop_revision=None, overwrite=False,
202
"""See InterBranch.update_revisions()."""
203
# TODO: stop_revision, overwrite
204
interrepo = repository.InterRepository.get(self.source.repository, self.target.repository)
205
self._last_revid = None
330
class GitBranchPullResult(branch.PullResult):
332
def report(self, to_file):
334
if self.old_revid == self.new_revid:
335
to_file.write('No revisions to pull.\n')
336
elif self.new_git_head is not None:
337
to_file.write('Now on revision %d (git sha: %s).\n' %
338
(self.new_revno, self.new_git_head))
340
to_file.write('Now on revision %d.\n' % (self.new_revno,))
341
self._show_tag_conficts(to_file)
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):
365
"""InterBranch implementation that pulls from Git into bzr."""
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)
206
387
def determine_wants(heads):
207
if not self.source.name in heads:
208
raise BzrError("No such remote branch '%s', found: %r" % (
209
self.source.name, heads.keys()))
210
head = heads[self.source.name]
211
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
388
if not self.source.ref in heads:
389
raise NoSuchRef(self.source.ref, heads.keys())
390
if stop_revision is not None:
391
self._last_revid = stop_revision
392
head, mapping = self.source.repository.lookup_bzr_revision_id(
395
head = heads[self.source.ref]
396
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
212
398
if self.target.repository.has_revision(self._last_revid):
215
interrepo.fetch_objects(determine_wants, self.source.mapping)
216
self.target.generate_revision_history(self._last_revid)
219
branch.InterBranch.register_optimiser(InterGitGenericBranch)
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)
408
prev_last_revid = None
410
prev_last_revid = self.target.last_revision()
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)
420
def pull(self, overwrite=False, stop_revision=None,
421
possible_transports=None, _hook_master=None, run_hooks=True,
422
_override_hook_target=None, local=False, limit=None):
425
:param _hook_master: Private parameter - set the branch to
426
be supplied as the master to pull hooks.
427
:param run_hooks: Private parameter - if false, this branch
428
is being called because it's the master of the primary branch,
429
so it should not run its hooks.
430
:param _override_hook_target: Private parameter - set the branch to be
431
supplied as the target_branch to pull hooks.
432
:param limit: Only import this many revisons. `None`, the default,
433
means import all revisions.
435
# This type of branch can't be bound.
437
raise errors.LocalRequiresBoundBranch()
438
result = GitBranchPullResult()
439
result.source_branch = self.source
440
if _override_hook_target is None:
441
result.target_branch = self.target
443
result.target_branch = _override_hook_target
444
self.source.lock_read()
446
# We assume that during 'pull' the target repository is closer than
448
graph = self.target.repository.get_graph(self.source.repository)
449
(result.old_revno, result.old_revid) = \
450
self.target.last_revision_info()
451
result.new_git_head = self._update_revisions(
452
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
453
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
455
(result.new_revno, result.new_revid) = \
456
self.target.last_revision_info()
458
result.master_branch = _hook_master
459
result.local_branch = result.target_branch
461
result.master_branch = result.target_branch
462
result.local_branch = None
464
for hook in branch.Branch.hooks['post_pull']:
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):
485
"""InterBranch implementation that pulls between Git branches."""
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
result = GitBranchPushResult()
499
result.source_branch = self.source
500
result.target_branch = self.target
501
if stop_revision is None:
502
stop_revision = self.source.last_revision()
503
# FIXME: Check for diverged branches
504
def get_changed_refs(old_refs):
505
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get(self.target.ref, "0" * 40))
506
refs = { self.target.ref: self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
507
result.new_revid = stop_revision
508
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
509
refs["refs/tags/%s" % name] = sha
511
self.target.repository.send_pack(get_changed_refs,
512
self.source.repository._git.object_store.generate_pack_contents)
516
class InterGitRemoteLocalBranch(InterGitBranch):
517
"""InterBranch that copies from a remote to a local git branch."""
520
def is_compatible(self, source, target):
521
from bzrlib.plugins.git.remote import RemoteGitBranch
522
return (isinstance(source, RemoteGitBranch) and
523
isinstance(target, LocalGitBranch))
525
def _basic_push(self, overwrite=False, stop_revision=None):
526
result = branch.BranchPushResult()
527
result.source_branch = self.source
528
result.target_branch = self.target
529
result.old_revid = self.target.last_revision()
530
refs, stop_revision = self.update_refs(stop_revision)
531
self.target.generate_revision_history(stop_revision, result.old_revid)
532
self.update_tags(refs)
533
result.new_revid = self.target.last_revision()
536
def update_tags(self, refs):
537
for name, v in extract_tags(refs).iteritems():
538
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
539
self.target.tags.set_tag(name, revid)
541
def update_refs(self, stop_revision=None):
542
interrepo = repository.InterRepository.get(self.source.repository,
543
self.target.repository)
544
if stop_revision is None:
545
refs = interrepo.fetch_refs(branches=["HEAD"])
546
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
548
refs = interrepo.fetch_refs(revision_id=stop_revision)
549
return refs, stop_revision
551
def pull(self, stop_revision=None, overwrite=False,
552
possible_transports=None, run_hooks=True,local=False):
553
# This type of branch can't be bound.
555
raise errors.LocalRequiresBoundBranch()
556
result = GitPullResult()
557
result.source_branch = self.source
558
result.target_branch = self.target
559
result.old_revid = self.target.last_revision()
560
refs, stop_revision = self.update_refs(stop_revision)
561
self.target.generate_revision_history(stop_revision, result.old_revid)
562
self.update_tags(refs)
563
result.new_revid = self.target.last_revision()
567
class InterToGitBranch(branch.InterBranch):
568
"""InterBranch implementation that pulls from Git into bzr."""
571
def _get_branch_formats_to_test():
575
def is_compatible(self, source, target):
576
return (not isinstance(source, GitBranch) and
577
isinstance(target, GitBranch))
579
def update_revisions(self, *args, **kwargs):
580
raise NoPushSupport()
582
def push(self, overwrite=True, stop_revision=None,
583
_override_hook_source_branch=None):
584
raise NoPushSupport()
586
def lossy_push(self, stop_revision=None):
587
result = GitBranchPushResult()
588
result.source_branch = self.source
589
result.target_branch = self.target
590
if stop_revision is None:
591
stop_revision = self.source.last_revision()
592
# FIXME: Check for diverged branches
593
refs = { self.target.ref: stop_revision }
594
for name, revid in self.source.tags.get_tag_dict().iteritems():
595
if self.source.repository.has_revision(revid):
596
refs["refs/tags/%s" % name] = revid
597
revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
598
self.source.repository, refs)
599
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
600
old_refs.get(self.target.ref, "0" * 40))
601
result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
602
new_refs[self.target.ref])
603
result.revidmap = revidmap
607
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
608
branch.InterBranch.register_optimiser(InterFromGitBranch)
609
branch.InterBranch.register_optimiser(InterToGitBranch)
610
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)