61
131
def get_format_description(self):
62
132
return 'Git Branch'
134
def network_name(self):
64
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()
144
def make_tags(self, branch):
145
if getattr(branch.repository, "get_refs", None) is not None:
146
from bzrlib.plugins.git.remote import RemoteGitTagDict
147
return RemoteGitTagDict(branch)
149
return LocalGitTagDict(branch)
68
152
class GitBranch(ForeignBranch):
69
153
"""An adapter to git repositories for bzr Branch objects."""
71
def __init__(self, bzrdir, repository, name, head, lockfiles):
155
def __init__(self, bzrdir, repository, ref, lockfiles, tagsdict=None):
72
156
self.repository = repository
73
super(GitBranch, self).__init__(default_mapping)
157
self._format = GitBranchFormat()
74
158
self.control_files = lockfiles
75
159
self.bzrdir = bzrdir
78
self.base = bzrdir.transport.base
79
self._format = GitBranchFormat()
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)
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")
181
def _get_nick(self, local=False, possible_master_transports=None):
182
"""Find the nick name for this branch.
188
def _set_nick(self, nick):
189
raise NotImplementedError
191
nick = property(_get_nick, _set_nick)
194
return "<%s(%r, %r)>" % (self.__class__.__name__, self.repository.base,
197
def generate_revision_history(self, revid, old_revid=None):
198
# FIXME: Check that old_revid is in the ancestry of revid
199
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
200
self._set_head(newhead)
81
202
def lock_write(self):
82
203
self.control_files.lock_write()
84
205
def get_stacked_on_url(self):
85
206
# Git doesn't do stacking (yet...)
207
raise errors.UnstackableBranchFormat(self._format, self.base)
88
209
def get_parent(self):
89
210
"""See Branch.get_parent()."""
211
# FIXME: Set "origin" url from .git/config ?
214
def set_parent(self, url):
215
# FIXME: Set "origin" url in .git/config ?
92
218
def lock_read(self):
93
219
self.control_files.lock_read()
222
return self.control_files.is_locked()
96
225
self.control_files.unlock()
98
227
def get_physical_lock_status(self):
102
class LocalGitBranch(GitBranch):
105
231
def last_revision(self):
106
232
# perhaps should escape this ?
108
234
return revision.NULL_REVISION
109
235
return self.mapping.revision_id_foreign_to_bzr(self.head)
111
def _make_tags(self):
112
return GitTagDict(self)
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)
251
def create_checkout(self, to_location, revision_id=None, lightweight=False,
252
accelerator_tree=None, hardlink=False):
254
t = transport.get_transport(to_location)
256
format = self._get_checkout_format()
257
checkout = format.initialize_on_transport(t)
258
from_branch = branch.BranchReferenceFormat().initialize(checkout,
260
tree = checkout.create_workingtree(revision_id,
261
from_branch=from_branch, hardlink=hardlink)
264
return self._create_heavyweight_checkout(to_location, revision_id,
267
def _create_heavyweight_checkout(self, to_location, revision_id=None,
269
"""Create a new heavyweight checkout of this branch.
271
:param to_location: URL of location to create the new checkout in.
272
:param revision_id: Revision that should be the tip of the checkout.
273
:param hardlink: Whether to hardlink
274
:return: WorkingTree object of checkout.
276
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
277
to_location, force_new_tree=False, format=get_rich_root_format())
278
checkout = checkout_branch.bzrdir
279
checkout_branch.bind(self)
280
# pull up to the specified revision_id to set the initial
281
# branch tip correctly, and seed it with history.
282
checkout_branch.pull(self, stop_revision=revision_id)
283
return checkout.create_workingtree(revision_id, hardlink=hardlink)
114
285
def _gen_revision_history(self):
115
286
if self.head is None:
117
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
288
ret = list(self.repository.iter_reverse_revision_history(
289
self.last_revision()))
295
return self.repository._git.ref(self.ref)
299
def set_last_revision_info(self, revno, revid):
300
self.set_last_revision(revid)
302
def set_last_revision(self, revid):
303
(newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
307
def _set_head(self, value):
309
self.repository._git.refs[self.ref] = self._head
310
self._clear_cached_state()
312
head = property(_get_head, _set_head)
121
314
def get_config(self):
122
315
return GitBranchConfig(self)
129
322
def set_push_location(self, location):
130
323
"""See Branch.set_push_location."""
131
324
self.get_config().set_user_option('push_location', location,
325
store=config.STORE_LOCATION)
134
327
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)
331
class GitBranchPullResult(branch.PullResult):
333
def report(self, to_file):
335
if self.old_revid == self.new_revid:
336
to_file.write('No revisions to pull.\n')
337
elif self.new_git_head is not None:
338
to_file.write('Now on revision %d (git sha: %s).\n' %
339
(self.new_revno, self.new_git_head))
341
to_file.write('Now on revision %d.\n' % (self.new_revno,))
342
self._show_tag_conficts(to_file)
345
class GitBranchPushResult(branch.BranchPushResult):
347
def _lookup_revno(self, revid):
348
assert isinstance(revid, str), "was %r" % revid
349
# Try in source branch first, it'll be faster
351
return self.source_branch.revision_id_to_revno(revid)
352
except errors.NoSuchRevision:
353
# FIXME: Check using graph.find_distance_to_null() ?
354
return self.target_branch.revision_id_to_revno(revid)
358
return self._lookup_revno(self.old_revid)
362
return self._lookup_revno(self.new_revid)
365
class InterFromGitBranch(branch.GenericInterBranch):
366
"""InterBranch implementation that pulls from Git into bzr."""
369
def _get_interrepo(self, source, target):
370
return repository.InterRepository.get(source.repository,
374
def is_compatible(cls, source, target):
375
return (isinstance(source, GitBranch) and
376
not isinstance(target, GitBranch) and
377
(getattr(cls._get_interrepo(source, target), "fetch_objects", None) is not None))
379
def _update_revisions(self, stop_revision=None, overwrite=False,
380
graph=None, limit=None):
381
"""Like InterBranch.update_revisions(), but with additions.
383
Compared to the `update_revisions()` below, this function takes a
384
`limit` argument that limits how many git commits will be converted
385
and returns the new git head.
387
interrepo = self._get_interrepo(self.source, self.target)
388
def determine_wants(heads):
389
if not self.source.ref in heads:
390
raise NoSuchRef(self.source.ref, heads.keys())
391
if stop_revision is not None:
392
self._last_revid = stop_revision
393
head, mapping = self.source.repository.lookup_bzr_revision_id(
396
head = heads[self.source.ref]
397
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(
399
if self.target.repository.has_revision(self._last_revid):
402
pack_hint, head = interrepo.fetch_objects(
403
determine_wants, self.source.mapping, limit=limit)
404
if pack_hint is not None and self.target.repository._format.pack_compresses:
405
self.target.repository.pack(hint=pack_hint)
407
self._last_revid = self.source.mapping.revision_id_foreign_to_bzr(head)
409
prev_last_revid = None
411
prev_last_revid = self.target.last_revision()
412
self.target.generate_revision_history(self._last_revid,
416
def update_revisions(self, stop_revision=None, overwrite=False,
418
"""See InterBranch.update_revisions()."""
419
self._update_revisions(stop_revision, overwrite, graph)
421
def pull(self, overwrite=False, stop_revision=None,
422
possible_transports=None, _hook_master=None, run_hooks=True,
423
_override_hook_target=None, local=False, limit=None):
426
:param _hook_master: Private parameter - set the branch to
427
be supplied as the master to pull hooks.
428
:param run_hooks: Private parameter - if false, this branch
429
is being called because it's the master of the primary branch,
430
so it should not run its hooks.
431
:param _override_hook_target: Private parameter - set the branch to be
432
supplied as the target_branch to pull hooks.
433
:param limit: Only import this many revisons. `None`, the default,
434
means import all revisions.
436
# This type of branch can't be bound.
438
raise errors.LocalRequiresBoundBranch()
439
result = GitBranchPullResult()
440
result.source_branch = self.source
441
if _override_hook_target is None:
442
result.target_branch = self.target
444
result.target_branch = _override_hook_target
445
self.source.lock_read()
447
# We assume that during 'pull' the target repository is closer than
449
graph = self.target.repository.get_graph(self.source.repository)
450
(result.old_revno, result.old_revid) = \
451
self.target.last_revision_info()
452
result.new_git_head = self._update_revisions(
453
stop_revision, overwrite=overwrite, graph=graph, limit=limit)
454
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
456
(result.new_revno, result.new_revid) = \
457
self.target.last_revision_info()
459
result.master_branch = _hook_master
460
result.local_branch = result.target_branch
462
result.master_branch = result.target_branch
463
result.local_branch = None
465
for hook in branch.Branch.hooks['post_pull']:
471
def _basic_push(self, overwrite=False, stop_revision=None):
472
result = branch.BranchPushResult()
473
result.source_branch = self.source
474
result.target_branch = self.target
475
graph = self.target.repository.get_graph(self.source.repository)
476
result.old_revno, result.old_revid = self.target.last_revision_info()
477
result.new_git_head = self._update_revisions(
478
stop_revision, overwrite=overwrite, graph=graph)
479
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
481
result.new_revno, result.new_revid = self.target.last_revision_info()
485
class InterGitBranch(branch.GenericInterBranch):
486
"""InterBranch implementation that pulls between Git branches."""
489
class InterGitLocalRemoteBranch(InterGitBranch):
490
"""InterBranch that copies from a local to a remote git branch."""
493
def is_compatible(self, source, target):
494
from bzrlib.plugins.git.remote import RemoteGitBranch
495
return (isinstance(source, LocalGitBranch) and
496
isinstance(target, RemoteGitBranch))
498
def _basic_push(self, overwrite=False, stop_revision=None):
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, "0" * 40))
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
524
isinstance(target, LocalGitBranch))
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()
557
result = GitPullResult()
558
result.source_branch = self.source
559
result.target_branch = self.target
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
result = GitBranchPushResult()
589
result.source_branch = self.source
590
result.target_branch = self.target
591
if stop_revision is None:
592
stop_revision = self.source.last_revision()
593
# FIXME: Check for diverged branches
594
refs = { self.target.ref: stop_revision }
595
for name, revid in self.source.tags.get_tag_dict().iteritems():
596
if self.source.repository.has_revision(revid):
597
refs[tag_name_to_ref(name)] = revid
598
revidmap, old_refs, new_refs = self.target.repository.dfetch_refs(
599
self.source.repository, refs)
600
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(
601
old_refs.get(self.target.ref, "0" * 40))
602
result.new_revid = self.target.mapping.revision_id_foreign_to_bzr(
603
new_refs[self.target.ref])
604
result.revidmap = revidmap
608
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
609
branch.InterBranch.register_optimiser(InterFromGitBranch)
610
branch.InterBranch.register_optimiser(InterToGitBranch)
611
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)