64
129
def supports_tags(self):
132
def make_tags(self, branch):
133
if getattr(branch.repository, "get_refs", None) is not None:
134
from bzrlib.plugins.git.remote import RemoteGitTagDict
135
return RemoteGitTagDict(branch)
137
return LocalGitTagDict(branch)
68
140
class GitBranch(ForeignBranch):
69
141
"""An adapter to git repositories for bzr Branch objects."""
71
def __init__(self, bzrdir, repository, name, head, lockfiles):
143
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
72
144
self.repository = repository
73
super(GitBranch, self).__init__(default_mapping)
145
self._format = GitBranchFormat()
74
146
self.control_files = lockfiles
75
147
self.bzrdir = bzrdir
148
super(GitBranch, self).__init__(repository.get_mapping())
149
if tagsdict is not None:
150
self.tags = DictTagDict(self, tagsdict)
78
153
self.base = bzrdir.transport.base
79
self._format = GitBranchFormat()
155
def _get_checkout_format(self):
156
"""Return the most suitable metadir for a checkout of this branch.
157
Weaves are used if this branch's repository uses weaves.
159
return get_rich_root_format()
161
def get_child_submit_format(self):
162
"""Return the preferred format of submissions to this branch."""
163
ret = self.get_config().get_user_option("child_submit_format")
168
def _get_nick(self, local=False, possible_master_transports=None):
169
"""Find the nick name for this branch.
175
def _set_nick(self, nick):
176
raise NotImplementedError
178
nick = property(_get_nick, _set_nick)
181
return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
183
def generate_revision_history(self, revid, old_revid=None):
184
# FIXME: Check that old_revid is in the ancestry of revid
185
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
186
self._set_head(newhead)
81
188
def lock_write(self):
82
189
self.control_files.lock_write()
108
220
return revision.NULL_REVISION
109
221
return self.mapping.revision_id_foreign_to_bzr(self.head)
111
def _make_tags(self):
112
return GitTagDict(self)
223
def _basic_push(self, target, overwrite=False, stop_revision=None):
224
return branch.InterBranch.get(self, target)._basic_push(
225
overwrite, stop_revision)
228
class LocalGitBranch(GitBranch):
229
"""A local Git branch."""
231
def create_checkout(self, to_location, revision_id=None, lightweight=False,
232
accelerator_tree=None, hardlink=False):
234
t = transport.get_transport(to_location)
236
format = self._get_checkout_format()
237
checkout = format.initialize_on_transport(t)
238
from_branch = branch.BranchReferenceFormat().initialize(checkout,
240
tree = checkout.create_workingtree(revision_id,
241
from_branch=from_branch, hardlink=hardlink)
244
return self._create_heavyweight_checkout(to_location, revision_id,
247
def _create_heavyweight_checkout(self, to_location, revision_id=None,
249
"""Create a new heavyweight checkout of this branch.
251
:param to_location: URL of location to create the new checkout in.
252
:param revision_id: Revision that should be the tip of the checkout.
253
:param hardlink: Whether to hardlink
254
:return: WorkingTree object of checkout.
256
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
257
to_location, force_new_tree=False, format=get_rich_root_format())
258
checkout = checkout_branch.bzrdir
259
checkout_branch.bind(self)
260
# pull up to the specified revision_id to set the initial
261
# branch tip correctly, and seed it with history.
262
checkout_branch.pull(self, stop_revision=revision_id)
263
return checkout.create_workingtree(revision_id, hardlink=hardlink)
114
265
def _gen_revision_history(self):
115
266
if self.head is None:
117
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
268
ret = list(self.repository.iter_reverse_revision_history(
269
self.last_revision()))
275
return self.repository._git.ref(self.name)
279
def set_last_revision_info(self, revno, revid):
280
self.set_last_revision(revid)
282
def set_last_revision(self, revid):
283
(newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
287
def _set_head(self, value):
289
self.repository._git.refs[self.name] = self._head
290
self._clear_cached_state()
292
head = property(_get_head, _set_head)
121
294
def get_config(self):
122
295
return GitBranchConfig(self)
129
302
def set_push_location(self, location):
130
303
"""See Branch.set_push_location."""
131
304
self.get_config().set_user_option('push_location', location,
305
store=config.STORE_LOCATION)
134
307
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)
311
class GitBranchPullResult(branch.PullResult):
313
def report(self, to_file):
315
if self.old_revid == self.new_revid:
316
to_file.write('No revisions to pull.\n')
318
to_file.write('Now on revision %d (git sha: %s).\n' %
319
(self.new_revno, self.new_git_head))
320
self._show_tag_conficts(to_file)
323
class GitBranchPushResult(branch.BranchPushResult):
325
def _lookup_revno(self, revid):
326
assert isinstance(revid, str), "was %r" % revid
327
# Try in source branch first, it'll be faster
329
return self.source_branch.revision_id_to_revno(revid)
330
except errors.NoSuchRevision:
331
# FIXME: Check using graph.find_distance_to_null() ?
332
return self.target_branch.revision_id_to_revno(revid)
336
return self._lookup_revno(self.old_revid)
340
return self._lookup_revno(self.new_revid)
343
class InterFromGitBranch(branch.GenericInterBranch):
344
"""InterBranch implementation that pulls from Git into bzr."""
347
def is_compatible(self, source, target):
348
return (isinstance(source, GitBranch) and
349
not isinstance(target, GitBranch))
351
def update_revisions(self, stop_revision=None, overwrite=False,
353
"""See InterBranch.update_revisions()."""
354
interrepo = repository.InterRepository.get(self.source.repository,
355
self.target.repository)
357
self._last_revid = None
358
def determine_wants(heads):
359
if not self.source.name in heads:
360
raise NoSuchRef(self.source.name, heads.keys())
361
if stop_revision is not None:
362
self._last_revid = stop_revision
363
self._head, mapping = self.source.repository.lookup_git_revid(
366
self._head = heads[self.source.name]
368
self.source.mapping.revision_id_foreign_to_bzr(self._head)
369
if self.target.repository.has_revision(self._last_revid):
372
interrepo.fetch_objects(determine_wants, self.source.mapping)
374
prev_last_revid = None
376
prev_last_revid = self.target.last_revision()
377
self.target.generate_revision_history(self._last_revid, prev_last_revid)
379
def pull(self, overwrite=False, stop_revision=None,
380
possible_transports=None, _hook_master=None, run_hooks=True,
381
_override_hook_target=None, local=False):
384
:param _hook_master: Private parameter - set the branch to
385
be supplied as the master to pull hooks.
386
:param run_hooks: Private parameter - if false, this branch
387
is being called because it's the master of the primary branch,
388
so it should not run its hooks.
389
:param _override_hook_target: Private parameter - set the branch to be
390
supplied as the target_branch to pull hooks.
392
# This type of branch can't be bound.
394
raise errors.LocalRequiresBoundBranch()
395
result = GitBranchPullResult()
396
result.source_branch = self.source
397
if _override_hook_target is None:
398
result.target_branch = self.target
400
result.target_branch = _override_hook_target
401
self.source.lock_read()
403
# We assume that during 'pull' the target repository is closer than
405
graph = self.target.repository.get_graph(self.source.repository)
406
result.old_revno, result.old_revid = \
407
self.target.last_revision_info()
408
self.update_revisions(stop_revision, overwrite=overwrite,
410
result.new_git_head = self._head
411
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
413
result.new_revno, result.new_revid = self.target.last_revision_info()
415
result.master_branch = _hook_master
416
result.local_branch = result.target_branch
418
result.master_branch = result.target_branch
419
result.local_branch = None
421
for hook in branch.Branch.hooks['post_pull']:
427
def _basic_push(self, overwrite=False, stop_revision=None):
428
result = branch.BranchPushResult()
429
result.source_branch = self.source
430
result.target_branch = self.target
431
graph = self.target.repository.get_graph(self.source.repository)
432
result.old_revno, result.old_revid = self.target.last_revision_info()
433
self.update_revisions(stop_revision, overwrite=overwrite,
435
result.new_git_head = self._head
436
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
438
result.new_revno, result.new_revid = self.target.last_revision_info()
442
class InterGitBranch(branch.GenericInterBranch):
443
"""InterBranch implementation that pulls between Git branches."""
446
class InterGitLocalRemoteBranch(InterGitBranch):
447
"""InterBranch that copies from a local to a remote git branch."""
450
def is_compatible(self, source, target):
451
from bzrlib.plugins.git.remote import RemoteGitBranch
452
return (isinstance(source, LocalGitBranch) and
453
isinstance(target, RemoteGitBranch))
455
def _basic_push(self, overwrite=False, stop_revision=None):
456
result = GitBranchPushResult()
457
result.source_branch = self.source
458
result.target_branch = self.target
459
if stop_revision is None:
460
stop_revision = self.source.last_revision()
461
# FIXME: Check for diverged branches
462
def get_changed_refs(old_refs):
463
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
464
refs = { "refs/heads/master": self.source.repository.lookup_git_revid(stop_revision)[0] }
465
result.new_revid = stop_revision
466
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
467
refs["refs/tags/%s" % name] = sha
469
self.target.repository.send_pack(get_changed_refs,
470
self.source.repository._git.object_store.generate_pack_contents)
474
class InterGitRemoteLocalBranch(InterGitBranch):
475
"""InterBranch that copies from a remote to a local git branch."""
478
def is_compatible(self, source, target):
479
from bzrlib.plugins.git.remote import RemoteGitBranch
480
return (isinstance(source, RemoteGitBranch) and
481
isinstance(target, LocalGitBranch))
483
def _basic_push(self, overwrite=False, stop_revision=None):
484
result = branch.BranchPushResult()
485
result.source_branch = self.source
486
result.target_branch = self.target
487
result.old_revid = self.target.last_revision()
488
refs, stop_revision = self.update_refs(stop_revision)
489
self.target.generate_revision_history(stop_revision, result.old_revid)
490
self.update_tags(refs)
491
result.new_revid = self.target.last_revision()
494
def update_tags(self, refs):
495
for name, revid in extract_tags(refs, self.target.mapping).iteritems():
496
self.target.tags.set_tag(name, revid)
498
def update_refs(self, stop_revision=None):
499
interrepo = repository.InterRepository.get(self.source.repository,
500
self.target.repository)
501
if stop_revision is None:
502
refs = interrepo.fetch_refs(branches=["HEAD"])
503
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
505
refs = interrepo.fetch_refs(revision_id=stop_revision)
506
return refs, stop_revision
508
def pull(self, stop_revision=None, overwrite=False,
509
possible_transports=None, local=False):
510
# This type of branch can't be bound.
512
raise errors.LocalRequiresBoundBranch()
513
result = GitPullResult()
514
result.source_branch = self.source
515
result.target_branch = self.target
516
result.old_revid = self.target.last_revision()
517
refs, stop_revision = self.update_refs(stop_revision)
518
self.target.generate_revision_history(stop_revision, result.old_revid)
519
self.update_tags(refs)
520
result.new_revid = self.target.last_revision()
524
class InterToGitBranch(branch.InterBranch):
525
"""InterBranch implementation that pulls from Git into bzr."""
528
def is_compatible(self, source, target):
529
return (not isinstance(source, GitBranch) and
530
isinstance(target, GitBranch))
532
def update_revisions(self, *args, **kwargs):
533
raise NoPushSupport()
535
def push(self, overwrite=True, stop_revision=None,
536
_override_hook_source_branch=None):
537
raise NoPushSupport()
539
def lossy_push(self, stop_revision=None):
540
result = GitBranchPushResult()
541
result.source_branch = self.source
542
result.target_branch = self.target
544
result.old_revid = self.target.last_revision()
546
result.old_revid = revision.NULL_REVISION
547
if stop_revision is None:
548
stop_revision = self.source.last_revision()
549
# FIXME: Check for diverged branches
550
refs = { "refs/heads/master": stop_revision }
551
for name, revid in self.source.tags.get_tag_dict().iteritems():
552
if self.source.repository.has_revision(revid):
553
refs["refs/tags/%s" % name] = revid
554
revidmap, new_refs = self.target.repository.dfetch_refs(
555
self.source.repository, refs)
557
self.target.generate_revision_history(revidmap[stop_revision])
558
result.new_revid = revidmap[stop_revision]
560
result.new_revid = result.old_revid
561
result.revidmap = revidmap
565
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
566
branch.InterBranch.register_optimiser(InterFromGitBranch)
567
branch.InterBranch.register_optimiser(InterToGitBranch)
568
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)