61
126
def get_format_description(self):
62
127
return 'Git Branch'
129
def network_name(self):
64
132
def supports_tags(self):
135
def get_foreign_tests_branch_factory(self):
136
from bzrlib.plugins.git.tests.test_branch import ForeignTestsBranchFactory
137
return ForeignTestsBranchFactory()
139
def make_tags(self, branch):
140
if getattr(branch.repository, "get_refs", None) is not None:
141
from bzrlib.plugins.git.remote import RemoteGitTagDict
142
return RemoteGitTagDict(branch)
144
return LocalGitTagDict(branch)
68
147
class GitBranch(ForeignBranch):
69
148
"""An adapter to git repositories for bzr Branch objects."""
71
def __init__(self, bzrdir, repository, name, head, lockfiles):
150
def __init__(self, bzrdir, repository, name, lockfiles, tagsdict=None):
72
151
self.repository = repository
73
super(GitBranch, self).__init__(default_mapping)
152
self._format = GitBranchFormat()
74
153
self.control_files = lockfiles
75
154
self.bzrdir = bzrdir
155
super(GitBranch, self).__init__(repository.get_mapping())
156
if tagsdict is not None:
157
self.tags = DictTagDict(self, tagsdict)
78
self.base = bzrdir.transport.base
79
self._format = GitBranchFormat()
160
self.base = bzrdir.root_transport.base
162
def _get_checkout_format(self):
163
"""Return the most suitable metadir for a checkout of this branch.
164
Weaves are used if this branch's repository uses weaves.
166
return get_rich_root_format()
168
def get_child_submit_format(self):
169
"""Return the preferred format of submissions to this branch."""
170
ret = self.get_config().get_user_option("child_submit_format")
175
def _get_nick(self, local=False, possible_master_transports=None):
176
"""Find the nick name for this branch.
182
def _set_nick(self, nick):
183
raise NotImplementedError
185
nick = property(_get_nick, _set_nick)
188
return "%s(%r, %r)" % (self.__class__.__name__, self.repository.base, self.name)
190
def generate_revision_history(self, revid, old_revid=None):
191
# FIXME: Check that old_revid is in the ancestry of revid
192
newhead, self.mapping = self.mapping.revision_id_bzr_to_foreign(revid)
193
self._set_head(newhead)
81
195
def lock_write(self):
82
196
self.control_files.lock_write()
84
198
def get_stacked_on_url(self):
85
199
# Git doesn't do stacking (yet...)
200
raise errors.UnstackableBranchFormat(self._format, self.base)
88
202
def get_parent(self):
89
203
"""See Branch.get_parent()."""
204
# FIXME: Set "origin" url from .git/config ?
207
def set_parent(self, url):
208
# FIXME: Set "origin" url in .git/config ?
92
211
def lock_read(self):
93
212
self.control_files.lock_read()
215
return self.control_files.is_locked()
96
218
self.control_files.unlock()
98
220
def get_physical_lock_status(self):
102
class LocalGitBranch(GitBranch):
105
224
def last_revision(self):
106
225
# perhaps should escape this ?
108
227
return revision.NULL_REVISION
109
228
return self.mapping.revision_id_foreign_to_bzr(self.head)
111
def _make_tags(self):
112
return GitTagDict(self)
230
def _basic_push(self, target, overwrite=False, stop_revision=None):
231
return branch.InterBranch.get(self, target)._basic_push(
232
overwrite, stop_revision)
235
class LocalGitBranch(GitBranch):
236
"""A local Git branch."""
238
def create_checkout(self, to_location, revision_id=None, lightweight=False,
239
accelerator_tree=None, hardlink=False):
241
t = transport.get_transport(to_location)
243
format = self._get_checkout_format()
244
checkout = format.initialize_on_transport(t)
245
from_branch = branch.BranchReferenceFormat().initialize(checkout,
247
tree = checkout.create_workingtree(revision_id,
248
from_branch=from_branch, hardlink=hardlink)
251
return self._create_heavyweight_checkout(to_location, revision_id,
254
def _create_heavyweight_checkout(self, to_location, revision_id=None,
256
"""Create a new heavyweight checkout of this branch.
258
:param to_location: URL of location to create the new checkout in.
259
:param revision_id: Revision that should be the tip of the checkout.
260
:param hardlink: Whether to hardlink
261
:return: WorkingTree object of checkout.
263
checkout_branch = bzrdir.BzrDir.create_branch_convenience(
264
to_location, force_new_tree=False, format=get_rich_root_format())
265
checkout = checkout_branch.bzrdir
266
checkout_branch.bind(self)
267
# pull up to the specified revision_id to set the initial
268
# branch tip correctly, and seed it with history.
269
checkout_branch.pull(self, stop_revision=revision_id)
270
return checkout.create_workingtree(revision_id, hardlink=hardlink)
114
272
def _gen_revision_history(self):
115
273
if self.head is None:
117
ret = list(self.repository.iter_reverse_revision_history(self.last_revision()))
275
ret = list(self.repository.iter_reverse_revision_history(
276
self.last_revision()))
282
return self.repository._git.ref(self.name)
286
def set_last_revision_info(self, revno, revid):
287
self.set_last_revision(revid)
289
def set_last_revision(self, revid):
290
(newhead, self.mapping) = self.mapping.revision_id_bzr_to_foreign(
294
def _set_head(self, value):
296
self.repository._git.refs[self.name] = self._head
297
self._clear_cached_state()
299
head = property(_get_head, _set_head)
121
301
def get_config(self):
122
302
return GitBranchConfig(self)
129
309
def set_push_location(self, location):
130
310
"""See Branch.set_push_location."""
131
311
self.get_config().set_user_option('push_location', location,
312
store=config.STORE_LOCATION)
134
314
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)
318
class GitBranchPullResult(branch.PullResult):
320
def report(self, to_file):
322
if self.old_revid == self.new_revid:
323
to_file.write('No revisions to pull.\n')
325
to_file.write('Now on revision %d (git sha: %s).\n' %
326
(self.new_revno, self.new_git_head))
327
self._show_tag_conficts(to_file)
330
class GitBranchPushResult(branch.BranchPushResult):
332
def _lookup_revno(self, revid):
333
assert isinstance(revid, str), "was %r" % revid
334
# Try in source branch first, it'll be faster
336
return self.source_branch.revision_id_to_revno(revid)
337
except errors.NoSuchRevision:
338
# FIXME: Check using graph.find_distance_to_null() ?
339
return self.target_branch.revision_id_to_revno(revid)
343
return self._lookup_revno(self.old_revid)
347
return self._lookup_revno(self.new_revid)
350
class InterFromGitBranch(branch.GenericInterBranch):
351
"""InterBranch implementation that pulls from Git into bzr."""
354
def is_compatible(self, source, target):
355
return (isinstance(source, GitBranch) and
356
not isinstance(target, GitBranch))
358
def update_revisions(self, stop_revision=None, overwrite=False,
360
"""See InterBranch.update_revisions()."""
361
interrepo = repository.InterRepository.get(self.source.repository,
362
self.target.repository)
364
self._last_revid = None
365
def determine_wants(heads):
366
if not self.source.name in heads:
367
raise NoSuchRef(self.source.name, heads.keys())
368
if stop_revision is not None:
369
self._last_revid = stop_revision
370
self._head, mapping = self.source.repository.lookup_bzr_revision_id(
373
self._head = heads[self.source.name]
375
self.source.mapping.revision_id_foreign_to_bzr(self._head)
376
if self.target.repository.has_revision(self._last_revid):
379
interrepo.fetch_objects(determine_wants, self.source.mapping)
381
prev_last_revid = None
383
prev_last_revid = self.target.last_revision()
384
self.target.generate_revision_history(self._last_revid, prev_last_revid)
386
def pull(self, overwrite=False, stop_revision=None,
387
possible_transports=None, _hook_master=None, run_hooks=True,
388
_override_hook_target=None, local=False):
391
:param _hook_master: Private parameter - set the branch to
392
be supplied as the master to pull hooks.
393
:param run_hooks: Private parameter - if false, this branch
394
is being called because it's the master of the primary branch,
395
so it should not run its hooks.
396
:param _override_hook_target: Private parameter - set the branch to be
397
supplied as the target_branch to pull hooks.
399
# This type of branch can't be bound.
401
raise errors.LocalRequiresBoundBranch()
402
result = GitBranchPullResult()
403
result.source_branch = self.source
404
if _override_hook_target is None:
405
result.target_branch = self.target
407
result.target_branch = _override_hook_target
408
self.source.lock_read()
410
# We assume that during 'pull' the target repository is closer than
412
graph = self.target.repository.get_graph(self.source.repository)
413
result.old_revno, result.old_revid = \
414
self.target.last_revision_info()
415
self.update_revisions(stop_revision, overwrite=overwrite,
417
result.new_git_head = self._head
418
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
420
result.new_revno, result.new_revid = self.target.last_revision_info()
422
result.master_branch = _hook_master
423
result.local_branch = result.target_branch
425
result.master_branch = result.target_branch
426
result.local_branch = None
428
for hook in branch.Branch.hooks['post_pull']:
434
def _basic_push(self, overwrite=False, stop_revision=None):
435
result = branch.BranchPushResult()
436
result.source_branch = self.source
437
result.target_branch = self.target
438
graph = self.target.repository.get_graph(self.source.repository)
439
result.old_revno, result.old_revid = self.target.last_revision_info()
440
self.update_revisions(stop_revision, overwrite=overwrite,
442
result.new_git_head = self._head
443
result.tag_conflicts = self.source.tags.merge_to(self.target.tags,
445
result.new_revno, result.new_revid = self.target.last_revision_info()
449
class InterGitBranch(branch.GenericInterBranch):
450
"""InterBranch implementation that pulls between Git branches."""
453
class InterGitLocalRemoteBranch(InterGitBranch):
454
"""InterBranch that copies from a local to a remote git branch."""
457
def is_compatible(self, source, target):
458
from bzrlib.plugins.git.remote import RemoteGitBranch
459
return (isinstance(source, LocalGitBranch) and
460
isinstance(target, RemoteGitBranch))
462
def _basic_push(self, overwrite=False, stop_revision=None):
463
result = GitBranchPushResult()
464
result.source_branch = self.source
465
result.target_branch = self.target
466
if stop_revision is None:
467
stop_revision = self.source.last_revision()
468
# FIXME: Check for diverged branches
469
def get_changed_refs(old_refs):
470
result.old_revid = self.target.mapping.revision_id_foreign_to_bzr(old_refs.get("refs/heads/master", "0" * 40))
471
refs = { "refs/heads/master": self.source.repository.lookup_bzr_revision_id(stop_revision)[0] }
472
result.new_revid = stop_revision
473
for name, sha in self.source.repository._git.refs.as_dict("refs/tags").iteritems():
474
refs["refs/tags/%s" % name] = sha
476
self.target.repository.send_pack(get_changed_refs,
477
self.source.repository._git.object_store.generate_pack_contents)
481
class InterGitRemoteLocalBranch(InterGitBranch):
482
"""InterBranch that copies from a remote to a local git branch."""
485
def is_compatible(self, source, target):
486
from bzrlib.plugins.git.remote import RemoteGitBranch
487
return (isinstance(source, RemoteGitBranch) and
488
isinstance(target, LocalGitBranch))
490
def _basic_push(self, overwrite=False, stop_revision=None):
491
result = branch.BranchPushResult()
492
result.source_branch = self.source
493
result.target_branch = self.target
494
result.old_revid = self.target.last_revision()
495
refs, stop_revision = self.update_refs(stop_revision)
496
self.target.generate_revision_history(stop_revision, result.old_revid)
497
self.update_tags(refs)
498
result.new_revid = self.target.last_revision()
501
def update_tags(self, refs):
502
for name, v in extract_tags(refs).iteritems():
503
revid = self.target.mapping.revision_id_foreign_to_bzr(v)
504
self.target.tags.set_tag(name, revid)
506
def update_refs(self, stop_revision=None):
507
interrepo = repository.InterRepository.get(self.source.repository,
508
self.target.repository)
509
if stop_revision is None:
510
refs = interrepo.fetch_refs(branches=["HEAD"])
511
stop_revision = self.target.mapping.revision_id_foreign_to_bzr(refs["HEAD"])
513
refs = interrepo.fetch_refs(revision_id=stop_revision)
514
return refs, stop_revision
516
def pull(self, stop_revision=None, overwrite=False,
517
possible_transports=None, local=False):
518
# This type of branch can't be bound.
520
raise errors.LocalRequiresBoundBranch()
521
result = GitPullResult()
522
result.source_branch = self.source
523
result.target_branch = self.target
524
result.old_revid = self.target.last_revision()
525
refs, stop_revision = self.update_refs(stop_revision)
526
self.target.generate_revision_history(stop_revision, result.old_revid)
527
self.update_tags(refs)
528
result.new_revid = self.target.last_revision()
532
class InterToGitBranch(branch.InterBranch):
533
"""InterBranch implementation that pulls from Git into bzr."""
536
def _get_branch_formats_to_test():
540
def is_compatible(self, source, target):
541
return (not isinstance(source, GitBranch) and
542
isinstance(target, GitBranch))
544
def update_revisions(self, *args, **kwargs):
545
raise NoPushSupport()
547
def push(self, overwrite=True, stop_revision=None,
548
_override_hook_source_branch=None):
549
raise NoPushSupport()
551
def lossy_push(self, stop_revision=None):
552
result = GitBranchPushResult()
553
result.source_branch = self.source
554
result.target_branch = self.target
556
result.old_revid = self.target.last_revision()
558
result.old_revid = revision.NULL_REVISION
559
if stop_revision is None:
560
stop_revision = self.source.last_revision()
561
# FIXME: Check for diverged branches
562
refs = { "refs/heads/master": stop_revision }
563
for name, revid in self.source.tags.get_tag_dict().iteritems():
564
if self.source.repository.has_revision(revid):
565
refs["refs/tags/%s" % name] = revid
566
revidmap, new_refs = self.target.repository.dfetch_refs(
567
self.source.repository, refs)
569
self.target.generate_revision_history(revidmap[stop_revision])
570
result.new_revid = revidmap[stop_revision]
572
result.new_revid = result.old_revid
573
result.revidmap = revidmap
577
branch.InterBranch.register_optimiser(InterGitRemoteLocalBranch)
578
branch.InterBranch.register_optimiser(InterFromGitBranch)
579
branch.InterBranch.register_optimiser(InterToGitBranch)
580
branch.InterBranch.register_optimiser(InterGitLocalRemoteBranch)