61
167
def get_format_description(self):
62
168
return 'Git Branch'
170
def network_name(self):
64
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()
180
def make_tags(self, branch):
181
if getattr(branch.repository, "get_refs", None) is not None:
182
from bzrlib.plugins.git.remote import RemoteGitTagDict
183
return RemoteGitTagDict(branch)
185
return LocalGitTagDict(branch)
68
188
class GitBranch(ForeignBranch):
69
189
"""An adapter to git repositories for bzr Branch objects."""
71
def __init__(self, bzrdir, repository, name, head, lockfiles):
191
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
72
192
self.repository = repository
73
super(GitBranch, self).__init__(default_mapping)
193
self._format = GitBranchFormat()
74
194
self.control_files = lockfiles
75
195
self.bzrdir = bzrdir
78
self.base = bzrdir.transport.base
79
self._format = GitBranchFormat()
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)
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")
217
def _get_nick(self, local=False, possible_master_transports=None):
218
"""Find the nick name for this branch.
224
def _set_nick(self, nick):
225
raise NotImplementedError
227
nick = property(_get_nick, _set_nick)
230
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
233
def generate_revision_history(self, revid, old_revid=None):
234
# FIXME: Check that old_revid is in the ancestry of revid
235
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
236
self._set_head(newhead)
81
238
def lock_write(self):
82
239
self.control_files.lock_write()
84
241
def get_stacked_on_url(self):
85
242
# Git doesn't do stacking (yet...)
243
raise errors.UnstackableBranchFormat(self._format, self.base)
88
245
def get_parent(self):
89
246
"""See Branch.get_parent()."""
247
# FIXME: Set "origin" url from .git/config ?
250
def set_parent(self, url):
251
# FIXME: Set "origin" url in .git/config ?
92
254
def lock_read(self):
93
255
self.control_files.lock_read()
258
return self.control_files.is_locked()
96
261
self.control_files.unlock()
98
263
def get_physical_lock_status(self):
102
class LocalGitBranch(GitBranch):
105
267
def last_revision(self):
106
268
# perhaps should escape this ?
108
270
return revision.NULL_REVISION
109
271
return self.mapping.revision_id_foreign_to_bzr(self.head)
111
def _make_tags(self):
112
return GitTagDict(self)
273
def _basic_push(self, target, overwrite=False, stop_revision=None):
274
return branch.InterBranch.get(self, target)._basic_push(
275
overwrite, stop_revision)
278
class LocalGitBranch(GitBranch):
279
"""A local Git branch."""
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)
287
def create_checkout(self, to_location, revision_id=None, lightweight=False,
288
accelerator_tree=None, hardlink=False):
290
t = transport.get_transport(to_location)
292
format = self._get_checkout_format()
293
checkout = format.initialize_on_transport(t)
294
from_branch = branch.BranchReferenceFormat().initialize(checkout,
296
tree = checkout.create_workingtree(revision_id,
297
from_branch=from_branch, hardlink=hardlink)
300
return self._create_heavyweight_checkout(to_location, revision_id,
303
def _create_heavyweight_checkout(self, to_location, revision_id=None,
305
"""Create a new heavyweight checkout of this branch.
307
:param to_location: URL of location to create the new checkout in.
308
:param revision_id: Revision that should be the tip of the checkout.
309
:param hardlink: Whether to hardlink
310
:return: WorkingTree object of checkout.
312
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
313
to_location, force_new_tree=False, format=get_rich_root_format())
314
checkout = checkout_branch.bzrdir
315
checkout_branch.bind(self)
316
# pull up to the specified revision_id to set the initial
317
# branch tip correctly, and seed it with history.
318
checkout_branch.pull(self, stop_revision=revision_id)
319
return checkout.create_workingtree(revision_id, hardlink=hardlink)
114
321
def _gen_revision_history(self):
115
322
if self.head is None:
117
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
324
ret = list(self.repository.iter_reverse_revision_history(
325
self.last_revision()))
331
return self.repository._git.ref(self.ref)
335
def set_last_revision_info(self, revno, revid):
336
self.set_last_revision(revid)
338
def set_last_revision(self, revid):
339
(newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
343
def _set_head(self, value):
345
self.repository._git.refs[self.ref] = self._head
346
self._clear_cached_state()
348
head = property(_get_head, _set_head)
121
350
def get_config(self):
122
351
return GitBranchConfig(self)
129
358
def set_push_location(self, location):
130
359
"""See Branch.set_push_location."""
131
360
self.get_config().set_user_option('push_location', location,
361
store=config.STORE_LOCATION)
134
363
def supports_tags(self):
137
def sprout(self, to_bzrdir, revision_id=None):
138
"""See Branch.sprout()."""
139
result = to_bzrdir.create_branch()
140
self.copy_content_into(result, revision_id=revision_id)
141
result.set_parent(self.bzrdir.root_transport.base)
367
class GitBranchPullResult(branch.PullResult):
369
def report(self, to_file):
371
if self.old_revid == self.new_revid:
372
to_file.write('No revisions to pull.\n')
373
elif self.new_git_head is not None:
374
to_file.write('Now on revision %d (git sha: %s).\n' %
375
(self.new_revno, self.new_git_head))
377
to_file.write('Now on revision %d.\n' % (self.new_revno,))
378
self._show_tag_conficts(to_file)
381
class GitBranchPushResult(branch.BranchPushResult):
383
def _lookup_revno(self, revid):
384
assert isinstance(revid, str), "was %r" % revid
385
# Try in source branch first, it'll be faster
387
return self.source_branch.revision_id_to_revno(revid)
388
except errors.NoSuchRevision:
389
# FIXME: Check using graph.find_distance_to_null() ?
390
return self.target_branch.revision_id_to_revno(revid)
394
return self._lookup_revno(self.old_revid)
398
return self._lookup_revno(self.new_revid)
401
class InterFromGitBranch(branch.GenericInterBranch):
402
"""InterBranch implementation that pulls from Git into bzr."""
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)
424
def determine_wants(heads):
425
if not self.source.name in heads:
426
raise NoSuchRef(self.source.name, heads.keys())
427
if stop_revision is not None:
428
self._last_revid = stop_revision
429
head, mapping = self.source.repository.lookup_bzr_revision_id(
432
head = heads[self.source.name]
433
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
435
if self.target.repository.has_revision(self._last_revid):
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)
445
prev_last_revid = None
447
prev_last_revid = self.target.last_revision()
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)
457
def pull(self, overwrite=False, stop_revision=None,
458
possible_transports=None, _hook_master=None, run_hooks=True,
459
_override_hook_target=None, local=False, limit=None):
462
:param _hook_master: Private parameter - set the branch to
463
be supplied as the master to pull hooks.
464
:param run_hooks: Private parameter - if false, this branch
465
is being called because it's the master of the primary branch,
466
so it should not run its hooks.
467
:param _override_hook_target: Private parameter - set the branch to be
468
supplied as the target_branch to pull hooks.
469
:param limit: Only import this many revisons. `None`, the default,
470
means import all revisions.
472
# This type of branch can't be bound.
474
raise errors.LocalRequiresBoundBranch()
475
result = GitBranchPullResult()
476
result.source_branch = self.source
477
if _override_hook_target is None:
478
result.target_branch = self.target
480
result.target_branch = _override_hook_target
481
self.source.lock_read()
483
# We assume that during 'pull' the target repository is closer than
485
graph = self.target.repository.get_graph(self.source.repository)
486
(result.old_revno, result.old_revid) = \
487
self.target.last_revision_info()
488
result.new_git_head = self._update_revisions(
489
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
490
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
492
(result.new_revno, result.new_revid) = \
493
self.target.last_revision_info()
495
result.master_branch = _hook_master
496
result.local_branch = result.target_branch
498
result.master_branch = result.target_branch
499
result.local_branch = None
501
for hook in branch.Branch.hooks['post_pull']:
507
def _basic_push(self, overwrite=False, stop_revision=None):
508
result = branch.BranchPushResult()
509
result.source_branch = self.source
510
result.target_branch = self.target
511
graph = self.target.repository.get_graph(self.source.repository)
512
result.old_revno, result.old_revid = self.target.last_revision_info()
513
result.new_git_head = self._update_revisions(
514
stop_revision, overwrite=overwrite, graph=graph)
515
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
517
result.new_revno, result.new_revid = self.target.last_revision_info()
521
class InterGitBranch(branch.GenericInterBranch):
522
"""InterBranch implementation that pulls between Git branches."""
525
class InterGitLocalRemoteBranch(InterGitBranch):
526
"""InterBranch that copies from a local to a remote git branch."""
529
def is_compatible(self, source, target):
530
from bzrlib.plugins.git.remote import RemoteGitBranch
531
return (isinstance(source, LocalGitBranch) and
532
isinstance(target, RemoteGitBranch))
534
def _basic_push(self, overwrite=False, stop_revision=None):
535
result = GitBranchPushResult()
536
result.source_branch = self.source
537
result.target_branch = self.target
538
if stop_revision is None:
539
stop_revision = self.source.last_revision()
540
# FIXME: Check for diverged branches
541
def get_changed_refs(old_refs):
542
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
543
refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
544
result.new_revid = stop_revision
545
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
546
refs["refs/tags/%s" % name] = sha
548
self.target.repository.send_pack(get_changed_refs,
549
self.source.repository._git.object_store.generate_pack_contents)
553
class InterGitRemoteLocalBranch(InterGitBranch):
554
"""InterBranch that copies from a remote to a local git branch."""
557
def is_compatible(self, source, target):
558
from bzrlib.plugins.git.remote import RemoteGitBranch
559
return (isinstance(source, RemoteGitBranch) and
560
isinstance(target, LocalGitBranch))
562
def _basic_push(self, overwrite=False, stop_revision=None):
563
result = branch.BranchPushResult()
564
result.source_branch = self.source
565
result.target_branch = self.target
566
result.old_revid = self.target.last_revision()
567
refs, stop_revision = self.update_refs(stop_revision)
568
self.target.generate_revision_history(stop_revision, result.old_revid)
569
self.update_tags(refs)
570
result.new_revid = self.target.last_revision()
573
def update_tags(self, refs):
574
for name, v in extract_tags(refs).iteritems():
575
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
576
self.target.tags.set_tag(name, revid)
578
def update_refs(self, stop_revision=None):
579
interrepo = repository.InterRepository.get(self.source.repository,
580
self.target.repository)
581
if stop_revision is None:
582
refs = interrepo.fetch_refs(branches=["HEAD"])
583
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
585
refs = interrepo.fetch_refs(revision_id=stop_revision)
586
return refs, stop_revision
588
def pull(self, stop_revision=None, overwrite=False,
589
possible_transports=None, run_hooks=True,local=False):
590
# This type of branch can't be bound.
592
raise errors.LocalRequiresBoundBranch()
593
result = GitPullResult()
594
result.source_branch = self.source
595
result.target_branch = self.target
596
result.old_revid = self.target.last_revision()
597
refs, stop_revision = self.update_refs(stop_revision)
598
self.target.generate_revision_history(stop_revision, result.old_revid)
599
self.update_tags(refs)
600
result.new_revid = self.target.last_revision()
604
class InterToGitBranch(branch.InterBranch):
605
"""InterBranch implementation that pulls from Git into bzr."""
608
def _get_branch_formats_to_test():
612
def is_compatible(self, source, target):
613
return (not isinstance(source, GitBranch) and
614
isinstance(target, GitBranch))
616
def update_revisions(self, *args, **kwargs):
617
raise NoPushSupport()
619
def push(self, overwrite=True, stop_revision=None,
620
_override_hook_source_branch=None):
621
raise NoPushSupport()
623
def lossy_push(self, stop_revision=None):
624
result = GitBranchPushResult()
625
result.source_branch = self.source
626
result.target_branch = self.target
628
result.old_revid = self.target.last_revision()
630
result.old_revid = revision.NULL_REVISION
631
if stop_revision is None:
632
stop_revision = self.source.last_revision()
633
# FIXME: Check for diverged branches
634
refs = { "refs/heads/master": stop_revision }
635
for name, revid in self.source.tags.get_tag_dict().iteritems():
636
if self.source.repository.has_revision(revid):
637
refs["refs/tags/%s" % name] = revid
638
revidmap, new_refs = self.target.repository.dfetch_refs(
639
self.source.repository, refs)
641
self.target.generate_revision_history(revidmap[stop_revision])
642
result.new_revid = revidmap[stop_revision]
644
result.new_revid = result.old_revid
645
result.revidmap = revidmap
649
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
650
branch.InterBranch.register_optimiser(InterFromGitBranch)
651
branch.InterBranch.register_optimiser(InterToGitBranch)
652
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)